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): don't throw errors when content-type is null on response #6627

Merged
merged 1 commit into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions android/capacitor/src/main/assets/native-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ var nativeBridge = (function (exports) {
if (nav) {
nav.app = nav.app || {};
nav.app.exitApp = () => {
if (!cap.Plugins || !cap.Plugins.App) {
var _a;
if (!((_a = cap.Plugins) === null || _a === void 0 ? void 0 : _a.App)) {
win.console.warn('App plugin not installed');
}
else {
Expand All @@ -147,6 +148,7 @@ var nativeBridge = (function (exports) {
if (doc) {
const docAddEventListener = doc.addEventListener;
doc.addEventListener = (...args) => {
var _a;
const eventName = args[0];
const handler = args[1];
if (eventName === 'deviceready' && handler) {
Expand All @@ -155,7 +157,7 @@ var nativeBridge = (function (exports) {
else if (eventName === 'backbutton' && cap.Plugins.App) {
// Add a dummy listener so Capacitor doesn't do the default
// back button action
if (!cap.Plugins || !cap.Plugins.App) {
if (!((_a = cap.Plugins) === null || _a === void 0 ? void 0 : _a.App)) {
win.console.warn('App plugin not installed');
}
else {
Expand Down Expand Up @@ -366,6 +368,7 @@ var nativeBridge = (function (exports) {
if (doPatchHttp) {
// fetch patch
window.fetch = async (resource, options) => {
var _a;
if (!(resource.toString().startsWith('http:') ||
resource.toString().startsWith('https:'))) {
return win.CapacitorWebFetch(resource, options);
Expand All @@ -384,9 +387,8 @@ var nativeBridge = (function (exports) {
data: (options === null || options === void 0 ? void 0 : options.body) ? options.body : undefined,
headers: headers,
});
let data = !nativeResponse.headers['Content-Type'].startsWith('application/json')
? nativeResponse.data
: JSON.stringify(nativeResponse.data);
let data = ((_a = nativeResponse.headers['Content-Type']) === null || _a === void 0 ? void 0 : _a.startsWith('application/json'))
? JSON.stringify(nativeResponse.data) : nativeResponse.data;
// use null data for 204 No Content HTTP response
if (nativeResponse.status === 204) {
data = null;
Expand Down Expand Up @@ -527,15 +529,15 @@ var nativeBridge = (function (exports) {
: undefined,
})
.then((nativeResponse) => {
var _a;
// intercept & parse response before returning
if (this.readyState == 2) {
this.dispatchEvent(new Event('loadstart'));
this._headers = nativeResponse.headers;
this.status = nativeResponse.status;
this.response = nativeResponse.data;
this.responseText = !nativeResponse.headers['Content-Type'].startsWith('application/json')
? nativeResponse.data
: JSON.stringify(nativeResponse.data);
this.responseText = ((_a = nativeResponse.headers['Content-Type']) === null || _a === void 0 ? void 0 : _a.startsWith('application/json'))
? JSON.stringify(nativeResponse.data) : nativeResponse.data;
this.responseURL = nativeResponse.url;
this.readyState = 4;
this.dispatchEvent(new Event('load'));
Expand Down
14 changes: 7 additions & 7 deletions core/native-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,11 +437,11 @@ const initBridge = (w: any): void => {
},
);

let data = !nativeResponse.headers['Content-Type'].startsWith(
let data = nativeResponse.headers['Content-Type']?.startsWith(
'application/json',
)
? nativeResponse.data
: JSON.stringify(nativeResponse.data);
? JSON.stringify(nativeResponse.data)
: nativeResponse.data;

// use null data for 204 No Content HTTP response
if (nativeResponse.status === 204) {
Expand Down Expand Up @@ -622,11 +622,11 @@ const initBridge = (w: any): void => {
this._headers = nativeResponse.headers;
this.status = nativeResponse.status;
this.response = nativeResponse.data;
this.responseText = !nativeResponse.headers[
this.responseText = nativeResponse.headers[
'Content-Type'
].startsWith('application/json')
? nativeResponse.data
: JSON.stringify(nativeResponse.data);
]?.startsWith('application/json')
? JSON.stringify(nativeResponse.data)
: nativeResponse.data;
this.responseURL = nativeResponse.url;
this.readyState = 4;
this.dispatchEvent(new Event('load'));
Expand Down
18 changes: 10 additions & 8 deletions ios/Capacitor/Capacitor/assets/native-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ var nativeBridge = (function (exports) {
if (nav) {
nav.app = nav.app || {};
nav.app.exitApp = () => {
if (!cap.Plugins || !cap.Plugins.App) {
var _a;
if (!((_a = cap.Plugins) === null || _a === void 0 ? void 0 : _a.App)) {
win.console.warn('App plugin not installed');
}
else {
Expand All @@ -147,6 +148,7 @@ var nativeBridge = (function (exports) {
if (doc) {
const docAddEventListener = doc.addEventListener;
doc.addEventListener = (...args) => {
var _a;
const eventName = args[0];
const handler = args[1];
if (eventName === 'deviceready' && handler) {
Expand All @@ -155,7 +157,7 @@ var nativeBridge = (function (exports) {
else if (eventName === 'backbutton' && cap.Plugins.App) {
// Add a dummy listener so Capacitor doesn't do the default
// back button action
if (!cap.Plugins || !cap.Plugins.App) {
if (!((_a = cap.Plugins) === null || _a === void 0 ? void 0 : _a.App)) {
win.console.warn('App plugin not installed');
}
else {
Expand Down Expand Up @@ -366,6 +368,7 @@ var nativeBridge = (function (exports) {
if (doPatchHttp) {
// fetch patch
window.fetch = async (resource, options) => {
var _a;
if (!(resource.toString().startsWith('http:') ||
resource.toString().startsWith('https:'))) {
return win.CapacitorWebFetch(resource, options);
Expand All @@ -384,9 +387,8 @@ var nativeBridge = (function (exports) {
data: (options === null || options === void 0 ? void 0 : options.body) ? options.body : undefined,
headers: headers,
});
let data = !nativeResponse.headers['Content-Type'].startsWith('application/json')
? nativeResponse.data
: JSON.stringify(nativeResponse.data);
let data = ((_a = nativeResponse.headers['Content-Type']) === null || _a === void 0 ? void 0 : _a.startsWith('application/json'))
? JSON.stringify(nativeResponse.data) : nativeResponse.data;
// use null data for 204 No Content HTTP response
if (nativeResponse.status === 204) {
data = null;
Expand Down Expand Up @@ -527,15 +529,15 @@ var nativeBridge = (function (exports) {
: undefined,
})
.then((nativeResponse) => {
var _a;
// intercept & parse response before returning
if (this.readyState == 2) {
this.dispatchEvent(new Event('loadstart'));
this._headers = nativeResponse.headers;
this.status = nativeResponse.status;
this.response = nativeResponse.data;
this.responseText = !nativeResponse.headers['Content-Type'].startsWith('application/json')
? nativeResponse.data
: JSON.stringify(nativeResponse.data);
this.responseText = ((_a = nativeResponse.headers['Content-Type']) === null || _a === void 0 ? void 0 : _a.startsWith('application/json'))
? JSON.stringify(nativeResponse.data) : nativeResponse.data;
this.responseURL = nativeResponse.url;
this.readyState = 4;
this.dispatchEvent(new Event('load'));
Expand Down