1package eu.siacs.conversations.http;
2
3import android.os.PowerManager;
4import android.util.Log;
5
6import java.io.FileInputStream;
7import java.io.InputStream;
8import java.io.OutputStream;
9import java.net.HttpURLConnection;
10import java.net.URL;
11import java.util.Arrays;
12import java.util.HashMap;
13import java.util.List;
14import java.util.Scanner;
15
16import javax.net.ssl.HttpsURLConnection;
17
18import eu.siacs.conversations.Config;
19import eu.siacs.conversations.entities.Account;
20import eu.siacs.conversations.entities.DownloadableFile;
21import eu.siacs.conversations.entities.Message;
22import eu.siacs.conversations.entities.Transferable;
23import eu.siacs.conversations.persistance.FileBackend;
24import eu.siacs.conversations.services.AbstractConnectionManager;
25import eu.siacs.conversations.services.XmppConnectionService;
26import eu.siacs.conversations.utils.Checksum;
27import eu.siacs.conversations.utils.CryptoHelper;
28import eu.siacs.conversations.utils.WakeLockHelper;
29
30public class HttpUploadConnection implements Transferable {
31
32 static final List<String> WHITE_LISTED_HEADERS = Arrays.asList(
33 "Authorization",
34 "Cookie",
35 "Expires"
36 );
37
38 private final HttpConnectionManager mHttpConnectionManager;
39 private final XmppConnectionService mXmppConnectionService;
40 private final SlotRequester mSlotRequester;
41 private final Method method;
42 private final boolean mUseTor;
43 private boolean cancelled = false;
44 private boolean delayed = false;
45 private DownloadableFile file;
46 private final Message message;
47 private String mime;
48 private SlotRequester.Slot slot;
49 private byte[] key = null;
50
51 private long transmitted = 0;
52
53 public HttpUploadConnection(Message message, Method method, HttpConnectionManager httpConnectionManager) {
54 this.message = message;
55 this.method = method;
56 this.mHttpConnectionManager = httpConnectionManager;
57 this.mXmppConnectionService = httpConnectionManager.getXmppConnectionService();
58 this.mSlotRequester = new SlotRequester(this.mXmppConnectionService);
59 this.mUseTor = mXmppConnectionService.useTorToConnect();
60 }
61
62 @Override
63 public boolean start() {
64 return false;
65 }
66
67 @Override
68 public int getStatus() {
69 return STATUS_UPLOADING;
70 }
71
72 @Override
73 public long getFileSize() {
74 return file == null ? 0 : file.getExpectedSize();
75 }
76
77 @Override
78 public int getProgress() {
79 if (file == null) {
80 return 0;
81 }
82 return (int) ((((double) transmitted) / file.getExpectedSize()) * 100);
83 }
84
85 @Override
86 public void cancel() {
87 this.cancelled = true;
88 }
89
90 private void fail(String errorMessage) {
91 finish();
92 mXmppConnectionService.markMessage(message, Message.STATUS_SEND_FAILED, cancelled ? Message.ERROR_MESSAGE_CANCELLED : errorMessage);
93 }
94
95 private void finish() {
96 mHttpConnectionManager.finishUploadConnection(this);
97 message.setTransferable(null);
98 }
99
100 public void init(boolean delay) {
101 final Account account = message.getConversation().getAccount();
102 this.file = mXmppConnectionService.getFileBackend().getFile(message, false);
103 if (message.getEncryption() == Message.ENCRYPTION_PGP || message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
104 this.mime = "application/pgp-encrypted";
105 } else {
106 this.mime = this.file.getMimeType();
107 }
108 this.delayed = delay;
109 if (Config.ENCRYPT_ON_HTTP_UPLOADED
110 || message.getEncryption() == Message.ENCRYPTION_AXOLOTL
111 || message.getEncryption() == Message.ENCRYPTION_OTR) {
112 this.key = new byte[48];
113 mXmppConnectionService.getRNG().nextBytes(this.key);
114 this.file.setKeyAndIv(this.key);
115 }
116
117 final String md5;
118
119 if (method == Method.P1_S3) {
120 try {
121 md5 = Checksum.md5(AbstractConnectionManager.upgrade(file, new FileInputStream(file)));
122 } catch (Exception e) {
123 Log.d(Config.LOGTAG, account.getJid().asBareJid()+": unable to calculate md5()", e);
124 fail(e.getMessage());
125 return;
126 }
127 } else {
128 md5 = null;
129 }
130
131 this.file.setExpectedSize(file.getSize() + (file.getKey() != null ? 16 : 0));
132 message.resetFileParams();
133 this.mSlotRequester.request(method, account, file, mime, md5, new SlotRequester.OnSlotRequested() {
134 @Override
135 public void success(SlotRequester.Slot slot) {
136 if (!cancelled) {
137 HttpUploadConnection.this.slot = slot;
138 new Thread(HttpUploadConnection.this::upload).start();
139 }
140 }
141
142 @Override
143 public void failure(String message) {
144 fail(message);
145 }
146 });
147 message.setTransferable(this);
148 mXmppConnectionService.markMessage(message, Message.STATUS_UNSEND);
149 }
150
151 private void upload() {
152 OutputStream os = null;
153 InputStream fileInputStream = null;
154 HttpURLConnection connection = null;
155 PowerManager.WakeLock wakeLock = mHttpConnectionManager.createWakeLock("http_upload_"+message.getUuid());
156 try {
157 fileInputStream = new FileInputStream(file);
158 final int expectedFileSize = (int) file.getExpectedSize();
159 final int readTimeout = (expectedFileSize / 2048) + Config.SOCKET_TIMEOUT; //assuming a minimum transfer speed of 16kbit/s
160 wakeLock.acquire(readTimeout);
161 Log.d(Config.LOGTAG, "uploading to " + slot.getPutUrl().toString()+ " w/ read timeout of "+readTimeout+"s");
162 if (mUseTor || message.getConversation().getAccount().isOnion()) {
163 connection = (HttpURLConnection) slot.getPutUrl().openConnection(HttpConnectionManager.getProxy());
164 } else {
165 connection = (HttpURLConnection) slot.getPutUrl().openConnection();
166 }
167 if (connection instanceof HttpsURLConnection) {
168 mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, true);
169 }
170 connection.setUseCaches(false);
171 connection.setRequestMethod("PUT");
172 connection.setFixedLengthStreamingMode(expectedFileSize);
173 connection.setRequestProperty("User-Agent",mXmppConnectionService.getIqGenerator().getUserAgent());
174 if(slot.getHeaders() != null) {
175 for(HashMap.Entry<String,String> entry : slot.getHeaders().entrySet()) {
176 connection.setRequestProperty(entry.getKey(),entry.getValue());
177 }
178 }
179 connection.setDoOutput(true);
180 connection.setDoInput(true);
181 connection.setConnectTimeout(Config.SOCKET_TIMEOUT * 1000);
182 connection.setReadTimeout(readTimeout * 1000);
183 connection.connect();
184 final InputStream innerInputStream = AbstractConnectionManager.upgrade(file, fileInputStream);
185 os = connection.getOutputStream();
186 transmitted = 0;
187 int count;
188 byte[] buffer = new byte[4096];
189 while (((count = innerInputStream.read(buffer)) != -1) && !cancelled) {
190 transmitted += count;
191 os.write(buffer, 0, count);
192 mHttpConnectionManager.updateConversationUi(false);
193 }
194 os.flush();
195 os.close();
196 int code = connection.getResponseCode();
197 InputStream is = connection.getErrorStream();
198 if (is != null) {
199 try (Scanner scanner = new Scanner(is)) {
200 scanner.useDelimiter("\\Z");
201 Log.d(Config.LOGTAG, "body: " + scanner.next());
202 }
203 }
204 if (code == 200 || code == 201) {
205 Log.d(Config.LOGTAG, "finished uploading file");
206 final URL get;
207 if (key != null) {
208 if (method == Method.P1_S3) {
209 get = new URL(slot.getGetUrl().toString()+"#"+CryptoHelper.bytesToHex(key));
210 } else {
211 get = CryptoHelper.toAesGcmUrl(new URL(slot.getGetUrl().toString() + "#" + CryptoHelper.bytesToHex(key)));
212 }
213 } else {
214 get = slot.getGetUrl();
215 }
216 mXmppConnectionService.getFileBackend().updateFileParams(message, get);
217 mXmppConnectionService.getFileBackend().updateMediaScanner(file);
218 finish();
219 message.setCounterpart(message.getConversation().getJid().asBareJid());
220 mXmppConnectionService.resendMessage(message, delayed);
221 } else {
222 Log.d(Config.LOGTAG,"http upload failed because response code was "+code);
223 fail("http upload failed because response code was "+code);
224 }
225 } catch (Exception e) {
226 e.printStackTrace();
227 Log.d(Config.LOGTAG,"http upload failed "+e.getMessage());
228 fail(e.getMessage());
229 } finally {
230 FileBackend.close(fileInputStream);
231 FileBackend.close(os);
232 if (connection != null) {
233 connection.disconnect();
234 }
235 WakeLockHelper.release(wakeLock);
236 }
237 }
238
239 public Message getMessage() {
240 return message;
241 }
242}