Skip to content

Commit 50c0cb2

Browse files
chiliecclaude
andcommitted
🐛 Ignore 1xx interim responses in http2_adapter
A `103 Early Hints` (or any 1xx) HEADERS frame was treated as the final response: the adapter parsed its `:status`, completed the response `Completer`, and then completed the same `Completer` again when the real final HEADERS frame arrived, crashing the isolate with `Bad state: Future already completed`. Interim headers also leaked into the final response because `responseHeaders` was never reset between frames. Skip 1xx responses instead of completing on them, and reset the accumulated headers per HEADERS frame so interim headers do not carry over into the final response. Closes #2600 Co-Authored-By: Claude <[email protected]>
1 parent 4684e29 commit 50c0cb2

3 files changed

Lines changed: 163 additions & 5 deletions

File tree

‎plugins/http2_adapter/CHANGELOG.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ See the [Migration Guide][] for the complete breaking changes list.**
55

66
## Unreleased
77

8-
*None.*
8+
- Fix crash on 1xx informational responses (e.g. `103 Early Hints`): interim
9+
responses are now ignored so the request resolves with the final response
10+
instead of throwing `Bad state: Future already completed`. Response headers
11+
are also reset per HEADERS frame so interim headers no longer leak into the
12+
final response.
913

1014
## 2.9.0
1115

‎plugins/http2_adapter/lib/src/http2_adapter.dart‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,16 +219,28 @@ class Http2Adapter implements HttpClientAdapter {
219219
responseSubscription = stream.incomingMessages.listen(
220220
(StreamMessage message) async {
221221
if (message is HeadersStreamMessage) {
222+
final frameHeaders = <MapEntry<String, String>>[];
223+
String? status;
222224
for (final header in message.headers) {
223225
final name = utf8.decode(header.name);
224226
final value = utf8.decode(header.value);
225-
responseHeaders.add(name, value);
227+
if (name == ':status') {
228+
status = value;
229+
} else {
230+
frameHeaders.add(MapEntry(name, value));
231+
}
226232
}
227233

228-
final status = responseHeaders.value(':status');
229234
if (status != null) {
230-
statusCode = int.parse(status);
231-
responseHeaders.removeAll(':status');
235+
final code = int.parse(status);
236+
if (code >= 100 && code < 200) {
237+
return;
238+
}
239+
statusCode = code;
240+
responseHeaders.clear();
241+
for (final entry in frameHeaders) {
242+
responseHeaders.add(entry.key, entry.value);
243+
}
232244
needRedirect = _needRedirect(options, statusCode);
233245
needResponse =
234246
!needRedirect && options.validateStatus(statusCode) ||
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import 'dart:async';
2+
import 'dart:io';
3+
4+
import 'package:dio/dio.dart';
5+
import 'package:dio_http2_adapter/dio_http2_adapter.dart';
6+
import 'package:http2/transport.dart';
7+
import 'package:test/test.dart';
8+
9+
void main() {
10+
test('ignores 1xx interim responses and resolves with the final response',
11+
() async {
12+
final fixture = await _H2Fixture.serve((stream) {
13+
stream.sendHeaders(
14+
[
15+
Header.ascii(':status', '103'),
16+
Header.ascii('link', '</s.css>; rel=preload; as=style'),
17+
],
18+
endStream: false,
19+
);
20+
stream.sendHeaders(
21+
[
22+
Header.ascii(':status', '200'),
23+
Header.ascii('content-type', 'text/plain'),
24+
],
25+
endStream: false,
26+
);
27+
stream.sendData('hello'.codeUnits, endStream: true);
28+
});
29+
addTearDown(fixture.close);
30+
31+
final dio = Dio()
32+
..httpClientAdapter = Http2Adapter(fixture.connectionManager);
33+
34+
final response = await dio.get<String>(
35+
'http://127.0.0.1/',
36+
options: Options(responseType: ResponseType.plain),
37+
);
38+
39+
expect(response.statusCode, 200);
40+
expect(response.data, 'hello');
41+
expect(response.headers.value('link'), isNull);
42+
expect(response.headers.value('content-type'), 'text/plain');
43+
});
44+
}
45+
46+
class _H2Fixture {
47+
_H2Fixture._(
48+
this._server,
49+
this._serverSubscription,
50+
this._connectionManager,
51+
this._serverConnections,
52+
this._subscriptions,
53+
);
54+
55+
final ServerSocket _server;
56+
final StreamSubscription<Socket> _serverSubscription;
57+
final _TestConnectionManager _connectionManager;
58+
final List<ServerTransportConnection> _serverConnections;
59+
final List<StreamSubscription<dynamic>> _subscriptions;
60+
61+
ConnectionManager get connectionManager => _connectionManager;
62+
63+
static Future<_H2Fixture> serve(
64+
void Function(ServerTransportStream stream) handler,
65+
) async {
66+
final server = await ServerSocket.bind(InternetAddress.loopbackIPv4, 0);
67+
final serverConnections = <ServerTransportConnection>[];
68+
final subscriptions = <StreamSubscription<dynamic>>[];
69+
late final StreamSubscription<Socket> serverSubscription;
70+
serverSubscription = server.listen((socket) {
71+
final connection = ServerTransportConnection.viaSocket(socket);
72+
serverConnections.add(connection);
73+
subscriptions.add(
74+
connection.incomingStreams.listen(
75+
(stream) {
76+
subscriptions.add(
77+
stream.incomingMessages.listen(
78+
(message) {
79+
if (message is HeadersStreamMessage) {
80+
handler(stream);
81+
}
82+
},
83+
onError: (_, __) {},
84+
),
85+
);
86+
},
87+
onError: (_, __) {},
88+
),
89+
);
90+
});
91+
92+
final socket = await Socket.connect(server.address, server.port);
93+
final clientConnection = ClientTransportConnection.viaSocket(socket);
94+
return _H2Fixture._(
95+
server,
96+
serverSubscription,
97+
_TestConnectionManager(clientConnection),
98+
serverConnections,
99+
subscriptions,
100+
);
101+
}
102+
103+
Future<void> close() async {
104+
await _serverSubscription.cancel();
105+
for (final subscription in _subscriptions) {
106+
await subscription.cancel();
107+
}
108+
try {
109+
await _connectionManager.terminate();
110+
} catch (_) {}
111+
for (final connection in _serverConnections) {
112+
try {
113+
await connection.terminate();
114+
} catch (_) {}
115+
}
116+
await _server.close();
117+
}
118+
}
119+
120+
class _TestConnectionManager implements ConnectionManager {
121+
_TestConnectionManager(this._connection);
122+
123+
final ClientTransportConnection _connection;
124+
125+
@override
126+
int get cachedConnectionsCount => 1;
127+
128+
@override
129+
Future<ClientTransportConnection> getConnection(
130+
RequestOptions options,
131+
List<RedirectRecord> redirects,
132+
) async =>
133+
_connection;
134+
135+
@override
136+
void removeConnection(ClientTransportConnection transport) {}
137+
138+
@override
139+
void close({bool force = false}) {}
140+
141+
Future<void> terminate() => _connection.terminate();
142+
}

0 commit comments

Comments
 (0)