@capacitor/android 4.1.0 → 4.1.1-dev-7e3589f9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +633 -0
- package/LICENSE +17 -19
- package/capacitor/src/main/assets/native-bridge.js +203 -0
- package/capacitor/src/main/java/com/getcapacitor/Bridge.java +2 -0
- package/capacitor/src/main/java/com/getcapacitor/JSValue.java +65 -0
- package/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorCookieManager.java +172 -0
- package/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorCookies.java +122 -0
- package/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorHttp.java +75 -0
- package/capacitor/src/main/java/com/getcapacitor/plugin/util/CapacitorHttpUrlConnection.java +381 -0
- package/capacitor/src/main/java/com/getcapacitor/plugin/util/HttpRequestHandler.java +409 -0
- package/capacitor/src/main/java/com/getcapacitor/plugin/util/ICapacitorHttpUrlConnection.java +15 -0
- package/capacitor/src/main/java/com/getcapacitor/plugin/util/MimeType.java +17 -0
- package/package.json +3 -2
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
package com.getcapacitor.plugin.util;
|
|
2
|
+
|
|
3
|
+
import android.os.Build;
|
|
4
|
+
import android.os.LocaleList;
|
|
5
|
+
import android.text.TextUtils;
|
|
6
|
+
import com.getcapacitor.JSArray;
|
|
7
|
+
import com.getcapacitor.JSObject;
|
|
8
|
+
import com.getcapacitor.JSValue;
|
|
9
|
+
import com.getcapacitor.PluginCall;
|
|
10
|
+
import java.io.DataOutputStream;
|
|
11
|
+
import java.io.IOException;
|
|
12
|
+
import java.io.InputStream;
|
|
13
|
+
import java.net.HttpURLConnection;
|
|
14
|
+
import java.net.ProtocolException;
|
|
15
|
+
import java.net.SocketTimeoutException;
|
|
16
|
+
import java.net.URL;
|
|
17
|
+
import java.net.URLEncoder;
|
|
18
|
+
import java.net.UnknownServiceException;
|
|
19
|
+
import java.nio.charset.StandardCharsets;
|
|
20
|
+
import java.util.Iterator;
|
|
21
|
+
import java.util.List;
|
|
22
|
+
import java.util.Locale;
|
|
23
|
+
import java.util.Map;
|
|
24
|
+
import org.json.JSONException;
|
|
25
|
+
|
|
26
|
+
public class CapacitorHttpUrlConnection implements ICapacitorHttpUrlConnection {
|
|
27
|
+
|
|
28
|
+
private final HttpURLConnection connection;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Make a new CapacitorHttpUrlConnection instance, which wraps around HttpUrlConnection
|
|
32
|
+
* and provides some helper functions for setting request headers and the request body
|
|
33
|
+
* @param conn the base HttpUrlConnection. You can pass the value from
|
|
34
|
+
* {@code (HttpUrlConnection) URL.openConnection()}
|
|
35
|
+
*/
|
|
36
|
+
public CapacitorHttpUrlConnection(HttpURLConnection conn) {
|
|
37
|
+
connection = conn;
|
|
38
|
+
this.setDefaultRequestProperties();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Returns the underlying HttpUrlConnection value
|
|
43
|
+
* @return the underlying HttpUrlConnection value
|
|
44
|
+
*/
|
|
45
|
+
public HttpURLConnection getHttpConnection() {
|
|
46
|
+
return connection;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Set the value of the {@code allowUserInteraction} field of
|
|
51
|
+
* this {@code URLConnection}.
|
|
52
|
+
*
|
|
53
|
+
* @param isAllowedInteraction the new value.
|
|
54
|
+
* @throws IllegalStateException if already connected
|
|
55
|
+
*/
|
|
56
|
+
public void setAllowUserInteraction(boolean isAllowedInteraction) {
|
|
57
|
+
connection.setAllowUserInteraction(isAllowedInteraction);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Set the method for the URL request, one of:
|
|
62
|
+
* <UL>
|
|
63
|
+
* <LI>GET
|
|
64
|
+
* <LI>POST
|
|
65
|
+
* <LI>HEAD
|
|
66
|
+
* <LI>OPTIONS
|
|
67
|
+
* <LI>PUT
|
|
68
|
+
* <LI>DELETE
|
|
69
|
+
* <LI>TRACE
|
|
70
|
+
* </UL> are legal, subject to protocol restrictions. The default
|
|
71
|
+
* method is GET.
|
|
72
|
+
*
|
|
73
|
+
* @param method the HTTP method
|
|
74
|
+
* @exception ProtocolException if the method cannot be reset or if
|
|
75
|
+
* the requested method isn't valid for HTTP.
|
|
76
|
+
* @exception SecurityException if a security manager is set and the
|
|
77
|
+
* method is "TRACE", but the "allowHttpTrace"
|
|
78
|
+
* NetPermission is not granted.
|
|
79
|
+
*/
|
|
80
|
+
public void setRequestMethod(String method) throws ProtocolException {
|
|
81
|
+
connection.setRequestMethod(method);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Sets a specified timeout value, in milliseconds, to be used
|
|
86
|
+
* when opening a communications link to the resource referenced
|
|
87
|
+
* by this URLConnection. If the timeout expires before the
|
|
88
|
+
* connection can be established, a
|
|
89
|
+
* java.net.SocketTimeoutException is raised. A timeout of zero is
|
|
90
|
+
* interpreted as an infinite timeout.
|
|
91
|
+
*
|
|
92
|
+
* <p><strong>Warning</strong>: If the hostname resolves to multiple IP
|
|
93
|
+
* addresses, Android's default implementation of {@link HttpURLConnection}
|
|
94
|
+
* will try each in
|
|
95
|
+
* <a href="http://www.ietf.org/rfc/rfc3484.txt">RFC 3484</a> order. If
|
|
96
|
+
* connecting to each of these addresses fails, multiple timeouts will
|
|
97
|
+
* elapse before the connect attempt throws an exception. Host names
|
|
98
|
+
* that support both IPv6 and IPv4 always have at least 2 IP addresses.
|
|
99
|
+
*
|
|
100
|
+
* @param timeout an {@code int} that specifies the connect
|
|
101
|
+
* timeout value in milliseconds
|
|
102
|
+
* @throws IllegalArgumentException if the timeout parameter is negative
|
|
103
|
+
*/
|
|
104
|
+
public void setConnectTimeout(int timeout) {
|
|
105
|
+
if (timeout < 0) {
|
|
106
|
+
throw new IllegalArgumentException("timeout can not be negative");
|
|
107
|
+
}
|
|
108
|
+
connection.setConnectTimeout(timeout);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Sets the read timeout to a specified timeout, in
|
|
113
|
+
* milliseconds. A non-zero value specifies the timeout when
|
|
114
|
+
* reading from Input stream when a connection is established to a
|
|
115
|
+
* resource. If the timeout expires before there is data available
|
|
116
|
+
* for read, a java.net.SocketTimeoutException is raised. A
|
|
117
|
+
* timeout of zero is interpreted as an infinite timeout.
|
|
118
|
+
*
|
|
119
|
+
* @param timeout an {@code int} that specifies the timeout
|
|
120
|
+
* value to be used in milliseconds
|
|
121
|
+
* @throws IllegalArgumentException if the timeout parameter is negative
|
|
122
|
+
*/
|
|
123
|
+
public void setReadTimeout(int timeout) {
|
|
124
|
+
if (timeout < 0) {
|
|
125
|
+
throw new IllegalArgumentException("timeout can not be negative");
|
|
126
|
+
}
|
|
127
|
+
connection.setReadTimeout(timeout);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Sets whether automatic HTTP redirects should be disabled
|
|
132
|
+
* @param disableRedirects the flag to determine if redirects should be followed
|
|
133
|
+
*/
|
|
134
|
+
public void setDisableRedirects(boolean disableRedirects) {
|
|
135
|
+
connection.setInstanceFollowRedirects(!disableRedirects);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Sets the request headers given a JSObject of key-value pairs
|
|
140
|
+
* @param headers the JSObject values to map to the HttpUrlConnection request headers
|
|
141
|
+
*/
|
|
142
|
+
public void setRequestHeaders(JSObject headers) {
|
|
143
|
+
Iterator<String> keys = headers.keys();
|
|
144
|
+
while (keys.hasNext()) {
|
|
145
|
+
String key = keys.next();
|
|
146
|
+
String value = headers.getString(key);
|
|
147
|
+
connection.setRequestProperty(key, value);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Sets the value of the {@code doOutput} field for this
|
|
153
|
+
* {@code URLConnection} to the specified value.
|
|
154
|
+
* <p>
|
|
155
|
+
* A URL connection can be used for input and/or output. Set the DoOutput
|
|
156
|
+
* flag to true if you intend to use the URL connection for output,
|
|
157
|
+
* false if not. The default is false.
|
|
158
|
+
*
|
|
159
|
+
* @param shouldDoOutput the new value.
|
|
160
|
+
* @throws IllegalStateException if already connected
|
|
161
|
+
*/
|
|
162
|
+
public void setDoOutput(boolean shouldDoOutput) {
|
|
163
|
+
connection.setDoOutput(shouldDoOutput);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
*
|
|
168
|
+
* @param call
|
|
169
|
+
* @throws JSONException
|
|
170
|
+
* @throws IOException
|
|
171
|
+
*/
|
|
172
|
+
public void setRequestBody(PluginCall call, JSValue body) throws JSONException, IOException {
|
|
173
|
+
String contentType = connection.getRequestProperty("Content-Type");
|
|
174
|
+
String dataString = "";
|
|
175
|
+
|
|
176
|
+
if (contentType == null || contentType.isEmpty()) return;
|
|
177
|
+
|
|
178
|
+
if (contentType.contains("application/json")) {
|
|
179
|
+
JSArray jsArray = null;
|
|
180
|
+
if (body != null) {
|
|
181
|
+
dataString = body.toString();
|
|
182
|
+
} else {
|
|
183
|
+
jsArray = call.getArray("data", null);
|
|
184
|
+
}
|
|
185
|
+
if (jsArray != null) {
|
|
186
|
+
dataString = jsArray.toString();
|
|
187
|
+
} else if (body == null) {
|
|
188
|
+
dataString = call.getString("data");
|
|
189
|
+
}
|
|
190
|
+
this.writeRequestBody(dataString != null ? dataString : "");
|
|
191
|
+
} else if (contentType.contains("application/x-www-form-urlencoded")) {
|
|
192
|
+
StringBuilder builder = new StringBuilder();
|
|
193
|
+
|
|
194
|
+
JSObject obj = body.toJSObject();
|
|
195
|
+
Iterator<String> keys = obj.keys();
|
|
196
|
+
while (keys.hasNext()) {
|
|
197
|
+
String key = keys.next();
|
|
198
|
+
Object d = obj.get(key);
|
|
199
|
+
builder.append(key).append("=").append(URLEncoder.encode(d.toString(), "UTF-8"));
|
|
200
|
+
|
|
201
|
+
if (keys.hasNext()) {
|
|
202
|
+
builder.append("&");
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
this.writeRequestBody(builder.toString());
|
|
206
|
+
} else {
|
|
207
|
+
this.writeRequestBody(body.toString());
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Writes the provided string to the HTTP connection managed by this instance.
|
|
213
|
+
*
|
|
214
|
+
* @param body The string value to write to the connection stream.
|
|
215
|
+
*/
|
|
216
|
+
private void writeRequestBody(String body) throws IOException {
|
|
217
|
+
try (DataOutputStream os = new DataOutputStream(connection.getOutputStream())) {
|
|
218
|
+
os.write(body.getBytes(StandardCharsets.UTF_8));
|
|
219
|
+
os.flush();
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Opens a communications link to the resource referenced by this
|
|
225
|
+
* URL, if such a connection has not already been established.
|
|
226
|
+
* <p>
|
|
227
|
+
* If the {@code connect} method is called when the connection
|
|
228
|
+
* has already been opened (indicated by the {@code connected}
|
|
229
|
+
* field having the value {@code true}), the call is ignored.
|
|
230
|
+
* <p>
|
|
231
|
+
* URLConnection objects go through two phases: first they are
|
|
232
|
+
* created, then they are connected. After being created, and
|
|
233
|
+
* before being connected, various options can be specified
|
|
234
|
+
* (e.g., doInput and UseCaches). After connecting, it is an
|
|
235
|
+
* error to try to set them. Operations that depend on being
|
|
236
|
+
* connected, like getContentLength, will implicitly perform the
|
|
237
|
+
* connection, if necessary.
|
|
238
|
+
*
|
|
239
|
+
* @throws SocketTimeoutException if the timeout expires before
|
|
240
|
+
* the connection can be established
|
|
241
|
+
* @exception IOException if an I/O error occurs while opening the
|
|
242
|
+
* connection.
|
|
243
|
+
*/
|
|
244
|
+
public void connect() throws IOException {
|
|
245
|
+
connection.connect();
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Gets the status code from an HTTP response message.
|
|
250
|
+
* For example, in the case of the following status lines:
|
|
251
|
+
* <PRE>
|
|
252
|
+
* HTTP/1.0 200 OK
|
|
253
|
+
* HTTP/1.0 401 Unauthorized
|
|
254
|
+
* </PRE>
|
|
255
|
+
* It will return 200 and 401 respectively.
|
|
256
|
+
* Returns -1 if no code can be discerned
|
|
257
|
+
* from the response (i.e., the response is not valid HTTP).
|
|
258
|
+
* @throws IOException if an error occurred connecting to the server.
|
|
259
|
+
* @return the HTTP Status-Code, or -1
|
|
260
|
+
*/
|
|
261
|
+
public int getResponseCode() throws IOException {
|
|
262
|
+
return connection.getResponseCode();
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Returns the value of this {@code URLConnection}'s {@code URL}
|
|
267
|
+
* field.
|
|
268
|
+
*
|
|
269
|
+
* @return the value of this {@code URLConnection}'s {@code URL}
|
|
270
|
+
* field.
|
|
271
|
+
*/
|
|
272
|
+
public URL getURL() {
|
|
273
|
+
return connection.getURL();
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Returns the error stream if the connection failed
|
|
278
|
+
* but the server sent useful data nonetheless. The
|
|
279
|
+
* typical example is when an HTTP server responds
|
|
280
|
+
* with a 404, which will cause a FileNotFoundException
|
|
281
|
+
* to be thrown in connect, but the server sent an HTML
|
|
282
|
+
* help page with suggestions as to what to do.
|
|
283
|
+
*
|
|
284
|
+
* <p>This method will not cause a connection to be initiated. If
|
|
285
|
+
* the connection was not connected, or if the server did not have
|
|
286
|
+
* an error while connecting or if the server had an error but
|
|
287
|
+
* no error data was sent, this method will return null. This is
|
|
288
|
+
* the default.
|
|
289
|
+
*
|
|
290
|
+
* @return an error stream if any, null if there have been no
|
|
291
|
+
* errors, the connection is not connected or the server sent no
|
|
292
|
+
* useful data.
|
|
293
|
+
*/
|
|
294
|
+
@Override
|
|
295
|
+
public InputStream getErrorStream() {
|
|
296
|
+
return connection.getErrorStream();
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Returns the value of the named header field.
|
|
301
|
+
* <p>
|
|
302
|
+
* If called on a connection that sets the same header multiple times
|
|
303
|
+
* with possibly different values, only the last value is returned.
|
|
304
|
+
*
|
|
305
|
+
*
|
|
306
|
+
* @param name the name of a header field.
|
|
307
|
+
* @return the value of the named header field, or {@code null}
|
|
308
|
+
* if there is no such field in the header.
|
|
309
|
+
*/
|
|
310
|
+
@Override
|
|
311
|
+
public String getHeaderField(String name) {
|
|
312
|
+
return connection.getHeaderField(name);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Returns an input stream that reads from this open connection.
|
|
317
|
+
*
|
|
318
|
+
* A SocketTimeoutException can be thrown when reading from the
|
|
319
|
+
* returned input stream if the read timeout expires before data
|
|
320
|
+
* is available for read.
|
|
321
|
+
*
|
|
322
|
+
* @return an input stream that reads from this open connection.
|
|
323
|
+
* @exception IOException if an I/O error occurs while
|
|
324
|
+
* creating the input stream.
|
|
325
|
+
* @exception UnknownServiceException if the protocol does not support
|
|
326
|
+
* input.
|
|
327
|
+
* @see #setReadTimeout(int)
|
|
328
|
+
*/
|
|
329
|
+
@Override
|
|
330
|
+
public InputStream getInputStream() throws IOException {
|
|
331
|
+
return connection.getInputStream();
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Returns an unmodifiable Map of the header fields.
|
|
336
|
+
* The Map keys are Strings that represent the
|
|
337
|
+
* response-header field names. Each Map value is an
|
|
338
|
+
* unmodifiable List of Strings that represents
|
|
339
|
+
* the corresponding field values.
|
|
340
|
+
*
|
|
341
|
+
* @return a Map of header fields
|
|
342
|
+
*/
|
|
343
|
+
public Map<String, List<String>> getHeaderFields() {
|
|
344
|
+
return connection.getHeaderFields();
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Sets the default request properties on the newly created connection.
|
|
349
|
+
* This is called as early as possible to allow overrides by user-provided values.
|
|
350
|
+
*/
|
|
351
|
+
private void setDefaultRequestProperties() {
|
|
352
|
+
connection.setRequestProperty("Accept-Charset", StandardCharsets.UTF_8.name());
|
|
353
|
+
String acceptLanguage = buildDefaultAcceptLanguageProperty();
|
|
354
|
+
if (!TextUtils.isEmpty(acceptLanguage)) {
|
|
355
|
+
connection.setRequestProperty("Accept-Language", acceptLanguage);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Builds and returns a locale string describing the device's current locale preferences.
|
|
361
|
+
*/
|
|
362
|
+
private String buildDefaultAcceptLanguageProperty() {
|
|
363
|
+
Locale locale;
|
|
364
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
365
|
+
locale = LocaleList.getDefault().get(0);
|
|
366
|
+
} else {
|
|
367
|
+
locale = Locale.getDefault();
|
|
368
|
+
}
|
|
369
|
+
String result = "";
|
|
370
|
+
String lang = locale.getLanguage();
|
|
371
|
+
String country = locale.getCountry();
|
|
372
|
+
if (!TextUtils.isEmpty(lang)) {
|
|
373
|
+
if (!TextUtils.isEmpty(country)) {
|
|
374
|
+
result = String.format("%s-%s,%s;q=0.5", lang, country, lang);
|
|
375
|
+
} else {
|
|
376
|
+
result = String.format("%s;q=0.5", lang);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
return result;
|
|
380
|
+
}
|
|
381
|
+
}
|