2 * Copyright (C) 2017-2018 Tobias Brunner
4 * Copyright (C) secunet Security Networks AG
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 package org
.strongswan
.android
.logic
;
19 import java
.io
.BufferedOutputStream
;
20 import java
.io
.ByteArrayOutputStream
;
21 import java
.io
.IOException
;
22 import java
.io
.InputStream
;
23 import java
.io
.OutputStream
;
24 import java
.net
.HttpURLConnection
;
25 import java
.net
.Proxy
;
26 import java
.net
.SocketTimeoutException
;
28 import java
.util
.ArrayList
;
29 import java
.util
.concurrent
.CancellationException
;
30 import java
.util
.concurrent
.ExecutionException
;
31 import java
.util
.concurrent
.ExecutorService
;
32 import java
.util
.concurrent
.Executors
;
33 import java
.util
.concurrent
.Future
;
34 import java
.util
.concurrent
.TimeUnit
;
35 import java
.util
.concurrent
.TimeoutException
;
37 import androidx
.annotation
.Keep
;
40 public class SimpleFetcher
42 private static ExecutorService mExecutor
= Executors
.newCachedThreadPool();
43 private static Object mLock
= new Object();
44 private static ArrayList
<Future
> mFutures
= new ArrayList
<>();
45 private static boolean mDisabled
;
47 public static byte[] fetch(String uri
, byte[] data
, String contentType
)
49 Future
<byte[]> future
;
57 future
= mExecutor
.submit(() -> {
58 URL url
= new URL(uri
);
59 HttpURLConnection conn
= (HttpURLConnection
) url
.openConnection(Proxy
.NO_PROXY
);
60 conn
.setConnectTimeout(10000);
61 conn
.setReadTimeout(10000);
62 conn
.setRequestProperty("Connection", "close");
65 if (contentType
!= null)
67 conn
.setRequestProperty("Content-Type", contentType
);
71 conn
.setDoOutput(true);
72 conn
.setFixedLengthStreamingMode(data
.length
);
73 OutputStream out
= new BufferedOutputStream(conn
.getOutputStream());
77 return streamToArray(conn
.getInputStream());
79 catch (SocketTimeoutException e
)
94 /* this enforces a timeout as the ones set on HttpURLConnection might not work reliably */
95 return future
.get(10000, TimeUnit
.MILLISECONDS
);
97 catch (InterruptedException
|ExecutionException
|TimeoutException
|CancellationException e
)
105 mFutures
.remove(future
);
111 * Enable fetching after it has been disabled.
113 public static void enable()
122 * Disable the fetcher and abort any future requests.
124 * The native thread is not cancelable as it is working on an IKE_SA (canceling the methods of
125 * HttpURLConnection is not reliably possible anyway), so to abort while fetching we cancel the
126 * Future (causing a return from fetch() immediately) and let the executor thread continue its
127 * thing in the background.
129 * Also prevents future fetches until enabled again (e.g. if we aborted OCSP but would then
130 * block in the subsequent fetch for a CRL).
132 public static void disable()
137 for (Future future
: mFutures
)
144 private static byte[] streamToArray(InputStream in
) throws IOException
146 ByteArrayOutputStream out
= new ByteArrayOutputStream();
147 byte[] buf
= new byte[1024];
152 while ((len
= in
.read(buf
)) != -1)
154 out
.write(buf
, 0, len
);
156 return out
.toByteArray();
158 catch (IOException e
)