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

MOBILE-4024 ws: Follow redirects not handled by Android #4228

Merged
merged 1 commit into from
Nov 8, 2024
Merged
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
68 changes: 49 additions & 19 deletions src/core/services/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -255,26 +256,33 @@ export class CoreWSProvider {
// Create the tmp file as an empty file.
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) {
transfer.onprogress = onProgress;
}
// Download the file in the tmp file.
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.
const fileDownloaded = await new Promise<{
entry: globalThis.FileEntry;
headers: Record<string, string> | undefined;
}>((resolve, reject) => {
transfer.download(
url,
CoreFile.getFileEntryURL(fileEntry),
(result) => resolve(result),
(error: FileTransferError) => reject(error),
true,
{ headers: { 'User-Agent': navigator.userAgent } },
);
});
// 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 = '';

Expand Down Expand Up @@ -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>>;
Expand Down