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			final AbstractConnectionManager.Extension extension = AbstractConnectionManager.Extension.of(mUrl.getPath());
 81			if (VALID_CRYPTO_EXTENSIONS.contains(extension.main)) {
 82				this.message.setEncryption(Message.ENCRYPTION_PGP);
 83			} else if (message.getEncryption() != Message.ENCRYPTION_OTR
 84					&& message.getEncryption() != Message.ENCRYPTION_AXOLOTL) {
 85				this.message.setEncryption(Message.ENCRYPTION_NONE);
 86			}
 87			final String ext;
 88			if (VALID_CRYPTO_EXTENSIONS.contains(extension.main)) {
 89				ext = extension.secondary;
 90			} else {
 91				ext = extension.main;
 92			}
 93			message.setRelativeFilePath(message.getUuid() + (ext != null ? ("." + ext) : ""));
 94			this.file = mXmppConnectionService.getFileBackend().getFile(message, false);
 95			final String reference = mUrl.getRef();
 96			if (reference != null && AesGcmURLStreamHandler.IV_KEY.matcher(reference).matches()) {
 97				this.file.setKeyAndIv(CryptoHelper.hexToBytes(reference));
 98			}
 99
100			if (this.message.getEncryption() == Message.ENCRYPTION_AXOLOTL && this.file.getKey() == null) {
101				this.message.setEncryption(Message.ENCRYPTION_NONE);
102			}
103			method = mUrl.getProtocol().equalsIgnoreCase(P1S3UrlStreamHandler.PROTOCOL_NAME) ? Method.P1_S3 : Method.HTTP_UPLOAD;
104			long knownFileSize = message.getFileParams().size;
105			if (knownFileSize > 0 && interactive && method != Method.P1_S3) {
106				this.file.setExpectedSize(knownFileSize);
107				download(true);
108			} else {
109				checkFileSize(interactive);
110			}
111		} catch (MalformedURLException e) {
112			this.cancel();
113		}
114	}
115
116	private void download(boolean interactive) {
117		new Thread(new FileDownloader(interactive)).start();
118	}
119
120	private void checkFileSize(boolean interactive) {
121		new Thread(new FileSizeChecker(interactive)).start();
122	}
123
124	@Override
125	public void cancel() {
126		this.canceled = true;
127		mHttpConnectionManager.finishConnection(this);
128		message.setTransferable(null);
129		if (message.isFileOrImage()) {
130			message.setDeleted(true);
131		}
132		mHttpConnectionManager.updateConversationUi(true);
133	}
134
135	private void finish() {
136		message.setTransferable(null);
137		mHttpConnectionManager.finishConnection(this);
138		boolean notify = acceptedAutomatically && !message.isRead();
139		if (message.getEncryption() == Message.ENCRYPTION_PGP) {
140			notify = message.getConversation().getAccount().getPgpDecryptionService().decrypt(message, notify);
141		}
142		mHttpConnectionManager.updateConversationUi(true);
143		final boolean notifyAfterScan = notify;
144		mXmppConnectionService.getFileBackend().updateMediaScanner(file, () -> {
145			if (notifyAfterScan) {
146				mXmppConnectionService.getNotificationService().push(message);
147			}
148		});
149	}
150
151	private void changeStatus(int status) {
152		this.mStatus = status;
153		mHttpConnectionManager.updateConversationUi(true);
154	}
155
156	private void showToastForException(Exception e) {
157		if (e instanceof java.net.UnknownHostException) {
158			mXmppConnectionService.showErrorToastInUi(R.string.download_failed_server_not_found);
159		} else if (e instanceof java.net.ConnectException) {
160			mXmppConnectionService.showErrorToastInUi(R.string.download_failed_could_not_connect);
161		} else if (e instanceof FileWriterException) {
162			mXmppConnectionService.showErrorToastInUi(R.string.download_failed_could_not_write_file);
163		} else if (!(e instanceof CancellationException)) {
164			mXmppConnectionService.showErrorToastInUi(R.string.download_failed_file_not_found);
165		}
166	}
167
168	private void updateProgress(long i) {
169		this.mProgress = (int) i;
170		mHttpConnectionManager.updateConversationUi(false);
171	}
172
173	@Override
174	public int getStatus() {
175		return this.mStatus;
176	}
177
178	@Override
179	public long getFileSize() {
180		if (this.file != null) {
181			return this.file.getExpectedSize();
182		} else {
183			return 0;
184		}
185	}
186
187	@Override
188	public int getProgress() {
189		return this.mProgress;
190	}
191
192	public Message getMessage() {
193		return message;
194	}
195
196	private class FileSizeChecker implements Runnable {
197
198		private final boolean interactive;
199
200		FileSizeChecker(boolean interactive) {
201			this.interactive = interactive;
202		}
203
204
205		@Override
206		public void run() {
207			if (mUrl.getProtocol().equalsIgnoreCase(P1S3UrlStreamHandler.PROTOCOL_NAME)) {
208				retrieveUrl();
209			} else {
210				check();
211			}
212		}
213
214		private void retrieveUrl() {
215			changeStatus(STATUS_CHECKING);
216			final Account account = message.getConversation().getAccount();
217			IqPacket request = mXmppConnectionService.getIqGenerator().requestP1S3Url(Jid.of(account.getJid().getDomain()), mUrl.getHost());
218			mXmppConnectionService.sendIqPacket(message.getConversation().getAccount(), request, (a, packet) -> {
219				if (packet.getType() == IqPacket.TYPE.RESULT) {
220					String download = packet.query().getAttribute("download");
221					if (download != null) {
222						try {
223							mUrl = new URL(download);
224							check();
225							return;
226						} catch (MalformedURLException e) {
227							//fallthrough
228						}
229					}
230				}
231				Log.d(Config.LOGTAG,"unable to retrieve actual download url");
232				retrieveFailed(null);
233			});
234		}
235
236		private void retrieveFailed(@Nullable Exception e) {
237			changeStatus(STATUS_OFFER_CHECK_FILESIZE);
238			if (interactive) {
239				if (e != null) {
240					showToastForException(e);
241				}
242			} else {
243				HttpDownloadConnection.this.acceptedAutomatically = false;
244				HttpDownloadConnection.this.mXmppConnectionService.getNotificationService().push(message);
245			}
246			cancel();
247		}
248
249		private void check() {
250			long size;
251			try {
252				size = retrieveFileSize();
253			} catch (Exception e) {
254				Log.d(Config.LOGTAG, "io exception in http file size checker: " + e.getMessage());
255				retrieveFailed(e);
256				return;
257			}
258			file.setExpectedSize(size);
259			message.resetFileParams();
260			if (mHttpConnectionManager.hasStoragePermission()
261					&& size <= mHttpConnectionManager.getAutoAcceptFileSize()
262					&& mXmppConnectionService.isDataSaverDisabled()) {
263				HttpDownloadConnection.this.acceptedAutomatically = true;
264				download(interactive);
265			} else {
266				changeStatus(STATUS_OFFER);
267				HttpDownloadConnection.this.acceptedAutomatically = false;
268				HttpDownloadConnection.this.mXmppConnectionService.getNotificationService().push(message);
269			}
270		}
271
272		private long retrieveFileSize() throws IOException {
273			try {
274				Log.d(Config.LOGTAG, "retrieve file size. interactive:" + String.valueOf(interactive));
275				changeStatus(STATUS_CHECKING);
276				HttpURLConnection connection;
277				final String hostname = mUrl.getHost();
278				final boolean onion = hostname != null && hostname.endsWith(".onion");
279				if (mUseTor || message.getConversation().getAccount().isOnion() || onion) {
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}