Skip to content

Commit 51bc3a0

Browse files
committed
🐛 Fail fast on malformed 1xx+END_STREAM; disclose trailer drop
Address review on cfug#2601: - 1xx HEADERS frame with END_STREAM terminates the stream without a final response (RFC 9110 §15.2, RFC 9113 §8.4). Previously the request hung until receiveTimeout on a pooled connection; now it completes with DioException.connectionError, matching the existing precedent in this method (redirect-without-location) and Go's x/net/http2. - Disclose in the CHANGELOG that trailer HEADERS frames (no :status, e.g. gRPC trailing metadata) are now discarded; proper support tracked in cfug#2602. - Add two tests: a trailer HEADERS frame after DATA still completes the request; a 1xx+END_STREAM response fails fast instead of hanging.
1 parent 50c0cb2 commit 51bc3a0

3 files changed

Lines changed: 96 additions & 1 deletion

File tree

‎plugins/http2_adapter/CHANGELOG.md‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ See the [Migration Guide][] for the complete breaking changes list.**
99
responses are now ignored so the request resolves with the final response
1010
instead of throwing `Bad state: Future already completed`. Response headers
1111
are also reset per HEADERS frame so interim headers no longer leak into the
12-
final response.
12+
final response. Trailer HEADERS frames (a HEADERS frame with no `:status`
13+
after the response body, e.g. gRPC trailing metadata) are consequently
14+
discarded rather than merged into the response headers; proper trailer
15+
support is tracked in https://github.com/cfug/dio/issues/2602. A malformed
16+
1xx response carrying END_STREAM now fails fast with a `connectionError`
17+
instead of hanging until `receiveTimeout`.
1318

1419
## 2.9.0
1520

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,21 @@ class Http2Adapter implements HttpClientAdapter {
234234
if (status != null) {
235235
final code = int.parse(status);
236236
if (code >= 100 && code < 200) {
237+
// Interim (1xx) responses precede a final response and never
238+
// terminate the stream (RFC 9110 §15.2, RFC 9113 §8.4). A 1xx
239+
// HEADERS frame carrying END_STREAM is therefore malformed: the
240+
// stream ends without a final response. Fail fast instead of
241+
// waiting forever on a pooled connection (matching the behavior
242+
// of Go's `x/net/http2`).
243+
if (message.endStream && !responseCompleter.isCompleted) {
244+
responseCompleter.completeError(
245+
DioException.connectionError(
246+
requestOptions: options,
247+
reason: 'Received an interim 1xx response with END_STREAM; '
248+
'the stream ended without a final response.',
249+
),
250+
);
251+
}
237252
return;
238253
}
239254
statusCode = code;

‎plugins/http2_adapter/test/early_hints_test.dart‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,81 @@ void main() {
4141
expect(response.headers.value('link'), isNull);
4242
expect(response.headers.value('content-type'), 'text/plain');
4343
});
44+
45+
test(
46+
'completes normally when the server sends a trailer HEADERS frame '
47+
'after DATA', () async {
48+
// A trailer section is a HEADERS frame without `:status`, sent after the
49+
// response body with END_STREAM (RFC 9113 §8.4, §8.8.5). Trailers are
50+
// currently discarded (proper support tracked in
51+
// https://github.com/cfug/dio/issues/2602); this test pins that the
52+
// request still resolves normally instead of hanging or throwing.
53+
final fixture = await _H2Fixture.serve((stream) {
54+
stream.sendHeaders(
55+
[
56+
Header.ascii(':status', '200'),
57+
Header.ascii('content-type', 'text/plain'),
58+
],
59+
endStream: false,
60+
);
61+
stream.sendData('hello'.codeUnits, endStream: false);
62+
// Trailing metadata (e.g. gRPC `grpc-status`) — no `:status`.
63+
stream.sendHeaders(
64+
[Header.ascii('grpc-status', '0')],
65+
endStream: true,
66+
);
67+
});
68+
addTearDown(fixture.close);
69+
70+
final dio = Dio()
71+
..httpClientAdapter = Http2Adapter(fixture.connectionManager);
72+
73+
final response = await dio.get<String>(
74+
'http://127.0.0.1/',
75+
options: Options(responseType: ResponseType.plain),
76+
);
77+
78+
expect(response.statusCode, 200);
79+
expect(response.data, 'hello');
80+
expect(response.headers.value('content-type'), 'text/plain');
81+
// Trailers are dropped for now.
82+
expect(response.headers.value('grpc-status'), isNull);
83+
});
84+
85+
test(
86+
'fails fast when a 1xx interim response carries END_STREAM '
87+
'instead of hanging', () async {
88+
// A 1xx HEADERS frame with END_STREAM terminates the stream without a
89+
// final response — malformed per RFC 9110 §15.2 and RFC 9113 §8.4. With
90+
// `receiveTimeout` unset (the default) the request would otherwise wait
91+
// forever on a pooled connection, so it should error out promptly.
92+
final fixture = await _H2Fixture.serve((stream) {
93+
stream.sendHeaders(
94+
[Header.ascii(':status', '100')],
95+
endStream: true,
96+
);
97+
});
98+
addTearDown(fixture.close);
99+
100+
final dio = Dio()
101+
..httpClientAdapter = Http2Adapter(fixture.connectionManager);
102+
103+
await expectLater(
104+
dio.get<String>(
105+
'http://127.0.0.1/',
106+
options: Options(responseType: ResponseType.plain),
107+
),
108+
throwsA(
109+
isA<DioException>()
110+
.having((e) => e.type, 'type', DioExceptionType.connectionError)
111+
.having(
112+
(e) => e.message,
113+
'message',
114+
contains('interim 1xx response with END_STREAM'),
115+
),
116+
),
117+
);
118+
});
44119
}
45120

46121
class _H2Fixture {

0 commit comments

Comments
 (0)