From 3375c22406b0875f135c58b3402b819de942dfef Mon Sep 17 00:00:00 2001 From: Michael Wolz Date: Thu, 20 Jun 2024 17:49:37 +0200 Subject: [PATCH 1/8] fix: resolve url encoding issues for android platform --- android/capacitor/src/main/assets/native-bridge.js | 9 +++++---- .../src/main/java/com/getcapacitor/Bridge.java | 1 + .../java/com/getcapacitor/WebViewLocalServer.java | 11 ++++------- core/native-bridge.ts | 10 ++++++---- ios/Capacitor/Capacitor/assets/native-bridge.js | 9 +++++---- 5 files changed, 21 insertions(+), 19 deletions(-) diff --git a/android/capacitor/src/main/assets/native-bridge.js b/android/capacitor/src/main/assets/native-bridge.js index b0f7581a42..b0c5c51baf 100644 --- a/android/capacitor/src/main/assets/native-bridge.js +++ b/android/capacitor/src/main/assets/native-bridge.js @@ -134,6 +134,7 @@ var nativeBridge = (function (exports) { }; const CAPACITOR_HTTP_INTERCEPTOR = '/_capacitor_http_interceptor_'; const CAPACITOR_HTTPS_INTERCEPTOR = '/_capacitor_https_interceptor_'; + const CAPACITOR_HTTP_INTERCEPTOR_URL_PARAM = 'u'; // TODO: export as Cap function const isRelativeOrProxyUrl = (url) => !url || !(url.startsWith('http:') || url.startsWith('https:')) || @@ -145,11 +146,11 @@ var nativeBridge = (function (exports) { if (isRelativeOrProxyUrl(url)) return url; const proxyUrl = new URL(url); - const bridgeUrl = new URL((_b = (_a = win.Capacitor) === null || _a === void 0 ? void 0 : _a.getServerUrl()) !== null && _b !== void 0 ? _b : ''); const isHttps = proxyUrl.protocol === 'https:'; - bridgeUrl.search = proxyUrl.search; - bridgeUrl.hash = proxyUrl.hash; - bridgeUrl.pathname = `${isHttps ? CAPACITOR_HTTPS_INTERCEPTOR : CAPACITOR_HTTP_INTERCEPTOR}/${encodeURIComponent(proxyUrl.host)}${proxyUrl.pathname}`; + const bridgeUrl = new URL((_b = (_a = win.Capacitor) === null || _a === void 0 ? void 0 : _a.getServerUrl()) !== null && _b !== void 0 ? _b : ''); + bridgeUrl.pathname = `${isHttps ? CAPACITOR_HTTPS_INTERCEPTOR : CAPACITOR_HTTP_INTERCEPTOR}`; + // URLSearchParams `append()` method will automatically percent encode the url + bridgeUrl.searchParams.append(CAPACITOR_HTTP_INTERCEPTOR_URL_PARAM, url); return bridgeUrl.toString(); }; const initBridge = (w) => { diff --git a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java index 163b1d66dc..588cb94194 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java @@ -93,6 +93,7 @@ public class Bridge { public static final String CAPACITOR_CONTENT_START = "/_capacitor_content_"; public static final String CAPACITOR_HTTP_INTERCEPTOR_START = "/_capacitor_http_interceptor_"; public static final String CAPACITOR_HTTPS_INTERCEPTOR_START = "/_capacitor_https_interceptor_"; + public static final String CAPACITOR_HTTP_INTERCEPTOR_URL_PARAM = "u"; public static final int DEFAULT_ANDROID_WEBVIEW_VERSION = 60; public static final int MINIMUM_ANDROID_WEBVIEW_VERSION = 55; diff --git a/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java b/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java index e25c73f517..7a3cd94828 100755 --- a/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java +++ b/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java @@ -262,13 +262,10 @@ private WebResourceResponse handleCapacitorHttpRequest(WebResourceRequest reques boolean isHttps = request.getUrl().getPath() != null && request.getUrl().getPath().startsWith(Bridge.CAPACITOR_HTTPS_INTERCEPTOR_START); - String urlString = request - .getUrl() - .toString() - .replace(bridge.getLocalUrl(), isHttps ? "https:/" : "http:/") - .replace(Bridge.CAPACITOR_HTTP_INTERCEPTOR_START, "") - .replace(Bridge.CAPACITOR_HTTPS_INTERCEPTOR_START, ""); - urlString = URLDecoder.decode(urlString, "UTF-8"); + Uri uri = Uri.parse(request.getUrl().toString()); + // `getQueryParameter` method of Android Uri lib already decodes the value automatically + String urlString = uri.getQueryParameter(Bridge.CAPACITOR_HTTP_INTERCEPTOR_URL_PARAM); + URL url = new URL(urlString); JSObject headers = new JSObject(); diff --git a/core/native-bridge.ts b/core/native-bridge.ts index b0e638d949..eba8f40f0d 100644 --- a/core/native-bridge.ts +++ b/core/native-bridge.ts @@ -121,6 +121,7 @@ const convertBody = async ( const CAPACITOR_HTTP_INTERCEPTOR = '/_capacitor_http_interceptor_'; const CAPACITOR_HTTPS_INTERCEPTOR = '/_capacitor_https_interceptor_'; +const CAPACITOR_HTTP_INTERCEPTOR_URL_PARAM = 'u'; // TODO: export as Cap function const isRelativeOrProxyUrl = (url: string | undefined): boolean => @@ -134,13 +135,14 @@ const createProxyUrl = (url: string, win: WindowCapacitor): string => { if (isRelativeOrProxyUrl(url)) return url; const proxyUrl = new URL(url); - const bridgeUrl = new URL(win.Capacitor?.getServerUrl() ?? ''); const isHttps = proxyUrl.protocol === 'https:'; - bridgeUrl.search = proxyUrl.search; - bridgeUrl.hash = proxyUrl.hash; + const bridgeUrl = new URL(win.Capacitor?.getServerUrl() ?? ''); bridgeUrl.pathname = `${ isHttps ? CAPACITOR_HTTPS_INTERCEPTOR : CAPACITOR_HTTP_INTERCEPTOR - }/${encodeURIComponent(proxyUrl.host)}${proxyUrl.pathname}`; + }`; + // URLSearchParams `append()` method will automatically percent encode the url + bridgeUrl.searchParams.append(CAPACITOR_HTTP_INTERCEPTOR_URL_PARAM, url); + return bridgeUrl.toString(); }; diff --git a/ios/Capacitor/Capacitor/assets/native-bridge.js b/ios/Capacitor/Capacitor/assets/native-bridge.js index b0f7581a42..b0c5c51baf 100644 --- a/ios/Capacitor/Capacitor/assets/native-bridge.js +++ b/ios/Capacitor/Capacitor/assets/native-bridge.js @@ -134,6 +134,7 @@ var nativeBridge = (function (exports) { }; const CAPACITOR_HTTP_INTERCEPTOR = '/_capacitor_http_interceptor_'; const CAPACITOR_HTTPS_INTERCEPTOR = '/_capacitor_https_interceptor_'; + const CAPACITOR_HTTP_INTERCEPTOR_URL_PARAM = 'u'; // TODO: export as Cap function const isRelativeOrProxyUrl = (url) => !url || !(url.startsWith('http:') || url.startsWith('https:')) || @@ -145,11 +146,11 @@ var nativeBridge = (function (exports) { if (isRelativeOrProxyUrl(url)) return url; const proxyUrl = new URL(url); - const bridgeUrl = new URL((_b = (_a = win.Capacitor) === null || _a === void 0 ? void 0 : _a.getServerUrl()) !== null && _b !== void 0 ? _b : ''); const isHttps = proxyUrl.protocol === 'https:'; - bridgeUrl.search = proxyUrl.search; - bridgeUrl.hash = proxyUrl.hash; - bridgeUrl.pathname = `${isHttps ? CAPACITOR_HTTPS_INTERCEPTOR : CAPACITOR_HTTP_INTERCEPTOR}/${encodeURIComponent(proxyUrl.host)}${proxyUrl.pathname}`; + const bridgeUrl = new URL((_b = (_a = win.Capacitor) === null || _a === void 0 ? void 0 : _a.getServerUrl()) !== null && _b !== void 0 ? _b : ''); + bridgeUrl.pathname = `${isHttps ? CAPACITOR_HTTPS_INTERCEPTOR : CAPACITOR_HTTP_INTERCEPTOR}`; + // URLSearchParams `append()` method will automatically percent encode the url + bridgeUrl.searchParams.append(CAPACITOR_HTTP_INTERCEPTOR_URL_PARAM, url); return bridgeUrl.toString(); }; const initBridge = (w) => { From cc099f692bb2c63c2350bd0a271cb09bd511e2b7 Mon Sep 17 00:00:00 2001 From: Michael Wolz Date: Thu, 20 Jun 2024 18:52:00 +0200 Subject: [PATCH 2/8] fix(ios): resolve url encoding issues --- .../src/main/assets/native-bridge.js | 8 ++----- .../main/java/com/getcapacitor/Bridge.java | 1 - .../com/getcapacitor/WebViewLocalServer.java | 5 +--- core/native-bridge.ts | 11 ++------- ios/Capacitor/Capacitor/CapacitorBridge.swift | 2 +- .../Capacitor/WebViewAssetHandler.swift | 24 +++++-------------- .../Capacitor/assets/native-bridge.js | 8 ++----- 7 files changed, 14 insertions(+), 45 deletions(-) diff --git a/android/capacitor/src/main/assets/native-bridge.js b/android/capacitor/src/main/assets/native-bridge.js index b0c5c51baf..01a340469d 100644 --- a/android/capacitor/src/main/assets/native-bridge.js +++ b/android/capacitor/src/main/assets/native-bridge.js @@ -133,22 +133,18 @@ var nativeBridge = (function (exports) { return { data: body, type: 'json' }; }; const CAPACITOR_HTTP_INTERCEPTOR = '/_capacitor_http_interceptor_'; - const CAPACITOR_HTTPS_INTERCEPTOR = '/_capacitor_https_interceptor_'; const CAPACITOR_HTTP_INTERCEPTOR_URL_PARAM = 'u'; // TODO: export as Cap function const isRelativeOrProxyUrl = (url) => !url || !(url.startsWith('http:') || url.startsWith('https:')) || - url.indexOf(CAPACITOR_HTTP_INTERCEPTOR) > -1 || - url.indexOf(CAPACITOR_HTTPS_INTERCEPTOR) > -1; + url.indexOf(CAPACITOR_HTTP_INTERCEPTOR) > -1; // TODO: export as Cap function const createProxyUrl = (url, win) => { var _a, _b; if (isRelativeOrProxyUrl(url)) return url; - const proxyUrl = new URL(url); - const isHttps = proxyUrl.protocol === 'https:'; const bridgeUrl = new URL((_b = (_a = win.Capacitor) === null || _a === void 0 ? void 0 : _a.getServerUrl()) !== null && _b !== void 0 ? _b : ''); - bridgeUrl.pathname = `${isHttps ? CAPACITOR_HTTPS_INTERCEPTOR : CAPACITOR_HTTP_INTERCEPTOR}`; + bridgeUrl.pathname = `${CAPACITOR_HTTP_INTERCEPTOR}`; // URLSearchParams `append()` method will automatically percent encode the url bridgeUrl.searchParams.append(CAPACITOR_HTTP_INTERCEPTOR_URL_PARAM, url); return bridgeUrl.toString(); diff --git a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java index 588cb94194..6cf8d32246 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java @@ -92,7 +92,6 @@ public class Bridge { public static final String CAPACITOR_FILE_START = "/_capacitor_file_"; public static final String CAPACITOR_CONTENT_START = "/_capacitor_content_"; public static final String CAPACITOR_HTTP_INTERCEPTOR_START = "/_capacitor_http_interceptor_"; - public static final String CAPACITOR_HTTPS_INTERCEPTOR_START = "/_capacitor_https_interceptor_"; public static final String CAPACITOR_HTTP_INTERCEPTOR_URL_PARAM = "u"; public static final int DEFAULT_ANDROID_WEBVIEW_VERSION = 60; diff --git a/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java b/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java index 7a3cd94828..af8814ecf6 100755 --- a/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java +++ b/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java @@ -174,10 +174,7 @@ public WebResourceResponse shouldInterceptRequest(WebResourceRequest request) { if ( null != loadingUrl.getPath() && - ( - loadingUrl.getPath().startsWith(Bridge.CAPACITOR_HTTP_INTERCEPTOR_START) || - loadingUrl.getPath().startsWith(Bridge.CAPACITOR_HTTPS_INTERCEPTOR_START) - ) + loadingUrl.getPath().startsWith(Bridge.CAPACITOR_HTTP_INTERCEPTOR_START) ) { Logger.debug("Handling CapacitorHttp request: " + loadingUrl); try { diff --git a/core/native-bridge.ts b/core/native-bridge.ts index eba8f40f0d..fc2163640a 100644 --- a/core/native-bridge.ts +++ b/core/native-bridge.ts @@ -120,26 +120,19 @@ const convertBody = async ( }; const CAPACITOR_HTTP_INTERCEPTOR = '/_capacitor_http_interceptor_'; -const CAPACITOR_HTTPS_INTERCEPTOR = '/_capacitor_https_interceptor_'; const CAPACITOR_HTTP_INTERCEPTOR_URL_PARAM = 'u'; // TODO: export as Cap function const isRelativeOrProxyUrl = (url: string | undefined): boolean => !url || !(url.startsWith('http:') || url.startsWith('https:')) || - url.indexOf(CAPACITOR_HTTP_INTERCEPTOR) > -1 || - url.indexOf(CAPACITOR_HTTPS_INTERCEPTOR) > -1; + url.indexOf(CAPACITOR_HTTP_INTERCEPTOR) > -1; // TODO: export as Cap function const createProxyUrl = (url: string, win: WindowCapacitor): string => { if (isRelativeOrProxyUrl(url)) return url; - - const proxyUrl = new URL(url); - const isHttps = proxyUrl.protocol === 'https:'; const bridgeUrl = new URL(win.Capacitor?.getServerUrl() ?? ''); - bridgeUrl.pathname = `${ - isHttps ? CAPACITOR_HTTPS_INTERCEPTOR : CAPACITOR_HTTP_INTERCEPTOR - }`; + bridgeUrl.pathname = `${CAPACITOR_HTTP_INTERCEPTOR}`; // URLSearchParams `append()` method will automatically percent encode the url bridgeUrl.searchParams.append(CAPACITOR_HTTP_INTERCEPTOR_URL_PARAM, url); diff --git a/ios/Capacitor/Capacitor/CapacitorBridge.swift b/ios/Capacitor/Capacitor/CapacitorBridge.swift index 06e52711a9..a88f0bf20d 100644 --- a/ios/Capacitor/Capacitor/CapacitorBridge.swift +++ b/ios/Capacitor/Capacitor/CapacitorBridge.swift @@ -95,7 +95,7 @@ open class CapacitorBridge: NSObject, CAPBridgeProtocol { public static let capacitorSite = "https://capacitorjs.com/" public static let fileStartIdentifier = "/_capacitor_file_" public static let httpInterceptorStartIdentifier = "/_capacitor_http_interceptor_" - public static let httpsInterceptorStartIdentifier = "/_capacitor_https_interceptor_" + public static let httpInterceptorUrlParam = "u" public static let defaultScheme = "capacitor" public private(set) var webViewAssetHandler: WebViewAssetHandler diff --git a/ios/Capacitor/Capacitor/WebViewAssetHandler.swift b/ios/Capacitor/Capacitor/WebViewAssetHandler.swift index 5efa94a932..b505a7daf5 100644 --- a/ios/Capacitor/Capacitor/WebViewAssetHandler.swift +++ b/ios/Capacitor/Capacitor/WebViewAssetHandler.swift @@ -35,11 +35,6 @@ open class WebViewAssetHandler: NSObject, WKURLSchemeHandler { return } - if url.path.starts(with: CapacitorBridge.httpsInterceptorStartIdentifier) { - handleCapacitorHttpRequest(urlSchemeTask, localUrl, true) - return - } - if stringToLoad.starts(with: CapacitorBridge.fileStartIdentifier) { startPath = stringToLoad.replacingOccurrences(of: CapacitorBridge.fileStartIdentifier, with: "") } else { @@ -138,21 +133,14 @@ open class WebViewAssetHandler: NSObject, WKURLSchemeHandler { func handleCapacitorHttpRequest(_ urlSchemeTask: WKURLSchemeTask, _ localUrl: URL, _ isHttpsRequest: Bool) { var urlRequest = urlSchemeTask.request guard let url = urlRequest.url else { return } - var targetUrl = url.absoluteString - .replacingOccurrences(of: CapacitorBridge.httpInterceptorStartIdentifier, with: "") - .replacingOccurrences(of: CapacitorBridge.httpsInterceptorStartIdentifier, with: "") - // Only replace first occurrence of the scheme - if let range = targetUrl.range(of: localUrl.scheme ?? InstanceDescriptorDefaults.scheme) { - targetUrl = targetUrl.replacingCharacters(in: range, with: isHttpsRequest ? "https" : "http") + + let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) + if let targetUrl = urlComponents?.queryItems?.first(where: { $0.name == CapacitorBridge.httpInterceptorUrlParam })?.value, + !targetUrl.isEmpty { + // Note that URLComponents are already percent-decoded so we do not need to do this manually + urlRequest.url = URL(string: targetUrl) } - // Only replace first occurrence of the hostname - if let range = targetUrl.range(of: (localUrl.host ?? InstanceDescriptorDefaults.hostname) + "/") { - targetUrl = targetUrl.replacingCharacters(in: range, with: "") - } - - urlRequest.url = URL(string: targetUrl.removingPercentEncoding ?? targetUrl) - let urlSession = URLSession.shared let task = urlSession.dataTask(with: urlRequest) { (data, response, error) in DispatchQueue.main.async { diff --git a/ios/Capacitor/Capacitor/assets/native-bridge.js b/ios/Capacitor/Capacitor/assets/native-bridge.js index b0c5c51baf..01a340469d 100644 --- a/ios/Capacitor/Capacitor/assets/native-bridge.js +++ b/ios/Capacitor/Capacitor/assets/native-bridge.js @@ -133,22 +133,18 @@ var nativeBridge = (function (exports) { return { data: body, type: 'json' }; }; const CAPACITOR_HTTP_INTERCEPTOR = '/_capacitor_http_interceptor_'; - const CAPACITOR_HTTPS_INTERCEPTOR = '/_capacitor_https_interceptor_'; const CAPACITOR_HTTP_INTERCEPTOR_URL_PARAM = 'u'; // TODO: export as Cap function const isRelativeOrProxyUrl = (url) => !url || !(url.startsWith('http:') || url.startsWith('https:')) || - url.indexOf(CAPACITOR_HTTP_INTERCEPTOR) > -1 || - url.indexOf(CAPACITOR_HTTPS_INTERCEPTOR) > -1; + url.indexOf(CAPACITOR_HTTP_INTERCEPTOR) > -1; // TODO: export as Cap function const createProxyUrl = (url, win) => { var _a, _b; if (isRelativeOrProxyUrl(url)) return url; - const proxyUrl = new URL(url); - const isHttps = proxyUrl.protocol === 'https:'; const bridgeUrl = new URL((_b = (_a = win.Capacitor) === null || _a === void 0 ? void 0 : _a.getServerUrl()) !== null && _b !== void 0 ? _b : ''); - bridgeUrl.pathname = `${isHttps ? CAPACITOR_HTTPS_INTERCEPTOR : CAPACITOR_HTTP_INTERCEPTOR}`; + bridgeUrl.pathname = `${CAPACITOR_HTTP_INTERCEPTOR}`; // URLSearchParams `append()` method will automatically percent encode the url bridgeUrl.searchParams.append(CAPACITOR_HTTP_INTERCEPTOR_URL_PARAM, url); return bridgeUrl.toString(); From 088fb1acac2d967123c22e1c4279c34d91b8f7bc Mon Sep 17 00:00:00 2001 From: Michael Wolz Date: Fri, 21 Jun 2024 10:25:07 +0200 Subject: [PATCH 3/8] fix(android): remove unused https check from WebViewLocalServer --- .../src/main/java/com/getcapacitor/WebViewLocalServer.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java b/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java index af8814ecf6..36650eecf9 100755 --- a/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java +++ b/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java @@ -256,9 +256,6 @@ private String getReasonPhraseFromResponseCode(int code) { } private WebResourceResponse handleCapacitorHttpRequest(WebResourceRequest request) throws IOException { - boolean isHttps = - request.getUrl().getPath() != null && request.getUrl().getPath().startsWith(Bridge.CAPACITOR_HTTPS_INTERCEPTOR_START); - Uri uri = Uri.parse(request.getUrl().toString()); // `getQueryParameter` method of Android Uri lib already decodes the value automatically String urlString = uri.getQueryParameter(Bridge.CAPACITOR_HTTP_INTERCEPTOR_URL_PARAM); From 70cf26e78ccd7a8ca3c6d141e8d05ee13bf28c32 Mon Sep 17 00:00:00 2001 From: Michael Wolz Date: Thu, 20 Jun 2024 19:04:15 +0200 Subject: [PATCH 4/8] fix: remove unnecessary template string --- android/capacitor/src/main/assets/native-bridge.js | 2 +- core/native-bridge.ts | 2 +- ios/Capacitor/Capacitor/assets/native-bridge.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/android/capacitor/src/main/assets/native-bridge.js b/android/capacitor/src/main/assets/native-bridge.js index 01a340469d..2a38f433d7 100644 --- a/android/capacitor/src/main/assets/native-bridge.js +++ b/android/capacitor/src/main/assets/native-bridge.js @@ -144,7 +144,7 @@ var nativeBridge = (function (exports) { if (isRelativeOrProxyUrl(url)) return url; const bridgeUrl = new URL((_b = (_a = win.Capacitor) === null || _a === void 0 ? void 0 : _a.getServerUrl()) !== null && _b !== void 0 ? _b : ''); - bridgeUrl.pathname = `${CAPACITOR_HTTP_INTERCEPTOR}`; + bridgeUrl.pathname = CAPACITOR_HTTP_INTERCEPTOR; // URLSearchParams `append()` method will automatically percent encode the url bridgeUrl.searchParams.append(CAPACITOR_HTTP_INTERCEPTOR_URL_PARAM, url); return bridgeUrl.toString(); diff --git a/core/native-bridge.ts b/core/native-bridge.ts index fc2163640a..f16c9a67d9 100644 --- a/core/native-bridge.ts +++ b/core/native-bridge.ts @@ -132,7 +132,7 @@ const isRelativeOrProxyUrl = (url: string | undefined): boolean => const createProxyUrl = (url: string, win: WindowCapacitor): string => { if (isRelativeOrProxyUrl(url)) return url; const bridgeUrl = new URL(win.Capacitor?.getServerUrl() ?? ''); - bridgeUrl.pathname = `${CAPACITOR_HTTP_INTERCEPTOR}`; + bridgeUrl.pathname = CAPACITOR_HTTP_INTERCEPTOR; // URLSearchParams `append()` method will automatically percent encode the url bridgeUrl.searchParams.append(CAPACITOR_HTTP_INTERCEPTOR_URL_PARAM, url); diff --git a/ios/Capacitor/Capacitor/assets/native-bridge.js b/ios/Capacitor/Capacitor/assets/native-bridge.js index 01a340469d..2a38f433d7 100644 --- a/ios/Capacitor/Capacitor/assets/native-bridge.js +++ b/ios/Capacitor/Capacitor/assets/native-bridge.js @@ -144,7 +144,7 @@ var nativeBridge = (function (exports) { if (isRelativeOrProxyUrl(url)) return url; const bridgeUrl = new URL((_b = (_a = win.Capacitor) === null || _a === void 0 ? void 0 : _a.getServerUrl()) !== null && _b !== void 0 ? _b : ''); - bridgeUrl.pathname = `${CAPACITOR_HTTP_INTERCEPTOR}`; + bridgeUrl.pathname = CAPACITOR_HTTP_INTERCEPTOR; // URLSearchParams `append()` method will automatically percent encode the url bridgeUrl.searchParams.append(CAPACITOR_HTTP_INTERCEPTOR_URL_PARAM, url); return bridgeUrl.toString(); From 32691d49cf37f9cd1973d0010cb150d7f12b4d47 Mon Sep 17 00:00:00 2001 From: Michael Wolz Date: Tue, 30 Jul 2024 16:09:42 +0200 Subject: [PATCH 5/8] chore: fix formatting --- .../src/main/java/com/getcapacitor/WebViewLocalServer.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java b/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java index 36650eecf9..dafa4dc143 100755 --- a/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java +++ b/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java @@ -172,10 +172,7 @@ private static Uri parseAndVerifyUrl(String url) { public WebResourceResponse shouldInterceptRequest(WebResourceRequest request) { Uri loadingUrl = request.getUrl(); - if ( - null != loadingUrl.getPath() && - loadingUrl.getPath().startsWith(Bridge.CAPACITOR_HTTP_INTERCEPTOR_START) - ) { + if (null != loadingUrl.getPath() && loadingUrl.getPath().startsWith(Bridge.CAPACITOR_HTTP_INTERCEPTOR_START)) { Logger.debug("Handling CapacitorHttp request: " + loadingUrl); try { return handleCapacitorHttpRequest(request); From 4fd503a16adb59b48064099442fd5c578f754e0b Mon Sep 17 00:00:00 2001 From: Michael Wolz Date: Tue, 30 Jul 2024 16:46:45 +0200 Subject: [PATCH 6/8] chore: apply suggestions from code review Co-authored-by: jcesarmobile --- .../src/main/java/com/getcapacitor/WebViewLocalServer.java | 5 +---- core/native-bridge.ts | 1 - ios/Capacitor/Capacitor/WebViewAssetHandler.swift | 1 - 3 files changed, 1 insertion(+), 6 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java b/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java index dafa4dc143..fa9a6ac243 100755 --- a/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java +++ b/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java @@ -253,10 +253,7 @@ private String getReasonPhraseFromResponseCode(int code) { } private WebResourceResponse handleCapacitorHttpRequest(WebResourceRequest request) throws IOException { - Uri uri = Uri.parse(request.getUrl().toString()); - // `getQueryParameter` method of Android Uri lib already decodes the value automatically - String urlString = uri.getQueryParameter(Bridge.CAPACITOR_HTTP_INTERCEPTOR_URL_PARAM); - + String urlString = request.getUrl().getQueryParameter(Bridge.CAPACITOR_HTTP_INTERCEPTOR_URL_PARAM); URL url = new URL(urlString); JSObject headers = new JSObject(); diff --git a/core/native-bridge.ts b/core/native-bridge.ts index 4b16aea51e..f224a7fa6c 100644 --- a/core/native-bridge.ts +++ b/core/native-bridge.ts @@ -139,7 +139,6 @@ const createProxyUrl = (url: string, win: WindowCapacitor): string => { if (isRelativeOrProxyUrl(url)) return url; const bridgeUrl = new URL(win.Capacitor?.getServerUrl() ?? ''); bridgeUrl.pathname = CAPACITOR_HTTP_INTERCEPTOR; - // URLSearchParams `append()` method will automatically percent encode the url bridgeUrl.searchParams.append(CAPACITOR_HTTP_INTERCEPTOR_URL_PARAM, url); return bridgeUrl.toString(); diff --git a/ios/Capacitor/Capacitor/WebViewAssetHandler.swift b/ios/Capacitor/Capacitor/WebViewAssetHandler.swift index b505a7daf5..17649b2567 100644 --- a/ios/Capacitor/Capacitor/WebViewAssetHandler.swift +++ b/ios/Capacitor/Capacitor/WebViewAssetHandler.swift @@ -137,7 +137,6 @@ open class WebViewAssetHandler: NSObject, WKURLSchemeHandler { let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) if let targetUrl = urlComponents?.queryItems?.first(where: { $0.name == CapacitorBridge.httpInterceptorUrlParam })?.value, !targetUrl.isEmpty { - // Note that URLComponents are already percent-decoded so we do not need to do this manually urlRequest.url = URL(string: targetUrl) } From ac97cd432c3198823d9800f04ee2a2a8920bba7c Mon Sep 17 00:00:00 2001 From: Michael Wolz Date: Tue, 30 Jul 2024 16:51:03 +0200 Subject: [PATCH 7/8] fix: re-add removed properties and deprecate them instead --- android/capacitor/src/main/java/com/getcapacitor/Bridge.java | 5 +++++ ios/Capacitor/Capacitor/CapacitorBridge.swift | 2 ++ 2 files changed, 7 insertions(+) diff --git a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java index b12e06d0fe..bcff4682eb 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java @@ -92,6 +92,11 @@ public class Bridge { public static final String CAPACITOR_FILE_START = "/_capacitor_file_"; public static final String CAPACITOR_CONTENT_START = "/_capacitor_content_"; public static final String CAPACITOR_HTTP_INTERCEPTOR_START = "/_capacitor_http_interceptor_"; + + /** @deprecated CAPACITOR_HTTPS_INTERCEPTOR_START is no longer required. All proxied requests are handled via CAPACITOR_HTTP_INTERCEPTOR_START instead */ + @Deprecated + public static final String CAPACITOR_HTTPS_INTERCEPTOR_START = "/_capacitor_https_interceptor_"; + public static final String CAPACITOR_HTTP_INTERCEPTOR_URL_PARAM = "u"; public static final int DEFAULT_ANDROID_WEBVIEW_VERSION = 60; diff --git a/ios/Capacitor/Capacitor/CapacitorBridge.swift b/ios/Capacitor/Capacitor/CapacitorBridge.swift index 213469e0cc..33edae6677 100644 --- a/ios/Capacitor/Capacitor/CapacitorBridge.swift +++ b/ios/Capacitor/Capacitor/CapacitorBridge.swift @@ -95,6 +95,8 @@ open class CapacitorBridge: NSObject, CAPBridgeProtocol { public static let capacitorSite = "https://capacitorjs.com/" public static let fileStartIdentifier = "/_capacitor_file_" public static let httpInterceptorStartIdentifier = "/_capacitor_http_interceptor_" + @available(*, deprecated, message: "`httpsInterceptorStartIdentifier` is no longer required. All proxied requests are handled via `httpInterceptorStartIdentifier` instead") + public static let httpsInterceptorStartIdentifier = "/_capacitor_https_interceptor_" public static let httpInterceptorUrlParam = "u" public static let defaultScheme = "capacitor" From e358f452388293cd4e05833c5266e3f8f23294b1 Mon Sep 17 00:00:00 2001 From: Michael Wolz Date: Tue, 30 Jul 2024 17:03:37 +0200 Subject: [PATCH 8/8] chore: remove unused import --- .../src/main/java/com/getcapacitor/WebViewLocalServer.java | 1 - 1 file changed, 1 deletion(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java b/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java index fa9a6ac243..f1fc63cbe3 100755 --- a/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java +++ b/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java @@ -30,7 +30,6 @@ import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; -import java.net.URLDecoder; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap;