2015-06-28 09:19:07 +00:00
|
|
|
package eu.siacs.conversations.http;
|
|
|
|
|
2015-08-10 17:48:36 +00:00
|
|
|
import android.os.PowerManager;
|
2015-06-28 09:19:07 +00:00
|
|
|
import android.util.Log;
|
2015-07-31 23:19:16 +00:00
|
|
|
import android.util.Pair;
|
2015-06-28 09:19:07 +00:00
|
|
|
|
2015-08-11 14:50:00 +00:00
|
|
|
import java.io.FileNotFoundException;
|
2015-06-28 09:19:07 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
2018-02-16 10:14:16 +00:00
|
|
|
import java.util.Arrays;
|
2018-01-28 13:17:42 +00:00
|
|
|
import java.util.HashMap;
|
2018-02-16 10:14:16 +00:00
|
|
|
import java.util.List;
|
2018-05-25 10:24:23 +00:00
|
|
|
import java.util.Scanner;
|
2015-06-28 09:19:07 +00:00
|
|
|
|
2015-07-10 12:14:45 +00:00
|
|
|
import javax.net.ssl.HttpsURLConnection;
|
|
|
|
|
2015-06-28 09:19:07 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
|
|
|
import eu.siacs.conversations.entities.Account;
|
|
|
|
import eu.siacs.conversations.entities.DownloadableFile;
|
|
|
|
import eu.siacs.conversations.entities.Message;
|
2015-07-20 12:26:29 +00:00
|
|
|
import eu.siacs.conversations.entities.Transferable;
|
2015-06-28 09:19:07 +00:00
|
|
|
import eu.siacs.conversations.persistance.FileBackend;
|
2015-07-31 23:19:16 +00:00
|
|
|
import eu.siacs.conversations.services.AbstractConnectionManager;
|
2015-06-28 09:19:07 +00:00
|
|
|
import eu.siacs.conversations.services.XmppConnectionService;
|
2018-05-25 10:24:23 +00:00
|
|
|
import eu.siacs.conversations.utils.Checksum;
|
2015-06-29 00:04:58 +00:00
|
|
|
import eu.siacs.conversations.utils.CryptoHelper;
|
2018-04-15 16:31:58 +00:00
|
|
|
import eu.siacs.conversations.utils.WakeLockHelper;
|
2015-06-28 09:19:07 +00:00
|
|
|
|
2015-07-10 13:11:03 +00:00
|
|
|
public class HttpUploadConnection implements Transferable {
|
2015-06-28 09:19:07 +00:00
|
|
|
|
2018-05-25 10:24:23 +00:00
|
|
|
public static final List<String> WHITE_LISTED_HEADERS = Arrays.asList(
|
2018-02-16 10:14:16 +00:00
|
|
|
"Authorization",
|
|
|
|
"Cookie",
|
|
|
|
"Expires"
|
|
|
|
);
|
|
|
|
|
2018-05-25 10:24:23 +00:00
|
|
|
private final HttpConnectionManager mHttpConnectionManager;
|
|
|
|
private final XmppConnectionService mXmppConnectionService;
|
|
|
|
private final SlotRequester mSlotRequester;
|
|
|
|
private final Method method;
|
2018-05-25 11:18:25 +00:00
|
|
|
private final boolean mUseTor;
|
2015-06-28 09:19:07 +00:00
|
|
|
private boolean canceled = false;
|
2015-07-20 16:11:33 +00:00
|
|
|
private boolean delayed = false;
|
2015-06-28 09:19:07 +00:00
|
|
|
private DownloadableFile file;
|
|
|
|
private Message message;
|
2015-08-01 20:37:17 +00:00
|
|
|
private String mime;
|
2018-05-25 10:24:23 +00:00
|
|
|
private SlotRequester.Slot slot;
|
2015-06-29 00:04:58 +00:00
|
|
|
private byte[] key = null;
|
|
|
|
|
2015-06-28 09:19:07 +00:00
|
|
|
private long transmitted = 0;
|
2015-08-01 20:23:58 +00:00
|
|
|
|
|
|
|
private InputStream mFileInputStream;
|
2015-06-28 09:19:07 +00:00
|
|
|
|
2018-05-25 10:24:23 +00:00
|
|
|
public HttpUploadConnection(Method method, HttpConnectionManager httpConnectionManager) {
|
|
|
|
this.method = method;
|
2015-06-28 09:19:07 +00:00
|
|
|
this.mHttpConnectionManager = httpConnectionManager;
|
|
|
|
this.mXmppConnectionService = httpConnectionManager.getXmppConnectionService();
|
2018-05-25 10:24:23 +00:00
|
|
|
this.mSlotRequester = new SlotRequester(this.mXmppConnectionService);
|
2015-11-28 19:11:38 +00:00
|
|
|
this.mUseTor = mXmppConnectionService.useTorToConnect();
|
2015-06-28 09:19:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean start() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getStatus() {
|
|
|
|
return STATUS_UPLOADING;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getFileSize() {
|
2015-09-15 20:49:43 +00:00
|
|
|
return file == null ? 0 : file.getExpectedSize();
|
2015-06-28 09:19:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getProgress() {
|
2015-09-15 20:49:43 +00:00
|
|
|
if (file == null) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-08-01 20:23:58 +00:00
|
|
|
return (int) ((((double) transmitted) / file.getExpectedSize()) * 100);
|
2015-06-28 09:19:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void cancel() {
|
|
|
|
this.canceled = true;
|
|
|
|
}
|
|
|
|
|
2016-10-26 10:26:04 +00:00
|
|
|
private void fail(String errorMessage) {
|
2015-06-28 09:19:07 +00:00
|
|
|
mHttpConnectionManager.finishUploadConnection(this);
|
2015-07-10 13:11:03 +00:00
|
|
|
message.setTransferable(null);
|
2016-10-26 10:26:04 +00:00
|
|
|
mXmppConnectionService.markMessage(message, Message.STATUS_SEND_FAILED, errorMessage);
|
2015-08-01 20:23:58 +00:00
|
|
|
FileBackend.close(mFileInputStream);
|
2015-06-28 09:19:07 +00:00
|
|
|
}
|
|
|
|
|
2015-07-20 16:11:33 +00:00
|
|
|
public void init(Message message, boolean delay) {
|
2015-06-28 09:19:07 +00:00
|
|
|
this.message = message;
|
2018-02-16 10:14:16 +00:00
|
|
|
final Account account = message.getConversation().getAccount();
|
2015-06-28 09:19:07 +00:00
|
|
|
this.file = mXmppConnectionService.getFileBackend().getFile(message, false);
|
2017-06-01 05:35:18 +00:00
|
|
|
if (message.getEncryption() == Message.ENCRYPTION_PGP || message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
|
|
|
|
this.mime = "application/pgp-encrypted";
|
|
|
|
} else {
|
|
|
|
this.mime = this.file.getMimeType();
|
|
|
|
}
|
2015-07-20 16:11:33 +00:00
|
|
|
this.delayed = delay;
|
2015-07-19 12:17:25 +00:00
|
|
|
if (Config.ENCRYPT_ON_HTTP_UPLOADED
|
|
|
|
|| message.getEncryption() == Message.ENCRYPTION_AXOLOTL
|
|
|
|
|| message.getEncryption() == Message.ENCRYPTION_OTR) {
|
2017-08-10 03:42:35 +00:00
|
|
|
this.key = new byte[48]; // todo: change this to 44 for 12-byte IV instead of 16-byte at some point in future
|
2015-06-29 00:04:58 +00:00
|
|
|
mXmppConnectionService.getRNG().nextBytes(this.key);
|
2015-07-31 23:19:16 +00:00
|
|
|
this.file.setKeyAndIv(this.key);
|
2015-06-29 00:04:58 +00:00
|
|
|
}
|
2018-05-25 10:24:23 +00:00
|
|
|
|
|
|
|
final String md5;
|
|
|
|
|
|
|
|
if (method == Method.P1_S3) {
|
|
|
|
try {
|
2018-10-03 10:50:54 +00:00
|
|
|
md5 = Checksum.md5(AbstractConnectionManager.createInputStream(file).first);
|
2018-05-25 10:24:23 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
Log.d(Config.LOGTAG, account.getJid().asBareJid()+": unable to calculate md5()", e);
|
|
|
|
fail(e.getMessage());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
md5 = null;
|
|
|
|
}
|
|
|
|
|
2015-08-11 14:50:00 +00:00
|
|
|
Pair<InputStream,Integer> pair;
|
|
|
|
try {
|
2018-10-03 10:50:54 +00:00
|
|
|
pair = AbstractConnectionManager.createInputStream(file);
|
2015-08-11 14:50:00 +00:00
|
|
|
} catch (FileNotFoundException e) {
|
2018-03-05 17:30:40 +00:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().asBareJid()+": could not find file to upload - "+e.getMessage());
|
2016-10-26 10:26:04 +00:00
|
|
|
fail(e.getMessage());
|
2015-08-11 14:50:00 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-08-01 20:23:58 +00:00
|
|
|
this.file.setExpectedSize(pair.second);
|
2017-08-04 09:58:12 +00:00
|
|
|
message.resetFileParams();
|
2015-08-01 20:23:58 +00:00
|
|
|
this.mFileInputStream = pair.first;
|
2018-05-25 10:24:23 +00:00
|
|
|
this.mSlotRequester.request(method, account, file, mime, md5, new SlotRequester.OnSlotRequested() {
|
|
|
|
@Override
|
|
|
|
public void success(SlotRequester.Slot slot) {
|
|
|
|
if (!canceled) {
|
|
|
|
HttpUploadConnection.this.slot = slot;
|
|
|
|
new Thread(HttpUploadConnection.this::upload).start();
|
2015-06-28 09:19:07 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-25 10:24:23 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void failure(String message) {
|
|
|
|
fail(message);
|
|
|
|
}
|
2015-06-28 09:19:07 +00:00
|
|
|
});
|
2015-09-15 20:49:43 +00:00
|
|
|
message.setTransferable(this);
|
|
|
|
mXmppConnectionService.markMessage(message, Message.STATUS_UNSEND);
|
2015-06-28 09:19:07 +00:00
|
|
|
}
|
|
|
|
|
2018-01-28 13:17:42 +00:00
|
|
|
private void upload() {
|
|
|
|
OutputStream os = null;
|
|
|
|
HttpURLConnection connection = null;
|
|
|
|
PowerManager.WakeLock wakeLock = mHttpConnectionManager.createWakeLock("http_upload_"+message.getUuid());
|
|
|
|
try {
|
|
|
|
final int expectedFileSize = (int) file.getExpectedSize();
|
|
|
|
final int readTimeout = (expectedFileSize / 2048) + Config.SOCKET_TIMEOUT; //assuming a minimum transfer speed of 16kbit/s
|
|
|
|
wakeLock.acquire(readTimeout);
|
2018-05-25 10:24:23 +00:00
|
|
|
Log.d(Config.LOGTAG, "uploading to " + slot.getPutUrl().toString()+ " w/ read timeout of "+readTimeout+"s");
|
2018-06-16 16:31:55 +00:00
|
|
|
if (mUseTor || message.getConversation().getAccount().isOnion()) {
|
2018-05-25 10:24:23 +00:00
|
|
|
connection = (HttpURLConnection) slot.getPutUrl().openConnection(HttpConnectionManager.getProxy());
|
2018-01-28 13:17:42 +00:00
|
|
|
} else {
|
2018-05-25 10:24:23 +00:00
|
|
|
connection = (HttpURLConnection) slot.getPutUrl().openConnection();
|
2018-01-28 13:17:42 +00:00
|
|
|
}
|
|
|
|
if (connection instanceof HttpsURLConnection) {
|
|
|
|
mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, true);
|
|
|
|
}
|
2018-03-13 10:59:32 +00:00
|
|
|
connection.setUseCaches(false);
|
2018-01-28 13:17:42 +00:00
|
|
|
connection.setRequestMethod("PUT");
|
|
|
|
connection.setFixedLengthStreamingMode(expectedFileSize);
|
|
|
|
connection.setRequestProperty("User-Agent",mXmppConnectionService.getIqGenerator().getIdentityName());
|
2018-05-25 10:24:23 +00:00
|
|
|
if(slot.getHeaders() != null) {
|
|
|
|
for(HashMap.Entry<String,String> entry : slot.getHeaders().entrySet()) {
|
2018-01-28 13:17:42 +00:00
|
|
|
connection.setRequestProperty(entry.getKey(),entry.getValue());
|
2015-06-28 09:19:07 +00:00
|
|
|
}
|
2018-01-28 13:17:42 +00:00
|
|
|
}
|
|
|
|
connection.setDoOutput(true);
|
2018-05-25 10:24:23 +00:00
|
|
|
connection.setDoInput(true);
|
2018-01-28 13:17:42 +00:00
|
|
|
connection.setConnectTimeout(Config.SOCKET_TIMEOUT * 1000);
|
|
|
|
connection.setReadTimeout(readTimeout * 1000);
|
|
|
|
connection.connect();
|
|
|
|
os = connection.getOutputStream();
|
|
|
|
transmitted = 0;
|
|
|
|
int count;
|
|
|
|
byte[] buffer = new byte[4096];
|
|
|
|
while (((count = mFileInputStream.read(buffer)) != -1) && !canceled) {
|
|
|
|
transmitted += count;
|
|
|
|
os.write(buffer, 0, count);
|
|
|
|
mHttpConnectionManager.updateConversationUi(false);
|
|
|
|
}
|
|
|
|
os.flush();
|
|
|
|
os.close();
|
|
|
|
mFileInputStream.close();
|
|
|
|
int code = connection.getResponseCode();
|
2018-05-25 10:24:23 +00:00
|
|
|
InputStream is = connection.getErrorStream();
|
|
|
|
if (is != null) {
|
|
|
|
try (Scanner scanner = new Scanner(is)) {
|
|
|
|
scanner.useDelimiter("\\Z");
|
|
|
|
Log.d(Config.LOGTAG, "body: " + scanner.next());
|
|
|
|
}
|
|
|
|
}
|
2018-01-28 13:17:42 +00:00
|
|
|
if (code == 200 || code == 201) {
|
|
|
|
Log.d(Config.LOGTAG, "finished uploading file");
|
2018-05-25 10:24:23 +00:00
|
|
|
final URL get;
|
2018-01-28 13:17:42 +00:00
|
|
|
if (key != null) {
|
2018-05-25 11:18:25 +00:00
|
|
|
if (method == Method.P1_S3) {
|
|
|
|
get = new URL(slot.getGetUrl().toString()+"#"+CryptoHelper.bytesToHex(key));
|
|
|
|
} else {
|
|
|
|
get = CryptoHelper.toAesGcmUrl(new URL(slot.getGetUrl().toString() + "#" + CryptoHelper.bytesToHex(key)));
|
|
|
|
}
|
2018-05-25 10:24:23 +00:00
|
|
|
} else {
|
|
|
|
get = slot.getGetUrl();
|
2015-06-28 09:19:07 +00:00
|
|
|
}
|
2018-05-25 10:24:23 +00:00
|
|
|
mXmppConnectionService.getFileBackend().updateFileParams(message, get);
|
2018-01-28 13:17:42 +00:00
|
|
|
mXmppConnectionService.getFileBackend().updateMediaScanner(file);
|
|
|
|
message.setTransferable(null);
|
2018-03-05 17:30:40 +00:00
|
|
|
message.setCounterpart(message.getConversation().getJid().asBareJid());
|
2018-01-28 13:17:42 +00:00
|
|
|
mXmppConnectionService.resendMessage(message, delayed);
|
|
|
|
} else {
|
|
|
|
Log.d(Config.LOGTAG,"http upload failed because response code was "+code);
|
|
|
|
fail("http upload failed because response code was "+code);
|
|
|
|
}
|
2018-07-16 18:42:40 +00:00
|
|
|
} catch (Exception e) {
|
2018-01-28 13:17:42 +00:00
|
|
|
e.printStackTrace();
|
|
|
|
Log.d(Config.LOGTAG,"http upload failed "+e.getMessage());
|
|
|
|
fail(e.getMessage());
|
|
|
|
} finally {
|
|
|
|
FileBackend.close(mFileInputStream);
|
|
|
|
FileBackend.close(os);
|
|
|
|
if (connection != null) {
|
|
|
|
connection.disconnect();
|
2015-06-28 09:19:07 +00:00
|
|
|
}
|
2018-04-15 16:31:58 +00:00
|
|
|
WakeLockHelper.release(wakeLock);
|
2015-06-28 09:19:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|