Merge pull request #4228 from albertgasset/MOBILE-4024

MOBILE-4024 ws: Follow redirects not handled by Android
main
Dani Palou 2024-11-08 08:38:45 +01:00 committed by GitHub
commit 9a970da466
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 49 additions and 19 deletions

View File

@ -16,6 +16,7 @@ import { Injectable } from '@angular/core';
import { HttpResponse, HttpParams, HttpErrorResponse } from '@angular/common/http';
import { FileEntry } from '@awesome-cordova-plugins/file/ngx';
import { HTTPResponse as NativeHttpResponse } from '@awesome-cordova-plugins/http';
import { Md5 } from 'ts-md5/dist/md5';
import { Observable, firstValueFrom } from 'rxjs';
import { timeout } from 'rxjs/operators';
@ -255,19 +256,19 @@ export class CoreWSProvider {
// Create the tmp file as an empty file.
const fileEntry = await CoreFile.createFile(tmpPath);
let fileDownloaded: { entry: globalThis.FileEntry; headers: Record<string, string> | undefined};
let redirectUrl: string | undefined;
let maxRedirects = 5;
do {
const transfer = new window.FileTransfer();
if (onProgress) {
transfer.onprogress = onProgress;
}
// Download the file in the tmp file.
const fileDownloaded = await new Promise<{
entry: globalThis.FileEntry;
headers: Record<string, string> | undefined;
}>((resolve, reject) => {
fileDownloaded = await new Promise((resolve, reject) => {
transfer.download(
url,
redirectUrl ?? url,
CoreFile.getFileEntryURL(fileEntry),
(result) => resolve(result),
(error: FileTransferError) => reject(error),
@ -276,6 +277,13 @@ export class CoreWSProvider {
);
});
// Redirections should have been handled by the platform,
// but Android does not follow redirections between HTTP and HTTPS.
// See: https://developer.android.com/reference/java/net/HttpURLConnection#response-handling
redirectUrl = fileDownloaded.headers?.['location'];
maxRedirects--;
} while (redirectUrl && maxRedirects >= 0);
let extension = '';
if (addExtension) {
@ -1185,7 +1193,29 @@ export class CoreWSProvider {
});
}
return NativeHttp.sendRequest(url, options).then((response) => new CoreNativeToAngularHttpResponse(response));
let response: NativeHttpResponse;
let redirectUrl: string | undefined;
let maxRedirects = 5;
do {
try {
response = await NativeHttp.sendRequest(redirectUrl ?? url, options);
redirectUrl = undefined;
} catch (error) {
// Error is a response object.
response = error as NativeHttpResponse;
// Redirections should have been handled by the platform,
// but Android does not follow redirections between HTTP and HTTPS.
// See: https://developer.android.com/reference/java/net/HttpURLConnection#response-handling
redirectUrl = response.headers['location'];
maxRedirects--;
if (!redirectUrl || maxRedirects < 0) {
throw error;
}
}
} while (redirectUrl);
return new CoreNativeToAngularHttpResponse(response);
} else {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let observable: Observable<HttpResponse<any>>;