HttpDownloadConnection.java

  1package eu.siacs.conversations.http;
  2
  3import android.os.PowerManager;
  4import android.support.annotation.Nullable;
  5import android.util.Log;
  6
  7import java.io.BufferedInputStream;
  8import java.io.IOException;
  9import java.io.InputStream;
 10import java.io.OutputStream;
 11import java.net.HttpURLConnection;
 12import java.net.MalformedURLException;
 13import java.net.URL;
 14import java.util.concurrent.CancellationException;
 15
 16import javax.net.ssl.HttpsURLConnection;
 17import javax.net.ssl.SSLHandshakeException;
 18
 19import eu.siacs.conversations.Config;
 20import eu.siacs.conversations.R;
 21import eu.siacs.conversations.entities.Account;
 22import eu.siacs.conversations.entities.DownloadableFile;
 23import eu.siacs.conversations.entities.Message;
 24import eu.siacs.conversations.entities.Transferable;
 25import eu.siacs.conversations.entities.TransferablePlaceholder;
 26import eu.siacs.conversations.persistance.FileBackend;
 27import eu.siacs.conversations.services.AbstractConnectionManager;
 28import eu.siacs.conversations.services.XmppConnectionService;
 29import eu.siacs.conversations.utils.CryptoHelper;
 30import eu.siacs.conversations.utils.FileWriterException;
 31import eu.siacs.conversations.utils.WakeLockHelper;
 32import eu.siacs.conversations.xmpp.OnIqPacketReceived;
 33import eu.siacs.conversations.xmpp.stanzas.IqPacket;
 34import rocks.xmpp.addr.Jid;
 35
 36public class HttpDownloadConnection implements Transferable {
 37
 38	private HttpConnectionManager mHttpConnectionManager;
 39	private XmppConnectionService mXmppConnectionService;
 40
 41	private URL mUrl;
 42	private final Message message;
 43	private DownloadableFile file;
 44	private int mStatus = Transferable.STATUS_UNKNOWN;
 45	private boolean acceptedAutomatically = false;
 46	private int mProgress = 0;
 47	private final boolean mUseTor;
 48	private boolean canceled = false;
 49	private Method method = Method.HTTP_UPLOAD;
 50
 51	HttpDownloadConnection(Message message, HttpConnectionManager manager) {
 52		this.message = message;
 53		this.mHttpConnectionManager = manager;
 54		this.mXmppConnectionService = manager.getXmppConnectionService();
 55		this.mUseTor = mXmppConnectionService.useTorToConnect();
 56	}
 57
 58	@Override
 59	public boolean start() {
 60		if (mXmppConnectionService.hasInternetConnection()) {
 61			if (this.mStatus == STATUS_OFFER_CHECK_FILESIZE) {
 62				checkFileSize(true);
 63			} else {
 64				download(true);
 65			}
 66			return true;
 67		} else {
 68			return false;
 69		}
 70	}
 71
 72	public void init(boolean interactive) {
 73		this.message.setTransferable(this);
 74		try {
 75			if (message.hasFileOnRemoteHost()) {
 76				mUrl = CryptoHelper.toHttpsUrl(message.getFileParams().url);
 77			} else {
 78				mUrl = CryptoHelper.toHttpsUrl(new URL(message.getBody().split("\n")[0]));
 79			}
 80			String[] parts = mUrl.getPath().toLowerCase().split("\\.");
 81			String lastPart = parts.length >= 1 ? parts[parts.length - 1] : null;
 82			String secondToLast = parts.length >= 2 ? parts[parts.length - 2] : null;
 83			if ("pgp".equals(lastPart) || "gpg".equals(lastPart)) {
 84				this.message.setEncryption(Message.ENCRYPTION_PGP);
 85			} else if (message.getEncryption() != Message.ENCRYPTION_OTR
 86					&& message.getEncryption() != Message.ENCRYPTION_AXOLOTL) {
 87				this.message.setEncryption(Message.ENCRYPTION_NONE);
 88			}
 89			String extension;
 90			if (VALID_CRYPTO_EXTENSIONS.contains(lastPart)) {
 91				extension = secondToLast;
 92			} else {
 93				extension = lastPart;
 94			}
 95			message.setRelativeFilePath(message.getUuid() + (extension != null ? ("." + extension) : ""));
 96			this.file = mXmppConnectionService.getFileBackend().getFile(message, false);
 97			final String reference = mUrl.getRef();
 98			if (reference != null && AesGcmURLStreamHandler.IV_KEY.matcher(reference).matches()) {
 99				this.file.setKeyAndIv(CryptoHelper.hexToBytes(reference));
100			}
101
102			if (this.message.getEncryption() == Message.ENCRYPTION_AXOLOTL && this.file.getKey() == null) {
103				this.message.setEncryption(Message.ENCRYPTION_NONE);
104			}
105			method = mUrl.getProtocol().equalsIgnoreCase(P1S3UrlStreamHandler.PROTOCOL_NAME) ? Method.P1_S3 : Method.HTTP_UPLOAD;
106			long knownFileSize = message.getFileParams().size;
107			if (knownFileSize > 0 && interactive && method != Method.P1_S3) {
108				this.file.setExpectedSize(knownFileSize);
109				download(true);
110			} else {
111				checkFileSize(interactive);
112			}
113		} catch (MalformedURLException e) {
114			this.cancel();
115		}
116	}
117
118	private void download(boolean interactive) {
119		new Thread(new FileDownloader(interactive)).start();
120	}
121
122	private void checkFileSize(boolean interactive) {
123		new Thread(new FileSizeChecker(interactive)).start();
124	}
125
126	@Override
127	public void cancel() {
128		this.canceled = true;
129		mHttpConnectionManager.finishConnection(this);
130		message.setTransferable(null);
131		if (message.isFileOrImage()) {
132			message.setDeleted(true);
133		}
134		mHttpConnectionManager.updateConversationUi(true);
135	}
136
137	private void finish() {
138		message.setTransferable(null);
139		mHttpConnectionManager.finishConnection(this);
140		boolean notify = acceptedAutomatically && !message.isRead();
141		if (message.getEncryption() == Message.ENCRYPTION_PGP) {
142			notify = message.getConversation().getAccount().getPgpDecryptionService().decrypt(message, notify);
143		}
144		mHttpConnectionManager.updateConversationUi(true);
145		final boolean notifyAfterScan = notify;
146		mXmppConnectionService.getFileBackend().updateMediaScanner(file, () -> {
147			if (notifyAfterScan) {
148				mXmppConnectionService.getNotificationService().push(message);
149			}
150		});
151	}
152
153	private void changeStatus(int status) {
154		this.mStatus = status;
155		mHttpConnectionManager.updateConversationUi(true);
156	}
157
158	private void showToastForException(Exception e) {
159		if (e instanceof java.net.UnknownHostException) {
160			mXmppConnectionService.showErrorToastInUi(R.string.download_failed_server_not_found);
161		} else if (e instanceof java.net.ConnectException) {
162			mXmppConnectionService.showErrorToastInUi(R.string.download_failed_could_not_connect);
163		} else if (e instanceof FileWriterException) {
164			mXmppConnectionService.showErrorToastInUi(R.string.download_failed_could_not_write_file);
165		} else if (!(e instanceof CancellationException)) {
166			mXmppConnectionService.showErrorToastInUi(R.string.download_failed_file_not_found);
167		}
168	}
169
170	private void updateProgress(long i) {
171		this.mProgress = (int) i;
172		mHttpConnectionManager.updateConversationUi(false);
173	}
174
175	@Override
176	public int getStatus() {
177		return this.mStatus;
178	}
179
180	@Override
181	public long getFileSize() {
182		if (this.file != null) {
183			return this.file.getExpectedSize();
184		} else {
185			return 0;
186		}
187	}
188
189	@Override
190	public int getProgress() {
191		return this.mProgress;
192	}
193
194	public Message getMessage() {
195		return message;
196	}
197
198	private class FileSizeChecker implements Runnable {
199
200		private final boolean interactive;
201
202		FileSizeChecker(boolean interactive) {
203			this.interactive = interactive;
204		}
205
206
207		@Override
208		public void run() {
209			if (mUrl.getProtocol().equalsIgnoreCase(P1S3UrlStreamHandler.PROTOCOL_NAME)) {
210				retrieveUrl();
211			} else {
212				check();
213			}
214		}
215
216		private void retrieveUrl() {
217			changeStatus(STATUS_CHECKING);
218			final Account account = message.getConversation().getAccount();
219			IqPacket request = mXmppConnectionService.getIqGenerator().requestP1S3Url(Jid.of(account.getJid().getDomain()), mUrl.getHost());
220			mXmppConnectionService.sendIqPacket(message.getConversation().getAccount(), request, (a, packet) -> {
221				if (packet.getType() == IqPacket.TYPE.RESULT) {
222					String download = packet.query().getAttribute("download");
223					if (download != null) {
224						try {
225							mUrl = new URL(download);
226							check();
227							return;
228						} catch (MalformedURLException e) {
229							//fallthrough
230						}
231					}
232				}
233				Log.d(Config.LOGTAG,"unable to retrieve actual download url");
234				retrieveFailed(null);
235			});
236		}
237
238		private void retrieveFailed(@Nullable Exception e) {
239			changeStatus(STATUS_OFFER_CHECK_FILESIZE);
240			if (interactive) {
241				if (e != null) {
242					showToastForException(e);
243				}
244			} else {
245				HttpDownloadConnection.this.acceptedAutomatically = false;
246				HttpDownloadConnection.this.mXmppConnectionService.getNotificationService().push(message);
247			}
248			cancel();
249		}
250
251		private void check() {
252			long size;
253			try {
254				size = retrieveFileSize();
255			} catch (Exception e) {
256				Log.d(Config.LOGTAG, "io exception in http file size checker: " + e.getMessage());
257				retrieveFailed(e);
258				return;
259			}
260			file.setExpectedSize(size);
261			message.resetFileParams();
262			if (mHttpConnectionManager.hasStoragePermission()
263					&& size <= mHttpConnectionManager.getAutoAcceptFileSize()
264					&& mXmppConnectionService.isDataSaverDisabled()) {
265				HttpDownloadConnection.this.acceptedAutomatically = true;
266				download(interactive);
267			} else {
268				changeStatus(STATUS_OFFER);
269				HttpDownloadConnection.this.acceptedAutomatically = false;
270				HttpDownloadConnection.this.mXmppConnectionService.getNotificationService().push(message);
271			}
272		}
273
274		private long retrieveFileSize() throws IOException {
275			try {
276				Log.d(Config.LOGTAG, "retrieve file size. interactive:" + String.valueOf(interactive));
277				changeStatus(STATUS_CHECKING);
278				HttpURLConnection connection;
279				final String hostname = mUrl.getHost();
280				final boolean onion = hostname != null && hostname.endsWith(".onion");
281				if (mUseTor || message.getConversation().getAccount().isOnion() || onion) {
282					connection = (HttpURLConnection) mUrl.openConnection(HttpConnectionManager.getProxy());
283				} else {
284					connection = (HttpURLConnection) mUrl.openConnection();
285				}
286				if (method == Method.P1_S3) {
287					connection.setRequestMethod("GET");
288					connection.addRequestProperty("Range","bytes=0-0");
289				} else {
290					connection.setRequestMethod("HEAD");
291				}
292				connection.setUseCaches(false);
293				Log.d(Config.LOGTAG, "url: " + connection.getURL().toString());
294				connection.setRequestProperty("User-Agent", mXmppConnectionService.getIqGenerator().getUserAgent());
295				if (connection instanceof HttpsURLConnection) {
296					mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, interactive);
297				}
298				connection.setConnectTimeout(Config.SOCKET_TIMEOUT * 1000);
299				connection.setReadTimeout(Config.SOCKET_TIMEOUT * 1000);
300				connection.connect();
301				String contentLength;
302				if (method == Method.P1_S3) {
303					String contentRange = connection.getHeaderField("Content-Range");
304					String[] contentRangeParts = contentRange == null ? new String[0] : contentRange.split("/");
305					if (contentRangeParts.length != 2) {
306						contentLength = null;
307					} else {
308						contentLength = contentRangeParts[1];
309					}
310				} else {
311					contentLength = connection.getHeaderField("Content-Length");
312				}
313				connection.disconnect();
314				if (contentLength == null) {
315					throw new IOException("no content-length found in HEAD response");
316				}
317				return Long.parseLong(contentLength, 10);
318			} catch (IOException e) {
319				Log.d(Config.LOGTAG, "io exception during HEAD " + e.getMessage());
320				throw e;
321			} catch (NumberFormatException e) {
322				throw new IOException();
323			}
324		}
325
326	}
327
328	private class FileDownloader implements Runnable {
329
330		private final boolean interactive;
331
332		private OutputStream os;
333
334		public FileDownloader(boolean interactive) {
335			this.interactive = interactive;
336		}
337
338		@Override
339		public void run() {
340			try {
341				changeStatus(STATUS_DOWNLOADING);
342				download();
343				updateImageBounds();
344				finish();
345			} catch (SSLHandshakeException e) {
346				changeStatus(STATUS_OFFER);
347			} catch (Exception e) {
348				if (interactive) {
349					showToastForException(e);
350				} else {
351					HttpDownloadConnection.this.acceptedAutomatically = false;
352					HttpDownloadConnection.this.mXmppConnectionService.getNotificationService().push(message);
353				}
354				cancel();
355			}
356		}
357
358		private void download() throws Exception {
359			InputStream is = null;
360			HttpURLConnection connection = null;
361			PowerManager.WakeLock wakeLock = mHttpConnectionManager.createWakeLock("http_download_" + message.getUuid());
362			try {
363				wakeLock.acquire();
364				if (mUseTor || message.getConversation().getAccount().isOnion()) {
365					connection = (HttpURLConnection) mUrl.openConnection(HttpConnectionManager.getProxy());
366				} else {
367					connection = (HttpURLConnection) mUrl.openConnection();
368				}
369				if (connection instanceof HttpsURLConnection) {
370					mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, interactive);
371				}
372				connection.setUseCaches(false);
373				connection.setRequestProperty("User-Agent", mXmppConnectionService.getIqGenerator().getUserAgent());
374				final long expected = file.getExpectedSize();
375				final boolean tryResume = file.exists() && file.getKey() == null && file.getSize() > 0 && file.getSize() < expected;
376				long resumeSize = 0;
377
378				if (tryResume) {
379					resumeSize = file.getSize();
380					Log.d(Config.LOGTAG, "http download trying resume after" + resumeSize + " of " + expected);
381					connection.setRequestProperty("Range", "bytes=" + resumeSize + "-");
382				}
383				connection.setConnectTimeout(Config.SOCKET_TIMEOUT * 1000);
384				connection.setReadTimeout(Config.SOCKET_TIMEOUT * 1000);
385				connection.connect();
386				is = new BufferedInputStream(connection.getInputStream());
387				final String contentRange = connection.getHeaderField("Content-Range");
388				boolean serverResumed = tryResume && contentRange != null && contentRange.startsWith("bytes " + resumeSize + "-");
389				long transmitted = 0;
390				if (tryResume && serverResumed) {
391					Log.d(Config.LOGTAG, "server resumed");
392					transmitted = file.getSize();
393					updateProgress(Math.round(((double) transmitted / expected) * 100));
394					os = AbstractConnectionManager.createAppendedOutputStream(file);
395					if (os == null) {
396						throw new FileWriterException();
397					}
398				} else {
399					long reportedContentLengthOnGet;
400					try {
401						reportedContentLengthOnGet = Long.parseLong(connection.getHeaderField("Content-Length"));
402					} catch (NumberFormatException | NullPointerException e) {
403						reportedContentLengthOnGet = 0;
404					}
405					if (expected != reportedContentLengthOnGet) {
406						Log.d(Config.LOGTAG, "content-length reported on GET (" + reportedContentLengthOnGet + ") did not match Content-Length reported on HEAD (" + expected + ")");
407					}
408					file.getParentFile().mkdirs();
409					if (!file.exists() && !file.createNewFile()) {
410						throw new FileWriterException();
411					}
412					os = AbstractConnectionManager.createOutputStream(file);
413				}
414				int count;
415				byte[] buffer = new byte[4096];
416				while ((count = is.read(buffer)) != -1) {
417					transmitted += count;
418					try {
419						os.write(buffer, 0, count);
420					} catch (IOException e) {
421						throw new FileWriterException();
422					}
423					updateProgress(Math.round(((double) transmitted / expected) * 100));
424					if (canceled) {
425						throw new CancellationException();
426					}
427				}
428				try {
429					os.flush();
430				} catch (IOException e) {
431					throw new FileWriterException();
432				}
433			} catch (CancellationException | IOException e) {
434				Log.d(Config.LOGTAG, "http download failed " + e.getMessage());
435				throw e;
436			} finally {
437				FileBackend.close(os);
438				FileBackend.close(is);
439				if (connection != null) {
440					connection.disconnect();
441				}
442				WakeLockHelper.release(wakeLock);
443			}
444		}
445
446		private void updateImageBounds() {
447			final boolean privateMessage = message.isPrivateMessage();
448			message.setType(privateMessage ? Message.TYPE_PRIVATE_FILE : Message.TYPE_FILE);
449			final URL url;
450			final String ref = mUrl.getRef();
451			if (method == Method.P1_S3) {
452				url = message.getFileParams().url;
453			} else if (ref != null && AesGcmURLStreamHandler.IV_KEY.matcher(ref).matches()) {
454				url = CryptoHelper.toAesGcmUrl(mUrl);
455			} else {
456				url = mUrl;
457			}
458			mXmppConnectionService.getFileBackend().updateFileParams(message, url);
459			mXmppConnectionService.updateMessage(message);
460		}
461
462	}
463}