Debian Codegraph @

test/draft_h3_webtransport_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(draft_h3_webtransport_SUITE).
16 -compile(export_all).
17 -compile(nowarn_export_all).
18
19 -import(ct_helper, [config/2]).
20 -import(ct_helper, [doc/1]).
21 -import(rfc9114_SUITE, [do_wait_stream_aborted/1]).
22
23 -ifdef(COWBOY_QUICER).
24
25 -include_lib("quicer/include/quicer.hrl").
26
27 all() ->
28 [{group, enabled}].
29
30 groups() ->
31 Tests = ct_helper:all(?MODULE),
32 [{enabled, [], Tests}]. %% @todo Enable parallel when all is better.
33
34 init_per_group(Name = enabled, Config) ->
35 cowboy_test:init_http3(Name, #{
36 enable_connect_protocol => true,
37 h3_datagram => true,
38 enable_webtransport => true, %% For compatibility with draft-02.
39 wt_max_sessions => 10,
40 env => #{dispatch => cowboy_router:compile(init_routes(Config))}
41 }, Config).
42
43 end_per_group(Name, _) ->
44 cowboy_test:stop_group(Name).
45
46 init_routes(_) -> [
47 {"localhost", [
48 {"/wt", wt_echo_h, []}
49 ]}
50 ].
51
52 %% Temporary.
53
54 %% To start Chromium the command line is roughly:
55 %% chromium --ignore-certificate-errors-spki-list=LeLykt63i2FRAm+XO91yBoSjKfrXnAFygqe5xt0zgDA= --ignore-certificate-errors --user-data-dir=/tmp/chromium-wt --allow-insecure-localhost --webtransport-developer-mode --enable-quic https://googlechrome.github.io/samples/webtransport/client.html
56 %%
57 %% To find the SPKI the command is roughly:
58 %% openssl x509 -in ~/ninenines/cowboy/test/rfc9114_SUITE_data/server.pem -pubkey -noout | \
59 %% openssl pkey -pubin -outform der | \
60 %% openssl dgst -sha256 -binary | \
61 %% openssl enc -base64
62
63 %run(Config) ->
64 % ct:pal("port ~p", [config(port, Config)]),
65 % timer:sleep(infinity).
66
67 %% 3. Session Establishment
68
69 %% 3.1. Establishing a WebTransport-Capable HTTP/3 Connection
70
71 %% In order to indicate support for WebTransport, the server MUST send a SETTINGS_WT_MAX_SESSIONS value greater than "0" in its SETTINGS frame. (3.1)
72 %% @todo reject_session_disabled
73 %% @todo accept_session_below
74 %% @todo accept_session_equal
75 %% @todo reject_session_above
76
77 %% The client MUST NOT send a WebTransport request until it has received the setting indicating WebTransport support from the server. (3.1)
78
79 %% For draft verisons of WebTransport only, the server MUST NOT process any incoming WebTransport requests until the client settings have been received, as the client may be using a version of the WebTransport extension that is different from the one used by the server. (3.1)
80
81 %% Because WebTransport over HTTP/3 requires support for HTTP/3 datagrams and the Capsule Protocol, both the client and the server MUST indicate support for HTTP/3 datagrams by sending a SETTINGS_H3_DATAGRAM value set to 1 in their SETTINGS frame (see Section 2.1.1 of [HTTP-DATAGRAM]). (3.1)
82 %% @todo settings_h3_datagram_enabled
83
84 %% WebTransport over HTTP/3 also requires support for QUIC datagrams. To indicate support, both the client and the server MUST send a max_datagram_frame_size transport parameter with a value greater than 0 (see Section 3 of [QUIC-DATAGRAM]). (3.1)
85 %% @todo quic_datagram_enabled (if size is too low the CONNECT stream can be used for capsules)
86
87 %% Any WebTransport requests sent by the client without enabling QUIC and HTTP datagrams MUST be treated as malformed by the server, as described in Section 4.1.2 of [HTTP3]. (3.1)
88 %% @todo reject_h3_datagram_disabled
89 %% @todo reject_quic_datagram_disabled
90
91 %% WebTransport over HTTP/3 relies on the RESET_STREAM_AT frame defined in [RESET-STREAM-AT]. To indicate support, both the client and the server MUST enable the extension as described in Section 3 of [RESET-STREAM-AT]. (3.1)
92 %% @todo reset_stream_at_enabled
93
94 %% 3.2. Extended CONNECT in HTTP/3
95
96 %% [RFC8441] defines an extended CONNECT method in Section 4, enabled by the SETTINGS_ENABLE_CONNECT_PROTOCOL setting. That setting is defined for HTTP/3 by [RFC9220]. A server supporting WebTransport over HTTP/3 MUST send both the SETTINGS_WT_MAX_SESSIONS setting with a value greater than "0" and the SETTINGS_ENABLE_CONNECT_PROTOCOL setting with a value of "1". (3.2)
97 %% @todo settings_enable_connect_protocol_enabled
98 %% @todo reject_settings_enable_connect_protocol_disabled
99
100 %% 3.3. Creating a New Session
101
102 %% As WebTransport sessions are established over HTTP/3, they are identified using the https URI scheme ([HTTP], Section 4.2.2). (3.3)
103
104 %% In order to create a new WebTransport session, a client can send an HTTP CONNECT request. The :protocol pseudo-header field ([RFC8441]) MUST be set to webtransport. The :scheme field MUST be https. Both the :authority and the :path value MUST be set; those fields indicate the desired WebTransport server. If the WebTransport session is coming from a browser client, an Origin header [RFC6454] MUST be provided within the request; otherwise, the header is OPTIONAL. (3.3)
105
106 %% If it does not (have a WT server), it SHOULD reply with status code 404 (Section 15.5.5 of [HTTP]). (3.3)
107
108 %% When the request contains the Origin header, the WebTransport server MUST verify the Origin header to ensure that the specified origin is allowed to access the server in question. If the verification fails, the WebTransport server SHOULD reply with status code 403 (Section 15.5.4 of [HTTP]). (3.3)
109
110 accept_session_when_enabled(Config) ->
111 doc("Confirm that a WebTransport session can be established over HTTP/3. "
112 "(draft_webtrans_http3 3.3, RFC9220)"),
113 %% Connect to the WebTransport server.
114 #{
115 conn := Conn,
116 session_id := SessionID
117 } = do_webtransport_connect(Config),
118 %% Create a bidi stream, send Hello, get Hello back.
119 {ok, BidiStreamRef} = quicer:start_stream(Conn, #{}),
120 {ok, _} = quicer:send(BidiStreamRef, <<1:2, 16#41:14, 0:2, SessionID:6, "Hello">>),
121 {nofin, <<"Hello">>} = do_receive_data(BidiStreamRef),
122 ok.
123
124 %% If the server accepts 0-RTT, the server MUST NOT reduce the limit of maximum open WebTransport sessions from the one negotiated during the previous session; such change would be deemed incompatible, and MUST result in a H3_SETTINGS_ERROR connection error. (3.3)
125
126 %% The capsule-protocol header field Section 3.4 of [HTTP-DATAGRAM] is not required by WebTransport and can safely be ignored by WebTransport endpoints. (3.3)
127
128 %% 3.4. Application Protocol Negotiation
129
130 application_protocol_negotiation(Config) ->
131 doc("Applications can negotiate a protocol to use via WebTransport. "
132 "(draft_webtrans_http3 3.4)"),
133 %% Connect to the WebTransport server.
134 WTAvailableProtocols = cow_http_hd:wt_available_protocols([<<"foo">>, <<"bar">>]),
135 #{
136 resp_headers := RespHeaders
137 } = do_webtransport_connect(Config, [{<<"wt-available-protocols">>, WTAvailableProtocols}]),
138 {<<"wt-protocol">>, WTProtocol} = lists:keyfind(<<"wt-protocol">>, 1, RespHeaders),
139 <<"foo">> = iolist_to_binary(cow_http_hd:parse_wt_protocol(WTProtocol)),
140 ok.
141
142 %% Both WT-Available-Protocols and WT-Protocol are Structured Fields [RFC8941]. WT-Available-Protocols is a List of Tokens, and WT-Protocol is a Token. The token in the WT-Protocol response header field MUST be one of the tokens listed in WT-Available-Protocols of the request. (3.4)
143
144 %% @todo 3.5 Prioritization
145
146 %% 4. WebTransport Features
147
148 %% The client MAY optimistically open unidirectional and bidirectional streams, as well as send datagrams, for a session that it has sent the CONNECT request for, even if it has not yet received the server's response to the request. (4)
149
150 %% If at any point a session ID is received that cannot be a valid ID for a client-initiated bidirectional stream, the recipient MUST close the connection with an H3_ID_ERROR error code. (4)
151 %% @todo Open bidi with Session ID 0, then do the CONNECT request.
152
153 %% 4.1. Unidirectional streams
154
155 unidirectional_streams(Config) ->
156 doc("Both endpoints can open and use unidirectional streams. "
157 "(draft_webtrans_http3 4.1)"),
158 %% Connect to the WebTransport server.
159 #{
160 conn := Conn,
161 session_id := SessionID
162 } = do_webtransport_connect(Config),
163 %% Create a unidi stream, send Hello with a Fin flag.
164 {ok, LocalStreamRef} = quicer:start_stream(Conn,
165 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
166 {ok, _} = quicer:send(LocalStreamRef,
167 <<1:2, 16#54:14, 0:2, SessionID:6, "Hello">>,
168 ?QUIC_SEND_FLAG_FIN),
169 %% Accept an identical unidi stream.
170 {unidi, RemoteStreamRef} = do_receive_new_stream(),
171 {nofin, <<1:2, 16#54:14, 0:2, SessionID:6>>} = do_receive_data(RemoteStreamRef),
172 {fin, <<"Hello">>} = do_receive_data(RemoteStreamRef),
173 ok.
174
175 %% 4.2. Bidirectional Streams
176
177 bidirectional_streams_client(Config) ->
178 doc("The WT client can open and use bidirectional streams. "
179 "(draft_webtrans_http3 4.2)"),
180 %% Connect to the WebTransport server.
181 #{
182 conn := Conn,
183 session_id := SessionID
184 } = do_webtransport_connect(Config),
185 %% Create a bidi stream, send Hello, get Hello back.
186 {ok, LocalStreamRef} = quicer:start_stream(Conn, #{}),
187 {ok, _} = quicer:send(LocalStreamRef, <<1:2, 16#41:14, 0:2, SessionID:6, "Hello">>),
188 {nofin, <<"Hello">>} = do_receive_data(LocalStreamRef),
189 ok.
190
191 bidirectional_streams_server(Config) ->
192 doc("The WT server can open and use bidirectional streams. "
193 "(draft_webtrans_http3 4.2)"),
194 %% Connect to the WebTransport server.
195 #{
196 conn := Conn,
197 session_id := SessionID
198 } = do_webtransport_connect(Config),
199 %% Create a bidi stream, send a special instruction
200 %% to make the server create another bidi stream.
201 {ok, LocalStreamRef} = quicer:start_stream(Conn, #{}),
202 {ok, _} = quicer:send(LocalStreamRef, <<1:2, 16#41:14, 0:2, SessionID:6, "TEST:open_bidi">>),
203 %% Accept the bidi stream and receive the data.
204 {bidi, RemoteStreamRef} = do_receive_new_stream(),
205 {nofin, <<1:2, 16#41:14, 0:2, SessionID:6>>} = do_receive_data(RemoteStreamRef),
206 {ok, _} = quicer:send(RemoteStreamRef, <<"Hello">>,
207 ?QUIC_SEND_FLAG_FIN),
208 {fin, <<"Hello">>} = do_receive_data(RemoteStreamRef),
209 ok.
210
211 %% Endpoints MUST NOT send WT_STREAM as a frame type on HTTP/3 streams other than the very first bytes of a request stream. Receiving this frame type in any other circumstances MUST be treated as a connection error of type H3_FRAME_ERROR. (4.2)
212
213 %% 4.3. Resetting Data Streams
214
215 %% A WebTransport endpoint may send a RESET_STREAM or a STOP_SENDING frame for a WebTransport data stream. Those signals are propagated by the WebTransport implementation to the application. (4.3)
216
217 %% A WebTransport application SHALL provide an error code for those operations. (4.3)
218
219 %% WebTransport implementations MUST use the RESET_STREAM_AT frame [RESET-STREAM-AT] with a Reliable Size set to at least the size of the WebTransport header when resetting a WebTransport data stream. This ensures that the ID field associating the data stream with a WebTransport session is always delivered. (4.3)
220
221 %% WebTransport implementations SHALL forward the error code for a stream associated with a known session to the application that owns that session (4.3)
222
223 %% 4.4. Datagrams
224
225 datagrams(Config) ->
226 doc("Both endpoints can send and receive datagrams. (draft_webtrans_http3 4.4)"),
227 %% Connect to the WebTransport server.
228 #{
229 conn := Conn,
230 session_id := SessionID
231 } = do_webtransport_connect(Config),
232 QuarterID = SessionID div 4,
233 %% Send a Hello datagram.
234 {ok, _} = quicer:send_dgram(Conn, <<0:2, QuarterID:6, "Hello">>),
235 %% Receive a Hello datagram back.
236 {datagram, SessionID, <<"Hello">>} = do_receive_datagram(Conn),
237 ok.
238
239 %% @todo datagrams_via_capsule?
240
241 %% 4.5. Buffering Incoming Streams and Datagrams
242
243 %% To handle this case (out of order stream_open/CONNECT), WebTransport endpoints SHOULD buffer streams and datagrams until those can be associated with an established session. (4.5)
244
245 %% To avoid resource exhaustion, the endpoints MUST limit the number of buffered streams and datagrams. When the number of buffered streams is exceeded, a stream SHALL be closed by sending a RESET_STREAM and/or STOP_SENDING with the WT_BUFFERED_STREAM_REJECTED error code. When the number of buffered datagrams is exceeded, a datagram SHALL be dropped. It is up to an implementation to choose what stream or datagram to discard. (4.5)
246
247 %% 4.6. Interaction with HTTP/3 GOAWAY frame
248
249 %% A client receiving GOAWAY cannot initiate CONNECT requests for new WebTransport sessions on that HTTP/3 connection; it must open a new HTTP/3 connection to initiate new WebTransport sessions with the same peer. (4.6)
250
251 %% An HTTP/3 GOAWAY frame is also a signal to applications to initiate shutdown for all WebTransport sessions. (4.6)
252
253 %% @todo Currently receipt of a GOAWAY frame immediately ends the connection.
254 %% We want to allow WT sessions to gracefully shut down before that.
255 %goaway_client(Config) ->
256 % doc("The HTTP/3 client can initiate the close of all WT sessions "
257 % "by sending a GOAWAY frame. (draft_webtrans_http3 4.6)"),
258 % %% Connect to the WebTransport server.
259 % #{
260 % conn := Conn,
261 % connect_stream_ref := ConnectStreamRef,
262 % session_id := SessionID
263 % } = do_webtransport_connect(Config),
264 % %% Open a control stream and send a GOAWAY frame.
265 % {ok, ControlRef} = quicer:start_stream(Conn,
266 % #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
267 % {ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
268 % {ok, _} = quicer:send(ControlRef, [
269 % <<0>>, %% CONTROL stream.
270 % SettingsBin,
271 % <<7>>, %% GOAWAY frame.
272 % cow_http3:encode_int(1),
273 % cow_http3:encode_int(0)
274 % ]),
275 % %% Receive a datagram indicating processing by the WT handler.
276 % {datagram, SessionID, <<"TEST:close_initiated">>} = do_receive_datagram(Conn),
277 % ok.
278
279 wt_drain_session_client(Config) ->
280 doc("The WT client can initiate the close of a single session. "
281 "(draft_webtrans_http3 4.6)"),
282 %% Connect to the WebTransport server.
283 #{
284 conn := Conn,
285 connect_stream_ref := ConnectStreamRef,
286 session_id := SessionID
287 } = do_webtransport_connect(Config),
288 %% Send the WT_DRAIN_SESSION capsule on the CONNECT stream.
289 {ok, _} = quicer:send(ConnectStreamRef, cow_capsule:wt_drain_session()),
290 %% Receive a datagram indicating processing by the WT handler.
291 {datagram, SessionID, <<"TEST:close_initiated">>} = do_receive_datagram(Conn),
292 ok.
293
294 wt_drain_session_server(Config) ->
295 doc("The WT server can initiate the close of a single session. "
296 "(draft_webtrans_http3 4.6)"),
297 %% Connect to the WebTransport server.
298 #{
299 conn := Conn,
300 connect_stream_ref := ConnectStreamRef,
301 session_id := SessionID
302 } = do_webtransport_connect(Config),
303 %% Create a bidi stream, send a special instruction to make it initiate the close.
304 {ok, LocalStreamRef} = quicer:start_stream(Conn, #{}),
305 {ok, _} = quicer:send(LocalStreamRef, <<1:2, 16#41:14, 0:2, SessionID:6, "TEST:initiate_close">>),
306 %% Receive the WT_DRAIN_SESSION capsule on the CONNECT stream.
307 DrainWTSessionCapsule = cow_capsule:wt_drain_session(),
308 {nofin, DrainWTSessionCapsule} = do_receive_data(ConnectStreamRef),
309 ok.
310
311 wt_drain_session_continue_client(Config) ->
312 doc("After the WT client has initiated the close of the session, "
313 "both client and server can continue using the session and "
314 "open new streams. (draft_webtrans_http3 4.6)"),
315 %% Connect to the WebTransport server.
316 #{
317 conn := Conn,
318 connect_stream_ref := ConnectStreamRef,
319 session_id := SessionID
320 } = do_webtransport_connect(Config),
321 %% Send the WT_DRAIN_SESSION capsule on the CONNECT stream.
322 {ok, _} = quicer:send(ConnectStreamRef, cow_capsule:wt_drain_session()),
323 %% Receive a datagram indicating processing by the WT handler.
324 {datagram, SessionID, <<"TEST:close_initiated">>} = do_receive_datagram(Conn),
325 %% Create a new bidi stream, send Hello, get Hello back.
326 {ok, ContinueStreamRef} = quicer:start_stream(Conn, #{}),
327 {ok, _} = quicer:send(ContinueStreamRef, <<1:2, 16#41:14, 0:2, SessionID:6, "Hello">>),
328 {nofin, <<"Hello">>} = do_receive_data(ContinueStreamRef),
329 ok.
330
331 wt_drain_session_continue_server(Config) ->
332 doc("After the WT server has initiated the close of the session, "
333 "both client and server can continue using the session and "
334 "open new streams. (draft_webtrans_http3 4.6)"),
335 %% Connect to the WebTransport server.
336 #{
337 conn := Conn,
338 connect_stream_ref := ConnectStreamRef,
339 session_id := SessionID
340 } = do_webtransport_connect(Config),
341 %% Create a bidi stream, send a special instruction to make it initiate the close.
342 {ok, LocalStreamRef} = quicer:start_stream(Conn, #{}),
343 {ok, _} = quicer:send(LocalStreamRef, <<1:2, 16#41:14, 0:2, SessionID:6, "TEST:initiate_close">>),
344 %% Receive the WT_DRAIN_SESSION capsule on the CONNECT stream.
345 DrainWTSessionCapsule = cow_capsule:wt_drain_session(),
346 {nofin, DrainWTSessionCapsule} = do_receive_data(ConnectStreamRef),
347 %% Create a new bidi stream, send Hello, get Hello back.
348 {ok, ContinueStreamRef} = quicer:start_stream(Conn, #{}),
349 {ok, _} = quicer:send(ContinueStreamRef, <<1:2, 16#41:14, 0:2, SessionID:6, "Hello">>),
350 {nofin, <<"Hello">>} = do_receive_data(ContinueStreamRef),
351 ok.
352
353 %% @todo 4.7. Use of Keying Material Exporters
354
355 %% 5. Flow Control
356
357 %% 5.1. Limiting the Number of Simultaneous Sessions
358
359 %% This document defines a SETTINGS_WT_MAX_SESSIONS parameter that allows the server to limit the maximum number of concurrent WebTransport sessions on a single HTTP/3 connection. The client MUST NOT open more simultaneous sessions than indicated in the server SETTINGS parameter. The server MUST NOT close the connection if the client opens sessions exceeding this limit, as the client and the server do not have a consistent view of how many sessions are open due to the asynchronous nature of the protocol; instead, it MUST reset all of the CONNECT streams it is not willing to process with the H3_REQUEST_REJECTED status defined in [HTTP3]. (5.1)
360
361 %% 5.2. Limiting the Number of Streams Within a Session
362
363 %% The WT_MAX_STREAMS capsule (Section 5.6.1) establishes a limit on the number of streams within a WebTransport session. (5.2)
364
365 %% Note that the CONNECT stream for the session is not included in either the bidirectional or the unidirectional stream limits (5.2)
366
367 %% The session-level stream limit applies in addition to the QUIC MAX_STREAMS frame, which provides a connection-level stream limit. New streams can only be created within the session if both the stream- and the connection-level limit permit (5.2)
368
369 %% The WT_STREAMS_BLOCKED capsule (Section 5.7) can be sent to indicate that an endpoint was unable to create a stream due to the session-level stream limit. (5.2)
370
371 %% Note that enforcing this limit requires reliable resets for stream headers so that both endpoints can agree on the number of streams that are open. (5.2)
372
373 %% 5.3. Data Limits
374
375 %% The WT_MAX_DATA capsule (Section 5.8) establishes a limit on the amount of data that can be sent within a WebTransport session. This limit counts all data that is sent on streams of the corresponding type, excluding the stream header (see Section 4.1 and Section 4.2). (5.3)
376
377 %% Implementing WT_MAX_DATA requires that the QUIC stack provide the WebTransport implementation with information about the final size of streams; see { {Section 4.5 of !RFC9000}}. This allows both endpoints to agree on how much data was consumed by that stream, although the stream header exclusion above applies. (5.3)
378
379 %% The WT_DATA_BLOCKED capsule (Section 5.9) can be sent to indicate that an endpoint was unable to send data due to a limit set by the WT_MAX_DATA capsule. (5.3)
380
381 %% The WT_MAX_STREAM_DATA and WT_STREAM_DATA_BLOCKED capsules (Part XX of [I-D.ietf-webtrans-http2]) are not used and so are prohibited. Endpoints MUST treat receipt of a WT_MAX_STREAM_DATA or a WT_STREAM_DATA_BLOCKED capsule as a session error. (5.3)
382
383 %% 5.4. Flow Control and Intermediaries
384
385 %% In practice, an intermediary that translates flow control signals between similar WebTransport protocols, such as between two HTTP/3 connections, can often simply reexpress the same limits received on one connection directly on the other connection. (5.4)
386
387 %% 5.5. Flow Control SETTINGS
388
389 %% WT_MAX_STREAMS via SETTINGS_WT_INITIAL_MAX_STREAMS_UNI and SETTINGS_WT_INITIAL_MAX_STREAMS_BIDI (5.5)
390
391 %% WT_MAX_DATA via SETTINGS_WT_INITIAL_MAX_DATA (5.5)
392
393 %% 5.6. Flow Control Capsules
394
395 %% 5.6.1. WT_MAX_STREAMS Capsule
396
397 %% An HTTP capsule [HTTP-DATAGRAM] called WT_MAX_STREAMS is introduced to inform the peer of the cumulative number of streams of a given type it is permitted to open. A WT_MAX_STREAMS capsule with a type of 0x190B4D3F applies to bidirectional streams, and a WT_MAX_STREAMS capsule with a type of 0x190B4D40 applies to unidirectional streams. (5.6.1)
398
399 %% Note that, because Maximum Streams is a cumulative value representing the total allowed number of streams, including previously closed streams, endpoints repeatedly send new WT_MAX_STREAMS capsules with increasing Maximum Streams values as streams are opened. (5.6.1)
400
401 %% Maximum Streams: A count of the cumulative number of streams of the corresponding type that can be opened over the lifetime of the session. This value cannot exceed 260, as it is not possible to encode stream IDs larger than 262-1. (5.6.1)
402
403 %% An endpoint MUST NOT open more streams than permitted by the current stream limit set by its peer. (5.6.1)
404
405 %% Note that this limit includes streams that have been closed as well as those that are open. (5.6.1)
406
407 %% Initial values for these limits MAY be communicated by sending non-zero values for SETTINGS_WT_INITIAL_MAX_STREAMS_UNI and SETTINGS_WT_INITIAL_MAX_STREAMS_BIDI. (5.6.1)
408
409 %% 5.7. WT_STREAMS_BLOCKED Capsule
410
411 %% A sender SHOULD send a WT_STREAMS_BLOCKED capsule (type=0x190B4D43 for bidi or 0x190B4D44 for unidi) when it wishes to open a stream but is unable to do so due to the maximum stream limit set by its peer. (5.7)
412
413 %% 5.8. WT_MAX_DATA Capsule
414
415 %% An HTTP capsule [HTTP-DATAGRAM] called WT_MAX_DATA (type=0x190B4D3D) is introduced to inform the peer of the maximum amount of data that can be sent on the WebTransport session as a whole. (5.8)
416
417 %% This limit counts all data that is sent on streams of the corresponding type, excluding the stream header (see Section 4.1 and Section 4.2). Implementing WT_MAX_DATA requires that the QUIC stack provide the WebTransport implementation with information about the final size of streams; see Section 4.5 of [RFC9000]. (5.8)
418
419 %% All data sent in WT_STREAM capsules counts toward this limit. The sum of the lengths of Stream Data fields in WT_STREAM capsules MUST NOT exceed the value advertised by a receiver. (5.8)
420
421 %% The initial value for this limit MAY be communicated by sending a non-zero value for SETTINGS_WT_INITIAL_MAX_DATA. (5.8)
422
423 %% 5.9. WT_DATA_BLOCKED Capsule
424
425 %% A sender SHOULD send a WT_DATA_BLOCKED capsule (type=0x190B4D41) when it wishes to send data but is unable to do so due to WebTransport session-level flow control. (5.9)
426
427 %% WT_DATA_BLOCKED capsules can be used as input to tuning of flow control algorithms. (5.9)
428
429 %% 6. Session Termination
430
431 %% A WebTransport session over HTTP/3 is considered terminated when either of the following conditions is met:
432 %% * the CONNECT stream is closed, either cleanly or abruptly, on either side; or
433 %% * a WT_CLOSE_SESSION capsule is either sent or received.
434 %% (6)
435
436 wt_close_session_client(Config) ->
437 doc("The WT client can close a single session. (draft_webtrans_http3 4.6)"),
438 %% Connect to the WebTransport server.
439 #{
440 connect_stream_ref := ConnectStreamRef
441 } = do_webtransport_connect(Config),
442 %% Send the WT_CLOSE_SESSION capsule on the CONNECT stream.
443 {ok, _} = quicer:send(ConnectStreamRef,
444 cow_capsule:wt_close_session(0, <<>>),
445 ?QUIC_SEND_FLAG_FIN),
446 %% Normally we should also stop reading but in order to detect
447 %% that the server stops the stream we must not otherwise the
448 %% stream will be de facto closed on our end.
449 %%
450 %% The recipient must close or reset the stream in response.
451 receive
452 {quic, stream_closed, ConnectStreamRef, _} ->
453 ok
454 after 1000 ->
455 error({timeout, waiting_for_stream_closed})
456 end.
457
458 wt_close_session_server(Config) ->
459 doc("The WT server can close a single session. (draft_webtrans_http3 4.6)"),
460 %% Connect to the WebTransport server.
461 #{
462 conn := Conn,
463 connect_stream_ref := ConnectStreamRef,
464 session_id := SessionID
465 } = do_webtransport_connect(Config),
466 %% Create a bidi stream, send a special instruction to make it initiate the close.
467 {ok, LocalStreamRef} = quicer:start_stream(Conn, #{}),
468 {ok, _} = quicer:send(LocalStreamRef, <<1:2, 16#41:14, 0:2, SessionID:6, "TEST:close">>),
469 %% Receive the WT_CLOSE_SESSION capsule on the CONNECT stream.
470 CloseWTSessionCapsule = cow_capsule:wt_close_session(0, <<>>),
471 {fin, CloseWTSessionCapsule} = do_receive_data(ConnectStreamRef),
472 ok.
473
474 wt_session_gone_client(Config) ->
475 doc("Upon learning that the session has been terminated, "
476 "the WT server must reset associated streams with the "
477 "WEBTRANSPORT_SESSION_GONE error code. (draft_webtrans_http3 4.6)"),
478 %% Connect to the WebTransport server.
479 #{
480 conn := Conn,
481 connect_stream_ref := ConnectStreamRef,
482 session_id := SessionID
483 } = do_webtransport_connect(Config),
484 %% Create a unidi stream.
485 {ok, LocalUnidiStreamRef} = quicer:start_stream(Conn,
486 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
487 {ok, _} = quicer:send(LocalUnidiStreamRef,
488 <<1:2, 16#54:14, 0:2, SessionID:6, "Hello">>),
489 %% Accept an identical unidi stream.
490 {unidi, RemoteUnidiStreamRef} = do_receive_new_stream(),
491 {nofin, <<1:2, 16#54:14, 0:2, SessionID:6>>} = do_receive_data(RemoteUnidiStreamRef),
492 {nofin, <<"Hello">>} = do_receive_data(RemoteUnidiStreamRef),
493 %% Create a bidi stream, send a special instruction
494 %% to make the server create another bidi stream.
495 {ok, LocalBidiStreamRef} = quicer:start_stream(Conn, #{}),
496 {ok, _} = quicer:send(LocalBidiStreamRef, <<1:2, 16#41:14, 0:2, SessionID:6, "TEST:open_bidi">>),
497 %% Accept the bidi stream and receive the data.
498 {bidi, RemoteBidiStreamRef} = do_receive_new_stream(),
499 {nofin, <<1:2, 16#41:14, 0:2, SessionID:6>>} = do_receive_data(RemoteBidiStreamRef),
500 {ok, _} = quicer:send(RemoteBidiStreamRef, <<"Hello">>),
501 {nofin, <<"Hello">>} = do_receive_data(RemoteBidiStreamRef),
502 %% Send the WT_CLOSE_SESSION capsule on the CONNECT stream.
503 {ok, _} = quicer:send(ConnectStreamRef,
504 cow_capsule:wt_close_session(0, <<>>),
505 ?QUIC_SEND_FLAG_FIN),
506 %% All streams from that WT session have been aborted.
507 #{reason := wt_session_gone} = do_wait_stream_aborted(LocalUnidiStreamRef),
508 #{reason := wt_session_gone} = do_wait_stream_aborted(RemoteUnidiStreamRef),
509 #{reason := wt_session_gone} = do_wait_stream_aborted(LocalBidiStreamRef),
510 #{reason := wt_session_gone} = do_wait_stream_aborted(RemoteBidiStreamRef),
511 ok.
512
513 wt_session_gone_server(Config) ->
514 doc("After the session has been terminated by the WT server, "
515 "the WT server must reset associated streams with the "
516 "WT_SESSION_GONE error code. (draft_webtrans_http3 4.6)"),
517 %% Connect to the WebTransport server.
518 #{
519 conn := Conn,
520 connect_stream_ref := ConnectStreamRef,
521 session_id := SessionID
522 } = do_webtransport_connect(Config),
523 %% Create a unidi stream.
524 {ok, LocalUnidiStreamRef} = quicer:start_stream(Conn,
525 #{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
526 {ok, _} = quicer:send(LocalUnidiStreamRef,
527 <<1:2, 16#54:14, 0:2, SessionID:6, "Hello">>),
528 %% Accept an identical unidi stream.
529 {unidi, RemoteUnidiStreamRef} = do_receive_new_stream(),
530 {nofin, <<1:2, 16#54:14, 0:2, SessionID:6>>} = do_receive_data(RemoteUnidiStreamRef),
531 {nofin, <<"Hello">>} = do_receive_data(RemoteUnidiStreamRef),
532 %% Create a bidi stream, send a special instruction
533 %% to make the server create another bidi stream.
534 {ok, LocalBidiStreamRef} = quicer:start_stream(Conn, #{}),
535 {ok, _} = quicer:send(LocalBidiStreamRef, <<1:2, 16#41:14, 0:2, SessionID:6, "TEST:open_bidi">>),
536 %% Accept the bidi stream and receive the data.
537 {bidi, RemoteBidiStreamRef} = do_receive_new_stream(),
538 {nofin, <<1:2, 16#41:14, 0:2, SessionID:6>>} = do_receive_data(RemoteBidiStreamRef),
539 {ok, _} = quicer:send(RemoteBidiStreamRef, <<"Hello">>),
540 {nofin, <<"Hello">>} = do_receive_data(RemoteBidiStreamRef),
541
542 %% Send a special instruction to make the server initiate the close.
543 {ok, _} = quicer:send(LocalBidiStreamRef, <<"TEST:close">>),
544 %% Receive the WT_CLOSE_SESSION capsule on the CONNECT stream.
545 CloseWTSessionCapsule = cow_capsule:wt_close_session(0, <<>>),
546 {fin, CloseWTSessionCapsule} = do_receive_data(ConnectStreamRef),
547 %% All streams from that WT session have been aborted.
548 #{reason := wt_session_gone} = do_wait_stream_aborted(LocalUnidiStreamRef),
549 #{reason := wt_session_gone} = do_wait_stream_aborted(RemoteUnidiStreamRef),
550 #{reason := wt_session_gone} = do_wait_stream_aborted(LocalBidiStreamRef),
551 #{reason := wt_session_gone} = do_wait_stream_aborted(RemoteBidiStreamRef),
552 ok.
553
554 %% Application Error Message: A UTF-8 encoded error message string provided by the application closing the session. The message takes up the remainder of the capsule, and its length MUST NOT exceed 1024 bytes. (6)
555 %% @todo What if it's larger?
556
557 wt_close_session_app_code_msg_client(Config) ->
558 doc("The WT client can close a single session with an application error code "
559 "and an application error message. (draft_webtrans_http3 4.6)"),
560 %% Connect to the WebTransport server.
561 #{
562 conn := Conn,
563 connect_stream_ref := ConnectStreamRef,
564 session_id := SessionID
565 } = do_webtransport_connect(Config),
566 %% Create a bidi stream, send a special instruction to make it propagate events.
567 {ok, LocalStreamRef} = quicer:start_stream(Conn, #{}),
568 EventPidBin = term_to_binary(self()),
569 {ok, _} = quicer:send(LocalStreamRef, <<1:2, 16#41:14, 0:2, SessionID:6,
570 "TEST:event_pid:", EventPidBin/binary>>),
571 %% Send the WT_CLOSE_SESSION capsule on the CONNECT stream.
572 {ok, _} = quicer:send(ConnectStreamRef,
573 cow_capsule:wt_close_session(17, <<"seventeen">>),
574 ?QUIC_SEND_FLAG_FIN),
575 %% @todo Stop reading from the CONNECt stream too. (STOP_SENDING)
576 %% Receive the terminate event from the WT handler.
577 receive
578 {'$wt_echo_h', terminate, {closed, 17, <<"seventeen">>}, _, _} ->
579 ok
580 after 1000 ->
581 error({timeout, waiting_for_terminate_event})
582 end.
583
584 wt_close_session_app_code_server(Config) ->
585 doc("The WT server can close a single session with an application error code. "
586 "(draft_webtrans_http3 4.6)"),
587 %% Connect to the WebTransport server.
588 #{
589 conn := Conn,
590 connect_stream_ref := ConnectStreamRef,
591 session_id := SessionID
592 } = do_webtransport_connect(Config),
593 %% Create a bidi stream, send a special instruction to make it initiate the close.
594 {ok, LocalStreamRef} = quicer:start_stream(Conn, #{}),
595 {ok, _} = quicer:send(LocalStreamRef, <<1:2, 16#41:14, 0:2, SessionID:6,
596 "TEST:close_app_code">>),
597 %% Receive the WT_CLOSE_SESSION capsule on the CONNECT stream.
598 CloseWTSessionCapsule = cow_capsule:wt_close_session(1234567890, <<>>),
599 {fin, CloseWTSessionCapsule} = do_receive_data(ConnectStreamRef),
600 ok.
601
602 wt_close_session_app_code_msg_server(Config) ->
603 doc("The WT server can close a single session with an application error code "
604 "and an application error message. (draft_webtrans_http3 4.6)"),
605 %% Connect to the WebTransport server.
606 #{
607 conn := Conn,
608 connect_stream_ref := ConnectStreamRef,
609 session_id := SessionID
610 } = do_webtransport_connect(Config),
611 %% Create a bidi stream, send a special instruction to make it initiate the close.
612 {ok, LocalStreamRef} = quicer:start_stream(Conn, #{}),
613 {ok, _} = quicer:send(LocalStreamRef, <<1:2, 16#41:14, 0:2, SessionID:6,
614 "TEST:close_app_code_msg">>),
615 %% Receive the WT_CLOSE_SESSION capsule on the CONNECT stream.
616 CloseWTSessionCapsule = iolist_to_binary(cow_capsule:wt_close_session(1234567890,
617 <<"onetwothreefourfivesixseveneightnineten">>)),
618 {fin, CloseWTSessionCapsule} = do_receive_data(ConnectStreamRef),
619 ok.
620
621 %% An endpoint that sends a WT_CLOSE_SESSION capsule MUST immediately send a FIN. The endpoint MAY send a STOP_SENDING to indicate it is no longer reading from the CONNECT stream. The recipient MUST either close or reset the stream in response. (6)
622 %% @todo wt_close_session_server_fin
623 %% @todo The part about close/reset should be tested in wt_close_session_client.
624
625 %% If any additional stream data is received on the CONNECT stream after receiving a WT_CLOSE_SESSION capsule, the stream MUST be reset with code H3_MESSAGE_ERROR. (6)
626 %% @todo wt_close_session_followed_by_data
627
628 connect_stream_closed_cleanly_fin(Config) ->
629 doc("The WT client closing the CONNECT stream cleanly "
630 "is equivalent to a capsule with an application error code of 0 "
631 "and an empty error string. (draft_webtrans_http3 4.6)"),
632 %% Connect to the WebTransport server.
633 #{
634 conn := Conn,
635 connect_stream_ref := ConnectStreamRef,
636 session_id := SessionID
637 } = do_webtransport_connect(Config),
638 %% Create a bidi stream, send a special instruction to make it propagate events.
639 {ok, LocalStreamRef} = quicer:start_stream(Conn, #{}),
640 EventPidBin = term_to_binary(self()),
641 {ok, _} = quicer:send(LocalStreamRef, <<1:2, 16#41:14, 0:2, SessionID:6,
642 "TEST:event_pid:", EventPidBin/binary>>),
643 {nofin, <<"event_pid_received">>} = do_receive_data(LocalStreamRef),
644 %% Cleanly terminate the CONNECT stream.
645 {ok, _} = quicer:send(ConnectStreamRef, <<>>, ?QUIC_SEND_FLAG_FIN),
646 %% Receive the terminate event from the WT handler.
647 receive
648 {'$wt_echo_h', terminate, {closed, 0, <<>>}, _, _} ->
649 ok
650 after 1000 ->
651 error({timeout, waiting_for_terminate_event})
652 end.
653
654 connect_stream_closed_cleanly_shutdown(Config) ->
655 doc("The WT client closing the CONNECT stream cleanly "
656 "is equivalent to a capsule with an application error code of 0 "
657 "and an empty error string. (draft_webtrans_http3 4.6)"),
658 %% Connect to the WebTransport server.
659 #{
660 conn := Conn,
661 connect_stream_ref := ConnectStreamRef,
662 session_id := SessionID
663 } = do_webtransport_connect(Config),
664 %% Create a bidi stream, send a special instruction to make it propagate events.
665 {ok, LocalStreamRef} = quicer:start_stream(Conn, #{}),
666 EventPidBin = term_to_binary(self()),
667 {ok, _} = quicer:send(LocalStreamRef, <<1:2, 16#41:14, 0:2, SessionID:6,
668 "TEST:event_pid:", EventPidBin/binary>>),
669 {nofin, <<"event_pid_received">>} = do_receive_data(LocalStreamRef),
670 %% Cleanly terminate the CONNECT stream.
671 _ = quicer:shutdown_stream(ConnectStreamRef),
672 %% Receive the terminate event from the WT handler.
673 receive
674 {'$wt_echo_h', terminate, {closed, 0, <<>>}, _, _} ->
675 ok
676 after 1000 ->
677 error({timeout, waiting_for_terminate_event})
678 end.
679
680 connect_stream_closed_abruptly(Config) ->
681 doc("The WT client may close the CONNECT stream abruptly. "
682 "(draft_webtrans_http3 4.6)"),
683 %% Connect to the WebTransport server.
684 #{
685 conn := Conn,
686 connect_stream_ref := ConnectStreamRef,
687 session_id := SessionID
688 } = do_webtransport_connect(Config),
689 %% Create a bidi stream, send a special instruction to make it propagate events.
690 {ok, LocalStreamRef} = quicer:start_stream(Conn, #{}),
691 EventPidBin = term_to_binary(self()),
692 {ok, _} = quicer:send(LocalStreamRef, <<1:2, 16#41:14, 0:2, SessionID:6,
693 "TEST:event_pid:", EventPidBin/binary>>),
694 {nofin, <<"event_pid_received">>} = do_receive_data(LocalStreamRef),
695 %% Abruptly terminate the CONNECT stream.
696 _ = quicer:shutdown_stream(ConnectStreamRef, ?QUIC_STREAM_SHUTDOWN_FLAG_ABORT,
697 0, infinity),
698 %% Receive the terminate event from the WT handler.
699 receive
700 %% @todo It would be good to forward a stream error as well
701 %% so that a WT error can be sent, but I have been unsuccessful.
702 {'$wt_echo_h', terminate, closed_abruptly, _, _} ->
703 ok
704 after 1000 ->
705 error({timeout, waiting_for_terminate_event})
706 end.
707
708 %% @todo This one is about gracefully closing HTTP/3 connection with WT sessions.
709 %% the endpoint SHOULD wait until all CONNECT streams have been closed by the peer before sending the CONNECTION_CLOSE (6)
710
711 %% Helpers.
712
713 do_webtransport_connect(Config) ->
714 do_webtransport_connect(Config, []).
715
716 do_webtransport_connect(Config, ExtraHeaders) ->
717 %% Connect to server.
718 #{conn := Conn, settings := Settings} = rfc9114_SUITE:do_connect(Config, #{
719 peer_unidi_stream_count => 100,
720 datagram_send_enabled => 1,
721 datagram_receive_enabled => 1
722 }),
723 %% Confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
724 #{enable_connect_protocol := true} = Settings,
725 %% Confirm that SETTINGS_WT_MAX_SESSIONS >= 1.
726 #{wt_max_sessions := WTMaxSessions} = Settings,
727 true = WTMaxSessions >= 1,
728 %% Confirm that SETTINGS_H3_DATAGRAM = 1.
729 #{h3_datagram := true} = Settings,
730 %% Confirm that QUIC's max_datagram_size > 0.
731 receive {quic, dgram_state_changed, Conn, DatagramState} ->
732 #{
733 dgram_max_len := DatagramMaxLen,
734 dgram_send_enabled := DatagramSendEnabled
735 } = DatagramState,
736 true = DatagramMaxLen > 0,
737 true = DatagramSendEnabled,
738 ok
739 after 5000 ->
740 error({timeout, waiting_for_datagram_state_change})
741 end,
742 %% Send a CONNECT :protocol request to upgrade the stream to Websocket.
743 {ok, ConnectStreamRef} = quicer:start_stream(Conn, #{}),
744 {ok, EncodedRequest, _EncData, _EncSt} = cow_qpack:encode_field_section([
745 {<<":method">>, <<"CONNECT">>},
746 {<<":protocol">>, <<"webtransport">>},
747 {<<":scheme">>, <<"https">>},
748 {<<":path">>, <<"/wt">>},
749 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
750 {<<"origin">>, <<"https://localhost">>}
751 |ExtraHeaders], 0, cow_qpack:init(encoder)),
752 {ok, _} = quicer:send(ConnectStreamRef, [
753 <<1>>, %% HEADERS frame.
754 cow_http3:encode_int(iolist_size(EncodedRequest)),
755 EncodedRequest
756 ]),
757 %% Receive a 200 response.
758 {nofin, Data} = do_receive_data(ConnectStreamRef),
759 {HLenEnc, HLenBits} = rfc9114_SUITE:do_guess_int_encoding(Data),
760 <<
761 1, %% HEADERS frame.
762 HLenEnc:2, HLen:HLenBits,
763 EncodedResponse:HLen/bytes
764 >> = Data,
765 {ok, DecodedResponse, _DecData, _DecSt}
766 = cow_qpack:decode_field_section(EncodedResponse, 0, cow_qpack:init(decoder)),
767 #{<<":status">> := <<"200">>} = maps:from_list(DecodedResponse),
768 %% Retrieve the Session ID.
769 {ok, SessionID} = quicer:get_stream_id(ConnectStreamRef),
770 %% Accept QPACK streams to avoid conflicts with unidi streams from tests.
771 Unidi1 = rfc9114_SUITE:do_accept_qpack_stream(Conn),
772 Unidi2 = rfc9114_SUITE:do_accept_qpack_stream(Conn),
773 %% Done.
774 #{
775 conn => Conn,
776 connect_stream_ref => ConnectStreamRef,
777 session_id => SessionID,
778 resp_headers => DecodedResponse,
779 enc_or_dec1 => Unidi1,
780 enc_or_dec2 => Unidi2
781 }.
782
783 do_receive_new_stream() ->
784 receive
785 {quic, new_stream, StreamRef, #{flags := Flags}} ->
786 ok = quicer:setopt(StreamRef, active, true),
787 case quicer:is_unidirectional(Flags) of
788 true -> {unidi, StreamRef};
789 false -> {bidi, StreamRef}
790 end
791 after 5000 ->
792 error({timeout, waiting_for_stream})
793 end.
794
795 do_receive_data(StreamRef) ->
796 receive {quic, Data, StreamRef, #{flags := Flags}} ->
797 IsFin = case Flags band ?QUIC_RECEIVE_FLAG_FIN of
798 ?QUIC_RECEIVE_FLAG_FIN -> fin;
799 _ -> nofin
800 end,
801 {IsFin, Data}
802 after 5000 ->
803 error({timeout, waiting_for_data})
804 end.
805
806 do_receive_datagram(Conn) ->
807 receive {quic, <<0:2, QuarterID:6, Data/bits>>, Conn, Flags} when is_integer(Flags) ->
808 {datagram, QuarterID * 4, Data}
809 after 5000 ->
810 ct:pal("~p", [process_info(self(), messages)]),
811 error({timeout, waiting_for_datagram})
812 end.
813
814 -else.
815
816 all() -> [].
817
818 -endif.