From ca5230e04764582e5cbb7a8b93bcdbf499345f0a Mon Sep 17 00:00:00 2001 From: Brian Quinlan Date: Tue, 16 Jan 2024 15:41:37 -0800 Subject: [PATCH] Update --- pkgs/cronet_http/CHANGELOG.md | 3 ++- pkgs/cronet_http/lib/src/cronet_client.dart | 19 +++++++++++++++++- pkgs/cronet_http/pubspec.yaml | 4 ++-- pkgs/cupertino_http/CHANGELOG.md | 3 ++- .../lib/src/cupertino_client.dart | 20 ++++++++++++++++++- pkgs/cupertino_http/pubspec.yaml | 4 ++-- .../lib/src/redirect_tests.dart | 20 +++++++++---------- 7 files changed, 55 insertions(+), 18 deletions(-) diff --git a/pkgs/cronet_http/CHANGELOG.md b/pkgs/cronet_http/CHANGELOG.md index fa461d5284..2f7c6d81bd 100644 --- a/pkgs/cronet_http/CHANGELOG.md +++ b/pkgs/cronet_http/CHANGELOG.md @@ -1,7 +1,8 @@ -## 1.0.1-wip +## 1.1.0-wip * Use `package:http_image_provider` in the example application. * Support Android API 21+. +* Support `BaseResponseWithUrl`. ## 1.0.0 diff --git a/pkgs/cronet_http/lib/src/cronet_client.dart b/pkgs/cronet_http/lib/src/cronet_client.dart index 272b602b3d..520d7c1975 100644 --- a/pkgs/cronet_http/lib/src/cronet_client.dart +++ b/pkgs/cronet_http/lib/src/cronet_client.dart @@ -23,6 +23,21 @@ import 'jni/jni_bindings.dart' as jb; final _digitRegex = RegExp(r'^\d+$'); const _bufferSize = 10 * 1024; // The size of the Cronet read buffer. +/// This class can be removed when `package:http` v2 is released. +class _StreamedResponseV2 extends StreamedResponse + implements BaseResponseWithUrl { + @override + final Uri url; + + _StreamedResponseV2(super.stream, super.statusCode, + {required this.url, + super.contentLength, + super.request, + super.headers, + super.isRedirect, + super.reasonPhrase}); +} + /// The type of caching to use when making HTTP requests. enum CacheMode { disabled, @@ -163,9 +178,11 @@ jb.UrlRequestCallbackProxy_UrlRequestCallbackInterface _urlRequestCallbacks( case final contentLengthHeader?: contentLength = int.parse(contentLengthHeader); } - responseCompleter.complete(StreamedResponse( + responseCompleter.complete(_StreamedResponseV2( responseStream!.stream, responseInfo.getHttpStatusCode(), + url: Uri.parse( + responseInfo.getUrl().toDartString(releaseOriginal: true)), contentLength: contentLength, reasonPhrase: responseInfo .getHttpStatusText() diff --git a/pkgs/cronet_http/pubspec.yaml b/pkgs/cronet_http/pubspec.yaml index c9e0e4d0a0..fcf18c5ddf 100644 --- a/pkgs/cronet_http/pubspec.yaml +++ b/pkgs/cronet_http/pubspec.yaml @@ -1,5 +1,5 @@ name: cronet_http -version: 1.0.1-wip +version: 1.1.0-wip description: >- An Android Flutter plugin that provides access to the Cronet HTTP client. repository: https://github.com/dart-lang/http/tree/master/pkgs/cronet_http @@ -11,7 +11,7 @@ environment: dependencies: flutter: sdk: flutter - http: '>=0.13.4 <2.0.0' + http: ^1.2.0 jni: ^0.7.2 dev_dependencies: diff --git a/pkgs/cupertino_http/CHANGELOG.md b/pkgs/cupertino_http/CHANGELOG.md index 395bb8b3b7..d077593629 100644 --- a/pkgs/cupertino_http/CHANGELOG.md +++ b/pkgs/cupertino_http/CHANGELOG.md @@ -1,6 +1,7 @@ -## 1.2.1-wip +## 1.3.0 * Use `package:http_image_provider` in the example application. +* Support `BaseResponseWithUrl`. ## 1.2.0 diff --git a/pkgs/cupertino_http/lib/src/cupertino_client.dart b/pkgs/cupertino_http/lib/src/cupertino_client.dart index a4c7715de5..7025958c12 100644 --- a/pkgs/cupertino_http/lib/src/cupertino_client.dart +++ b/pkgs/cupertino_http/lib/src/cupertino_client.dart @@ -17,11 +17,27 @@ import 'cupertino_api.dart'; final _digitRegex = RegExp(r'^\d+$'); +/// This class can be removed when `package:http` v2 is released. +class _StreamedResponseV2 extends StreamedResponse + implements BaseResponseWithUrl { + @override + final Uri url; + + _StreamedResponseV2(super.stream, super.statusCode, + {required this.url, + super.contentLength, + super.request, + super.headers, + super.isRedirect, + super.reasonPhrase}); +} + class _TaskTracker { final responseCompleter = Completer(); final BaseRequest request; final responseController = StreamController(); int numRedirects = 0; + Uri? lastUrl; // The last URL redirected to. _TaskTracker(this.request); @@ -180,6 +196,7 @@ class CupertinoClient extends BaseClient { ++taskTracker.numRedirects; if (taskTracker.request.followRedirects && taskTracker.numRedirects <= taskTracker.request.maxRedirects) { + taskTracker.lastUrl = request.url; return request; } return null; @@ -292,9 +309,10 @@ class CupertinoClient extends BaseClient { ); } - return StreamedResponse( + return _StreamedResponseV2( taskTracker.responseController.stream, response.statusCode, + url: taskTracker.lastUrl ?? request.url, contentLength: response.expectedContentLength == -1 ? null : response.expectedContentLength, diff --git a/pkgs/cupertino_http/pubspec.yaml b/pkgs/cupertino_http/pubspec.yaml index e9a2bbfdb2..d22edfcec1 100644 --- a/pkgs/cupertino_http/pubspec.yaml +++ b/pkgs/cupertino_http/pubspec.yaml @@ -1,5 +1,5 @@ name: cupertino_http -version: 1.2.1-wip +version: 1.3.0-wip description: >- A macOS/iOS Flutter plugin that provides access to the Foundation URL Loading System. @@ -14,7 +14,7 @@ dependencies: ffi: ^2.1.0 flutter: sdk: flutter - http: '>=0.13.4 <2.0.0' + http: ^1.2.0 dev_dependencies: dart_flutter_team_lints: ^2.0.0 diff --git a/pkgs/http_client_conformance_tests/lib/src/redirect_tests.dart b/pkgs/http_client_conformance_tests/lib/src/redirect_tests.dart index 0d6a0a1129..a33d077705 100644 --- a/pkgs/http_client_conformance_tests/lib/src/redirect_tests.dart +++ b/pkgs/http_client_conformance_tests/lib/src/redirect_tests.dart @@ -33,8 +33,8 @@ void testRedirect(Client client, {bool redirectAlwaysAllowed = false}) async { final response = await client.send(request); expect(response.statusCode, 200); expect(response.isRedirect, false); - if (response is BaseResponseV2) { - expect((response as BaseResponseV2).url, Uri.http(host, '/')); + if (response case BaseResponseWithUrl(url: final url)) { + expect(url, Uri.http(host, '/')); } }); @@ -44,8 +44,8 @@ void testRedirect(Client client, {bool redirectAlwaysAllowed = false}) async { final response = await client.send(request); expect(response.statusCode, 302); expect(response.isRedirect, true); - if (response is BaseResponseV2) { - expect((response as BaseResponseV2).url, Uri.http(host, '/1')); + if (response case BaseResponseWithUrl(url: final url)) { + expect(url, Uri.http(host, '/1')); } }, skip: redirectAlwaysAllowed ? 'redirects always allowed' : false); @@ -56,8 +56,8 @@ void testRedirect(Client client, {bool redirectAlwaysAllowed = false}) async { final response = await client.send(request); expect(response.statusCode, 302); expect(response.isRedirect, true); - if (response is BaseResponseV2) { - expect((response as BaseResponseV2).url, Uri.http(host, '/1')); + if (response case BaseResponseWithUrl(url: final url)) { + expect(url, Uri.http(host, '/1')); } }, skip: redirectAlwaysAllowed ? 'redirects always allowed' : false); @@ -67,8 +67,8 @@ void testRedirect(Client client, {bool redirectAlwaysAllowed = false}) async { final response = await client.send(request); expect(response.statusCode, 200); expect(response.isRedirect, false); - if (response is BaseResponseV2) { - expect((response as BaseResponseV2).url, Uri.http(host, '/')); + if (response case BaseResponseWithUrl(url: final url)) { + expect(url, Uri.http(host, '/')); } }); @@ -89,8 +89,8 @@ void testRedirect(Client client, {bool redirectAlwaysAllowed = false}) async { final response = await client.send(request); expect(response.statusCode, 200); expect(response.isRedirect, false); - if (response is BaseResponseV2) { - expect((response as BaseResponseV2).url, Uri.http(host, '/')); + if (response case BaseResponseWithUrl(url: final url)) { + expect(url, Uri.http(host, '/')); } }, skip: redirectAlwaysAllowed ? 'redirects always allowed' : false);