@capacitor/android 5.7.0 → 5.7.1
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/capacitor/src/main/assets/native-bridge.js +45 -9
- package/capacitor/src/main/java/com/getcapacitor/Bridge.java +3 -0
- package/capacitor/src/main/java/com/getcapacitor/JSInjector.java +3 -3
- package/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java +131 -0
- package/capacitor/src/main/java/com/getcapacitor/plugin/util/HttpRequestHandler.java +25 -16
- package/package.json +1 -1
|
@@ -130,6 +130,26 @@ var nativeBridge = (function (exports) {
|
|
|
130
130
|
}
|
|
131
131
|
return { data: body, type: 'json' };
|
|
132
132
|
};
|
|
133
|
+
const CAPACITOR_HTTP_INTERCEPTOR = '/_capacitor_http_interceptor_';
|
|
134
|
+
const CAPACITOR_HTTPS_INTERCEPTOR = '/_capacitor_https_interceptor_';
|
|
135
|
+
// TODO: export as Cap function
|
|
136
|
+
const isRelativeOrProxyUrl = (url) => !url ||
|
|
137
|
+
!(url.startsWith('http:') || url.startsWith('https:')) ||
|
|
138
|
+
url.indexOf(CAPACITOR_HTTP_INTERCEPTOR) > -1 ||
|
|
139
|
+
url.indexOf(CAPACITOR_HTTPS_INTERCEPTOR) > -1;
|
|
140
|
+
// TODO: export as Cap function
|
|
141
|
+
const createProxyUrl = (url, win) => {
|
|
142
|
+
var _a, _b;
|
|
143
|
+
if (isRelativeOrProxyUrl(url))
|
|
144
|
+
return url;
|
|
145
|
+
let proxyUrl = new URL(url);
|
|
146
|
+
const isHttps = proxyUrl.protocol === 'https:';
|
|
147
|
+
const originalHost = encodeURIComponent(proxyUrl.host);
|
|
148
|
+
const originalPathname = proxyUrl.pathname;
|
|
149
|
+
proxyUrl = new URL((_b = (_a = win.Capacitor) === null || _a === void 0 ? void 0 : _a.getServerUrl()) !== null && _b !== void 0 ? _b : '');
|
|
150
|
+
proxyUrl.pathname = `${isHttps ? CAPACITOR_HTTPS_INTERCEPTOR : CAPACITOR_HTTP_INTERCEPTOR}/${originalHost}${originalPathname}`;
|
|
151
|
+
return proxyUrl.toString();
|
|
152
|
+
};
|
|
133
153
|
const initBridge = (w) => {
|
|
134
154
|
const getPlatformId = (win) => {
|
|
135
155
|
var _a, _b;
|
|
@@ -476,6 +496,15 @@ var nativeBridge = (function (exports) {
|
|
|
476
496
|
if (request.url.startsWith(`${cap.getServerUrl()}/`)) {
|
|
477
497
|
return win.CapacitorWebFetch(resource, options);
|
|
478
498
|
}
|
|
499
|
+
if (!(options === null || options === void 0 ? void 0 : options.method) ||
|
|
500
|
+
options.method.toLocaleUpperCase() === 'GET' ||
|
|
501
|
+
options.method.toLocaleUpperCase() === 'HEAD' ||
|
|
502
|
+
options.method.toLocaleUpperCase() === 'OPTIONS' ||
|
|
503
|
+
options.method.toLocaleUpperCase() === 'TRACE') {
|
|
504
|
+
const modifiedResource = createProxyUrl(resource.toString(), win);
|
|
505
|
+
const response = await win.CapacitorWebFetch(modifiedResource, options);
|
|
506
|
+
return response;
|
|
507
|
+
}
|
|
479
508
|
const tag = `CapacitorHttp fetch ${Date.now()} ${resource}`;
|
|
480
509
|
console.time(tag);
|
|
481
510
|
try {
|
|
@@ -546,12 +575,11 @@ var nativeBridge = (function (exports) {
|
|
|
546
575
|
});
|
|
547
576
|
xhr.readyState = 0;
|
|
548
577
|
const prototype = win.CapacitorWebXMLHttpRequest.prototype;
|
|
549
|
-
const isRelativeURL = (url) => !url || !(url.startsWith('http:') || url.startsWith('https:'));
|
|
550
578
|
const isProgressEventAvailable = () => typeof ProgressEvent !== 'undefined' &&
|
|
551
579
|
ProgressEvent.prototype instanceof Event;
|
|
552
580
|
// XHR patch abort
|
|
553
581
|
prototype.abort = function () {
|
|
554
|
-
if (
|
|
582
|
+
if (isRelativeOrProxyUrl(this._url)) {
|
|
555
583
|
return win.CapacitorWebXMLHttpRequest.abort.call(this);
|
|
556
584
|
}
|
|
557
585
|
this.readyState = 0;
|
|
@@ -562,10 +590,18 @@ var nativeBridge = (function (exports) {
|
|
|
562
590
|
};
|
|
563
591
|
// XHR patch open
|
|
564
592
|
prototype.open = function (method, url) {
|
|
593
|
+
this._method = method.toLocaleUpperCase();
|
|
565
594
|
this._url = url;
|
|
566
|
-
this._method
|
|
567
|
-
|
|
568
|
-
|
|
595
|
+
if (!this._method ||
|
|
596
|
+
this._method === 'GET' ||
|
|
597
|
+
this._method === 'HEAD' ||
|
|
598
|
+
this._method === 'OPTIONS' ||
|
|
599
|
+
this._method === 'TRACE') {
|
|
600
|
+
if (isRelativeOrProxyUrl(url)) {
|
|
601
|
+
return win.CapacitorWebXMLHttpRequest.open.call(this, method, url);
|
|
602
|
+
}
|
|
603
|
+
this._url = createProxyUrl(this._url, win);
|
|
604
|
+
return win.CapacitorWebXMLHttpRequest.open.call(this, method, this._url);
|
|
569
605
|
}
|
|
570
606
|
setTimeout(() => {
|
|
571
607
|
this.dispatchEvent(new Event('loadstart'));
|
|
@@ -574,14 +610,14 @@ var nativeBridge = (function (exports) {
|
|
|
574
610
|
};
|
|
575
611
|
// XHR patch set request header
|
|
576
612
|
prototype.setRequestHeader = function (header, value) {
|
|
577
|
-
if (
|
|
613
|
+
if (isRelativeOrProxyUrl(this._url)) {
|
|
578
614
|
return win.CapacitorWebXMLHttpRequest.setRequestHeader.call(this, header, value);
|
|
579
615
|
}
|
|
580
616
|
this._headers[header] = value;
|
|
581
617
|
};
|
|
582
618
|
// XHR patch send
|
|
583
619
|
prototype.send = function (body) {
|
|
584
|
-
if (
|
|
620
|
+
if (isRelativeOrProxyUrl(this._url)) {
|
|
585
621
|
return win.CapacitorWebXMLHttpRequest.send.call(this, body);
|
|
586
622
|
}
|
|
587
623
|
const tag = `CapacitorHttp XMLHttpRequest ${Date.now()} ${this._url}`;
|
|
@@ -710,7 +746,7 @@ var nativeBridge = (function (exports) {
|
|
|
710
746
|
};
|
|
711
747
|
// XHR patch getAllResponseHeaders
|
|
712
748
|
prototype.getAllResponseHeaders = function () {
|
|
713
|
-
if (
|
|
749
|
+
if (isRelativeOrProxyUrl(this._url)) {
|
|
714
750
|
return win.CapacitorWebXMLHttpRequest.getAllResponseHeaders.call(this);
|
|
715
751
|
}
|
|
716
752
|
let returnString = '';
|
|
@@ -723,7 +759,7 @@ var nativeBridge = (function (exports) {
|
|
|
723
759
|
};
|
|
724
760
|
// XHR patch getResponseHeader
|
|
725
761
|
prototype.getResponseHeader = function (name) {
|
|
726
|
-
if (
|
|
762
|
+
if (isRelativeOrProxyUrl(this._url)) {
|
|
727
763
|
return win.CapacitorWebXMLHttpRequest.getResponseHeader.call(this, name);
|
|
728
764
|
}
|
|
729
765
|
for (const key in this._headers) {
|
|
@@ -88,6 +88,9 @@ public class Bridge {
|
|
|
88
88
|
public static final String CAPACITOR_HTTPS_SCHEME = "https";
|
|
89
89
|
public static final String CAPACITOR_FILE_START = "/_capacitor_file_";
|
|
90
90
|
public static final String CAPACITOR_CONTENT_START = "/_capacitor_content_";
|
|
91
|
+
public static final String CAPACITOR_HTTP_INTERCEPTOR_START = "/_capacitor_http_interceptor_";
|
|
92
|
+
public static final String CAPACITOR_HTTPS_INTERCEPTOR_START = "/_capacitor_https_interceptor_";
|
|
93
|
+
|
|
91
94
|
public static final int DEFAULT_ANDROID_WEBVIEW_VERSION = 60;
|
|
92
95
|
public static final int MINIMUM_ANDROID_WEBVIEW_VERSION = 55;
|
|
93
96
|
public static final int DEFAULT_HUAWEI_WEBVIEW_VERSION = 10;
|
|
@@ -69,12 +69,12 @@ class JSInjector {
|
|
|
69
69
|
* @return
|
|
70
70
|
*/
|
|
71
71
|
public InputStream getInjectedStream(InputStream responseStream) {
|
|
72
|
-
String js = "<script type=\"text/javascript\">" + getScriptString() + "</script>";
|
|
72
|
+
String js = "<script type=\"text/javascript\">" + getScriptString().replace("${", "\\${") + "</script>";
|
|
73
73
|
String html = this.readAssetStream(responseStream);
|
|
74
74
|
if (html.contains("<head>")) {
|
|
75
|
-
html = html.
|
|
75
|
+
html = html.replaceFirst("<head>", "<head>\n" + js + "\n");
|
|
76
76
|
} else if (html.contains("</head>")) {
|
|
77
|
-
html = html.
|
|
77
|
+
html = html.replaceFirst("</head>", js + "\n" + "</head>");
|
|
78
78
|
} else {
|
|
79
79
|
Logger.error("Unable to inject Capacitor, Plugins won't work");
|
|
80
80
|
}
|
|
@@ -15,20 +15,26 @@ limitations under the License.
|
|
|
15
15
|
*/
|
|
16
16
|
package com.getcapacitor;
|
|
17
17
|
|
|
18
|
+
import static com.getcapacitor.plugin.util.HttpRequestHandler.isDomainExcludedFromSSL;
|
|
19
|
+
|
|
18
20
|
import android.content.Context;
|
|
19
21
|
import android.net.Uri;
|
|
20
22
|
import android.util.Base64;
|
|
21
23
|
import android.webkit.CookieManager;
|
|
22
24
|
import android.webkit.WebResourceRequest;
|
|
23
25
|
import android.webkit.WebResourceResponse;
|
|
26
|
+
import com.getcapacitor.plugin.util.CapacitorHttpUrlConnection;
|
|
27
|
+
import com.getcapacitor.plugin.util.HttpRequestHandler;
|
|
24
28
|
import java.io.IOException;
|
|
25
29
|
import java.io.InputStream;
|
|
26
30
|
import java.net.HttpURLConnection;
|
|
27
31
|
import java.net.URL;
|
|
28
32
|
import java.net.URLConnection;
|
|
33
|
+
import java.net.URLDecoder;
|
|
29
34
|
import java.nio.charset.StandardCharsets;
|
|
30
35
|
import java.util.ArrayList;
|
|
31
36
|
import java.util.HashMap;
|
|
37
|
+
import java.util.LinkedHashMap;
|
|
32
38
|
import java.util.List;
|
|
33
39
|
import java.util.Map;
|
|
34
40
|
|
|
@@ -165,6 +171,23 @@ public class WebViewLocalServer {
|
|
|
165
171
|
*/
|
|
166
172
|
public WebResourceResponse shouldInterceptRequest(WebResourceRequest request) {
|
|
167
173
|
Uri loadingUrl = request.getUrl();
|
|
174
|
+
|
|
175
|
+
if (
|
|
176
|
+
null != loadingUrl.getPath() &&
|
|
177
|
+
(
|
|
178
|
+
loadingUrl.getPath().startsWith(Bridge.CAPACITOR_HTTP_INTERCEPTOR_START) ||
|
|
179
|
+
loadingUrl.getPath().startsWith(Bridge.CAPACITOR_HTTPS_INTERCEPTOR_START)
|
|
180
|
+
)
|
|
181
|
+
) {
|
|
182
|
+
Logger.debug("Handling CapacitorHttp request: " + loadingUrl);
|
|
183
|
+
try {
|
|
184
|
+
return handleCapacitorHttpRequest(request);
|
|
185
|
+
} catch (Exception e) {
|
|
186
|
+
Logger.error(e.getLocalizedMessage());
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
168
191
|
PathHandler handler;
|
|
169
192
|
synchronized (uriMatcher) {
|
|
170
193
|
handler = (PathHandler) uriMatcher.match(request.getUrl());
|
|
@@ -199,6 +222,114 @@ public class WebViewLocalServer {
|
|
|
199
222
|
return !(bridge.getServerUrl() == null && !bridge.getAppAllowNavigationMask().matches(loadingUrl.getHost()));
|
|
200
223
|
}
|
|
201
224
|
|
|
225
|
+
private String getReasonPhraseFromResponseCode(int code) {
|
|
226
|
+
return switch (code) {
|
|
227
|
+
case 100 -> "Continue";
|
|
228
|
+
case 101 -> "Switching Protocols";
|
|
229
|
+
case 200 -> "OK";
|
|
230
|
+
case 201 -> "Created";
|
|
231
|
+
case 202 -> "Accepted";
|
|
232
|
+
case 203 -> "Non-Authoritative Information";
|
|
233
|
+
case 204 -> "No Content";
|
|
234
|
+
case 205 -> "Reset Content";
|
|
235
|
+
case 206 -> "Partial Content";
|
|
236
|
+
case 300 -> "Multiple Choices";
|
|
237
|
+
case 301 -> "Moved Permanently";
|
|
238
|
+
case 302 -> "Found";
|
|
239
|
+
case 303 -> "See Other";
|
|
240
|
+
case 304 -> "Not Modified";
|
|
241
|
+
case 400 -> "Bad Request";
|
|
242
|
+
case 401 -> "Unauthorized";
|
|
243
|
+
case 403 -> "Forbidden";
|
|
244
|
+
case 404 -> "Not Found";
|
|
245
|
+
case 405 -> "Method Not Allowed";
|
|
246
|
+
case 406 -> "Not Acceptable";
|
|
247
|
+
case 407 -> "Proxy Authentication Required";
|
|
248
|
+
case 408 -> "Request Timeout";
|
|
249
|
+
case 409 -> "Conflict";
|
|
250
|
+
case 410 -> "Gone";
|
|
251
|
+
case 500 -> "Internal Server Error";
|
|
252
|
+
case 501 -> "Not Implemented";
|
|
253
|
+
case 502 -> "Bad Gateway";
|
|
254
|
+
case 503 -> "Service Unavailable";
|
|
255
|
+
case 504 -> "Gateway Timeout";
|
|
256
|
+
case 505 -> "HTTP Version Not Supported";
|
|
257
|
+
default -> "Unknown";
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
private WebResourceResponse handleCapacitorHttpRequest(WebResourceRequest request) throws IOException {
|
|
262
|
+
boolean isHttps =
|
|
263
|
+
request.getUrl().getPath() != null && request.getUrl().getPath().startsWith(Bridge.CAPACITOR_HTTPS_INTERCEPTOR_START);
|
|
264
|
+
|
|
265
|
+
String urlString = request
|
|
266
|
+
.getUrl()
|
|
267
|
+
.toString()
|
|
268
|
+
.replace(bridge.getLocalUrl(), isHttps ? "https:/" : "http:/")
|
|
269
|
+
.replace(Bridge.CAPACITOR_HTTP_INTERCEPTOR_START, "")
|
|
270
|
+
.replace(Bridge.CAPACITOR_HTTPS_INTERCEPTOR_START, "");
|
|
271
|
+
urlString = URLDecoder.decode(urlString, "UTF-8");
|
|
272
|
+
URL url = new URL(urlString);
|
|
273
|
+
JSObject headers = new JSObject();
|
|
274
|
+
|
|
275
|
+
for (Map.Entry<String, String> header : request.getRequestHeaders().entrySet()) {
|
|
276
|
+
headers.put(header.getKey(), header.getValue());
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
HttpRequestHandler.HttpURLConnectionBuilder connectionBuilder = new HttpRequestHandler.HttpURLConnectionBuilder()
|
|
280
|
+
.setUrl(url)
|
|
281
|
+
.setMethod(request.getMethod())
|
|
282
|
+
.setHeaders(headers)
|
|
283
|
+
.openConnection();
|
|
284
|
+
|
|
285
|
+
CapacitorHttpUrlConnection connection = connectionBuilder.build();
|
|
286
|
+
|
|
287
|
+
if (!isDomainExcludedFromSSL(bridge, url)) {
|
|
288
|
+
connection.setSSLSocketFactory(bridge);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
connection.connect();
|
|
292
|
+
|
|
293
|
+
String mimeType = null;
|
|
294
|
+
String encoding = null;
|
|
295
|
+
Map<String, String> responseHeaders = new LinkedHashMap<>();
|
|
296
|
+
for (Map.Entry<String, List<String>> entry : connection.getHeaderFields().entrySet()) {
|
|
297
|
+
StringBuilder builder = new StringBuilder();
|
|
298
|
+
for (String value : entry.getValue()) {
|
|
299
|
+
builder.append(value);
|
|
300
|
+
builder.append(", ");
|
|
301
|
+
}
|
|
302
|
+
builder.setLength(builder.length() - 2);
|
|
303
|
+
|
|
304
|
+
if ("Content-Type".equalsIgnoreCase(entry.getKey())) {
|
|
305
|
+
String[] contentTypeParts = builder.toString().split(";");
|
|
306
|
+
mimeType = contentTypeParts[0].trim();
|
|
307
|
+
if (contentTypeParts.length > 1) {
|
|
308
|
+
String[] encodingParts = contentTypeParts[1].split("=");
|
|
309
|
+
if (encodingParts.length > 1) {
|
|
310
|
+
encoding = encodingParts[1].trim();
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
} else {
|
|
314
|
+
responseHeaders.put(entry.getKey(), builder.toString());
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
InputStream inputStream = connection.getErrorStream();
|
|
319
|
+
if (inputStream == null) {
|
|
320
|
+
inputStream = connection.getInputStream();
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
if (null == mimeType) {
|
|
324
|
+
mimeType = getMimeType(request.getUrl().getPath(), inputStream);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
int responseCode = connection.getResponseCode();
|
|
328
|
+
String reasonPhrase = getReasonPhraseFromResponseCode(responseCode);
|
|
329
|
+
|
|
330
|
+
return new WebResourceResponse(mimeType, encoding, responseCode, reasonPhrase, responseHeaders, inputStream);
|
|
331
|
+
}
|
|
332
|
+
|
|
202
333
|
private WebResourceResponse handleLocalRequest(WebResourceRequest request, PathHandler handler) {
|
|
203
334
|
String path = request.getUrl().getPath();
|
|
204
335
|
|
|
@@ -12,12 +12,14 @@ import java.io.ByteArrayOutputStream;
|
|
|
12
12
|
import java.io.IOException;
|
|
13
13
|
import java.io.InputStream;
|
|
14
14
|
import java.io.InputStreamReader;
|
|
15
|
+
import java.io.UnsupportedEncodingException;
|
|
15
16
|
import java.lang.reflect.Method;
|
|
16
17
|
import java.net.HttpURLConnection;
|
|
17
18
|
import java.net.MalformedURLException;
|
|
18
19
|
import java.net.URI;
|
|
19
20
|
import java.net.URISyntaxException;
|
|
20
21
|
import java.net.URL;
|
|
22
|
+
import java.net.URLEncoder;
|
|
21
23
|
import java.util.Iterator;
|
|
22
24
|
import java.util.List;
|
|
23
25
|
import java.util.Locale;
|
|
@@ -141,7 +143,7 @@ public class HttpRequestHandler {
|
|
|
141
143
|
StringBuilder value = new StringBuilder();
|
|
142
144
|
JSONArray arr = params.getJSONArray(key);
|
|
143
145
|
for (int x = 0; x < arr.length(); x++) {
|
|
144
|
-
|
|
146
|
+
this.addUrlParam(value, key, arr.getString(x), shouldEncode);
|
|
145
147
|
if (x != arr.length() - 1) {
|
|
146
148
|
value.append("&");
|
|
147
149
|
}
|
|
@@ -154,30 +156,37 @@ public class HttpRequestHandler {
|
|
|
154
156
|
if (urlQueryBuilder.length() > 0) {
|
|
155
157
|
urlQueryBuilder.append("&");
|
|
156
158
|
}
|
|
157
|
-
|
|
159
|
+
this.addUrlParam(urlQueryBuilder, key, params.getString(key), shouldEncode);
|
|
158
160
|
}
|
|
159
161
|
}
|
|
160
162
|
|
|
161
163
|
String urlQuery = urlQueryBuilder.toString();
|
|
162
164
|
|
|
163
165
|
URI uri = url.toURI();
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
uri.getPath() +
|
|
173
|
-
((!urlQuery.equals("")) ? "?" + urlQuery : "") +
|
|
174
|
-
((uri.getFragment() != null) ? uri.getFragment() : "");
|
|
175
|
-
this.url = new URL(unEncodedUrlString);
|
|
176
|
-
}
|
|
166
|
+
String unEncodedUrlString =
|
|
167
|
+
uri.getScheme() +
|
|
168
|
+
"://" +
|
|
169
|
+
uri.getAuthority() +
|
|
170
|
+
uri.getPath() +
|
|
171
|
+
((!urlQuery.equals("")) ? "?" + urlQuery : "") +
|
|
172
|
+
((uri.getFragment() != null) ? uri.getFragment() : "");
|
|
173
|
+
this.url = new URL(unEncodedUrlString);
|
|
177
174
|
|
|
178
175
|
return this;
|
|
179
176
|
}
|
|
180
177
|
|
|
178
|
+
private static void addUrlParam(StringBuilder sb, String key, String value, boolean shouldEncode) {
|
|
179
|
+
if (shouldEncode) {
|
|
180
|
+
try {
|
|
181
|
+
key = URLEncoder.encode(key, "UTF-8");
|
|
182
|
+
value = URLEncoder.encode(value, "UTF-8");
|
|
183
|
+
} catch (UnsupportedEncodingException ex) {
|
|
184
|
+
throw new RuntimeException(ex.getCause());
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
sb.append(key).append("=").append(value);
|
|
188
|
+
}
|
|
189
|
+
|
|
181
190
|
public CapacitorHttpUrlConnection build() {
|
|
182
191
|
return connection;
|
|
183
192
|
}
|
|
@@ -426,7 +435,7 @@ public class HttpRequestHandler {
|
|
|
426
435
|
return response;
|
|
427
436
|
}
|
|
428
437
|
|
|
429
|
-
|
|
438
|
+
public static Boolean isDomainExcludedFromSSL(Bridge bridge, URL url) {
|
|
430
439
|
try {
|
|
431
440
|
Class<?> sslPinningImpl = Class.forName("io.ionic.sslpinning.SSLPinning");
|
|
432
441
|
Method method = sslPinningImpl.getDeclaredMethod("isDomainExcluded", Bridge.class, URL.class);
|
package/package.json
CHANGED