2014-10-13 23:06:45 +00:00
|
|
|
package eu.siacs.conversations.http;
|
|
|
|
|
2015-08-10 17:48:36 +00:00
|
|
|
import android.os.PowerManager;
|
2018-05-25 13:37:14 +00:00
|
|
|
import android.support.annotation.Nullable;
|
2015-07-10 11:28:50 +00:00
|
|
|
import android.util.Log;
|
2014-11-03 18:59:11 +00:00
|
|
|
|
2014-10-13 23:06:45 +00:00
|
|
|
import java.io.BufferedInputStream;
|
|
|
|
import java.io.IOException;
|
2015-08-10 10:15:14 +00:00
|
|
|
import java.io.InputStream;
|
2014-10-13 23:06:45 +00:00
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
2015-12-11 19:33:41 +00:00
|
|
|
import java.util.concurrent.CancellationException;
|
2014-10-13 23:06:45 +00:00
|
|
|
|
|
|
|
import javax.net.ssl.HttpsURLConnection;
|
2014-10-19 21:42:53 +00:00
|
|
|
import javax.net.ssl.SSLHandshakeException;
|
2014-10-13 23:06:45 +00:00
|
|
|
|
2014-11-14 00:29:56 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
2015-07-10 11:28:50 +00:00
|
|
|
import eu.siacs.conversations.R;
|
2018-05-25 13:37:14 +00:00
|
|
|
import eu.siacs.conversations.entities.Account;
|
2014-10-14 10:02:48 +00:00
|
|
|
import eu.siacs.conversations.entities.DownloadableFile;
|
2014-10-13 23:06:45 +00:00
|
|
|
import eu.siacs.conversations.entities.Message;
|
2015-07-20 12:26:29 +00:00
|
|
|
import eu.siacs.conversations.entities.Transferable;
|
2015-10-17 13:23:46 +00:00
|
|
|
import eu.siacs.conversations.entities.TransferablePlaceholder;
|
2015-07-31 23:19:16 +00:00
|
|
|
import eu.siacs.conversations.persistance.FileBackend;
|
|
|
|
import eu.siacs.conversations.services.AbstractConnectionManager;
|
2014-10-13 23:06:45 +00:00
|
|
|
import eu.siacs.conversations.services.XmppConnectionService;
|
2014-10-22 11:06:46 +00:00
|
|
|
import eu.siacs.conversations.utils.CryptoHelper;
|
2016-11-15 14:43:04 +00:00
|
|
|
import eu.siacs.conversations.utils.FileWriterException;
|
2018-04-15 16:31:58 +00:00
|
|
|
import eu.siacs.conversations.utils.WakeLockHelper;
|
2018-05-25 13:37:14 +00:00
|
|
|
import eu.siacs.conversations.xmpp.OnIqPacketReceived;
|
|
|
|
import eu.siacs.conversations.xmpp.stanzas.IqPacket;
|
|
|
|
import rocks.xmpp.addr.Jid;
|
2014-10-13 23:06:45 +00:00
|
|
|
|
2015-07-10 13:14:13 +00:00
|
|
|
public class HttpDownloadConnection implements Transferable {
|
2014-10-13 23:06:45 +00:00
|
|
|
|
|
|
|
private HttpConnectionManager mHttpConnectionManager;
|
|
|
|
private XmppConnectionService mXmppConnectionService;
|
|
|
|
|
|
|
|
private URL mUrl;
|
2019-01-28 09:39:01 +00:00
|
|
|
private final Message message;
|
2014-10-13 23:06:45 +00:00
|
|
|
private DownloadableFile file;
|
2015-07-10 13:11:03 +00:00
|
|
|
private int mStatus = Transferable.STATUS_UNKNOWN;
|
2014-10-24 11:29:18 +00:00
|
|
|
private boolean acceptedAutomatically = false;
|
2014-11-13 20:04:05 +00:00
|
|
|
private int mProgress = 0;
|
2018-05-25 13:37:14 +00:00
|
|
|
private final boolean mUseTor;
|
2015-12-11 19:33:41 +00:00
|
|
|
private boolean canceled = false;
|
2018-05-25 13:37:14 +00:00
|
|
|
private Method method = Method.HTTP_UPLOAD;
|
2014-10-13 23:06:45 +00:00
|
|
|
|
2019-01-28 09:39:01 +00:00
|
|
|
HttpDownloadConnection(Message message, HttpConnectionManager manager) {
|
|
|
|
this.message = message;
|
2014-10-13 23:06:45 +00:00
|
|
|
this.mHttpConnectionManager = manager;
|
|
|
|
this.mXmppConnectionService = manager.getXmppConnectionService();
|
2015-11-28 19:11:38 +00:00
|
|
|
this.mUseTor = mXmppConnectionService.useTorToConnect();
|
2014-10-13 23:06:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-10-17 11:09:02 +00:00
|
|
|
public boolean start() {
|
|
|
|
if (mXmppConnectionService.hasInternetConnection()) {
|
2014-10-19 21:42:53 +00:00
|
|
|
if (this.mStatus == STATUS_OFFER_CHECK_FILESIZE) {
|
|
|
|
checkFileSize(true);
|
|
|
|
} else {
|
2018-06-28 15:44:00 +00:00
|
|
|
download(true);
|
2014-10-19 21:42:53 +00:00
|
|
|
}
|
2014-10-17 11:09:02 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2014-10-13 23:06:45 +00:00
|
|
|
}
|
|
|
|
|
2019-01-28 09:39:01 +00:00
|
|
|
public void init(boolean interactive) {
|
2015-07-10 13:11:03 +00:00
|
|
|
this.message.setTransferable(this);
|
2014-10-13 23:06:45 +00:00
|
|
|
try {
|
2015-10-17 13:23:46 +00:00
|
|
|
if (message.hasFileOnRemoteHost()) {
|
2017-04-16 06:44:21 +00:00
|
|
|
mUrl = CryptoHelper.toHttpsUrl(message.getFileParams().url);
|
2015-10-17 13:23:46 +00:00
|
|
|
} else {
|
2017-12-09 13:45:54 +00:00
|
|
|
mUrl = CryptoHelper.toHttpsUrl(new URL(message.getBody().split("\n")[0]));
|
2015-10-17 13:23:46 +00:00
|
|
|
}
|
2015-01-11 14:19:36 +00:00
|
|
|
String[] parts = mUrl.getPath().toLowerCase().split("\\.");
|
|
|
|
String lastPart = parts.length >= 1 ? parts[parts.length - 1] : null;
|
2017-08-04 09:58:12 +00:00
|
|
|
String secondToLast = parts.length >= 2 ? parts[parts.length - 2] : null;
|
2015-01-11 14:19:36 +00:00
|
|
|
if ("pgp".equals(lastPart) || "gpg".equals(lastPart)) {
|
2014-10-22 17:05:15 +00:00
|
|
|
this.message.setEncryption(Message.ENCRYPTION_PGP);
|
2015-07-19 12:17:25 +00:00
|
|
|
} else if (message.getEncryption() != Message.ENCRYPTION_OTR
|
|
|
|
&& message.getEncryption() != Message.ENCRYPTION_AXOLOTL) {
|
2014-10-27 10:00:03 +00:00
|
|
|
this.message.setEncryption(Message.ENCRYPTION_NONE);
|
2014-10-22 17:05:15 +00:00
|
|
|
}
|
2015-01-11 14:19:36 +00:00
|
|
|
String extension;
|
2016-03-04 10:24:53 +00:00
|
|
|
if (VALID_CRYPTO_EXTENSIONS.contains(lastPart)) {
|
2015-01-11 14:19:36 +00:00
|
|
|
extension = secondToLast;
|
|
|
|
} else {
|
|
|
|
extension = lastPart;
|
|
|
|
}
|
2018-01-29 00:40:02 +00:00
|
|
|
message.setRelativeFilePath(message.getUuid() + (extension != null ? ("." + extension) : ""));
|
2015-01-11 14:19:36 +00:00
|
|
|
this.file = mXmppConnectionService.getFileBackend().getFile(message, false);
|
2017-04-16 06:44:21 +00:00
|
|
|
final String reference = mUrl.getRef();
|
2017-08-10 03:42:35 +00:00
|
|
|
if (reference != null && AesGcmURLStreamHandler.IV_KEY.matcher(reference).matches()) {
|
2015-07-31 23:19:16 +00:00
|
|
|
this.file.setKeyAndIv(CryptoHelper.hexToBytes(reference));
|
2014-10-22 11:06:46 +00:00
|
|
|
}
|
2014-11-03 18:59:11 +00:00
|
|
|
|
2018-02-19 12:55:48 +00:00
|
|
|
if (this.message.getEncryption() == Message.ENCRYPTION_AXOLOTL && this.file.getKey() == null) {
|
2014-10-22 11:06:46 +00:00
|
|
|
this.message.setEncryption(Message.ENCRYPTION_NONE);
|
2017-04-05 19:01:29 +00:00
|
|
|
}
|
2018-05-25 13:37:14 +00:00
|
|
|
method = mUrl.getProtocol().equalsIgnoreCase(P1S3UrlStreamHandler.PROTOCOL_NAME) ? Method.P1_S3 : Method.HTTP_UPLOAD;
|
2018-06-28 15:44:00 +00:00
|
|
|
long knownFileSize = message.getFileParams().size;
|
|
|
|
if (knownFileSize > 0 && interactive && method != Method.P1_S3) {
|
|
|
|
this.file.setExpectedSize(knownFileSize);
|
|
|
|
download(true);
|
|
|
|
} else {
|
|
|
|
checkFileSize(interactive);
|
|
|
|
}
|
2014-10-13 23:06:45 +00:00
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
this.cancel();
|
|
|
|
}
|
|
|
|
}
|
2014-10-15 17:32:12 +00:00
|
|
|
|
2018-06-28 15:44:00 +00:00
|
|
|
private void download(boolean interactive) {
|
|
|
|
new Thread(new FileDownloader(interactive)).start();
|
|
|
|
}
|
|
|
|
|
2014-10-19 21:42:53 +00:00
|
|
|
private void checkFileSize(boolean interactive) {
|
|
|
|
new Thread(new FileSizeChecker(interactive)).start();
|
2014-10-13 23:06:45 +00:00
|
|
|
}
|
|
|
|
|
2015-12-11 19:33:41 +00:00
|
|
|
@Override
|
2014-10-13 23:06:45 +00:00
|
|
|
public void cancel() {
|
2015-12-11 19:33:41 +00:00
|
|
|
this.canceled = true;
|
2014-10-14 17:27:49 +00:00
|
|
|
mHttpConnectionManager.finishConnection(this);
|
2019-01-10 13:52:27 +00:00
|
|
|
message.setTransferable(null);
|
2015-10-17 13:23:46 +00:00
|
|
|
if (message.isFileOrImage()) {
|
2019-01-10 13:52:27 +00:00
|
|
|
message.setDeleted(true);
|
2015-10-17 13:23:46 +00:00
|
|
|
}
|
2017-01-12 15:02:09 +00:00
|
|
|
mHttpConnectionManager.updateConversationUi(true);
|
2014-10-14 17:27:49 +00:00
|
|
|
}
|
2014-10-15 17:32:12 +00:00
|
|
|
|
2014-10-14 17:27:49 +00:00
|
|
|
private void finish() {
|
2015-07-10 13:11:03 +00:00
|
|
|
message.setTransferable(null);
|
2014-10-14 17:27:49 +00:00
|
|
|
mHttpConnectionManager.finishConnection(this);
|
2016-06-15 10:44:29 +00:00
|
|
|
boolean notify = acceptedAutomatically && !message.isRead();
|
2015-11-25 19:47:02 +00:00
|
|
|
if (message.getEncryption() == Message.ENCRYPTION_PGP) {
|
2016-06-15 10:44:29 +00:00
|
|
|
notify = message.getConversation().getAccount().getPgpDecryptionService().decrypt(message, notify);
|
2015-11-25 19:47:02 +00:00
|
|
|
}
|
2017-01-12 15:02:09 +00:00
|
|
|
mHttpConnectionManager.updateConversationUi(true);
|
2019-01-09 11:47:09 +00:00
|
|
|
final boolean notifyAfterScan = notify;
|
|
|
|
mXmppConnectionService.getFileBackend().updateMediaScanner(file, () -> {
|
|
|
|
if (notifyAfterScan) {
|
|
|
|
mXmppConnectionService.getNotificationService().push(message);
|
|
|
|
}
|
|
|
|
});
|
2014-10-13 23:06:45 +00:00
|
|
|
}
|
|
|
|
|
2014-10-15 17:32:12 +00:00
|
|
|
private void changeStatus(int status) {
|
|
|
|
this.mStatus = status;
|
2017-01-12 15:02:09 +00:00
|
|
|
mHttpConnectionManager.updateConversationUi(true);
|
2014-10-15 17:32:12 +00:00
|
|
|
}
|
2014-10-20 19:08:33 +00:00
|
|
|
|
2015-08-10 10:15:14 +00:00
|
|
|
private void showToastForException(Exception e) {
|
|
|
|
if (e instanceof java.net.UnknownHostException) {
|
|
|
|
mXmppConnectionService.showErrorToastInUi(R.string.download_failed_server_not_found);
|
|
|
|
} else if (e instanceof java.net.ConnectException) {
|
|
|
|
mXmppConnectionService.showErrorToastInUi(R.string.download_failed_could_not_connect);
|
2016-11-15 14:43:04 +00:00
|
|
|
} else if (e instanceof FileWriterException) {
|
2016-06-02 19:37:52 +00:00
|
|
|
mXmppConnectionService.showErrorToastInUi(R.string.download_failed_could_not_write_file);
|
2017-08-04 09:58:12 +00:00
|
|
|
} else if (!(e instanceof CancellationException)) {
|
2015-08-10 10:15:14 +00:00
|
|
|
mXmppConnectionService.showErrorToastInUi(R.string.download_failed_file_not_found);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-25 13:37:14 +00:00
|
|
|
private void updateProgress(long i) {
|
2017-08-04 09:58:12 +00:00
|
|
|
this.mProgress = (int) i;
|
|
|
|
mHttpConnectionManager.updateConversationUi(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getStatus() {
|
|
|
|
return this.mStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getFileSize() {
|
|
|
|
if (this.file != null) {
|
|
|
|
return this.file.getExpectedSize();
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getProgress() {
|
|
|
|
return this.mProgress;
|
|
|
|
}
|
|
|
|
|
2019-01-28 09:39:01 +00:00
|
|
|
public Message getMessage() {
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2014-10-13 23:06:45 +00:00
|
|
|
private class FileSizeChecker implements Runnable {
|
2014-10-20 19:08:33 +00:00
|
|
|
|
2018-05-25 13:37:14 +00:00
|
|
|
private final boolean interactive;
|
2014-10-19 21:42:53 +00:00
|
|
|
|
2018-05-25 13:37:14 +00:00
|
|
|
FileSizeChecker(boolean interactive) {
|
2014-10-19 21:42:53 +00:00
|
|
|
this.interactive = interactive;
|
|
|
|
}
|
2014-10-13 23:06:45 +00:00
|
|
|
|
2018-05-25 13:37:14 +00:00
|
|
|
|
2014-10-13 23:06:45 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2018-05-25 13:37:14 +00:00
|
|
|
if (mUrl.getProtocol().equalsIgnoreCase(P1S3UrlStreamHandler.PROTOCOL_NAME)) {
|
|
|
|
retrieveUrl();
|
|
|
|
} else {
|
|
|
|
check();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void retrieveUrl() {
|
|
|
|
changeStatus(STATUS_CHECKING);
|
|
|
|
final Account account = message.getConversation().getAccount();
|
|
|
|
IqPacket request = mXmppConnectionService.getIqGenerator().requestP1S3Url(Jid.of(account.getJid().getDomain()), mUrl.getHost());
|
|
|
|
mXmppConnectionService.sendIqPacket(message.getConversation().getAccount(), request, (a, packet) -> {
|
|
|
|
if (packet.getType() == IqPacket.TYPE.RESULT) {
|
|
|
|
String download = packet.query().getAttribute("download");
|
|
|
|
if (download != null) {
|
|
|
|
try {
|
|
|
|
mUrl = new URL(download);
|
|
|
|
check();
|
|
|
|
return;
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
//fallthrough
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Log.d(Config.LOGTAG,"unable to retrieve actual download url");
|
|
|
|
retrieveFailed(null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void retrieveFailed(@Nullable Exception e) {
|
|
|
|
changeStatus(STATUS_OFFER_CHECK_FILESIZE);
|
|
|
|
if (interactive) {
|
|
|
|
if (e != null) {
|
|
|
|
showToastForException(e);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
HttpDownloadConnection.this.acceptedAutomatically = false;
|
|
|
|
HttpDownloadConnection.this.mXmppConnectionService.getNotificationService().push(message);
|
|
|
|
}
|
|
|
|
cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void check() {
|
2014-10-14 17:27:49 +00:00
|
|
|
long size;
|
2014-10-13 23:06:45 +00:00
|
|
|
try {
|
2014-10-14 17:27:49 +00:00
|
|
|
size = retrieveFileSize();
|
2016-07-21 17:17:26 +00:00
|
|
|
} catch (Exception e) {
|
2015-07-10 11:28:50 +00:00
|
|
|
Log.d(Config.LOGTAG, "io exception in http file size checker: " + e.getMessage());
|
2018-05-25 13:37:14 +00:00
|
|
|
retrieveFailed(e);
|
2014-10-14 17:27:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
file.setExpectedSize(size);
|
2017-08-04 09:58:12 +00:00
|
|
|
message.resetFileParams();
|
2016-08-27 11:35:52 +00:00
|
|
|
if (mHttpConnectionManager.hasStoragePermission()
|
|
|
|
&& size <= mHttpConnectionManager.getAutoAcceptFileSize()
|
|
|
|
&& mXmppConnectionService.isDataSaverDisabled()) {
|
2015-07-10 13:14:13 +00:00
|
|
|
HttpDownloadConnection.this.acceptedAutomatically = true;
|
2018-06-28 15:44:00 +00:00
|
|
|
download(interactive);
|
2014-10-14 17:27:49 +00:00
|
|
|
} else {
|
2014-10-15 17:32:12 +00:00
|
|
|
changeStatus(STATUS_OFFER);
|
2015-07-10 13:14:13 +00:00
|
|
|
HttpDownloadConnection.this.acceptedAutomatically = false;
|
|
|
|
HttpDownloadConnection.this.mXmppConnectionService.getNotificationService().push(message);
|
2014-10-13 23:06:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-10 11:28:50 +00:00
|
|
|
private long retrieveFileSize() throws IOException {
|
|
|
|
try {
|
2015-08-10 10:15:14 +00:00
|
|
|
Log.d(Config.LOGTAG, "retrieve file size. interactive:" + String.valueOf(interactive));
|
|
|
|
changeStatus(STATUS_CHECKING);
|
2015-11-28 19:11:38 +00:00
|
|
|
HttpURLConnection connection;
|
2018-06-16 16:31:55 +00:00
|
|
|
if (mUseTor || message.getConversation().getAccount().isOnion()) {
|
2018-05-11 10:42:39 +00:00
|
|
|
connection = (HttpURLConnection) mUrl.openConnection(HttpConnectionManager.getProxy());
|
2015-11-28 19:11:38 +00:00
|
|
|
} else {
|
|
|
|
connection = (HttpURLConnection) mUrl.openConnection();
|
|
|
|
}
|
2018-05-25 13:37:14 +00:00
|
|
|
if (method == Method.P1_S3) {
|
|
|
|
connection.setRequestMethod("GET");
|
|
|
|
connection.addRequestProperty("Range","bytes=0-0");
|
|
|
|
} else {
|
|
|
|
connection.setRequestMethod("HEAD");
|
|
|
|
}
|
2018-03-13 10:59:32 +00:00
|
|
|
connection.setUseCaches(false);
|
2017-08-04 09:58:12 +00:00
|
|
|
Log.d(Config.LOGTAG, "url: " + connection.getURL().toString());
|
2018-10-23 18:57:31 +00:00
|
|
|
connection.setRequestProperty("User-Agent", mXmppConnectionService.getIqGenerator().getUserAgent());
|
2015-08-10 10:15:14 +00:00
|
|
|
if (connection instanceof HttpsURLConnection) {
|
|
|
|
mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, interactive);
|
|
|
|
}
|
2016-09-20 18:02:25 +00:00
|
|
|
connection.setConnectTimeout(Config.SOCKET_TIMEOUT * 1000);
|
|
|
|
connection.setReadTimeout(Config.SOCKET_TIMEOUT * 1000);
|
2015-08-10 10:15:14 +00:00
|
|
|
connection.connect();
|
2018-05-25 13:37:14 +00:00
|
|
|
String contentLength;
|
|
|
|
if (method == Method.P1_S3) {
|
|
|
|
String contentRange = connection.getHeaderField("Content-Range");
|
|
|
|
String[] contentRangeParts = contentRange == null ? new String[0] : contentRange.split("/");
|
|
|
|
if (contentRangeParts.length != 2) {
|
|
|
|
contentLength = null;
|
|
|
|
} else {
|
|
|
|
contentLength = contentRangeParts[1];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
contentLength = connection.getHeaderField("Content-Length");
|
|
|
|
}
|
2015-08-10 10:15:14 +00:00
|
|
|
connection.disconnect();
|
|
|
|
if (contentLength == null) {
|
2016-06-16 18:36:51 +00:00
|
|
|
throw new IOException("no content-length found in HEAD response");
|
2015-08-10 10:15:14 +00:00
|
|
|
}
|
2015-07-10 11:28:50 +00:00
|
|
|
return Long.parseLong(contentLength, 10);
|
2015-08-10 10:15:14 +00:00
|
|
|
} catch (IOException e) {
|
2017-08-04 09:58:12 +00:00
|
|
|
Log.d(Config.LOGTAG, "io exception during HEAD " + e.getMessage());
|
2015-08-10 10:15:14 +00:00
|
|
|
throw e;
|
2015-07-10 11:28:50 +00:00
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
throw new IOException();
|
|
|
|
}
|
2014-10-13 23:06:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-10-15 17:32:12 +00:00
|
|
|
|
2014-10-13 23:06:45 +00:00
|
|
|
private class FileDownloader implements Runnable {
|
|
|
|
|
2018-05-25 13:37:14 +00:00
|
|
|
private final boolean interactive;
|
2014-10-21 22:00:01 +00:00
|
|
|
|
2015-07-31 23:19:16 +00:00
|
|
|
private OutputStream os;
|
|
|
|
|
2014-10-21 22:00:01 +00:00
|
|
|
public FileDownloader(boolean interactive) {
|
|
|
|
this.interactive = interactive;
|
|
|
|
}
|
|
|
|
|
2014-10-13 23:06:45 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
2014-10-21 22:00:01 +00:00
|
|
|
changeStatus(STATUS_DOWNLOADING);
|
2014-10-13 23:06:45 +00:00
|
|
|
download();
|
2014-10-14 16:16:03 +00:00
|
|
|
updateImageBounds();
|
2014-10-14 17:27:49 +00:00
|
|
|
finish();
|
2014-10-21 22:00:01 +00:00
|
|
|
} catch (SSLHandshakeException e) {
|
|
|
|
changeStatus(STATUS_OFFER);
|
2015-12-11 19:33:41 +00:00
|
|
|
} catch (Exception e) {
|
2015-08-10 10:15:14 +00:00
|
|
|
if (interactive) {
|
|
|
|
showToastForException(e);
|
2016-07-21 17:17:26 +00:00
|
|
|
} else {
|
|
|
|
HttpDownloadConnection.this.acceptedAutomatically = false;
|
|
|
|
HttpDownloadConnection.this.mXmppConnectionService.getNotificationService().push(message);
|
2015-08-10 10:15:14 +00:00
|
|
|
}
|
2014-10-13 23:06:45 +00:00
|
|
|
cancel();
|
|
|
|
}
|
|
|
|
}
|
2014-10-15 17:32:12 +00:00
|
|
|
|
2017-08-04 09:58:12 +00:00
|
|
|
private void download() throws Exception {
|
2015-08-10 10:15:14 +00:00
|
|
|
InputStream is = null;
|
2017-04-29 20:59:36 +00:00
|
|
|
HttpURLConnection connection = null;
|
2017-08-04 09:58:12 +00:00
|
|
|
PowerManager.WakeLock wakeLock = mHttpConnectionManager.createWakeLock("http_download_" + message.getUuid());
|
2015-08-10 10:15:14 +00:00
|
|
|
try {
|
2015-08-10 17:48:36 +00:00
|
|
|
wakeLock.acquire();
|
2018-06-16 16:31:55 +00:00
|
|
|
if (mUseTor || message.getConversation().getAccount().isOnion()) {
|
2018-05-11 10:42:39 +00:00
|
|
|
connection = (HttpURLConnection) mUrl.openConnection(HttpConnectionManager.getProxy());
|
2015-11-28 19:11:38 +00:00
|
|
|
} else {
|
|
|
|
connection = (HttpURLConnection) mUrl.openConnection();
|
|
|
|
}
|
2015-08-10 10:15:14 +00:00
|
|
|
if (connection instanceof HttpsURLConnection) {
|
|
|
|
mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, interactive);
|
|
|
|
}
|
2018-03-13 10:59:32 +00:00
|
|
|
connection.setUseCaches(false);
|
2018-10-23 18:57:31 +00:00
|
|
|
connection.setRequestProperty("User-Agent", mXmppConnectionService.getIqGenerator().getUserAgent());
|
2019-01-10 17:23:09 +00:00
|
|
|
final long expected = file.getExpectedSize();
|
|
|
|
final boolean tryResume = file.exists() && file.getKey() == null && file.getSize() > 0 && file.getSize() < expected;
|
2016-12-06 22:27:29 +00:00
|
|
|
long resumeSize = 0;
|
2019-01-10 17:23:09 +00:00
|
|
|
|
2015-12-11 19:33:41 +00:00
|
|
|
if (tryResume) {
|
2016-12-06 22:27:29 +00:00
|
|
|
resumeSize = file.getSize();
|
2017-08-04 09:58:12 +00:00
|
|
|
Log.d(Config.LOGTAG, "http download trying resume after" + resumeSize + " of " + expected);
|
|
|
|
connection.setRequestProperty("Range", "bytes=" + resumeSize + "-");
|
2015-12-11 19:33:41 +00:00
|
|
|
}
|
2016-09-20 18:02:25 +00:00
|
|
|
connection.setConnectTimeout(Config.SOCKET_TIMEOUT * 1000);
|
|
|
|
connection.setReadTimeout(Config.SOCKET_TIMEOUT * 1000);
|
2015-08-10 10:15:14 +00:00
|
|
|
connection.connect();
|
|
|
|
is = new BufferedInputStream(connection.getInputStream());
|
2016-12-06 22:27:29 +00:00
|
|
|
final String contentRange = connection.getHeaderField("Content-Range");
|
2017-08-04 09:58:12 +00:00
|
|
|
boolean serverResumed = tryResume && contentRange != null && contentRange.startsWith("bytes " + resumeSize + "-");
|
2015-08-10 10:15:14 +00:00
|
|
|
long transmitted = 0;
|
2015-12-11 19:33:41 +00:00
|
|
|
if (tryResume && serverResumed) {
|
2017-08-04 09:58:12 +00:00
|
|
|
Log.d(Config.LOGTAG, "server resumed");
|
2015-12-11 19:33:41 +00:00
|
|
|
transmitted = file.getSize();
|
2017-08-04 09:58:12 +00:00
|
|
|
updateProgress(Math.round(((double) transmitted / expected) * 100));
|
2015-12-11 19:33:41 +00:00
|
|
|
os = AbstractConnectionManager.createAppendedOutputStream(file);
|
2016-12-06 22:27:29 +00:00
|
|
|
if (os == null) {
|
|
|
|
throw new FileWriterException();
|
|
|
|
}
|
2015-12-11 19:33:41 +00:00
|
|
|
} else {
|
2017-08-04 09:58:12 +00:00
|
|
|
long reportedContentLengthOnGet;
|
|
|
|
try {
|
|
|
|
reportedContentLengthOnGet = Long.parseLong(connection.getHeaderField("Content-Length"));
|
|
|
|
} catch (NumberFormatException | NullPointerException e) {
|
|
|
|
reportedContentLengthOnGet = 0;
|
|
|
|
}
|
|
|
|
if (expected != reportedContentLengthOnGet) {
|
|
|
|
Log.d(Config.LOGTAG, "content-length reported on GET (" + reportedContentLengthOnGet + ") did not match Content-Length reported on HEAD (" + expected + ")");
|
|
|
|
}
|
2015-12-11 19:33:41 +00:00
|
|
|
file.getParentFile().mkdirs();
|
2016-12-06 22:27:29 +00:00
|
|
|
if (!file.exists() && !file.createNewFile()) {
|
|
|
|
throw new FileWriterException();
|
|
|
|
}
|
2018-09-30 11:48:11 +00:00
|
|
|
os = AbstractConnectionManager.createOutputStream(file);
|
2015-12-11 19:33:41 +00:00
|
|
|
}
|
2016-06-02 19:37:52 +00:00
|
|
|
int count;
|
2017-08-04 09:58:12 +00:00
|
|
|
byte[] buffer = new byte[4096];
|
2015-08-10 10:15:14 +00:00
|
|
|
while ((count = is.read(buffer)) != -1) {
|
|
|
|
transmitted += count;
|
2016-06-02 19:37:52 +00:00
|
|
|
try {
|
|
|
|
os.write(buffer, 0, count);
|
|
|
|
} catch (IOException e) {
|
2016-11-15 14:43:04 +00:00
|
|
|
throw new FileWriterException();
|
2016-06-02 19:37:52 +00:00
|
|
|
}
|
2017-08-04 09:58:12 +00:00
|
|
|
updateProgress(Math.round(((double) transmitted / expected) * 100));
|
2015-12-11 19:33:41 +00:00
|
|
|
if (canceled) {
|
|
|
|
throw new CancellationException();
|
|
|
|
}
|
2015-08-10 10:15:14 +00:00
|
|
|
}
|
2016-06-03 12:27:05 +00:00
|
|
|
try {
|
|
|
|
os.flush();
|
|
|
|
} catch (IOException e) {
|
2016-11-15 14:43:04 +00:00
|
|
|
throw new FileWriterException();
|
2016-06-03 12:27:05 +00:00
|
|
|
}
|
2015-12-11 19:33:41 +00:00
|
|
|
} catch (CancellationException | IOException e) {
|
2017-08-04 09:58:12 +00:00
|
|
|
Log.d(Config.LOGTAG, "http download failed " + e.getMessage());
|
2015-08-10 10:15:14 +00:00
|
|
|
throw e;
|
|
|
|
} finally {
|
|
|
|
FileBackend.close(os);
|
|
|
|
FileBackend.close(is);
|
2017-04-29 20:59:36 +00:00
|
|
|
if (connection != null) {
|
|
|
|
connection.disconnect();
|
|
|
|
}
|
2018-04-15 16:31:58 +00:00
|
|
|
WakeLockHelper.release(wakeLock);
|
2014-10-13 23:06:45 +00:00
|
|
|
}
|
2014-10-14 16:16:03 +00:00
|
|
|
}
|
2014-10-15 17:32:12 +00:00
|
|
|
|
2014-10-14 16:16:03 +00:00
|
|
|
private void updateImageBounds() {
|
2015-06-30 15:15:02 +00:00
|
|
|
message.setType(Message.TYPE_FILE);
|
2017-04-16 06:44:21 +00:00
|
|
|
final URL url;
|
|
|
|
final String ref = mUrl.getRef();
|
2018-05-25 13:37:14 +00:00
|
|
|
if (method == Method.P1_S3) {
|
|
|
|
url = message.getFileParams().url;
|
|
|
|
} else if (ref != null && AesGcmURLStreamHandler.IV_KEY.matcher(ref).matches()) {
|
2017-04-16 06:44:21 +00:00
|
|
|
url = CryptoHelper.toAesGcmUrl(mUrl);
|
|
|
|
} else {
|
|
|
|
url = mUrl;
|
|
|
|
}
|
|
|
|
mXmppConnectionService.getFileBackend().updateFileParams(message, url);
|
2014-10-15 17:32:12 +00:00
|
|
|
mXmppConnectionService.updateMessage(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-01-14 17:20:02 +00:00
|
|
|
}
|