Skip to content

Commit a002e8f

Browse files
authored
Merge pull request #4670 from dpalou/MOBILE-4941
MOBILE-4941 ws: Make header checks case insensitive
2 parents 8926581 + 48f72c5 commit a002e8f

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

src/core/services/ws.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414

1515
import { Injectable } from '@angular/core';
16-
import { HttpResponse, HttpParams, HttpErrorResponse } from '@angular/common/http';
16+
import { HttpResponse, HttpParams, HttpErrorResponse, HttpHeaders } from '@angular/common/http';
1717

1818
import { FileEntry } from '@awesome-cordova-plugins/file/ngx';
1919
import { HTTPResponse as NativeHttpResponse } from '@awesome-cordova-plugins/http';
@@ -256,8 +256,8 @@ export class CoreWSProvider {
256256
// Create the tmp file as an empty file.
257257
const fileEntry = await CoreFile.createFile(tmpPath);
258258

259-
let fileDownloaded: { entry: globalThis.FileEntry; headers: Record<string, string> | undefined};
260-
let redirectUrl: string | undefined;
259+
let headers: HttpHeaders | undefined;
260+
let redirectUrl: string | null = null;
261261
let maxRedirects = 5;
262262
do {
263263
const transfer = new window.FileTransfer();
@@ -266,21 +266,23 @@ export class CoreWSProvider {
266266
}
267267

268268
// Download the file in the tmp file.
269-
fileDownloaded = await new Promise((resolve, reject) => {
269+
const responseHeaders = await new Promise<Record<string, string> | undefined>((resolve, reject) => {
270270
transfer.download(
271271
redirectUrl ?? url,
272272
CoreFile.getFileEntryURL(fileEntry),
273-
(result) => resolve(result),
273+
(result) => resolve(result.headers),
274274
(error: FileTransferError) => reject(error),
275275
true,
276276
{ headers: { 'User-Agent': navigator.userAgent } },
277277
);
278278
});
279279

280+
headers = new HttpHeaders(responseHeaders); // Convert to HttpHeaders because names are normalised.
281+
280282
// Redirections should have been handled by the platform,
281283
// but Android does not follow redirections between HTTP and HTTPS.
282284
// See: https://developer.android.com/reference/java/net/HttpURLConnection#response-handling
283-
redirectUrl = fileDownloaded.headers?.['location'] ?? fileDownloaded.headers?.['Location'];
285+
redirectUrl = headers.get('Location');
284286
maxRedirects--;
285287
} while (redirectUrl && maxRedirects >= 0);
286288

@@ -293,8 +295,7 @@ export class CoreWSProvider {
293295
if (!extension || ['gdoc', 'gsheet', 'gslides', 'gdraw', 'php'].includes(extension)) {
294296

295297
// Not valid, get the file's mimetype.
296-
const contentType = fileDownloaded.headers?.['Content-Type'] || fileDownloaded.headers?.['content-type'];
297-
const requestContentType = contentType?.split(';')[0];
298+
const requestContentType = headers.get('Content-Type')?.split(';')[0];
298299
const mimetype = requestContentType ?? await this.getRemoteFileMimeType(url);
299300

300301
if (mimetype) {
@@ -1200,23 +1201,22 @@ export class CoreWSProvider {
12001201
}
12011202

12021203
let response: NativeHttpResponse;
1203-
let redirectUrl: string | undefined;
1204+
let redirectUrl: string | null = null;
12041205
let maxRedirects = 5;
12051206
do {
12061207
try {
12071208
response = await NativeHttp.sendRequest(redirectUrl ?? url, options);
1208-
redirectUrl = undefined;
1209+
redirectUrl = null;
12091210
} catch (error) {
12101211
// Error is a response object.
12111212
response = error as NativeHttpResponse;
12121213

1213-
// For some errors, the response doesn't contain headers. Make sure it always exists, even if it's empty.
1214-
response.headers = response.headers || {};
1214+
const headers = new HttpHeaders(response.headers); // Convert to HttpHeaders because names are normalised.
12151215

12161216
// Redirections should have been handled by the platform,
12171217
// but Android does not follow redirections between HTTP and HTTPS.
12181218
// See: https://developer.android.com/reference/java/net/HttpURLConnection#response-handling
1219-
redirectUrl = response.headers['location'] ?? response.headers['Location'];
1219+
redirectUrl = headers.get('Location');
12201220
maxRedirects--;
12211221
if (!redirectUrl || maxRedirects < 0) {
12221222
throw error;

0 commit comments

Comments
 (0)