@capacitor/android 5.6.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.
@@ -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;
@@ -279,6 +299,10 @@ var nativeBridge = (function (exports) {
279
299
  callback(result.path);
280
300
  });
281
301
  };
302
+ IonicWebView.setServerAssetPath = (path) => {
303
+ var _a;
304
+ (_a = Plugins === null || Plugins === void 0 ? void 0 : Plugins.WebView) === null || _a === void 0 ? void 0 : _a.setServerAssetPath({ path });
305
+ };
282
306
  IonicWebView.setServerBasePath = (path) => {
283
307
  var _a;
284
308
  (_a = Plugins === null || Plugins === void 0 ? void 0 : Plugins.WebView) === null || _a === void 0 ? void 0 : _a.setServerBasePath({ path });
@@ -472,6 +496,15 @@ var nativeBridge = (function (exports) {
472
496
  if (request.url.startsWith(`${cap.getServerUrl()}/`)) {
473
497
  return win.CapacitorWebFetch(resource, options);
474
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
+ }
475
508
  const tag = `CapacitorHttp fetch ${Date.now()} ${resource}`;
476
509
  console.time(tag);
477
510
  try {
@@ -542,12 +575,11 @@ var nativeBridge = (function (exports) {
542
575
  });
543
576
  xhr.readyState = 0;
544
577
  const prototype = win.CapacitorWebXMLHttpRequest.prototype;
545
- const isRelativeURL = (url) => !url || !(url.startsWith('http:') || url.startsWith('https:'));
546
578
  const isProgressEventAvailable = () => typeof ProgressEvent !== 'undefined' &&
547
579
  ProgressEvent.prototype instanceof Event;
548
580
  // XHR patch abort
549
581
  prototype.abort = function () {
550
- if (isRelativeURL(this._url)) {
582
+ if (isRelativeOrProxyUrl(this._url)) {
551
583
  return win.CapacitorWebXMLHttpRequest.abort.call(this);
552
584
  }
553
585
  this.readyState = 0;
@@ -558,10 +590,18 @@ var nativeBridge = (function (exports) {
558
590
  };
559
591
  // XHR patch open
560
592
  prototype.open = function (method, url) {
593
+ this._method = method.toLocaleUpperCase();
561
594
  this._url = url;
562
- this._method = method;
563
- if (isRelativeURL(url)) {
564
- return win.CapacitorWebXMLHttpRequest.open.call(this, method, url);
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);
565
605
  }
566
606
  setTimeout(() => {
567
607
  this.dispatchEvent(new Event('loadstart'));
@@ -570,14 +610,14 @@ var nativeBridge = (function (exports) {
570
610
  };
571
611
  // XHR patch set request header
572
612
  prototype.setRequestHeader = function (header, value) {
573
- if (isRelativeURL(this._url)) {
613
+ if (isRelativeOrProxyUrl(this._url)) {
574
614
  return win.CapacitorWebXMLHttpRequest.setRequestHeader.call(this, header, value);
575
615
  }
576
616
  this._headers[header] = value;
577
617
  };
578
618
  // XHR patch send
579
619
  prototype.send = function (body) {
580
- if (isRelativeURL(this._url)) {
620
+ if (isRelativeOrProxyUrl(this._url)) {
581
621
  return win.CapacitorWebXMLHttpRequest.send.call(this, body);
582
622
  }
583
623
  const tag = `CapacitorHttp XMLHttpRequest ${Date.now()} ${this._url}`;
@@ -706,7 +746,7 @@ var nativeBridge = (function (exports) {
706
746
  };
707
747
  // XHR patch getAllResponseHeaders
708
748
  prototype.getAllResponseHeaders = function () {
709
- if (isRelativeURL(this._url)) {
749
+ if (isRelativeOrProxyUrl(this._url)) {
710
750
  return win.CapacitorWebXMLHttpRequest.getAllResponseHeaders.call(this);
711
751
  }
712
752
  let returnString = '';
@@ -719,7 +759,7 @@ var nativeBridge = (function (exports) {
719
759
  };
720
760
  // XHR patch getResponseHeader
721
761
  prototype.getResponseHeader = function (name) {
722
- if (isRelativeURL(this._url)) {
762
+ if (isRelativeOrProxyUrl(this._url)) {
723
763
  return win.CapacitorWebXMLHttpRequest.getResponseHeader.call(this, name);
724
764
  }
725
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.replace("<head>", "<head>\n" + js + "\n");
75
+ html = html.replaceFirst("<head>", "<head>\n" + js + "\n");
76
76
  } else if (html.contains("</head>")) {
77
- html = html.replace("</head>", js + "\n" + "</head>");
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
  }
@@ -370,6 +370,13 @@ public class PluginCall {
370
370
  return defaultValue;
371
371
  }
372
372
 
373
+ /**
374
+ * @param name of the option to check
375
+ * @return boolean indicating if the plugin call has an option for the provided name.
376
+ * @deprecated Presence of a key should not be considered significant.
377
+ * Use typed accessors to check the value instead.
378
+ */
379
+ @Deprecated
373
380
  public boolean hasOption(String name) {
374
381
  return this.data.has(name);
375
382
  }
@@ -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
 
@@ -24,7 +24,7 @@ import java.util.concurrent.Executors;
24
24
  )
25
25
  public class CapacitorHttp extends Plugin {
26
26
 
27
- private Map<Runnable, PluginCall> activeRequests = new HashMap<>();
27
+ private final Map<Runnable, PluginCall> activeRequests = new HashMap<>();
28
28
  private final ExecutorService executor = Executors.newCachedThreadPool();
29
29
 
30
30
  @Override
@@ -59,17 +59,26 @@ public class CapacitorHttp extends Plugin {
59
59
  }
60
60
 
61
61
  private void http(final PluginCall call, final String httpMethod) {
62
- Runnable asyncHttpCall = () -> {
63
- try {
64
- JSObject response = HttpRequestHandler.request(call, httpMethod, getBridge());
65
- call.resolve(response);
66
- } catch (Exception e) {
67
- call.reject(e.getLocalizedMessage(), e.getClass().getSimpleName(), e);
62
+ Runnable asyncHttpCall = new Runnable() {
63
+ @Override
64
+ public void run() {
65
+ try {
66
+ JSObject response = HttpRequestHandler.request(call, httpMethod, getBridge());
67
+ call.resolve(response);
68
+ } catch (Exception e) {
69
+ call.reject(e.getLocalizedMessage(), e.getClass().getSimpleName(), e);
70
+ } finally {
71
+ activeRequests.remove(this);
72
+ }
68
73
  }
69
74
  };
70
75
 
71
- activeRequests.put(asyncHttpCall, call);
72
- executor.submit(asyncHttpCall);
76
+ if (!executor.isShutdown()) {
77
+ activeRequests.put(asyncHttpCall, call);
78
+ executor.submit(asyncHttpCall);
79
+ } else {
80
+ call.reject("Failed to execute request - Http Plugin was shutdown");
81
+ }
73
82
  }
74
83
 
75
84
  @JavascriptInterface
@@ -14,6 +14,13 @@ public class WebView extends Plugin {
14
14
  public static final String WEBVIEW_PREFS_NAME = "CapWebViewSettings";
15
15
  public static final String CAP_SERVER_PATH = "serverBasePath";
16
16
 
17
+ @PluginMethod
18
+ public void setServerAssetPath(PluginCall call) {
19
+ String path = call.getString("path");
20
+ bridge.setServerAssetPath(path);
21
+ call.resolve();
22
+ }
23
+
17
24
  @PluginMethod
18
25
  public void setServerBasePath(PluginCall call) {
19
26
  String path = call.getString("path");
@@ -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
- value.append(key).append("=").append(arr.getString(x));
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
- urlQueryBuilder.append(key).append("=").append(params.getString(key));
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
- if (shouldEncode) {
165
- URI encodedUri = new URI(uri.getScheme(), uri.getAuthority(), uri.getPath(), urlQuery, uri.getFragment());
166
- this.url = encodedUri.toURL();
167
- } else {
168
- String unEncodedUrlString =
169
- uri.getScheme() +
170
- "://" +
171
- uri.getAuthority() +
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
- private static Boolean isDomainExcludedFromSSL(Bridge bridge, URL url) {
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capacitor/android",
3
- "version": "5.6.0",
3
+ "version": "5.7.1",
4
4
  "description": "Capacitor: Cross-platform apps with JavaScript and the web",
5
5
  "homepage": "https://capacitorjs.com",
6
6
  "author": "Ionic Team <hi@ionic.io> (https://ionic.io)",
@@ -23,7 +23,7 @@
23
23
  "verify": "./gradlew clean lint build test -b capacitor/build.gradle"
24
24
  },
25
25
  "peerDependencies": {
26
- "@capacitor/core": "^5.6.0"
26
+ "@capacitor/core": "^5.7.0"
27
27
  },
28
28
  "publishConfig": {
29
29
  "access": "public"