2015-06-28 09:19:07 +00:00
|
|
|
package eu.siacs.conversations.http;
|
|
|
|
|
2015-06-29 13:38:16 +00:00
|
|
|
import android.app.PendingIntent;
|
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.MalformedURLException;
|
|
|
|
import java.net.URL;
|
|
|
|
|
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;
|
2016-10-26 10:26:04 +00:00
|
|
|
import eu.siacs.conversations.parser.IqParser;
|
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;
|
2015-06-29 13:38:16 +00:00
|
|
|
import eu.siacs.conversations.ui.UiCallback;
|
2015-06-29 00:04:58 +00:00
|
|
|
import eu.siacs.conversations.utils.CryptoHelper;
|
2017-03-01 12:01:46 +00:00
|
|
|
import eu.siacs.conversations.xml.Namespace;
|
2015-06-28 09:19:07 +00:00
|
|
|
import eu.siacs.conversations.xml.Element;
|
|
|
|
import eu.siacs.conversations.xmpp.OnIqPacketReceived;
|
|
|
|
import eu.siacs.conversations.xmpp.jid.Jid;
|
|
|
|
import eu.siacs.conversations.xmpp.stanzas.IqPacket;
|
|
|
|
|
2015-07-10 13:11:03 +00:00
|
|
|
public class HttpUploadConnection implements Transferable {
|
2015-06-28 09:19:07 +00:00
|
|
|
|
|
|
|
private HttpConnectionManager mHttpConnectionManager;
|
|
|
|
private XmppConnectionService mXmppConnectionService;
|
|
|
|
|
|
|
|
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 Account account;
|
|
|
|
private DownloadableFile file;
|
|
|
|
private Message message;
|
2015-08-01 20:37:17 +00:00
|
|
|
private String mime;
|
2015-06-28 09:19:07 +00:00
|
|
|
private URL mGetUrl;
|
|
|
|
private URL mPutUrl;
|
2015-11-28 19:11:38 +00:00
|
|
|
private boolean mUseTor = false;
|
2015-06-28 09:19:07 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
public HttpUploadConnection(HttpConnectionManager httpConnectionManager) {
|
|
|
|
this.mHttpConnectionManager = httpConnectionManager;
|
|
|
|
this.mXmppConnectionService = httpConnectionManager.getXmppConnectionService();
|
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;
|
|
|
|
this.account = message.getConversation().getAccount();
|
|
|
|
this.file = mXmppConnectionService.getFileBackend().getFile(message, false);
|
2015-08-01 20:37:17 +00:00
|
|
|
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) {
|
2015-06-29 00:04:58 +00:00
|
|
|
this.key = new byte[48];
|
|
|
|
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
|
|
|
}
|
2015-08-11 14:50:00 +00:00
|
|
|
Pair<InputStream,Integer> pair;
|
|
|
|
try {
|
|
|
|
pair = AbstractConnectionManager.createInputStream(file, true);
|
|
|
|
} catch (FileNotFoundException e) {
|
2016-06-15 10:33:59 +00:00
|
|
|
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": 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);
|
|
|
|
this.mFileInputStream = pair.first;
|
2017-03-01 12:01:46 +00:00
|
|
|
Jid host = account.getXmppConnection().findDiscoItemByFeature(Namespace.HTTP_UPLOAD);
|
2015-08-01 20:37:17 +00:00
|
|
|
IqPacket request = mXmppConnectionService.getIqGenerator().requestHttpUploadSlot(host,file,mime);
|
2015-06-28 09:19:07 +00:00
|
|
|
mXmppConnectionService.sendIqPacket(account, request, new OnIqPacketReceived() {
|
|
|
|
@Override
|
|
|
|
public void onIqPacketReceived(Account account, IqPacket packet) {
|
|
|
|
if (packet.getType() == IqPacket.TYPE.RESULT) {
|
2017-03-01 12:01:46 +00:00
|
|
|
Element slot = packet.findChild("slot", Namespace.HTTP_UPLOAD);
|
2015-06-28 09:19:07 +00:00
|
|
|
if (slot != null) {
|
|
|
|
try {
|
|
|
|
mGetUrl = new URL(slot.findChildContent("get"));
|
|
|
|
mPutUrl = new URL(slot.findChildContent("put"));
|
|
|
|
if (!canceled) {
|
|
|
|
new Thread(new FileUploader()).start();
|
|
|
|
}
|
2016-06-15 10:33:59 +00:00
|
|
|
return;
|
2015-06-28 09:19:07 +00:00
|
|
|
} catch (MalformedURLException e) {
|
2016-06-15 10:33:59 +00:00
|
|
|
//fall through
|
2015-06-28 09:19:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-15 10:33:59 +00:00
|
|
|
Log.d(Config.LOGTAG,account.getJid().toString()+": invalid response to slot request "+packet);
|
2016-10-26 10:26:04 +00:00
|
|
|
fail(IqParser.extractErrorMessage(packet));
|
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
|
|
|
}
|
|
|
|
|
|
|
|
private class FileUploader implements Runnable {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
this.upload();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void upload() {
|
|
|
|
OutputStream os = null;
|
|
|
|
HttpURLConnection connection = null;
|
2015-08-10 17:48:36 +00:00
|
|
|
PowerManager.WakeLock wakeLock = mHttpConnectionManager.createWakeLock("http_upload_"+message.getUuid());
|
2015-06-28 09:19:07 +00:00
|
|
|
try {
|
2015-08-10 17:48:36 +00:00
|
|
|
wakeLock.acquire();
|
2015-06-28 09:19:07 +00:00
|
|
|
Log.d(Config.LOGTAG, "uploading to " + mPutUrl.toString());
|
2015-11-28 19:11:38 +00:00
|
|
|
if (mUseTor) {
|
|
|
|
connection = (HttpURLConnection) mPutUrl.openConnection(mHttpConnectionManager.getProxy());
|
|
|
|
} else {
|
|
|
|
connection = (HttpURLConnection) mPutUrl.openConnection();
|
|
|
|
}
|
2015-07-10 12:14:45 +00:00
|
|
|
if (connection instanceof HttpsURLConnection) {
|
|
|
|
mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, true);
|
|
|
|
}
|
2015-06-28 09:19:07 +00:00
|
|
|
connection.setRequestMethod("PUT");
|
2015-08-01 20:23:58 +00:00
|
|
|
connection.setFixedLengthStreamingMode((int) file.getExpectedSize());
|
2015-08-01 20:37:17 +00:00
|
|
|
connection.setRequestProperty("Content-Type", mime == null ? "application/octet-stream" : mime);
|
2015-09-23 16:05:51 +00:00
|
|
|
connection.setRequestProperty("User-Agent",mXmppConnectionService.getIqGenerator().getIdentityName());
|
2015-06-28 09:19:07 +00:00
|
|
|
connection.setDoOutput(true);
|
2016-09-20 18:02:25 +00:00
|
|
|
connection.setConnectTimeout(Config.SOCKET_TIMEOUT * 1000);
|
|
|
|
connection.setReadTimeout(Config.SOCKET_TIMEOUT * 1000);
|
2015-06-28 09:19:07 +00:00
|
|
|
connection.connect();
|
|
|
|
os = connection.getOutputStream();
|
|
|
|
transmitted = 0;
|
2016-09-20 18:02:25 +00:00
|
|
|
int count;
|
2015-06-28 09:19:07 +00:00
|
|
|
byte[] buffer = new byte[4096];
|
2015-08-01 20:23:58 +00:00
|
|
|
while (((count = mFileInputStream.read(buffer)) != -1) && !canceled) {
|
2015-06-28 09:19:07 +00:00
|
|
|
transmitted += count;
|
|
|
|
os.write(buffer, 0, count);
|
2017-01-12 15:02:09 +00:00
|
|
|
mHttpConnectionManager.updateConversationUi(false);
|
2015-06-28 09:19:07 +00:00
|
|
|
}
|
|
|
|
os.flush();
|
|
|
|
os.close();
|
2015-08-01 20:23:58 +00:00
|
|
|
mFileInputStream.close();
|
2015-06-28 09:19:07 +00:00
|
|
|
int code = connection.getResponseCode();
|
2015-07-19 11:36:02 +00:00
|
|
|
if (code == 200 || code == 201) {
|
2015-06-28 09:19:07 +00:00
|
|
|
Log.d(Config.LOGTAG, "finished uploading file");
|
2015-06-29 00:04:58 +00:00
|
|
|
if (key != null) {
|
|
|
|
mGetUrl = new URL(mGetUrl.toString() + "#" + CryptoHelper.bytesToHex(key));
|
|
|
|
}
|
2015-06-30 15:15:02 +00:00
|
|
|
mXmppConnectionService.getFileBackend().updateFileParams(message, mGetUrl);
|
2016-03-23 18:23:22 +00:00
|
|
|
mXmppConnectionService.getFileBackend().updateMediaScanner(file);
|
2015-07-10 13:11:03 +00:00
|
|
|
message.setTransferable(null);
|
2015-06-28 11:14:21 +00:00
|
|
|
message.setCounterpart(message.getConversation().getJid().toBareJid());
|
2015-06-29 13:38:16 +00:00
|
|
|
if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
|
|
|
|
mXmppConnectionService.getPgpEngine().encrypt(message, new UiCallback<Message>() {
|
|
|
|
@Override
|
|
|
|
public void success(Message message) {
|
2015-07-20 16:11:33 +00:00
|
|
|
mXmppConnectionService.resendMessage(message,delayed);
|
2015-06-29 13:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void error(int errorCode, Message object) {
|
2016-06-15 10:33:59 +00:00
|
|
|
Log.d(Config.LOGTAG,"pgp encryption failed");
|
2016-10-26 10:26:04 +00:00
|
|
|
fail("pgp encryption failed");
|
2015-06-29 13:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void userInputRequried(PendingIntent pi, Message object) {
|
2016-10-26 10:26:04 +00:00
|
|
|
fail("pgp encryption failed");
|
2015-06-29 13:38:16 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2015-07-29 21:45:37 +00:00
|
|
|
mXmppConnectionService.resendMessage(message, delayed);
|
2015-06-29 13:38:16 +00:00
|
|
|
}
|
2015-06-28 09:19:07 +00:00
|
|
|
} else {
|
2016-05-31 15:19:56 +00:00
|
|
|
Log.d(Config.LOGTAG,"http upload failed because response code was "+code);
|
2016-10-26 10:26:04 +00:00
|
|
|
fail("http upload failed because response code was "+code);
|
2015-06-28 09:19:07 +00:00
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
2015-07-29 21:45:37 +00:00
|
|
|
e.printStackTrace();
|
2015-07-28 20:26:29 +00:00
|
|
|
Log.d(Config.LOGTAG,"http upload failed "+e.getMessage());
|
2016-10-26 10:26:04 +00:00
|
|
|
fail(e.getMessage());
|
2015-06-28 09:19:07 +00:00
|
|
|
} finally {
|
2015-08-01 20:23:58 +00:00
|
|
|
FileBackend.close(mFileInputStream);
|
2015-06-28 09:19:07 +00:00
|
|
|
FileBackend.close(os);
|
|
|
|
if (connection != null) {
|
|
|
|
connection.disconnect();
|
|
|
|
}
|
2015-08-10 17:48:36 +00:00
|
|
|
wakeLock.release();
|
2015-06-28 09:19:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|