Merge pull request #4228 from albertgasset/MOBILE-4024
MOBILE-4024 ws: Follow redirects not handled by Android
This commit is contained in:
		
						commit
						9a970da466
					
				| @ -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>>; | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user