Debian Codegraph @

test/rfc8441_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(rfc8441_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 all() -> [{group, enabled}].
23
24 groups() ->
25 Tests = ct_helper:all(?MODULE),
26 [{enabled, [parallel], Tests}].
27
28 init_per_group(Name = enabled, Config) ->
29 cowboy_test:init_http(Name, #{
30 enable_connect_protocol => true,
31 env => #{dispatch => cowboy_router:compile(init_routes(Config))}
32 }, Config).
33
34 end_per_group(Name, _) ->
35 ok = cowboy:stop_listener(Name).
36
37 init_routes(_) -> [
38 {"localhost", [
39 {"/ws", ws_echo, []}
40 ]}
41 ].
42
43 %% Do a prior knowledge handshake.
44 do_handshake(Config) ->
45 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
46 %% Send a valid preface.
47 ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
48 %% Receive the server preface.
49 {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
50 {ok, << 4:8, 0:40, SettingsPayload:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
51 Settings = cow_http2:parse_settings_payload(SettingsPayload),
52 %% Send the SETTINGS ack.
53 ok = gen_tcp:send(Socket, cow_http2:settings_ack()),
54 %% Receive the SETTINGS ack.
55 {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
56 {ok, Socket, Settings}.
57
58 % The SETTINGS_ENABLE_CONNECT_PROTOCOL SETTINGS Parameter.
59
60 % The new parameter name is SETTINGS_ENABLE_CONNECT_PROTOCOL. The
61 % value of the parameter MUST be 0 or 1.
62
63 % Upon receipt of SETTINGS_ENABLE_CONNECT_PROTOCOL with a value of 1 a
64 % client MAY use the Extended CONNECT definition of this document when
65 % creating new streams. Receipt of this parameter by a server does not
66 % have any impact.
67 %% @todo ignore_client_enable_setting(Config) ->
68
69 % A sender MUST NOT send a SETTINGS_ENABLE_CONNECT_PROTOCOL parameter
70 % with the value of 0 after previously sending a value of 1.
71
72 reject_handshake_when_disabled(Config0) ->
73 doc("Extended CONNECT requests MUST be rejected with a "
74 "PROTOCOL_ERROR stream error when enable_connect_protocol=false. (draft-01 3)"),
75 Config = cowboy_test:init_http(disabled, #{
76 enable_connect_protocol => false,
77 env => #{dispatch => cowboy_router:compile(init_routes(Config0))},
78 max_concurrent_streams => infinity
79 }, Config0),
80 %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 0.
81 {ok, Socket, Settings} = do_handshake(Config),
82 case Settings of
83 #{enable_connect_protocol := false} -> ok;
84 _ when map_size(Settings) =:= 0 -> ok
85 end,
86 %% Send a CONNECT :protocol request to upgrade the stream to Websocket.
87 {ReqHeadersBlock, _} = cow_hpack:encode([
88 {<<":method">>, <<"CONNECT">>},
89 {<<":protocol">>, <<"websocket">>},
90 {<<":scheme">>, <<"http">>},
91 {<<":path">>, <<"/ws">>},
92 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
93 {<<"sec-websocket-version">>, <<"13">>},
94 {<<"origin">>, <<"http://localhost">>}
95 ]),
96 ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
97 %% Receive a PROTOCOL_ERROR stream error.
98 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
99 ok.
100
101 reject_handshake_disabled_by_default(Config0) ->
102 doc("Extended CONNECT requests MUST be rejected with a "
103 "PROTOCOL_ERROR stream error with default enable_connect_protocol. (draft-01 3)"),
104 Config = cowboy_test:init_http(disabled_by_default, #{
105 env => #{dispatch => cowboy_router:compile(init_routes(Config0))},
106 max_concurrent_streams => infinity
107 }, Config0),
108 %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 0.
109 {ok, Socket, Settings} = do_handshake(Config),
110 case Settings of
111 #{enable_connect_protocol := false} -> ok;
112 _ when map_size(Settings) =:= 0 -> ok
113 end,
114 %% Send a CONNECT :protocol request to upgrade the stream to Websocket.
115 {ReqHeadersBlock, _} = cow_hpack:encode([
116 {<<":method">>, <<"CONNECT">>},
117 {<<":protocol">>, <<"websocket">>},
118 {<<":scheme">>, <<"http">>},
119 {<<":path">>, <<"/ws">>},
120 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
121 {<<"sec-websocket-version">>, <<"13">>},
122 {<<"origin">>, <<"http://localhost">>}
123 ]),
124 ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
125 %% Receive a PROTOCOL_ERROR stream error.
126 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
127 ok.
128
129 % The Extended CONNECT Method.
130
131 %% @todo Refer to RFC9110 7.8 about the case insensitive comparison.
132 accept_uppercase_pseudo_header_protocol(Config) ->
133 doc("The :protocol pseudo header is case insensitive. (draft-01 4)"),
134 %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
135 {ok, Socket, Settings} = do_handshake(Config),
136 #{enable_connect_protocol := true} = Settings,
137 %% Send a CONNECT :protocol request to upgrade the stream to Websocket.
138 {ReqHeadersBlock, _} = cow_hpack:encode([
139 {<<":method">>, <<"CONNECT">>},
140 {<<":protocol">>, <<"WEBSOCKET">>},
141 {<<":scheme">>, <<"http">>},
142 {<<":path">>, <<"/ws">>},
143 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
144 {<<"sec-websocket-version">>, <<"13">>},
145 {<<"origin">>, <<"http://localhost">>}
146 ]),
147 ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
148 %% Receive a 200 response.
149 {ok, << Len1:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
150 {ok, RespHeadersBlock} = gen_tcp:recv(Socket, Len1, 1000),
151 {RespHeaders, _} = cow_hpack:decode(RespHeadersBlock),
152 {_, <<"200">>} = lists:keyfind(<<":status">>, 1, RespHeaders),
153 ok.
154
155 reject_many_pseudo_header_protocol(Config) ->
156 doc("An extended CONNECT request containing more than one protocol component "
157 "must be rejected with a PROTOCOL_ERROR stream error. (draft-01 4, RFC7540 8.1.2.6)"),
158 %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
159 {ok, Socket, Settings} = do_handshake(Config),
160 #{enable_connect_protocol := true} = Settings,
161 %% Send an extended CONNECT request with more than one :protocol pseudo-header.
162 {ReqHeadersBlock, _} = cow_hpack:encode([
163 {<<":method">>, <<"CONNECT">>},
164 {<<":protocol">>, <<"websocket">>},
165 {<<":protocol">>, <<"mqtt">>},
166 {<<":scheme">>, <<"http">>},
167 {<<":path">>, <<"/ws">>},
168 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
169 {<<"sec-websocket-version">>, <<"13">>},
170 {<<"origin">>, <<"http://localhost">>}
171 ]),
172 ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
173 %% Receive a PROTOCOL_ERROR stream error.
174 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
175 ok.
176
177 reject_unknown_pseudo_header_protocol(Config) ->
178 %% @todo This probably shouldn't send 400 but 501 instead based on RFC 9220.
179 doc("An extended CONNECT request with an unknown protocol must be rejected "
180 "with a 400 error. (draft-01 4)"),
181 %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
182 {ok, Socket, Settings} = do_handshake(Config),
183 #{enable_connect_protocol := true} = Settings,
184 %% Send an extended CONNECT request with an unknown :protocol pseudo-header.
185 {ReqHeadersBlock, _} = cow_hpack:encode([
186 {<<":method">>, <<"CONNECT">>},
187 {<<":protocol">>, <<"mqtt">>},
188 {<<":scheme">>, <<"http">>},
189 {<<":path">>, <<"/ws">>},
190 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
191 {<<"sec-websocket-version">>, <<"13">>},
192 {<<"origin">>, <<"http://localhost">>}
193 ]),
194 ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
195 %% Receive a 400 response.
196 {ok, << Len1:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
197 {ok, RespHeadersBlock} = gen_tcp:recv(Socket, Len1, 1000),
198 {RespHeaders, _} = cow_hpack:decode(RespHeadersBlock),
199 {_, <<"501">>} = lists:keyfind(<<":status">>, 1, RespHeaders),
200 ok.
201
202 reject_invalid_pseudo_header_protocol(Config) ->
203 %% @todo This probably shouldn't send 400 but 501 instead based on RFC 9220.
204 doc("An extended CONNECT request with an invalid :protocol component "
205 "must be rejected with a PROTOCOL_ERROR stream error. "
206 "(draft-01 4, RFC7540 8.1.2.6)"),
207 %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
208 {ok, Socket, Settings} = do_handshake(Config),
209 #{enable_connect_protocol := true} = Settings,
210 %% Send an extended CONNECT request with an invalid :protocol pseudo-header.
211 {ReqHeadersBlock, _} = cow_hpack:encode([
212 {<<":method">>, <<"CONNECT">>},
213 {<<":protocol">>, <<"websocket mqtt">>},
214 {<<":scheme">>, <<"http">>},
215 {<<":path">>, <<"/ws">>},
216 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
217 {<<"sec-websocket-version">>, <<"13">>},
218 {<<"origin">>, <<"http://localhost">>}
219 ]),
220 ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
221 %% Receive a PROTOCOL_ERROR stream error.
222 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
223 ok.
224
225 reject_invalid_pseudo_header_protocol_crlf(Config) ->
226 doc("An extended CONNECT request with an invalid :protocol component "
227 "must be rejected with a PROTOCOL_ERROR stream error. "
228 "(draft-01 4, RFC7540 8.1.2.6)"),
229 %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
230 {ok, Socket, Settings} = do_handshake(Config),
231 #{enable_connect_protocol := true} = Settings,
232 %% Send an extended CONNECT request with an invalid :protocol pseudo-header.
233 {ReqHeadersBlock, _} = cow_hpack:encode([
234 {<<":method">>, <<"CONNECT">>},
235 {<<":protocol">>, <<"websocket\r\n">>},
236 {<<":scheme">>, <<"http">>},
237 {<<":path">>, <<"/ws">>},
238 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
239 {<<"sec-websocket-version">>, <<"13">>},
240 {<<"origin">>, <<"http://localhost">>}
241 ]),
242 ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
243 %% Receive a PROTOCOL_ERROR stream error.
244 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
245 ok.
246
247 reject_invalid_pseudo_header_protocol_nul(Config) ->
248 doc("An extended CONNECT request with an invalid :protocol component "
249 "must be rejected with a PROTOCOL_ERROR stream error. "
250 "(draft-01 4, RFC7540 8.1.2.6)"),
251 %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
252 {ok, Socket, Settings} = do_handshake(Config),
253 #{enable_connect_protocol := true} = Settings,
254 %% Send an extended CONNECT request with an invalid :protocol pseudo-header.
255 {ReqHeadersBlock, _} = cow_hpack:encode([
256 {<<":method">>, <<"CONNECT">>},
257 {<<":protocol">>, <<"websocket\0">>},
258 {<<":scheme">>, <<"http">>},
259 {<<":path">>, <<"/ws">>},
260 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
261 {<<"sec-websocket-version">>, <<"13">>},
262 {<<"origin">>, <<"http://localhost">>}
263 ]),
264 ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
265 %% Receive a PROTOCOL_ERROR stream error.
266 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
267 ok.
268
269 reject_missing_pseudo_header_scheme(Config) ->
270 doc("An extended CONNECT request without a scheme component must be rejected "
271 "with a PROTOCOL_ERROR stream error. (draft-01 4, RFC7540 8.1.2.6)"),
272 %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
273 {ok, Socket, Settings} = do_handshake(Config),
274 #{enable_connect_protocol := true} = Settings,
275 %% Send an extended CONNECT request without a :scheme pseudo-header.
276 {ReqHeadersBlock, _} = cow_hpack:encode([
277 {<<":method">>, <<"CONNECT">>},
278 {<<":protocol">>, <<"websocket">>},
279 {<<":path">>, <<"/ws">>},
280 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
281 {<<"sec-websocket-version">>, <<"13">>},
282 {<<"origin">>, <<"http://localhost">>}
283 ]),
284 ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
285 %% Receive a PROTOCOL_ERROR stream error.
286 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
287 ok.
288
289 reject_missing_pseudo_header_path(Config) ->
290 doc("An extended CONNECT request without a path component must be rejected "
291 "with a PROTOCOL_ERROR stream error. (draft-01 4, RFC7540 8.1.2.6)"),
292 %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
293 {ok, Socket, Settings} = do_handshake(Config),
294 #{enable_connect_protocol := true} = Settings,
295 %% Send an extended CONNECT request without a :path pseudo-header.
296 {ReqHeadersBlock, _} = cow_hpack:encode([
297 {<<":method">>, <<"CONNECT">>},
298 {<<":protocol">>, <<"websocket">>},
299 {<<":scheme">>, <<"http">>},
300 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
301 {<<"sec-websocket-version">>, <<"13">>},
302 {<<"origin">>, <<"http://localhost">>}
303 ]),
304 ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
305 %% Receive a PROTOCOL_ERROR stream error.
306 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
307 ok.
308
309 % On requests bearing the :protocol pseudo-header, the :authority
310 % pseudo-header field is interpreted according to Section 8.1.2.3 of
311 % [RFC7540] instead of Section 8.3 of [RFC7540]. In particular the
312 % server MUST not make a new TCP connection to the host and port
313 % indicated by the :authority.
314
315 reject_missing_pseudo_header_authority(Config) ->
316 doc("An extended CONNECT request without an authority component must be rejected "
317 "with a PROTOCOL_ERROR stream error. (draft-01 4, draft-01 5)"),
318 %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
319 {ok, Socket, Settings} = do_handshake(Config),
320 #{enable_connect_protocol := true} = Settings,
321 %% Send an extended CONNECT request without an :authority pseudo-header.
322 {ReqHeadersBlock, _} = cow_hpack:encode([
323 {<<":method">>, <<"CONNECT">>},
324 {<<":protocol">>, <<"websocket">>},
325 {<<":scheme">>, <<"http">>},
326 {<<":path">>, <<"/ws">>},
327 {<<"sec-websocket-version">>, <<"13">>},
328 {<<"origin">>, <<"http://localhost">>}
329 ]),
330 ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
331 %% Receive a PROTOCOL_ERROR stream error.
332 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
333 ok.
334
335 % Using Extended CONNECT To Bootstrap The WebSocket Protocol.
336
337 reject_missing_pseudo_header_protocol(Config) ->
338 doc("An extended CONNECT request without a protocol component must be rejected "
339 "with a PROTOCOL_ERROR stream error. (draft-01 4, RFC7540 8.1.2.6)"),
340 %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
341 {ok, Socket, Settings} = do_handshake(Config),
342 #{enable_connect_protocol := true} = Settings,
343 %% Send an extended CONNECT request without a :protocol pseudo-header.
344 {ReqHeadersBlock, _} = cow_hpack:encode([
345 {<<":method">>, <<"CONNECT">>},
346 {<<":scheme">>, <<"http">>},
347 {<<":path">>, <<"/ws">>},
348 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
349 {<<"sec-websocket-version">>, <<"13">>},
350 {<<"origin">>, <<"http://localhost">>}
351 ]),
352 ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
353 %% Receive a PROTOCOL_ERROR stream error.
354 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
355 ok.
356
357 % The scheme of the Target URI [RFC7230] MUST be https for wss schemed
358 % WebSockets and http for ws schemed WebSockets. The websocket URI is
359 % still used for proxy autoconfiguration.
360
361 reject_connection_header(Config) ->
362 doc("An extended CONNECT request with a connection header must be rejected "
363 "with a PROTOCOL_ERROR stream error. (draft-01 5, RFC7540 8.1.2.6)"),
364 %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
365 {ok, Socket, Settings} = do_handshake(Config),
366 #{enable_connect_protocol := true} = Settings,
367 %% Send an extended CONNECT request with a connection header.
368 {ReqHeadersBlock, _} = cow_hpack:encode([
369 {<<":method">>, <<"CONNECT">>},
370 {<<":protocol">>, <<"websocket">>},
371 {<<":scheme">>, <<"http">>},
372 {<<":path">>, <<"/ws">>},
373 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
374 {<<"connection">>, <<"upgrade">>},
375 {<<"sec-websocket-version">>, <<"13">>},
376 {<<"origin">>, <<"http://localhost">>}
377 ]),
378 ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
379 %% Receive a PROTOCOL_ERROR stream error.
380 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
381 ok.
382
383 reject_upgrade_header(Config) ->
384 doc("An extended CONNECT request with a upgrade header must be rejected "
385 "with a PROTOCOL_ERROR stream error. (draft-01 5, RFC7540 8.1.2.6)"),
386 %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
387 {ok, Socket, Settings} = do_handshake(Config),
388 #{enable_connect_protocol := true} = Settings,
389 %% Send an extended CONNECT request with a upgrade header.
390 {ReqHeadersBlock, _} = cow_hpack:encode([
391 {<<":method">>, <<"CONNECT">>},
392 {<<":protocol">>, <<"websocket">>},
393 {<<":scheme">>, <<"http">>},
394 {<<":path">>, <<"/ws">>},
395 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
396 {<<"upgrade">>, <<"websocket">>},
397 {<<"sec-websocket-version">>, <<"13">>},
398 {<<"origin">>, <<"http://localhost">>}
399 ]),
400 ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
401 %% Receive a PROTOCOL_ERROR stream error.
402 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
403 ok.
404
405 % After successfully processing the opening handshake the peers should
406 % proceed with The WebSocket Protocol [RFC6455] using the HTTP/2 stream
407 % from the CONNECT transaction as if it were the TCP connection
408 % referred to in [RFC6455]. The state of the WebSocket connection at
409 % this point is OPEN as defined by [RFC6455], Section 4.1.
410 %% @todo I'm guessing we should test for things like RST_STREAM,
411 %% closing the connection and others?
412
413 % Examples.
414
415 %% @todo Probably worth testing that we get the correct option
416 %% over all different connection types (alpn, prior, upgrade).
417 accept_handshake_when_enabled(Config) ->
418 doc("Confirm the example for Websocket over HTTP/2 works. (draft-01 5.1)"),
419 %% Connect to server and confirm that SETTINGS_ENABLE_CONNECT_PROTOCOL = 1.
420 {ok, Socket, Settings} = do_handshake(Config),
421 #{enable_connect_protocol := true} = Settings,
422 %% Send a CONNECT :protocol request to upgrade the stream to Websocket.
423 {ReqHeadersBlock, _} = cow_hpack:encode([
424 {<<":method">>, <<"CONNECT">>},
425 {<<":protocol">>, <<"websocket">>},
426 {<<":scheme">>, <<"http">>},
427 {<<":path">>, <<"/ws">>},
428 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
429 {<<"sec-websocket-version">>, <<"13">>},
430 {<<"origin">>, <<"http://localhost">>}
431 ]),
432 ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
433 %% Receive a 200 response.
434 {ok, << Len1:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
435 {ok, RespHeadersBlock} = gen_tcp:recv(Socket, Len1, 1000),
436 {RespHeaders, _} = cow_hpack:decode(RespHeadersBlock),
437 {_, <<"200">>} = lists:keyfind(<<":status">>, 1, RespHeaders),
438 %% Masked text hello echoed back clear by the server.
439 Mask = 16#37fa213d,
440 MaskedHello = ws_SUITE:do_mask(<<"Hello">>, Mask, <<>>),
441 ok = gen_tcp:send(Socket, cow_http2:data(1, nofin,
442 <<1:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary>>)),
443 %% Ignore expected WINDOW_UPDATE frames.
444 {ok, <<4:24, 8:8, _:72>>} = gen_tcp:recv(Socket, 13, 1000),
445 {ok, <<4:24, 8:8, _:72>>} = gen_tcp:recv(Socket, 13, 1000),
446 {ok, <<Len2:24, _:8, _:8, _:32>>} = gen_tcp:recv(Socket, 9, 1000),
447 {ok, <<1:1, 0:3, 1:4, 0:1, 5:7, "Hello">>} = gen_tcp:recv(Socket, Len2, 1000),
448 ok.
449
450 %% Closing a Websocket stream.
451
452 % The HTTP/2 stream closure is also analagous to the TCP connection closure of
453 % [RFC6455]. Orderly TCP level closures are represented as END_STREAM
454 % ([RFC7540] Section 6.1) flags and RST exceptions are represented with
455 % the RST_STREAM ([RFC7540] Section 6.4) frame with the CANCEL
456 % ([RFC7540] Secion 7) error code.
457
458 %% @todo client close frame with END_STREAM
459 %% @todo server close frame with END_STREAM
460 %% @todo client other frame with END_STREAM
461 %% @todo server other frame with END_STREAM
462 %% @todo client close connection