HttpDownloadConnection.java

  1package eu.siacs.conversations.http;
  2
  3import android.util.Log;
  4
  5import androidx.annotation.Nullable;
  6
  7import com.google.common.base.Strings;
  8import com.google.common.io.ByteStreams;
  9import com.google.common.primitives.Longs;
 10
 11import java.io.FileInputStream;
 12import java.io.IOException;
 13import java.io.InputStream;
 14import java.io.OutputStream;
 15import java.util.Locale;
 16
 17import javax.net.ssl.SSLHandshakeException;
 18
 19import eu.siacs.conversations.Config;
 20import eu.siacs.conversations.R;
 21import eu.siacs.conversations.entities.DownloadableFile;
 22import eu.siacs.conversations.entities.Message;
 23import eu.siacs.conversations.entities.Transferable;
 24import eu.siacs.conversations.persistance.FileBackend;
 25import eu.siacs.conversations.services.AbstractConnectionManager;
 26import eu.siacs.conversations.services.XmppConnectionService;
 27import eu.siacs.conversations.utils.CryptoHelper;
 28import eu.siacs.conversations.utils.FileWriterException;
 29import eu.siacs.conversations.utils.MimeUtils;
 30import okhttp3.Call;
 31import okhttp3.HttpUrl;
 32import okhttp3.OkHttpClient;
 33import okhttp3.Request;
 34import okhttp3.Response;
 35
 36import static eu.siacs.conversations.http.HttpConnectionManager.EXECUTOR;
 37
 38public class HttpDownloadConnection implements Transferable {
 39
 40    private final Message message;
 41    private final HttpConnectionManager mHttpConnectionManager;
 42    private final XmppConnectionService mXmppConnectionService;
 43    private HttpUrl mUrl;
 44    private DownloadableFile file;
 45    private int mStatus = Transferable.STATUS_UNKNOWN;
 46    private boolean acceptedAutomatically = false;
 47    private int mProgress = 0;
 48    private Call mostRecentCall;
 49
 50    HttpDownloadConnection(Message message, HttpConnectionManager manager) {
 51        this.message = message;
 52        this.mHttpConnectionManager = manager;
 53        this.mXmppConnectionService = manager.getXmppConnectionService();
 54    }
 55
 56    @Override
 57    public boolean start() {
 58        if (mXmppConnectionService.hasInternetConnection()) {
 59            if (this.mStatus == STATUS_OFFER_CHECK_FILESIZE) {
 60                checkFileSize(true);
 61            } else {
 62                download(true);
 63            }
 64            return true;
 65        } else {
 66            return false;
 67        }
 68    }
 69
 70    public void init(boolean interactive) {
 71        if (message.isDeleted()) {
 72            if (message.getType() == Message.TYPE_PRIVATE_FILE) {
 73                message.setType(Message.TYPE_PRIVATE);
 74            } else if (message.isFileOrImage()) {
 75                message.setType(Message.TYPE_TEXT);
 76            }
 77            message.setOob(true);
 78            message.setDeleted(false);
 79            mXmppConnectionService.updateMessage(message);
 80        }
 81        this.message.setTransferable(this);
 82        try {
 83            final Message.FileParams fileParams = message.getFileParams();
 84            if (message.hasFileOnRemoteHost()) {
 85                mUrl = AesGcmURL.of(fileParams.url);
 86            } else if (message.isOOb() && fileParams.url != null && fileParams.size != null) {
 87                mUrl = AesGcmURL.of(fileParams.url);
 88            } else {
 89                mUrl = AesGcmURL.of(message.getBody().split("\n")[0]);
 90            }
 91            final AbstractConnectionManager.Extension extension = AbstractConnectionManager.Extension.of(mUrl.encodedPath());
 92            if (VALID_CRYPTO_EXTENSIONS.contains(extension.main)) {
 93                this.message.setEncryption(Message.ENCRYPTION_PGP);
 94            } else if (message.getEncryption() != Message.ENCRYPTION_OTR
 95                    && message.getEncryption() != Message.ENCRYPTION_AXOLOTL) {
 96                this.message.setEncryption(Message.ENCRYPTION_NONE);
 97            }
 98            final String ext = extension.getExtension();
 99            final String filename = Strings.isNullOrEmpty(ext) ? message.getUuid() : String.format("%s.%s", message.getUuid(), ext);
100            mXmppConnectionService.getFileBackend().setupRelativeFilePath(message, filename);
101            setupFile();
102            if (this.message.getEncryption() == Message.ENCRYPTION_AXOLOTL && this.file.getKey() == null) {
103                this.message.setEncryption(Message.ENCRYPTION_NONE);
104            }
105            final Long knownFileSize = message.getFileParams().size;
106            Log.d(Config.LOGTAG,"knownFileSize: "+knownFileSize+", body="+message.getBody());
107            if (knownFileSize != null && interactive) {
108                if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL
109                        && this.file.getKey() != null) {
110                    this.file.setExpectedSize(knownFileSize + 16);
111                } else {
112                    this.file.setExpectedSize(knownFileSize);
113                }
114                download(true);
115            } else {
116                checkFileSize(interactive);
117            }
118        } catch (final IllegalArgumentException e) {
119            this.cancel();
120        }
121    }
122
123    private void setupFile() {
124        final String reference = mUrl.fragment();
125        if (reference != null && AesGcmURL.IV_KEY.matcher(reference).matches()) {
126            this.file = new DownloadableFile(mXmppConnectionService.getCacheDir(), message.getUuid());
127            this.file.setKeyAndIv(CryptoHelper.hexToBytes(reference));
128            Log.d(Config.LOGTAG, "create temporary OMEMO encrypted file: " + this.file.getAbsolutePath() + "(" + message.getMimeType() + ")");
129        } else {
130            this.file = mXmppConnectionService.getFileBackend().getFile(message, false);
131        }
132    }
133
134    private void download(final boolean interactive) {
135        EXECUTOR.execute(new FileDownloader(interactive));
136    }
137
138    private void checkFileSize(final boolean interactive) {
139        EXECUTOR.execute(new FileSizeChecker(interactive));
140    }
141
142    @Override
143    public void cancel() {
144        final Call call = this.mostRecentCall;
145        if (call != null && !call.isCanceled()) {
146            call.cancel();
147        }
148        mHttpConnectionManager.finishConnection(this);
149        message.setTransferable(null);
150        if (message.isFileOrImage()) {
151            message.setDeleted(true);
152        }
153        mHttpConnectionManager.updateConversationUi(true);
154    }
155
156    private void decryptFile() throws IOException {
157        final DownloadableFile outputFile = mXmppConnectionService.getFileBackend().getFile(message, true);
158
159        if (outputFile.getParentFile().mkdirs()) {
160            Log.d(Config.LOGTAG, "created parent directories for " + outputFile.getAbsolutePath());
161        }
162
163        if (!outputFile.createNewFile()) {
164            Log.w(Config.LOGTAG, "unable to create output file " + outputFile.getAbsolutePath());
165        }
166
167        final InputStream is = new FileInputStream(this.file);
168
169        outputFile.setKey(this.file.getKey());
170        outputFile.setIv(this.file.getIv());
171        final OutputStream os = AbstractConnectionManager.createOutputStream(outputFile, false, true);
172
173        ByteStreams.copy(is, os);
174
175        FileBackend.close(is);
176        FileBackend.close(os);
177
178        if (!file.delete()) {
179            Log.w(Config.LOGTAG, "unable to delete temporary OMEMO encrypted file " + file.getAbsolutePath());
180        }
181    }
182
183    private void finish() {
184        message.setTransferable(null);
185        mHttpConnectionManager.finishConnection(this);
186        boolean notify = acceptedAutomatically && !message.isRead();
187        if (message.getEncryption() == Message.ENCRYPTION_PGP) {
188            notify = message.getConversation().getAccount().getPgpDecryptionService().decrypt(message, notify);
189        }
190        mHttpConnectionManager.updateConversationUi(true);
191        final boolean notifyAfterScan = notify;
192        final DownloadableFile file = mXmppConnectionService.getFileBackend().getFile(message, true);
193        mXmppConnectionService.getFileBackend().updateMediaScanner(file, () -> {
194            if (notifyAfterScan) {
195                mXmppConnectionService.getNotificationService().push(message);
196            }
197        });
198    }
199
200    private void decryptIfNeeded() throws IOException {
201        if (file.getKey() != null && file.getIv() != null) {
202            decryptFile();
203        }
204    }
205
206    private void changeStatus(int status) {
207        this.mStatus = status;
208        mHttpConnectionManager.updateConversationUi(true);
209    }
210
211    private void showToastForException(final Exception e) {
212        final Call call = mostRecentCall;
213        final boolean cancelled = call != null && call.isCanceled();
214        if (e == null || cancelled) {
215            return;
216        }
217        if (e instanceof java.net.UnknownHostException) {
218            mXmppConnectionService.showErrorToastInUi(R.string.download_failed_server_not_found);
219        } else if (e instanceof java.net.ConnectException) {
220            mXmppConnectionService.showErrorToastInUi(R.string.download_failed_could_not_connect);
221        } else if (e instanceof FileWriterException) {
222            mXmppConnectionService.showErrorToastInUi(R.string.download_failed_could_not_write_file);
223        } else if (e instanceof InvalidFileException) {
224            mXmppConnectionService.showErrorToastInUi(R.string.download_failed_invalid_file);
225        } else {
226            mXmppConnectionService.showErrorToastInUi(R.string.download_failed_file_not_found);
227        }
228    }
229
230    private void updateProgress(long i) {
231        this.mProgress = (int) i;
232        mHttpConnectionManager.updateConversationUi(false);
233    }
234
235    @Override
236    public int getStatus() {
237        return this.mStatus;
238    }
239
240    @Override
241    public Long getFileSize() {
242        if (this.file != null) {
243            return this.file.getExpectedSize();
244        } else {
245            return null;
246        }
247    }
248
249    @Override
250    public int getProgress() {
251        return this.mProgress;
252    }
253
254    public Message getMessage() {
255        return message;
256    }
257
258    private class FileSizeChecker implements Runnable {
259
260        private final boolean interactive;
261
262        FileSizeChecker(boolean interactive) {
263            this.interactive = interactive;
264        }
265
266
267        @Override
268        public void run() {
269            check();
270        }
271
272        private void retrieveFailed(@Nullable final Exception e) {
273            changeStatus(STATUS_OFFER_CHECK_FILESIZE);
274            if (interactive) {
275                showToastForException(e);
276            } else {
277                HttpDownloadConnection.this.acceptedAutomatically = false;
278                HttpDownloadConnection.this.mXmppConnectionService.getNotificationService().push(message);
279            }
280            cancel();
281        }
282
283        private void check() {
284            long size;
285            try {
286                size = retrieveFileSize();
287            } catch (final Exception e) {
288                Log.d(Config.LOGTAG, "io exception in http file size checker: " + e.getMessage());
289                retrieveFailed(e);
290                return;
291            }
292            final Message.FileParams fileParams = message.getFileParams();
293            FileBackend.updateFileParams(message, fileParams.url, size);
294            message.setOob(true);
295            mXmppConnectionService.databaseBackend.updateMessage(message, true);
296            file.setExpectedSize(size);
297            message.resetFileParams();
298            if (mHttpConnectionManager.hasStoragePermission()
299                    && size <= mHttpConnectionManager.getAutoAcceptFileSize()
300                    && mXmppConnectionService.isDataSaverDisabled()) {
301                HttpDownloadConnection.this.acceptedAutomatically = true;
302                download(interactive);
303            } else {
304                changeStatus(STATUS_OFFER);
305                HttpDownloadConnection.this.acceptedAutomatically = false;
306                HttpDownloadConnection.this.mXmppConnectionService.getNotificationService().push(message);
307            }
308        }
309
310        private long retrieveFileSize() throws IOException {
311            Log.d(Config.LOGTAG, "retrieve file size. interactive:" + interactive);
312            changeStatus(STATUS_CHECKING);
313            final OkHttpClient client = mHttpConnectionManager.buildHttpClient(
314                    mUrl,
315                    message.getConversation().getAccount(),
316                    interactive
317            );
318            final Request request = new Request.Builder()
319                    .url(URL.stripFragment(mUrl))
320                    .head()
321                    .build();
322            mostRecentCall = client.newCall(request);
323            try {
324                final Response response = mostRecentCall.execute();
325                throwOnInvalidCode(response);
326                final String contentLength = response.header("Content-Length");
327                final String contentType = response.header("Content-Type");
328                final AbstractConnectionManager.Extension extension = AbstractConnectionManager.Extension.of(mUrl.encodedPath());
329                if (Strings.isNullOrEmpty(extension.getExtension()) && contentType != null) {
330                    final String fileExtension = MimeUtils.guessExtensionFromMimeType(contentType);
331                    if (fileExtension != null) {
332                        mXmppConnectionService.getFileBackend().setupRelativeFilePath(message, String.format("%s.%s", message.getUuid(), fileExtension), contentType);
333                        Log.d(Config.LOGTAG, "rewriting name after not finding extension in url but in content type");
334                        setupFile();
335                    }
336                }
337                if (Strings.isNullOrEmpty(contentLength)) {
338                    throw new IOException("no content-length found in HEAD response");
339                }
340                final long size = Long.parseLong(contentLength, 10);
341                if (size < 0) {
342                    throw new IOException("Server reported negative file size");
343                }
344                return size;
345            } catch (IOException e) {
346                Log.d(Config.LOGTAG, "io exception during HEAD " + e.getMessage());
347                throw e;
348            } catch (NumberFormatException e) {
349                throw new IOException();
350            }
351        }
352
353    }
354
355    private class FileDownloader implements Runnable {
356
357        private final boolean interactive;
358
359        public FileDownloader(boolean interactive) {
360            this.interactive = interactive;
361        }
362
363        @Override
364        public void run() {
365            try {
366                changeStatus(STATUS_DOWNLOADING);
367                download();
368                decryptIfNeeded();
369                updateImageBounds();
370                finish();
371            } catch (final SSLHandshakeException e) {
372                changeStatus(STATUS_OFFER);
373            } catch (final Exception e) {
374                Log.d(Config.LOGTAG, message.getConversation().getAccount().getJid().asBareJid() + ": unable to download file", e);
375                if (interactive) {
376                    showToastForException(e);
377                } else {
378                    HttpDownloadConnection.this.acceptedAutomatically = false;
379                    HttpDownloadConnection.this.mXmppConnectionService.getNotificationService().push(message);
380                }
381                cancel();
382            }
383        }
384
385        private void download() throws Exception {
386            final OkHttpClient client = mHttpConnectionManager.buildHttpClient(
387                    mUrl,
388                    message.getConversation().getAccount(),
389                    interactive
390            );
391
392            final Request.Builder requestBuilder = new Request.Builder().url(URL.stripFragment(mUrl));
393
394            final long expected = file.getExpectedSize();
395            final boolean tryResume = file.exists() && file.getSize() > 0 && file.getSize() < expected;
396            final long resumeSize;
397            if (tryResume) {
398                resumeSize = file.getSize();
399                Log.d(Config.LOGTAG, "http download trying resume after " + resumeSize + " of " + expected);
400                requestBuilder.addHeader("Range", String.format(Locale.ENGLISH, "bytes=%d-", resumeSize));
401            } else {
402                resumeSize = 0;
403            }
404            final Request request = requestBuilder.build();
405            mostRecentCall = client.newCall(request);
406            final Response response = mostRecentCall.execute();
407            throwOnInvalidCode(response);
408            final String contentRange = response.header("Content-Range");
409            final boolean serverResumed = tryResume && contentRange != null && contentRange.startsWith("bytes " + resumeSize + "-");
410            final InputStream inputStream = response.body().byteStream();
411            final OutputStream outputStream;
412            long transmitted = 0;
413            if (tryResume && serverResumed) {
414                Log.d(Config.LOGTAG, "server resumed");
415                transmitted = file.getSize();
416                updateProgress(Math.round(((double) transmitted / expected) * 100));
417                outputStream = AbstractConnectionManager.createOutputStream(file, true, false);
418            } else {
419                final String contentLength = response.header("Content-Length");
420                final long size = Strings.isNullOrEmpty(contentLength) ? 0 : Longs.tryParse(contentLength);
421                if (expected != size) {
422                    Log.d(Config.LOGTAG, "content-length reported on GET (" + size + ") did not match Content-Length reported on HEAD (" + expected + ")");
423                }
424                file.getParentFile().mkdirs();
425                Log.d(Config.LOGTAG,"creating file: "+file.getAbsolutePath());
426                if (!file.exists() && !file.createNewFile()) {
427                    throw new FileWriterException(file);
428                }
429                outputStream = AbstractConnectionManager.createOutputStream(file, false, false);
430            }
431            int count;
432            final byte[] buffer = new byte[4096];
433            while ((count = inputStream.read(buffer)) != -1) {
434                transmitted += count;
435                try {
436                    outputStream.write(buffer, 0, count);
437                } catch (final IOException e) {
438                    throw new FileWriterException(file);
439                }
440                if (transmitted > expected) {
441                    throw new InvalidFileException(String.format("File exceeds expected size of %d", expected));
442                }
443                updateProgress(Math.round(((double) transmitted / expected) * 100));
444            }
445            outputStream.flush();
446        }
447
448        private void updateImageBounds() {
449            final boolean privateMessage = message.isPrivateMessage();
450            message.setType(privateMessage ? Message.TYPE_PRIVATE_FILE : Message.TYPE_FILE);
451            final String url;
452            final String ref = mUrl.fragment();
453            if (ref != null && AesGcmURL.IV_KEY.matcher(ref).matches()) {
454                url = AesGcmURL.toAesGcmUrl(mUrl);
455            } else {
456                url = mUrl.toString();
457            }
458            mXmppConnectionService.getFileBackend().updateFileParams(message, url);
459            mXmppConnectionService.updateMessage(message);
460        }
461
462    }
463
464    private static void throwOnInvalidCode(final Response response) throws IOException {
465        final int code = response.code();
466        if (code < 200 || code >= 300) {
467            throw new IOException(String.format(Locale.ENGLISH, "HTTP Status code was %d", code));
468        }
469    }
470
471    private static class InvalidFileException extends IOException {
472
473        private InvalidFileException(final String message) {
474            super(message);
475        }
476
477    }
478}