Skip to content

Commit a202d8e

Browse files
feat(cronet_http): Add support for QUIC hints (#1873)
* feat(cronet_http): Add support for QUIC hints * Fix comment * Update version * Update cronet_configuration_test.dart * Update cronet_configuration_test.dart * Update cronet_configuration_test.dart * Fix tests * Add wip * Update pkgs/cronet_http/lib/src/cronet_client.dart Co-authored-by: Michael Goderbauer <goderbauer@google.com> --------- Co-authored-by: Michael Goderbauer <goderbauer@google.com>
1 parent 95891d4 commit a202d8e

4 files changed

Lines changed: 61 additions & 3 deletions

File tree

pkgs/cronet_http/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
## 1.7.1-wip
1+
## 1.8.0-wip
22

33
* Made callbacks asynchronous to prevent background errors caused by the
44
unavailability of the Dart callback.
55
* Change the compile SDK version to 35 for compatibility with `package:jni`.
6+
* Add support for QUIC hints.
67

78
## 1.7.0
89

pkgs/cronet_http/example/integration_test/cronet_configuration_test.dart

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,49 @@ void testUserAgent() {
121121
});
122122
}
123123

124+
void testQuicHints() {
125+
group('quicHints', () {
126+
late HttpServer server;
127+
128+
setUp(() async {
129+
server = (await HttpServer.bind('localhost', 0))
130+
..listen((request) async {
131+
await request.drain<void>();
132+
request.response.headers.set('Content-Type', 'text/plain');
133+
request.response.write('Hello World');
134+
await request.response.close();
135+
});
136+
});
137+
tearDown(() {
138+
server.close();
139+
});
140+
141+
test('quic hints', () async {
142+
final engine = CronetEngine.build(
143+
cacheMode: CacheMode.diskNoHttp,
144+
enableQuic: true,
145+
enableHttp2: false,
146+
quicHints: [
147+
('www.google.com', 443, 443),
148+
]);
149+
final response = await CronetClient.fromCronetEngine(engine)
150+
.send(Request('GET', Uri.parse('https://www.google.com/')));
151+
expect(response.negotiatedProtocol, 'h3');
152+
}, skip: 'requires internet access');
153+
154+
test('no quic hints', () async {
155+
final engine = CronetEngine.build(
156+
cacheMode: CacheMode.diskNoHttp,
157+
enableQuic: true,
158+
enableHttp2: false,
159+
);
160+
final response = await CronetClient.fromCronetEngine(engine)
161+
.send(Request('GET', Uri.parse('https://www.google.com/')));
162+
expect(response.negotiatedProtocol, 'http1.1');
163+
}, skip: 'requires internet access');
164+
});
165+
}
166+
124167
void testEngineClose() {
125168
group('engine close', () {
126169
test('multiple close', () {
@@ -155,5 +198,6 @@ void main() {
155198
testCache();
156199
testInvalidConfigurations();
157200
testUserAgent();
201+
testQuicHints();
158202
testEngineClose();
159203
}

pkgs/cronet_http/lib/src/cronet_client.dart

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ class CronetEngine {
119119
/// should be used per [CronetEngine].
120120
///
121121
/// [userAgent] controls the `User-Agent` header.
122+
///
123+
/// [quicHints] adds a list of hosts that support QUIC. Each hint is a tuple
124+
/// of (host, port, alternativePort) that indicates that the host supports
125+
/// QUIC. Note that [CacheMode.disk] or [CacheMode.diskNoHttp] is needed to
126+
/// take advantage of 0-RTT connection establishment between sessions.
122127
static CronetEngine build(
123128
{CacheMode? cacheMode,
124129
int? cacheMaxSize,
@@ -127,7 +132,8 @@ class CronetEngine {
127132
bool? enablePublicKeyPinningBypassForLocalTrustAnchors,
128133
bool? enableQuic,
129134
String? storagePath,
130-
String? userAgent}) {
135+
String? userAgent,
136+
List<(String, int, int)>? quicHints}) {
131137
try {
132138
return using((arena) {
133139
final builder = jb.CronetEngine$Builder(
@@ -173,6 +179,13 @@ class CronetEngine {
173179
?.release();
174180
}
175181

182+
if (quicHints != null) {
183+
for (final (host, port, alternativePort) in quicHints) {
184+
builder.addQuicHint(
185+
host.toJString()..releasedBy(arena), port, alternativePort);
186+
}
187+
}
188+
176189
return CronetEngine._(builder.build()!);
177190
});
178191
} on JniException catch (e) {

pkgs/cronet_http/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: cronet_http
2-
version: 1.7.1-wip
2+
version: 1.8.0-wip
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

0 commit comments

Comments
 (0)