Skip to content

Commit c4e040a

Browse files
authored
fix(http): fix local http requests on native platforms
1 parent 8e8414f commit c4e040a

File tree

3 files changed

+115
-12
lines changed

3 files changed

+115
-12
lines changed

android/capacitor/src/main/assets/native-bridge.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,8 @@ const nativeBridge = (function (exports) {
335335
win.CapacitorWebFetch = window.fetch;
336336
win.CapacitorWebXMLHttpRequest = {
337337
abort: window.XMLHttpRequest.prototype.abort,
338+
getAllResponseHeaders: window.XMLHttpRequest.prototype.getAllResponseHeaders,
339+
getResponseHeader: window.XMLHttpRequest.prototype.getResponseHeader,
338340
open: window.XMLHttpRequest.prototype.open,
339341
send: window.XMLHttpRequest.prototype.send,
340342
setRequestHeader: window.XMLHttpRequest.prototype.setRequestHeader,
@@ -361,8 +363,8 @@ const nativeBridge = (function (exports) {
361363
if (doPatchHttp) {
362364
// fetch patch
363365
window.fetch = async (resource, options) => {
364-
if (resource.toString().startsWith('data:') ||
365-
resource.toString().startsWith('blob:')) {
366+
if (!(resource.toString().startsWith('http:') ||
367+
resource.toString().startsWith('https:'))) {
366368
return win.CapacitorWebFetch(resource, options);
367369
}
368370
try {
@@ -420,17 +422,28 @@ const nativeBridge = (function (exports) {
420422
};
421423
// XHR patch abort
422424
window.XMLHttpRequest.prototype.abort = function () {
425+
if (this._url == null || !(this._url.startsWith('http:') || this._url.startsWith('https:'))) {
426+
return win.CapacitorWebXMLHttpRequest.abort.call(this);
427+
}
423428
this.readyState = 0;
424429
this.dispatchEvent(new Event('abort'));
425430
this.dispatchEvent(new Event('loadend'));
426431
};
427432
// XHR patch open
428433
window.XMLHttpRequest.prototype.open = function (method, url) {
434+
this._url = url;
435+
if (!(url.startsWith('http:') || url.toString().startsWith('https:'))) {
436+
return win.CapacitorWebXMLHttpRequest.open.call(this, method, url);
437+
}
429438
Object.defineProperties(this, {
430439
_headers: {
431440
value: {},
432441
writable: true,
433442
},
443+
_method: {
444+
value: method,
445+
writable: true,
446+
},
434447
readyState: {
435448
get: function () {
436449
var _a;
@@ -459,16 +472,20 @@ const nativeBridge = (function (exports) {
459472
},
460473
});
461474
addEventListeners.call(this);
462-
this._method = method;
463-
this._url = url;
464475
this.readyState = 1;
465476
};
466477
// XHR patch set request header
467478
window.XMLHttpRequest.prototype.setRequestHeader = function (header, value) {
479+
if (this._url == null || !(this._url.startsWith('http:') || this._url.startsWith('https:'))) {
480+
return win.CapacitorWebXMLHttpRequest.setRequestHeader.call(this, header, value);
481+
}
468482
this._headers[header] = value;
469483
};
470484
// XHR patch send
471485
window.XMLHttpRequest.prototype.send = function (body) {
486+
if (this._url == null || !(this._url.startsWith('http:') || this._url.startsWith('https:'))) {
487+
return win.CapacitorWebXMLHttpRequest.send.call(this, body);
488+
}
472489
try {
473490
this.readyState = 2;
474491
// intercept request & pass to the bridge
@@ -522,6 +539,9 @@ const nativeBridge = (function (exports) {
522539
};
523540
// XHR patch getAllResponseHeaders
524541
window.XMLHttpRequest.prototype.getAllResponseHeaders = function () {
542+
if (this._url == null || !(this._url.startsWith('http:') || this._url.startsWith('https:'))) {
543+
return win.CapacitorWebXMLHttpRequest.getAllResponseHeaders.call(this);
544+
}
525545
let returnString = '';
526546
for (const key in this._headers) {
527547
if (key != 'Set-Cookie') {
@@ -532,6 +552,9 @@ const nativeBridge = (function (exports) {
532552
};
533553
// XHR patch getResponseHeader
534554
window.XMLHttpRequest.prototype.getResponseHeader = function (name) {
555+
if (this._url == null || !(this._url.startsWith('http:') || this._url.startsWith('https:'))) {
556+
return win.CapacitorWebXMLHttpRequest.getResponseHeader.call(this, name);
557+
}
535558
return this._headers[name];
536559
};
537560
}

core/native-bridge.ts

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,9 @@ const initBridge = (w: any): void => {
369369
win.CapacitorWebFetch = window.fetch;
370370
win.CapacitorWebXMLHttpRequest = {
371371
abort: window.XMLHttpRequest.prototype.abort,
372+
getAllResponseHeaders:
373+
window.XMLHttpRequest.prototype.getAllResponseHeaders,
374+
getResponseHeader: window.XMLHttpRequest.prototype.getResponseHeader,
372375
open: window.XMLHttpRequest.prototype.open,
373376
send: window.XMLHttpRequest.prototype.send,
374377
setRequestHeader: window.XMLHttpRequest.prototype.setRequestHeader,
@@ -403,8 +406,10 @@ const initBridge = (w: any): void => {
403406
options?: RequestInit,
404407
) => {
405408
if (
406-
resource.toString().startsWith('data:') ||
407-
resource.toString().startsWith('blob:')
409+
!(
410+
resource.toString().startsWith('http:') ||
411+
resource.toString().startsWith('https:')
412+
)
408413
) {
409414
return win.CapacitorWebFetch(resource, options);
410415
}
@@ -472,6 +477,12 @@ const initBridge = (w: any): void => {
472477

473478
// XHR patch abort
474479
window.XMLHttpRequest.prototype.abort = function () {
480+
if (
481+
this._url == null ||
482+
!(this._url.startsWith('http:') || this._url.startsWith('https:'))
483+
) {
484+
return win.CapacitorWebXMLHttpRequest.abort.call(this);
485+
}
475486
this.readyState = 0;
476487
this.dispatchEvent(new Event('abort'));
477488
this.dispatchEvent(new Event('loadend'));
@@ -482,11 +493,23 @@ const initBridge = (w: any): void => {
482493
method: string,
483494
url: string,
484495
) {
496+
this._url = url;
497+
498+
if (
499+
!(url.startsWith('http:') || url.toString().startsWith('https:'))
500+
) {
501+
return win.CapacitorWebXMLHttpRequest.open.call(this, method, url);
502+
}
503+
485504
Object.defineProperties(this, {
486505
_headers: {
487506
value: {},
488507
writable: true,
489508
},
509+
_method: {
510+
value: method,
511+
writable: true,
512+
},
490513
readyState: {
491514
get: function () {
492515
return this._readyState ?? 0;
@@ -513,9 +536,8 @@ const initBridge = (w: any): void => {
513536
writable: true,
514537
},
515538
});
539+
516540
addEventListeners.call(this);
517-
this._method = method;
518-
this._url = url;
519541
this.readyState = 1;
520542
};
521543

@@ -524,13 +546,30 @@ const initBridge = (w: any): void => {
524546
header: string,
525547
value: string,
526548
) {
549+
if (
550+
this._url == null ||
551+
!(this._url.startsWith('http:') || this._url.startsWith('https:'))
552+
) {
553+
return win.CapacitorWebXMLHttpRequest.setRequestHeader.call(
554+
this,
555+
header,
556+
value,
557+
);
558+
}
527559
this._headers[header] = value;
528560
};
529561

530562
// XHR patch send
531563
window.XMLHttpRequest.prototype.send = function (
532564
body?: Document | XMLHttpRequestBodyInit,
533565
) {
566+
if (
567+
this._url == null ||
568+
!(this._url.startsWith('http:') || this._url.startsWith('https:'))
569+
) {
570+
return win.CapacitorWebXMLHttpRequest.send.call(this, body);
571+
}
572+
534573
try {
535574
this.readyState = 2;
536575

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

586625
// XHR patch getAllResponseHeaders
587626
window.XMLHttpRequest.prototype.getAllResponseHeaders = function () {
627+
if (
628+
this._url == null ||
629+
!(this._url.startsWith('http:') || this._url.startsWith('https:'))
630+
) {
631+
return win.CapacitorWebXMLHttpRequest.getAllResponseHeaders.call(
632+
this,
633+
);
634+
}
635+
588636
let returnString = '';
589637
for (const key in this._headers) {
590638
if (key != 'Set-Cookie') {
@@ -596,6 +644,15 @@ const initBridge = (w: any): void => {
596644

597645
// XHR patch getResponseHeader
598646
window.XMLHttpRequest.prototype.getResponseHeader = function (name) {
647+
if (
648+
this._url == null ||
649+
!(this._url.startsWith('http:') || this._url.startsWith('https:'))
650+
) {
651+
return win.CapacitorWebXMLHttpRequest.getResponseHeader.call(
652+
this,
653+
name,
654+
);
655+
}
599656
return this._headers[name];
600657
};
601658
}

ios/Capacitor/Capacitor/assets/native-bridge.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,8 @@ const nativeBridge = (function (exports) {
335335
win.CapacitorWebFetch = window.fetch;
336336
win.CapacitorWebXMLHttpRequest = {
337337
abort: window.XMLHttpRequest.prototype.abort,
338+
getAllResponseHeaders: window.XMLHttpRequest.prototype.getAllResponseHeaders,
339+
getResponseHeader: window.XMLHttpRequest.prototype.getResponseHeader,
338340
open: window.XMLHttpRequest.prototype.open,
339341
send: window.XMLHttpRequest.prototype.send,
340342
setRequestHeader: window.XMLHttpRequest.prototype.setRequestHeader,
@@ -361,8 +363,8 @@ const nativeBridge = (function (exports) {
361363
if (doPatchHttp) {
362364
// fetch patch
363365
window.fetch = async (resource, options) => {
364-
if (resource.toString().startsWith('data:') ||
365-
resource.toString().startsWith('blob:')) {
366+
if (!(resource.toString().startsWith('http:') ||
367+
resource.toString().startsWith('https:'))) {
366368
return win.CapacitorWebFetch(resource, options);
367369
}
368370
try {
@@ -420,17 +422,28 @@ const nativeBridge = (function (exports) {
420422
};
421423
// XHR patch abort
422424
window.XMLHttpRequest.prototype.abort = function () {
425+
if (this._url == null || !(this._url.startsWith('http:') || this._url.startsWith('https:'))) {
426+
return win.CapacitorWebXMLHttpRequest.abort.call(this);
427+
}
423428
this.readyState = 0;
424429
this.dispatchEvent(new Event('abort'));
425430
this.dispatchEvent(new Event('loadend'));
426431
};
427432
// XHR patch open
428433
window.XMLHttpRequest.prototype.open = function (method, url) {
434+
this._url = url;
435+
if (!(url.startsWith('http:') || url.toString().startsWith('https:'))) {
436+
return win.CapacitorWebXMLHttpRequest.open.call(this, method, url);
437+
}
429438
Object.defineProperties(this, {
430439
_headers: {
431440
value: {},
432441
writable: true,
433442
},
443+
_method: {
444+
value: method,
445+
writable: true,
446+
},
434447
readyState: {
435448
get: function () {
436449
var _a;
@@ -459,16 +472,20 @@ const nativeBridge = (function (exports) {
459472
},
460473
});
461474
addEventListeners.call(this);
462-
this._method = method;
463-
this._url = url;
464475
this.readyState = 1;
465476
};
466477
// XHR patch set request header
467478
window.XMLHttpRequest.prototype.setRequestHeader = function (header, value) {
479+
if (this._url == null || !(this._url.startsWith('http:') || this._url.startsWith('https:'))) {
480+
return win.CapacitorWebXMLHttpRequest.setRequestHeader.call(this, header, value);
481+
}
468482
this._headers[header] = value;
469483
};
470484
// XHR patch send
471485
window.XMLHttpRequest.prototype.send = function (body) {
486+
if (this._url == null || !(this._url.startsWith('http:') || this._url.startsWith('https:'))) {
487+
return win.CapacitorWebXMLHttpRequest.send.call(this, body);
488+
}
472489
try {
473490
this.readyState = 2;
474491
// intercept request & pass to the bridge
@@ -522,6 +539,9 @@ const nativeBridge = (function (exports) {
522539
};
523540
// XHR patch getAllResponseHeaders
524541
window.XMLHttpRequest.prototype.getAllResponseHeaders = function () {
542+
if (this._url == null || !(this._url.startsWith('http:') || this._url.startsWith('https:'))) {
543+
return win.CapacitorWebXMLHttpRequest.getAllResponseHeaders.call(this);
544+
}
525545
let returnString = '';
526546
for (const key in this._headers) {
527547
if (key != 'Set-Cookie') {
@@ -532,6 +552,9 @@ const nativeBridge = (function (exports) {
532552
};
533553
// XHR patch getResponseHeader
534554
window.XMLHttpRequest.prototype.getResponseHeader = function (name) {
555+
if (this._url == null || !(this._url.startsWith('http:') || this._url.startsWith('https:'))) {
556+
return win.CapacitorWebXMLHttpRequest.getResponseHeader.call(this, name);
557+
}
535558
return this._headers[name];
536559
};
537560
}

0 commit comments

Comments
 (0)