1package eu.siacs.conversations.http;
2
3import android.os.PowerManager;
4import android.util.Log;
5
6import java.io.BufferedInputStream;
7import java.io.IOException;
8import java.io.InputStream;
9import java.io.OutputStream;
10import java.net.HttpURLConnection;
11import java.net.MalformedURLException;
12import java.net.URL;
13import java.util.concurrent.CancellationException;
14
15import javax.net.ssl.HttpsURLConnection;
16import javax.net.ssl.SSLHandshakeException;
17
18import eu.siacs.conversations.Config;
19import eu.siacs.conversations.R;
20import eu.siacs.conversations.entities.DownloadableFile;
21import eu.siacs.conversations.entities.Message;
22import eu.siacs.conversations.entities.Transferable;
23import eu.siacs.conversations.entities.TransferablePlaceholder;
24import eu.siacs.conversations.persistance.FileBackend;
25import eu.siacs.conversations.services.AbstractConnectionManager;
26import eu.siacs.conversations.services.XmppConnectionService;
27import eu.siacs.conversations.utils.CryptoHelper;
28import eu.siacs.conversations.utils.FileWriterException;
29
30public class HttpDownloadConnection implements Transferable {
31
32 private HttpConnectionManager mHttpConnectionManager;
33 private XmppConnectionService mXmppConnectionService;
34
35 private URL mUrl;
36 private Message message;
37 private DownloadableFile file;
38 private int mStatus = Transferable.STATUS_UNKNOWN;
39 private boolean acceptedAutomatically = false;
40 private int mProgress = 0;
41 private boolean mUseTor = false;
42 private boolean canceled = false;
43
44 public HttpDownloadConnection(HttpConnectionManager manager) {
45 this.mHttpConnectionManager = manager;
46 this.mXmppConnectionService = manager.getXmppConnectionService();
47 this.mUseTor = mXmppConnectionService.useTorToConnect();
48 }
49
50 @Override
51 public boolean start() {
52 if (mXmppConnectionService.hasInternetConnection()) {
53 if (this.mStatus == STATUS_OFFER_CHECK_FILESIZE) {
54 checkFileSize(true);
55 } else {
56 new Thread(new FileDownloader(true)).start();
57 }
58 return true;
59 } else {
60 return false;
61 }
62 }
63
64 public void init(Message message) {
65 init(message, false);
66 }
67
68 public void init(Message message, boolean interactive) {
69 this.message = message;
70 this.message.setTransferable(this);
71 try {
72 if (message.hasFileOnRemoteHost()) {
73 mUrl = CryptoHelper.toHttpsUrl(message.getFileParams().url);
74 } else {
75 mUrl = CryptoHelper.toHttpsUrl(new URL(message.getBody().split("\n")[0]));
76 }
77 String[] parts = mUrl.getPath().toLowerCase().split("\\.");
78 String lastPart = parts.length >= 1 ? parts[parts.length - 1] : null;
79 String secondToLast = parts.length >= 2 ? parts[parts.length - 2] : null;
80 if ("pgp".equals(lastPart) || "gpg".equals(lastPart)) {
81 this.message.setEncryption(Message.ENCRYPTION_PGP);
82 } else if (message.getEncryption() != Message.ENCRYPTION_OTR
83 && message.getEncryption() != Message.ENCRYPTION_AXOLOTL) {
84 this.message.setEncryption(Message.ENCRYPTION_NONE);
85 }
86 String extension;
87 if (VALID_CRYPTO_EXTENSIONS.contains(lastPart)) {
88 extension = secondToLast;
89 } else {
90 extension = lastPart;
91 }
92 message.setRelativeFilePath(message.getUuid() + (extension != null ? ("." + extension) : ""));
93 this.file = mXmppConnectionService.getFileBackend().getFile(message, false);
94 final String reference = mUrl.getRef();
95 if (reference != null && AesGcmURLStreamHandler.IV_KEY.matcher(reference).matches()) {
96 this.file.setKeyAndIv(CryptoHelper.hexToBytes(reference));
97 }
98
99 if (this.message.getEncryption() == Message.ENCRYPTION_AXOLOTL && this.file.getKey() == null) {
100 this.message.setEncryption(Message.ENCRYPTION_NONE);
101 }
102 checkFileSize(interactive);
103 } catch (MalformedURLException e) {
104 this.cancel();
105 }
106 }
107
108 private void checkFileSize(boolean interactive) {
109 new Thread(new FileSizeChecker(interactive)).start();
110 }
111
112 @Override
113 public void cancel() {
114 this.canceled = true;
115 mHttpConnectionManager.finishConnection(this);
116 if (message.isFileOrImage()) {
117 message.setTransferable(new TransferablePlaceholder(Transferable.STATUS_DELETED));
118 } else {
119 message.setTransferable(null);
120 }
121 mHttpConnectionManager.updateConversationUi(true);
122 }
123
124 private void finish() {
125 mXmppConnectionService.getFileBackend().updateMediaScanner(file);
126 message.setTransferable(null);
127 mHttpConnectionManager.finishConnection(this);
128 boolean notify = acceptedAutomatically && !message.isRead();
129 if (message.getEncryption() == Message.ENCRYPTION_PGP) {
130 notify = message.getConversation().getAccount().getPgpDecryptionService().decrypt(message, notify);
131 }
132 mHttpConnectionManager.updateConversationUi(true);
133 if (notify) {
134 mXmppConnectionService.getNotificationService().push(message);
135 }
136 }
137
138 private void changeStatus(int status) {
139 this.mStatus = status;
140 mHttpConnectionManager.updateConversationUi(true);
141 }
142
143 private void showToastForException(Exception e) {
144 if (e instanceof java.net.UnknownHostException) {
145 mXmppConnectionService.showErrorToastInUi(R.string.download_failed_server_not_found);
146 } else if (e instanceof java.net.ConnectException) {
147 mXmppConnectionService.showErrorToastInUi(R.string.download_failed_could_not_connect);
148 } else if (e instanceof FileWriterException) {
149 mXmppConnectionService.showErrorToastInUi(R.string.download_failed_could_not_write_file);
150 } else if (!(e instanceof CancellationException)) {
151 mXmppConnectionService.showErrorToastInUi(R.string.download_failed_file_not_found);
152 }
153 }
154
155 public void updateProgress(long i) {
156 this.mProgress = (int) i;
157 mHttpConnectionManager.updateConversationUi(false);
158 }
159
160 @Override
161 public int getStatus() {
162 return this.mStatus;
163 }
164
165 @Override
166 public long getFileSize() {
167 if (this.file != null) {
168 return this.file.getExpectedSize();
169 } else {
170 return 0;
171 }
172 }
173
174 @Override
175 public int getProgress() {
176 return this.mProgress;
177 }
178
179 private class FileSizeChecker implements Runnable {
180
181 private boolean interactive = false;
182
183 public FileSizeChecker(boolean interactive) {
184 this.interactive = interactive;
185 }
186
187 @Override
188 public void run() {
189 long size;
190 try {
191 size = retrieveFileSize();
192 } catch (Exception e) {
193 changeStatus(STATUS_OFFER_CHECK_FILESIZE);
194 Log.d(Config.LOGTAG, "io exception in http file size checker: " + e.getMessage());
195 if (interactive) {
196 showToastForException(e);
197 } else {
198 HttpDownloadConnection.this.acceptedAutomatically = false;
199 HttpDownloadConnection.this.mXmppConnectionService.getNotificationService().push(message);
200 }
201 cancel();
202 return;
203 }
204 file.setExpectedSize(size);
205 message.resetFileParams();
206 if (mHttpConnectionManager.hasStoragePermission()
207 && size <= mHttpConnectionManager.getAutoAcceptFileSize()
208 && mXmppConnectionService.isDataSaverDisabled()) {
209 HttpDownloadConnection.this.acceptedAutomatically = true;
210 new Thread(new FileDownloader(interactive)).start();
211 } else {
212 changeStatus(STATUS_OFFER);
213 HttpDownloadConnection.this.acceptedAutomatically = false;
214 HttpDownloadConnection.this.mXmppConnectionService.getNotificationService().push(message);
215 }
216 }
217
218 private long retrieveFileSize() throws IOException {
219 try {
220 Log.d(Config.LOGTAG, "retrieve file size. interactive:" + String.valueOf(interactive));
221 changeStatus(STATUS_CHECKING);
222 HttpURLConnection connection;
223 if (mUseTor) {
224 connection = (HttpURLConnection) mUrl.openConnection(mHttpConnectionManager.getProxy());
225 } else {
226 connection = (HttpURLConnection) mUrl.openConnection();
227 }
228 connection.setRequestMethod("HEAD");
229 Log.d(Config.LOGTAG, "url: " + connection.getURL().toString());
230 Log.d(Config.LOGTAG, "connection: " + connection.toString());
231 connection.setRequestProperty("User-Agent", mXmppConnectionService.getIqGenerator().getIdentityName());
232 if (connection instanceof HttpsURLConnection) {
233 mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, interactive);
234 }
235 connection.setConnectTimeout(Config.SOCKET_TIMEOUT * 1000);
236 connection.setReadTimeout(Config.SOCKET_TIMEOUT * 1000);
237 connection.connect();
238 String contentLength = connection.getHeaderField("Content-Length");
239 connection.disconnect();
240 if (contentLength == null) {
241 throw new IOException("no content-length found in HEAD response");
242 }
243 return Long.parseLong(contentLength, 10);
244 } catch (IOException e) {
245 Log.d(Config.LOGTAG, "io exception during HEAD " + e.getMessage());
246 throw e;
247 } catch (NumberFormatException e) {
248 throw new IOException();
249 }
250 }
251
252 }
253
254 private class FileDownloader implements Runnable {
255
256 private boolean interactive = false;
257
258 private OutputStream os;
259
260 public FileDownloader(boolean interactive) {
261 this.interactive = interactive;
262 }
263
264 @Override
265 public void run() {
266 try {
267 changeStatus(STATUS_DOWNLOADING);
268 download();
269 updateImageBounds();
270 finish();
271 } catch (SSLHandshakeException e) {
272 changeStatus(STATUS_OFFER);
273 } catch (Exception e) {
274 if (interactive) {
275 showToastForException(e);
276 } else {
277 HttpDownloadConnection.this.acceptedAutomatically = false;
278 HttpDownloadConnection.this.mXmppConnectionService.getNotificationService().push(message);
279 }
280 cancel();
281 }
282 }
283
284 private void download() throws Exception {
285 InputStream is = null;
286 HttpURLConnection connection = null;
287 PowerManager.WakeLock wakeLock = mHttpConnectionManager.createWakeLock("http_download_" + message.getUuid());
288 try {
289 wakeLock.acquire();
290 if (mUseTor) {
291 connection = (HttpURLConnection) mUrl.openConnection(mHttpConnectionManager.getProxy());
292 } else {
293 connection = (HttpURLConnection) mUrl.openConnection();
294 }
295 if (connection instanceof HttpsURLConnection) {
296 mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, interactive);
297 }
298 connection.setRequestProperty("User-Agent", mXmppConnectionService.getIqGenerator().getIdentityName());
299 final boolean tryResume = file.exists() && file.getKey() == null && file.getSize() > 0;
300 long resumeSize = 0;
301 long expected = file.getExpectedSize();
302 if (tryResume) {
303 resumeSize = file.getSize();
304 Log.d(Config.LOGTAG, "http download trying resume after" + resumeSize + " of " + expected);
305 connection.setRequestProperty("Range", "bytes=" + resumeSize + "-");
306 }
307 connection.setConnectTimeout(Config.SOCKET_TIMEOUT * 1000);
308 connection.setReadTimeout(Config.SOCKET_TIMEOUT * 1000);
309 connection.connect();
310 is = new BufferedInputStream(connection.getInputStream());
311 final String contentRange = connection.getHeaderField("Content-Range");
312 boolean serverResumed = tryResume && contentRange != null && contentRange.startsWith("bytes " + resumeSize + "-");
313 long transmitted = 0;
314 if (tryResume && serverResumed) {
315 Log.d(Config.LOGTAG, "server resumed");
316 transmitted = file.getSize();
317 updateProgress(Math.round(((double) transmitted / expected) * 100));
318 os = AbstractConnectionManager.createAppendedOutputStream(file);
319 if (os == null) {
320 throw new FileWriterException();
321 }
322 } else {
323 long reportedContentLengthOnGet;
324 try {
325 reportedContentLengthOnGet = Long.parseLong(connection.getHeaderField("Content-Length"));
326 } catch (NumberFormatException | NullPointerException e) {
327 reportedContentLengthOnGet = 0;
328 }
329 if (expected != reportedContentLengthOnGet) {
330 Log.d(Config.LOGTAG, "content-length reported on GET (" + reportedContentLengthOnGet + ") did not match Content-Length reported on HEAD (" + expected + ")");
331 }
332 file.getParentFile().mkdirs();
333 if (!file.exists() && !file.createNewFile()) {
334 throw new FileWriterException();
335 }
336 os = AbstractConnectionManager.createOutputStream(file, true);
337 }
338 int count;
339 byte[] buffer = new byte[4096];
340 while ((count = is.read(buffer)) != -1) {
341 transmitted += count;
342 try {
343 os.write(buffer, 0, count);
344 } catch (IOException e) {
345 throw new FileWriterException();
346 }
347 updateProgress(Math.round(((double) transmitted / expected) * 100));
348 if (canceled) {
349 throw new CancellationException();
350 }
351 }
352 try {
353 os.flush();
354 } catch (IOException e) {
355 throw new FileWriterException();
356 }
357 } catch (CancellationException | IOException e) {
358 Log.d(Config.LOGTAG, "http download failed " + e.getMessage());
359 throw e;
360 } finally {
361 FileBackend.close(os);
362 FileBackend.close(is);
363 if (connection != null) {
364 connection.disconnect();
365 }
366 wakeLock.release();
367 }
368 }
369
370 private void updateImageBounds() {
371 message.setType(Message.TYPE_FILE);
372 final URL url;
373 final String ref = mUrl.getRef();
374 if (ref != null && AesGcmURLStreamHandler.IV_KEY.matcher(ref).matches()) {
375 url = CryptoHelper.toAesGcmUrl(mUrl);
376 } else {
377 url = mUrl;
378 }
379 mXmppConnectionService.getFileBackend().updateFileParams(message, url);
380 mXmppConnectionService.updateMessage(message);
381 }
382
383 }
384}