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				if (mUseTor || message.getConversation().getAccount().isOnion()) {
280					connection = (HttpURLConnection) mUrl.openConnection(HttpConnectionManager.getProxy());
281				} else {
282					connection = (HttpURLConnection) mUrl.openConnection();
283				}
284				if (method == Method.P1_S3) {
285					connection.setRequestMethod("GET");
286					connection.addRequestProperty("Range","bytes=0-0");
287				} else {
288					connection.setRequestMethod("HEAD");
289				}
290				connection.setUseCaches(false);
291				Log.d(Config.LOGTAG, "url: " + connection.getURL().toString());
292				connection.setRequestProperty("User-Agent", mXmppConnectionService.getIqGenerator().getUserAgent());
293				if (connection instanceof HttpsURLConnection) {
294					mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, interactive);
295				}
296				connection.setConnectTimeout(Config.SOCKET_TIMEOUT * 1000);
297				connection.setReadTimeout(Config.SOCKET_TIMEOUT * 1000);
298				connection.connect();
299				String contentLength;
300				if (method == Method.P1_S3) {
301					String contentRange = connection.getHeaderField("Content-Range");
302					String[] contentRangeParts = contentRange == null ? new String[0] : contentRange.split("/");
303					if (contentRangeParts.length != 2) {
304						contentLength = null;
305					} else {
306						contentLength = contentRangeParts[1];
307					}
308				} else {
309					contentLength = connection.getHeaderField("Content-Length");
310				}
311				connection.disconnect();
312				if (contentLength == null) {
313					throw new IOException("no content-length found in HEAD response");
314				}
315				return Long.parseLong(contentLength, 10);
316			} catch (IOException e) {
317				Log.d(Config.LOGTAG, "io exception during HEAD " + e.getMessage());
318				throw e;
319			} catch (NumberFormatException e) {
320				throw new IOException();
321			}
322		}
323
324	}
325
326	private class FileDownloader implements Runnable {
327
328		private final boolean interactive;
329
330		private OutputStream os;
331
332		public FileDownloader(boolean interactive) {
333			this.interactive = interactive;
334		}
335
336		@Override
337		public void run() {
338			try {
339				changeStatus(STATUS_DOWNLOADING);
340				download();
341				updateImageBounds();
342				finish();
343			} catch (SSLHandshakeException e) {
344				changeStatus(STATUS_OFFER);
345			} catch (Exception e) {
346				if (interactive) {
347					showToastForException(e);
348				} else {
349					HttpDownloadConnection.this.acceptedAutomatically = false;
350					HttpDownloadConnection.this.mXmppConnectionService.getNotificationService().push(message);
351				}
352				cancel();
353			}
354		}
355
356		private void download() throws Exception {
357			InputStream is = null;
358			HttpURLConnection connection = null;
359			PowerManager.WakeLock wakeLock = mHttpConnectionManager.createWakeLock("http_download_" + message.getUuid());
360			try {
361				wakeLock.acquire();
362				if (mUseTor || message.getConversation().getAccount().isOnion()) {
363					connection = (HttpURLConnection) mUrl.openConnection(HttpConnectionManager.getProxy());
364				} else {
365					connection = (HttpURLConnection) mUrl.openConnection();
366				}
367				if (connection instanceof HttpsURLConnection) {
368					mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, interactive);
369				}
370				connection.setUseCaches(false);
371				connection.setRequestProperty("User-Agent", mXmppConnectionService.getIqGenerator().getUserAgent());
372				final long expected = file.getExpectedSize();
373				final boolean tryResume = file.exists() && file.getKey() == null && file.getSize() > 0 && file.getSize() < expected;
374				long resumeSize = 0;
375
376				if (tryResume) {
377					resumeSize = file.getSize();
378					Log.d(Config.LOGTAG, "http download trying resume after" + resumeSize + " of " + expected);
379					connection.setRequestProperty("Range", "bytes=" + resumeSize + "-");
380				}
381				connection.setConnectTimeout(Config.SOCKET_TIMEOUT * 1000);
382				connection.setReadTimeout(Config.SOCKET_TIMEOUT * 1000);
383				connection.connect();
384				is = new BufferedInputStream(connection.getInputStream());
385				final String contentRange = connection.getHeaderField("Content-Range");
386				boolean serverResumed = tryResume && contentRange != null && contentRange.startsWith("bytes " + resumeSize + "-");
387				long transmitted = 0;
388				if (tryResume && serverResumed) {
389					Log.d(Config.LOGTAG, "server resumed");
390					transmitted = file.getSize();
391					updateProgress(Math.round(((double) transmitted / expected) * 100));
392					os = AbstractConnectionManager.createAppendedOutputStream(file);
393					if (os == null) {
394						throw new FileWriterException();
395					}
396				} else {
397					long reportedContentLengthOnGet;
398					try {
399						reportedContentLengthOnGet = Long.parseLong(connection.getHeaderField("Content-Length"));
400					} catch (NumberFormatException | NullPointerException e) {
401						reportedContentLengthOnGet = 0;
402					}
403					if (expected != reportedContentLengthOnGet) {
404						Log.d(Config.LOGTAG, "content-length reported on GET (" + reportedContentLengthOnGet + ") did not match Content-Length reported on HEAD (" + expected + ")");
405					}
406					file.getParentFile().mkdirs();
407					if (!file.exists() && !file.createNewFile()) {
408						throw new FileWriterException();
409					}
410					os = AbstractConnectionManager.createOutputStream(file);
411				}
412				int count;
413				byte[] buffer = new byte[4096];
414				while ((count = is.read(buffer)) != -1) {
415					transmitted += count;
416					try {
417						os.write(buffer, 0, count);
418					} catch (IOException e) {
419						throw new FileWriterException();
420					}
421					updateProgress(Math.round(((double) transmitted / expected) * 100));
422					if (canceled) {
423						throw new CancellationException();
424					}
425				}
426				try {
427					os.flush();
428				} catch (IOException e) {
429					throw new FileWriterException();
430				}
431			} catch (CancellationException | IOException e) {
432				Log.d(Config.LOGTAG, "http download failed " + e.getMessage());
433				throw e;
434			} finally {
435				FileBackend.close(os);
436				FileBackend.close(is);
437				if (connection != null) {
438					connection.disconnect();
439				}
440				WakeLockHelper.release(wakeLock);
441			}
442		}
443
444		private void updateImageBounds() {
445			final boolean privateMessage = message.isPrivateMessage();
446			message.setType(privateMessage ? Message.TYPE_PRIVATE_FILE : Message.TYPE_FILE);
447			final URL url;
448			final String ref = mUrl.getRef();
449			if (method == Method.P1_S3) {
450				url = message.getFileParams().url;
451			} else if (ref != null && AesGcmURLStreamHandler.IV_KEY.matcher(ref).matches()) {
452				url = CryptoHelper.toAesGcmUrl(mUrl);
453			} else {
454				url = mUrl;
455			}
456			mXmppConnectionService.getFileBackend().updateFileParams(message, url);
457			mXmppConnectionService.updateMessage(message);
458		}
459
460	}
461}