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 { HttpResponse, HttpParams, HttpErrorResponse } from '@angular/common/http';
import { FileEntry } from '@awesome-cordova-plugins/file/ngx'; 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 { Md5 } from 'ts-md5/dist/md5';
import { Observable, firstValueFrom } from 'rxjs'; import { Observable, firstValueFrom } from 'rxjs';
import { timeout } from 'rxjs/operators'; import { timeout } from 'rxjs/operators';
@ -255,26 +256,33 @@ export class CoreWSProvider {
// Create the tmp file as an empty file. // Create the tmp file as an empty file.
const fileEntry = await CoreFile.createFile(tmpPath); const fileEntry = await CoreFile.createFile(tmpPath);
const transfer = new window.FileTransfer(); 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;
}
if (onProgress) { // Download the file in the tmp file.
transfer.onprogress = onProgress; fileDownloaded = await new Promise((resolve, reject) => {
} transfer.download(
redirectUrl ?? url,
CoreFile.getFileEntryURL(fileEntry),
(result) => resolve(result),
(error: FileTransferError) => reject(error),
true,
{ headers: { 'User-Agent': navigator.userAgent } },
);
});
// Download the file in the tmp file. // Redirections should have been handled by the platform,
const fileDownloaded = await new Promise<{ // but Android does not follow redirections between HTTP and HTTPS.
entry: globalThis.FileEntry; // See: https://developer.android.com/reference/java/net/HttpURLConnection#response-handling
headers: Record<string, string> | undefined; redirectUrl = fileDownloaded.headers?.['location'];
}>((resolve, reject) => { maxRedirects--;
transfer.download( } while (redirectUrl && maxRedirects >= 0);
url,
CoreFile.getFileEntryURL(fileEntry),
(result) => resolve(result),
(error: FileTransferError) => reject(error),
true,
{ headers: { 'User-Agent': navigator.userAgent } },
);
});
let extension = ''; let extension = '';
@ -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 { } else {
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
let observable: Observable<HttpResponse<any>>; let observable: Observable<HttpResponse<any>>;