Skip to content

Commit ccefa7c

Browse files
authored
Support BaseResponseWithUrl in package:cupertino_http and package:cronet_http (#1110)
1 parent e7a8e25 commit ccefa7c

File tree

12 files changed

+91
-31
lines changed

12 files changed

+91
-31
lines changed

.github/workflows/cupertino.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ jobs:
5757
matrix:
5858
# Test on the minimum supported flutter version and the latest
5959
# version.
60-
flutter-version: ["3.10.0", "any"]
60+
flutter-version: ["3.16.0", "any"]
6161
runs-on: macos-latest
6262
defaults:
6363
run:

.github/workflows/dart.yml

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkgs/cronet_http/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
## 1.0.1-wip
1+
## 1.1.0
22

33
* Use `package:http_image_provider` in the example application.
44
* Support Android API 21+.
5+
* Support `BaseResponseWithUrl`.
56

67
## 1.0.0
78

pkgs/cronet_http/example/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Demonstrates how to use the cronet_http plugin.
44
publish_to: 'none'
55

66
environment:
7-
sdk: ^3.0.0
7+
sdk: ^3.2.0
88

99
dependencies:
1010
cronet_http:

pkgs/cronet_http/lib/src/cronet_client.dart

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ import 'jni/jni_bindings.dart' as jb;
2323
final _digitRegex = RegExp(r'^\d+$');
2424
const _bufferSize = 10 * 1024; // The size of the Cronet read buffer.
2525

26+
/// This class can be removed when `package:http` v2 is released.
27+
class _StreamedResponseWithUrl extends StreamedResponse
28+
implements BaseResponseWithUrl {
29+
@override
30+
final Uri url;
31+
32+
_StreamedResponseWithUrl(super.stream, super.statusCode,
33+
{required this.url,
34+
super.contentLength,
35+
super.request,
36+
super.headers,
37+
super.isRedirect,
38+
super.reasonPhrase});
39+
}
40+
2641
/// The type of caching to use when making HTTP requests.
2742
enum CacheMode {
2843
disabled,
@@ -163,9 +178,11 @@ jb.UrlRequestCallbackProxy_UrlRequestCallbackInterface _urlRequestCallbacks(
163178
case final contentLengthHeader?:
164179
contentLength = int.parse(contentLengthHeader);
165180
}
166-
responseCompleter.complete(StreamedResponse(
181+
responseCompleter.complete(_StreamedResponseWithUrl(
167182
responseStream!.stream,
168183
responseInfo.getHttpStatusCode(),
184+
url: Uri.parse(
185+
responseInfo.getUrl().toDartString(releaseOriginal: true)),
169186
contentLength: contentLength,
170187
reasonPhrase: responseInfo
171188
.getHttpStatusText()

pkgs/cronet_http/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: cronet_http
2-
version: 1.0.1-wip
2+
version: 1.1.0
33
description: >-
44
An Android Flutter plugin that provides access to the Cronet HTTP client.
55
repository: https://github.com/dart-lang/http/tree/master/pkgs/cronet_http
@@ -11,7 +11,7 @@ environment:
1111
dependencies:
1212
flutter:
1313
sdk: flutter
14-
http: '>=0.13.4 <2.0.0'
14+
http: ^1.2.0
1515
jni: ^0.7.2
1616

1717
dev_dependencies:

pkgs/cupertino_http/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
## 1.2.1-wip
1+
## 1.3.0
22

33
* Use `package:http_image_provider` in the example application.
4+
* Support `BaseResponseWithUrl`.
45

56
## 1.2.0
67

pkgs/cupertino_http/example/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ publish_to: 'none'
66
version: 1.0.0+1
77

88
environment:
9-
sdk: ^3.0.0
10-
flutter: '>=3.10.0'
9+
sdk: ^3.2.0
10+
flutter: ^3.16.0
1111

1212
dependencies:
1313
cupertino_http:

pkgs/cupertino_http/lib/src/cupertino_client.dart

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,27 @@ import 'cupertino_api.dart';
1717

1818
final _digitRegex = RegExp(r'^\d+$');
1919

20+
/// This class can be removed when `package:http` v2 is released.
21+
class _StreamedResponseWithUrl extends StreamedResponse
22+
implements BaseResponseWithUrl {
23+
@override
24+
final Uri url;
25+
26+
_StreamedResponseWithUrl(super.stream, super.statusCode,
27+
{required this.url,
28+
super.contentLength,
29+
super.request,
30+
super.headers,
31+
super.isRedirect,
32+
super.reasonPhrase});
33+
}
34+
2035
class _TaskTracker {
2136
final responseCompleter = Completer<URLResponse>();
2237
final BaseRequest request;
2338
final responseController = StreamController<Uint8List>();
2439
int numRedirects = 0;
40+
Uri? lastUrl; // The last URL redirected to.
2541

2642
_TaskTracker(this.request);
2743

@@ -180,6 +196,7 @@ class CupertinoClient extends BaseClient {
180196
++taskTracker.numRedirects;
181197
if (taskTracker.request.followRedirects &&
182198
taskTracker.numRedirects <= taskTracker.request.maxRedirects) {
199+
taskTracker.lastUrl = request.url;
183200
return request;
184201
}
185202
return null;
@@ -292,9 +309,10 @@ class CupertinoClient extends BaseClient {
292309
);
293310
}
294311

295-
return StreamedResponse(
312+
return _StreamedResponseWithUrl(
296313
taskTracker.responseController.stream,
297314
response.statusCode,
315+
url: taskTracker.lastUrl ?? request.url,
298316
contentLength: response.expectedContentLength == -1
299317
? null
300318
: response.expectedContentLength,

pkgs/cupertino_http/pubspec.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
name: cupertino_http
2-
version: 1.2.1-wip
2+
version: 1.3.0
33
description: >-
44
A macOS/iOS Flutter plugin that provides access to the Foundation URL
55
Loading System.
66
repository: https://github.com/dart-lang/http/tree/master/pkgs/cupertino_http
77

88
environment:
9-
sdk: ^3.0.0
10-
flutter: '>=3.10.0' # If changed, update test matrix.
9+
sdk: ^3.2.0
10+
flutter: ^3.16.0 # If changed, update test matrix.
1111

1212
dependencies:
1313
async: ^2.5.0
1414
ffi: ^2.1.0
1515
flutter:
1616
sdk: flutter
17-
http: '>=0.13.4 <2.0.0'
17+
http: ^1.2.0
1818

1919
dev_dependencies:
2020
dart_flutter_team_lints: ^2.0.0

pkgs/http_client_conformance_tests/lib/src/redirect_tests.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,26 @@ void testRedirect(Client client, {bool redirectAlwaysAllowed = false}) async {
2727
});
2828
tearDownAll(() => httpServerChannel.sink.add(null));
2929

30+
test('no redirect', () async {
31+
final request = Request('GET', Uri.http(host, '/'))
32+
..followRedirects = false;
33+
final response = await client.send(request);
34+
expect(response.statusCode, 200);
35+
expect(response.isRedirect, false);
36+
if (response case BaseResponseWithUrl(url: final url)) {
37+
expect(url, Uri.http(host, '/'));
38+
}
39+
});
40+
3041
test('disallow redirect', () async {
3142
final request = Request('GET', Uri.http(host, '/1'))
3243
..followRedirects = false;
3344
final response = await client.send(request);
3445
expect(response.statusCode, 302);
3546
expect(response.isRedirect, true);
47+
if (response case BaseResponseWithUrl(url: final url)) {
48+
expect(url, Uri.http(host, '/1'));
49+
}
3650
}, skip: redirectAlwaysAllowed ? 'redirects always allowed' : false);
3751

3852
test('disallow redirect, 0 maxRedirects', () async {
@@ -42,6 +56,9 @@ void testRedirect(Client client, {bool redirectAlwaysAllowed = false}) async {
4256
final response = await client.send(request);
4357
expect(response.statusCode, 302);
4458
expect(response.isRedirect, true);
59+
if (response case BaseResponseWithUrl(url: final url)) {
60+
expect(url, Uri.http(host, '/1'));
61+
}
4562
}, skip: redirectAlwaysAllowed ? 'redirects always allowed' : false);
4663

4764
test('allow redirect', () async {
@@ -50,6 +67,9 @@ void testRedirect(Client client, {bool redirectAlwaysAllowed = false}) async {
5067
final response = await client.send(request);
5168
expect(response.statusCode, 200);
5269
expect(response.isRedirect, false);
70+
if (response case BaseResponseWithUrl(url: final url)) {
71+
expect(url, Uri.http(host, '/'));
72+
}
5373
});
5474

5575
test('allow redirect, 0 maxRedirects', () async {
@@ -69,6 +89,9 @@ void testRedirect(Client client, {bool redirectAlwaysAllowed = false}) async {
6989
final response = await client.send(request);
7090
expect(response.statusCode, 200);
7191
expect(response.isRedirect, false);
92+
if (response case BaseResponseWithUrl(url: final url)) {
93+
expect(url, Uri.http(host, '/'));
94+
}
7295
}, skip: redirectAlwaysAllowed ? 'redirects always allowed' : false);
7396

7497
test('too many redirects', () async {

pkgs/http_client_conformance_tests/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ repository: https://github.com/dart-lang/http/tree/master/pkgs/http_client_confo
77
publish_to: none
88

99
environment:
10-
sdk: ^3.0.0
10+
sdk: ^3.2.0
1111

1212
dependencies:
1313
async: ^2.8.2
1414
dart_style: ^2.2.3
15-
http: ^1.0.0
15+
http: ^1.2.0
1616
stream_channel: ^2.1.1
1717
test: ^1.21.2
1818

0 commit comments

Comments
 (0)