support new http upload namespace

This commit is contained in:
Daniel Gultsch 2018-01-28 14:17:42 +01:00
parent 4541ac8b7b
commit e48788e821
3 changed files with 102 additions and 93 deletions

View file

@ -342,11 +342,9 @@ public class IqGenerator extends AbstractGenerator {
IqPacket packet = new IqPacket(IqPacket.TYPE.GET); IqPacket packet = new IqPacket(IqPacket.TYPE.GET);
packet.setTo(host); packet.setTo(host);
Element request = packet.addChild("request", Namespace.HTTP_UPLOAD); Element request = packet.addChild("request", Namespace.HTTP_UPLOAD);
request.addChild("filename").setContent(convertFilename(file.getName())); request.setAttribute("filename",convertFilename(file.getName()));
request.addChild("size").setContent(String.valueOf(file.getExpectedSize())); request.setAttribute("size",file.getExpectedSize());
if (mime != null) { request.setAttribute("content-type",mime);
request.addChild("content-type").setContent(mime);
}
return packet; return packet;
} }

View file

@ -11,6 +11,7 @@ import java.io.OutputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.HashMap;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
@ -26,7 +27,6 @@ import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.utils.CryptoHelper; import eu.siacs.conversations.utils.CryptoHelper;
import eu.siacs.conversations.xml.Namespace; import eu.siacs.conversations.xml.Namespace;
import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.OnIqPacketReceived;
import eu.siacs.conversations.xmpp.jid.Jid; import eu.siacs.conversations.xmpp.jid.Jid;
import eu.siacs.conversations.xmpp.stanzas.IqPacket; import eu.siacs.conversations.xmpp.stanzas.IqPacket;
@ -43,6 +43,7 @@ public class HttpUploadConnection implements Transferable {
private String mime; private String mime;
private URL mGetUrl; private URL mGetUrl;
private URL mPutUrl; private URL mPutUrl;
private HashMap<String,String> mPutHeaders;
private boolean mUseTor = false; private boolean mUseTor = false;
private byte[] key = null; private byte[] key = null;
@ -122,17 +123,31 @@ public class HttpUploadConnection implements Transferable {
this.mFileInputStream = pair.first; this.mFileInputStream = pair.first;
Jid host = account.getXmppConnection().findDiscoItemByFeature(Namespace.HTTP_UPLOAD); Jid host = account.getXmppConnection().findDiscoItemByFeature(Namespace.HTTP_UPLOAD);
IqPacket request = mXmppConnectionService.getIqGenerator().requestHttpUploadSlot(host,file,mime); IqPacket request = mXmppConnectionService.getIqGenerator().requestHttpUploadSlot(host,file,mime);
mXmppConnectionService.sendIqPacket(account, request, new OnIqPacketReceived() { mXmppConnectionService.sendIqPacket(account, request, (account, packet) -> {
@Override
public void onIqPacketReceived(Account account, IqPacket packet) {
if (packet.getType() == IqPacket.TYPE.RESULT) { if (packet.getType() == IqPacket.TYPE.RESULT) {
Element slot = packet.findChild("slot", Namespace.HTTP_UPLOAD); Element slot = packet.findChild("slot", Namespace.HTTP_UPLOAD);
if (slot != null) { if (slot != null) {
try { try {
mGetUrl = new URL(slot.findChildContent("get")); final Element put = slot.findChild("put");
mPutUrl = new URL(slot.findChildContent("put")); final Element get = slot.findChild("get");
final String putUrl = put == null ? null : put.getAttribute("url");
final String getUrl = get == null ? null : get.getAttribute("url");
if (getUrl != null && putUrl != null) {
this.mGetUrl = new URL(getUrl);
this.mPutUrl = new URL(putUrl);
this.mPutHeaders = new HashMap<>();
for(Element child : put.getChildren()) {
if ("header".equals(child.getName())) {
String name = child.getAttribute("name");
String value = child.getContent();
if (name != null && value != null && !name.trim().contains("\n") && !value.trim().contains("\n")) {
this.mPutHeaders.put(name.trim(),value.trim());
}
}
}
}
if (!canceled) { if (!canceled) {
new Thread(new FileUploader()).start(); new Thread(this::upload).start();
} }
return; return;
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
@ -142,27 +157,19 @@ public class HttpUploadConnection implements Transferable {
} }
Log.d(Config.LOGTAG,account.getJid().toString()+": invalid response to slot request "+packet); Log.d(Config.LOGTAG,account.getJid().toString()+": invalid response to slot request "+packet);
fail(IqParser.extractErrorMessage(packet)); fail(IqParser.extractErrorMessage(packet));
}
}); });
message.setTransferable(this); message.setTransferable(this);
mXmppConnectionService.markMessage(message, Message.STATUS_UNSEND); mXmppConnectionService.markMessage(message, Message.STATUS_UNSEND);
} }
private class FileUploader implements Runnable {
@Override
public void run() {
this.upload();
}
private void upload() { private void upload() {
OutputStream os = null; OutputStream os = null;
HttpURLConnection connection = null; HttpURLConnection connection = null;
PowerManager.WakeLock wakeLock = mHttpConnectionManager.createWakeLock("http_upload_"+message.getUuid()); PowerManager.WakeLock wakeLock = mHttpConnectionManager.createWakeLock("http_upload_"+message.getUuid());
try { try {
wakeLock.acquire();
final int expectedFileSize = (int) file.getExpectedSize(); final int expectedFileSize = (int) file.getExpectedSize();
final int readTimeout = (expectedFileSize / 2048) + Config.SOCKET_TIMEOUT; //assuming a minimum transfer speed of 16kbit/s final int readTimeout = (expectedFileSize / 2048) + Config.SOCKET_TIMEOUT; //assuming a minimum transfer speed of 16kbit/s
wakeLock.acquire(readTimeout);
Log.d(Config.LOGTAG, "uploading to " + mPutUrl.toString()+ " w/ read timeout of "+readTimeout+"s"); Log.d(Config.LOGTAG, "uploading to " + mPutUrl.toString()+ " w/ read timeout of "+readTimeout+"s");
if (mUseTor) { if (mUseTor) {
connection = (HttpURLConnection) mPutUrl.openConnection(mHttpConnectionManager.getProxy()); connection = (HttpURLConnection) mPutUrl.openConnection(mHttpConnectionManager.getProxy());
@ -176,6 +183,11 @@ public class HttpUploadConnection implements Transferable {
connection.setFixedLengthStreamingMode(expectedFileSize); connection.setFixedLengthStreamingMode(expectedFileSize);
connection.setRequestProperty("Content-Type", mime == null ? "application/octet-stream" : mime); connection.setRequestProperty("Content-Type", mime == null ? "application/octet-stream" : mime);
connection.setRequestProperty("User-Agent",mXmppConnectionService.getIqGenerator().getIdentityName()); connection.setRequestProperty("User-Agent",mXmppConnectionService.getIqGenerator().getIdentityName());
if(mPutHeaders != null) {
for(HashMap.Entry<String,String> entry : mPutHeaders.entrySet()) {
connection.setRequestProperty(entry.getKey(),entry.getValue());
}
}
connection.setDoOutput(true); connection.setDoOutput(true);
connection.setConnectTimeout(Config.SOCKET_TIMEOUT * 1000); connection.setConnectTimeout(Config.SOCKET_TIMEOUT * 1000);
connection.setReadTimeout(readTimeout * 1000); connection.setReadTimeout(readTimeout * 1000);
@ -221,4 +233,3 @@ public class HttpUploadConnection implements Transferable {
} }
} }
} }
}

View file

@ -5,7 +5,7 @@ public final class Namespace {
public static final String ROSTER = "jabber:iq:roster"; public static final String ROSTER = "jabber:iq:roster";
public static final String REGISTER = "jabber:iq:register"; public static final String REGISTER = "jabber:iq:register";
public static final String BYTE_STREAMS = "http://jabber.org/protocol/bytestreams"; public static final String BYTE_STREAMS = "http://jabber.org/protocol/bytestreams";
public static final String HTTP_UPLOAD = "urn:xmpp:http:upload"; public static final String HTTP_UPLOAD = "urn:xmpp:http:upload:0";
public static final String STANZA_IDS = "urn:xmpp:sid:0"; public static final String STANZA_IDS = "urn:xmpp:sid:0";
public static final String MAM = "urn:xmpp:mam:2"; public static final String MAM = "urn:xmpp:mam:2";
public static final String MAM_LEGACY = "urn:xmpp:mam:0"; public static final String MAM_LEGACY = "urn:xmpp:mam:0";