Skip to content

Commit

Permalink
fix(http): handle proxy urls with port (#7273)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Feb 23, 2024
1 parent 42aef33 commit 514409a
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 13 deletions.
7 changes: 4 additions & 3 deletions android/capacitor/src/main/assets/native-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ var nativeBridge = (function (exports) {
return url;
let proxyUrl = new URL(url);
const isHttps = proxyUrl.protocol === 'https:';
const originalHostname = proxyUrl.hostname;
const originalHost = encodeURIComponent(proxyUrl.host);
const originalPathname = proxyUrl.pathname;
proxyUrl = new URL((_b = (_a = win.Capacitor) === null || _a === void 0 ? void 0 : _a.getServerUrl()) !== null && _b !== void 0 ? _b : '');
proxyUrl.pathname = `${isHttps ? CAPACITOR_HTTPS_INTERCEPTOR : CAPACITOR_HTTP_INTERCEPTOR}/${originalHostname}${originalPathname}`;
proxyUrl.pathname = `${isHttps ? CAPACITOR_HTTPS_INTERCEPTOR : CAPACITOR_HTTP_INTERCEPTOR}/${originalHost}${originalPathname}`;
return proxyUrl.toString();
};
const initBridge = (w) => {
Expand Down Expand Up @@ -678,7 +678,8 @@ var nativeBridge = (function (exports) {
else {
this.response = nativeResponse.data;
}
this.responseText = ((_a = (nativeResponse.headers['Content-Type'] || nativeResponse.headers['content-type'])) === null || _a === void 0 ? void 0 : _a.startsWith('application/json'))
this.responseText = ((_a = (nativeResponse.headers['Content-Type'] ||
nativeResponse.headers['content-type'])) === null || _a === void 0 ? void 0 : _a.startsWith('application/json'))
? JSON.stringify(nativeResponse.data)
: nativeResponse.data;
this.responseURL = nativeResponse.url;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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;
Expand Down Expand Up @@ -266,6 +267,7 @@ private WebResourceResponse handleCapacitorHttpRequest(WebResourceRequest reques
.replace(bridge.getLocalUrl(), isHttps ? "https:/" : "http:/")
.replace(Bridge.CAPACITOR_HTTP_INTERCEPTOR_START, "")
.replace(Bridge.CAPACITOR_HTTPS_INTERCEPTOR_START, "");
urlString = URLDecoder.decode(urlString, "UTF-8");
URL url = new URL(urlString);
JSObject headers = new JSObject();

Expand Down
4 changes: 2 additions & 2 deletions core/native-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,13 @@ const createProxyUrl = (url: string, win: WindowCapacitor): string => {

let proxyUrl = new URL(url);
const isHttps = proxyUrl.protocol === 'https:';
const originalHostname = proxyUrl.hostname;
const originalHost = encodeURIComponent(proxyUrl.host);
const originalPathname = proxyUrl.pathname;
proxyUrl = new URL(win.Capacitor?.getServerUrl() ?? '');

proxyUrl.pathname = `${
isHttps ? CAPACITOR_HTTPS_INTERCEPTOR : CAPACITOR_HTTP_INTERCEPTOR
}/${originalHostname}${originalPathname}`;
}/${originalHost}${originalPathname}`;
return proxyUrl.toString();
};

Expand Down
3 changes: 1 addition & 2 deletions ios/Capacitor/Capacitor/WebViewAssetHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ open class WebViewAssetHandler: NSObject, WKURLSchemeHandler {
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")
Expand All @@ -152,7 +151,7 @@ open class WebViewAssetHandler: NSObject, WKURLSchemeHandler {
targetUrl = targetUrl.replacingCharacters(in: range, with: "")
}

urlRequest.url = URL(string: targetUrl)
urlRequest.url = URL(string: targetUrl.removingPercentEncoding ?? targetUrl)

let urlSession = URLSession.shared
let task = urlSession.dataTask(with: urlRequest) { (data, response, error) in
Expand Down
11 changes: 5 additions & 6 deletions ios/Capacitor/Capacitor/assets/native-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ var nativeBridge = (function (exports) {
return url;
let proxyUrl = new URL(url);
const isHttps = proxyUrl.protocol === 'https:';
const originalHostname = proxyUrl.hostname;
const originalHost = encodeURIComponent(proxyUrl.host);
const originalPathname = proxyUrl.pathname;
proxyUrl = new URL((_b = (_a = win.Capacitor) === null || _a === void 0 ? void 0 : _a.getServerUrl()) !== null && _b !== void 0 ? _b : '');
proxyUrl.pathname = `${isHttps ? CAPACITOR_HTTPS_INTERCEPTOR : CAPACITOR_HTTP_INTERCEPTOR}/${originalHostname}${originalPathname}`;
proxyUrl.pathname = `${isHttps ? CAPACITOR_HTTPS_INTERCEPTOR : CAPACITOR_HTTP_INTERCEPTOR}/${originalHost}${originalPathname}`;
return proxyUrl.toString();
};
const initBridge = (w) => {
Expand Down Expand Up @@ -678,7 +678,8 @@ var nativeBridge = (function (exports) {
else {
this.response = nativeResponse.data;
}
this.responseText = ((_a = (nativeResponse.headers['Content-Type'] || nativeResponse.headers['content-type'])) === null || _a === void 0 ? void 0 : _a.startsWith('application/json'))
this.responseText = ((_a = (nativeResponse.headers['Content-Type'] ||
nativeResponse.headers['content-type'])) === null || _a === void 0 ? void 0 : _a.startsWith('application/json'))
? JSON.stringify(nativeResponse.data)
: nativeResponse.data;
this.responseURL = nativeResponse.url;
Expand Down Expand Up @@ -751,9 +752,7 @@ var nativeBridge = (function (exports) {
if (isRelativeOrProxyUrl(this._url)) {
return win.CapacitorWebXMLHttpRequest.getResponseHeader.call(this, name);
}
// The search for the name is case insenstive. The Swift Code makes sure that the keys
// in the headers dictionary are already lowercased.
return this._headers[name.toLowerCase()];
return this._headers[name];
};
Object.setPrototypeOf(xhr, prototype);
return xhr;
Expand Down

0 comments on commit 514409a

Please sign in to comment.