Debian Codegraph @

test/rfc9114_SUITE.erl

raw
1 %% Copyright (c) Loïc Hoguin <[email protected]>
2 %%
3 %% Permission to use, copy, modify, and/or distribute this software for any
4 %% purpose with or without fee is hereby granted, provided that the above
5 %% copyright notice and this permission notice appear in all copies.
6 %%
7 %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13 %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15 -module(rfc9114_SUITE).
16 -compile(export_all).
17 -compile(nowarn_export_all).
18
19 -import(ct_helper, [config/2]).
20 -import(ct_helper, [doc/1]).
21
22 -ifdef(COWBOY_QUICER).
23
24 -include_lib("quicer/include/quicer.hrl").
25
26 all() ->
27 [{group, h3}].
28
29 groups() ->
30 %% @todo Enable parallel tests but for this issues in the
31 %% QUIC accept loop need to be figured out (can't connect
32 %% concurrently somehow, no backlog?).
33 [{h3, [], ct_helper:all(?MODULE)}].
34
35 init_per_group(Name = h3, Config) ->
36 cowboy_test:init_http3(Name, #{
37 env => #{dispatch => cowboy_router:compile(init_routes(Config))}
38 }, Config).
39
40 end_per_group(Name, _) ->
41 cowboy_test:stop_group(Name).
42
43 init_routes(_) -> [
44 {"localhost", [
45 {"/", hello_h, []},
46 {"/echo/:key", echo_h, []}
47 ]}
48 ].
49
50 %% Starting HTTP/3 for "https" URIs.
51
52 alpn(Config) ->
53 doc("Successful ALPN negotiation. (RFC9114 3.1)"),
54 {ok, Conn} = quicer:connect("localhost", config(port, Config),
55 #{alpn => ["h3"], verify => none}, 5000),
56 {ok, <<"h3">>} = quicer:negotiated_protocol(Conn),
57 %% To make sure the connection is fully established we wait
58 %% to receive the SETTINGS frame on the control stream.
59 {ok, _ControlRef, _Settings} = do_wait_settings(Conn),
60 ok.
61
62 alpn_error(Config) ->
63 doc("Failed ALPN negotiation using the 'h2' token. (RFC9114 3.1)"),
64 {error, transport_down, #{status := alpn_neg_failure}}
65 = quicer:connect("localhost", config(port, Config),
66 #{alpn => ["h2"], verify => none}, 5000),
67 ok.
68
69 %% @todo 3.2. Connection Establishment
70 %% After the QUIC connection is established, a SETTINGS frame MUST be sent by each endpoint as the initial frame of their respective HTTP control stream.
71
72 %% @todo 3.3. Connection Reuse
73 %% Servers are encouraged to maintain open HTTP/3 connections for as long as
74 %possible but are permitted to terminate idle connections if necessary. When
75 %either endpoint chooses to close the HTTP/3 connection, the terminating
76 %endpoint SHOULD first send a GOAWAY frame (Section 5.2) so that both endpoints
77 %can reliably determine whether previously sent frames have been processed and
78 %gracefully complete or terminate any necessary remaining tasks.
79
80 %% Frame format.
81
82 req_stream(Config) ->
83 doc("Complete lifecycle of a request stream. (RFC9114 4.1)"),
84 {ok, Conn} = quicer:connect("localhost", config(port, Config),
85 #{alpn => ["h3"], verify => none}, 5000),
86 %% To make sure the connection is fully established we wait
87 %% to receive the SETTINGS frame on the control stream.
88 {ok, ControlRef, _Settings} = do_wait_settings(Conn),
89 %% Send a request on a request stream.
90 {ok, StreamRef} = quicer:start_stream(Conn, #{}),
91 {ok, EncodedRequest, _EncData, _EncSt} = cow_qpack:encode_field_section([
92 {<<":method">>, <<"GET">>},
93 {<<":scheme">>, <<"https">>},
94 {<<":authority">>, <<"localhost">>},
95 {<<":path">>, <<"/">>},
96 {<<"content-length">>, <<"0">>}
97 ], 0, cow_qpack:init(encoder)),
98 {ok, _} = quicer:send(StreamRef, [
99 <<1>>, %% HEADERS frame.
100 cow_http3:encode_int(iolist_size(EncodedRequest)),
101 EncodedRequest
102 ], ?QUIC_SEND_FLAG_FIN),
103 %% Receive the response.
104 {ok, Data} = do_receive_data(StreamRef),
105 {HLenEnc, HLenBits} = do_guess_int_encoding(Data),
106 <<
107 1, %% HEADERS frame.
108 HLenEnc:2, HLen:HLenBits,
109 EncodedResponse:HLen/bytes,
110 Rest/bits
111 >> = Data,
112 {ok, DecodedResponse, _DecData, _DecSt}
113 = cow_qpack:decode_field_section(EncodedResponse, 0, cow_qpack:init(decoder)),
114 #{
115 <<":status">> := <<"200">>,
116 <<"content-length">> := BodyLen
117 } = maps:from_list(DecodedResponse),
118 {DLenEnc, DLenBits} = do_guess_int_encoding(Rest),
119 <<
120 0, %% DATA frame.
121 DLenEnc:2, DLen:DLenBits,
122 Body:DLen/bytes
123 >> = Rest,
124 <<"Hello world!">> = Body,
125 BodyLen = integer_to_binary(byte_size(Body)),
126 ok = do_wait_peer_send_shutdown(StreamRef),
127 ok = do_wait_stream_closed(StreamRef).
128
129 %% @todo Same test as above but with content-length unset?
130
131 req_stream_two_requests(Config) ->
132 doc("Receipt of multiple requests on a single stream must "
133 "be rejected with an H3_MESSAGE_ERROR stream error. "
134 "(RFC9114 4.1, RFC9114 4.1.2)"),
135 {ok, Conn} = quicer:connect("localhost", config(port, Config),
136 #{alpn => ["h3"], verify => none}, 5000),
137 %% To make sure the connection is fully established we wait
138 %% to receive the SETTINGS frame on the control stream.
139 {ok, ControlRef, _Settings} = do_wait_settings(Conn),
140 %% Send two requests on a request stream.
141 {ok, StreamRef} = quicer:start_stream(Conn, #{}),
142 {ok, EncodedRequest1, _EncData1, EncSt0} = cow_qpack:encode_field_section([
143 {<<":method">>, <<"GET">>},
144 {<<":scheme">>, <<"https">>},
145 {<<":authority">>, <<"localhost">>},
146 {<<":path">>, <<"/">>},
147 {<<"content-length">>, <<"0">>}
148 ], 0, cow_qpack:init(encoder)),
149 {ok, EncodedRequest2, _EncData2, _EncSt} = cow_qpack:encode_field_section([
150 {<<":method">>, <<"GET">>},
151 {<<":scheme">>, <<"https">>},
152 {<<":authority">>, <<"localhost">>},
153 {<<":path">>, <<"/">>},
154 {<<"content-length">>, <<"0">>}
155 ], 0, EncSt0),
156 {ok, _} = quicer:send(StreamRef, [
157 <<1>>, %% HEADERS frame.
158 cow_http3:encode_int(iolist_size(EncodedRequest1)),
159 EncodedRequest1,
160 <<1>>, %% HEADERS frame.
161 cow_http3:encode_int(iolist_size(EncodedRequest2)),
162 EncodedRequest2
163 ]),
164 %% The stream should have been aborted.
165 #{reason := h3_message_error} = do_wait_stream_aborted(StreamRef),
166 ok.
167
168 headers_then_trailers(Config) ->
169 doc("Receipt of HEADERS followed by trailer HEADERS must be accepted. (RFC9114 4.1)"),
170 #{conn := Conn} = do_connect(Config),
171 {ok, StreamRef} = quicer:start_stream(Conn, #{}),
172 {ok, EncodedHeaders, _EncData1, EncSt0} = cow_qpack:encode_field_section([
173 {<<":method">>, <<"GET">>},
174 {<<":scheme">>, <<"https">>},
175 {<<":authority">>, <<"localhost">>},
176 {<<":path">>, <<"/">>},
177 {<<"content-length">>, <<"0">>}
178 ], 0, cow_qpack:init(encoder)),
179 {ok, EncodedTrailers, _EncData2, _EncSt} = cow_qpack:encode_field_section([
180 {<<"content-type">>, <<"text/plain">>}
181 ], 0, EncSt0),
182 {ok, _} = quicer:send(StreamRef, [
183 <<1>>, %% HEADERS frame.
184 cow_http3:encode_int(iolist_size(EncodedHeaders)),
185 EncodedHeaders,
186 <<1>>, %% HEADERS frame for trailers.
187 cow_http3:encode_int(iolist_size(EncodedTrailers)),
188 EncodedTrailers
189 ], ?QUIC_SEND_FLAG_FIN),
190 #{
191 headers := #{<<":status">> := <<"200">>},
192 body := <<"Hello world!">>
193 } = do_receive_response(StreamRef),
194 ok.
195
196 headers_then_data_then_trailers(Config) ->
197 doc("Receipt of HEADERS followed by DATA followed by trailer HEADERS "
198 "must be accepted. (RFC9114 4.1)"),
199 #{conn := Conn} = do_connect(Config),
200 {ok, StreamRef} = quicer:start_stream(Conn, #{}),
201 {ok, EncodedHeaders, _EncData1, EncSt0} = cow_qpack:encode_field_section([
202 {<<":method">>, <<"GET">>},
203 {<<":scheme">>, <<"https">>},
204 {<<":authority">>, <<"localhost">>},
205 {<<":path">>, <<"/">>},
206 {<<"content-length">>, <<"13">>}
207 ], 0, cow_qpack:init(encoder)),
208 {ok, EncodedTrailers, _EncData2, _EncSt} = cow_qpack:encode_field_section([
209 {<<"content-type">>, <<"text/plain">>}
210 ], 0, EncSt0),
211 {ok, _} = quicer:send(StreamRef, [
212 <<1>>, %% HEADERS frame.
213 cow_http3:encode_int(iolist_size(EncodedHeaders)),
214 EncodedHeaders,
215 <<0>>, %% DATA frame.
216 cow_http3:encode_int(13),
217 <<"Hello server!">>,
218 <<1>>, %% HEADERS frame for trailers.
219 cow_http3:encode_int(iolist_size(EncodedTrailers)),
220 EncodedTrailers
221 ], ?QUIC_SEND_FLAG_FIN),
222 #{
223 headers := #{<<":status">> := <<"200">>},
224 body := <<"Hello world!">>
225 } = do_receive_response(StreamRef),
226 ok.
227
228 data_then_headers(Config) ->
229 doc("Receipt of DATA before HEADERS must be rejected "
230 "with an H3_FRAME_UNEXPECTED connection error. (RFC9114 4.1)"),
231 #{conn := Conn} = do_connect(Config),
232 {ok, StreamRef} = quicer:start_stream(Conn, #{}),
233 {ok, EncodedHeaders, _EncData1, _EncSt} = cow_qpack:encode_field_section([
234 {<<":method">>, <<"GET">>},
235 {<<":scheme">>, <<"https">>},
236 {<<":authority">>, <<"localhost">>},
237 {<<":path">>, <<"/">>},
238 {<<"content-length">>, <<"13">>}
239 ], 0, cow_qpack:init(encoder)),
240 {ok, _} = quicer:send(StreamRef, [
241 <<0>>, %% DATA frame.
242 cow_http3:encode_int(13),
243 <<"Hello server!">>,
244 <<1>>, %% HEADERS frame.
245 cow_http3:encode_int(iolist_size(EncodedHeaders)),
246 EncodedHeaders
247 ], ?QUIC_SEND_FLAG_FIN),
248 %% The connection should have been closed.
249 #{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
250 ok.
251
252 headers_then_trailers_then_data(Config) ->
253 doc("Receipt of DATA after trailer HEADERS must be rejected "
254 "with an H3_FRAME_UNEXPECTED connection error. (RFC9114 4.1)"),
255 #{conn := Conn} = do_connect(Config),
256 {ok, StreamRef} = quicer:start_stream(Conn, #{}),
257 {ok, EncodedHeaders, _EncData1, EncSt0} = cow_qpack:encode_field_section([
258 {<<":method">>, <<"GET">>},
259 {<<":scheme">>, <<"https">>},
260 {<<":authority">>, <<"localhost">>},
261 {<<":path">>, <<"/">>}
262 ], 0, cow_qpack:init(encoder)),
263 {ok, EncodedTrailers, _EncData2, _EncSt} = cow_qpack:encode_field_section([
264 {<<"content-type">>, <<"text/plain">>}
265 ], 0, EncSt0),
266 {ok, _} = quicer:send(StreamRef, [
267 <<1>>, %% HEADERS frame.
268 cow_http3:encode_int(iolist_size(EncodedHeaders)),
269 EncodedHeaders,
270 <<1>>, %% HEADERS frame for trailers.
271 cow_http3:encode_int(iolist_size(EncodedTrailers)),
272 EncodedTrailers,
273 <<0>>, %% DATA frame.
274 cow_http3:encode_int(13),
275 <<"Hello server!">>
276 ], ?QUIC_SEND_FLAG_FIN),
277 %% The connection should have been closed.
278 #{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
279 ok.
280
281 headers_then_data_then_trailers_then_data(Config) ->
282 doc("Receipt of DATA after trailer HEADERS must be rejected "
283 "with an H3_FRAME_UNEXPECTED connection error. (RFC9114 4.1)"),
284 #{conn := Conn} = do_connect(Config),
285 {ok, StreamRef} = quicer:start_stream(Conn, #{}),
286 {ok, EncodedHeaders, _EncData1, EncSt0} = cow_qpack:encode_field_section([
287 {<<":method">>, <<"GET">>},
288 {<<":scheme">>, <<"https">>},
289 {<<":authority">>, <<"localhost">>},
290 {<<":path">>, <<"/">>},
291 {<<"content-length">>, <<"13">>}
292 ], 0, cow_qpack:init(encoder)),
293 {ok, EncodedTrailers, _EncData2, _EncSt} = cow_qpack:encode_field_section([
294 {<<"content-type">>, <<"text/plain">>}
295 ], 0, EncSt0),
296 {ok, _} = quicer:send(StreamRef, [
297 <<1>>, %% HEADERS frame.
298 cow_http3:encode_int(iolist_size(EncodedHeaders)),
299 EncodedHeaders,
300 <<0>>, %% DATA frame.
301 cow_http3:encode_int(13),
302 <<"Hello server!">>,
303 <<1>>, %% HEADERS frame for trailers.
304 cow_http3:encode_int(iolist_size(EncodedTrailers)),
305 EncodedTrailers,
306 <<0>>, %% DATA frame.
307 cow_http3:encode_int(13),
308 <<"Hello server!">>
309 ], ?QUIC_SEND_FLAG_FIN),
310 %% The connection should have been closed.
311 #{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
312 ok.
313
314 headers_then_data_then_trailers_then_trailers(Config) ->
315 doc("Receipt of DATA after trailer HEADERS must be rejected "
316 "with an H3_FRAME_UNEXPECTED connection error. (RFC9114 4.1)"),
317 #{conn := Conn} = do_connect(Config),
318 {ok, StreamRef} = quicer:start_stream(Conn, #{}),
319 {ok, EncodedHeaders, _EncData1, EncSt0} = cow_qpack:encode_field_section([
320 {<<":method">>, <<"GET">>},
321 {<<":scheme">>, <<"https">>},
322 {<<":authority">>, <<"localhost">>},
323 {<<":path">>, <<"/">>},
324 {<<"content-length">>, <<"13">>}
325 ], 0, cow_qpack:init(encoder)),
326 {ok, EncodedTrailers1, _EncData2, EncSt1} = cow_qpack:encode_field_section([
327 {<<"content-type">>, <<"text/plain">>}
328 ], 0, EncSt0),
329 {ok, EncodedTrailers2, _EncData3, _EncSt} = cow_qpack:encode_field_section([
330 {<<"content-type">>, <<"text/plain">>}
331 ], 0, EncSt1),
332 {ok, _} = quicer:send(StreamRef, [
333 <<1>>, %% HEADERS frame.
334 cow_http3:encode_int(iolist_size(EncodedHeaders)),
335 EncodedHeaders,
336 <<0>>, %% DATA frame.
337 cow_http3:encode_int(13),
338 <<"Hello server!">>,
339 <<1>>, %% HEADERS frame for trailers.
340 cow_http3:encode_int(iolist_size(EncodedTrailers1)),
341 EncodedTrailers1,
342 <<1>>, %% HEADERS frame for trailers.
343 cow_http3:encode_int(iolist_size(EncodedTrailers2)),
344 EncodedTrailers2
345 ], ?QUIC_SEND_FLAG_FIN),
346 %% The connection should have been closed.
347 #{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
348 ok.
349
350 unknown_then_headers(Config) ->
351 doc("Receipt of unknown frame followed by HEADERS "
352 "must be accepted. (RFC9114 4.1, RFC9114 9)"),
353 unknown_then_headers(Config, do_unknown_frame_type(),
354 rand:bytes(rand:uniform(4096))).
355
356 unknown_then_headers(Config, Type, Bytes) ->
357 #{conn := Conn} = do_connect(Config),
358 {ok, StreamRef} = quicer:start_stream(Conn, #{}),
359 {ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
360 {<<":method">>, <<"GET">>},
361 {<<":scheme">>, <<"https">>},
362 {<<":authority">>, <<"localhost">>},
363 {<<":path">>, <<"/">>},
364 {<<"content-length">>, <<"0">>}
365 ], 0, cow_qpack:init(encoder)),
366 {ok, _} = quicer:send(StreamRef, [
367 cow_http3:encode_int(Type), %% Unknown frame.
368 cow_http3:encode_int(iolist_size(Bytes)),
369 Bytes,
370 <<1>>, %% HEADERS frame.
371 cow_http3:encode_int(iolist_size(EncodedHeaders)),
372 EncodedHeaders
373 ], ?QUIC_SEND_FLAG_FIN),
374 #{
375 headers := #{<<":status">> := <<"200">>},
376 body := <<"Hello world!">>
377 } = do_receive_response(StreamRef),
378 ok.
379
380 headers_then_unknown(Config) ->
381 doc("Receipt of HEADERS followed by unknown frame "
382 "must be accepted. (RFC9114 4.1, RFC9114 9)"),
383 headers_then_unknown(Config, do_unknown_frame_type(),
384 rand:bytes(rand:uniform(4096))).
385
386 headers_then_unknown(Config, Type, Bytes) ->
387 #{conn := Conn} = do_connect(Config),
388 {ok, StreamRef} = quicer:start_stream(Conn, #{}),
389 {ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
390 {<<":method">>, <<"GET">>},
391 {<<":scheme">>, <<"https">>},
392 {<<":authority">>, <<"localhost">>},
393 {<<":path">>, <<"/">>},
394 {<<"content-length">>, <<"0">>}
395 ], 0, cow_qpack:init(encoder)),
396 {ok, _} = quicer:send(StreamRef, [
397 <<1>>, %% HEADERS frame.
398 cow_http3:encode_int(iolist_size(EncodedHeaders)),
399 EncodedHeaders,
400 cow_http3:encode_int(Type), %% Unknown frame.
401 cow_http3:encode_int(iolist_size(Bytes)),
402 Bytes
403 ], ?QUIC_SEND_FLAG_FIN),
404 #{
405 headers := #{<<":status">> := <<"200">>},
406 body := <<"Hello world!">>
407 } = do_receive_response(StreamRef),
408 ok.
409
410 headers_then_data_then_unknown(Config) ->
411 doc("Receipt of HEADERS followed by DATA followed by unknown frame "
412 "must be accepted. (RFC9114 4.1, RFC9114 9)"),
413 headers_then_data_then_unknown(Config, do_unknown_frame_type(),
414 rand:bytes(rand:uniform(4096))).
415
416 headers_then_data_then_unknown(Config, Type, Bytes) ->
417 #{conn := Conn} = do_connect(Config),
418 {ok, StreamRef} = quicer:start_stream(Conn, #{}),
419 {ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
420 {<<":method">>, <<"GET">>},
421 {<<":scheme">>, <<"https">>},
422 {<<":authority">>, <<"localhost">>},
423 {<<":path">>, <<"/">>},
424 {<<"content-length">>, <<"13">>}
425 ], 0, cow_qpack:init(encoder)),
426 {ok, _} = quicer:send(StreamRef, [
427 <<1>>, %% HEADERS frame.
428 cow_http3:encode_int(iolist_size(EncodedHeaders)),
429 EncodedHeaders,
430 <<0>>, %% DATA frame.
431 cow_http3:encode_int(13),
432 <<"Hello server!">>,
433 cow_http3:encode_int(Type), %% Unknown frame.
434 cow_http3:encode_int(iolist_size(Bytes)),
435 Bytes
436 ], ?QUIC_SEND_FLAG_FIN),
437 #{
438 headers := #{<<":status">> := <<"200">>},
439 body := <<"Hello world!">>
440 } = do_receive_response(StreamRef),
441 ok.
442
443 headers_then_trailers_then_unknown(Config) ->
444 doc("Receipt of HEADERS followed by trailer HEADERS followed by unknown frame "
445 "must be accepted. (RFC9114 4.1, RFC9114 9)"),
446 headers_then_data_then_unknown(Config, do_unknown_frame_type(),
447 rand:bytes(rand:uniform(4096))).
448
449 headers_then_trailers_then_unknown(Config, Type, Bytes) ->
450 #{conn := Conn} = do_connect(Config),
451 {ok, StreamRef} = quicer:start_stream(Conn, #{}),
452 {ok, EncodedHeaders, _EncData, EncSt0} = cow_qpack:encode_field_section([
453 {<<":method">>, <<"GET">>},
454 {<<":scheme">>, <<"https">>},
455 {<<":authority">>, <<"localhost">>},
456 {<<":path">>, <<"/">>}
457 ], 0, cow_qpack:init(encoder)),
458 {ok, EncodedTrailers, _EncData2, _EncSt} = cow_qpack:encode_field_section([
459 {<<"content-type">>, <<"text/plain">>}
460 ], 0, EncSt0),
461 {ok, _} = quicer:send(StreamRef, [
462 <<1>>, %% HEADERS frame.
463 cow_http3:encode_int(iolist_size(EncodedHeaders)),
464 EncodedHeaders,
465 <<1>>, %% HEADERS frame for trailers.
466 cow_http3:encode_int(iolist_size(EncodedTrailers)),
467 EncodedTrailers,
468 cow_http3:encode_int(Type), %% Unknown frame.
469 cow_http3:encode_int(iolist_size(Bytes)),
470 Bytes
471 ], ?QUIC_SEND_FLAG_FIN),
472 #{
473 headers := #{<<":status">> := <<"200">>},
474 body := <<"Hello world!">>
475 } = do_receive_response(StreamRef),
476 ok.
477
478 headers_then_data_then_unknown_then_trailers(Config) ->
479 doc("Receipt of HEADERS followed by DATA followed by "
480 "unknown frame followed by trailer HEADERS "
481 "must be accepted. (RFC9114 4.1, RFC9114 9)"),
482 headers_then_data_then_unknown_then_trailers(Config,
483 do_unknown_frame_type(), rand:bytes(rand:uniform(4096))).
484
485 headers_then_data_then_unknown_then_trailers(Config, Type, Bytes) ->
486 #{conn := Conn} = do_connect(Config),
487 {ok, StreamRef} = quicer:start_stream(Conn, #{}),
488 {ok, EncodedHeaders, _EncData, EncSt0} = cow_qpack:encode_field_section([
489 {<<":method">>, <<"GET">>},
490 {<<":scheme">>, <<"https">>},
491 {<<":authority">>, <<"localhost">>},
492 {<<":path">>, <<"/">>},
493 {<<"content-length">>, <<"13">>}
494 ], 0, cow_qpack:init(encoder)),
495 {ok, EncodedTrailers, _EncData2, _EncSt} = cow_qpack:encode_field_section([
496 {<<"content-type">>, <<"text/plain">>}
497 ], 0, EncSt0),
498 {ok, _} = quicer:send(StreamRef, [
499 <<1>>, %% HEADERS frame.
500 cow_http3:encode_int(iolist_size(EncodedHeaders)),
501 EncodedHeaders,
502 <<0>>, %% DATA frame.
503 cow_http3:encode_int(13),
504 <<"Hello server!">>,
505 cow_http3:encode_int(Type), %% Unknown frame.
506 cow_http3:encode_int(iolist_size(Bytes)),
507 Bytes,
508 <<1>>, %% HEADERS frame for trailers.
509 cow_http3:encode_int(iolist_size(EncodedTrailers)),
510 EncodedTrailers
511 ], ?QUIC_SEND_FLAG_FIN),
512 #{
513 headers := #{<<":status">> := <<"200">>},
514 body := <<"Hello world!">>
515 } = do_receive_response(StreamRef),
516 ok.
517
518 headers_then_data_then_unknown_then_data(Config) ->
519 doc("Receipt of HEADERS followed by DATA followed by "
520 "unknown frame followed by DATA "
521 "must be accepted. (RFC9114 4.1, RFC9114 9)"),
522 headers_then_data_then_unknown_then_data(Config,
523 do_unknown_frame_type(), rand:bytes(rand:uniform(4096))).
524
525 headers_then_data_then_unknown_then_data(Config, Type, Bytes) ->
526 #{conn := Conn} = do_connect(Config),
527 {ok, StreamRef} = quicer:start_stream(Conn, #{}),
528 {ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
529 {<<":method">>, <<"GET">>},
530 {<<":scheme">>, <<"https">>},
531 {<<":authority">>, <<"localhost">>},
532 {<<":path">>, <<"/">>},
533 {<<"content-length">>, <<"13">>}
534 ], 0, cow_qpack:init(encoder)),
535 {ok, _} = quicer:send(StreamRef, [
536 <<1>>, %% HEADERS frame.
537 cow_http3:encode_int(iolist_size(EncodedHeaders)),
538 EncodedHeaders,
539 <<0>>, %% DATA frame.
540 cow_http3:encode_int(6),
541 <<"Hello ">>,
542 cow_http3:encode_int(Type), %% Unknown frame.
543 cow_http3:encode_int(iolist_size(Bytes)),
544 Bytes,
545 <<0>>, %% DATA frame.
546 cow_http3:encode_int(7),
547 <<"server!">>
548 ], ?QUIC_SEND_FLAG_FIN),
549 #{
550 headers := #{<<":status">> := <<"200">>},
551 body := <<"Hello world!">>
552 } = do_receive_response(StreamRef),
553 ok.
554
555 headers_then_data_then_trailers_then_unknown(Config) ->
556 doc("Receipt of HEADERS followed by DATA followed by "
557 "trailer HEADERS followed by unknown frame "
558 "must be accepted. (RFC9114 4.1, RFC9114 9)"),
559 headers_then_data_then_trailers_then_unknown(Config,
560 do_unknown_frame_type(), rand:bytes(rand:uniform(4096))).
561
562 headers_then_data_then_trailers_then_unknown(Config, Type, Bytes) ->
563 #{conn := Conn} = do_connect(Config),
564 {ok, StreamRef} = quicer:start_stream(Conn, #{}),
565 {ok, EncodedHeaders, _EncData, EncSt0} = cow_qpack:encode_field_section([
566 {<<":method">>, <<"GET">>},
567 {<<":scheme">>, <<"https">>},
568 {<<":authority">>, <<"localhost">>},
569 {<<":path">>, <<"/">>},
570 {<<"content-length">>, <<"13">>}
571 ], 0, cow_qpack:init(encoder)),
572 {ok, EncodedTrailers, _EncData2, _EncSt} = cow_qpack:encode_field_section([
573 {<<"content-type">>, <<"text/plain">>}
574 ], 0, EncSt0),
575 {ok, _} = quicer:send(StreamRef, [
576 <<1>>, %% HEADERS frame.
577 cow_http3:encode_int(iolist_size(EncodedHeaders)),
578 EncodedHeaders,
579 <<0>>, %% DATA frame.
580 cow_http3:encode_int(13),
581 <<"Hello server!">>,
582 <<1>>, %% HEADERS frame for trailers.
583 cow_http3:encode_int(iolist_size(EncodedTrailers)),
584 EncodedTrailers,
585 cow_http3:encode_int(Type), %% Unknown frame.
586 cow_http3:encode_int(iolist_size(Bytes)),
587 Bytes
588 ], ?QUIC_SEND_FLAG_FIN),
589 #{
590 headers := #{<<":status">> := <<"200">>},
591 body := <<"Hello world!">>
592 } = do_receive_response(StreamRef),
593 ok.
594
595 do_unknown_frame_type() ->
596 Type = rand:uniform(4611686018427387904) - 1,
597 %% Retry if we get a value that's specified.
598 case lists:member(Type, [
599 16#0, 16#1, 16#3, 16#4, 16#5, 16#7, 16#d, %% HTTP/3 core frame types.
600 16#2, 16#6, 16#8, 16#9 %% HTTP/3 reserved frame types that must be rejected.
601 ]) of
602 true -> do_unknown_frame_type();
603 false -> Type
604 end.
605
606 reserved_then_headers(Config) ->
607 doc("Receipt of reserved frame followed by HEADERS "
608 "must be accepted when the reserved frame type is "
609 "of the format 0x1f * N + 0x21. (RFC9114 4.1, RFC9114 7.2.8)"),
610 unknown_then_headers(Config, do_reserved_type(),
611 rand:bytes(rand:uniform(4096))).
612
613 headers_then_reserved(Config) ->
614 doc("Receipt of HEADERS followed by reserved frame "
615 "must be accepted when the reserved frame type is "
616 "of the format 0x1f * N + 0x21. (RFC9114 4.1, RFC9114 7.2.8)"),
617 headers_then_unknown(Config, do_reserved_type(),
618 rand:bytes(rand:uniform(4096))).
619
620 headers_then_data_then_reserved(Config) ->
621 doc("Receipt of HEADERS followed by DATA followed by reserved frame "
622 "must be accepted when the reserved frame type is "
623 "of the format 0x1f * N + 0x21. (RFC9114 4.1, RFC9114 7.2.8)"),
624 headers_then_data_then_unknown(Config, do_reserved_type(),
625 rand:bytes(rand:uniform(4096))).
626
627 headers_then_trailers_then_reserved(Config) ->
628 doc("Receipt of HEADERS followed by trailer HEADERS followed by reserved frame "
629 "must be accepted when the reserved frame type is "
630 "of the format 0x1f * N + 0x21. (RFC9114 4.1, RFC9114 7.2.8)"),
631 headers_then_trailers_then_unknown(Config, do_reserved_type(),
632 rand:bytes(rand:uniform(4096))).
633
634 headers_then_data_then_reserved_then_trailers(Config) ->
635 doc("Receipt of HEADERS followed by DATA followed by "
636 "reserved frame followed by trailer HEADERS "
637 "must be accepted when the reserved frame type is "
638 "of the format 0x1f * N + 0x21. (RFC9114 4.1, RFC9114 7.2.8)"),
639 headers_then_data_then_unknown_then_trailers(Config,
640 do_reserved_type(), rand:bytes(rand:uniform(4096))).
641
642 headers_then_data_then_reserved_then_data(Config) ->
643 doc("Receipt of HEADERS followed by DATA followed by "
644 "reserved frame followed by DATA "
645 "must be accepted when the reserved frame type is "
646 "of the format 0x1f * N + 0x21. (RFC9114 4.1, RFC9114 7.2.8)"),
647 headers_then_data_then_unknown_then_data(Config,
648 do_reserved_type(), rand:bytes(rand:uniform(4096))).
649
650 headers_then_data_then_trailers_then_reserved(Config) ->
651 doc("Receipt of HEADERS followed by DATA followed by "
652 "trailer HEADERS followed by reserved frame "
653 "must be accepted when the reserved frame type is "
654 "of the format 0x1f * N + 0x21. (RFC9114 4.1, RFC9114 7.2.8)"),
655 headers_then_data_then_trailers_then_unknown(Config,
656 do_reserved_type(), rand:bytes(rand:uniform(4096))).
657
658 reject_transfer_encoding_header_with_body(Config) ->
659 doc("Requests containing a transfer-encoding header must be rejected "
660 "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.1, RFC9114 4.1.2, RFC9114 4.2)"),
661 #{conn := Conn} = do_connect(Config),
662 {ok, StreamRef} = quicer:start_stream(Conn, #{}),
663 {ok, EncodedHeaders, _EncData1, _EncSt0} = cow_qpack:encode_field_section([
664 {<<":method">>, <<"GET">>},
665 {<<":scheme">>, <<"https">>},
666 {<<":authority">>, <<"localhost">>},
667 {<<":path">>, <<"/">>},
668 {<<"transfer-encoding">>, <<"chunked">>}
669 ], 0, cow_qpack:init(encoder)),
670 {ok, _} = quicer:send(StreamRef, [
671 <<1>>, %% HEADERS frame.
672 cow_http3:encode_int(iolist_size(EncodedHeaders)),
673 EncodedHeaders,
674 <<0>>, %% DATA frame.
675 cow_http3:encode_int(24),
676 <<"13\r\nHello server!\r\n0\r\n\r\n">>
677 ]),
678 %% The stream should have been aborted.
679 #{reason := h3_message_error} = do_wait_stream_aborted(StreamRef),
680 ok.
681
682 %% 4. Expressing HTTP Semantics in HTTP/3
683 %% 4.1. HTTP Message Framing
684
685 %% An HTTP request/response exchange fully consumes a client-initiated
686 %bidirectional QUIC stream. After sending a request, a client MUST close the
687 %stream for sending. Unless using the CONNECT method (see Section 4.4), clients
688 %MUST NOT make stream closure dependent on receiving a response to their
689 %request. After sending a final response, the server MUST close the stream for
690 %sending. At this point, the QUIC stream is fully closed.
691 %% @todo What to do with clients that DON'T close the stream
692 %% for sending after the request is sent?
693
694 %% If a client-initiated stream terminates without enough of the HTTP message
695 %to provide a complete response, the server SHOULD abort its response stream
696 %with the error code H3_REQUEST_INCOMPLETE.
697 %% @todo difficult!!
698
699 %% When the server does not need to receive the remainder of the request, it
700 %MAY abort reading the request stream, send a complete response, and cleanly
701 %close the sending part of the stream. The error code H3_NO_ERROR SHOULD be
702 %used when requesting that the client stop sending on the request stream.
703 %% @todo read_body related; h2 has this behavior but there is no corresponding test
704
705 %% 4.1.1. Request Cancellation and Rejection
706
707 %% When possible, it is RECOMMENDED that servers send an HTTP response with an
708 %appropriate status code rather than cancelling a request it has already begun
709 %processing.
710
711 %% Implementations SHOULD cancel requests by abruptly terminating any
712 %directions of a stream that are still open. To do so, an implementation resets
713 %the sending parts of streams and aborts reading on the receiving parts of
714 %streams; see Section 2.4 of [QUIC-TRANSPORT].
715
716 %% When the server cancels a request without performing any application
717 %processing, the request is considered "rejected". The server SHOULD abort its
718 %response stream with the error code H3_REQUEST_REJECTED. In this context,
719 %"processed" means that some data from the stream was passed to some higher
720 %layer of software that might have taken some action as a result. The client
721 %can treat requests rejected by the server as though they had never been sent
722 %at all, thereby allowing them to be retried later.
723
724 %% Servers MUST NOT use the H3_REQUEST_REJECTED error code for requests that
725 %were partially or fully processed. When a server abandons a response after
726 %partial processing, it SHOULD abort its response stream with the error code
727 %H3_REQUEST_CANCELLED.
728 %% @todo
729
730 %% Client SHOULD use the error code H3_REQUEST_CANCELLED to cancel requests.
731 %Upon receipt of this error code, a server MAY abruptly terminate the response
732 %using the error code H3_REQUEST_REJECTED if no processing was performed.
733 %Clients MUST NOT use the H3_REQUEST_REJECTED error code, except when a server
734 %has requested closure of the request stream with this error code.
735 %% @todo
736
737 %4.1.2. Malformed Requests and Responses
738 %A malformed request or response is one that is an otherwise valid sequence of
739 %frames but is invalid due to:
740 %
741 %the presence of prohibited fields or pseudo-header fields,
742 %% @todo reject_response_pseudo_headers
743 %% @todo reject_unknown_pseudo_headers
744 %% @todo reject_pseudo_headers_in_trailers
745
746 %the absence of mandatory pseudo-header fields,
747 %invalid values for pseudo-header fields,
748 %pseudo-header fields after fields,
749 %% @todo reject_pseudo_headers_after_regular_headers
750
751 %an invalid sequence of HTTP messages,
752 %the inclusion of invalid characters in field names or values.
753 %
754 %A request or response that is defined as having content when it contains a
755 %Content-Length header field (Section 8.6 of [HTTP]) is malformed if the value
756 %of the Content-Length header field does not equal the sum of the DATA frame
757 %lengths received. A response that is defined as never having content, even
758 %when a Content-Length is present, can have a non-zero Content-Length header
759 %field even though no content is included in DATA frames.
760 %
761 %For malformed requests, a server MAY send an HTTP response indicating the
762 %error prior to closing or resetting the stream.
763 %% @todo All the malformed tests
764
765 headers_reject_uppercase_header_name(Config) ->
766 doc("Requests containing uppercase header names must be rejected "
767 "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.2, RFC9114 4.1.2)"),
768 do_reject_malformed_header(Config,
769 {<<"I-AM-GIGANTIC">>, <<"How's the weather up there?">>}
770 ).
771
772 %% 4.2. HTTP Fields
773 %% An endpoint MUST NOT generate an HTTP/3 field section containing
774 %connection-specific fields; any message containing connection-specific fields
775 %MUST be treated as malformed.
776
777 reject_connection_header(Config) ->
778 doc("Requests containing a connection header must be rejected "
779 "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.2, RFC9114 4.1.2)"),
780 do_reject_malformed_header(Config,
781 {<<"connection">>, <<"close">>}
782 ).
783
784 reject_keep_alive_header(Config) ->
785 doc("Requests containing a keep-alive header must be rejected "
786 "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.2, RFC9114 4.1.2)"),
787 do_reject_malformed_header(Config,
788 {<<"keep-alive">>, <<"timeout=5, max=1000">>}
789 ).
790
791 reject_proxy_authenticate_header(Config) ->
792 doc("Requests containing a proxy-authenticate header must be rejected "
793 "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.2, RFC9114 4.1.2)"),
794 do_reject_malformed_header(Config,
795 {<<"proxy-authenticate">>, <<"Basic">>}
796 ).
797
798 reject_proxy_authorization_header(Config) ->
799 doc("Requests containing a proxy-authorization header must be rejected "
800 "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.2, RFC9114 4.1.2)"),
801 do_reject_malformed_header(Config,
802 {<<"proxy-authorization">>, <<"Basic YWxhZGRpbjpvcGVuc2VzYW1l">>}
803 ).
804
805 reject_transfer_encoding_header(Config) ->
806 doc("Requests containing a transfer-encoding header must be rejected "
807 "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.2, RFC9114 4.1.2)"),
808 do_reject_malformed_header(Config,
809 {<<"transfer-encoding">>, <<"chunked">>}
810 ).
811
812 reject_upgrade_header(Config) ->
813 doc("Requests containing an upgrade header must be rejected "
814 "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.2, RFC9114 4.5, RFC9114 4.1.2)"),
815 do_reject_malformed_header(Config,
816 {<<"upgrade">>, <<"websocket">>}
817 ).
818
819 accept_te_header_value_trailers(Config) ->
820 doc("Requests containing a TE header with a value of \"trailers\" "
821 "must be accepted. (RFC9114 4.2)"),
822 #{conn := Conn} = do_connect(Config),
823 {ok, StreamRef} = quicer:start_stream(Conn, #{}),
824 {ok, EncodedHeaders, _EncData1, EncSt0} = cow_qpack:encode_field_section([
825 {<<":method">>, <<"GET">>},
826 {<<":scheme">>, <<"https">>},
827 {<<":authority">>, <<"localhost">>},
828 {<<":path">>, <<"/">>},
829 {<<"content-length">>, <<"0">>},
830 {<<"te">>, <<"trailers">>}
831 ], 0, cow_qpack:init(encoder)),
832 {ok, EncodedTrailers, _EncData2, _EncSt} = cow_qpack:encode_field_section([
833 {<<"content-type">>, <<"text/plain">>}
834 ], 0, EncSt0),
835 {ok, _} = quicer:send(StreamRef, [
836 <<1>>, %% HEADERS frame.
837 cow_http3:encode_int(iolist_size(EncodedHeaders)),
838 EncodedHeaders,
839 <<1>>, %% HEADERS frame for trailers.
840 cow_http3:encode_int(iolist_size(EncodedTrailers)),
841 EncodedTrailers
842 ], ?QUIC_SEND_FLAG_FIN),
843 #{
844 headers := #{<<":status">> := <<"200">>},
845 body := <<"Hello world!">>
846 } = do_receive_response(StreamRef),
847 ok.
848
849 reject_te_header_other_values(Config) ->
850 doc("Requests containing a TE header with a value other than \"trailers\" must be rejected "
851 "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.2, RFC9114 4.1.2)"),
852 do_reject_malformed_header(Config,
853 {<<"te">>, <<"trailers, deflate;q=0.5">>}
854 ).
855
856 %% @todo response_dont_send_header_in_connection
857 %% @todo response_dont_send_connection_header
858 %% @todo response_dont_send_keep_alive_header
859 %% @todo response_dont_send_proxy_connection_header
860 %% @todo response_dont_send_transfer_encoding_header
861 %% @todo response_dont_send_upgrade_header
862
863 %% 4.2.1. Field Compression
864 %% To allow for better compression efficiency, the Cookie header field
865 %([COOKIES]) MAY be split into separate field lines, each with one or more
866 %cookie-pairs, before compression. If a decompressed field section contains
867 %multiple cookie field lines, these MUST be concatenated into a single byte
868 %string using the two-byte delimiter of "; " (ASCII 0x3b, 0x20) before being
869 %passed into a context other than HTTP/2 or HTTP/3, such as an HTTP/1.1
870 %connection, or a generic HTTP server application.
871
872 %% 4.2.2. Header Size Constraints
873 %% An HTTP/3 implementation MAY impose a limit on the maximum size of the
874 %message header it will accept on an individual HTTP message. A server that
875 %receives a larger header section than it is willing to handle can send an HTTP
876 %431 (Request Header Fields Too Large) status code ([RFC6585]). The size of a
877 %field list is calculated based on the uncompressed size of fields, including
878 %the length of the name and value in bytes plus an overhead of 32 bytes for
879 %each field.
880 %% If an implementation wishes to advise its peer of this limit, it can be
881 %conveyed as a number of bytes in the SETTINGS_MAX_FIELD_SECTION_SIZE
882 %parameter.
883
884 reject_unknown_pseudo_headers(Config) ->
885 doc("Requests containing unknown pseudo-headers must be rejected "
886 "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3, RFC9114 4.1.2)"),
887 do_reject_malformed_header(Config,
888 {<<":upgrade">>, <<"websocket">>}
889 ).
890
891 reject_response_pseudo_headers(Config) ->
892 doc("Requests containing response pseudo-headers must be rejected "
893 "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3, RFC9114 4.1.2)"),
894 do_reject_malformed_header(Config,
895 {<<":status">>, <<"200">>}
896 ).
897
898 reject_pseudo_headers_in_trailers(Config) ->
899 doc("Requests containing pseudo-headers in trailers must be rejected "
900 "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3, RFC9114 4.1.2)"),
901 #{conn := Conn} = do_connect(Config),
902 {ok, StreamRef} = quicer:start_stream(Conn, #{}),
903 {ok, EncodedHeaders, _EncData1, EncSt0} = cow_qpack:encode_field_section([
904 {<<":method">>, <<"GET">>},
905 {<<":scheme">>, <<"https">>},
906 {<<":authority">>, <<"localhost">>},
907 {<<":path">>, <<"/">>},
908 {<<"trailer">>, <<"x-checksum">>}
909 ], 0, cow_qpack:init(encoder)),
910 {ok, EncodedTrailers, _EncData2, _EncSt} = cow_qpack:encode_field_section([
911 {<<"x-checksum">>, <<"md5:4cc909a007407f3706399b6496babec3">>},
912 {<<":path">>, <<"/">>}
913 ], 0, EncSt0),
914 {ok, _} = quicer:send(StreamRef, [
915 <<1>>, %% HEADERS frame.
916 cow_http3:encode_int(iolist_size(EncodedHeaders)),
917 EncodedHeaders,
918 <<0>>, %% DATA frame.
919 cow_http3:encode_int(10000),
920 <<0:10000/unit:8>>,
921 <<1>>, %% HEADERS frame for trailers.
922 cow_http3:encode_int(iolist_size(EncodedTrailers)),
923 EncodedTrailers
924 ]),
925 %% The stream should have been aborted.
926 #{reason := h3_message_error} = do_wait_stream_aborted(StreamRef),
927 ok.
928
929 reject_pseudo_headers_after_regular_headers(Config) ->
930 doc("Requests containing pseudo-headers after regular headers must be rejected "
931 "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3, RFC9114 4.1.2)"),
932 do_reject_malformed_headers(Config, [
933 {<<":method">>, <<"GET">>},
934 {<<":scheme">>, <<"https">>},
935 {<<":authority">>, <<"localhost">>},
936 {<<"content-length">>, <<"0">>},
937 {<<":path">>, <<"/">>}
938 ]).
939
940 reject_userinfo(Config) ->
941 doc("An authority containing a userinfo component must be rejected "
942 "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3.1, RFC9114 4.1.2)"),
943 do_reject_malformed_headers(Config, [
944 {<<":method">>, <<"GET">>},
945 {<<":scheme">>, <<"http">>},
946 {<<":authority">>, <<"user@localhost">>},
947 {<<":path">>, <<"/">>}
948 ]).
949
950 %% To ensure that the HTTP/1.1 request line can be reproduced accurately, this
951 %% pseudo-header field (:authority) MUST be omitted when translating from an
952 %% HTTP/1.1 request that has a request target in a method-specific form;
953 %% see Section 7.1 of [HTTP].
954
955 reject_empty_path(Config) ->
956 doc("A request containing an empty path component must be rejected "
957 "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3.1, RFC9114 4.1.2)"),
958 do_reject_malformed_headers(Config, [
959 {<<":method">>, <<"GET">>},
960 {<<":scheme">>, <<"http">>},
961 {<<":authority">>, <<"localhost">>},
962 {<<":path">>, <<>>}
963 ]).
964
965 reject_missing_pseudo_header_method(Config) ->
966 doc("A request without a method component must be rejected "
967 "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3.1, RFC9114 4.1.2)"),
968 do_reject_malformed_headers(Config, [
969 {<<":scheme">>, <<"http">>},
970 {<<":authority">>, <<"localhost">>},
971 {<<":path">>, <<"/">>}
972 ]).
973
974 reject_many_pseudo_header_method(Config) ->
975 doc("A request containing more than one method component must be rejected "
976 "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3.1, RFC9114 4.1.2)"),
977 do_reject_malformed_headers(Config, [
978 {<<":method">>, <<"GET">>},
979 {<<":method">>, <<"GET">>},
980 {<<":scheme">>, <<"http">>},
981 {<<":authority">>, <<"localhost">>},
982 {<<":path">>, <<"/">>}
983 ]).
984
985 reject_missing_pseudo_header_scheme(Config) ->
986 doc("A request without a scheme component must be rejected "
987 "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3.1, RFC9114 4.1.2)"),
988 do_reject_malformed_headers(Config, [
989 {<<":method">>, <<"GET">>},
990 {<<":authority">>, <<"localhost">>},
991 {<<":path">>, <<"/">>}
992 ]).
993
994 reject_many_pseudo_header_scheme(Config) ->
995 doc("A request containing more than one scheme component must be rejected "
996 "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3.1, RFC9114 4.1.2)"),
997 do_reject_malformed_headers(Config, [
998 {<<":method">>, <<"GET">>},
999 {<<":scheme">>, <<"http">>},
1000 {<<":scheme">>, <<"http">>},
1001 {<<":authority">>, <<"localhost">>},
1002 {<<":path">>, <<"/">>}
1003 ]).
1004
1005 reject_missing_pseudo_header_authority(Config) ->
1006 doc("A request without an authority or host component must be rejected "
1007 "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3.1, RFC9114 4.1.2)"),
1008 do_reject_malformed_headers(Config, [
1009 {<<":method">>, <<"GET">>},
1010 {<<":scheme">>, <<"http">>},
1011 {<<":path">>, <<"/">>}
1012 ]).
1013
1014 accept_host_header_on_missing_pseudo_header_authority(Config) ->
1015 doc("A request without an authority but with a host header must be accepted. "
1016 "(RFC9114 4.3.1)"),
1017 #{conn := Conn} = do_connect(Config),
1018 {ok, StreamRef} = quicer:start_stream(Conn, #{}),
1019 {ok, EncodedHeaders, _EncData1, _EncSt0} = cow_qpack:encode_field_section([
1020 {<<":method">>, <<"GET">>},
1021 {<<":scheme">>, <<"https">>},
1022 {<<":path">>, <<"/">>},
1023 {<<"host">>, <<"localhost">>}
1024 ], 0, cow_qpack:init(encoder)),
1025 {ok, _} = quicer:send(StreamRef, [
1026 <<1>>, %% HEADERS frame.
1027 cow_http3:encode_int(iolist_size(EncodedHeaders)),
1028 EncodedHeaders
1029 ], ?QUIC_SEND_FLAG_FIN),
1030 #{
1031 headers := #{<<":status">> := <<"200">>},
1032 body := <<"Hello world!">>
1033 } = do_receive_response(StreamRef),
1034 ok.
1035
1036 %% @todo
1037 %% If the :scheme pseudo-header field identifies a scheme that has a mandatory
1038 %% authority component (including "http" and "https"), the request MUST contain
1039 %% either an :authority pseudo-header field or a Host header field.
1040 %% - If both fields are present, they MUST NOT be empty.
1041 %% - If both fields are present, they MUST contain the same value.
1042
1043 reject_many_pseudo_header_authority(Config) ->
1044 doc("A request containing more than one authority component must be rejected "
1045 "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3.1, RFC9114 4.1.2)"),
1046 do_reject_malformed_headers(Config, [
1047 {<<":method">>, <<"GET">>},
1048 {<<":scheme">>, <<"http">>},
1049 {<<":authority">>, <<"localhost">>},
1050 {<<":authority">>, <<"localhost">>},
1051 {<<":path">>, <<"/">>}
1052 ]).
1053
1054 reject_missing_pseudo_header_path(Config) ->
1055 doc("A request without a path component must be rejected "
1056 "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3.1, RFC9114 4.1.2)"),
1057 do_reject_malformed_headers(Config, [
1058 {<<":method">>, <<"GET">>},
1059 {<<":scheme">>, <<"http">>},
1060 {<<":authority">>, <<"localhost">>}
1061 ]).
1062
1063 reject_many_pseudo_header_path(Config) ->
1064 doc("A request containing more than one path component must be rejected "
1065 "with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3.1, RFC9114 4.1.2)"),
1066 do_reject_malformed_headers(Config, [
1067 {<<":method">>, <<"GET">>},
1068 {<<":scheme">>, <<"http">>},
1069 {<<":authority">>, <<"localhost">>},
1070 {<<":path">>, <<"/">>},
1071 {<<":path">>, <<"/">>}
1072 ]).
1073
1074 do_reject_malformed_header(Config, Header) ->
1075 do_reject_malformed_headers(Config, [
1076 {<<":method">>, <<"GET">>},
1077 {<<":scheme">>, <<"https">>},
1078 {<<":authority">>, <<"localhost">>},
1079 {<<":path">>, <<"/">>},
1080 Header
1081 ]).
1082
1083 do_reject_malformed_headers(Config, Headers) ->
1084 #{conn := Conn} = do_connect(Config),
1085 {ok, StreamRef} = quicer:start_stream(Conn, #{}),
1086 {ok, EncodedHeaders, _EncData1, _EncSt0}
1087 = cow_qpack:encode_field_section(Headers, 0, cow_qpack:init(encoder)),
1088 {ok, _} = quicer:send(StreamRef, [
1089 <<1>>, %% HEADERS frame.
1090 cow_http3:encode_int(iolist_size(EncodedHeaders)),
1091 EncodedHeaders
1092 ]),
1093 %% The stream should have been aborted.
1094 #{reason := h3_message_error} = do_wait_stream_aborted(StreamRef),
1095 ok.
1096
1097 %% For responses, a single ":status" pseudo-header field is defined that
1098 %% carries the HTTP status code; see Section 15 of [HTTP]. This pseudo-header
1099 %% field MUST be included in all responses; otherwise, the response is malformed
1100 %% (see Section 4.1.2).
1101
1102 %% @todo Implement CONNECT. (RFC9114 4.4. The CONNECT Method)
1103
1104 %% @todo Maybe block the sending of 101 responses? (RFC9114 4.5. HTTP Upgrade) - also HTTP/2.
1105
1106 %% @todo Implement server push (RFC9114 4.6. Server Push)
1107
1108 %% @todo - need a way to list connections
1109 %% 5.2. Connection Shutdown
1110 %% Endpoints initiate the graceful shutdown of an HTTP/3 connection by sending
1111 %% a GOAWAY frame. The GOAWAY frame contains an identifier that indicates to the
1112 %% receiver the range of requests or pushes that were or might be processed in
1113 %% this connection. The server sends a client-initiated bidirectional stream ID;
1114 %% the client sends a push ID. Requests or pushes with the indicated identifier
1115 %% or greater are rejected (Section 4.1.1) by the sender of the GOAWAY. This
1116 %% identifier MAY be zero if no requests or pushes were processed.
1117
1118 %% @todo
1119 %% Upon sending a GOAWAY frame, the endpoint SHOULD explicitly cancel (see
1120 %% Sections 4.1.1 and 7.2.3) any requests or pushes that have identifiers greater
1121 %% than or equal to the one indicated, in order to clean up transport state for
1122 %% the affected streams. The endpoint SHOULD continue to do so as more requests
1123 %% or pushes arrive.
1124
1125 %% @todo
1126 %% Endpoints MUST NOT initiate new requests or promise new pushes on the
1127 %% connection after receipt of a GOAWAY frame from the peer.
1128
1129 %% @todo
1130 %% Requests on stream IDs less than the stream ID in a GOAWAY frame from the
1131 %% server might have been processed; their status cannot be known until a
1132 %% response is received, the stream is reset individually, another GOAWAY is
1133 %% received with a lower stream ID than that of the request in question, or the
1134 %% connection terminates.
1135
1136 %% @todo
1137 %% Servers MAY reject individual requests on streams below the indicated ID if
1138 %% these requests were not processed.
1139
1140 %% @todo
1141 %% If a server receives a GOAWAY frame after having promised pushes with a push
1142 %% ID greater than or equal to the identifier contained in the GOAWAY frame,
1143 %% those pushes will not be accepted.
1144
1145 %% @todo
1146 %% Servers SHOULD send a GOAWAY frame when the closing of a connection is known
1147 %% in advance, even if the advance notice is small, so that the remote peer can
1148 %% know whether or not a request has been partially processed.
1149
1150 %% @todo
1151 %% An endpoint MAY send multiple GOAWAY frames indicating different
1152 %% identifiers, but the identifier in each frame MUST NOT be greater than the
1153 %% identifier in any previous frame, since clients might already have retried
1154 %% unprocessed requests on another HTTP connection. Receiving a GOAWAY containing
1155 %% a larger identifier than previously received MUST be treated as a connection
1156 %% error of type H3_ID_ERROR.
1157
1158 %% @todo
1159 %% An endpoint that is attempting to gracefully shut down a connection can send
1160 %% a GOAWAY frame with a value set to the maximum possible value (2^62-4 for
1161 %% servers, 2^62-1 for clients).
1162
1163 %% @todo
1164 %% Even when a GOAWAY indicates that a given request or push will not be
1165 %% processed or accepted upon receipt, the underlying transport resources still
1166 %% exist. The endpoint that initiated these requests can cancel them to clean up
1167 %% transport state.
1168
1169 %% @todo
1170 %% Once all accepted requests and pushes have been processed, the endpoint can
1171 %% permit the connection to become idle, or it MAY initiate an immediate closure
1172 %% of the connection. An endpoint that completes a graceful shutdown SHOULD use
1173 %% the H3_NO_ERROR error code when closing the connection.
1174
1175 %% @todo
1176 %% If a client has consumed all available bidirectional stream IDs with
1177 %% requests, the server need not send a GOAWAY frame, since the client is unable
1178 %% to make further requests. @todo OK that one's some weird stuff lol
1179
1180 %% @todo
1181 %% 5.3. Immediate Application Closure
1182 %% Before closing the connection, a GOAWAY frame MAY be sent to allow the
1183 %% client to retry some requests. Including the GOAWAY frame in the same packet
1184 %% as the QUIC CONNECTION_CLOSE frame improves the chances of the frame being
1185 %% received by clients.
1186
1187 bidi_allow_at_least_a_hundred(Config) ->
1188 doc("Endpoints must allow the peer to create at least "
1189 "one hundred bidirectional streams. (RFC9114 6.1"),
1190 #{conn := Conn} = do_connect(Config),
1191 receive
1192 {quic, streams_available, Conn, #{bidi_streams := NumStreams}} ->
1193 true = NumStreams >= 100,
1194 ok
1195 after 5000 ->
1196 error(timeout)
1197 end.
1198
1199 unidi_allow_at_least_three(Config) ->
1200 doc("Endpoints must allow the peer to create at least "
1201 "three unidirectional streams. (RFC9114 6.2"),
1202 #{conn := Conn} = do_connect(Config),
1203 %% Confirm that the server advertised support for at least 3 unidi streams.
1204 receive
1205 {quic, streams_available, Conn, #{unidi_streams := NumStreams}} ->
1206 true = NumStreams >= 3,
1207 ok
1208 after 5000 ->
1209 error(timeout)
1210 end,
1211 %% Confirm that we can create the unidi streams.
1212 {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
1213 {ok, ControlRef} = quicer:start_stream(Conn,
1214 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
1215 {ok, _} = quicer:send(ControlRef, [<<0>>, SettingsBin]),
1216 {ok, EncoderRef} = quicer:start_stream(Conn,
1217 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
1218 {ok, _} = quicer:send(EncoderRef, <<2>>),
1219 {ok, DecoderRef} = quicer:start_stream(Conn,
1220 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
1221 {ok, _} = quicer:send(DecoderRef, <<3>>),
1222 %% Streams shouldn't get closed.
1223 fun Loop() ->
1224 receive
1225 %% We don't care about these messages.
1226 {quic, dgram_state_changed, Conn, _} ->
1227 Loop();
1228 {quic, peer_needs_streams, Conn, _} ->
1229 Loop();
1230 %% Any other we do care.
1231 Msg ->
1232 error(Msg)
1233 after 1000 ->
1234 ok
1235 end
1236 end().
1237
1238 unidi_create_critical_first(Config) ->
1239 doc("Endpoints should create the HTTP control stream as well as "
1240 "the QPACK encoder and decoder streams first. (RFC9114 6.2"),
1241 %% The control stream is accepted in the do_connect/1 function.
1242 #{conn := Conn} = do_connect(Config, #{peer_unidi_stream_count => 3}),
1243 Unidi1 = do_accept_qpack_stream(Conn),
1244 Unidi2 = do_accept_qpack_stream(Conn),
1245 case {Unidi1, Unidi2} of
1246 {{encoder, _}, {decoder, _}} ->
1247 ok;
1248 {{decoder, _}, {encoder, _}} ->
1249 ok
1250 end.
1251
1252 do_accept_qpack_stream(Conn) ->
1253 receive
1254 {quic, new_stream, StreamRef, #{flags := Flags}} ->
1255 ok = quicer:setopt(StreamRef, active, true),
1256 true = quicer:is_unidirectional(Flags),
1257 receive {quic, <<Type>>, StreamRef, _} ->
1258 {case Type of
1259 2 -> encoder;
1260 3 -> decoder
1261 end, StreamRef}
1262 after 5000 ->
1263 error(timeout)
1264 end
1265 after 5000 ->
1266 error(timeout)
1267 end.
1268
1269 %% @todo We should also confirm that there's at least 1,024 bytes of
1270 %% flow-control credit for each unidi stream the server creates. (How?)
1271 %% It can be set via stream_recv_window_default in quicer.
1272
1273 unidi_abort_unknown_type(Config) ->
1274 doc("Receipt of an unknown stream type must be aborted "
1275 "with an H3_STREAM_CREATION_ERROR stream error. (RFC9114 6.2, RFC9114 9)"),
1276 #{conn := Conn} = do_connect(Config),
1277 %% Create an unknown unidirectional stream.
1278 {ok, StreamRef} = quicer:start_stream(Conn,
1279 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
1280 {ok, _} = quicer:send(StreamRef, [
1281 cow_http3:encode_int(1 + do_reserved_type()),
1282 rand:bytes(rand:uniform(4096))
1283 ]),
1284 %% The stream should have been aborted.
1285 #{reason := h3_stream_creation_error} = do_wait_stream_aborted(StreamRef),
1286 ok.
1287
1288 unidi_abort_reserved_type(Config) ->
1289 doc("Receipt of a reserved stream type must be aborted "
1290 "with an H3_STREAM_CREATION_ERROR stream error. "
1291 "(RFC9114 6.2, RFC9114 6.2.3, RFC9114 9)"),
1292 #{conn := Conn} = do_connect(Config),
1293 %% Create a reserved unidirectional stream.
1294 {ok, StreamRef} = quicer:start_stream(Conn,
1295 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
1296 {ok, _} = quicer:send(StreamRef, [
1297 cow_http3:encode_int(do_reserved_type()),
1298 rand:bytes(rand:uniform(4096))
1299 ]),
1300 %% The stream should have been aborted.
1301 #{reason := h3_stream_creation_error} = do_wait_stream_aborted(StreamRef),
1302 ok.
1303
1304 %% As certain stream types can affect connection state, a recipient SHOULD NOT
1305 %% discard data from incoming unidirectional streams prior to reading the stream type.
1306
1307 %% Implementations MAY send stream types before knowing whether the peer
1308 %supports them. However, stream types that could modify the state or semantics
1309 %of existing protocol components, including QPACK or other extensions, MUST NOT
1310 %be sent until the peer is known to support them.
1311 %% @todo It may make sense for Cowboy to delay the creation of unidi streams
1312 %% a little in order to save resources. We could create them when the
1313 %% client does as well, or something similar.
1314
1315 %% A receiver MUST tolerate unidirectional streams being closed or reset prior
1316 %% to the reception of the unidirectional stream header.
1317
1318 %% Each side MUST initiate a single control stream at the beginning of the
1319 %% connection and send its SETTINGS frame as the first frame on this stream.
1320 %% @todo What to do when the client never opens a control stream?
1321 %% @todo Similarly, a stream could be opened but with no data being sent.
1322 %% @todo Similarly, a control stream could be opened with no SETTINGS frame sent.
1323
1324 control_reject_first_frame_data(Config) ->
1325 doc("The first frame on a control stream must be a SETTINGS frame "
1326 "or the connection must be closed with an H3_MISSING_SETTINGS "
1327 "connection error. (RFC9114 6.2.1, RFC9114 9)"),
1328 #{conn := Conn} = do_connect(Config),
1329 {ok, ControlRef} = quicer:start_stream(Conn,
1330 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
1331 {ok, _} = quicer:send(ControlRef, [
1332 <<0>>, %% CONTROL stream.
1333 <<0>>, %% DATA frame.
1334 cow_http3:encode_int(12),
1335 <<"Hello world!">>
1336 ]),
1337 %% The connection should have been closed.
1338 #{reason := h3_missing_settings} = do_wait_connection_closed(Conn),
1339 ok.
1340
1341 control_reject_first_frame_headers(Config) ->
1342 doc("The first frame on a control stream must be a SETTINGS frame "
1343 "or the connection must be closed with an H3_MISSING_SETTINGS "
1344 "connection error. (RFC9114 6.2.1, RFC9114 9)"),
1345 #{conn := Conn} = do_connect(Config),
1346 {ok, ControlRef} = quicer:start_stream(Conn,
1347 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
1348 {ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
1349 {<<":method">>, <<"GET">>},
1350 {<<":scheme">>, <<"https">>},
1351 {<<":authority">>, <<"localhost">>},
1352 {<<":path">>, <<"/">>},
1353 {<<"content-length">>, <<"0">>}
1354 ], 0, cow_qpack:init(encoder)),
1355 {ok, _} = quicer:send(ControlRef, [
1356 <<0>>, %% CONTROL stream.
1357 <<1>>, %% HEADERS frame.
1358 cow_http3:encode_int(iolist_size(EncodedHeaders)),
1359 EncodedHeaders
1360 ]),
1361 %% The connection should have been closed.
1362 #{reason := h3_missing_settings} = do_wait_connection_closed(Conn),
1363 ok.
1364
1365 control_reject_first_frame_cancel_push(Config) ->
1366 doc("The first frame on a control stream must be a SETTINGS frame "
1367 "or the connection must be closed with an H3_MISSING_SETTINGS "
1368 "connection error. (RFC9114 6.2.1, RFC9114 9)"),
1369 #{conn := Conn} = do_connect(Config),
1370 {ok, ControlRef} = quicer:start_stream(Conn,
1371 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
1372 {ok, _} = quicer:send(ControlRef, [
1373 <<0>>, %% CONTROL stream.
1374 <<3>>, %% CANCEL_PUSH frame.
1375 cow_http3:encode_int(1),
1376 cow_http3:encode_int(0)
1377 ]),
1378 %% The connection should have been closed.
1379 #{reason := h3_missing_settings} = do_wait_connection_closed(Conn),
1380 ok.
1381
1382 control_accept_first_frame_settings(Config) ->
1383 doc("The first frame on a control stream "
1384 "must be a SETTINGS frame. (RFC9114 6.2.1, RFC9114 9)"),
1385 #{conn := Conn} = do_connect(Config),
1386 {ok, ControlRef} = quicer:start_stream(Conn,
1387 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
1388 {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
1389 {ok, _} = quicer:send(ControlRef, [
1390 <<0>>, %% CONTROL stream.
1391 SettingsBin
1392 ]),
1393 %% The connection should remain up.
1394 receive
1395 {quic, shutdown, Conn, {unknown_quic_status, Code}} ->
1396 Reason = cow_http3:code_to_error(Code),
1397 error(Reason)
1398 after 1000 ->
1399 ok
1400 end.
1401
1402 control_reject_first_frame_push_promise(Config) ->
1403 doc("The first frame on a control stream must be a SETTINGS frame "
1404 "or the connection must be closed with an H3_MISSING_SETTINGS "
1405 "connection error. (RFC9114 6.2.1, RFC9114 9)"),
1406 #{conn := Conn} = do_connect(Config),
1407 {ok, ControlRef} = quicer:start_stream(Conn,
1408 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
1409 {ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
1410 {<<":method">>, <<"GET">>},
1411 {<<":scheme">>, <<"https">>},
1412 {<<":authority">>, <<"localhost">>},
1413 {<<":path">>, <<"/">>},
1414 {<<"content-length">>, <<"0">>}
1415 ], 0, cow_qpack:init(encoder)),
1416
1417 {ok, _} = quicer:send(ControlRef, [
1418 <<0>>, %% CONTROL stream.
1419 <<5>>, %% PUSH_PROMISE frame.
1420 cow_http3:encode_int(iolist_size(EncodedHeaders) + 1),
1421 cow_http3:encode_int(0),
1422 EncodedHeaders
1423 ]),
1424 %% The connection should have been closed.
1425 #{reason := h3_missing_settings} = do_wait_connection_closed(Conn),
1426 ok.
1427
1428 control_reject_first_frame_goaway(Config) ->
1429 doc("The first frame on a control stream must be a SETTINGS frame "
1430 "or the connection must be closed with an H3_MISSING_SETTINGS "
1431 "connection error. (RFC9114 6.2.1, RFC9114 9)"),
1432 #{conn := Conn} = do_connect(Config),
1433 {ok, ControlRef} = quicer:start_stream(Conn,
1434 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
1435 {ok, _} = quicer:send(ControlRef, [
1436 <<0>>, %% CONTROL stream.
1437 <<7>>, %% GOAWAY frame.
1438 cow_http3:encode_int(1),
1439 cow_http3:encode_int(0)
1440 ]),
1441 %% The connection should have been closed.
1442 #{reason := h3_missing_settings} = do_wait_connection_closed(Conn),
1443 ok.
1444
1445 control_reject_first_frame_max_push_id(Config) ->
1446 doc("The first frame on a control stream must be a SETTINGS frame "
1447 "or the connection must be closed with an H3_MISSING_SETTINGS "
1448 "connection error. (RFC9114 6.2.1, RFC9114 9)"),
1449 #{conn := Conn} = do_connect(Config),
1450 {ok, ControlRef} = quicer:start_stream(Conn,
1451 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
1452 {ok, _} = quicer:send(ControlRef, [
1453 <<0>>, %% CONTROL stream.
1454 <<13>>, %% MAX_PUSH_ID frame.
1455 cow_http3:encode_int(1),
1456 cow_http3:encode_int(0)
1457 ]),
1458 %% The connection should have been closed.
1459 #{reason := h3_missing_settings} = do_wait_connection_closed(Conn),
1460 ok.
1461
1462 control_reject_first_frame_reserved(Config) ->
1463 doc("The first frame on a control stream must be a SETTINGS frame "
1464 "or the connection must be closed with an H3_MISSING_SETTINGS "
1465 "connection error. (RFC9114 6.2.1, RFC9114 9)"),
1466 #{conn := Conn} = do_connect(Config),
1467 {ok, ControlRef} = quicer:start_stream(Conn,
1468 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
1469 Len = rand:uniform(512),
1470 {ok, _} = quicer:send(ControlRef, [
1471 <<0>>, %% CONTROL stream.
1472 cow_http3:encode_int(do_reserved_type()),
1473 cow_http3:encode_int(Len),
1474 rand:bytes(Len)
1475 ]),
1476 %% The connection should have been closed.
1477 #{reason := h3_missing_settings} = do_wait_connection_closed(Conn),
1478 ok.
1479
1480 control_reject_multiple(Config) ->
1481 doc("Endpoints must not create multiple control streams. (RFC9114 6.2.1)"),
1482 {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
1483 do_critical_reject_multiple(Config, [<<0>>, SettingsBin]).
1484
1485 do_critical_reject_multiple(Config, HeaderData) ->
1486 #{conn := Conn} = do_connect(Config),
1487 %% Create two critical streams.
1488 {ok, StreamRef1} = quicer:start_stream(Conn,
1489 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
1490 {ok, _} = quicer:send(StreamRef1, HeaderData),
1491 {ok, StreamRef2} = quicer:start_stream(Conn,
1492 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
1493 {ok, _} = quicer:send(StreamRef2, HeaderData),
1494 %% The connection should have been closed.
1495 #{reason := h3_stream_creation_error} = do_wait_connection_closed(Conn),
1496 ok.
1497
1498 control_local_closed_abort(Config) ->
1499 doc("Endpoints must not close the control stream. (RFC9114 6.2.1)"),
1500 {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
1501 do_critical_local_closed_abort(Config, [<<0>>, SettingsBin]).
1502
1503 do_critical_local_closed_abort(Config, HeaderData) ->
1504 #{conn := Conn} = do_connect(Config),
1505 {ok, StreamRef} = quicer:start_stream(Conn,
1506 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
1507 {ok, _} = quicer:send(StreamRef, HeaderData),
1508 %% Wait a little to make sure the stream data was received before we abort.
1509 timer:sleep(100),
1510 %% Close the critical stream.
1511 quicer:async_shutdown_stream(StreamRef, ?QUIC_STREAM_SHUTDOWN_FLAG_ABORT, 0),
1512 %% The connection should have been closed.
1513 timer:sleep(1000),
1514 #{reason := h3_closed_critical_stream} = do_wait_connection_closed(Conn),
1515 ok.
1516
1517 control_local_closed_graceful(Config) ->
1518 doc("Endpoints must not close the control stream. (RFC9114 6.2.1)"),
1519 {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
1520 do_critical_local_closed_graceful(Config, [<<0>>, SettingsBin]).
1521
1522 do_critical_local_closed_graceful(Config, HeaderData) ->
1523 #{conn := Conn} = do_connect(Config),
1524 {ok, StreamRef} = quicer:start_stream(Conn,
1525 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
1526 {ok, _} = quicer:send(StreamRef, HeaderData),
1527 %% Close the critical stream.
1528 quicer:async_shutdown_stream(StreamRef, ?QUIC_STREAM_SHUTDOWN_FLAG_GRACEFUL, 0),
1529 %% The connection should have been closed.
1530 #{reason := h3_closed_critical_stream} = do_wait_connection_closed(Conn),
1531 ok.
1532
1533 control_remote_closed_abort(Config) ->
1534 doc("Endpoints must not close the control stream. (RFC9114 6.2.1)"),
1535 #{conn := Conn, control := ControlRef} = do_connect(Config),
1536 %% Close the control stream.
1537 quicer:async_shutdown_stream(ControlRef, ?QUIC_STREAM_SHUTDOWN_FLAG_ABORT, 0),
1538 %% The connection should have been closed.
1539 #{reason := h3_closed_critical_stream} = do_wait_connection_closed(Conn),
1540 ok.
1541
1542 %% We cannot gracefully shutdown a remote unidi stream; only abort reading.
1543
1544 %% Because the contents of the control stream are used to manage the behavior
1545 %% of other streams, endpoints SHOULD provide enough flow-control credit to keep
1546 %% the peer's control stream from becoming blocked.
1547
1548 %% @todo Implement server push (RFC9114 6.2.2 Push Streams)
1549
1550 data_frame_can_span_multiple_packets(Config) ->
1551 doc("HTTP/3 frames can span multiple packets. (RFC9114 7)"),
1552 #{conn := Conn} = do_connect(Config),
1553 {ok, StreamRef} = quicer:start_stream(Conn, #{}),
1554 {ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
1555 {<<":method">>, <<"GET">>},
1556 {<<":scheme">>, <<"https">>},
1557 {<<":authority">>, <<"localhost">>},
1558 {<<":path">>, <<"/echo/read_body">>},
1559 {<<"content-length">>, <<"13">>}
1560 ], 0, cow_qpack:init(encoder)),
1561 {ok, _} = quicer:send(StreamRef, [
1562 <<1>>, %% HEADERS frame.
1563 cow_http3:encode_int(iolist_size(EncodedHeaders)),
1564 EncodedHeaders,
1565 <<0>>, %% DATA frame.
1566 cow_http3:encode_int(13),
1567 <<"Hello ">>
1568 ]),
1569 timer:sleep(100),
1570 {ok, _} = quicer:send(StreamRef, [
1571 <<"server!">>
1572 ], ?QUIC_SEND_FLAG_FIN),
1573 #{
1574 headers := #{<<":status">> := <<"200">>},
1575 body := <<"Hello server!">>
1576 } = do_receive_response(StreamRef),
1577 ok.
1578
1579 headers_frame_can_span_multiple_packets(Config) ->
1580 doc("HTTP/3 frames can span multiple packets. (RFC9114 7)"),
1581 #{conn := Conn} = do_connect(Config),
1582 {ok, StreamRef} = quicer:start_stream(Conn, #{}),
1583 {ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
1584 {<<":method">>, <<"GET">>},
1585 {<<":scheme">>, <<"https">>},
1586 {<<":authority">>, <<"localhost">>},
1587 {<<":path">>, <<"/">>},
1588 {<<"content-length">>, <<"0">>}
1589 ], 0, cow_qpack:init(encoder)),
1590 Half = iolist_size(EncodedHeaders) div 2,
1591 <<EncodedHeadersPart1:Half/binary, EncodedHeadersPart2/bits>>
1592 = iolist_to_binary(EncodedHeaders),
1593 {ok, _} = quicer:send(StreamRef, [
1594 <<1>>, %% HEADERS frame.
1595 cow_http3:encode_int(iolist_size(EncodedHeaders)),
1596 EncodedHeadersPart1
1597 ]),
1598 timer:sleep(100),
1599 {ok, _} = quicer:send(StreamRef, [
1600 EncodedHeadersPart2
1601 ]),
1602 #{
1603 headers := #{<<":status">> := <<"200">>},
1604 body := <<"Hello world!">>
1605 } = do_receive_response(StreamRef),
1606 ok.
1607
1608 %% @todo Implement server push. cancel_push_frame_can_span_multiple_packets(Config) ->
1609
1610 settings_frame_can_span_multiple_packets(Config) ->
1611 doc("HTTP/3 frames can span multiple packets. (RFC9114 7)"),
1612 #{conn := Conn} = do_connect(Config),
1613 {ok, ControlRef} = quicer:start_stream(Conn,
1614 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
1615 {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
1616 <<SettingsPart1:1/binary, SettingsPart2/bits>> = SettingsBin,
1617 {ok, _} = quicer:send(ControlRef, [
1618 <<0>>, %% CONTROL stream.
1619 SettingsPart1
1620 ]),
1621 timer:sleep(100),
1622 {ok, _} = quicer:send(ControlRef, [
1623 SettingsPart2
1624 ]),
1625 %% The connection should remain up.
1626 receive
1627 {quic, shutdown, Conn, {unknown_quic_status, Code}} ->
1628 Reason = cow_http3:code_to_error(Code),
1629 error(Reason)
1630 after 1000 ->
1631 ok
1632 end.
1633
1634 goaway_frame_can_span_multiple_packets(Config) ->
1635 doc("HTTP/3 frames can span multiple packets. (RFC9114 7)"),
1636 #{conn := Conn} = do_connect(Config),
1637 {ok, ControlRef} = quicer:start_stream(Conn,
1638 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
1639 {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
1640 {ok, _} = quicer:send(ControlRef, [
1641 <<0>>, %% CONTROL stream.
1642 SettingsBin,
1643 <<7>>, cow_http3:encode_int(1) %% GOAWAY part 1.
1644 ]),
1645 timer:sleep(100),
1646 {ok, _} = quicer:send(ControlRef, [
1647 cow_http3:encode_int(0) %% GOAWAY part 2.
1648 ]),
1649 %% The connection should be closed gracefully.
1650 receive
1651 {quic, shutdown, Conn, {unknown_quic_status, Code}} ->
1652 h3_no_error = cow_http3:code_to_error(Code),
1653 ok;
1654 %% @todo Temporarily also accept this message. I am
1655 %% not sure why it happens but it isn't wrong per se.
1656 {quic, shutdown, Conn, success} ->
1657 ok
1658 after 1000 ->
1659 error(timeout)
1660 end.
1661
1662 max_push_id_frame_can_span_multiple_packets(Config) ->
1663 doc("HTTP/3 frames can span multiple packets. (RFC9114 7)"),
1664 #{conn := Conn} = do_connect(Config),
1665 {ok, ControlRef} = quicer:start_stream(Conn,
1666 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
1667 {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
1668 {ok, _} = quicer:send(ControlRef, [
1669 <<0>>, %% CONTROL stream.
1670 SettingsBin,
1671 <<13>>, cow_http3:encode_int(1) %% MAX_PUSH_ID part 1.
1672 ]),
1673 timer:sleep(100),
1674 {ok, _} = quicer:send(ControlRef, [
1675 cow_http3:encode_int(0) %% MAX_PUSH_ID part 2.
1676 ]),
1677 %% The connection should remain up.
1678 receive
1679 {quic, shutdown, Conn, {unknown_quic_status, Code}} ->
1680 Reason = cow_http3:code_to_error(Code),
1681 error(Reason)
1682 after 1000 ->
1683 ok
1684 end.
1685
1686 unknown_frame_can_span_multiple_packets(Config) ->
1687 doc("HTTP/3 frames can span multiple packets. (RFC9114 7)"),
1688 #{conn := Conn} = do_connect(Config),
1689 {ok, StreamRef} = quicer:start_stream(Conn, #{}),
1690 {ok, _} = quicer:send(StreamRef, [
1691 cow_http3:encode_int(do_unknown_frame_type()),
1692 cow_http3:encode_int(16383)
1693 ]),
1694 timer:sleep(100),
1695 {ok, _} = quicer:send(StreamRef, rand:bytes(4096)),
1696 timer:sleep(100),
1697 {ok, _} = quicer:send(StreamRef, rand:bytes(4096)),
1698 timer:sleep(100),
1699 {ok, _} = quicer:send(StreamRef, rand:bytes(4096)),
1700 timer:sleep(100),
1701 {ok, _} = quicer:send(StreamRef, rand:bytes(4095)),
1702 {ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
1703 {<<":method">>, <<"GET">>},
1704 {<<":scheme">>, <<"https">>},
1705 {<<":authority">>, <<"localhost">>},
1706 {<<":path">>, <<"/">>}
1707 ], 0, cow_qpack:init(encoder)),
1708 {ok, _} = quicer:send(StreamRef, [
1709 <<1>>, %% HEADERS frame.
1710 cow_http3:encode_int(iolist_size(EncodedHeaders)),
1711 EncodedHeaders
1712 ], ?QUIC_SEND_FLAG_FIN),
1713 #{
1714 headers := #{<<":status">> := <<"200">>},
1715 body := <<"Hello world!">>
1716 } = do_receive_response(StreamRef),
1717 ok.
1718
1719 %% The DATA and SETTINGS frames can be zero-length therefore
1720 %% they cannot be too short.
1721
1722 headers_frame_too_short(Config) ->
1723 doc("Frames that terminate before the end of identified fields "
1724 "must be rejected with an H3_FRAME_ERROR connection error. "
1725 "(RFC9114 7.1, RFC9114 10.8)"),
1726 #{conn := Conn} = do_connect(Config),
1727 {ok, StreamRef} = quicer:start_stream(Conn, #{}),
1728 {ok, _} = quicer:send(StreamRef, [
1729 <<1>>, %% HEADERS frame.
1730 cow_http3:encode_int(0)
1731 ]),
1732 %% The connection should have been closed.
1733 #{reason := h3_frame_error} = do_wait_connection_closed(Conn),
1734 ok.
1735
1736 %% @todo Implement server push. cancel_push_frame_too_short(Config) ->
1737
1738 goaway_frame_too_short(Config) ->
1739 doc("Frames that terminate before the end of identified fields "
1740 "must be rejected with an H3_FRAME_ERROR connection error. "
1741 "(RFC9114 7.1, RFC9114 10.8)"),
1742 #{conn := Conn} = do_connect(Config),
1743 {ok, ControlRef} = quicer:start_stream(Conn,
1744 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
1745 {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
1746 {ok, _} = quicer:send(ControlRef, [
1747 <<0>>, %% CONTROL stream.
1748 SettingsBin,
1749 <<7>>, cow_http3:encode_int(0) %% GOAWAY.
1750 ]),
1751 %% The connection should have been closed.
1752 #{reason := h3_frame_error} = do_wait_connection_closed(Conn),
1753 ok.
1754
1755 max_push_id_frame_too_short(Config) ->
1756 doc("Frames that terminate before the end of identified fields "
1757 "must be rejected with an H3_FRAME_ERROR connection error. "
1758 "(RFC9114 7.1, RFC9114 10.8)"),
1759 #{conn := Conn} = do_connect(Config),
1760 {ok, ControlRef} = quicer:start_stream(Conn,
1761 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
1762 {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
1763 {ok, _} = quicer:send(ControlRef, [
1764 <<0>>, %% CONTROL stream.
1765 SettingsBin,
1766 <<13>>, cow_http3:encode_int(0) %% MAX_PUSH_ID.
1767 ]),
1768 %% The connection should have been closed.
1769 #{reason := h3_frame_error} = do_wait_connection_closed(Conn),
1770 ok.
1771
1772 data_frame_truncated(Config) ->
1773 doc("Truncated frames must be rejected with an "
1774 "H3_FRAME_ERROR connection error. (RFC9114 7.1, RFC9114 10.8)"),
1775 #{conn := Conn} = do_connect(Config),
1776 {ok, StreamRef} = quicer:start_stream(Conn, #{}),
1777 {ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
1778 {<<":method">>, <<"GET">>},
1779 {<<":scheme">>, <<"https">>},
1780 {<<":authority">>, <<"localhost">>},
1781 {<<":path">>, <<"/echo/read_body">>},
1782 {<<"content-length">>, <<"13">>}
1783 ], 0, cow_qpack:init(encoder)),
1784 {ok, _} = quicer:send(StreamRef, [
1785 <<1>>, %% HEADERS frame.
1786 cow_http3:encode_int(iolist_size(EncodedHeaders)),
1787 EncodedHeaders,
1788 <<0>>, %% DATA frame.
1789 cow_http3:encode_int(13),
1790 <<"Hello ">>
1791 ], ?QUIC_SEND_FLAG_FIN),
1792 %% The connection should have been closed.
1793 #{reason := h3_frame_error} = do_wait_connection_closed(Conn),
1794 ok.
1795
1796 headers_frame_truncated(Config) ->
1797 doc("Truncated frames must be rejected with an "
1798 "H3_FRAME_ERROR connection error. (RFC9114 7.1, RFC9114 10.8)"),
1799 #{conn := Conn} = do_connect(Config),
1800 {ok, StreamRef} = quicer:start_stream(Conn, #{}),
1801 {ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
1802 {<<":method">>, <<"GET">>},
1803 {<<":scheme">>, <<"https">>},
1804 {<<":authority">>, <<"localhost">>},
1805 {<<":path">>, <<"/">>},
1806 {<<"content-length">>, <<"0">>}
1807 ], 0, cow_qpack:init(encoder)),
1808 {ok, _} = quicer:send(StreamRef, [
1809 <<1>>, %% HEADERS frame.
1810 cow_http3:encode_int(iolist_size(EncodedHeaders))
1811 ], ?QUIC_SEND_FLAG_FIN),
1812 %% The connection should have been closed.
1813 #{reason := h3_frame_error} = do_wait_connection_closed(Conn),
1814 ok.
1815
1816 %% I am not sure how to test truncated CANCEL_PUSH, SETTINGS, GOAWAY
1817 %% or MAX_PUSH_ID frames, as those are sent on the control stream,
1818 %% which we cannot terminate.
1819
1820 %% The DATA, HEADERS and SETTINGS frames can be of any length
1821 %% therefore they cannot be too long per se, even if unwanted
1822 %% data can be included at the end of the frame's payload.
1823
1824 %% @todo Implement server push. cancel_push_frame_too_long(Config) ->
1825
1826 goaway_frame_too_long(Config) ->
1827 doc("Frames that contain additional bytes after the end of identified fields "
1828 "must be rejected with an H3_FRAME_ERROR connection error. "
1829 "(RFC9114 7.1, RFC9114 10.8)"),
1830 #{conn := Conn} = do_connect(Config),
1831 {ok, ControlRef} = quicer:start_stream(Conn,
1832 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
1833 {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
1834 {ok, _} = quicer:send(ControlRef, [
1835 <<0>>, %% CONTROL stream.
1836 SettingsBin,
1837 <<7>>, cow_http3:encode_int(3), %% GOAWAY.
1838 <<0, 1, 2>>
1839 ]),
1840 %% The connection should have been closed.
1841 #{reason := h3_frame_error} = do_wait_connection_closed(Conn),
1842 ok.
1843
1844 max_push_id_frame_too_long(Config) ->
1845 doc("Frames that contain additional bytes after the end of identified fields "
1846 "must be rejected with an H3_FRAME_ERROR connection error. "
1847 "(RFC9114 7.1, RFC9114 10.8)"),
1848 #{conn := Conn} = do_connect(Config),
1849 {ok, ControlRef} = quicer:start_stream(Conn,
1850 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
1851 {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
1852 {ok, _} = quicer:send(ControlRef, [
1853 <<0>>, %% CONTROL stream.
1854 SettingsBin,
1855 <<13>>, cow_http3:encode_int(9), %% MAX_PUSH_ID.
1856 <<0, 1, 2, 3, 4, 5, 6, 7, 8>>
1857 ]),
1858 %% The connection should have been closed.
1859 #{reason := h3_frame_error} = do_wait_connection_closed(Conn),
1860 ok.
1861
1862 %% Streams may terminate abruptly in the middle of frames.
1863
1864 data_frame_rejected_on_control_stream(Config) ->
1865 doc("DATA frames received on the control stream must be rejected "
1866 "with an H3_FRAME_UNEXPECTED connection error. (RFC9114 7.2.1)"),
1867 #{conn := Conn} = do_connect(Config),
1868 {ok, ControlRef} = quicer:start_stream(Conn,
1869 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
1870 {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
1871 {ok, _} = quicer:send(ControlRef, [
1872 <<0>>, %% CONTROL stream.
1873 SettingsBin,
1874 <<0>>, %% DATA frame.
1875 cow_http3:encode_int(12),
1876 <<"Hello world!">>
1877 ]),
1878 %% The connection should have been closed.
1879 #{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
1880 ok.
1881
1882 headers_frame_rejected_on_control_stream(Config) ->
1883 doc("HEADERS frames received on the control stream must be rejected "
1884 "with an H3_FRAME_UNEXPECTED connection error. (RFC9114 7.2.2)"),
1885 #{conn := Conn} = do_connect(Config),
1886 {ok, ControlRef} = quicer:start_stream(Conn,
1887 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
1888 {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
1889 {ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
1890 {<<":method">>, <<"GET">>},
1891 {<<":scheme">>, <<"https">>},
1892 {<<":authority">>, <<"localhost">>},
1893 {<<":path">>, <<"/">>},
1894 {<<"content-length">>, <<"0">>}
1895 ], 0, cow_qpack:init(encoder)),
1896 {ok, _} = quicer:send(ControlRef, [
1897 <<0>>, %% CONTROL stream.
1898 SettingsBin,
1899 <<1>>, %% HEADERS frame.
1900 cow_http3:encode_int(iolist_size(EncodedHeaders)),
1901 EncodedHeaders
1902 ]),
1903 %% The connection should have been closed.
1904 #{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
1905 ok.
1906
1907 %% @todo Implement server push. (RFC9114 7.2.3. CANCEL_PUSH)
1908
1909 settings_twice(Config) ->
1910 doc("Receipt of a second SETTINGS frame on the control stream "
1911 "must be rejected with an H3_FRAME_UNEXPECTED connection error. (RFC9114 7.2.4)"),
1912 #{conn := Conn} = do_connect(Config),
1913 {ok, ControlRef} = quicer:start_stream(Conn,
1914 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
1915 {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
1916 {ok, _} = quicer:send(ControlRef, [
1917 <<0>>, %% CONTROL stream.
1918 SettingsBin,
1919 SettingsBin
1920 ]),
1921 %% The connection should have been closed.
1922 #{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
1923 ok.
1924
1925 settings_on_bidi_stream(Config) ->
1926 doc("Receipt of a SETTINGS frame on a bidirectional stream "
1927 "must be rejected with an H3_FRAME_UNEXPECTED connection error. (RFC9114 7.2.4)"),
1928 #{conn := Conn} = do_connect(Config),
1929 {ok, StreamRef} = quicer:start_stream(Conn, #{}),
1930 {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
1931 {ok, EncodedRequest, _EncData, _EncSt} = cow_qpack:encode_field_section([
1932 {<<":method">>, <<"GET">>},
1933 {<<":scheme">>, <<"https">>},
1934 {<<":authority">>, <<"localhost">>},
1935 {<<":path">>, <<"/">>},
1936 {<<"content-length">>, <<"0">>}
1937 ], 0, cow_qpack:init(encoder)),
1938 {ok, _} = quicer:send(StreamRef, [
1939 SettingsBin,
1940 <<1>>, %% HEADERS frame.
1941 cow_http3:encode_int(iolist_size(EncodedRequest)),
1942 EncodedRequest
1943 ], ?QUIC_SEND_FLAG_FIN),
1944 %% The connection should have been closed.
1945 #{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
1946 ok.
1947
1948 settings_identifier_twice(Config) ->
1949 doc("Receipt of a duplicate SETTINGS identifier must be rejected "
1950 "with an H3_SETTINGS_ERROR connection error. (RFC9114 7.2.4)"),
1951 #{conn := Conn} = do_connect(Config),
1952 {ok, ControlRef} = quicer:start_stream(Conn,
1953 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
1954 SettingsPayload = [
1955 cow_http3:encode_int(6), cow_http3:encode_int(4096),
1956 cow_http3:encode_int(6), cow_http3:encode_int(8192)
1957 ],
1958 {ok, _} = quicer:send(ControlRef, [
1959 <<0>>, %% CONTROL stream.
1960 <<4>>, %% SETTINGS frame.
1961 cow_http3:encode_int(iolist_size(SettingsPayload)),
1962 SettingsPayload
1963 ]),
1964 %% The connection should have been closed.
1965 #{reason := h3_settings_error} = do_wait_connection_closed(Conn),
1966 ok.
1967
1968 settings_ignore_unknown_identifier(Config) ->
1969 doc("Unknown SETTINGS identifiers must be ignored (RFC9114 7.2.4, RFC9114 9)"),
1970 #{conn := Conn} = do_connect(Config),
1971 {ok, ControlRef} = quicer:start_stream(Conn,
1972 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
1973 SettingsPayload = [
1974 cow_http3:encode_int(999), cow_http3:encode_int(4096)
1975 ],
1976 {ok, _} = quicer:send(ControlRef, [
1977 <<0>>, %% CONTROL stream.
1978 <<4>>, %% SETTINGS frame.
1979 cow_http3:encode_int(iolist_size(SettingsPayload)),
1980 SettingsPayload
1981 ]),
1982 %% The connection should remain up.
1983 receive
1984 {quic, shutdown, Conn, {unknown_quic_status, Code}} ->
1985 Reason = cow_http3:code_to_error(Code),
1986 error(Reason)
1987 after 1000 ->
1988 ok
1989 end.
1990
1991 settings_ignore_reserved_identifier(Config) ->
1992 doc("Reserved SETTINGS identifiers must be ignored (RFC9114 7.2.4.1)"),
1993 #{conn := Conn} = do_connect(Config),
1994 {ok, ControlRef} = quicer:start_stream(Conn,
1995 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
1996 SettingsPayload = [
1997 cow_http3:encode_int(do_reserved_type()), cow_http3:encode_int(4096)
1998 ],
1999 {ok, _} = quicer:send(ControlRef, [
2000 <<0>>, %% CONTROL stream.
2001 <<4>>, %% SETTINGS frame.
2002 cow_http3:encode_int(iolist_size(SettingsPayload)),
2003 SettingsPayload
2004 ]),
2005 %% The connection should remain up.
2006 receive
2007 {quic, shutdown, Conn, {unknown_quic_status, Code}} ->
2008 Reason = cow_http3:code_to_error(Code),
2009 error(Reason)
2010 after 1000 ->
2011 ok
2012 end.
2013
2014 %% @todo Check that we send a reserved SETTINGS identifier when sending a
2015 %% non-empty SETTINGS frame. (7.2.4.1. Defined SETTINGS Parameters)
2016
2017 %% @todo Check that setting SETTINGS_MAX_FIELD_SECTION_SIZE works.
2018
2019 %% It is unclear whether the SETTINGS identifier 0x00 must be rejected or ignored.
2020
2021 settings_reject_http2_0x02(Config) ->
2022 do_settings_reject_http2(Config, 2, 1).
2023
2024 settings_reject_http2_0x03(Config) ->
2025 do_settings_reject_http2(Config, 3, 100).
2026
2027 settings_reject_http2_0x04(Config) ->
2028 do_settings_reject_http2(Config, 4, 128000).
2029
2030 settings_reject_http2_0x05(Config) ->
2031 do_settings_reject_http2(Config, 5, 1000000).
2032
2033 do_settings_reject_http2(Config, Identifier, Value) ->
2034 doc("Receipt of an unused HTTP/2 SETTINGS identifier must be rejected "
2035 "with an H3_SETTINGS_ERROR connection error. (RFC9114 7.2.4, RFC9114 11.2.2)"),
2036 #{conn := Conn} = do_connect(Config),
2037 {ok, ControlRef} = quicer:start_stream(Conn,
2038 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
2039 SettingsPayload = [
2040 cow_http3:encode_int(Identifier), cow_http3:encode_int(Value)
2041 ],
2042 {ok, _} = quicer:send(ControlRef, [
2043 <<0>>, %% CONTROL stream.
2044 <<4>>, %% SETTINGS frame.
2045 cow_http3:encode_int(iolist_size(SettingsPayload)),
2046 SettingsPayload
2047 ]),
2048 %% The connection should have been closed.
2049 #{reason := h3_settings_error} = do_wait_connection_closed(Conn),
2050 ok.
2051
2052 %% 7.2.4.2. Initialization
2053 %% An HTTP implementation MUST NOT send frames or requests that would be
2054 %% invalid based on its current understanding of the peer's settings.
2055 %% @todo In the case of SETTINGS_MAX_FIELD_SECTION_SIZE I don't think we have a choice.
2056
2057 %% All settings begin at an initial value. Each endpoint SHOULD use these
2058 %% initial values to send messages before the peer's SETTINGS frame has arrived,
2059 %% as packets carrying the settings can be lost or delayed. When the SETTINGS
2060 %% frame arrives, any settings are changed to their new values.
2061
2062 %% Endpoints MUST NOT require any data to be received from the peer prior to
2063 %% sending the SETTINGS frame; settings MUST be sent as soon as the transport is
2064 %% ready to send data.
2065
2066 %% @todo Implement 0-RTT. (7.2.4.2. Initialization)
2067
2068 %% @todo Implement server push. (7.2.5. PUSH_PROMISE)
2069
2070 goaway_on_bidi_stream(Config) ->
2071 doc("Receipt of a GOAWAY frame on a bidirectional stream "
2072 "must be rejected with an H3_FRAME_UNEXPECTED connection error. (RFC9114 7.2.6)"),
2073 #{conn := Conn} = do_connect(Config),
2074 {ok, StreamRef} = quicer:start_stream(Conn, #{}),
2075 {ok, _} = quicer:send(StreamRef, [
2076 <<7>>, cow_http3:encode_int(1), cow_http3:encode_int(0) %% GOAWAY.
2077 ], ?QUIC_SEND_FLAG_FIN),
2078 %% The connection should have been closed.
2079 #{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
2080 ok.
2081
2082 %% @todo Implement server push. (7.2.6 GOAWAY - will have to reject too large push IDs)
2083
2084 max_push_id_on_bidi_stream(Config) ->
2085 doc("Receipt of a MAX_PUSH_ID frame on a bidirectional stream "
2086 "must be rejected with an H3_FRAME_UNEXPECTED connection error. (RFC9114 7.2.7)"),
2087 #{conn := Conn} = do_connect(Config),
2088 {ok, StreamRef} = quicer:start_stream(Conn, #{}),
2089 {ok, _} = quicer:send(StreamRef, [
2090 <<13>>, cow_http3:encode_int(1), cow_http3:encode_int(0) %% MAX_PUSH_ID.
2091 ], ?QUIC_SEND_FLAG_FIN),
2092 %% The connection should have been closed.
2093 #{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
2094 ok.
2095
2096 %% @todo Implement server push. (7.2.7 MAX_PUSH_ID)
2097
2098 max_push_id_reject_lower(Config) ->
2099 doc("Receipt of a MAX_PUSH_ID value lower than previously received "
2100 "must be rejected with an H3_ID_ERROR connection error. (RFC9114 7.2.7)"),
2101 #{conn := Conn} = do_connect(Config),
2102 {ok, ControlRef} = quicer:start_stream(Conn,
2103 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
2104 {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
2105 {ok, _} = quicer:send(ControlRef, [
2106 <<0>>, %% CONTROL stream.
2107 SettingsBin,
2108 <<13>>, cow_http3:encode_int(1), cow_http3:encode_int(20), %% MAX_PUSH_ID.
2109 <<13>>, cow_http3:encode_int(1), cow_http3:encode_int(10) %% MAX_PUSH_ID.
2110 ]),
2111 %% The connection should have been closed.
2112 #{reason := h3_id_error} = do_wait_connection_closed(Conn),
2113 ok.
2114
2115 reserved_on_control_stream(Config) ->
2116 doc("Receipt of a reserved frame type on a control stream "
2117 "must be ignored. (RFC9114 7.2.8)"),
2118 #{conn := Conn} = do_connect(Config),
2119 {ok, ControlRef} = quicer:start_stream(Conn,
2120 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
2121 {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
2122 Len = rand:uniform(512),
2123 {ok, _} = quicer:send(ControlRef, [
2124 <<0>>, %% CONTROL stream.
2125 SettingsBin,
2126 cow_http3:encode_int(do_reserved_type()),
2127 cow_http3:encode_int(Len),
2128 rand:bytes(Len)
2129 ]),
2130 %% The connection should remain up.
2131 receive
2132 {quic, shutdown, Conn, {unknown_quic_status, Code}} ->
2133 Reason = cow_http3:code_to_error(Code),
2134 error(Reason)
2135 after 1000 ->
2136 ok
2137 end.
2138
2139 reserved_reject_http2_0x02_control(Config) ->
2140 do_reserved_reject_http2_control(Config, 2).
2141
2142 reserved_reject_http2_0x06_control(Config) ->
2143 do_reserved_reject_http2_control(Config, 6).
2144
2145 reserved_reject_http2_0x08_control(Config) ->
2146 do_reserved_reject_http2_control(Config, 8).
2147
2148 reserved_reject_http2_0x09_control(Config) ->
2149 do_reserved_reject_http2_control(Config, 9).
2150
2151 do_reserved_reject_http2_control(Config, Type) ->
2152 doc("Receipt of an unused HTTP/2 frame type must be rejected "
2153 "with an H3_FRAME_UNEXPECTED connection error. (RFC9114 7.2.8, RFC9114 11.2.1)"),
2154 #{conn := Conn} = do_connect(Config),
2155 {ok, ControlRef} = quicer:start_stream(Conn,
2156 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
2157 {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
2158 Len = rand:uniform(512),
2159 {ok, _} = quicer:send(ControlRef, [
2160 <<0>>, %% CONTROL stream.
2161 SettingsBin,
2162 cow_http3:encode_int(Type),
2163 cow_http3:encode_int(Len),
2164 rand:bytes(Len)
2165 ]),
2166 %% The connection should have been closed.
2167 #{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
2168 ok.
2169
2170 reserved_reject_http2_0x02_bidi(Config) ->
2171 do_reserved_reject_http2_bidi(Config, 2).
2172
2173 reserved_reject_http2_0x06_bidi(Config) ->
2174 do_reserved_reject_http2_bidi(Config, 6).
2175
2176 reserved_reject_http2_0x08_bidi(Config) ->
2177 do_reserved_reject_http2_bidi(Config, 8).
2178
2179 reserved_reject_http2_0x09_bidi(Config) ->
2180 do_reserved_reject_http2_bidi(Config, 9).
2181
2182 do_reserved_reject_http2_bidi(Config, Type) ->
2183 doc("Receipt of an unused HTTP/2 frame type must be rejected "
2184 "with an H3_FRAME_UNEXPECTED connection error. (RFC9114 7.2.8, RFC9114 11.2.1)"),
2185 #{conn := Conn} = do_connect(Config),
2186 {ok, StreamRef} = quicer:start_stream(Conn, #{}),
2187 {ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
2188 {<<":method">>, <<"GET">>},
2189 {<<":scheme">>, <<"https">>},
2190 {<<":authority">>, <<"localhost">>},
2191 {<<":path">>, <<"/">>},
2192 {<<"content-length">>, <<"0">>}
2193 ], 0, cow_qpack:init(encoder)),
2194 Len = rand:uniform(512),
2195 {ok, _} = quicer:send(StreamRef, [
2196 cow_http3:encode_int(Type),
2197 cow_http3:encode_int(Len),
2198 rand:bytes(Len),
2199 <<1>>, %% HEADERS frame.
2200 cow_http3:encode_int(iolist_size(EncodedHeaders)),
2201 EncodedHeaders
2202 ], ?QUIC_SEND_FLAG_FIN),
2203 %% The connection should have been closed.
2204 #{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
2205 ok.
2206
2207 %% An endpoint MAY choose to treat a stream error as a connection error under
2208 %% certain circumstances, closing the entire connection in response to a
2209 %% condition on a single stream.
2210
2211 %% Because new error codes can be defined without negotiation (see Section 9),
2212 %% use of an error code in an unexpected context or receipt of an unknown error
2213 %% code MUST be treated as equivalent to H3_NO_ERROR.
2214
2215 %% 8.1. HTTP/3 Error Codes
2216 %% H3_INTERNAL_ERROR (0x0102): An internal error has occurred in the HTTP stack.
2217 %% H3_EXCESSIVE_LOAD (0x0107): The endpoint detected that its peer is
2218 %% exhibiting a behavior that might be generating excessive load.
2219 %% H3_MISSING_SETTINGS (0x010a): No SETTINGS frame was received
2220 %% at the beginning of the control stream.
2221 %% H3_REQUEST_REJECTED (0x010b): A server rejected a request without
2222 %% performing any application processing.
2223 %% H3_REQUEST_CANCELLED (0x010c): The request or its response
2224 %% (including pushed response) is cancelled.
2225 %% H3_REQUEST_INCOMPLETE (0x010d): The client's stream terminated
2226 %% without containing a fully formed request.
2227 %% H3_CONNECT_ERROR (0x010f): The TCP connection established in
2228 %% response to a CONNECT request was reset or abnormally closed.
2229 %% H3_VERSION_FALLBACK (0x0110): The requested operation cannot
2230 %% be served over HTTP/3. The peer should retry over HTTP/1.1.
2231
2232 %% 9. Extensions to HTTP/3
2233 %% If a setting is used for extension negotiation, the default value MUST be
2234 %% defined in such a fashion that the extension is disabled if the setting is
2235 %% omitted.
2236
2237 %% 10. Security Considerations
2238 %% 10.3. Intermediary-Encapsulation Attacks
2239 %% Requests or responses containing invalid field names MUST be treated as malformed.
2240 %% Any request or response that contains a character not permitted in a field
2241 %% value MUST be treated as malformed.
2242
2243 %% 10.5. Denial-of-Service Considerations
2244 %% Implementations SHOULD track the use of these features and set limits on
2245 %% their use. An endpoint MAY treat activity that is suspicious as a connection
2246 %% error of type H3_EXCESSIVE_LOAD, but false positives will result in disrupting
2247 %% valid connections and requests.
2248
2249 reject_large_unknown_frame(Config) ->
2250 doc("Large unknown frames may risk denial-of-service "
2251 "and should be rejected. (RFC9114 10.5)"),
2252 #{conn := Conn} = do_connect(Config),
2253 {ok, StreamRef} = quicer:start_stream(Conn, #{}),
2254 {ok, _} = quicer:send(StreamRef, [
2255 cow_http3:encode_int(do_unknown_frame_type()),
2256 cow_http3:encode_int(16385)
2257 ]),
2258 #{reason := h3_excessive_load} = do_wait_connection_closed(Conn),
2259 ok.
2260
2261 %% 10.5.1. Limits on Field Section Size
2262 %% An endpoint can use the SETTINGS_MAX_FIELD_SECTION_SIZE (Section 4.2.2)
2263 %% setting to advise peers of limits that might apply on the size of field
2264 %% sections.
2265 %%
2266 %% A server that receives a larger field section than it is willing to handle
2267 %% can send an HTTP 431 (Request Header Fields Too Large) status code
2268 %% ([RFC6585]).
2269
2270 %% 10.6. Use of Compression
2271 %% Implementations communicating on a secure channel MUST NOT compress content
2272 %% that includes both confidential and attacker-controlled data unless separate
2273 %% compression contexts are used for each source of data. Compression MUST NOT be
2274 %% used if the source of data cannot be reliably determined.
2275
2276 %% 10.9. Early Data
2277 %% The anti-replay mitigations in [HTTP-REPLAY] MUST be applied when using HTTP/3 with 0-RTT.
2278
2279 %% 10.10. Migration
2280 %% Certain HTTP implementations use the client address for logging or
2281 %% access-control purposes. Since a QUIC client's address might change during a
2282 %% connection (and future versions might support simultaneous use of multiple
2283 %% addresses), such implementations will need to either actively retrieve the
2284 %% client's current address or addresses when they are relevant or explicitly
2285 %% accept that the original address might change. @todo Document this behavior.
2286
2287 %% Appendix A. Considerations for Transitioning from HTTP/2
2288 %% A.1. Streams
2289 %% QUIC considers a stream closed when all data has been received and sent data
2290 %% has been acknowledged by the peer. HTTP/2 considers a stream closed when the
2291 %% frame containing the END_STREAM bit has been committed to the transport. As a
2292 %% result, the stream for an equivalent exchange could remain "active" for a
2293 %% longer period of time. HTTP/3 servers might choose to permit a larger number
2294 %% of concurrent client-initiated bidirectional streams to achieve equivalent
2295 %% concurrency to HTTP/2, depending on the expected usage patterns. @todo Document this.
2296
2297 %% Helper functions.
2298
2299 %% @todo Maybe have a function in cow_http3.
2300 do_reserved_type() ->
2301 16#1f * (rand:uniform(148764065110560900) - 1) + 16#21.
2302
2303 do_connect(Config) ->
2304 do_connect(Config, #{}).
2305
2306 do_connect(Config, Opts) ->
2307 {ok, Conn} = quicer:connect("localhost", config(port, Config),
2308 Opts#{alpn => ["h3"], verify => none}, 5000),
2309 %% To make sure the connection is fully established we wait
2310 %% to receive the SETTINGS frame on the control stream.
2311 {ok, ControlRef, Settings} = do_wait_settings(Conn),
2312 #{
2313 conn => Conn,
2314 control => ControlRef, %% This is the peer control stream.
2315 settings => Settings
2316 }.
2317
2318 do_wait_settings(Conn) ->
2319 receive
2320 {quic, new_stream, StreamRef, #{flags := Flags}} ->
2321 ok = quicer:setopt(StreamRef, active, true),
2322 true = quicer:is_unidirectional(Flags),
2323 receive {quic, <<
2324 0, %% Control stream.
2325 SettingsFrame/bits
2326 >>, StreamRef, _} ->
2327 {ok, {settings, Settings}, <<>>} = cow_http3:parse(SettingsFrame),
2328 {ok, StreamRef, Settings}
2329 after 5000 ->
2330 {error, timeout}
2331 end
2332 after 5000 ->
2333 {error, timeout}
2334 end.
2335
2336 do_receive_data(StreamRef) ->
2337 receive
2338 {quic, Data, StreamRef, _Flags} when is_binary(Data) ->
2339 {ok, Data}
2340 after 5000 ->
2341 {error, timeout}
2342 end.
2343
2344 do_guess_int_encoding(Data) ->
2345 SizeWithLen = byte_size(Data) - 1,
2346 if
2347 SizeWithLen < 64 + 1 ->
2348 {0, 6};
2349 SizeWithLen < 16384 + 2 ->
2350 {1, 14};
2351 SizeWithLen < 1073741824 + 4 ->
2352 {2, 30};
2353 SizeWithLen < 4611686018427387904 + 8 ->
2354 {3, 62}
2355 end.
2356
2357 do_wait_peer_send_shutdown(StreamRef) ->
2358 receive
2359 {quic, peer_send_shutdown, StreamRef, undefined} ->
2360 ok
2361 after 5000 ->
2362 {error, timeout}
2363 end.
2364
2365 do_wait_stream_aborted(StreamRef) ->
2366 receive
2367 {quic, peer_send_aborted, StreamRef, Code} ->
2368 Reason = cow_http3:code_to_error(Code),
2369 #{reason => Reason};
2370 {quic, peer_receive_aborted, StreamRef, Code} ->
2371 Reason = cow_http3:code_to_error(Code),
2372 #{reason => Reason}
2373 after 5000 ->
2374 {error, timeout}
2375 end.
2376
2377 do_wait_stream_closed(StreamRef) ->
2378 receive
2379 {quic, stream_closed, StreamRef, #{error := Error, is_conn_shutdown := false}} ->
2380 0 = Error,
2381 ok
2382 after 5000 ->
2383 {error, timeout}
2384 end.
2385
2386 do_receive_response(StreamRef) ->
2387 {ok, Data} = do_receive_data(StreamRef),
2388 {HLenEnc, HLenBits} = do_guess_int_encoding(Data),
2389 <<
2390 1, %% HEADERS frame.
2391 HLenEnc:2, HLen:HLenBits,
2392 EncodedResponse:HLen/bytes,
2393 Rest/bits
2394 >> = Data,
2395 {ok, DecodedResponse, _DecData, _DecSt}
2396 = cow_qpack:decode_field_section(EncodedResponse, 0, cow_qpack:init(decoder)),
2397 Headers = maps:from_list(DecodedResponse),
2398 #{<<"content-length">> := BodyLen} = Headers,
2399 {DLenEnc, DLenBits} = do_guess_int_encoding(Rest),
2400 Body = case Rest of
2401 <<>> ->
2402 <<>>;
2403 <<
2404 0, %% DATA frame.
2405 DLenEnc:2, DLen:DLenBits,
2406 Body0:DLen/bytes
2407 >> ->
2408 BodyLen = integer_to_binary(byte_size(Body0)),
2409 Body0
2410 end,
2411 ok = do_wait_peer_send_shutdown(StreamRef),
2412 #{
2413 headers => Headers,
2414 body => Body
2415 }.
2416
2417 do_wait_connection_closed(Conn) ->
2418 receive
2419 {quic, shutdown, Conn, {unknown_quic_status, Code}} ->
2420 Reason = cow_http3:code_to_error(Code),
2421 #{reason => Reason}
2422 after 5000 ->
2423 {error, timeout}
2424 end.
2425
2426 -else.
2427
2428 all() -> [].
2429
2430 -endif.