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