Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(http): pass original url as query param on the proxy url #7527

Merged
merged 10 commits into from
Aug 5, 2024
13 changes: 5 additions & 8 deletions android/capacitor/src/main/assets/native-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,23 +140,20 @@ 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 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}`;
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();
};
const initBridge = (w) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ 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_";
michaelwolz marked this conversation as resolved.
Show resolved Hide resolved
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +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) ||
loadingUrl.getPath().startsWith(Bridge.CAPACITOR_HTTPS_INTERCEPTOR_START)
)
) {
if (null != loadingUrl.getPath() && loadingUrl.getPath().startsWith(Bridge.CAPACITOR_HTTP_INTERCEPTOR_START)) {
Logger.debug("Handling CapacitorHttp request: " + loadingUrl);
try {
return handleCapacitorHttpRequest(request);
Expand Down Expand Up @@ -259,16 +253,10 @@ 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);

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);

michaelwolz marked this conversation as resolved.
Show resolved Hide resolved
URL url = new URL(urlString);
JSObject headers = new JSObject();

Expand Down
17 changes: 6 additions & 11 deletions core/native-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,27 +126,22 @@ 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 bridgeUrl = new URL(win.Capacitor?.getServerUrl() ?? '');
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}`;
bridgeUrl.pathname = CAPACITOR_HTTP_INTERCEPTOR;
// URLSearchParams `append()` method will automatically percent encode the url
michaelwolz marked this conversation as resolved.
Show resolved Hide resolved
bridgeUrl.searchParams.append(CAPACITOR_HTTP_INTERCEPTOR_URL_PARAM, url);

return bridgeUrl.toString();
};

Expand Down
2 changes: 1 addition & 1 deletion ios/Capacitor/Capacitor/CapacitorBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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_"
michaelwolz marked this conversation as resolved.
Show resolved Hide resolved
public static let httpInterceptorUrlParam = "u"
public static let defaultScheme = "capacitor"

public private(set) var webViewAssetHandler: WebViewAssetHandler
Expand Down
24 changes: 6 additions & 18 deletions ios/Capacitor/Capacitor/WebViewAssetHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
michaelwolz marked this conversation as resolved.
Show resolved Hide resolved
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 {
Expand Down
13 changes: 5 additions & 8 deletions ios/Capacitor/Capacitor/assets/native-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,23 +140,20 @@ 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 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}`;
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();
};
const initBridge = (w) => {
Expand Down