Skip to content

Commit

Permalink
fix(http): fix local http requests on native platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsChaceD committed Oct 17, 2022
1 parent 8e8414f commit c4e040a
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 12 deletions.
31 changes: 27 additions & 4 deletions android/capacitor/src/main/assets/native-bridge.js
Expand Up @@ -335,6 +335,8 @@ const nativeBridge = (function (exports) {
win.CapacitorWebFetch = window.fetch;
win.CapacitorWebXMLHttpRequest = {
abort: window.XMLHttpRequest.prototype.abort,
getAllResponseHeaders: window.XMLHttpRequest.prototype.getAllResponseHeaders,
getResponseHeader: window.XMLHttpRequest.prototype.getResponseHeader,
open: window.XMLHttpRequest.prototype.open,
send: window.XMLHttpRequest.prototype.send,
setRequestHeader: window.XMLHttpRequest.prototype.setRequestHeader,
Expand All @@ -361,8 +363,8 @@ const nativeBridge = (function (exports) {
if (doPatchHttp) {
// fetch patch
window.fetch = async (resource, options) => {
if (resource.toString().startsWith('data:') ||
resource.toString().startsWith('blob:')) {
if (!(resource.toString().startsWith('http:') ||
resource.toString().startsWith('https:'))) {
return win.CapacitorWebFetch(resource, options);
}
try {
Expand Down Expand Up @@ -420,17 +422,28 @@ const nativeBridge = (function (exports) {
};
// XHR patch abort
window.XMLHttpRequest.prototype.abort = function () {
if (this._url == null || !(this._url.startsWith('http:') || this._url.startsWith('https:'))) {
return win.CapacitorWebXMLHttpRequest.abort.call(this);
}
this.readyState = 0;
this.dispatchEvent(new Event('abort'));
this.dispatchEvent(new Event('loadend'));
};
// XHR patch open
window.XMLHttpRequest.prototype.open = function (method, url) {
this._url = url;
if (!(url.startsWith('http:') || url.toString().startsWith('https:'))) {
return win.CapacitorWebXMLHttpRequest.open.call(this, method, url);
}
Object.defineProperties(this, {
_headers: {
value: {},
writable: true,
},
_method: {
value: method,
writable: true,
},
readyState: {
get: function () {
var _a;
Expand Down Expand Up @@ -459,16 +472,20 @@ const nativeBridge = (function (exports) {
},
});
addEventListeners.call(this);
this._method = method;
this._url = url;
this.readyState = 1;
};
// XHR patch set request header
window.XMLHttpRequest.prototype.setRequestHeader = function (header, value) {
if (this._url == null || !(this._url.startsWith('http:') || this._url.startsWith('https:'))) {
return win.CapacitorWebXMLHttpRequest.setRequestHeader.call(this, header, value);
}
this._headers[header] = value;
};
// XHR patch send
window.XMLHttpRequest.prototype.send = function (body) {
if (this._url == null || !(this._url.startsWith('http:') || this._url.startsWith('https:'))) {
return win.CapacitorWebXMLHttpRequest.send.call(this, body);
}
try {
this.readyState = 2;
// intercept request & pass to the bridge
Expand Down Expand Up @@ -522,6 +539,9 @@ const nativeBridge = (function (exports) {
};
// XHR patch getAllResponseHeaders
window.XMLHttpRequest.prototype.getAllResponseHeaders = function () {
if (this._url == null || !(this._url.startsWith('http:') || this._url.startsWith('https:'))) {
return win.CapacitorWebXMLHttpRequest.getAllResponseHeaders.call(this);
}
let returnString = '';
for (const key in this._headers) {
if (key != 'Set-Cookie') {
Expand All @@ -532,6 +552,9 @@ const nativeBridge = (function (exports) {
};
// XHR patch getResponseHeader
window.XMLHttpRequest.prototype.getResponseHeader = function (name) {
if (this._url == null || !(this._url.startsWith('http:') || this._url.startsWith('https:'))) {
return win.CapacitorWebXMLHttpRequest.getResponseHeader.call(this, name);
}
return this._headers[name];
};
}
Expand Down
65 changes: 61 additions & 4 deletions core/native-bridge.ts
Expand Up @@ -369,6 +369,9 @@ const initBridge = (w: any): void => {
win.CapacitorWebFetch = window.fetch;
win.CapacitorWebXMLHttpRequest = {
abort: window.XMLHttpRequest.prototype.abort,
getAllResponseHeaders:
window.XMLHttpRequest.prototype.getAllResponseHeaders,
getResponseHeader: window.XMLHttpRequest.prototype.getResponseHeader,
open: window.XMLHttpRequest.prototype.open,
send: window.XMLHttpRequest.prototype.send,
setRequestHeader: window.XMLHttpRequest.prototype.setRequestHeader,
Expand Down Expand Up @@ -403,8 +406,10 @@ const initBridge = (w: any): void => {
options?: RequestInit,
) => {
if (
resource.toString().startsWith('data:') ||
resource.toString().startsWith('blob:')
!(
resource.toString().startsWith('http:') ||
resource.toString().startsWith('https:')
)
) {
return win.CapacitorWebFetch(resource, options);
}
Expand Down Expand Up @@ -472,6 +477,12 @@ const initBridge = (w: any): void => {

// XHR patch abort
window.XMLHttpRequest.prototype.abort = function () {
if (
this._url == null ||
!(this._url.startsWith('http:') || this._url.startsWith('https:'))
) {
return win.CapacitorWebXMLHttpRequest.abort.call(this);
}
this.readyState = 0;
this.dispatchEvent(new Event('abort'));
this.dispatchEvent(new Event('loadend'));
Expand All @@ -482,11 +493,23 @@ const initBridge = (w: any): void => {
method: string,
url: string,
) {
this._url = url;

if (
!(url.startsWith('http:') || url.toString().startsWith('https:'))
) {
return win.CapacitorWebXMLHttpRequest.open.call(this, method, url);
}

Object.defineProperties(this, {
_headers: {
value: {},
writable: true,
},
_method: {
value: method,
writable: true,
},
readyState: {
get: function () {
return this._readyState ?? 0;
Expand All @@ -513,9 +536,8 @@ const initBridge = (w: any): void => {
writable: true,
},
});

addEventListeners.call(this);
this._method = method;
this._url = url;
this.readyState = 1;
};

Expand All @@ -524,13 +546,30 @@ const initBridge = (w: any): void => {
header: string,
value: string,
) {
if (
this._url == null ||
!(this._url.startsWith('http:') || this._url.startsWith('https:'))
) {
return win.CapacitorWebXMLHttpRequest.setRequestHeader.call(
this,
header,
value,
);
}
this._headers[header] = value;
};

// XHR patch send
window.XMLHttpRequest.prototype.send = function (
body?: Document | XMLHttpRequestBodyInit,
) {
if (
this._url == null ||
!(this._url.startsWith('http:') || this._url.startsWith('https:'))
) {
return win.CapacitorWebXMLHttpRequest.send.call(this, body);
}

try {
this.readyState = 2;

Expand Down Expand Up @@ -585,6 +624,15 @@ const initBridge = (w: any): void => {

// XHR patch getAllResponseHeaders
window.XMLHttpRequest.prototype.getAllResponseHeaders = function () {
if (
this._url == null ||
!(this._url.startsWith('http:') || this._url.startsWith('https:'))
) {
return win.CapacitorWebXMLHttpRequest.getAllResponseHeaders.call(
this,
);
}

let returnString = '';
for (const key in this._headers) {
if (key != 'Set-Cookie') {
Expand All @@ -596,6 +644,15 @@ const initBridge = (w: any): void => {

// XHR patch getResponseHeader
window.XMLHttpRequest.prototype.getResponseHeader = function (name) {
if (
this._url == null ||
!(this._url.startsWith('http:') || this._url.startsWith('https:'))
) {
return win.CapacitorWebXMLHttpRequest.getResponseHeader.call(
this,
name,
);
}
return this._headers[name];
};
}
Expand Down
31 changes: 27 additions & 4 deletions ios/Capacitor/Capacitor/assets/native-bridge.js
Expand Up @@ -335,6 +335,8 @@ const nativeBridge = (function (exports) {
win.CapacitorWebFetch = window.fetch;
win.CapacitorWebXMLHttpRequest = {
abort: window.XMLHttpRequest.prototype.abort,
getAllResponseHeaders: window.XMLHttpRequest.prototype.getAllResponseHeaders,
getResponseHeader: window.XMLHttpRequest.prototype.getResponseHeader,
open: window.XMLHttpRequest.prototype.open,
send: window.XMLHttpRequest.prototype.send,
setRequestHeader: window.XMLHttpRequest.prototype.setRequestHeader,
Expand All @@ -361,8 +363,8 @@ const nativeBridge = (function (exports) {
if (doPatchHttp) {
// fetch patch
window.fetch = async (resource, options) => {
if (resource.toString().startsWith('data:') ||
resource.toString().startsWith('blob:')) {
if (!(resource.toString().startsWith('http:') ||
resource.toString().startsWith('https:'))) {
return win.CapacitorWebFetch(resource, options);
}
try {
Expand Down Expand Up @@ -420,17 +422,28 @@ const nativeBridge = (function (exports) {
};
// XHR patch abort
window.XMLHttpRequest.prototype.abort = function () {
if (this._url == null || !(this._url.startsWith('http:') || this._url.startsWith('https:'))) {
return win.CapacitorWebXMLHttpRequest.abort.call(this);
}
this.readyState = 0;
this.dispatchEvent(new Event('abort'));
this.dispatchEvent(new Event('loadend'));
};
// XHR patch open
window.XMLHttpRequest.prototype.open = function (method, url) {
this._url = url;
if (!(url.startsWith('http:') || url.toString().startsWith('https:'))) {
return win.CapacitorWebXMLHttpRequest.open.call(this, method, url);
}
Object.defineProperties(this, {
_headers: {
value: {},
writable: true,
},
_method: {
value: method,
writable: true,
},
readyState: {
get: function () {
var _a;
Expand Down Expand Up @@ -459,16 +472,20 @@ const nativeBridge = (function (exports) {
},
});
addEventListeners.call(this);
this._method = method;
this._url = url;
this.readyState = 1;
};
// XHR patch set request header
window.XMLHttpRequest.prototype.setRequestHeader = function (header, value) {
if (this._url == null || !(this._url.startsWith('http:') || this._url.startsWith('https:'))) {
return win.CapacitorWebXMLHttpRequest.setRequestHeader.call(this, header, value);
}
this._headers[header] = value;
};
// XHR patch send
window.XMLHttpRequest.prototype.send = function (body) {
if (this._url == null || !(this._url.startsWith('http:') || this._url.startsWith('https:'))) {
return win.CapacitorWebXMLHttpRequest.send.call(this, body);
}
try {
this.readyState = 2;
// intercept request & pass to the bridge
Expand Down Expand Up @@ -522,6 +539,9 @@ const nativeBridge = (function (exports) {
};
// XHR patch getAllResponseHeaders
window.XMLHttpRequest.prototype.getAllResponseHeaders = function () {
if (this._url == null || !(this._url.startsWith('http:') || this._url.startsWith('https:'))) {
return win.CapacitorWebXMLHttpRequest.getAllResponseHeaders.call(this);
}
let returnString = '';
for (const key in this._headers) {
if (key != 'Set-Cookie') {
Expand All @@ -532,6 +552,9 @@ const nativeBridge = (function (exports) {
};
// XHR patch getResponseHeader
window.XMLHttpRequest.prototype.getResponseHeader = function (name) {
if (this._url == null || !(this._url.startsWith('http:') || this._url.startsWith('https:'))) {
return win.CapacitorWebXMLHttpRequest.getResponseHeader.call(this, name);
}
return this._headers[name];
};
}
Expand Down

0 comments on commit c4e040a

Please sign in to comment.