mirror of
				https://github.com/ImranR98/Obtainium.git
				synced 2025-10-30 21:13:28 +01:00 
			
		
		
		
	Ensure headers are still sent when URL request is redirected (#1973)
This commit is contained in:
		| @@ -3,12 +3,12 @@ | |||||||
|  |  | ||||||
| import 'dart:convert'; | import 'dart:convert'; | ||||||
| import 'dart:io'; | import 'dart:io'; | ||||||
|  | import 'package:http/http.dart' as http; | ||||||
|  |  | ||||||
| import 'package:device_info_plus/device_info_plus.dart'; | import 'package:device_info_plus/device_info_plus.dart'; | ||||||
| import 'package:easy_localization/easy_localization.dart'; | import 'package:easy_localization/easy_localization.dart'; | ||||||
| import 'package:html/dom.dart'; | import 'package:html/dom.dart'; | ||||||
| import 'package:http/http.dart'; | import 'package:http/http.dart'; | ||||||
| import 'package:http/io_client.dart'; |  | ||||||
| import 'package:obtainium/app_sources/apkmirror.dart'; | import 'package:obtainium/app_sources/apkmirror.dart'; | ||||||
| import 'package:obtainium/app_sources/apkpure.dart'; | import 'package:obtainium/app_sources/apkpure.dart'; | ||||||
| import 'package:obtainium/app_sources/aptoide.dart'; | import 'package:obtainium/app_sources/aptoide.dart'; | ||||||
| @@ -566,23 +566,59 @@ abstract class AppSource { | |||||||
|       String url, Map<String, dynamic> additionalSettings, |       String url, Map<String, dynamic> additionalSettings, | ||||||
|       {bool followRedirects = true, Object? postBody}) async { |       {bool followRedirects = true, Object? postBody}) async { | ||||||
|     var requestHeaders = await getRequestHeaders(additionalSettings); |     var requestHeaders = await getRequestHeaders(additionalSettings); | ||||||
|  |  | ||||||
|     if (requestHeaders != null || followRedirects == false) { |     if (requestHeaders != null || followRedirects == false) { | ||||||
|       var req = Request(postBody == null ? 'GET' : 'POST', Uri.parse(url)); |       var method = postBody == null ? 'GET' : 'POST'; | ||||||
|       req.followRedirects = followRedirects; |       var currentUrl = url; | ||||||
|  |       var redirectCount = 0; | ||||||
|  |       const maxRedirects = 10; | ||||||
|  |       while (redirectCount < maxRedirects) { | ||||||
|  |         var httpClient = | ||||||
|  |             createHttpClient(additionalSettings['allowInsecure'] == true); | ||||||
|  |         var request = await httpClient.openUrl(method, Uri.parse(currentUrl)); | ||||||
|         if (requestHeaders != null) { |         if (requestHeaders != null) { | ||||||
|         req.headers.addAll(requestHeaders); |           requestHeaders.forEach((key, value) { | ||||||
|  |             request.headers.set(key, value); | ||||||
|  |           }); | ||||||
|         } |         } | ||||||
|  |         request.followRedirects = false; | ||||||
|         if (postBody != null) { |         if (postBody != null) { | ||||||
|         req.headers[HttpHeaders.contentTypeHeader] = 'application/json'; |           request.headers.contentType = ContentType.json; | ||||||
|         req.body = jsonEncode(postBody); |           request.write(jsonEncode(postBody)); | ||||||
|         } |         } | ||||||
|       return Response.fromStream(await IOClient( |         final response = await request.close(); | ||||||
|               createHttpClient(additionalSettings['allowInsecure'] == true)) |  | ||||||
|           .send(req)); |         if (followRedirects && | ||||||
|  |             (response.statusCode == 301 || response.statusCode == 302)) { | ||||||
|  |           final location = response.headers.value(HttpHeaders.locationHeader); | ||||||
|  |           if (location != null) { | ||||||
|  |             currentUrl = location; | ||||||
|  |             redirectCount++; | ||||||
|  |             httpClient.close(); | ||||||
|  |             continue; | ||||||
|  |           } | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         final headers = <String, String>{}; | ||||||
|  |         response.headers.forEach((name, values) { | ||||||
|  |           headers[name] = values.join(', '); | ||||||
|  |         }); | ||||||
|  |  | ||||||
|  |         final body = await response.transform(utf8.decoder).join(); | ||||||
|  |         httpClient.close(); | ||||||
|  |  | ||||||
|  |         return http.Response( | ||||||
|  |           body, | ||||||
|  |           response.statusCode, | ||||||
|  |           headers: headers, | ||||||
|  |           request: http.Request(method, Uri.parse(url)), | ||||||
|  |         ); | ||||||
|  |       } | ||||||
|  |       throw ObtainiumError('Too many redirects ($maxRedirects)'); | ||||||
|     } else { |     } else { | ||||||
|       return postBody == null |       return postBody == null | ||||||
|           ? get(Uri.parse(url)) |           ? http.get(Uri.parse(url)) | ||||||
|           : post(Uri.parse(url), body: jsonEncode(postBody)); |           : http.post(Uri.parse(url), body: jsonEncode(postBody)); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user