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