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 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 download(true);
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 long knownFileSize = message.getFileParams().size;
111 if (knownFileSize > 0 && interactive && method != Method.P1_S3) {
112 this.file.setExpectedSize(knownFileSize);
113 download(true);
114 } else {
115 checkFileSize(interactive);
116 }
117 } catch (MalformedURLException e) {
118 this.cancel();
119 }
120 }
121
122 private void download(boolean interactive) {
123 new Thread(new FileDownloader(interactive)).start();
124 }
125
126 private void checkFileSize(boolean interactive) {
127 new Thread(new FileSizeChecker(interactive)).start();
128 }
129
130 @Override
131 public void cancel() {
132 this.canceled = true;
133 mHttpConnectionManager.finishConnection(this);
134 if (message.isFileOrImage()) {
135 message.setTransferable(new TransferablePlaceholder(Transferable.STATUS_DELETED));
136 } else {
137 message.setTransferable(null);
138 }
139 mHttpConnectionManager.updateConversationUi(true);
140 }
141
142 private void finish() {
143 message.setTransferable(null);
144 mHttpConnectionManager.finishConnection(this);
145 boolean notify = acceptedAutomatically && !message.isRead();
146 if (message.getEncryption() == Message.ENCRYPTION_PGP) {
147 notify = message.getConversation().getAccount().getPgpDecryptionService().decrypt(message, notify);
148 }
149 mHttpConnectionManager.updateConversationUi(true);
150 final boolean notifyAfterScan = notify;
151 mXmppConnectionService.getFileBackend().updateMediaScanner(file, () -> {
152 if (notifyAfterScan) {
153 mXmppConnectionService.getNotificationService().push(message);
154 }
155 });
156 }
157
158 private void changeStatus(int status) {
159 this.mStatus = status;
160 mHttpConnectionManager.updateConversationUi(true);
161 }
162
163 private void showToastForException(Exception e) {
164 if (e instanceof java.net.UnknownHostException) {
165 mXmppConnectionService.showErrorToastInUi(R.string.download_failed_server_not_found);
166 } else if (e instanceof java.net.ConnectException) {
167 mXmppConnectionService.showErrorToastInUi(R.string.download_failed_could_not_connect);
168 } else if (e instanceof FileWriterException) {
169 mXmppConnectionService.showErrorToastInUi(R.string.download_failed_could_not_write_file);
170 } else if (!(e instanceof CancellationException)) {
171 mXmppConnectionService.showErrorToastInUi(R.string.download_failed_file_not_found);
172 }
173 }
174
175 private void updateProgress(long i) {
176 this.mProgress = (int) i;
177 mHttpConnectionManager.updateConversationUi(false);
178 }
179
180 @Override
181 public int getStatus() {
182 return this.mStatus;
183 }
184
185 @Override
186 public long getFileSize() {
187 if (this.file != null) {
188 return this.file.getExpectedSize();
189 } else {
190 return 0;
191 }
192 }
193
194 @Override
195 public int getProgress() {
196 return this.mProgress;
197 }
198
199 private class FileSizeChecker implements Runnable {
200
201 private final boolean interactive;
202
203 FileSizeChecker(boolean interactive) {
204 this.interactive = interactive;
205 }
206
207
208 @Override
209 public void run() {
210 if (mUrl.getProtocol().equalsIgnoreCase(P1S3UrlStreamHandler.PROTOCOL_NAME)) {
211 retrieveUrl();
212 } else {
213 check();
214 }
215 }
216
217 private void retrieveUrl() {
218 changeStatus(STATUS_CHECKING);
219 final Account account = message.getConversation().getAccount();
220 IqPacket request = mXmppConnectionService.getIqGenerator().requestP1S3Url(Jid.of(account.getJid().getDomain()), mUrl.getHost());
221 mXmppConnectionService.sendIqPacket(message.getConversation().getAccount(), request, (a, packet) -> {
222 if (packet.getType() == IqPacket.TYPE.RESULT) {
223 String download = packet.query().getAttribute("download");
224 if (download != null) {
225 try {
226 mUrl = new URL(download);
227 check();
228 return;
229 } catch (MalformedURLException e) {
230 //fallthrough
231 }
232 }
233 }
234 Log.d(Config.LOGTAG,"unable to retrieve actual download url");
235 retrieveFailed(null);
236 });
237 }
238
239 private void retrieveFailed(@Nullable Exception e) {
240 changeStatus(STATUS_OFFER_CHECK_FILESIZE);
241 if (interactive) {
242 if (e != null) {
243 showToastForException(e);
244 }
245 } else {
246 HttpDownloadConnection.this.acceptedAutomatically = false;
247 HttpDownloadConnection.this.mXmppConnectionService.getNotificationService().push(message);
248 }
249 cancel();
250 }
251
252 private void check() {
253 long size;
254 try {
255 size = retrieveFileSize();
256 } catch (Exception e) {
257 Log.d(Config.LOGTAG, "io exception in http file size checker: " + e.getMessage());
258 retrieveFailed(e);
259 return;
260 }
261 file.setExpectedSize(size);
262 message.resetFileParams();
263 if (mHttpConnectionManager.hasStoragePermission()
264 && size <= mHttpConnectionManager.getAutoAcceptFileSize()
265 && mXmppConnectionService.isDataSaverDisabled()) {
266 HttpDownloadConnection.this.acceptedAutomatically = true;
267 download(interactive);
268 } else {
269 changeStatus(STATUS_OFFER);
270 HttpDownloadConnection.this.acceptedAutomatically = false;
271 HttpDownloadConnection.this.mXmppConnectionService.getNotificationService().push(message);
272 }
273 }
274
275 private long retrieveFileSize() throws IOException {
276 try {
277 Log.d(Config.LOGTAG, "retrieve file size. interactive:" + String.valueOf(interactive));
278 changeStatus(STATUS_CHECKING);
279 HttpURLConnection connection;
280 if (mUseTor || message.getConversation().getAccount().isOnion()) {
281 connection = (HttpURLConnection) mUrl.openConnection(HttpConnectionManager.getProxy());
282 } else {
283 connection = (HttpURLConnection) mUrl.openConnection();
284 }
285 if (method == Method.P1_S3) {
286 connection.setRequestMethod("GET");
287 connection.addRequestProperty("Range","bytes=0-0");
288 } else {
289 connection.setRequestMethod("HEAD");
290 }
291 connection.setUseCaches(false);
292 Log.d(Config.LOGTAG, "url: " + connection.getURL().toString());
293 connection.setRequestProperty("User-Agent", mXmppConnectionService.getIqGenerator().getUserAgent());
294 if (connection instanceof HttpsURLConnection) {
295 mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, interactive);
296 }
297 connection.setConnectTimeout(Config.SOCKET_TIMEOUT * 1000);
298 connection.setReadTimeout(Config.SOCKET_TIMEOUT * 1000);
299 connection.connect();
300 String contentLength;
301 if (method == Method.P1_S3) {
302 String contentRange = connection.getHeaderField("Content-Range");
303 String[] contentRangeParts = contentRange == null ? new String[0] : contentRange.split("/");
304 if (contentRangeParts.length != 2) {
305 contentLength = null;
306 } else {
307 contentLength = contentRangeParts[1];
308 }
309 } else {
310 contentLength = connection.getHeaderField("Content-Length");
311 }
312 connection.disconnect();
313 if (contentLength == null) {
314 throw new IOException("no content-length found in HEAD response");
315 }
316 return Long.parseLong(contentLength, 10);
317 } catch (IOException e) {
318 Log.d(Config.LOGTAG, "io exception during HEAD " + e.getMessage());
319 throw e;
320 } catch (NumberFormatException e) {
321 throw new IOException();
322 }
323 }
324
325 }
326
327 private class FileDownloader implements Runnable {
328
329 private final boolean interactive;
330
331 private OutputStream os;
332
333 public FileDownloader(boolean interactive) {
334 this.interactive = interactive;
335 }
336
337 @Override
338 public void run() {
339 try {
340 changeStatus(STATUS_DOWNLOADING);
341 download();
342 updateImageBounds();
343 finish();
344 } catch (SSLHandshakeException e) {
345 changeStatus(STATUS_OFFER);
346 } catch (Exception e) {
347 if (interactive) {
348 showToastForException(e);
349 } else {
350 HttpDownloadConnection.this.acceptedAutomatically = false;
351 HttpDownloadConnection.this.mXmppConnectionService.getNotificationService().push(message);
352 }
353 cancel();
354 }
355 }
356
357 private void download() throws Exception {
358 InputStream is = null;
359 HttpURLConnection connection = null;
360 PowerManager.WakeLock wakeLock = mHttpConnectionManager.createWakeLock("http_download_" + message.getUuid());
361 try {
362 wakeLock.acquire();
363 if (mUseTor || message.getConversation().getAccount().isOnion()) {
364 connection = (HttpURLConnection) mUrl.openConnection(HttpConnectionManager.getProxy());
365 } else {
366 connection = (HttpURLConnection) mUrl.openConnection();
367 }
368 if (connection instanceof HttpsURLConnection) {
369 mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, interactive);
370 }
371 connection.setUseCaches(false);
372 connection.setRequestProperty("User-Agent", mXmppConnectionService.getIqGenerator().getUserAgent());
373 final boolean tryResume = file.exists() && file.getKey() == null && file.getSize() > 0;
374 long resumeSize = 0;
375 long expected = file.getExpectedSize();
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 message.setType(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}