Debian Codegraph @

test/rfc7540_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 %% Note that Cowboy does not implement the PRIORITY mechanism.
16 %% Everyone has been moving away from it and it is widely seen
17 %% as a failure. Setting priorities has been counter productive
18 %% with regards to performance. Clients have been moving away
19 %% from the mechanism.
20
21 -module(rfc7540_SUITE).
22 -compile(export_all).
23 -compile(nowarn_export_all).
24
25 -import(ct_helper, [config/2]).
26 -import(ct_helper, [doc/1]).
27 -import(ct_helper, [get_remote_pid_tcp/1]).
28 -import(cowboy_test, [gun_open/1]).
29 -import(cowboy_test, [raw_open/1]).
30 -import(cowboy_test, [raw_send/2]).
31 -import(cowboy_test, [raw_recv_head/1]).
32 -import(cowboy_test, [raw_recv/3]).
33
34 all() -> [{group, clear}, {group, tls}].
35
36 groups() ->
37 Tests = ct_helper:all(?MODULE),
38 RejectTLS = [http_upgrade_reject_tls, prior_knowledge_reject_tls],
39 Clear = [T || T <- Tests, lists:sublist(atom_to_list(T), 4) =/= "alpn"] -- RejectTLS,
40 TLS = [T || T <- Tests, lists:sublist(atom_to_list(T), 4) =:= "alpn"] ++ RejectTLS,
41 [{clear, [parallel], Clear}, {tls, [parallel], TLS}].
42
43 init_per_group(Name = clear, Config) ->
44 [{protocol, http2}|cowboy_test:init_http(Name, #{
45 env => #{dispatch => cowboy_router:compile(init_routes(Config))},
46 %% Disable the DATA threshold for this test suite.
47 stream_window_data_threshold => 0
48 }, Config)];
49 init_per_group(Name = tls, Config) ->
50 cowboy_test:init_http2(Name, #{
51 env => #{dispatch => cowboy_router:compile(init_routes(Config))},
52 %% Disable the DATA threshold for this test suite.
53 stream_window_data_threshold => 0
54 }, Config).
55
56 end_per_group(Name, _) ->
57 ok = cowboy:stop_listener(Name).
58
59 init_routes(_) -> [
60 {"localhost", [
61 {"/", hello_h, []},
62 {"/echo/:key", echo_h, []},
63 {"/delay_hello", delay_hello_h, 1200},
64 {"/long_polling", long_polling_h, []},
65 {"/loop_handler_abort", loop_handler_abort_h, []},
66 {"/resp/:key[/:arg]", resp_h, []}
67 ]}
68 ].
69
70 %% Starting HTTP/2 for "http" URIs.
71
72 http_upgrade_reject_tls(Config) ->
73 doc("Implementations that support HTTP/2 over TLS must use ALPN. (RFC7540 3.4)"),
74 TlsOpts = ct_helper:get_certs_from_ets(),
75 {ok, Socket} = ssl:connect("localhost", config(port, Config),
76 [binary, {active, false}|TlsOpts]),
77 %% Send a valid preface.
78 ok = ssl:send(Socket, [
79 "GET / HTTP/1.1\r\n"
80 "Host: localhost\r\n"
81 "Connection: Upgrade, HTTP2-Settings\r\n"
82 "Upgrade: h2c\r\n"
83 "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
84 "\r\n"]),
85 %% We expect the server to send an HTTP 400 error
86 %% when trying to use HTTP/2 without going through ALPN negotiation.
87 {ok, <<"HTTP/1.1 400">>} = ssl:recv(Socket, 12, 1000),
88 ok.
89
90 http_upgrade_ignore_h2(Config) ->
91 doc("An h2 token in an Upgrade field must be ignored. (RFC7540 3.2)"),
92 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
93 ok = gen_tcp:send(Socket, [
94 "GET / HTTP/1.1\r\n"
95 "Host: localhost\r\n"
96 "Connection: Upgrade, HTTP2-Settings\r\n"
97 "Upgrade: h2\r\n"
98 "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
99 "\r\n"]),
100 {ok, <<"HTTP/1.1 200">>} = gen_tcp:recv(Socket, 12, 1000),
101 ok.
102
103 http_upgrade_ignore_if_http_10(Config) ->
104 doc("The Upgrade header must be ignored if part of an HTTP/1.0 request. (RFC7230 6.7)"),
105 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
106 ok = gen_tcp:send(Socket, [
107 "GET / HTTP/1.0\r\n"
108 "Host: localhost\r\n"
109 "Connection: Upgrade, HTTP2-Settings\r\n"
110 "Upgrade: h2c\r\n"
111 "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
112 "\r\n"]),
113 {ok, <<"HTTP/1.1 200">>} = gen_tcp:recv(Socket, 12, 1000),
114 ok.
115
116 http_upgrade_ignore_if_empty(Config) ->
117 doc("The Upgrade header must be ignored if empty. (RFC7230 6.7)"),
118 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
119 ok = gen_tcp:send(Socket, [
120 "GET / HTTP/1.1\r\n"
121 "Host: localhost\r\n"
122 "Connection: Upgrade, HTTP2-Settings\r\n"
123 "Upgrade: \r\n"
124 "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
125 "\r\n"]),
126 {ok, <<"HTTP/1.1 200">>} = gen_tcp:recv(Socket, 12, 1000),
127 ok.
128
129 http_upgrade_ignore_missing_upgrade_in_connection(Config) ->
130 doc("The Upgrade header must be listed in the "
131 "Connection header field. (RFC7230 6.7)"),
132 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
133 ok = gen_tcp:send(Socket, [
134 "GET / HTTP/1.1\r\n"
135 "Host: localhost\r\n"
136 "Connection: HTTP2-Settings\r\n"
137 "Upgrade: h2c\r\n"
138 "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
139 "\r\n"]),
140 {ok, <<"HTTP/1.1 200">>} = gen_tcp:recv(Socket, 12, 1000),
141 ok.
142
143 http_upgrade_ignore_missing_http2_settings_in_connection(Config) ->
144 doc("The HTTP2-Settings header must be listed in the "
145 "Connection header field. (RFC7540 3.2.1, RFC7230 6.7)"),
146 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
147 ok = gen_tcp:send(Socket, [
148 "GET / HTTP/1.1\r\n"
149 "Host: localhost\r\n"
150 "Connection: Upgrade\r\n"
151 "Upgrade: h2c\r\n"
152 "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
153 "\r\n"]),
154 {ok, <<"HTTP/1.1 200">>} = gen_tcp:recv(Socket, 12, 1000),
155 ok.
156
157 http_upgrade_ignore_zero_http2_settings_header(Config) ->
158 doc("The HTTP Upgrade request must include "
159 "exactly one HTTP2-Settings header field (RFC7540 3.2, RFC7540 3.2.1)"),
160 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
161 ok = gen_tcp:send(Socket, [
162 "GET / HTTP/1.1\r\n"
163 "Host: localhost\r\n"
164 "Connection: Upgrade, HTTP2-Settings\r\n"
165 "Upgrade: h2c\r\n"
166 "\r\n"]),
167 {ok, <<"HTTP/1.1 200">>} = gen_tcp:recv(Socket, 12, 1000),
168 ok.
169
170 http_upgrade_reject_two_http2_settings_header(Config) ->
171 doc("The HTTP Upgrade request must include "
172 "exactly one HTTP2-Settings header field (RFC7540 3.2, RFC7540 3.2.1)"),
173 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
174 ok = gen_tcp:send(Socket, [
175 "GET / HTTP/1.1\r\n"
176 "Host: localhost\r\n"
177 "Connection: Upgrade, HTTP2-Settings\r\n"
178 "Upgrade: h2c\r\n"
179 "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
180 "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
181 "\r\n"]),
182 {ok, <<"HTTP/1.1 400">>} = gen_tcp:recv(Socket, 12, 1000),
183 ok.
184
185 http_upgrade_reject_bad_http2_settings_header(Config) ->
186 doc("The HTTP Upgrade request must include "
187 "a valid HTTP2-Settings header field (RFC7540 3.2, RFC7540 3.2.1)"),
188 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
189 ok = gen_tcp:send(Socket, [
190 "GET / HTTP/1.1\r\n"
191 "Host: localhost\r\n"
192 "Connection: Upgrade, HTTP2-Settings\r\n"
193 "Upgrade: h2c\r\n"
194 %% We send a full SETTINGS frame on purpose.
195 "HTTP2-Settings: ", base64:encode(iolist_to_binary(cow_http2:settings(#{}))), "\r\n",
196 "\r\n"]),
197 {ok, <<"HTTP/1.1 400">>} = gen_tcp:recv(Socket, 12, 1000),
198 ok.
199
200 %% Match directly for now.
201 do_recv_101(Socket) ->
202 {ok, <<
203 "HTTP/1.1 101 Switching Protocols\r\n"
204 "connection: Upgrade\r\n"
205 "upgrade: h2c\r\n"
206 "\r\n"
207 >>} = gen_tcp:recv(Socket, 71, 1000),
208 ok.
209
210 http_upgrade_101(Config) ->
211 doc("A 101 response must be sent on successful upgrade "
212 "to HTTP/2 when using the HTTP Upgrade mechanism. (RFC7540 3.2, RFC7230 6.7)"),
213 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
214 ok = gen_tcp:send(Socket, [
215 "GET / HTTP/1.1\r\n"
216 "Host: localhost\r\n"
217 "Connection: Upgrade, HTTP2-Settings\r\n"
218 "Upgrade: h2c\r\n"
219 "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
220 "\r\n"]),
221 ok = do_recv_101(Socket),
222 ok.
223
224 http_upgrade_server_preface(Config) ->
225 doc("The first frame after the upgrade must be a "
226 "SETTINGS frame for the server connection preface. (RFC7540 3.2, RFC7540 3.5, RFC7540 6.5)"),
227 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
228 ok = gen_tcp:send(Socket, [
229 "GET / HTTP/1.1\r\n"
230 "Host: localhost\r\n"
231 "Connection: Upgrade, HTTP2-Settings\r\n"
232 "Upgrade: h2c\r\n"
233 "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
234 "\r\n"]),
235 ok = do_recv_101(Socket),
236 %% Receive the server preface.
237 {ok, << _:24, 4:8, 0:40 >>} = gen_tcp:recv(Socket, 9, 1000),
238 ok.
239
240 http_upgrade_client_preface_timeout(Config) ->
241 doc("Clients negotiating HTTP/2 and not sending a preface in "
242 "a timely manner must be disconnected."),
243 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
244 ok = gen_tcp:send(Socket, [
245 "GET / HTTP/1.1\r\n"
246 "Host: localhost\r\n"
247 "Connection: Upgrade, HTTP2-Settings\r\n"
248 "Upgrade: h2c\r\n"
249 "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
250 "\r\n"]),
251 ok = do_recv_101(Socket),
252 %% Receive the server preface.
253 {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
254 {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
255 %% Receive the response to the initial HTTP/1.1 request.
256 {ok, << HeadersSkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
257 {ok, _} = gen_tcp:recv(Socket, HeadersSkipLen, 1000),
258 {ok, << DataSkipLen:24, 0:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
259 {ok, _} = gen_tcp:recv(Socket, DataSkipLen, 1000),
260 %% Do not send the preface. Wait for the server to disconnect us.
261 {error, closed} = gen_tcp:recv(Socket, 9, 6000),
262 ok.
263
264 http_upgrade_reject_missing_client_preface(Config) ->
265 doc("Servers must treat an invalid connection preface as a "
266 "connection error of type PROTOCOL_ERROR. (RFC7540 3.2, RFC7540 3.5)"),
267 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
268 ok = gen_tcp:send(Socket, [
269 "GET / HTTP/1.1\r\n"
270 "Host: localhost\r\n"
271 "Connection: Upgrade, HTTP2-Settings\r\n"
272 "Upgrade: h2c\r\n"
273 "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
274 "\r\n"]),
275 ok = do_recv_101(Socket),
276 %% Send a SETTINGS frame directly instead of the proper preface.
277 ok = gen_tcp:send(Socket, cow_http2:settings(#{})),
278 %% Receive the server preface.
279 {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
280 {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
281 %% We expect the server to close the connection when it receives a bad preface.
282 %% The server may however have already started sending the response to the
283 %% initial HTTP/1.1 request.
284 Received = lists:reverse(lists:foldl(fun(_, Acc) ->
285 case gen_tcp:recv(Socket, 9, 1000) of
286 {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} ->
287 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
288 [headers|Acc];
289 {ok, << SkipLen:24, 0:8, _:8, 1:32 >>} ->
290 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
291 [data|Acc];
292 {error, _} ->
293 [closed|Acc]
294 end
295 end, [], [1, 2, 3])),
296 case Received of
297 [closed|_] -> ok;
298 [headers, closed|_] -> ok;
299 [headers, data, closed] -> ok
300 end.
301
302 http_upgrade_reject_invalid_client_preface(Config) ->
303 doc("Servers must treat an invalid connection preface as a "
304 "connection error of type PROTOCOL_ERROR. (RFC7540 3.2, RFC7540 3.5)"),
305 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
306 ok = gen_tcp:send(Socket, [
307 "GET / HTTP/1.1\r\n"
308 "Host: localhost\r\n"
309 "Connection: Upgrade, HTTP2-Settings\r\n"
310 "Upgrade: h2c\r\n"
311 "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
312 "\r\n"]),
313 ok = do_recv_101(Socket),
314 %% Send a slightly incorrect preface.
315 ok = gen_tcp:send(Socket, "PRI * HTTP/2.0\r\n\r\nSM: Value\r\n\r\n"),
316 %% Receive the server preface.
317 {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
318 {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
319 %% We expect the server to close the connection when it receives a bad preface.
320 %% The server may however have already started sending the response to the
321 %% initial HTTP/1.1 request.
322 Received = lists:reverse(lists:foldl(fun(_, Acc) ->
323 case gen_tcp:recv(Socket, 9, 1000) of
324 {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} ->
325 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
326 [headers|Acc];
327 {ok, << SkipLen:24, 0:8, _:8, 1:32 >>} ->
328 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
329 [data|Acc];
330 {error, _} ->
331 [closed|Acc]
332 end
333 end, [], [1, 2, 3])),
334 case Received of
335 [closed|_] -> ok;
336 [headers, closed|_] -> ok;
337 [headers, data, closed] -> ok
338 end.
339
340 http_upgrade_reject_missing_client_preface_settings(Config) ->
341 doc("Servers must treat an invalid connection preface as a "
342 "connection error of type PROTOCOL_ERROR. (RFC7540 3.2, RFC7540 3.5)"),
343 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
344 ok = gen_tcp:send(Socket, [
345 "GET / HTTP/1.1\r\n"
346 "Host: localhost\r\n"
347 "Connection: Upgrade, HTTP2-Settings\r\n"
348 "Upgrade: h2c\r\n"
349 "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
350 "\r\n"]),
351 ok = do_recv_101(Socket),
352 %% Send a valid preface sequence except followed by a PING instead of a SETTINGS frame.
353 ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:ping(0)]),
354 %% Receive the server preface.
355 {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
356 {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
357 %% We expect the server to close the connection when it receives a bad preface.
358 %% The server may however have already started sending the response to the
359 %% initial HTTP/1.1 request.
360 Received = lists:reverse(lists:foldl(fun(_, Acc) ->
361 case gen_tcp:recv(Socket, 9, 1000) of
362 {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} ->
363 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
364 [headers|Acc];
365 {ok, << SkipLen:24, 0:8, _:8, 1:32 >>} ->
366 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
367 [data|Acc];
368 {error, _} ->
369 [closed|Acc]
370 end
371 end, [], [1, 2, 3])),
372 case Received of
373 [closed|_] -> ok;
374 [headers, closed|_] -> ok;
375 [headers, data, closed] -> ok
376 end.
377
378 http_upgrade_reject_invalid_client_preface_settings(Config) ->
379 doc("Servers must treat an invalid connection preface as a "
380 "connection error of type PROTOCOL_ERROR. (RFC7540 3.2, RFC7540 3.5)"),
381 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
382 ok = gen_tcp:send(Socket, [
383 "GET / HTTP/1.1\r\n"
384 "Host: localhost\r\n"
385 "Connection: Upgrade, HTTP2-Settings\r\n"
386 "Upgrade: h2c\r\n"
387 "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
388 "\r\n"]),
389 ok = do_recv_101(Socket),
390 %% Send a valid preface sequence except followed by a badly formed SETTINGS frame.
391 ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", << 0:24, 4:8, 0:9, 1:31 >>]),
392 %% Receive the server preface.
393 {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
394 {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
395 %% We expect the server to close the connection when it receives a bad preface.
396 %% The server may however have already started sending the response to the
397 %% initial HTTP/1.1 request.
398 Received = lists:reverse(lists:foldl(fun(_, Acc) ->
399 case gen_tcp:recv(Socket, 9, 1000) of
400 {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} ->
401 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
402 [headers|Acc];
403 {ok, << SkipLen:24, 0:8, _:8, 1:32 >>} ->
404 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
405 [data|Acc];
406 {error, _} ->
407 [closed|Acc]
408 end
409 end, [], [1, 2, 3])),
410 case Received of
411 [closed|_] -> ok;
412 [headers, closed|_] -> ok;
413 [headers, data, closed] -> ok
414 end.
415
416 http_upgrade_accept_client_preface_empty_settings(Config) ->
417 doc("The SETTINGS frame in the client preface may be empty. (RFC7540 3.2, RFC7540 3.5)"),
418 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
419 ok = gen_tcp:send(Socket, [
420 "GET / HTTP/1.1\r\n"
421 "Host: localhost\r\n"
422 "Connection: Upgrade, HTTP2-Settings\r\n"
423 "Upgrade: h2c\r\n"
424 "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
425 "\r\n"]),
426 ok = do_recv_101(Socket),
427 %% Send a valid preface sequence except followed by an empty SETTINGS frame.
428 ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
429 %% Receive the server preface.
430 {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
431 {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
432 %% Receive the SETTINGS ack. The response might arrive beforehand.
433 Received = lists:reverse(lists:foldl(fun(_, Acc) ->
434 case gen_tcp:recv(Socket, 9, 1000) of
435 {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} ->
436 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
437 [headers|Acc];
438 {ok, << SkipLen:24, 0:8, _:8, 1:32 >>} ->
439 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
440 [data|Acc];
441 {ok, << 0:24, 4:8, 1:8, 0:32 >>} ->
442 [settings_ack|Acc]
443 end
444 end, [], [1, 2, 3])),
445 case Received of
446 [settings_ack|_] -> ok;
447 [headers, settings_ack|_] -> ok;
448 [headers, data, settings_ack] -> ok
449 end.
450
451 http_upgrade_client_preface_settings_ack_timeout(Config) ->
452 doc("The SETTINGS frames sent by the client must be acknowledged. (RFC7540 3.5, RFC7540 6.5.3)"),
453 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
454 ok = gen_tcp:send(Socket, [
455 "GET / HTTP/1.1\r\n"
456 "Host: localhost\r\n"
457 "Connection: Upgrade, HTTP2-Settings\r\n"
458 "Upgrade: h2c\r\n"
459 "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
460 "\r\n"]),
461 ok = do_recv_101(Socket),
462 %% Send a valid preface.
463 ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
464 %% Receive the server preface.
465 {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
466 {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
467 %% Skip the SETTINGS ack. Receive a GOAWAY with reason SETTINGS_TIMEOUT,
468 %% possibly following a HEADERS or HEADERS and DATA frames.
469 Received = lists:reverse(lists:foldl(fun(_, Acc) ->
470 case gen_tcp:recv(Socket, 9, 6000) of
471 {ok, << 0:24, 4:8, 1:8, 0:32 >>} ->
472 Acc;
473 {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} ->
474 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
475 [headers|Acc];
476 {ok, << SkipLen:24, 0:8, _:8, 1:32 >>} ->
477 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
478 [data|Acc];
479 {ok, << 8:24, 7:8, 0:40 >>} ->
480 %% We expect a SETTINGS_TIMEOUT reason.
481 {ok, << 1:32, 4:32 >>} = gen_tcp:recv(Socket, 8, 1000),
482 [goaway|Acc];
483 {error, _} ->
484 %% Can be timeouts, ignore them.
485 Acc
486 end
487 end, [], [1, 2, 3, 4])),
488 case Received of
489 [goaway] -> ok;
490 [headers, goaway] -> ok;
491 [headers, data, goaway] -> ok
492 end.
493
494 %% @todo We need a successful test with actual options in HTTP2-Settings.
495 %% SETTINGS_MAX_FRAME_SIZE is probably the easiest to test. The relevant
496 %% RFC quote is:
497 %%
498 %% 3.2.1
499 %% A server decodes and interprets these values as it would any other
500 %% SETTINGS frame. Explicit acknowledgement of these settings
501 %% (Section 6.5.3) is not necessary, since a 101 response serves as
502 %% implicit acknowledgement.
503
504 %% @todo We need to test an upgrade with a request body. It is probably
505 %% worth having a configuration value for how much we accept while still
506 %% upgrading (if too big, we would just stay on HTTP/1.1). We therefore
507 %% needs a test for when the body is small enough, and one for when the
508 %% body is larger than we accept. The relevant RFC quote is:
509 %%
510 %% 3.2
511 %% Requests that contain a payload body MUST be sent in their entirety
512 %% before the client can send HTTP/2 frames. This means that a large
513 %% request can block the use of the connection until it is completely
514 %% sent.
515
516 %% @todo We should definitely have a test with OPTIONS. The relevant
517 %% RFC quote is:
518 %%
519 %% 3.2
520 %% If concurrency of an initial request with subsequent requests is
521 %% important, an OPTIONS request can be used to perform the upgrade to
522 %% HTTP/2, at the cost of an additional round trip.
523
524 http_upgrade_response(Config) ->
525 doc("A response must be sent to the initial HTTP/1.1 request "
526 "after switching to HTTP/2. The response must use "
527 "the stream identifier 1. (RFC7540 3.2)"),
528 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
529 ok = gen_tcp:send(Socket, [
530 "GET / HTTP/1.1\r\n"
531 "Host: localhost\r\n"
532 "Connection: Upgrade, HTTP2-Settings\r\n"
533 "Upgrade: h2c\r\n"
534 "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
535 "\r\n"]),
536 ok = do_recv_101(Socket),
537 %% Send a valid preface.
538 %% @todo Use non-empty SETTINGS here. Just because.
539 ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
540 %% Receive the server preface.
541 {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
542 {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
543 %% Send the SETTINGS ack.
544 ok = gen_tcp:send(Socket, cow_http2:settings_ack()),
545 %% Receive the SETTINGS ack, and the response HEADERS and DATA (Stream ID 1).
546 Received = lists:reverse(lists:foldl(fun(_, Acc) ->
547 case gen_tcp:recv(Socket, 9, 1000) of
548 {ok, << 0:24, 4:8, 1:8, 0:32 >>} ->
549 [settings_ack|Acc];
550 {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} ->
551 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
552 [headers|Acc];
553 {ok, << SkipLen:24, 0:8, _:8, 1:32 >>} ->
554 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
555 [data|Acc]
556 end
557 end, [], [1, 2, 3])),
558 case Received of
559 [settings_ack, headers, data] -> ok;
560 [headers, settings_ack, data] -> ok;
561 [headers, data, settings_ack] -> ok
562 end.
563
564 http_upgrade_response_half_closed(Config) ->
565 doc("The stream for the initial HTTP/1.1 request is half-closed. (RFC7540 3.2)"),
566 %% Try sending more data after the upgrade and get an error.
567 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
568 ok = gen_tcp:send(Socket, [
569 "GET /long_polling HTTP/1.1\r\n"
570 "Host: localhost\r\n"
571 "Connection: Upgrade, HTTP2-Settings\r\n"
572 "Upgrade: h2c\r\n"
573 "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
574 "\r\n"]),
575 ok = do_recv_101(Socket),
576 %% Send a valid preface followed by an unexpected DATA frame.
577 ok = gen_tcp:send(Socket, [
578 "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n",
579 cow_http2:settings(#{}),
580 cow_http2:data(1, fin, <<"Unexpected DATA frame.">>)
581 ]),
582 %% Receive the server preface.
583 {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
584 {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
585 %% Skip the SETTINGS ack. Receive an RST_STREAM possibly following by
586 %% a HEADERS frame, or a GOAWAY following HEADERS and DATA. This
587 %% corresponds to the stream being in half-closed and closed states.
588 %% The reason must be STREAM_CLOSED.
589 Received = lists:reverse(lists:foldl(fun(_, Acc) ->
590 case gen_tcp:recv(Socket, 9, 1000) of
591 {ok, << 0:24, 4:8, 1:8, 0:32 >>} ->
592 Acc;
593 {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} ->
594 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
595 [headers|Acc];
596 {ok, << SkipLen:24, 0:8, _:8, 1:32 >>} ->
597 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
598 [data|Acc];
599 {ok, << 4:24, 3:8, 0:8, 1:32 >>} ->
600 %% We expect a STREAM_CLOSED reason.
601 {ok, << 5:32 >>} = gen_tcp:recv(Socket, 4, 1000),
602 [rst_stream|Acc];
603 {ok, << 8:24, 7:8, 0:40 >>} ->
604 %% We expect a STREAM_CLOSED reason.
605 {ok, << 1:32, 5:32 >>} = gen_tcp:recv(Socket, 8, 1000),
606 [goaway|Acc];
607 {error, _} ->
608 %% Can be timeouts, ignore them.
609 Acc
610 end
611 end, [], [1, 2, 3, 4])),
612 case Received of
613 [rst_stream] -> ok;
614 [headers, rst_stream] -> ok;
615 [headers, data, goaway] -> ok
616 end.
617
618 %% Starting HTTP/2 for "https" URIs.
619
620 alpn_ignore_h2c(Config) ->
621 doc("An h2c ALPN protocol identifier must be ignored. (RFC7540 3.3)"),
622 TlsOpts = ct_helper:get_certs_from_ets(),
623 {ok, Socket} = ssl:connect("localhost", config(port, Config),
624 [{alpn_advertised_protocols, [<<"h2c">>, <<"http/1.1">>]},
625 binary, {active, false}|TlsOpts]),
626 {ok, <<"http/1.1">>} = ssl:negotiated_protocol(Socket),
627 ok.
628
629 alpn_server_preface(Config) ->
630 doc("The first frame must be a SETTINGS frame "
631 "for the server connection preface. (RFC7540 3.3, RFC7540 3.5, RFC7540 6.5)"),
632 TlsOpts = ct_helper:get_certs_from_ets(),
633 {ok, Socket} = ssl:connect("localhost", config(port, Config),
634 [{alpn_advertised_protocols, [<<"h2">>]},
635 binary, {active, false}|TlsOpts]),
636 {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
637 %% Receive the server preface.
638 {ok, << _:24, 4:8, 0:40 >>} = ssl:recv(Socket, 9, 1000),
639 ok.
640
641 alpn_client_preface_timeout(Config) ->
642 doc("Clients negotiating HTTP/2 and not sending a preface in "
643 "a timely manner must be disconnected."),
644 TlsOpts = ct_helper:get_certs_from_ets(),
645 {ok, Socket} = ssl:connect("localhost", config(port, Config),
646 [{alpn_advertised_protocols, [<<"h2">>]},
647 binary, {active, false}|TlsOpts]),
648 {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
649 %% Receive the server preface.
650 {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
651 {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
652 %% Do not send the preface. Wait for the server to disconnect us.
653 {error, closed} = ssl:recv(Socket, 3, 6000),
654 ok.
655
656 alpn_reject_missing_client_preface(Config) ->
657 doc("Servers must treat an invalid connection preface as a "
658 "connection error of type PROTOCOL_ERROR. (RFC7540 3.3, RFC7540 3.5)"),
659 TlsOpts = ct_helper:get_certs_from_ets(),
660 {ok, Socket} = ssl:connect("localhost", config(port, Config),
661 [{alpn_advertised_protocols, [<<"h2">>]},
662 binary, {active, false}|TlsOpts]),
663 {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
664 %% Send a SETTINGS frame directly instead of the proper preface.
665 ok = ssl:send(Socket, cow_http2:settings(#{})),
666 %% Receive the server preface.
667 {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
668 {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
669 %% We expect the server to close the connection when it receives a bad preface.
670 {error, closed} = ssl:recv(Socket, 3, 1000),
671 ok.
672
673 alpn_reject_invalid_client_preface(Config) ->
674 doc("Servers must treat an invalid connection preface as a "
675 "connection error of type PROTOCOL_ERROR. (RFC7540 3.3, RFC7540 3.5)"),
676 TlsOpts = ct_helper:get_certs_from_ets(),
677 {ok, Socket} = ssl:connect("localhost", config(port, Config),
678 [{alpn_advertised_protocols, [<<"h2">>]},
679 binary, {active, false}|TlsOpts]),
680 {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
681 %% Send a slightly incorrect preface.
682 ok = ssl:send(Socket, "PRI * HTTP/2.0\r\n\r\nSM: Value\r\n\r\n"),
683 %% Receive the server preface.
684 {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
685 {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
686 %% We expect the server to close the connection when it receives a bad preface.
687 {error, closed} = ssl:recv(Socket, 3, 1000),
688 ok.
689
690 alpn_reject_missing_client_preface_settings(Config) ->
691 doc("Servers must treat an invalid connection preface as a "
692 "connection error of type PROTOCOL_ERROR. (RFC7540 3.3, RFC7540 3.5)"),
693 TlsOpts = ct_helper:get_certs_from_ets(),
694 {ok, Socket} = ssl:connect("localhost", config(port, Config),
695 [{alpn_advertised_protocols, [<<"h2">>]},
696 binary, {active, false}|TlsOpts]),
697 {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
698 %% Send a valid preface sequence except followed by a PING instead of a SETTINGS frame.
699 ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:ping(0)]),
700 %% Receive the server preface.
701 {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
702 {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
703 %% We expect the server to close the connection when it receives a bad preface.
704 {error, closed} = ssl:recv(Socket, 3, 1000),
705 ok.
706
707 alpn_reject_invalid_client_preface_settings(Config) ->
708 doc("Servers must treat an invalid connection preface as a "
709 "connection error of type PROTOCOL_ERROR. (RFC7540 3.3, RFC7540 3.5)"),
710 TlsOpts = ct_helper:get_certs_from_ets(),
711 {ok, Socket} = ssl:connect("localhost", config(port, Config),
712 [{alpn_advertised_protocols, [<<"h2">>]},
713 binary, {active, false}|TlsOpts]),
714 {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
715 %% Send a valid preface sequence except followed by a badly formed SETTINGS frame.
716 ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", << 0:24, 4:8, 0:9, 1:31 >>]),
717 %% Receive the server preface.
718 {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
719 {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
720 %% We expect the server to close the connection when it receives a bad preface.
721 {error, closed} = ssl:recv(Socket, 3, 1000),
722 ok.
723
724 alpn_accept_client_preface_empty_settings(Config) ->
725 doc("The SETTINGS frame in the client preface may be empty. (RFC7540 3.3, RFC7540 3.5)"),
726 TlsOpts = ct_helper:get_certs_from_ets(),
727 {ok, Socket} = ssl:connect("localhost", config(port, Config),
728 [{alpn_advertised_protocols, [<<"h2">>]},
729 binary, {active, false}|TlsOpts]),
730 {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
731 %% Send a valid preface sequence except followed by an empty SETTINGS frame.
732 ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
733 %% Receive the server preface.
734 {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
735 {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
736 %% Receive the SETTINGS ack.
737 {ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
738 ok.
739
740 alpn_client_preface_settings_ack_timeout(Config) ->
741 doc("Failure to acknowledge the server's SETTINGS frame "
742 "results in a SETTINGS_TIMEOUT connection error. (RFC7540 3.5, RFC7540 6.5.3)"),
743 TlsOpts = ct_helper:get_certs_from_ets(),
744 {ok, Socket} = ssl:connect("localhost", config(port, Config),
745 [{alpn_advertised_protocols, [<<"h2">>]},
746 binary, {active, false}|TlsOpts]),
747 {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
748 %% Send a valid preface.
749 ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
750 %% Receive the server preface.
751 {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
752 {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
753 %% Receive the SETTINGS ack.
754 {ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
755 %% Do not ack the server preface. Expect a GOAWAY with reason SETTINGS_TIMEOUT.
756 {ok, << _:24, 7:8, _:72, 4:32 >>} = ssl:recv(Socket, 17, 6000),
757 ok.
758
759 alpn(Config) ->
760 doc("Successful ALPN negotiation. (RFC7540 3.3)"),
761 TlsOpts = ct_helper:get_certs_from_ets(),
762 {ok, Socket} = ssl:connect("localhost", config(port, Config),
763 [{alpn_advertised_protocols, [<<"h2">>]},
764 binary, {active, false}|TlsOpts]),
765 {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
766 %% Send a valid preface.
767 %% @todo Use non-empty SETTINGS here. Just because.
768 ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
769 %% Receive the server preface.
770 {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
771 {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
772 %% Send the SETTINGS ack.
773 ok = ssl:send(Socket, cow_http2:settings_ack()),
774 %% Receive the SETTINGS ack.
775 {ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
776 %% Wait until after the SETTINGS ack timeout was supposed to trigger.
777 receive after 6000 -> ok end,
778 %% Send a PING.
779 ok = ssl:send(Socket, cow_http2:ping(0)),
780 %% Receive a PING ack back, indicating the connection is still up.
781 {ok, << 8:24, 6:8, 0:7, 1:1, 0:96 >>} = ssl:recv(Socket, 17, 1000),
782 ok.
783
784 %% Starting HTTP/2 with prior knowledge.
785
786 prior_knowledge_reject_tls(Config) ->
787 doc("Implementations that support HTTP/2 over TLS must use ALPN. (RFC7540 3.4)"),
788 TlsOpts = ct_helper:get_certs_from_ets(),
789 {ok, Socket} = ssl:connect("localhost", config(port, Config),
790 [binary, {active, false}|TlsOpts]),
791 %% Send a valid preface.
792 ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
793 %% We expect the server to send an HTTP 400 error
794 %% when trying to use HTTP/2 without going through ALPN negotiation.
795 {ok, <<"HTTP/1.1 400">>} = ssl:recv(Socket, 12, 1000),
796 ok.
797
798 prior_knowledge_server_preface(Config) ->
799 doc("The first frame must be a SETTINGS frame "
800 "for the server connection preface. (RFC7540 3.4, RFC7540 3.5, RFC7540 6.5)"),
801 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
802 %% Send a valid preface.
803 ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
804 %% Receive the server preface.
805 {ok, << _:24, 4:8, 0:40 >>} = gen_tcp:recv(Socket, 9, 1000),
806 ok.
807
808 %% Note: the client preface timeout doesn't apply in this case,
809 %% so we don't test it. An HTTP/1.1 client that does not send
810 %% a request in a timely manner will get disconnected by the
811 %% HTTP protocol code, not by HTTP/2's.
812
813 %% Note: the test that starts by sending a SETTINGS frame is
814 %% redundant with tests sending garbage on the connection.
815 %% From the point of view of an HTTP/1.1 connection, a
816 %% SETTINGS frame is indistinguishable from garbage.
817
818 prior_knowledge_reject_invalid_client_preface(Config) ->
819 doc("An incorrect preface is an invalid HTTP/1.1 request. (RFC7540 3.4)"),
820 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
821 %% Send a slightly incorrect preface.
822 ok = gen_tcp:send(Socket, "PRI * HTTP/2.0\r\n\r\nSM: Value\r\n\r\n"),
823 %% We propagate to HTTP/2 after checking only the request-line.
824 %% The server then sends its preface before checking the full client preface.
825 {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
826 {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
827 %% We expect the server to close the connection when it receives a bad preface.
828 {error, closed} = gen_tcp:recv(Socket, 9, 1000),
829 ok.
830
831 prior_knowledge_reject_missing_client_preface_settings(Config) ->
832 doc("Servers must treat an invalid connection preface as a "
833 "connection error of type PROTOCOL_ERROR. (RFC7540 3.4, RFC7540 3.5)"),
834 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
835 %% Send a valid preface sequence except followed by a PING instead of a SETTINGS frame.
836 ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:ping(0)]),
837 %% Receive the server preface.
838 {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
839 {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
840 %% We expect the server to close the connection when it receives a bad preface.
841 {error, closed} = gen_tcp:recv(Socket, 9, 1000),
842 ok.
843
844 prior_knowledge_reject_invalid_client_preface_settings(Config) ->
845 doc("Servers must treat an invalid connection preface as a "
846 "connection error of type PROTOCOL_ERROR. (RFC7540 3.4, RFC7540 3.5)"),
847 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
848 %% Send a valid preface sequence except followed by a badly formed SETTINGS frame.
849 ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", << 0:24, 4:8, 0:9, 1:31 >>]),
850 %% Receive the server preface.
851 {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
852 {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
853 %% We expect the server to close the connection when it receives a bad preface.
854 {error, closed} = gen_tcp:recv(Socket, 9, 1000),
855 ok.
856
857 prior_knowledge_accept_client_preface_empty_settings(Config) ->
858 doc("The SETTINGS frame in the client preface may be empty. (RFC7540 3.4, RFC7540 3.5)"),
859 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
860 %% Send a valid preface sequence except followed by an empty SETTINGS frame.
861 ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
862 %% Receive the server preface.
863 {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
864 {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
865 %% Receive the SETTINGS ack.
866 {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
867 ok.
868
869 prior_knowledge_client_preface_settings_ack_timeout(Config) ->
870 doc("The SETTINGS frames sent by the client must be acknowledged. (RFC7540 3.5, RFC7540 6.5.3)"),
871 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
872 %% Send a valid preface.
873 ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
874 %% Receive the server preface.
875 {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
876 {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
877 %% Receive the SETTINGS ack.
878 {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
879 %% Do not ack the server preface. Expect a GOAWAY with reason SETTINGS_TIMEOUT.
880 {ok, << _:24, 7:8, _:72, 4:32 >>} = gen_tcp:recv(Socket, 17, 6000),
881 ok.
882
883 %% Do a prior knowledge handshake.
884 do_handshake(Config) ->
885 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
886 %% Send a valid preface.
887 ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
888 %% Receive the server preface.
889 {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
890 {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
891 %% Send the SETTINGS ack.
892 ok = gen_tcp:send(Socket, cow_http2:settings_ack()),
893 %% Receive the SETTINGS ack.
894 {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
895 {ok, Socket}.
896
897 prior_knowledge(Config) ->
898 doc("Streams can be initiated after a successful HTTP/2 connection "
899 "with prior knowledge of server capabilities. (RFC7540 3.4)"),
900 %% @todo Use non-empty SETTINGS here. Just because.
901 {ok, Socket} = do_handshake(Config),
902 %% Wait until after the SETTINGS ack timeout was supposed to trigger.
903 receive after 6000 -> ok end,
904 %% Send a PING.
905 ok = gen_tcp:send(Socket, cow_http2:ping(0)),
906 %% Receive a PING ack back, indicating the connection is still up.
907 {ok, << 8:24, 6:8, 0:7, 1:1, 0:96 >>} = gen_tcp:recv(Socket, 17, 1000),
908 ok.
909
910 %% @todo If we ever add an option to disable HTTP/2, we need to check
911 %% the following things:
912 %% * HTTP/1.1 Upgrade returns an HTTP/1.1 response (3.2)
913 %% * HTTP/1.1 Upgrade errors out if the client sends HTTP/2 frames
914 %% without waiting for the 101 response (3.2, 3.5)
915 %% * Prior knowledge handshake fails (3.4)
916 %% * ALPN selects HTTP/1.1 (3.3)
917
918 %% Frame format.
919
920 ignore_unknown_frames(Config) ->
921 doc("Frames of unknown type must be ignored and discarded. (RFC7540 4.1)"),
922 {ok, Socket} = do_handshake(Config),
923 %% Send a POST request with a single DATA frame,
924 %% and an unknown frame type interleaved.
925 {HeadersBlock, _} = cow_hpack:encode([
926 {<<":method">>, <<"POST">>},
927 {<<":scheme">>, <<"http">>},
928 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
929 {<<":path">>, <<"/echo/read_body">>}
930 ]),
931 ok = gen_tcp:send(Socket, [
932 cow_http2:headers(1, nofin, HeadersBlock),
933 << 10:24, 99:8, 0:40, 0:80 >>,
934 cow_http2:data(1, fin, << 0:100/unit:8 >>)
935 ]),
936 %% Receive a response with the same DATA frame.
937 {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
938 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
939 {ok, << 100:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
940 {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
941 ok.
942
943 ignore_data_unknown_flags(Config) ->
944 doc("Undefined DATA frame flags must be ignored. (RFC7540 4.1, RFC7540 6.1)"),
945 {ok, Socket} = do_handshake(Config),
946 %% Send a POST request with a DATA frame with unknown flags.
947 {HeadersBlock, _} = cow_hpack:encode([
948 {<<":method">>, <<"POST">>},
949 {<<":scheme">>, <<"http">>},
950 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
951 {<<":path">>, <<"/echo/read_body">>}
952 ]),
953 ok = gen_tcp:send(Socket, [
954 cow_http2:headers(1, nofin, HeadersBlock),
955 << 100:24, 0:8,
956 1:1, 1:1, 1:1, 1:1, %% Undefined.
957 0:1, %% PADDED.
958 1:1, 1:1, %% Undefined.
959 1:1, %% END_STREAM.
960 0:1, 1:31, 0:100/unit:8 >>
961 ]),
962 %% Receive a response with the same DATA frame.
963 {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
964 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
965 {ok, << 100:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
966 {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
967 ok.
968
969 ignore_headers_unknown_flags(Config) ->
970 doc("Undefined HEADERS frame flags must be ignored. (RFC7540 4.1, RFC7540 6.2)"),
971 {ok, Socket} = do_handshake(Config),
972 %% Send a POST request with a HEADERS frame with unknown flags.
973 {HeadersBlock, _} = cow_hpack:encode([
974 {<<":method">>, <<"POST">>},
975 {<<":scheme">>, <<"http">>},
976 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
977 {<<":path">>, <<"/echo/read_body">>}
978 ]),
979 Len = iolist_size(HeadersBlock),
980 ok = gen_tcp:send(Socket, [
981 << Len:24, 1:8,
982 1:1, 1:1, %% Undefined.
983 0:1, %% PRIORITY.
984 1:1, %% Undefined.
985 0:1, %% PADDED.
986 1:1, %% END_HEADERS.
987 1:1, %% Undefined.
988 0:1, %% END_STREAM.
989 0:1, 1:31 >>,
990 HeadersBlock,
991 cow_http2:data(1, fin, << 0:100/unit:8 >>)
992 ]),
993 %% Receive a response with the same DATA frame.
994 {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
995 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
996 {ok, << 100:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
997 {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
998 ok.
999
1000 ignore_priority_unknown_flags(Config) ->
1001 doc("Undefined PRIORITY frame flags must be ignored. (RFC7540 4.1, RFC7540 6.3)"),
1002 {ok, Socket} = do_handshake(Config),
1003 %% Send a POST request with an interleaved PRIORITY frame with unknown flags.
1004 {HeadersBlock, _} = cow_hpack:encode([
1005 {<<":method">>, <<"POST">>},
1006 {<<":scheme">>, <<"http">>},
1007 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
1008 {<<":path">>, <<"/echo/read_body">>}
1009 ]),
1010 ok = gen_tcp:send(Socket, [
1011 cow_http2:headers(1, nofin, HeadersBlock),
1012 << 5:24, 2:8,
1013 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, %% Undefined.
1014 0:1, 1:31, 0:1, 3:31, 0:8 >>,
1015 cow_http2:data(1, fin, << 0:100/unit:8 >>)
1016 ]),
1017 %% Receive a response with the same DATA frame.
1018 {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
1019 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
1020 {ok, << 100:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
1021 {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
1022 ok.
1023
1024 ignore_rst_stream_unknown_flags(Config) ->
1025 doc("Undefined RST_STREAM frame flags must be ignored. (RFC7540 4.1, RFC7540 6.4)"),
1026 {ok, Socket} = do_handshake(Config),
1027 %% Send a POST request then cancel it with an RST_STREAM frame with unknown flags.
1028 {HeadersBlock, _} = cow_hpack:encode([
1029 {<<":method">>, <<"POST">>},
1030 {<<":scheme">>, <<"http">>},
1031 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
1032 {<<":path">>, <<"/echo/read_body">>}
1033 ]),
1034 ok = gen_tcp:send(Socket, [
1035 cow_http2:headers(1, nofin, HeadersBlock),
1036 << 4:24, 3:8,
1037 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, %% Undefined.
1038 0:1, 1:31, 8:32 >>,
1039 cow_http2:headers(3, nofin, HeadersBlock),
1040 cow_http2:data(3, fin, << 0:100/unit:8 >>)
1041 ]),
1042 %% Receive a response with the same DATA frame.
1043 {ok, << SkipLen:24, 1:8, _:8, 3:32 >>} = gen_tcp:recv(Socket, 9, 1000),
1044 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
1045 {ok, << 100:24, 0:8, 1:8, 3:32 >>} = gen_tcp:recv(Socket, 9, 1000),
1046 {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
1047 ok.
1048
1049 ignore_settings_unknown_flags(Config) ->
1050 doc("Undefined SETTINGS frame flags must be ignored. (RFC7540 4.1, RFC7540 6.5)"),
1051 {ok, Socket} = do_handshake(Config),
1052 %% Send a SETTINGS frame with unknown flags.
1053 ok = gen_tcp:send(Socket, << 6:24, 4:8,
1054 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, %% Undefined.
1055 0:1, %% ACK.
1056 0:32, 2:16, 0:32 >>),
1057 %% Receive a SETTINGS ack.
1058 {ok, << 0:24, 4:8, 0:7, 1:1, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
1059 ok.
1060
1061 ignore_push_promise_unknown_flags(Config) ->
1062 doc("Undefined PUSH_PROMISE frame flags must be ignored. (RFC7540 4.1, RFC7540 6.6)"),
1063 {ok, Socket} = do_handshake(Config),
1064 %% Send a PUSH_PROMISE frame with unknown flags.
1065 ok = gen_tcp:send(Socket, << 4:24, 5:8,
1066 1:1, 1:1, 1:1, 1:1, %% Undefined.
1067 0:1, %% PADDED.
1068 1:1, %% END_HEADERS.
1069 1:1, 1:1, %% Undefined.
1070 0:1, 1:31, 0:1, 3:31 >>
1071 ),
1072 %% Receive a PROTOCOL_ERROR connection error.
1073 %%
1074 %% Note that it is not possible to distinguish between the expected
1075 %% result and the server rejecting PUSH_PROMISE frames.
1076 {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1077 ok.
1078
1079 ignore_ping_unknown_flags(Config) ->
1080 doc("Undefined PING frame flags must be ignored. (RFC7540 4.1, RFC7540 6.7)"),
1081 {ok, Socket} = do_handshake(Config),
1082 %% Send a PING frame with unknown flags.
1083 ok = gen_tcp:send(Socket, << 8:24, 6:8,
1084 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, %% Undefined.
1085 0:1, %% ACK.
1086 0:32, 0:64 >>),
1087 %% Receive a PING ACK in return.
1088 {ok, << 8:24, 6:8, _:7, 1:1, _:32, 0:64 >>} = gen_tcp:recv(Socket, 17, 6000),
1089 ok.
1090
1091 ignore_goaway_unknown_flags(Config) ->
1092 doc("Undefined GOAWAY frame flags must be ignored. (RFC7540 4.1, RFC7540 6.8)"),
1093 {ok, Socket} = do_handshake(Config),
1094 %% Send a GOAWAY frame with unknown flags.
1095 ok = gen_tcp:send(Socket, << 8:24, 7:8,
1096 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, %% Undefined.
1097 0:32, 0:64 >>),
1098 %% Receive a GOAWAY frame back.
1099 {ok, << _:24, 7:8, _:72, 0:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1100 ok.
1101
1102 ignore_window_update_unknown_flags(Config) ->
1103 doc("Undefined WINDOW_UPDATE frame flags must be ignored. (RFC7540 4.1, RFC7540 6.9)"),
1104 {ok, Socket} = do_handshake(Config),
1105 %% Send a WINDOW_UPDATE frame with unknown flags.
1106 ok = gen_tcp:send(Socket, << 4:24, 8:8,
1107 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, %% Undefined.
1108 0:32, 1000:32 >>),
1109 %% We expect no errors or replies, therefore we send a PING frame.
1110 ok = gen_tcp:send(Socket, cow_http2:ping(0)),
1111 %% And receive a PING ACK in return.
1112 {ok, << 8:24, 6:8, _:7, 1:1, _:32, 0:64 >>} = gen_tcp:recv(Socket, 17, 6000),
1113 ok.
1114
1115 ignore_continuation_unknown_flags(Config) ->
1116 doc("Undefined CONTINUATION frame flags must be ignored. (RFC7540 4.1, RFC7540 6.10)"),
1117 {ok, Socket} = do_handshake(Config),
1118 %% Send a POST request with a CONTINUATION frame with unknown flags.
1119 {HeadersBlock, _} = cow_hpack:encode([
1120 {<<":method">>, <<"POST">>},
1121 {<<":scheme">>, <<"http">>},
1122 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
1123 {<<":path">>, <<"/echo/read_body">>}
1124 ]),
1125 Len = iolist_size(HeadersBlock),
1126 ok = gen_tcp:send(Socket, [
1127 << 0:24, 1:8, 0:8, 0:1, 1:31 >>,
1128 << Len:24, 9:8,
1129 1:1, 1:1, 1:1, 1:1, 1:1, %% Undefined.
1130 1:1, %% END_HEADERS.
1131 1:1, 1:1, %% Undefined.
1132 0:1, 1:31 >>,
1133 HeadersBlock,
1134 cow_http2:data(1, fin, << 0:100/unit:8 >>)
1135 ]),
1136 %% Receive a response with the same DATA frame.
1137 {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
1138 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
1139 {ok, << 100:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
1140 {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
1141 ok.
1142
1143 %% @todo Flags that have no defined semantics for
1144 %% a particular frame type MUST be left unset (0x0) when sending. (RFC7540 4.1)
1145
1146 ignore_data_reserved_bit(Config) ->
1147 doc("Reserved 1-bit field of DATA frame must be ignored. (RFC7540 4.1, RFC7540 6.1)"),
1148 {ok, Socket} = do_handshake(Config),
1149 %% Send a POST request with a DATA frame with the reserved bit set.
1150 {HeadersBlock, _} = cow_hpack:encode([
1151 {<<":method">>, <<"POST">>},
1152 {<<":scheme">>, <<"http">>},
1153 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
1154 {<<":path">>, <<"/echo/read_body">>}
1155 ]),
1156 ok = gen_tcp:send(Socket, [
1157 cow_http2:headers(1, nofin, HeadersBlock),
1158 << 100:24, 0:8, 0:7, 1:1,
1159 1:1, %% Reserved bit.
1160 1:31, 0:100/unit:8 >>
1161 ]),
1162 %% Receive a response with the same DATA frame.
1163 {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
1164 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
1165 {ok, << 100:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
1166 {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
1167 ok.
1168
1169 ignore_headers_reserved_bit(Config) ->
1170 doc("Reserved 1-bit field of HEADERS frame must be ignored. (RFC7540 4.1, RFC7540 6.2)"),
1171 {ok, Socket} = do_handshake(Config),
1172 %% Send a POST request with a HEADERS frame with the reserved bit set.
1173 {HeadersBlock, _} = cow_hpack:encode([
1174 {<<":method">>, <<"POST">>},
1175 {<<":scheme">>, <<"http">>},
1176 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
1177 {<<":path">>, <<"/echo/read_body">>}
1178 ]),
1179 Len = iolist_size(HeadersBlock),
1180 ok = gen_tcp:send(Socket, [
1181 << Len:24, 1:8, 0:5, 1:1, 0:2,
1182 1:1, %% Reserved bit.
1183 1:31 >>,
1184 HeadersBlock,
1185 cow_http2:data(1, fin, << 0:100/unit:8 >>)
1186 ]),
1187 %% Receive a response with the same DATA frame.
1188 {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
1189 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
1190 {ok, << 100:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
1191 {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
1192 ok.
1193
1194 ignore_priority_reserved_bit(Config) ->
1195 doc("Reserved 1-bit field of PRIORITY frame must be ignored. (RFC7540 4.1, RFC7540 6.3)"),
1196 {ok, Socket} = do_handshake(Config),
1197 %% Send a POST request with an interleaved PRIORITY frame with the reserved bit set.
1198 {HeadersBlock, _} = cow_hpack:encode([
1199 {<<":method">>, <<"POST">>},
1200 {<<":scheme">>, <<"http">>},
1201 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
1202 {<<":path">>, <<"/echo/read_body">>}
1203 ]),
1204 ok = gen_tcp:send(Socket, [
1205 cow_http2:headers(1, nofin, HeadersBlock),
1206 << 5:24, 2:8, 0:8,
1207 1:1, %% Reserved bit.
1208 1:31, 0:1, 3:31, 0:8 >>,
1209 cow_http2:data(1, fin, << 0:100/unit:8 >>)
1210 ]),
1211 %% Receive a response with the same DATA frame.
1212 {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
1213 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
1214 {ok, << 100:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
1215 {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
1216 ok.
1217
1218 ignore_rst_stream_reserved_bit(Config) ->
1219 doc("Reserved 1-bit field of RST_STREAM frame must be ignored. (RFC7540 4.1, RFC7540 6.4)"),
1220 {ok, Socket} = do_handshake(Config),
1221 %% Send a POST request then cancel it with an RST_STREAM frame with the reserved bit set.
1222 {HeadersBlock, _} = cow_hpack:encode([
1223 {<<":method">>, <<"POST">>},
1224 {<<":scheme">>, <<"http">>},
1225 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
1226 {<<":path">>, <<"/echo/read_body">>}
1227 ]),
1228 ok = gen_tcp:send(Socket, [
1229 cow_http2:headers(1, nofin, HeadersBlock),
1230 << 4:24, 3:8, 0:8,
1231 1:1, %% Reserved bit.
1232 1:31, 8:32 >>,
1233 cow_http2:headers(3, nofin, HeadersBlock),
1234 cow_http2:data(3, fin, << 0:100/unit:8 >>)
1235 ]),
1236 %% Receive a response with the same DATA frame.
1237 {ok, << SkipLen:24, 1:8, _:8, 3:32 >>} = gen_tcp:recv(Socket, 9, 1000),
1238 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
1239 {ok, << 100:24, 0:8, 1:8, 3:32 >>} = gen_tcp:recv(Socket, 9, 1000),
1240 {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
1241 ok.
1242
1243 ignore_settings_reserved_bit(Config) ->
1244 doc("Reserved 1-bit field of SETTINGS frame must be ignored. (RFC7540 4.1, RFC7540 6.5)"),
1245 {ok, Socket} = do_handshake(Config),
1246 %% Send a SETTINGS frame with the reserved bit set.
1247 ok = gen_tcp:send(Socket, << 6:24, 4:8, 0:8,
1248 1:1, %% Reserved bit.
1249 0:31, 2:16, 0:32 >>),
1250 %% Receive a SETTINGS ack.
1251 {ok, << 0:24, 4:8, 0:7, 1:1, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
1252 ok.
1253
1254 ignore_push_promise_reserved_bit(Config) ->
1255 doc("Reserved 1-bit field of PUSH_PROMISE frame must be ignored. (RFC7540 4.1, RFC7540 6.6)"),
1256 {ok, Socket} = do_handshake(Config),
1257 %% Send a PUSH_PROMISE frame with the reserved bit set.
1258 ok = gen_tcp:send(Socket, << 4:24, 5:8, 0:5, 1:1, 0:2,
1259 1:1, %% Reserved bit.
1260 1:31, 0:1, 3:31 >>
1261 ),
1262 %% Receive a PROTOCOL_ERROR connection error.
1263 %%
1264 %% Note that it is not possible to distinguish between the expected
1265 %% result and the server rejecting PUSH_PROMISE frames.
1266 {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1267 ok.
1268
1269 ignore_ping_reserved_bit(Config) ->
1270 doc("Reserved 1-bit field of PING frame must be ignored. (RFC7540 4.1, RFC7540 6.7)"),
1271 {ok, Socket} = do_handshake(Config),
1272 %% Send a PING frame with the reserved bit set.
1273 ok = gen_tcp:send(Socket, << 8:24, 6:8, 0:8,
1274 1:1, %% Reserved bit.
1275 0:31, 0:64 >>),
1276 %% Receive a PING ACK in return.
1277 {ok, << 8:24, 6:8, _:7, 1:1, _:32, 0:64 >>} = gen_tcp:recv(Socket, 17, 6000),
1278 ok.
1279
1280 ignore_goaway_reserved_bit(Config) ->
1281 doc("Reserved 1-bit field of GOAWAY frame must be ignored. (RFC7540 4.1, RFC7540 6.8)"),
1282 {ok, Socket} = do_handshake(Config),
1283 %% Send a GOAWAY frame with the reserved bit set.
1284 ok = gen_tcp:send(Socket, << 8:24, 7:8, 0:8,
1285 1:1, %% Reserved bit.
1286 0:31, 0:64 >>),
1287 %% Receive a GOAWAY frame back.
1288 {ok, << _:24, 7:8, _:72, 0:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1289 ok.
1290
1291 ignore_window_update_reserved_bit(Config) ->
1292 doc("Reserved 1-bit field of WINDOW_UPDATE frame must be ignored. (RFC7540 4.1, RFC7540 6.9)"),
1293 {ok, Socket} = do_handshake(Config),
1294 %% Send a WINDOW_UPDATE frame with the reserved bit set.
1295 ok = gen_tcp:send(Socket, << 4:24, 8:8, 0:8,
1296 1:1, %% Reserved bit.
1297 0:31, 1000:32 >>),
1298 %% We expect no errors or replies, therefore we send a PING frame.
1299 ok = gen_tcp:send(Socket, cow_http2:ping(0)),
1300 %% And receive a PING ACK in return.
1301 {ok, << 8:24, 6:8, _:7, 1:1, _:32, 0:64 >>} = gen_tcp:recv(Socket, 17, 6000),
1302 ok.
1303
1304 ignore_continuation_reserved_bit(Config) ->
1305 doc("Reserved 1-bit field of CONTINUATION frame must be ignored. (RFC7540 4.1, RFC7540 6.10)"),
1306 {ok, Socket} = do_handshake(Config),
1307 %% Send a POST request with a CONTINUATION frame with the reserved bit set.
1308 {HeadersBlock, _} = cow_hpack:encode([
1309 {<<":method">>, <<"POST">>},
1310 {<<":scheme">>, <<"http">>},
1311 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
1312 {<<":path">>, <<"/echo/read_body">>}
1313 ]),
1314 Len = iolist_size(HeadersBlock),
1315 ok = gen_tcp:send(Socket, [
1316 << 0:24, 1:8, 0:8, 0:1, 1:31 >>,
1317 << Len:24, 9:8, 0:5, 1:1, 0:2,
1318 1:1, %% Reserved bit.
1319 1:31 >>,
1320 HeadersBlock,
1321 cow_http2:data(1, fin, << 0:100/unit:8 >>)
1322 ]),
1323 %% Receive a response with the same DATA frame.
1324 {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
1325 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
1326 {ok, << 100:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
1327 {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
1328 ok.
1329
1330 %% @todo The reserved 1-bit field MUST remain unset (0x0) when sending. (RFC7540 4.1)
1331
1332 %% Frame size.
1333
1334 max_frame_size_allow_exactly_default(Config) ->
1335 doc("All implementations must allow frame sizes of at least 16384. (RFC7540 4.1, RFC7540 4.2)"),
1336 {ok, Socket} = do_handshake(Config),
1337 %% Send a POST request with a DATA frame of exactly 16384 bytes.
1338 {HeadersBlock, _} = cow_hpack:encode([
1339 {<<":method">>, <<"POST">>},
1340 {<<":scheme">>, <<"http">>},
1341 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
1342 {<<":path">>, <<"/echo/read_body">>}
1343 ]),
1344 ok = gen_tcp:send(Socket, [
1345 cow_http2:headers(1, nofin, HeadersBlock),
1346 cow_http2:data(1, fin, << 0:16384/unit:8 >>)
1347 ]),
1348 %% Receive a response with the same DATA frame.
1349 {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = case gen_tcp:recv(Socket, 9, 1000) of
1350 %% We received a WINDOW_UPDATE first. Skip it and the next.
1351 {ok, <<4:24, 8:8, 0:40>>} ->
1352 {ok, _} = gen_tcp:recv(Socket, 4 + 13, 1000),
1353 gen_tcp:recv(Socket, 9, 1000);
1354 Res ->
1355 Res
1356 end,
1357 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
1358 {ok, << 16384:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
1359 {ok, << 0:16384/unit:8 >>} = gen_tcp:recv(Socket, 16384, 1000),
1360 ok.
1361
1362 max_frame_size_reject_larger_than_default(Config) ->
1363 doc("A FRAME_SIZE_ERROR connection error must be sent when receiving "
1364 "frames larger than the default 16384 length. (RFC7540 4.1, RFC7540 4.2)"),
1365 {ok, Socket} = do_handshake(Config),
1366 %% Send a POST request with a DATA frame larger than 16384 bytes.
1367 {HeadersBlock, _} = cow_hpack:encode([
1368 {<<":method">>, <<"POST">>},
1369 {<<":scheme">>, <<"http">>},
1370 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
1371 {<<":path">>, <<"/echo/read_body">>}
1372 ]),
1373 ok = gen_tcp:send(Socket, [
1374 cow_http2:headers(1, nofin, HeadersBlock),
1375 cow_http2:data(1, fin, << 0:16385/unit:8 >>)
1376 ]),
1377 %% Receive a FRAME_SIZE_ERROR connection error.
1378 {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1379 ok.
1380
1381 max_frame_size_allow_exactly_custom(Config0) ->
1382 doc("An endpoint that sets SETTINGS_MAX_FRAME_SIZE must allow frames "
1383 "of up to that size. (RFC7540 4.2, RFC7540 6.5.2)"),
1384 %% Create a new listener that sets the maximum frame size to 30000.
1385 Config = cowboy_test:init_http(?FUNCTION_NAME, #{
1386 env => #{dispatch => cowboy_router:compile(init_routes(Config0))},
1387 max_frame_size_received => 30000
1388 }, Config0),
1389 try
1390 %% Do the handshake.
1391 {ok, Socket} = do_handshake(Config),
1392 %% Send a HEADERS frame initiating a stream followed by
1393 %% a single 30000 bytes DATA frame.
1394 Headers = [
1395 {<<":method">>, <<"POST">>},
1396 {<<":scheme">>, <<"http">>},
1397 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
1398 {<<":path">>, <<"/long_polling">>}
1399 ],
1400 {HeadersBlock, _} = cow_hpack:encode(Headers),
1401 ok = gen_tcp:send(Socket, [
1402 cow_http2:headers(1, nofin, HeadersBlock),
1403 cow_http2:data(1, fin, <<0:30000/unit:8>>)
1404 ]),
1405 %% Receive a proper response.
1406 {ok, << Len2:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
1407 {ok, _} = gen_tcp:recv(Socket, Len2, 6000),
1408 %% No errors follow due to our sending of a 25000 bytes frame.
1409 {error, timeout} = gen_tcp:recv(Socket, 0, 1000),
1410 gen_tcp:close(Socket)
1411 after
1412 cowboy:stop_listener(?FUNCTION_NAME)
1413 end.
1414
1415 max_frame_size_reject_larger_than_custom(Config0) ->
1416 doc("An endpoint that sets SETTINGS_MAX_FRAME_SIZE must reject frames "
1417 "of up to that size with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.5.2)"),
1418 %% Create a new listener that sets the maximum frame size to 30000.
1419 Config = cowboy_test:init_http(?FUNCTION_NAME, #{
1420 env => #{dispatch => cowboy_router:compile(init_routes(Config0))},
1421 max_frame_size_received => 30000
1422 }, Config0),
1423 try
1424 %% Do the handshake.
1425 {ok, Socket} = do_handshake(Config),
1426 %% Send a HEADERS frame initiating a stream followed by
1427 %% a single DATA frame larger than 30000 bytes.
1428 Headers = [
1429 {<<":method">>, <<"POST">>},
1430 {<<":scheme">>, <<"http">>},
1431 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
1432 {<<":path">>, <<"/long_polling">>}
1433 ],
1434 {HeadersBlock, _} = cow_hpack:encode(Headers),
1435 ok = gen_tcp:send(Socket, [
1436 cow_http2:headers(1, nofin, HeadersBlock),
1437 cow_http2:data(1, fin, <<0:30001/unit:8>>)
1438 ]),
1439 %% Receive a FRAME_SIZE_ERROR connection error.
1440 {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1441 gen_tcp:close(Socket)
1442 after
1443 cowboy:stop_listener(?FUNCTION_NAME)
1444 end.
1445
1446 %% I am using FRAME_SIZE_ERROR here because the information in the
1447 %% frame header tells us this frame is at least 1 byte long, while
1448 %% the given length is smaller; i.e. it is too small to contain
1449 %% mandatory frame data (the pad length).
1450
1451 data_reject_frame_size_0_padded_flag(Config) ->
1452 doc("DATA frames of size 0 with the PADDED flag set must be rejected "
1453 "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.1)"),
1454 {ok, Socket} = do_handshake(Config),
1455 %% Send a POST request with an incorrect padded DATA frame size.
1456 {HeadersBlock, _} = cow_hpack:encode([
1457 {<<":method">>, <<"POST">>},
1458 {<<":scheme">>, <<"http">>},
1459 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
1460 {<<":path">>, <<"/echo/read_body">>}
1461 ]),
1462 ok = gen_tcp:send(Socket, [
1463 cow_http2:headers(1, nofin, HeadersBlock),
1464 << 0:24, 0:8, 0:4, 1:1, 0:2, 1:1, 0:1, 1:31 >>
1465 ]),
1466 %% Receive a FRAME_SIZE_ERROR connection error.
1467 {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1468 ok.
1469
1470 %% This case on the other hand is noted specifically in the RFC
1471 %% as being a PROTOCOL_ERROR. It can be thought of as the Pad Length
1472 %% being incorrect, rather than the frame size.
1473
1474 data_reject_frame_size_too_small_padded_flag(Config) ->
1475 doc("DATA frames with Pad Length >= Length must be rejected "
1476 "with a PROTOCOL_ERROR connection error. (RFC7540 6.1)"),
1477 {ok, Socket} = do_handshake(Config),
1478 %% Send a POST request with an incorrect padded DATA frame size.
1479 {HeadersBlock, _} = cow_hpack:encode([
1480 {<<":method">>, <<"POST">>},
1481 {<<":scheme">>, <<"http">>},
1482 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
1483 {<<":path">>, <<"/echo/read_body">>}
1484 ]),
1485 ok = gen_tcp:send(Socket, [
1486 cow_http2:headers(1, nofin, HeadersBlock),
1487 << 10:24, 0:8, 0:4, 1:1, 0:2, 1:1, 0:1, 1:31, 10:8, 0:80 >>
1488 ]),
1489 %% Receive a PROTOCOL_ERROR connection error.
1490 {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1491 ok.
1492
1493 headers_reject_frame_size_0_padded_flag(Config) ->
1494 doc("HEADERS frames of size 0 with the PADDED flag set must be rejected "
1495 "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.2)"),
1496 {ok, Socket} = do_handshake(Config),
1497 %% Send a padded HEADERS frame with an incorrect size.
1498 ok = gen_tcp:send(Socket, << 0:24, 1:8, 0:4, 1:1, 0:2, 1:1, 0:1, 1:31 >>),
1499 %% Receive a FRAME_SIZE_ERROR connection error.
1500 {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1501 ok.
1502
1503 headers_reject_frame_size_too_small_padded_flag(Config) ->
1504 doc("HEADERS frames with no priority flag and Pad Length >= Length "
1505 "must be rejected with a PROTOCOL_ERROR connection error. (RFC7540 6.2)"),
1506 {ok, Socket} = do_handshake(Config),
1507 %% Send a padded HEADERS frame with an incorrect size.
1508 ok = gen_tcp:send(Socket, << 10:24, 1:8, 0:4, 1:1, 0:2, 1:1, 0:1, 1:31, 10:8, 0:80 >>),
1509 %% Receive a PROTOCOL_ERROR connection error.
1510 {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1511 ok.
1512
1513 headers_reject_frame_size_too_small_priority_flag(Config) ->
1514 doc("HEADERS frames of size smaller than 5 with the PRIORITY flag set must be rejected "
1515 "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.2)"),
1516 {ok, Socket} = do_handshake(Config),
1517 %% Send a HEADERS frame with priority set and an incorrect size.
1518 ok = gen_tcp:send(Socket, << 4:24, 1:8,
1519 0:2, 1:1, 0:4, 1:1, 0:1, 1:31, 0:1, 3:31, 0:8 >>),
1520 %% Receive a FRAME_SIZE_ERROR connection error.
1521 {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1522 ok.
1523
1524 headers_reject_frame_size_5_padded_and_priority_flags(Config) ->
1525 doc("HEADERS frames of size smaller than 6 with the PADDED "
1526 "and PRIORITY flags set must be rejected "
1527 "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.2)"),
1528 {ok, Socket} = do_handshake(Config),
1529 %% Send a padded HEADERS frame with an incorrect size.
1530 ok = gen_tcp:send(Socket, << 5:24, 1:8,
1531 0:2, 1:1, 0:1, 1:1, 0:2, 1:1, 0:1, 1:31, 0:8, 0:1, 3:31, 0:8 >>),
1532 %% Receive a FRAME_SIZE_ERROR connection error.
1533 {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1534 ok.
1535
1536 headers_reject_frame_size_too_small_padded_and_priority_flags(Config) ->
1537 doc("HEADERS frames of size smaller than Length+6 with the PADDED and PRIORITY flags set "
1538 "must be rejected with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.2)"),
1539 {ok, Socket} = do_handshake(Config),
1540 %% Send a padded HEADERS frame with an incorrect size.
1541 ok = gen_tcp:send(Socket, << 15:24, 1:8,
1542 0:2, 1:1, 0:1, 1:1, 0:2, 1:1, 0:1, 1:31, 10:8, 0:1, 3:31, 0:8, 0:80 >>),
1543 %% Receive a PROTOCOL_ERROR connection error.
1544 {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1545 ok.
1546
1547 priority_reject_frame_size_too_small(Config) ->
1548 doc("PRIORITY frames of size smaller than 5 must be rejected "
1549 "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.3)"),
1550 {ok, Socket} = do_handshake(Config),
1551 %% Send a PRIORITY frame with an incorrect size.
1552 ok = gen_tcp:send(Socket, << 4:24, 2:8, 0:9, 1:31, 0:1, 3:31, 0:8 >>),
1553 %% Receive a FRAME_SIZE_ERROR stream error.
1554 {ok, << _:24, 3:8, _:40, 6:32 >>} = gen_tcp:recv(Socket, 13, 6000),
1555 ok.
1556
1557 priority_reject_frame_size_too_large(Config) ->
1558 doc("PRIORITY frames of size larger than 5 must be rejected "
1559 "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.3)"),
1560 {ok, Socket} = do_handshake(Config),
1561 %% Send a PRIORITY frame with an incorrect size.
1562 ok = gen_tcp:send(Socket, << 6:24, 2:8, 0:9, 1:31, 0:1, 3:31, 0:16 >>),
1563 %% Receive a FRAME_SIZE_ERROR stream error.
1564 {ok, << _:24, 3:8, _:40, 6:32 >>} = gen_tcp:recv(Socket, 13, 6000),
1565 ok.
1566
1567 rst_stream_reject_frame_size_too_small(Config) ->
1568 doc("RST_STREAM frames of size smaller than 4 must be rejected "
1569 "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.4)"),
1570 {ok, Socket} = do_handshake(Config),
1571 %% Send a request and reset it immediately.
1572 {HeadersBlock, _} = cow_hpack:encode([
1573 {<<":method">>, <<"GET">>},
1574 {<<":scheme">>, <<"http">>},
1575 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
1576 {<<":path">>, <<"/long_polling">>}
1577 ]),
1578 ok = gen_tcp:send(Socket, [
1579 cow_http2:headers(1, fin, HeadersBlock),
1580 << 3:24, 3:8, 0:9, 1:31, 8:32 >>
1581 ]),
1582 %% Receive a FRAME_SIZE_ERROR connection error.
1583 {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1584 ok.
1585
1586 rst_stream_reject_frame_size_too_large(Config) ->
1587 doc("RST_STREAM frames of size larger than 4 must be rejected "
1588 "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.4)"),
1589 {ok, Socket} = do_handshake(Config),
1590 %% Send a request and reset it immediately.
1591 {HeadersBlock, _} = cow_hpack:encode([
1592 {<<":method">>, <<"GET">>},
1593 {<<":scheme">>, <<"http">>},
1594 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
1595 {<<":path">>, <<"/long_polling">>}
1596 ]),
1597 ok = gen_tcp:send(Socket, [
1598 cow_http2:headers(1, fin, HeadersBlock),
1599 << 5:24, 3:8, 0:9, 1:31, 8:32 >>
1600 ]),
1601 %% Receive a FRAME_SIZE_ERROR connection error.
1602 {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1603 ok.
1604
1605 settings_reject_bad_frame_size(Config) ->
1606 doc("SETTINGS frames must have a size multiple of 6 or be rejected "
1607 "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.5)"),
1608 {ok, Socket} = do_handshake(Config),
1609 %% Send a SETTINGS frame with an incorrect size.
1610 ok = gen_tcp:send(Socket, << 5:24, 4:8, 0:40, 1:16, 4096:32 >>),
1611 %% Receive a FRAME_SIZE_ERROR connection error.
1612 {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1613 ok.
1614
1615 settings_ack_reject_non_empty_frame_size(Config) ->
1616 doc("SETTINGS frames with the ACK flag set and a non-empty payload "
1617 "must be rejected with a FRAME_SIZE_ERROR connection error (RFC7540 4.2, RFC7540 6.5)"),
1618 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
1619 %% Send a valid preface.
1620 ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
1621 %% Receive the server preface.
1622 {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
1623 {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
1624 %% Send a SETTINGS ack with a payload.
1625 ok = gen_tcp:send(Socket, << 6:24, 4:8, 0:7, 1:1, 0:32, 1:16, 4096:32 >>),
1626 %% Receive the SETTINGS ack.
1627 {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
1628 %% Receive a FRAME_SIZE_ERROR connection error.
1629 {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1630 ok.
1631
1632 %% Note that clients are not supposed to send PUSH_PROMISE frames.
1633 %% However when they do, we need to be able to parse it in order
1634 %% to reject it, and so these errors may still occur.
1635
1636 push_promise_reject_frame_size_too_small(Config) ->
1637 doc("PUSH_PROMISE frames of size smaller than 4 must be rejected "
1638 "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.6)"),
1639 {ok, Socket} = do_handshake(Config),
1640 %% Send a PUSH_PROMISE frame with an incorrect size.
1641 ok = gen_tcp:send(Socket, << 3:24, 5:8, 0:5, 1:1, 0:3, 1:31, 0:1, 3:31 >>),
1642 %% Receive a FRAME_SIZE_ERROR connection error.
1643 {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1644 ok.
1645
1646 push_promise_reject_frame_size_4_padded_flag(Config) ->
1647 doc("PUSH_PROMISE frames of size smaller than 5 with the PADDED flag set must be rejected "
1648 "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.6)"),
1649 {ok, Socket} = do_handshake(Config),
1650 %% Send a PUSH_PROMISE frame with an incorrect size.
1651 ok = gen_tcp:send(Socket, << 4:24, 5:8, 0:4, 1:1, 1:1, 0:3, 1:31, 0:1, 0:8, 3:31 >>),
1652 %% Receive a FRAME_SIZE_ERROR connection error.
1653 {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1654 ok.
1655
1656 push_promise_reject_frame_size_too_small_padded_flag(Config) ->
1657 doc("PUSH_PROMISE frames of size smaller than Length+5 with the PADDED flag set "
1658 "must be rejected with a PROTOCOL_ERROR connection error. (RFC7540 4.2, RFC7540 6.6)"),
1659 {ok, Socket} = do_handshake(Config),
1660 %% Send a PUSH_PROMISE frame with an incorrect size.
1661 {HeadersBlock, _} = cow_hpack:encode([
1662 {<<":method">>, <<"GET">>},
1663 {<<":scheme">>, <<"http">>},
1664 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
1665 {<<":path">>, <<"/long_polling">>}
1666 ]),
1667 Len = 14 + iolist_size(HeadersBlock),
1668 ok = gen_tcp:send(Socket, [
1669 << Len:24, 5:8, 0:4, 1:1, 1:1, 0:3, 1:31, 10:8, 0:1, 3:31 >>,
1670 HeadersBlock,
1671 << 0:80 >>
1672 ]),
1673 %% Receive a PROTOCOL_ERROR connection error.
1674 %%
1675 %% Note that it is not possible to distinguish between a Pad Length
1676 %% error and the server rejecting PUSH_PROMISE frames.
1677 {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1678 ok.
1679
1680 ping_reject_frame_size_too_small(Config) ->
1681 doc("PING frames of size smaller than 8 must be rejected "
1682 "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.7)"),
1683 {ok, Socket} = do_handshake(Config),
1684 %% Send a PING frame with an incorrect size.
1685 ok = gen_tcp:send(Socket, << 7:24, 6:8, 0:40, 0:56 >>),
1686 %% Receive a FRAME_SIZE_ERROR connection error.
1687 {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1688 ok.
1689
1690 ping_reject_frame_size_too_large(Config) ->
1691 doc("PING frames of size larger than 8 must be rejected "
1692 "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.7)"),
1693 {ok, Socket} = do_handshake(Config),
1694 %% Send a PING frame with an incorrect size.
1695 ok = gen_tcp:send(Socket, << 9:24, 6:8, 0:40, 0:72 >>),
1696 %% Receive a FRAME_SIZE_ERROR connection error.
1697 {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1698 ok.
1699
1700 goaway_reject_frame_size_too_small(Config) ->
1701 doc("GOAWAY frames of size smaller than 8 must be rejected "
1702 "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.8)"),
1703 {ok, Socket} = do_handshake(Config),
1704 %% Send a GOAWAY frame with an incorrect size.
1705 ok = gen_tcp:send(Socket, << 7:24, 7:8, 0:40, 0:56 >>),
1706 %% Receive a FRAME_SIZE_ERROR connection error.
1707 {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1708 ok.
1709
1710 goaway_allow_frame_size_too_large(Config) ->
1711 doc("GOAWAY frames of size larger than 8 must be allowed. (RFC7540 6.8)"),
1712 {ok, Socket} = do_handshake(Config),
1713 %% Send a GOAWAY frame with debug data.
1714 ok = gen_tcp:send(Socket, << 12:24, 7:8, 0:40, 0:64, 99999:32 >>),
1715 %% Receive a GOAWAY frame back.
1716 {ok, << _:24, 7:8, _:72, 0:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1717 ok.
1718
1719 window_update_reject_frame_size_too_small(Config) ->
1720 doc("WINDOW_UPDATE frames of size smaller than 4 must be rejected "
1721 "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.9)"),
1722 {ok, Socket} = do_handshake(Config),
1723 %% Send a WINDOW_UPDATE frame with an incorrect size.
1724 ok = gen_tcp:send(Socket, << 3:24, 8:8, 0:40, 1000:24 >>),
1725 %% Receive a FRAME_SIZE_ERROR connection error.
1726 {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1727 ok.
1728
1729 window_update_reject_frame_size_too_large(Config) ->
1730 doc("WINDOW_UPDATE frames of size larger than 4 must be rejected "
1731 "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.9)"),
1732 {ok, Socket} = do_handshake(Config),
1733 %% Send a WINDOW_UPDATE frame with an incorrect size.
1734 ok = gen_tcp:send(Socket, << 5:24, 8:8, 0:40, 1000:40 >>),
1735 %% Receive a FRAME_SIZE_ERROR connection error.
1736 {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1737 ok.
1738
1739 %% Note: There is no particular limits on the size of CONTINUATION frames,
1740 %% they can go from 0 to SETTINGS_MAX_FRAME_SIZE.
1741
1742 %% Header compression and decompression.
1743
1744 headers_compression_error(Config) ->
1745 doc("A decoding error in a HEADERS frame's header block must be rejected "
1746 "with a COMPRESSION_ERROR connection error. (RFC7540 4.3, RFC7540 6.2)"),
1747 {ok, Socket} = do_handshake(Config),
1748 %% Send a HEADERS frame with an invalid header block.
1749 ok = gen_tcp:send(Socket, << 10:24, 1:8, 0:5, 1:1, 0:1, 1:1, 0:1, 1:31, 0:10/unit:8 >>),
1750 %% Receive a COMPRESSION_ERROR connection error.
1751 {ok, << _:24, 7:8, _:72, 9:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1752 ok.
1753
1754 continuation_compression_error(Config) ->
1755 doc("A decoding error in a CONTINUATION frame's header block must be rejected "
1756 "with a COMPRESSION_ERROR connection error. (RFC7540 4.3, RFC7540 6.10)"),
1757 {ok, Socket} = do_handshake(Config),
1758 %% Send a CONTINUATION frame with an invalid header block.
1759 ok = gen_tcp:send(Socket, [
1760 << 0:24, 1:8, 0:7, 1:1, 0:1, 1:31 >>,
1761 << 10:24, 9:8, 0:5, 1:1, 0:3, 1:31, 0:10/unit:8 >>
1762 ]),
1763 %% Receive a COMPRESSION_ERROR connection error.
1764 {ok, << _:24, 7:8, _:72, 9:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1765 ok.
1766
1767 continuation_with_frame_interleaved_error(Config) ->
1768 doc("Frames interleaved in a header block must be rejected "
1769 "with a PROTOCOL_ERROR connection error. (RFC7540 4.3, RFC7540 6.2, RFC7540 6.10)"),
1770 {ok, Socket} = do_handshake(Config),
1771 %% Send an unterminated HEADERS frame followed by a PING frame.
1772 ok = gen_tcp:send(Socket, [
1773 << 0:24, 1:8, 0:7, 1:1, 0:1, 1:31 >>,
1774 cow_http2:ping(0)
1775 ]),
1776 %% Receive a PROTOCOL_ERROR connection error.
1777 {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1778 ok.
1779
1780 continuation_wrong_stream_error(Config) ->
1781 doc("CONTINUATION frames with an incorrect stream identifier must be rejected "
1782 "with a PROTOCOL_ERROR connection error. (RFC7540 4.3, RFC7540 6.2)"),
1783 {ok, Socket} = do_handshake(Config),
1784 %% Send an unterminated HEADERS frame followed by a CONTINUATION frame for another stream.
1785 ok = gen_tcp:send(Socket, [
1786 << 0:24, 1:8, 0:7, 1:1, 0:1, 1:31 >>,
1787 << 12:24, 9:8, 0:9, 3:31, "header-block" >>
1788 ]),
1789 %% Receive a PROTOCOL_ERROR connection error.
1790 {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1791 ok.
1792
1793 %% Stream states.
1794
1795 idle_stream_reject_data(Config) ->
1796 doc("DATA frames received on an idle stream must be rejected "
1797 "with a PROTOCOL_ERROR connection error. (RFC7540 5.1, RFC7540 6.1)"),
1798 {ok, Socket} = do_handshake(Config),
1799 %% Send a DATA frame on an idle stream.
1800 ok = gen_tcp:send(Socket, cow_http2:data(1, fin, <<"Unexpected DATA frame.">>)),
1801 %% Receive a PROTOCOL_ERROR connection error.
1802 {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1803 ok.
1804
1805 idle_stream_accept_headers(Config) ->
1806 doc("HEADERS frames received on an idle stream must be accepted. (RFC7540 5.1)"),
1807 {ok, Socket} = do_handshake(Config),
1808 %% Send a HEADERS frame on an idle stream.
1809 {HeadersBlock, _} = cow_hpack:encode([
1810 {<<":method">>, <<"GET">>},
1811 {<<":scheme">>, <<"http">>},
1812 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
1813 {<<":path">>, <<"/">>}
1814 ]),
1815 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
1816 %% Receive a HEADERS frame as a response.
1817 {ok, << _:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
1818 ok.
1819
1820 idle_stream_accept_priority(Config) ->
1821 doc("PRIORITY frames received on an idle stream must be accepted. (RFC7540 5.1)"),
1822 {ok, Socket} = do_handshake(Config),
1823 %% Send a PRIORITY frame on an idle stream.
1824 ok = gen_tcp:send(Socket, cow_http2:priority(1, shared, 3, 123)),
1825 %% Receive no error.
1826 {error, timeout} = gen_tcp:recv(Socket, 7, 1000),
1827 %% Send a HEADERS frame on the same stream.
1828 {HeadersBlock, _} = cow_hpack:encode([
1829 {<<":method">>, <<"GET">>},
1830 {<<":scheme">>, <<"http">>},
1831 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
1832 {<<":path">>, <<"/">>}
1833 ]),
1834 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
1835 %% Receive a HEADERS frame as a response.
1836 {ok, << _:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
1837 ok.
1838
1839 idle_stream_reject_rst_stream(Config) ->
1840 doc("RST_STREAM frames received on an idle stream must be rejected "
1841 "with a PROTOCOL_ERROR connection error. (RFC7540 5.1)"),
1842 {ok, Socket} = do_handshake(Config),
1843 %% Send an RST_STREAM frame on an idle stream.
1844 ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, no_error)),
1845 %% Receive a PROTOCOL_ERROR connection error.
1846 {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1847 ok.
1848
1849 idle_stream_reject_push_promise(Config) ->
1850 doc("PUSH_PROMISE frames received on an idle stream must be rejected "
1851 "with a PROTOCOL_ERROR connection error. (RFC7540 5.1)"),
1852 {ok, Socket} = do_handshake(Config),
1853 %% Send a PUSH_PROMISE frame on an idle stream.
1854 {HeadersBlock, _} = cow_hpack:encode([
1855 {<<":method">>, <<"GET">>},
1856 {<<":scheme">>, <<"http">>},
1857 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
1858 {<<":path">>, <<"/long_polling">>}
1859 ]),
1860 ok = gen_tcp:send(Socket, cow_http2:push_promise(1, 3, HeadersBlock)),
1861 %% Receive a PROTOCOL_ERROR connection error.
1862 {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1863 ok.
1864
1865 idle_stream_reject_window_update(Config) ->
1866 doc("WINDOW_UPDATE frames received on an idle stream must be rejected "
1867 "with a PROTOCOL_ERROR connection error. (RFC7540 5.1)"),
1868 {ok, Socket} = do_handshake(Config),
1869 %% Send a WINDOW_UPDATE frame on an idle stream.
1870 ok = gen_tcp:send(Socket, cow_http2:window_update(1, 12345)),
1871 %% Receive a PROTOCOL_ERROR connection error.
1872 {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1873 ok.
1874
1875 %reserved (local) - after sending PUSH_PROMISE:
1876 % An endpoint MUST NOT send any type of frame other than HEADERS,
1877 % RST_STREAM, or PRIORITY in this state.
1878 %%% how to test this?
1879 %
1880 % A PRIORITY or WINDOW_UPDATE frame MAY be received in this state.
1881 % Receiving any type of frame other than RST_STREAM, PRIORITY, or
1882 % WINDOW_UPDATE on a stream in this state MUST be treated as a
1883 % connection error (Section 5.4.1) of type PROTOCOL_ERROR.
1884 %%% we need to use a large enough file for this
1885 %
1886 %reserved_local_reject_data
1887 %reserved_local_reject_headers
1888 %reserved_local_accept_priority
1889 %reserved_local_accept_rst_stream
1890 %reserved_local_reject_push_promise %% do we even care? we reject it always
1891 %reserved_local_accept_window_update
1892 %
1893 %half-closed (remote):
1894 % If an endpoint receives additional frames, other than
1895 % WINDOW_UPDATE, PRIORITY, or RST_STREAM, for a stream that is in
1896 % this state, it MUST respond with a stream error (Section 5.4.2) of
1897 % type STREAM_CLOSED.
1898
1899 half_closed_remote_reject_data(Config) ->
1900 doc("DATA frames received on a half-closed (remote) stream must be rejected "
1901 "with a STREAM_CLOSED stream error. (RFC7540 5.1, RFC7540 6.1)"),
1902 {ok, Socket} = do_handshake(Config),
1903 %% Send a HEADERS frame with the FIN flag set.
1904 {HeadersBlock, _} = cow_hpack:encode([
1905 {<<":method">>, <<"GET">>},
1906 {<<":scheme">>, <<"http">>},
1907 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
1908 {<<":path">>, <<"/long_polling">>}
1909 ]),
1910 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
1911 %% Send a DATA frame on that now half-closed (remote) stream.
1912 ok = gen_tcp:send(Socket, cow_http2:data(1, fin, <<"Unexpected DATA frame.">>)),
1913 %% Receive a STREAM_CLOSED stream error.
1914 {ok, << _:24, 3:8, _:8, 1:32, 5:32 >>} = gen_tcp:recv(Socket, 13, 6000),
1915 ok.
1916
1917 %% We reject all invalid HEADERS with a connection error because
1918 %% we do not want to waste resources decoding them.
1919 half_closed_remote_reject_headers(Config) ->
1920 doc("HEADERS frames received on a half-closed (remote) stream must be rejected "
1921 "with a STREAM_CLOSED connection error. (RFC7540 4.3, RFC7540 5.1)"),
1922 {ok, Socket} = do_handshake(Config),
1923 %% Send a HEADERS frame with the FIN flag set.
1924 {HeadersBlock, _} = cow_hpack:encode([
1925 {<<":method">>, <<"GET">>},
1926 {<<":scheme">>, <<"http">>},
1927 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
1928 {<<":path">>, <<"/long_polling">>}
1929 ]),
1930 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
1931 %% Send a HEADERS frame on that now half-closed (remote) stream.
1932 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
1933 %% Receive a STREAM_CLOSED connection error.
1934 {ok, << _:24, 7:8, _:72, 5:32 >>} = gen_tcp:recv(Socket, 17, 6000),
1935 ok.
1936
1937 half_closed_remote_accept_priority(Config) ->
1938 doc("PRIORITY frames received on a half-closed stream must be accepted. (RFC7540 5.1)"),
1939 {ok, Socket} = do_handshake(Config),
1940 %% Send a HEADERS frame with the FIN flag set.
1941 {HeadersBlock, _} = cow_hpack:encode([
1942 {<<":method">>, <<"GET">>},
1943 {<<":scheme">>, <<"http">>},
1944 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
1945 {<<":path">>, <<"/long_polling">>}
1946 ]),
1947 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
1948 %% Send a PRIORITY frame on that now half-closed (remote) stream.
1949 ok = gen_tcp:send(Socket, cow_http2:priority(1, shared, 3, 123)),
1950 %% Receive a HEADERS frame as a response.
1951 {ok, << _:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
1952 ok.
1953
1954 half_closed_remote_accept_rst_stream(Config) ->
1955 doc("RST_STREAM frames received on a half-closed stream must be accepted. (RFC7540 5.1)"),
1956 {ok, Socket} = do_handshake(Config),
1957 %% Send a HEADERS frame with the FIN flag set.
1958 {HeadersBlock, _} = cow_hpack:encode([
1959 {<<":method">>, <<"GET">>},
1960 {<<":scheme">>, <<"http">>},
1961 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
1962 {<<":path">>, <<"/long_polling">>}
1963 ]),
1964 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
1965 %% Send an RST_STREAM frame on that now half-closed (remote) stream.
1966 ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, no_error)),
1967 %% Receive nothing back.
1968 {error, timeout} = gen_tcp:recv(Socket, 9, 6000),
1969 ok.
1970
1971 %% half_closed_remote_reject_push_promise
1972 %%
1973 %% We respond to all PUSH_PROMISE frames with a PROTOCOL_ERROR connection error
1974 %% because PUSH is disabled in that direction. We therefore cannot test other
1975 %% error conditions.
1976
1977 half_closed_remote_accept_window_update(Config) ->
1978 doc("WINDOW_UPDATE frames received on a half-closed stream must be accepted. (RFC7540 5.1)"),
1979 {ok, Socket} = do_handshake(Config),
1980 %% Send a HEADERS frame with the FIN flag set.
1981 {HeadersBlock, _} = cow_hpack:encode([
1982 {<<":method">>, <<"GET">>},
1983 {<<":scheme">>, <<"http">>},
1984 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
1985 {<<":path">>, <<"/long_polling">>}
1986 ]),
1987 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
1988 %% Send a WINDOW_UPDATE frame on that now half-closed (remote) stream.
1989 ok = gen_tcp:send(Socket, cow_http2:window_update(1, 12345)),
1990 %% Receive a HEADERS frame as a response.
1991 {ok, << _:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
1992 ok.
1993
1994 %% We reject DATA frames sent on closed streams with a STREAM_CLOSED
1995 %% connection error regardless of how the stream was closed to simplify
1996 %% the implementation. This excludes the few frames we ignore from
1997 %% lingering streams that we canceled.
1998 rst_stream_closed_reject_data(Config) ->
1999 doc("DATA frames received on a stream closed via RST_STREAM must be rejected "
2000 "with a STREAM_CLOSED connection error. (RFC7540 5.1, RFC7540 6.1)"),
2001 {ok, Socket} = do_handshake(Config),
2002 %% Send a HEADERS frame.
2003 {HeadersBlock, _} = cow_hpack:encode([
2004 {<<":method">>, <<"GET">>},
2005 {<<":scheme">>, <<"http">>},
2006 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
2007 {<<":path">>, <<"/long_polling">>}
2008 ]),
2009 ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, HeadersBlock)),
2010 %% Send an RST_STREAM frame to close the stream.
2011 ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, cancel)),
2012 %% Send a DATA frame on the now RST_STREAM closed stream.
2013 ok = gen_tcp:send(Socket, cow_http2:data(1, fin, <<"Unexpected DATA frame.">>)),
2014 %% Receive a STREAM_CLOSED connection error.
2015 {ok, << _:24, 7:8, _:72, 5:32 >>} = gen_tcp:recv(Socket, 17, 6000),
2016 ok.
2017
2018 %% We reject all invalid HEADERS with a connection error because
2019 %% we do not want to waste resources decoding them.
2020 rst_stream_closed_reject_headers(Config) ->
2021 doc("HEADERS frames received on a stream closed via RST_STREAM must be rejected "
2022 "with a STREAM_CLOSED connection error. (RFC7540 4.3, RFC7540 5.1)"),
2023 {ok, Socket} = do_handshake(Config),
2024 %% Send a HEADERS frame.
2025 {HeadersBlock, _} = cow_hpack:encode([
2026 {<<":method">>, <<"GET">>},
2027 {<<":scheme">>, <<"http">>},
2028 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
2029 {<<":path">>, <<"/long_polling">>}
2030 ]),
2031 ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, HeadersBlock)),
2032 %% Send an RST_STREAM frame to close the stream.
2033 ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, cancel)),
2034 %% Send a HEADERS frame on the now RST_STREAM closed stream.
2035 ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, HeadersBlock)),
2036 %% Receive a STREAM_CLOSED connection error.
2037 {ok, << _:24, 7:8, _:72, 5:32 >>} = gen_tcp:recv(Socket, 17, 6000),
2038 ok.
2039
2040 rst_stream_closed_accept_priority(Config) ->
2041 doc("PRIORITY frames received on a stream closed via RST_STREAM "
2042 "must be accepted. (RFC7540 5.1)"),
2043 {ok, Socket} = do_handshake(Config),
2044 %% Send a HEADERS frame.
2045 {HeadersBlock, _} = cow_hpack:encode([
2046 {<<":method">>, <<"GET">>},
2047 {<<":scheme">>, <<"http">>},
2048 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
2049 {<<":path">>, <<"/long_polling">>}
2050 ]),
2051 ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, HeadersBlock)),
2052 %% Send an RST_STREAM frame to close the stream.
2053 ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, cancel)),
2054 %% Send a PRIORITY frame on that now RST_STREAM closed stream.
2055 ok = gen_tcp:send(Socket, cow_http2:priority(1, shared, 3, 123)),
2056 %% Receive nothing back.
2057 {error, timeout} = gen_tcp:recv(Socket, 9, 6000),
2058 ok.
2059
2060 rst_stream_closed_ignore_rst_stream(Config) ->
2061 doc("RST_STREAM frames received on a stream closed via RST_STREAM "
2062 "must be ignored to avoid looping. (RFC7540 5.1, RFC7540 5.4.2)"),
2063 {ok, Socket} = do_handshake(Config),
2064 %% Send a HEADERS frame.
2065 {HeadersBlock, _} = cow_hpack:encode([
2066 {<<":method">>, <<"GET">>},
2067 {<<":scheme">>, <<"http">>},
2068 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
2069 {<<":path">>, <<"/long_polling">>}
2070 ]),
2071 ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, HeadersBlock)),
2072 %% Send an RST_STREAM frame to close the stream.
2073 ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, cancel)),
2074 %% Send an extra RST_STREAM.
2075 ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, cancel)),
2076 %% Receive nothing back.
2077 {error, timeout} = gen_tcp:recv(Socket, 9, 6000),
2078 ok.
2079
2080 %% rst_stream_closed_reject_push_promise
2081 %%
2082 %% We respond to all PUSH_PROMISE frames with a PROTOCOL_ERROR connection error
2083 %% because PUSH is disabled in that direction. We therefore cannot test other
2084 %% error conditions.
2085
2086 rst_stream_closed_reject_window_update(Config) ->
2087 doc("WINDOW_UPDATE frames received on a stream closed via RST_STREAM "
2088 "must be rejected with a STREAM_CLOSED stream error. (RFC7540 5.1)"),
2089 {ok, Socket} = do_handshake(Config),
2090 %% Send a HEADERS frame.
2091 {HeadersBlock, _} = cow_hpack:encode([
2092 {<<":method">>, <<"GET">>},
2093 {<<":scheme">>, <<"http">>},
2094 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
2095 {<<":path">>, <<"/long_polling">>}
2096 ]),
2097 ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, HeadersBlock)),
2098 %% Send an RST_STREAM frame to close the stream.
2099 ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, cancel)),
2100 %% Send a WINDOW_UPDATE frame on the now RST_STREAM closed stream.
2101 ok = gen_tcp:send(Socket, cow_http2:window_update(1, 12345)),
2102 %% Receive a STREAM_CLOSED stream error.
2103 {ok, << _:24, 3:8, _:8, 1:32, 5:32 >>} = gen_tcp:recv(Socket, 13, 6000),
2104 ok.
2105
2106 stream_closed_reject_data(Config) ->
2107 doc("DATA frames received on a stream closed normally must be rejected "
2108 "with a STREAM_CLOSED connection error. (RFC7540 5.1, RFC7540 6.1)"),
2109 {ok, Socket} = do_handshake(Config),
2110 %% Send a HEADERS frame.
2111 {HeadersBlock, _} = cow_hpack:encode([
2112 {<<":method">>, <<"GET">>},
2113 {<<":scheme">>, <<"http">>},
2114 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
2115 {<<":path">>, <<"/">>}
2116 ]),
2117 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
2118 %% Receive the response.
2119 {ok, << Length1:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
2120 {ok, _} = gen_tcp:recv(Socket, Length1, 6000),
2121 {ok, << Length2:24, 0:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
2122 {ok, _} = gen_tcp:recv(Socket, Length2, 6000),
2123 %% Send a DATA frame on the now closed stream.
2124 ok = gen_tcp:send(Socket, cow_http2:data(1, fin, <<"Unexpected DATA frame.">>)),
2125 %% Receive a STREAM_CLOSED connection error.
2126 {ok, << _:24, 7:8, _:72, 5:32 >>} = gen_tcp:recv(Socket, 17, 6000),
2127 ok.
2128
2129 stream_closed_reject_headers(Config) ->
2130 doc("HEADERS frames received on a stream closed normally must be rejected "
2131 "with a STREAM_CLOSED connection error. (RFC7540 5.1)"),
2132 {ok, Socket} = do_handshake(Config),
2133 %% Send a HEADERS frame.
2134 {HeadersBlock, _} = cow_hpack:encode([
2135 {<<":method">>, <<"GET">>},
2136 {<<":scheme">>, <<"http">>},
2137 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
2138 {<<":path">>, <<"/">>}
2139 ]),
2140 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
2141 %% Receive the response.
2142 {ok, << Length1:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
2143 {ok, _} = gen_tcp:recv(Socket, Length1, 6000),
2144 {ok, << Length2:24, 0:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
2145 {ok, _} = gen_tcp:recv(Socket, Length2, 6000),
2146 %% Send a HEADERS frame on the now closed stream.
2147 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
2148 %% Receive a STREAM_CLOSED connection error.
2149 {ok, << _:24, 7:8, _:72, 5:32 >>} = gen_tcp:recv(Socket, 17, 6000),
2150 ok.
2151
2152 stream_closed_accept_priority(Config) ->
2153 doc("PRIORITY frames received on a stream closed normally must be accepted. (RFC7540 5.1)"),
2154 {ok, Socket} = do_handshake(Config),
2155 %% Send a HEADERS frame.
2156 {HeadersBlock, _} = cow_hpack:encode([
2157 {<<":method">>, <<"GET">>},
2158 {<<":scheme">>, <<"http">>},
2159 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
2160 {<<":path">>, <<"/">>}
2161 ]),
2162 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
2163 %% Receive the response.
2164 {ok, << Length1:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
2165 {ok, _} = gen_tcp:recv(Socket, Length1, 6000),
2166 {ok, << Length2:24, 0:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
2167 {ok, _} = gen_tcp:recv(Socket, Length2, 6000),
2168 %% Send a PRIORITY frame on the now closed stream.
2169 ok = gen_tcp:send(Socket, cow_http2:priority(1, shared, 3, 123)),
2170 %% Receive nothing back.
2171 {error, timeout} = gen_tcp:recv(Socket, 9, 6000),
2172 ok.
2173
2174 stream_closed_accept_rst_stream(Config) ->
2175 doc("RST_STREAM frames received on a stream closed normally "
2176 "must be accepted for a short period. (RFC7540 5.1)"),
2177 {ok, Socket} = do_handshake(Config),
2178 %% Send a HEADERS frame.
2179 {HeadersBlock, _} = cow_hpack:encode([
2180 {<<":method">>, <<"GET">>},
2181 {<<":scheme">>, <<"http">>},
2182 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
2183 {<<":path">>, <<"/">>}
2184 ]),
2185 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
2186 %% Receive the response.
2187 {ok, << Length1:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
2188 {ok, _} = gen_tcp:recv(Socket, Length1, 6000),
2189 {ok, << Length2:24, 0:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
2190 {ok, _} = gen_tcp:recv(Socket, Length2, 6000),
2191 %% Send an RST_STREAM frame on the now closed stream.
2192 ok = gen_tcp:send(Socket, cow_http2:rst_stream(1, cancel)),
2193 %% Receive nothing back.
2194 {error, timeout} = gen_tcp:recv(Socket, 9, 6000),
2195 ok.
2196
2197 %% stream_closed_reject_push_promise
2198 %%
2199 %% We respond to all PUSH_PROMISE frames with a PROTOCOL_ERROR connection error
2200 %% because PUSH is disabled in that direction. We therefore cannot test other
2201 %% error conditions.
2202
2203 stream_closed_accept_window_update(Config) ->
2204 doc("WINDOW_UPDATE frames received on a stream closed normally "
2205 "must be accepted for a short period. (RFC7540 5.1)"),
2206 {ok, Socket} = do_handshake(Config),
2207 %% Send a HEADERS frame.
2208 {HeadersBlock, _} = cow_hpack:encode([
2209 {<<":method">>, <<"GET">>},
2210 {<<":scheme">>, <<"http">>},
2211 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
2212 {<<":path">>, <<"/">>}
2213 ]),
2214 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
2215 %% Receive the response.
2216 {ok, << Length1:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
2217 {ok, _} = gen_tcp:recv(Socket, Length1, 6000),
2218 {ok, << Length2:24, 0:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
2219 {ok, _} = gen_tcp:recv(Socket, Length2, 6000),
2220 %% Send a WINDOW_UPDATE frame on the now closed stream.
2221 ok = gen_tcp:send(Socket, cow_http2:window_update(1, 12345)),
2222 %% Receive nothing back.
2223 {error, timeout} = gen_tcp:recv(Socket, 9, 6000),
2224 ok.
2225
2226 %% @todo While we accept RST_STREAM and WINDOW_UPDATE for a short period
2227 %% after the stream closed normally, we may want to reject the ones coming
2228 %% a significant amount of time after that.
2229
2230 %% @todo Frames may arrive on a stream after we send an RST_STREAM for it.
2231 %% They must be ignored for a short period of time:
2232 %
2233 % If this state is reached as a result of sending a RST_STREAM
2234 % frame, the peer that receives the RST_STREAM might have already
2235 % sent -- or enqueued for sending -- frames on the stream that
2236 % cannot be withdrawn. An endpoint MUST ignore frames that it
2237 % receives on closed streams after it has sent a RST_STREAM frame.
2238 % An endpoint MAY choose to limit the period over which it ignores
2239 % frames and treat frames that arrive after this time as being in
2240 % error.
2241
2242 %% @todo Ensure that rejected DATA frames result in the connection
2243 %% flow-control window being updated. How to test this?
2244 %
2245 % Flow-controlled frames (i.e., DATA) received after sending
2246 % RST_STREAM are counted toward the connection flow-control window.
2247 % Even though these frames might be ignored, because they are sent
2248 % before the sender receives the RST_STREAM, the sender will
2249 % consider the frames to count against the flow-control window.
2250
2251 %% Stream identifiers.
2252
2253 reject_streamid_even(Config) ->
2254 doc("HEADERS frames received with an even-numbered streamid "
2255 "must be rejected with a PROTOCOL_ERROR connection error. (RFC7540 5.1.1)"),
2256 {ok, Socket} = do_handshake(Config),
2257 %% Send a HEADERS frame with an even-numbered streamid.
2258 {HeadersBlock, _} = cow_hpack:encode([
2259 {<<":method">>, <<"GET">>},
2260 {<<":scheme">>, <<"http">>},
2261 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
2262 {<<":path">>, <<"/">>}
2263 ]),
2264 ok = gen_tcp:send(Socket, cow_http2:headers(2, fin, HeadersBlock)),
2265 %% Receive a PROTOCOL_ERROR connection error.
2266 {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
2267 ok.
2268
2269 reject_streamid_0(Config) ->
2270 doc("HEADERS frames received with streamid 0 (zero) "
2271 "must be rejected with a PROTOCOL_ERROR connection error. (RFC7540 5.1.1)"),
2272 {ok, Socket} = do_handshake(Config),
2273 %% Send a HEADERS frame with an streamid 0.
2274 {HeadersBlock, _} = cow_hpack:encode([
2275 {<<":method">>, <<"GET">>},
2276 {<<":scheme">>, <<"http">>},
2277 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
2278 {<<":path">>, <<"/">>}
2279 ]),
2280 ok = gen_tcp:send(Socket, cow_http2:headers(0, fin, HeadersBlock)),
2281 %% Receive a PROTOCOL_ERROR connection error.
2282 {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
2283 ok.
2284
2285 %% See the comment for reject_streamid_lower for the rationale behind
2286 %% having a STREAM_CLOSED connection error.
2287 http_upgrade_reject_reuse_streamid_1(Config) ->
2288 doc("Attempts to reuse streamid 1 after upgrading to HTTP/2 "
2289 "must be rejected with a STREAM_CLOSED connection error. (RFC7540 5.1.1)"),
2290 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
2291 ok = gen_tcp:send(Socket, [
2292 "GET / HTTP/1.1\r\n"
2293 "Host: localhost\r\n"
2294 "Connection: Upgrade, HTTP2-Settings\r\n"
2295 "Upgrade: h2c\r\n"
2296 "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
2297 "\r\n"]),
2298 ok = do_recv_101(Socket),
2299 %% Send a valid preface.
2300 ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
2301 %% Receive the server preface.
2302 {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
2303 {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
2304 %% Send the SETTINGS ack.
2305 ok = gen_tcp:send(Socket, cow_http2:settings_ack()),
2306 %% Receive the SETTINGS ack, and the response HEADERS and DATA (Stream ID 1).
2307 Received = lists:reverse(lists:foldl(fun(_, Acc) ->
2308 case gen_tcp:recv(Socket, 9, 1000) of
2309 {ok, << 0:24, 4:8, 1:8, 0:32 >>} ->
2310 [settings_ack|Acc];
2311 {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} ->
2312 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
2313 [headers|Acc];
2314 {ok, << SkipLen:24, 0:8, _:8, 1:32 >>} ->
2315 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
2316 [data|Acc]
2317 end
2318 end, [], [1, 2, 3])),
2319 case Received of
2320 [settings_ack, headers, data] -> ok;
2321 [headers, settings_ack, data] -> ok;
2322 [headers, data, settings_ack] -> ok
2323 end,
2324 %% Send a HEADERS frame with streamid 1.
2325 {HeadersBlock, _} = cow_hpack:encode([
2326 {<<":method">>, <<"GET">>},
2327 {<<":scheme">>, <<"http">>},
2328 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
2329 {<<":path">>, <<"/">>}
2330 ]),
2331 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
2332 %% Receive a STREAM_CLOSED connection error.
2333 {ok, << _:24, 7:8, _:72, 5:32 >>} = gen_tcp:recv(Socket, 17, 6000),
2334 ok.
2335
2336 %% The RFC gives us various error codes to return for this case,
2337 %% depending on whether the stream existed previously and how it
2338 %% ended up being (half-)closed. Cowboy rejects all these HEADERS
2339 %% frames the same way: with a STREAM_CLOSED connection error.
2340 %% Making it a connection error is particularly important in the
2341 %% cases where a stream error would be allowed because we avoid
2342 %% having to decode the headers and save up resources.
2343 reject_streamid_lower(Config) ->
2344 doc("HEADERS frames received with streamid lower than the previous stream "
2345 "must be rejected with a STREAM_CLOSED connection error. (RFC7540 5.1.1)"),
2346 {ok, Socket} = do_handshake(Config),
2347 %% Send a HEADERS frame with streamid 5.
2348 {HeadersBlock, _} = cow_hpack:encode([
2349 {<<":method">>, <<"GET">>},
2350 {<<":scheme">>, <<"http">>},
2351 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
2352 {<<":path">>, <<"/">>}
2353 ]),
2354 ok = gen_tcp:send(Socket, cow_http2:headers(5, fin, HeadersBlock)),
2355 %% Receive the response.
2356 {ok, << Length1:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
2357 {ok, _} = gen_tcp:recv(Socket, Length1, 6000),
2358 {ok, << Length2:24, 0:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
2359 {ok, _} = gen_tcp:recv(Socket, Length2, 6000),
2360 %% Send a HEADERS frame with streamid 3.
2361 ok = gen_tcp:send(Socket, cow_http2:headers(3, fin, HeadersBlock)),
2362 %% Receive a STREAM_CLOSED connection error.
2363 {ok, << _:24, 7:8, _:72, 5:32 >>} = gen_tcp:recv(Socket, 17, 6000),
2364 ok.
2365
2366 %% @todo We need an option to limit the number of streams one can open
2367 %% on a connection. And we need to enforce it. (RFC7540 5.1.1)
2368 %
2369 % Stream identifiers cannot be reused. Long-lived connections can
2370 % result in an endpoint exhausting the available range of stream
2371 % identifiers. A server
2372 % that is unable to establish a new stream identifier can send a GOAWAY
2373 % frame so that the client is forced to open a new connection for new
2374 % streams.
2375
2376 %% (RFC7540 5.2.1)
2377 % 3. Flow control is directional with overall control provided by the
2378 % receiver. A receiver MAY choose to set any window size that it
2379 % desires for each stream and for the entire connection. A sender
2380 % MUST respect flow-control limits imposed by a receiver. Clients,
2381 % servers, and intermediaries all independently advertise their
2382 % flow-control window as a receiver and abide by the flow-control
2383 % limits set by their peer when sending.
2384 %
2385 % 4. The initial value for the flow-control window is 65,535 octets
2386 % for both new streams and the overall connection.
2387 %
2388 % 5. The frame type determines whether flow control applies to a
2389 % frame. Of the frames specified in this document, only DATA
2390 % frames are subject to flow control; all other frame types do not
2391 % consume space in the advertised flow-control window. This
2392 % ensures that important control frames are not blocked by flow
2393 % control.
2394 %
2395 % 6. Flow control cannot be disabled.
2396
2397 %% (RFC7540 5.2.2)
2398 % Even with full awareness of the current bandwidth-delay product,
2399 % implementation of flow control can be difficult. When using flow
2400 % control, the receiver MUST read from the TCP receive buffer in a
2401 % timely fashion. Failure to do so could lead to a deadlock when
2402 % critical frames, such as WINDOW_UPDATE, are not read and acted upon.
2403
2404 %% (RFC7540 5.3.1)
2405 % Inside the dependency tree, a dependent stream SHOULD only be
2406 % allocated resources if either all of the streams that it depends on
2407 % (the chain of parent streams up to 0x0) are closed or it is not
2408 % possible to make progress on them.
2409
2410 %% We reject all invalid HEADERS with a connection error because
2411 %% we do not want to waste resources decoding them.
2412 reject_self_dependent_stream_headers(Config) ->
2413 doc("HEADERS frames opening a stream that depends on itself "
2414 "must be rejected with a PROTOCOL_ERROR connection error. (RFC7540 5.3.1)"),
2415 {ok, Socket} = do_handshake(Config),
2416 %% Send a HEADERS frame with priority set to depend on itself.
2417 ok = gen_tcp:send(Socket, << 5:24, 1:8,
2418 0:2, 1:1, 0:4, 1:1, 0:1, 1:31, 0:1, 1:31, 0:8 >>),
2419 %% Receive a PROTOCOL_ERROR connection error.
2420 {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
2421 ok.
2422
2423 %% We reject all invalid HEADERS with a connection error because
2424 %% we do not want to waste resources decoding them.
2425 reject_self_dependent_stream_headers_with_padding(Config) ->
2426 doc("HEADERS frames opening a stream that depends on itself "
2427 "must be rejected with a PROTOCOL_ERROR connection error. (RFC7540 5.3.1)"),
2428 {ok, Socket} = do_handshake(Config),
2429 %% Send a HEADERS frame with priority set to depend on itself.
2430 ok = gen_tcp:send(Socket, << 6:24, 1:8,
2431 0:2, 1:1, 0:1, 1:1, 0:2, 1:1, 0:1, 1:31, 0:8, 0:1, 1:31, 0:8 >>),
2432 %% Receive a PROTOCOL_ERROR connection error.
2433 {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
2434 ok.
2435
2436 reject_self_dependent_stream_priority(Config) ->
2437 doc("PRIORITY frames making a stream depend on itself "
2438 "must be rejected with a PROTOCOL_ERROR stream error. (RFC7540 5.3.1)"),
2439 {ok, Socket} = do_handshake(Config),
2440 %% Send a PRIORITY frame making a stream depend on itself.
2441 ok = gen_tcp:send(Socket, cow_http2:priority(1, shared, 1, 123)),
2442 %% Receive a PROTOCOL_ERROR stream error.
2443 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
2444 ok.
2445
2446 %% @todo Stream priorities. (RFC7540 5.3.2 5.3.3 5.3.4 5.3.5)
2447
2448 %% (RFC7540 5.4.1)
2449 % An endpoint that encounters a connection error SHOULD first send a
2450 % GOAWAY frame (Section 6.8) with the stream identifier of the last
2451 % stream that it successfully received from its peer. The GOAWAY frame
2452 % includes an error code that indicates why the connection is
2453 % terminating. After sending the GOAWAY frame for an error condition,
2454 % the endpoint MUST close the TCP connection.
2455 %
2456 % An endpoint can end a connection at any time. In particular, an
2457 % endpoint MAY choose to treat a stream error as a connection error.
2458 % Endpoints SHOULD send a GOAWAY frame when ending a connection,
2459 % providing that circumstances permit it.
2460
2461 %% (RFC7540 5.4.2)
2462 % A RST_STREAM is the last frame that an endpoint can send on a stream.
2463 % The peer that sends the RST_STREAM frame MUST be prepared to receive
2464 % any frames that were sent or enqueued for sending by the remote peer.
2465 % These frames can be ignored, except where they modify connection
2466 % state (such as the state maintained for header compression
2467 % (Section 4.3) or flow control).
2468 %
2469 % Normally, an endpoint SHOULD NOT send more than one RST_STREAM frame
2470 % for any stream. However, an endpoint MAY send additional RST_STREAM
2471 % frames if it receives frames on a closed stream after more than a
2472 % round-trip time. This behavior is permitted to deal with misbehaving
2473 % implementations.
2474 %
2475 % To avoid looping, an endpoint MUST NOT send a RST_STREAM in response
2476 % to a RST_STREAM frame.
2477
2478 %% (RFC7540 5.5)
2479 % Extensions are permitted to use new frame types (Section 4.1), new
2480 % settings (Section 6.5.2), or new error codes (Section 7). Registries
2481 % are established for managing these extension points: frame types
2482 % (Section 11.2), settings (Section 11.3), and error codes
2483 % (Section 11.4).
2484 %
2485 % Implementations MUST ignore unknown or unsupported values in all
2486 % extensible protocol elements. Implementations MUST discard frames
2487 % that have unknown or unsupported types. This means that any of these
2488 % extension points can be safely used by extensions without prior
2489 % arrangement or negotiation. However, extension frames that appear in
2490 % the middle of a header block (Section 4.3) are not permitted; these
2491 % MUST be treated as a connection error (Section 5.4.1) of type
2492 % PROTOCOL_ERROR.
2493
2494 continuation_with_extension_frame_interleaved_error(Config) ->
2495 doc("Extension frames interleaved in a header block must be rejected "
2496 "with a PROTOCOL_ERROR connection error. "
2497 "(RFC7540 4.3, RFC7540 5.5, RFC7540 6.2, RFC7540 6.10)"),
2498 {ok, Socket} = do_handshake(Config),
2499 %% Send an unterminated HEADERS frame followed by an extension frame.
2500 ok = gen_tcp:send(Socket, [
2501 << 0:24, 1:8, 0:7, 1:1, 0:1, 1:31 >>,
2502 << 0:24, 128:8, 0:8, 0:32 >>
2503 ]),
2504 %% Receive a PROTOCOL_ERROR connection error.
2505 {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
2506 ok.
2507
2508 %% (RFC7540 6.1) DATA
2509 % Padding: Padding octets that contain no application semantic value.
2510 % Padding octets MUST be set to zero when sending. A receiver is
2511 % not obligated to verify padding but MAY treat non-zero padding as
2512 % a connection error (Section 5.4.1) of type PROTOCOL_ERROR.
2513 %
2514 % DATA frames MUST be associated with a stream. If a DATA frame is
2515 % received whose stream identifier field is 0x0, the recipient MUST
2516 % respond with a connection error (Section 5.4.1) of type
2517 % PROTOCOL_ERROR.
2518
2519 %% (RFC7540 6.2) HEADERS
2520 % Padding: Padding octets that contain no application semantic value.
2521 % Padding octets MUST be set to zero when sending. A receiver is
2522 % not obligated to verify padding but MAY treat non-zero padding as
2523 % a connection error (Section 5.4.1) of type PROTOCOL_ERROR.
2524 %
2525 % A HEADERS frame carries the END_STREAM flag that signals the end
2526 % of a stream. However, a HEADERS frame with the END_STREAM flag
2527 % set can be followed by CONTINUATION frames on the same stream.
2528 % Logically, the CONTINUATION frames are part of the HEADERS frame.
2529 %
2530 %% @todo We probably need a test for the server sending HEADERS too large.
2531 % The payload of a HEADERS frame contains a header block fragment
2532 % (Section 4.3). A header block that does not fit within a HEADERS
2533 % frame is continued in a CONTINUATION frame (Section 6.10).
2534 %
2535 % HEADERS frames MUST be associated with a stream. If a HEADERS frame
2536 % is received whose stream identifier field is 0x0, the recipient MUST
2537 % respond with a connection error (Section 5.4.1) of type
2538 % PROTOCOL_ERROR.
2539
2540 %% (RFC7540 6.3) PRIORITY
2541 % The PRIORITY frame always identifies a stream. If a PRIORITY frame
2542 % is received with a stream identifier of 0x0, the recipient MUST
2543 % respond with a connection error (Section 5.4.1) of type
2544 % PROTOCOL_ERROR.
2545
2546 %% (RFC7540 6.4) RST_STREAM
2547 % The RST_STREAM frame fully terminates the referenced stream and
2548 % causes it to enter the "closed" state. After receiving a RST_STREAM
2549 % on a stream, the receiver MUST NOT send additional frames for that
2550 % stream, with the exception of PRIORITY. However, after sending the
2551 % RST_STREAM, the sending endpoint MUST be prepared to receive and
2552 % process additional frames sent on the stream that might have been
2553 % sent by the peer prior to the arrival of the RST_STREAM.
2554 %
2555 % RST_STREAM frames MUST be associated with a stream. If a RST_STREAM
2556 % frame is received with a stream identifier of 0x0, the recipient MUST
2557 % treat this as a connection error (Section 5.4.1) of type
2558 % PROTOCOL_ERROR.
2559
2560 %% (RFC7540 6.5) SETTINGS
2561 % A SETTINGS frame MUST be sent by both endpoints at the start of a
2562 % connection and MAY be sent at any other time by either endpoint over
2563 % the lifetime of the connection. Implementations MUST support all of
2564 % the parameters defined by this specification.
2565 %
2566 % SETTINGS frames always apply to a connection, never a single stream.
2567 % The stream identifier for a SETTINGS frame MUST be zero (0x0). If an
2568 % endpoint receives a SETTINGS frame whose stream identifier field is
2569 % anything other than 0x0, the endpoint MUST respond with a connection
2570 % error (Section 5.4.1) of type PROTOCOL_ERROR.
2571 %
2572 % The SETTINGS frame affects connection state. A badly formed or
2573 % incomplete SETTINGS frame MUST be treated as a connection error
2574 % (Section 5.4.1) of type PROTOCOL_ERROR.
2575
2576 %% Settings.
2577
2578 settings_header_table_size_client(Config) ->
2579 doc("The SETTINGS_HEADER_TABLE_SIZE setting can be used to "
2580 "inform the server of the maximum header table size "
2581 "used by the client to decode header blocks. (RFC7540 6.5.2)"),
2582 HeaderTableSize = 128,
2583 %% Do the handhsake.
2584 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
2585 %% Send a valid preface.
2586 ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n",
2587 cow_http2:settings(#{header_table_size => HeaderTableSize})]),
2588 %% Receive the server preface.
2589 {ok, << Len0:24 >>} = gen_tcp:recv(Socket, 3, 1000),
2590 {ok, << 4:8, 0:40, _:Len0/binary >>} = gen_tcp:recv(Socket, 6 + Len0, 1000),
2591 %% Send the SETTINGS ack.
2592 ok = gen_tcp:send(Socket, cow_http2:settings_ack()),
2593 %% Receive the SETTINGS ack.
2594 {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
2595 %% Initialize decoding/encoding states.
2596 DecodeState = cow_hpack:set_max_size(HeaderTableSize, cow_hpack:init()),
2597 EncodeState = cow_hpack:init(),
2598 %% Send a HEADERS frame as a request.
2599 {ReqHeadersBlock1, _} = cow_hpack:encode([
2600 {<<":method">>, <<"GET">>},
2601 {<<":scheme">>, <<"http">>},
2602 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
2603 {<<":path">>, <<"/">>}
2604 ], EncodeState),
2605 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, ReqHeadersBlock1)),
2606 %% Receive a HEADERS frame as a response.
2607 {ok, << Len1:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
2608 {ok, RespHeadersBlock1} = gen_tcp:recv(Socket, Len1, 6000),
2609 {RespHeaders, _} = cow_hpack:decode(RespHeadersBlock1, DecodeState),
2610 {_, <<"200">>} = lists:keyfind(<<":status">>, 1, RespHeaders),
2611 %% The decoding succeeded, confirming that the table size is
2612 %% lower than or equal to HeaderTableSize.
2613 ok.
2614
2615 settings_header_table_size_server(Config0) ->
2616 doc("The SETTINGS_HEADER_TABLE_SIZE setting can be used to "
2617 "inform the client of the maximum header table size "
2618 "used by the server to decode header blocks. (RFC7540 6.5.2)"),
2619 HeaderTableSize = 128,
2620 %% Create a new listener that allows larger header table sizes.
2621 Config = cowboy_test:init_http(?FUNCTION_NAME, #{
2622 env => #{dispatch => cowboy_router:compile(init_routes(Config0))},
2623 max_decode_table_size => HeaderTableSize
2624 }, Config0),
2625 try
2626 %% Do the handhsake.
2627 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
2628 %% Send a valid preface.
2629 ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n",
2630 cow_http2:settings(#{header_table_size => HeaderTableSize})]),
2631 %% Receive the server preface.
2632 {ok, << Len0:24 >>} = gen_tcp:recv(Socket, 3, 1000),
2633 {ok, Data = <<_:48, _:Len0/binary>>} = gen_tcp:recv(Socket, 6 + Len0, 1000),
2634 %% Confirm the server's SETTINGS_HEADERS_TABLE_SIZE uses HeaderTableSize.
2635 {ok, {settings, #{header_table_size := HeaderTableSize}}, <<>>}
2636 = cow_http2:parse(<<Len0:24, Data/binary>>),
2637 %% Send the SETTINGS ack.
2638 ok = gen_tcp:send(Socket, cow_http2:settings_ack()),
2639 %% Receive the SETTINGS ack.
2640 {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
2641 %% Initialize decoding/encoding states.
2642 DecodeState = cow_hpack:init(),
2643 EncodeState = cow_hpack:set_max_size(HeaderTableSize, cow_hpack:init()),
2644 %% Send a HEADERS frame as a request.
2645 {ReqHeadersBlock1, _} = cow_hpack:encode([
2646 {<<":method">>, <<"GET">>},
2647 {<<":scheme">>, <<"http">>},
2648 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
2649 {<<":path">>, <<"/">>}
2650 ], EncodeState),
2651 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, ReqHeadersBlock1)),
2652 %% Receive a HEADERS frame as a response.
2653 {ok, << Len1:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
2654 {ok, RespHeadersBlock1} = gen_tcp:recv(Socket, Len1, 6000),
2655 {RespHeaders, _} = cow_hpack:decode(RespHeadersBlock1, DecodeState),
2656 {_, <<"200">>} = lists:keyfind(<<":status">>, 1, RespHeaders),
2657 %% The decoding succeeded on the server, confirming that
2658 %% the table size was updated to HeaderTableSize.
2659 gen_tcp:close(Socket)
2660 after
2661 cowboy:stop_listener(?FUNCTION_NAME)
2662 end.
2663
2664 settings_max_concurrent_streams(Config0) ->
2665 doc("The SETTINGS_MAX_CONCURRENT_STREAMS setting can be used to "
2666 "restrict the number of concurrent streams. (RFC7540 5.1.2, RFC7540 6.5.2)"),
2667 %% Create a new listener that allows only a single concurrent stream.
2668 Config = cowboy_test:init_http(?FUNCTION_NAME, #{
2669 env => #{dispatch => cowboy_router:compile(init_routes(Config0))},
2670 max_concurrent_streams => 1
2671 }, Config0),
2672 try
2673 {ok, Socket} = do_handshake(Config),
2674 %% Send two HEADERS frames as two separate streams.
2675 Headers = [
2676 {<<":method">>, <<"GET">>},
2677 {<<":scheme">>, <<"http">>},
2678 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
2679 {<<":path">>, <<"/long_polling">>}
2680 ],
2681 {ReqHeadersBlock1, EncodeState} = cow_hpack:encode(Headers),
2682 {ReqHeadersBlock2, _} = cow_hpack:encode(Headers, EncodeState),
2683 ok = gen_tcp:send(Socket, [
2684 cow_http2:headers(1, fin, ReqHeadersBlock1),
2685 cow_http2:headers(3, fin, ReqHeadersBlock2)
2686 ]),
2687 %% Receive a REFUSED_STREAM stream error.
2688 {ok, << _:24, 3:8, _:8, 3:32, 7:32 >>} = gen_tcp:recv(Socket, 13, 6000),
2689 gen_tcp:close(Socket)
2690 after
2691 cowboy:stop_listener(?FUNCTION_NAME)
2692 end.
2693
2694 settings_max_concurrent_streams_0(Config0) ->
2695 doc("The SETTINGS_MAX_CONCURRENT_STREAMS setting can be set to "
2696 "0 to refuse all incoming streams. (RFC7540 5.1.2, RFC7540 6.5.2)"),
2697 %% Create a new listener that allows only a single concurrent stream.
2698 Config = cowboy_test:init_http(?FUNCTION_NAME, #{
2699 env => #{dispatch => cowboy_router:compile(init_routes(Config0))},
2700 max_concurrent_streams => 0
2701 }, Config0),
2702 try
2703 {ok, Socket} = do_handshake(Config),
2704 %% Send a HEADERS frame.
2705 {HeadersBlock, _} = cow_hpack:encode([
2706 {<<":method">>, <<"GET">>},
2707 {<<":scheme">>, <<"http">>},
2708 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
2709 {<<":path">>, <<"/long_polling">>}
2710 ]),
2711 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
2712 %% Receive a REFUSED_STREAM stream error.
2713 {ok, << _:24, 3:8, _:8, 1:32, 7:32 >>} = gen_tcp:recv(Socket, 13, 6000),
2714 gen_tcp:close(Socket)
2715 after
2716 cowboy:stop_listener(?FUNCTION_NAME)
2717 end.
2718
2719 %% @todo The client can limit the number of concurrent streams too. (RFC7540 5.1.2)
2720 %
2721 % A peer can limit the number of concurrently active streams using the
2722 % SETTINGS_MAX_CONCURRENT_STREAMS parameter (see Section 6.5.2) within
2723 % a SETTINGS frame. The maximum concurrent streams setting is specific
2724 % to each endpoint and applies only to the peer that receives the
2725 % setting. That is, clients specify the maximum number of concurrent
2726 % streams the server can initiate, and servers specify the maximum
2727 % number of concurrent streams the client can initiate.
2728 %
2729 % Endpoints MUST NOT exceed the limit set by their peer. An endpoint
2730 % that receives a HEADERS frame that causes its advertised concurrent
2731 % stream limit to be exceeded MUST treat this as a stream error
2732 % (Section 5.4.2) of type PROTOCOL_ERROR or REFUSED_STREAM. The choice
2733 % of error code determines whether the endpoint wishes to enable
2734 % automatic retry (see Section 8.1.4) for details).
2735
2736 settings_initial_window_size(Config0) ->
2737 doc("The SETTINGS_INITIAL_WINDOW_SIZE setting can be used to "
2738 "change the initial window size of streams. (RFC7540 6.5.2)"),
2739 %% Create a new listener that sets initial window sizes to 100000.
2740 Config = cowboy_test:init_http(?FUNCTION_NAME, #{
2741 env => #{dispatch => cowboy_router:compile(init_routes(Config0))},
2742 initial_connection_window_size => 100000,
2743 initial_stream_window_size => 100000
2744 }, Config0),
2745 try
2746 %% We need to do the handshake manually because a WINDOW_UPDATE
2747 %% frame will be sent to update the connection window.
2748 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
2749 %% Send a valid preface.
2750 ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
2751 %% Receive the server preface.
2752 {ok, << Len1:24 >>} = gen_tcp:recv(Socket, 3, 1000),
2753 {ok, << 4:8, 0:40, _:Len1/binary >>} = gen_tcp:recv(Socket, 6 + Len1, 1000),
2754 %% Send the SETTINGS ack.
2755 ok = gen_tcp:send(Socket, cow_http2:settings_ack()),
2756 %% Receive the WINDOW_UPDATE for the connection.
2757 {ok, << 4:24, 8:8, 0:40, _:32 >>} = gen_tcp:recv(Socket, 13, 1000),
2758 %% Receive the SETTINGS ack.
2759 {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
2760 %% Send a HEADERS frame initiating a stream followed by
2761 %% DATA frames totaling 90000 bytes of body.
2762 Headers = [
2763 {<<":method">>, <<"POST">>},
2764 {<<":scheme">>, <<"http">>},
2765 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
2766 {<<":path">>, <<"/long_polling">>}
2767 ],
2768 {HeadersBlock, _} = cow_hpack:encode(Headers),
2769 ok = gen_tcp:send(Socket, [
2770 cow_http2:headers(1, nofin, HeadersBlock),
2771 cow_http2:data(1, nofin, <<0:15000/unit:8>>),
2772 cow_http2:data(1, nofin, <<0:15000/unit:8>>),
2773 cow_http2:data(1, nofin, <<0:15000/unit:8>>),
2774 cow_http2:data(1, nofin, <<0:15000/unit:8>>),
2775 cow_http2:data(1, nofin, <<0:15000/unit:8>>),
2776 cow_http2:data(1, fin, <<0:15000/unit:8>>)
2777 ]),
2778 %% Receive a proper response.
2779 {ok, << Len2:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
2780 {ok, _} = gen_tcp:recv(Socket, Len2, 6000),
2781 %% No errors follow due to our sending of more than 65535 bytes of data.
2782 {error, timeout} = gen_tcp:recv(Socket, 0, 1000),
2783 gen_tcp:close(Socket)
2784 after
2785 cowboy:stop_listener(?FUNCTION_NAME)
2786 end.
2787
2788 settings_initial_window_size_after_ack(Config0) ->
2789 doc("The SETTINGS_INITIAL_WINDOW_SIZE setting can be used to "
2790 "change the initial window size of streams. It is applied "
2791 "to all existing streams upon receipt of the SETTINGS ack. (RFC7540 6.5.2)"),
2792 %% Create a new listener that sets the initial stream window sizes to 0.
2793 Config = cowboy_test:init_http(?FUNCTION_NAME, #{
2794 env => #{dispatch => cowboy_router:compile(init_routes(Config0))},
2795 initial_stream_window_size => 0
2796 }, Config0),
2797 try
2798 %% We need to do the handshake manually because we don't
2799 %% want to send the SETTINGS ack immediately.
2800 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
2801 %% Send a valid preface.
2802 ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
2803 %% Receive the server preface.
2804 {ok, << Len1:24 >>} = gen_tcp:recv(Socket, 3, 1000),
2805 {ok, << 4:8, 0:40, _:Len1/binary >>} = gen_tcp:recv(Socket, 6 + Len1, 1000),
2806 %%
2807 %% Don't send the SETTINGS ack yet! We want to create a stream first.
2808 %%
2809 %% Receive the SETTINGS ack.
2810 {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
2811 %% Send a HEADERS frame initiating a stream, a SETTINGS ack
2812 %% and a small DATA frame despite no window available in the stream.
2813 Headers = [
2814 {<<":method">>, <<"POST">>},
2815 {<<":scheme">>, <<"http">>},
2816 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
2817 {<<":path">>, <<"/long_polling">>}
2818 ],
2819 {HeadersBlock, _} = cow_hpack:encode(Headers),
2820 ok = gen_tcp:send(Socket, [
2821 cow_http2:headers(1, nofin, HeadersBlock),
2822 cow_http2:settings_ack(),
2823 cow_http2:data(1, fin, <<0:32/unit:8>>)
2824 ]),
2825 %% Receive a FLOW_CONTROL_ERROR stream error.
2826 {ok, << _:24, 3:8, _:8, 1:32, 3:32 >>} = gen_tcp:recv(Socket, 13, 6000),
2827 gen_tcp:close(Socket)
2828 after
2829 cowboy:stop_listener(?FUNCTION_NAME)
2830 end.
2831
2832 settings_initial_window_size_before_ack(Config0) ->
2833 doc("The SETTINGS_INITIAL_WINDOW_SIZE setting can be used to "
2834 "change the initial window size of streams. It is only "
2835 "applied upon receipt of the SETTINGS ack. (RFC7540 6.5.2)"),
2836 %% Create a new listener that sets the initial stream window sizes to 0.
2837 Config = cowboy_test:init_http(?FUNCTION_NAME, #{
2838 env => #{dispatch => cowboy_router:compile(init_routes(Config0))},
2839 initial_stream_window_size => 0
2840 }, Config0),
2841 try
2842 %% We need to do the handshake manually because we don't
2843 %% want to send the SETTINGS ack.
2844 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
2845 %% Send a valid preface.
2846 ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
2847 %% Receive the server preface.
2848 {ok, << Len1:24 >>} = gen_tcp:recv(Socket, 3, 1000),
2849 {ok, << 4:8, 0:40, _:Len1/binary >>} = gen_tcp:recv(Socket, 6 + Len1, 1000),
2850 %%
2851 %% Don't send the SETTINGS ack! We want the server to keep the original settings.
2852 %%
2853 %% Receive the SETTINGS ack.
2854 {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
2855 %% Send a HEADERS frame initiating a stream followed by
2856 %% DATA frames totaling 60000 bytes of body.
2857 Headers = [
2858 {<<":method">>, <<"POST">>},
2859 {<<":scheme">>, <<"http">>},
2860 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
2861 {<<":path">>, <<"/long_polling">>}
2862 ],
2863 {HeadersBlock, _} = cow_hpack:encode(Headers),
2864 ok = gen_tcp:send(Socket, [
2865 cow_http2:headers(1, nofin, HeadersBlock),
2866 cow_http2:data(1, nofin, <<0:15000/unit:8>>),
2867 cow_http2:data(1, nofin, <<0:15000/unit:8>>),
2868 cow_http2:data(1, nofin, <<0:15000/unit:8>>),
2869 cow_http2:data(1, fin, <<0:15000/unit:8>>)
2870 ]),
2871 %% Receive a proper response.
2872 {ok, << Len2:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
2873 {ok, _} = gen_tcp:recv(Socket, Len2, 6000),
2874 %% No errors follow due to our sending of more than 0 bytes of data.
2875 {error, timeout} = gen_tcp:recv(Socket, 0, 1000),
2876 gen_tcp:close(Socket)
2877 after
2878 cowboy:stop_listener(?FUNCTION_NAME)
2879 end.
2880
2881 settings_max_frame_size(Config0) ->
2882 doc("The SETTINGS_MAX_FRAME_SIZE setting can be used to "
2883 "change the maximum frame size allowed. (RFC7540 6.5.2)"),
2884 %% Create a new listener that sets the maximum frame size to 30000.
2885 Config = cowboy_test:init_http(?FUNCTION_NAME, #{
2886 env => #{dispatch => cowboy_router:compile(init_routes(Config0))},
2887 max_frame_size_received => 30000
2888 }, Config0),
2889 try
2890 %% Do the handshake.
2891 {ok, Socket} = do_handshake(Config),
2892 %% Send a HEADERS frame initiating a stream followed by
2893 %% a single 25000 bytes DATA frame.
2894 Headers = [
2895 {<<":method">>, <<"POST">>},
2896 {<<":scheme">>, <<"http">>},
2897 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
2898 {<<":path">>, <<"/long_polling">>}
2899 ],
2900 {HeadersBlock, _} = cow_hpack:encode(Headers),
2901 ok = gen_tcp:send(Socket, [
2902 cow_http2:headers(1, nofin, HeadersBlock),
2903 cow_http2:data(1, fin, <<0:25000/unit:8>>)
2904 ]),
2905 %% Receive a proper response.
2906 {ok, << Len2:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
2907 {ok, _} = gen_tcp:recv(Socket, Len2, 6000),
2908 %% No errors follow due to our sending of a 25000 bytes frame.
2909 {error, timeout} = gen_tcp:recv(Socket, 0, 1000),
2910 gen_tcp:close(Socket)
2911 after
2912 cowboy:stop_listener(?FUNCTION_NAME)
2913 end.
2914
2915 settings_max_frame_size_reject_too_small(Config) ->
2916 doc("A SETTINGS_MAX_FRAME_SIZE smaller than 16384 must be rejected "
2917 "with a PROTOCOL_ERROR connection error. (RFC7540 6.5.2)"),
2918 {ok, Socket} = do_handshake(Config),
2919 %% Send a SETTINGS frame with a SETTINGS_MAX_FRAME_SIZE lower than 16384.
2920 ok = gen_tcp:send(Socket, << 6:24, 4:8, 0:40, 5:16, 16383:32 >>),
2921 %% Receive a PROTOCOL_ERROR connection error.
2922 {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
2923 ok.
2924
2925 settings_max_frame_size_reject_too_large(Config) ->
2926 doc("A SETTINGS_MAX_FRAME_SIZE larger than 16777215 must be rejected "
2927 "with a PROTOCOL_ERROR connection error. (RFC7540 6.5.2)"),
2928 {ok, Socket} = do_handshake(Config),
2929 %% Send a SETTINGS frame with a SETTINGS_MAX_FRAME_SIZE larger than 16777215.
2930 ok = gen_tcp:send(Socket, << 6:24, 4:8, 0:40, 5:16, 16777216:32 >>),
2931 %% Receive a PROTOCOL_ERROR connection error.
2932 {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
2933 ok.
2934
2935 % SETTINGS_MAX_HEADER_LIST_SIZE (0x6): This advisory setting informs a
2936 % peer of the maximum size of header list that the sender is
2937 % prepared to accept, in octets. The value is based on the
2938 % uncompressed size of header fields, including the length of the
2939 % name and value in octets plus an overhead of 32 octets for each
2940 % header field.
2941 %
2942 % For any given request, a lower limit than what is advertised MAY
2943 % be enforced. The initial value of this setting is unlimited.
2944 %
2945 % An endpoint that receives a SETTINGS frame with any unknown or
2946 % unsupported identifier MUST ignore that setting. (6.5.2 and 6.5.3)
2947
2948 %% (RFC7540 6.5.3)
2949 % Upon receiving a SETTINGS frame with the ACK flag set, the
2950 % sender of the altered parameters can rely on the setting having been
2951 % applied.
2952 %
2953 % If the sender of a SETTINGS frame does not receive an acknowledgement
2954 % within a reasonable amount of time, it MAY issue a connection error
2955 % (Section 5.4.1) of type SETTINGS_TIMEOUT.
2956
2957 %% (RFC7540 6.6) PUSH_PROMISE
2958 % @todo PUSH_PROMISE frames have a reserved bit in the payload that must be ignored.
2959 %
2960 % Padding: Padding octets that contain no application semantic value.
2961 % Padding octets MUST be set to zero when sending. A receiver is
2962 % not obligated to verify padding but MAY treat non-zero padding as
2963 % a connection error (Section 5.4.1) of type PROTOCOL_ERROR.
2964 %
2965 % PUSH_PROMISE frames MUST only be sent on a peer-initiated stream that
2966 % is in either the "open" or "half-closed (remote)" state. The stream
2967 % identifier of a PUSH_PROMISE frame indicates the stream it is
2968 % associated with. If the stream identifier field specifies the value
2969 % 0x0, a recipient MUST respond with a connection error (Section 5.4.1)
2970 % of type PROTOCOL_ERROR.
2971
2972 client_settings_disable_push(Config) ->
2973 doc("PUSH_PROMISE frames must not be sent when the setting "
2974 "SETTINGS_ENABLE_PUSH is disabled. (RFC7540 6.5.2, RFC7540 6.6, RFC7540 8.2)"),
2975 %% Do a prior knowledge handshake.
2976 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
2977 %% Send a valid preface.
2978 ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{
2979 enable_push => false
2980 })]),
2981 %% Receive the server preface.
2982 {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
2983 {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
2984 %% Send the SETTINGS ack.
2985 ok = gen_tcp:send(Socket, cow_http2:settings_ack()),
2986 %% Receive the SETTINGS ack.
2987 {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
2988 %% Send a HEADERS frame on a resource that sends PUSH_PROMISE frames.
2989 {HeadersBlock, _} = cow_hpack:encode([
2990 {<<":method">>, <<"GET">>},
2991 {<<":scheme">>, <<"http">>},
2992 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
2993 {<<":path">>, <<"/resp/push">>}
2994 ]),
2995 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
2996 %% Receive a HEADERS frame as a response, no PUSH_PROMISE frames.
2997 {ok, << _:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
2998 ok.
2999
3000 % Since PUSH_PROMISE reserves a stream, ignoring a PUSH_PROMISE frame
3001 % causes the stream state to become indeterminate. A receiver MUST
3002 % treat the receipt of a PUSH_PROMISE on a stream that is neither
3003 % "open" nor "half-closed (local)" as a connection error
3004 % (Section 5.4.1) of type PROTOCOL_ERROR.
3005 %
3006 % A receiver MUST treat the receipt of a PUSH_PROMISE that promises an
3007 % illegal stream identifier (Section 5.1.1) as a connection error
3008 % (Section 5.4.1) of type PROTOCOL_ERROR. Note that an illegal stream
3009 % identifier is an identifier for a stream that is not currently in the
3010 % "idle" state.
3011
3012 %% (RFC7540 6.7) PING
3013 % PING frames are not associated with any individual stream. If a PING
3014 % frame is received with a stream identifier field value other than
3015 % 0x0, the recipient MUST respond with a connection error
3016 % (Section 5.4.1) of type PROTOCOL_ERROR.
3017
3018 %% (RFC7540 6.8) GOAWAY
3019 % @todo GOAWAY frames have a reserved bit in the payload that must be ignored.
3020 %
3021 % A GOAWAY frame might not immediately precede closing of the
3022 % connection; a receiver of a GOAWAY that has no more use for the
3023 % connection SHOULD still send a GOAWAY frame before terminating the
3024 % connection.
3025
3026 graceful_shutdown_client_stays(Config) ->
3027 doc("A server gracefully shutting down must send a GOAWAY frame with the "
3028 "last stream identifier set to 2^31-1 and a NO_ERROR code. After allowing "
3029 "time for any in-flight stream creation the server can send another GOAWAY "
3030 "frame with an updated last stream identifier. (RFC7540 6.8)"),
3031 {ok, Socket} = do_handshake(Config),
3032 ServerConnPid = get_remote_pid_tcp(Socket),
3033 ok = sys:terminate(ServerConnPid, whatever),
3034 %% First GOAWAY frame.
3035 {ok, <<_:24, 7:8, 0:8, 0:1, 0:31, 0:1, 16#7fffffff:31, 0:32>>} = gen_tcp:recv(Socket, 17, 500),
3036 %% Second GOAWAY frame.
3037 {ok, <<_:24, 7:8, 0:8, 0:1, 0:31, 0:1, 0:31, 0:32>>} = gen_tcp:recv(Socket, 17, 1500),
3038 {error, closed} = gen_tcp:recv(Socket, 3, 1000),
3039 ok.
3040
3041 %% @todo We should add this test also for discarded DATA and CONTINUATION frames.
3042 %% The test can be the same for CONTINUATION (just send headers differently) but
3043 %% the DATA test should make sure the global window is not corrupted.
3044 %%
3045 %% @todo We should extend this test to have two requests: one initiated before
3046 %% the second GOAWAY, but not terminated; another initiated after the GOAWAY, terminated.
3047 %% Finally the first request is terminated by sending a body and a trailing
3048 %% HEADERS frame. This way we know for sure that the connection state is not corrupt.
3049 graceful_shutdown_race_condition(Config) ->
3050 doc("A server in the process of gracefully shutting down must discard frames "
3051 "for streams initiated by the receiver with identifiers higher than the "
3052 "identified last stream. This may include frames that alter connection "
3053 "state such as HEADERS frames. (RFC7540 6.8)"),
3054 {ok, Socket} = do_handshake(Config),
3055 ServerConnPid = get_remote_pid_tcp(Socket),
3056 ok = sys:terminate(ServerConnPid, whatever),
3057 %% First GOAWAY frame.
3058 {ok, <<_:24, 7:8, 0:8, 0:1, 0:31, 0:1, 16#7fffffff:31, 0:32>>} = gen_tcp:recv(Socket, 17, 500),
3059 %% Simulate an in-flight request, sent by the client before the
3060 %% GOAWAY frame arrived to the client.
3061 {HeadersBlock, _} = cow_hpack:encode([
3062 {<<":method">>, <<"GET">>},
3063 {<<":scheme">>, <<"http">>},
3064 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3065 {<<":path">>, <<"/delay_hello">>}
3066 ]),
3067 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
3068 %% Second GOAWAY frame.
3069 {ok, <<_:24, 7:8, 0:8, 0:1, 0:31, 0:1, 1:31, 0:32>>} = gen_tcp:recv(Socket, 17, 2000),
3070 %% The client tries to send another request, ignoring the GOAWAY.
3071 ok = gen_tcp:send(Socket, cow_http2:headers(3, fin, HeadersBlock)),
3072 %% The server responds to the first request (streamid 1) and closes.
3073 {ok, <<RespHeadersPayloadLength:24, 1, 4, 0:1, 1:31>>} = gen_tcp:recv(Socket, 9, 1000),
3074 {ok, _RespHeaders} = gen_tcp:recv(Socket, RespHeadersPayloadLength, 1000),
3075 {ok, <<12:24, 0, 1, 0:1, 1:31, "Hello world!">>} = gen_tcp:recv(Socket, 21, 1000),
3076 {error, closed} = gen_tcp:recv(Socket, 3, 1000),
3077 ok.
3078
3079 % The GOAWAY frame applies to the connection, not a specific stream.
3080 % An endpoint MUST treat a GOAWAY frame with a stream identifier other
3081 % than 0x0 as a connection error (Section 5.4.1) of type
3082 % PROTOCOL_ERROR.
3083
3084 %% (RFC7540 6.9) WINDOW_UPDATE
3085 % @todo WINDOW_UPDATE frames have a reserved bit in the payload that must be ignored.
3086
3087 window_update_reject_0(Config) ->
3088 doc("WINDOW_UPDATE frames with an increment of 0 for the connection "
3089 "flow control window must be rejected with a "
3090 "PROTOCOL_ERROR connection error. (RFC7540 6.9.1)"),
3091 {ok, Socket} = do_handshake(Config),
3092 %% Send connection-wide WINDOW_UPDATE frame with a value of 0.
3093 ok = gen_tcp:send(Socket, [
3094 cow_http2:window_update(0)
3095 ]),
3096 %% Receive a PROTOCOL_ERROR connection error.
3097 {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
3098 ok.
3099
3100 window_update_reject_0_stream(Config) ->
3101 doc("WINDOW_UPDATE frames with an increment of 0 for a stream "
3102 "flow control window must be rejected with a "
3103 "PROTOCOL_ERROR stream error. (RFC7540 6.9.1)"),
3104 {ok, Socket} = do_handshake(Config),
3105 %% Send a HEADERS frame immediately followed by
3106 %% a WINDOW_UPDATE frame with a value of 0.
3107 {HeadersBlock, _} = cow_hpack:encode([
3108 {<<":method">>, <<"GET">>},
3109 {<<":scheme">>, <<"http">>},
3110 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3111 {<<":path">>, <<"/long_polling">>}
3112 ]),
3113 ok = gen_tcp:send(Socket, [
3114 cow_http2:headers(1, fin, HeadersBlock),
3115 cow_http2:window_update(1, 0)
3116 ]),
3117 %% Receive a PROTOCOL_ERROR stream error.
3118 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
3119 ok.
3120
3121 % A receiver that receives a flow-controlled frame MUST always account
3122 % for its contribution against the connection flow-control window,
3123 % unless the receiver treats this as a connection error
3124 % (Section 5.4.1). This is necessary even if the frame is in error.
3125 % The sender counts the frame toward the flow-control window, but if
3126 % the receiver does not, the flow-control window at the sender and
3127 % receiver can become different.
3128
3129 data_reject_overflow(Config0) ->
3130 doc("DATA frames that cause the connection flow control window "
3131 "to overflow must be rejected with a FLOW_CONTROL_ERROR "
3132 "connection error. (RFC7540 6.9.1)"),
3133 %% Create a new listener that allows only a single concurrent stream.
3134 Config = cowboy_test:init_http(?FUNCTION_NAME, #{
3135 env => #{dispatch => cowboy_router:compile(init_routes(Config0))},
3136 initial_stream_window_size => 100000
3137 }, Config0),
3138 try
3139 {ok, Socket} = do_handshake(Config),
3140 %% Send a HEADERS frame initiating a stream followed by
3141 %% DATA frames totaling 90000 bytes of body.
3142 Headers = [
3143 {<<":method">>, <<"POST">>},
3144 {<<":scheme">>, <<"http">>},
3145 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3146 {<<":path">>, <<"/long_polling">>}
3147 ],
3148 {HeadersBlock, _} = cow_hpack:encode(Headers),
3149 ok = gen_tcp:send(Socket, [
3150 cow_http2:headers(1, nofin, HeadersBlock),
3151 cow_http2:data(1, nofin, <<0:15000/unit:8>>),
3152 cow_http2:data(1, nofin, <<0:15000/unit:8>>),
3153 cow_http2:data(1, nofin, <<0:15000/unit:8>>),
3154 cow_http2:data(1, nofin, <<0:15000/unit:8>>),
3155 cow_http2:data(1, nofin, <<0:15000/unit:8>>),
3156 cow_http2:data(1, fin, <<0:15000/unit:8>>)
3157 ]),
3158 %% Receive a FLOW_CONTROL_ERROR connection error.
3159 {ok, << _:24, 7:8, _:72, 3:32 >>} = gen_tcp:recv(Socket, 17, 6000),
3160 gen_tcp:close(Socket)
3161 after
3162 cowboy:stop_listener(?FUNCTION_NAME)
3163 end.
3164
3165 data_reject_overflow_stream(Config0) ->
3166 doc("DATA frames that cause the stream flow control window "
3167 "to overflow must be rejected with a FLOW_CONTROL_ERROR "
3168 "stream error. (RFC7540 6.9.1)"),
3169 %% Create a new listener that allows only a single concurrent stream.
3170 Config = cowboy_test:init_http(?FUNCTION_NAME, #{
3171 env => #{dispatch => cowboy_router:compile(init_routes(Config0))},
3172 initial_connection_window_size => 100000
3173 }, Config0),
3174 try
3175 %% We need to do the handshake manually because a WINDOW_UPDATE
3176 %% frame will be sent to update the connection window.
3177 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
3178 %% Send a valid preface.
3179 ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
3180 %% Receive the server preface.
3181 {ok, << Len1:24 >>} = gen_tcp:recv(Socket, 3, 1000),
3182 {ok, << 4:8, 0:40, _:Len1/binary >>} = gen_tcp:recv(Socket, 6 + Len1, 1000),
3183 %% Send the SETTINGS ack.
3184 ok = gen_tcp:send(Socket, cow_http2:settings_ack()),
3185 %% Receive the WINDOW_UPDATE for the connection.
3186 {ok, << 4:24, 8:8, 0:40, _:32 >>} = gen_tcp:recv(Socket, 13, 1000),
3187 %% Receive the SETTINGS ack.
3188 {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
3189 %% Send a HEADERS frame initiating a stream followed by
3190 %% DATA frames totaling 90000 bytes of body.
3191 Headers = [
3192 {<<":method">>, <<"POST">>},
3193 {<<":scheme">>, <<"http">>},
3194 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3195 {<<":path">>, <<"/long_polling">>}
3196 ],
3197 {HeadersBlock, _} = cow_hpack:encode(Headers),
3198 ok = gen_tcp:send(Socket, [
3199 cow_http2:headers(1, nofin, HeadersBlock),
3200 cow_http2:data(1, nofin, <<0:15000/unit:8>>),
3201 cow_http2:data(1, nofin, <<0:15000/unit:8>>),
3202 cow_http2:data(1, nofin, <<0:15000/unit:8>>),
3203 cow_http2:data(1, nofin, <<0:15000/unit:8>>),
3204 cow_http2:data(1, nofin, <<0:15000/unit:8>>),
3205 cow_http2:data(1, fin, <<0:15000/unit:8>>)
3206 ]),
3207 %% Receive a FLOW_CONTROL_ERROR stream error.
3208 {ok, << _:24, 3:8, _:8, 1:32, 3:32 >>} = gen_tcp:recv(Socket, 13, 6000),
3209 gen_tcp:close(Socket)
3210 after
3211 cowboy:stop_listener(?FUNCTION_NAME)
3212 end.
3213
3214 %% (RFC7540 6.9.1)
3215 % Frames with zero length with the END_STREAM flag set (that
3216 % is, an empty DATA frame) MAY be sent if there is no available space
3217 % in either flow-control window.
3218
3219 window_update_reject_overflow(Config) ->
3220 doc("WINDOW_UPDATE frames that cause the connection flow control "
3221 "window to exceed 2^31-1 must be rejected with a "
3222 "FLOW_CONTROL_ERROR connection error. (RFC7540 6.9.1)"),
3223 {ok, Socket} = do_handshake(Config),
3224 %% Send a connection-wide WINDOW_UPDATE frame that causes the window to overflow.
3225 ok = gen_tcp:send(Socket, [
3226 cow_http2:window_update(16#7fffffff)
3227 ]),
3228 %% Receive a FLOW_CONTROL_ERROR connection error.
3229 {ok, << _:24, 7:8, _:72, 3:32 >>} = gen_tcp:recv(Socket, 17, 6000),
3230 ok.
3231
3232 window_update_reject_overflow_stream(Config) ->
3233 doc("WINDOW_UPDATE frames that cause a stream flow control "
3234 "window to exceed 2^31-1 must be rejected with a "
3235 "FLOW_CONTROL_ERROR stream error. (RFC7540 6.9.1)"),
3236 {ok, Socket} = do_handshake(Config),
3237 %% Send a HEADERS frame immediately followed by a WINDOW_UPDATE
3238 %% frame that causes the stream window to overflow.
3239 {HeadersBlock, _} = cow_hpack:encode([
3240 {<<":method">>, <<"GET">>},
3241 {<<":scheme">>, <<"http">>},
3242 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3243 {<<":path">>, <<"/long_polling">>}
3244 ]),
3245 ok = gen_tcp:send(Socket, [
3246 cow_http2:headers(1, fin, HeadersBlock),
3247 cow_http2:window_update(1, 16#7fffffff)
3248 ]),
3249 %% Receive a FLOW_CONTROL_ERROR stream error.
3250 {ok, << _:24, 3:8, _:8, 1:32, 3:32 >>} = gen_tcp:recv(Socket, 13, 6000),
3251 ok.
3252
3253 settings_initial_window_size_changes(Config) ->
3254 doc("When the value of SETTINGS_INITIAL_WINDOW_SIZE changes, the server "
3255 "must adjust the size of the flow control windows of the active "
3256 "streams. (RFC7540 6.9.2)"),
3257 {ok, Socket} = do_handshake(Config),
3258 %% Set SETTINGS_INITIAL_WINDOW_SIZE to 0 to prevent sending of DATA.
3259 ok = gen_tcp:send(Socket, cow_http2:settings(#{initial_window_size => 0})),
3260 %% Receive the SETTINGS ack.
3261 {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
3262 %% Send a HEADERS frame.
3263 {HeadersBlock, _} = cow_hpack:encode([
3264 {<<":method">>, <<"GET">>},
3265 {<<":scheme">>, <<"http">>},
3266 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3267 {<<":path">>, <<"/">>}
3268 ]),
3269 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
3270 %% Receive a response but no DATA frames are coming.
3271 {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
3272 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
3273 {error, timeout} = gen_tcp:recv(Socket, 9, 1000),
3274 %% Set SETTINGS_INITIAL_WINDOW_SIZE to a larger value.
3275 ok = gen_tcp:send(Socket, cow_http2:settings(#{initial_window_size => 5})),
3276 %% Receive the SETTINGS ack.
3277 {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
3278 %% Receive a DATA frame of that size and no other.
3279 {ok, << 5:24, 0:8, 0:8, 1:32, "Hello" >>} = gen_tcp:recv(Socket, 14, 1000),
3280 {error, timeout} = gen_tcp:recv(Socket, 9, 1000),
3281 %% Set SETTINGS_INITIAL_WINDOW_SIZE to exactly the size in the body.
3282 ok = gen_tcp:send(Socket, cow_http2:settings(#{initial_window_size => 12})),
3283 %% Receive the SETTINGS ack.
3284 {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
3285 %% Receive the rest of the response.
3286 {ok, << 7:24, 0:8, 1:8, 1:32, " world!" >>} = gen_tcp:recv(Socket, 16, 1000),
3287 ok.
3288
3289 settings_initial_window_size_changes_negative(Config) ->
3290 doc("When the value of SETTINGS_INITIAL_WINDOW_SIZE changes, the server "
3291 "must adjust the size of the flow control windows of the active "
3292 "streams even if their window end up negative. (RFC7540 6.9.2)"),
3293 {ok, Socket} = do_handshake(Config),
3294 %% Set SETTINGS_INITIAL_WINDOW_SIZE to 5.
3295 ok = gen_tcp:send(Socket, cow_http2:settings(#{initial_window_size => 5})),
3296 %% Receive the SETTINGS ack.
3297 {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
3298 %% Send a HEADERS frame.
3299 {HeadersBlock, _} = cow_hpack:encode([
3300 {<<":method">>, <<"GET">>},
3301 {<<":scheme">>, <<"http">>},
3302 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3303 {<<":path">>, <<"/">>}
3304 ]),
3305 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
3306 %% Receive a response with a single DATA frame of the initial size we set.
3307 {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
3308 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
3309 {ok, << 5:24, 0:8, 0:8, 1:32, "Hello" >>} = gen_tcp:recv(Socket, 14, 1000),
3310 {error, timeout} = gen_tcp:recv(Socket, 9, 1000),
3311 %% Set SETTINGS_INITIAL_WINDOW_SIZE to 0 to make the stream's window negative.
3312 ok = gen_tcp:send(Socket, cow_http2:settings(#{initial_window_size => 0})),
3313 %% Receive the SETTINGS ack.
3314 {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
3315 %% Set SETTINGS_INITIAL_WINDOW_SIZE to exactly the size in the body.
3316 ok = gen_tcp:send(Socket, cow_http2:settings(#{initial_window_size => 12})),
3317 %% Receive the SETTINGS ack.
3318 {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
3319 %% Receive the rest of the response.
3320 {ok, << 7:24, 0:8, 1:8, 1:32, " world!" >>} = gen_tcp:recv(Socket, 16, 1000),
3321 ok.
3322
3323 settings_initial_window_size_reject_overflow(Config) ->
3324 doc("A SETTINGS_INITIAL_WINDOW_SIZE that causes a flow control window "
3325 "to exceed 2^31-1 must be rejected with a FLOW_CONTROL_ERROR "
3326 "connection error. (RFC7540 6.5.2, RFC7540 6.9.2)"),
3327 {ok, Socket} = do_handshake(Config),
3328 %% Set SETTINGS_INITIAL_WINDOW_SIZE to 2^31.
3329 ok = gen_tcp:send(Socket, cow_http2:settings(#{initial_window_size => 16#80000000})),
3330 %% Receive a FLOW_CONTROL_ERROR connection error.
3331 {ok, << _:24, 7:8, _:72, 3:32 >>} = gen_tcp:recv(Socket, 17, 6000),
3332 ok.
3333
3334 %% (RFC7540 6.9.3)
3335 %% @todo The right way to do this seems to be to wait for the SETTINGS ack
3336 %% before we KNOW the flow control window was updated on the other side.
3337 % A receiver that wishes to use a smaller flow-control window than the
3338 % current size can send a new SETTINGS frame. However, the receiver
3339 % MUST be prepared to receive data that exceeds this window size, since
3340 % the sender might send data that exceeds the lower limit prior to
3341 % processing the SETTINGS frame.
3342
3343 %% (RFC7540 6.10) CONTINUATION
3344 % CONTINUATION frames MUST be associated with a stream. If a
3345 % CONTINUATION frame is received whose stream identifier field is 0x0,
3346 % the recipient MUST respond with a connection error (Section 5.4.1) of
3347 % type PROTOCOL_ERROR.
3348 %
3349 % A CONTINUATION frame MUST be preceded by a HEADERS, PUSH_PROMISE or
3350 % CONTINUATION frame without the END_HEADERS flag set. A recipient
3351 % that observes violation of this rule MUST respond with a connection
3352 % error (Section 5.4.1) of type PROTOCOL_ERROR.
3353
3354 %% (RFC7540 7) Error Codes
3355 % Unknown or unsupported error codes MUST NOT trigger any special
3356 % behavior. These MAY be treated by an implementation as being
3357 % equivalent to INTERNAL_ERROR.
3358
3359 accept_trailers(Config) ->
3360 doc("Trailing HEADERS frames must be accepted. (RFC7540 8.1)"),
3361 {ok, Socket} = do_handshake(Config),
3362 %% Send a request containing DATA and trailing HEADERS frames.
3363 {HeadersBlock, EncodeState} = cow_hpack:encode([
3364 {<<":method">>, <<"POST">>},
3365 {<<":scheme">>, <<"http">>},
3366 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3367 {<<":path">>, <<"/long_polling">>},
3368 {<<"trailer">>, <<"x-checksum">>}
3369 ]),
3370 {TrailersBlock, _} = cow_hpack:encode([
3371 {<<"x-checksum">>, <<"md5:4cc909a007407f3706399b6496babec3">>}
3372 ], EncodeState),
3373 ok = gen_tcp:send(Socket, [
3374 cow_http2:headers(1, nofin, HeadersBlock),
3375 cow_http2:data(1, nofin, <<0:10000/unit:8>>),
3376 cow_http2:headers(1, fin, TrailersBlock)
3377 ]),
3378 %% Receive a HEADERS frame as a response.
3379 {ok, << _:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
3380 ok.
3381
3382 accept_trailers_continuation(Config) ->
3383 doc("Trailing HEADERS and CONTINUATION frames must be accepted. (RFC7540 8.1)"),
3384 {ok, Socket} = do_handshake(Config),
3385 %% Send a request containing DATA and trailing HEADERS and CONTINUATION frames.
3386 {HeadersBlock, EncodeState} = cow_hpack:encode([
3387 {<<":method">>, <<"POST">>},
3388 {<<":scheme">>, <<"http">>},
3389 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3390 {<<":path">>, <<"/long_polling">>},
3391 {<<"trailer">>, <<"x-checksum">>}
3392 ]),
3393 {TrailersBlock, _} = cow_hpack:encode([
3394 {<<"x-checksum">>, <<"md5:4cc909a007407f3706399b6496babec3">>}
3395 ], EncodeState),
3396 Len = iolist_size(TrailersBlock),
3397 ok = gen_tcp:send(Socket, [
3398 cow_http2:headers(1, nofin, HeadersBlock),
3399 cow_http2:data(1, nofin, <<0:10000/unit:8>>),
3400 <<0:24, 1:8, 0:7, 1:1, 0:1, 1:31>>,
3401 <<Len:24, 9:8, 0:5, 1:1, 0:3, 1:31>>,
3402 TrailersBlock
3403 ]),
3404 %% Receive a HEADERS frame as a response.
3405 {ok, << _:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000),
3406 ok.
3407
3408 %% We reject all invalid HEADERS with a connection error because
3409 %% we do not want to waste resources decoding them.
3410 reject_trailers_nofin(Config) ->
3411 doc("Trailing HEADERS frames received without the END_STREAM flag "
3412 "set must be rejected with a PROTOCOL_ERROR connection error. "
3413 "(RFC7540 8.1, RFC7540 8.1.2.6)"),
3414 {ok, Socket} = do_handshake(Config),
3415 %% Send a request containing DATA and trailing HEADERS frames.
3416 %% The trailing HEADERS does not have the END_STREAM flag set.
3417 {HeadersBlock, EncodeState} = cow_hpack:encode([
3418 {<<":method">>, <<"POST">>},
3419 {<<":scheme">>, <<"http">>},
3420 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3421 {<<":path">>, <<"/long_polling">>},
3422 {<<"trailer">>, <<"x-checksum">>}
3423 ]),
3424 {TrailersBlock, _} = cow_hpack:encode([
3425 {<<"x-checksum">>, <<"md5:4cc909a007407f3706399b6496babec3">>}
3426 ], EncodeState),
3427 ok = gen_tcp:send(Socket, [
3428 cow_http2:headers(1, nofin, HeadersBlock),
3429 cow_http2:data(1, nofin, <<0:10000/unit:8>>),
3430 cow_http2:headers(1, nofin, TrailersBlock)
3431 ]),
3432 %% Receive a PROTOCOL_ERROR connection error.
3433 {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
3434 ok.
3435
3436 %% We reject all invalid HEADERS with a connection error because
3437 %% we do not want to waste resources decoding them.
3438 reject_trailers_nofin_continuation(Config) ->
3439 doc("Trailing HEADERS frames received without the END_STREAM flag "
3440 "set must be rejected with a PROTOCOL_ERROR connection error. "
3441 "(RFC7540 8.1, RFC7540 8.1.2.6)"),
3442 {ok, Socket} = do_handshake(Config),
3443 %% Send a request containing DATA and trailing HEADERS and CONTINUATION frames.
3444 %% The trailing HEADERS does not have the END_STREAM flag set.
3445 {HeadersBlock, EncodeState} = cow_hpack:encode([
3446 {<<":method">>, <<"POST">>},
3447 {<<":scheme">>, <<"http">>},
3448 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3449 {<<":path">>, <<"/long_polling">>},
3450 {<<"trailer">>, <<"x-checksum">>}
3451 ]),
3452 {TrailersBlock, _} = cow_hpack:encode([
3453 {<<"x-checksum">>, <<"md5:4cc909a007407f3706399b6496babec3">>}
3454 ], EncodeState),
3455 Len = iolist_size(TrailersBlock),
3456 ok = gen_tcp:send(Socket, [
3457 cow_http2:headers(1, nofin, HeadersBlock),
3458 cow_http2:data(1, nofin, <<0:10000/unit:8>>),
3459 <<0:24, 1:8, 0:9, 1:31>>,
3460 <<Len:24, 9:8, 0:5, 1:1, 0:3, 1:31>>,
3461 TrailersBlock
3462 ]),
3463 %% Receive a PROTOCOL_ERROR connection error.
3464 {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
3465 ok.
3466
3467 headers_informational_nofin(Config) ->
3468 doc("Informational HEADERS frames must not have the END_STREAM flag set. (RFC7540 8.1)"),
3469 {ok, Socket} = do_handshake(Config),
3470 %% Send a HEADERS frame on an idle stream.
3471 {HeadersBlock, _} = cow_hpack:encode([
3472 {<<":method">>, <<"POST">>},
3473 {<<":scheme">>, <<"http">>},
3474 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3475 {<<":path">>, <<"/echo/read_body">>},
3476 {<<"expect">>, <<"100-continue">>},
3477 {<<"content-length">>, <<"1000000">>}
3478 ]),
3479 ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, HeadersBlock)),
3480 %% Receive an informational HEADERS frame without the END_STREAM flag.
3481 {ok, << Len:24, 1:8, 0:5, 1:1, 0:2, _:32 >>} = gen_tcp:recv(Socket, 9, 6000),
3482 {ok, RespHeadersBlock} = gen_tcp:recv(Socket, Len, 6000),
3483 %% Confirm it has a 100 status code.
3484 {RespHeaders, _} = cow_hpack:decode(RespHeadersBlock),
3485 {_, <<"100">>} = lists:keyfind(<<":status">>, 1, RespHeaders),
3486 ok.
3487
3488 %% @todo This one is interesting to implement because Cowboy DOES this.
3489 % A server can
3490 % send a complete response prior to the client sending an entire
3491 % request if the response does not depend on any portion of the request
3492 % that has not been sent and received. When this is true, a server MAY
3493 % request that the client abort transmission of a request without error
3494 % by sending a RST_STREAM with an error code of NO_ERROR after sending
3495 % a complete response (i.e., a frame with the END_STREAM flag).
3496
3497 headers_reject_uppercase_header_name(Config) ->
3498 doc("Requests containing uppercase header names must be rejected "
3499 "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2, RFC7540 8.1.2.6)"),
3500 {ok, Socket} = do_handshake(Config),
3501 %% Send a HEADERS frame with a uppercase header name.
3502 {HeadersBlock, _} = cow_hpack:encode([
3503 {<<":method">>, <<"GET">>},
3504 {<<":scheme">>, <<"http">>},
3505 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3506 {<<":path">>, <<"/">>},
3507 {<<"HELLO">>, <<"world">>}
3508 ]),
3509 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
3510 %% Receive a PROTOCOL_ERROR stream error.
3511 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
3512 ok.
3513
3514 reject_response_pseudo_headers(Config) ->
3515 doc("Requests containing response pseudo-headers must be rejected "
3516 "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.1, RFC7540 8.1.2.6)"),
3517 {ok, Socket} = do_handshake(Config),
3518 %% Send a HEADERS frame with a response pseudo-header.
3519 {HeadersBlock, _} = cow_hpack:encode([
3520 {<<":method">>, <<"GET">>},
3521 {<<":scheme">>, <<"http">>},
3522 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3523 {<<":path">>, <<"/">>},
3524 {<<":status">>, <<"200">>}
3525 ]),
3526 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
3527 %% Receive a PROTOCOL_ERROR stream error.
3528 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
3529 ok.
3530
3531 reject_unknown_pseudo_headers(Config) ->
3532 doc("Requests containing unknown pseudo-headers must be rejected "
3533 "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.1, RFC7540 8.1.2.6)"),
3534 {ok, Socket} = do_handshake(Config),
3535 %% Send a HEADERS frame with an unknown pseudo-header.
3536 {HeadersBlock, _} = cow_hpack:encode([
3537 {<<":method">>, <<"GET">>},
3538 {<<":scheme">>, <<"http">>},
3539 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3540 {<<":path">>, <<"/">>},
3541 {<<":upgrade">>, <<"websocket">>}
3542 ]),
3543 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
3544 %% Receive a PROTOCOL_ERROR stream error.
3545 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
3546 ok.
3547
3548 reject_pseudo_headers_in_trailers(Config) ->
3549 doc("Requests containing pseudo-headers in trailers must be rejected "
3550 "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.1, RFC7540 8.1.2.6)"),
3551 {ok, Socket} = do_handshake(Config),
3552 %% Send a request containing DATA and trailing HEADERS frames.
3553 %% The trailing HEADERS contains pseudo-headers.
3554 {HeadersBlock, EncodeState} = cow_hpack:encode([
3555 {<<":method">>, <<"POST">>},
3556 {<<":scheme">>, <<"http">>},
3557 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3558 {<<":path">>, <<"/long_polling">>},
3559 {<<"trailer">>, <<"x-checksum">>}
3560 ]),
3561 {TrailersBlock, _} = cow_hpack:encode([
3562 {<<"x-checksum">>, <<"md5:4cc909a007407f3706399b6496babec3">>},
3563 {<<":path">>, <<"/">>}
3564 ], EncodeState),
3565 ok = gen_tcp:send(Socket, [
3566 cow_http2:headers(1, nofin, HeadersBlock),
3567 cow_http2:data(1, nofin, <<0:10000/unit:8>>),
3568 cow_http2:headers(1, fin, TrailersBlock)
3569 ]),
3570 %% Receive a PROTOCOL_ERROR stream error.
3571 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
3572 ok.
3573
3574 reject_pseudo_headers_after_regular_headers(Config) ->
3575 doc("Requests containing pseudo-headers after regular headers must be rejected "
3576 "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.1, RFC7540 8.1.2.6)"),
3577 {ok, Socket} = do_handshake(Config),
3578 %% Send a HEADERS frame with a pseudo-header after regular headers.
3579 {HeadersBlock, _} = cow_hpack:encode([
3580 {<<":method">>, <<"GET">>},
3581 {<<":scheme">>, <<"http">>},
3582 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3583 {<<"content-length">>, <<"0">>},
3584 {<<":path">>, <<"/">>}
3585 ]),
3586 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
3587 %% Receive a PROTOCOL_ERROR stream error.
3588 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
3589 ok.
3590
3591 reject_connection_header(Config) ->
3592 doc("Requests containing a connection header must be rejected "
3593 "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.2, RFC7540 8.1.2.6)"),
3594 {ok, Socket} = do_handshake(Config),
3595 %% Send a HEADERS frame with a connection header.
3596 {HeadersBlock, _} = cow_hpack:encode([
3597 {<<":method">>, <<"GET">>},
3598 {<<":scheme">>, <<"http">>},
3599 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3600 {<<":path">>, <<"/">>},
3601 {<<"connection">>, <<"close">>}
3602 ]),
3603 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
3604 %% Receive a PROTOCOL_ERROR stream error.
3605 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
3606 ok.
3607
3608 reject_keep_alive_header(Config) ->
3609 doc("Requests containing a keep-alive header must be rejected "
3610 "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.2, RFC7540 8.1.2.6)"),
3611 {ok, Socket} = do_handshake(Config),
3612 %% Send a HEADERS frame with a keep-alive header.
3613 {HeadersBlock, _} = cow_hpack:encode([
3614 {<<":method">>, <<"GET">>},
3615 {<<":scheme">>, <<"http">>},
3616 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3617 {<<":path">>, <<"/">>},
3618 {<<"keep-alive">>, <<"timeout=5, max=1000">>}
3619 ]),
3620 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
3621 %% Receive a PROTOCOL_ERROR stream error.
3622 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
3623 ok.
3624
3625 reject_proxy_authenticate_header(Config) ->
3626 doc("Requests containing a connection header must be rejected "
3627 "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.2, RFC7540 8.1.2.6)"),
3628 {ok, Socket} = do_handshake(Config),
3629 %% Send a HEADERS frame with a proxy-authenticate header.
3630 {HeadersBlock, _} = cow_hpack:encode([
3631 {<<":method">>, <<"GET">>},
3632 {<<":scheme">>, <<"http">>},
3633 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3634 {<<":path">>, <<"/">>},
3635 {<<"proxy-authenticate">>, <<"Basic">>}
3636 ]),
3637 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
3638 %% Receive a PROTOCOL_ERROR stream error.
3639 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
3640 ok.
3641
3642 reject_proxy_authorization_header(Config) ->
3643 doc("Requests containing a connection header must be rejected "
3644 "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.2, RFC7540 8.1.2.6)"),
3645 {ok, Socket} = do_handshake(Config),
3646 %% Send a HEADERS frame with a proxy-authorization header.
3647 {HeadersBlock, _} = cow_hpack:encode([
3648 {<<":method">>, <<"GET">>},
3649 {<<":scheme">>, <<"http">>},
3650 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3651 {<<":path">>, <<"/">>},
3652 {<<"proxy-authorization">>, <<"Basic YWxhZGRpbjpvcGVuc2VzYW1l">>}
3653 ]),
3654 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
3655 %% Receive a PROTOCOL_ERROR stream error.
3656 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
3657 ok.
3658
3659 reject_transfer_encoding_header(Config) ->
3660 doc("Requests containing a connection header must be rejected "
3661 "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.2, RFC7540 8.1.2.6)"),
3662 {ok, Socket} = do_handshake(Config),
3663 %% Send a HEADERS frame with a transfer-encoding header.
3664 {HeadersBlock, _} = cow_hpack:encode([
3665 {<<":method">>, <<"GET">>},
3666 {<<":scheme">>, <<"http">>},
3667 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3668 {<<":path">>, <<"/">>},
3669 {<<"transfer-encoding">>, <<"chunked">>}
3670 ]),
3671 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
3672 %% Receive a PROTOCOL_ERROR stream error.
3673 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
3674 ok.
3675
3676 reject_upgrade_header(Config) ->
3677 doc("Requests containing a connection header must be rejected "
3678 "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.2, RFC7540 8.1.2.6)"),
3679 {ok, Socket} = do_handshake(Config),
3680 %% Send a HEADERS frame with a upgrade header.
3681 {HeadersBlock, _} = cow_hpack:encode([
3682 {<<":method">>, <<"GET">>},
3683 {<<":scheme">>, <<"http">>},
3684 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3685 {<<":path">>, <<"/">>},
3686 {<<"upgrade">>, <<"websocket">>}
3687 ]),
3688 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
3689 %% Receive a PROTOCOL_ERROR stream error.
3690 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
3691 ok.
3692
3693 accept_te_header_value_trailers(Config) ->
3694 doc("Requests containing a TE header with a value of \"trailers\" "
3695 "must be accepted. (RFC7540 8.1.2.2)"),
3696 {ok, Socket} = do_handshake(Config),
3697 %% Send a HEADERS frame with a TE header with value "trailers".
3698 {HeadersBlock, _} = cow_hpack:encode([
3699 {<<":method">>, <<"GET">>},
3700 {<<":scheme">>, <<"http">>},
3701 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3702 {<<":path">>, <<"/">>},
3703 {<<"te">>, <<"trailers">>}
3704 ]),
3705 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
3706 %% Receive a response.
3707 {ok, << _:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
3708 ok.
3709
3710 reject_te_header_other_values(Config) ->
3711 doc("Requests containing a TE header with a value other than \"trailers\" must be rejected "
3712 "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.2, RFC7540 8.1.2.6)"),
3713 {ok, Socket} = do_handshake(Config),
3714 %% Send a HEADERS frame with a TE header with a different value.
3715 {HeadersBlock, _} = cow_hpack:encode([
3716 {<<":method">>, <<"GET">>},
3717 {<<":scheme">>, <<"http">>},
3718 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3719 {<<":path">>, <<"/">>},
3720 {<<"te">>, <<"trailers, deflate;q=0.5">>}
3721 ]),
3722 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
3723 %% Receive a PROTOCOL_ERROR stream error.
3724 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
3725 ok.
3726
3727 %% (RFC7540 8.1.2.2)
3728 % This means that an intermediary transforming an HTTP/1.x message to
3729 % HTTP/2 will need to remove any header fields nominated by the
3730 % Connection header field, along with the Connection header field
3731 % itself. Such intermediaries SHOULD also remove other connection-
3732 % specific header fields, such as Keep-Alive, Proxy-Connection,
3733 % Transfer-Encoding, and Upgrade, even if they are not nominated by the
3734 % Connection header field.
3735
3736 response_dont_send_header_in_connection(Config) ->
3737 doc("Intermediaries must remove HTTP/1.1 connection headers when "
3738 "transforming an HTTP/1.1 messages to HTTP/2. The server must "
3739 "not send them either. All headers listed in the connection "
3740 "header must be removed. (RFC7540 8.1.2.2)"),
3741 do_response_dont_send_http11_header(Config, <<"custom-header">>).
3742
3743 response_dont_send_connection_header(Config) ->
3744 doc("Intermediaries must remove HTTP/1.1 connection headers when "
3745 "transforming an HTTP/1.1 messages to HTTP/2. The server must "
3746 "not send them either. The connection header must be removed. (RFC7540 8.1.2.2)"),
3747 do_response_dont_send_http11_header(Config, <<"connection">>).
3748
3749 response_dont_send_keep_alive_header(Config) ->
3750 doc("Intermediaries must remove HTTP/1.1 connection headers when "
3751 "transforming an HTTP/1.1 messages to HTTP/2. The server must "
3752 "not send them either. The keep-alive header must be removed "
3753 "even if not listed in the connection header. (RFC7540 8.1.2.2)"),
3754 do_response_dont_send_http11_header(Config, <<"keep-alive">>).
3755
3756 response_dont_send_proxy_connection_header(Config) ->
3757 doc("Intermediaries must remove HTTP/1.1 connection headers when "
3758 "transforming an HTTP/1.1 messages to HTTP/2. The server must "
3759 "not send them either. The proxy-connection header must be removed "
3760 "even if not listed in the connection header. (RFC7540 8.1.2.2)"),
3761 do_response_dont_send_http11_header(Config, <<"proxy-connection">>).
3762
3763 response_dont_send_transfer_encoding_header(Config) ->
3764 doc("Intermediaries must remove HTTP/1.1 connection headers when "
3765 "transforming an HTTP/1.1 messages to HTTP/2. The server must "
3766 "not send them either. The transfer-encoding header must be removed "
3767 "even if not listed in the connection header. (RFC7540 8.1.2.2)"),
3768 do_response_dont_send_http11_header(Config, <<"transfer-encoding">>).
3769
3770 response_dont_send_upgrade_header(Config) ->
3771 doc("Intermediaries must remove HTTP/1.1 connection headers when "
3772 "transforming an HTTP/1.1 messages to HTTP/2. The server must "
3773 "not send them either. The upgrade header must be removed "
3774 "even if not listed in the connection header. (RFC7540 8.1.2.2)"),
3775 do_response_dont_send_http11_header(Config, <<"upgrade">>).
3776
3777 do_response_dont_send_http11_header(Config, Name) ->
3778 ConnPid = gun_open(Config),
3779 Ref = gun:get(ConnPid, "/resp/set_resp_headers_http11"),
3780 {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
3781 false = lists:keyfind(Name, 1, Headers),
3782 ok.
3783
3784 reject_userinfo(Config) ->
3785 doc("An authority containing a userinfo component must be rejected "
3786 "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.3, RFC7540 8.1.2.6)"),
3787 {ok, Socket} = do_handshake(Config),
3788 %% Send a HEADERS frame with a userinfo authority component.
3789 {HeadersBlock, _} = cow_hpack:encode([
3790 {<<":method">>, <<"GET">>},
3791 {<<":scheme">>, <<"http">>},
3792 {<<":authority">>, <<"user@localhost">>}, %% @todo Correct port number.
3793 {<<":path">>, <<"/">>}
3794 ]),
3795 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
3796 %% Receive a PROTOCOL_ERROR stream error.
3797 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
3798 ok.
3799
3800 %% (RFC7540 8.1.2.3)
3801 % To ensure that the HTTP/1.1 request line can be reproduced
3802 % accurately, this pseudo-header field MUST be omitted when
3803 % translating from an HTTP/1.1 request that has a request target in
3804 % origin or asterisk form (see [RFC7230], Section 5.3). Clients
3805 % that generate HTTP/2 requests directly SHOULD use the ":authority"
3806 % pseudo-header field instead of the Host header field. An
3807 % intermediary that converts an HTTP/2 request to HTTP/1.1 MUST
3808 % create a Host header field if one is not present in a request by
3809 % copying the value of the ":authority" pseudo-header field.
3810
3811 reject_empty_path(Config) ->
3812 doc("A request containing an empty path component must be rejected "
3813 "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.3, RFC7540 8.1.2.6)"),
3814 {ok, Socket} = do_handshake(Config),
3815 %% Send a HEADERS frame with an empty path component.
3816 {HeadersBlock, _} = cow_hpack:encode([
3817 {<<":method">>, <<"GET">>},
3818 {<<":scheme">>, <<"http">>},
3819 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3820 {<<":path">>, <<>>}
3821 ]),
3822 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
3823 %% Receive a PROTOCOL_ERROR stream error.
3824 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
3825 ok.
3826
3827 reject_invalid_pseudo_header_method_crlf(Config) ->
3828 doc("A request with an invalid method component must be rejected "
3829 "with a PROTOCOL_ERROR stream error. (RFC9110 5.5)"),
3830 {ok, Socket} = do_handshake(Config),
3831 %% Send a HEADERS frame with an invalid :method pseudo-header.
3832 {HeadersBlock, _} = cow_hpack:encode([
3833 {<<":method">>, <<"GET\r\n">>},
3834 {<<":scheme">>, <<"http">>},
3835 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3836 {<<":path">>, <<"/">>}
3837 ]),
3838 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
3839 %% Receive a PROTOCOL_ERROR stream error.
3840 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
3841 ok.
3842
3843 reject_invalid_pseudo_header_method_nul(Config) ->
3844 doc("A request with an invalid method component must be rejected "
3845 "with a PROTOCOL_ERROR stream error. (RFC9110 5.5)"),
3846 {ok, Socket} = do_handshake(Config),
3847 %% Send a HEADERS frame with an invalid :method pseudo-header.
3848 {HeadersBlock, _} = cow_hpack:encode([
3849 {<<":method">>, <<"GET\0">>},
3850 {<<":scheme">>, <<"http">>},
3851 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3852 {<<":path">>, <<"/">>}
3853 ]),
3854 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
3855 %% Receive a PROTOCOL_ERROR stream error.
3856 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
3857 ok.
3858
3859 reject_missing_pseudo_header_method(Config) ->
3860 doc("A request without a method component must be rejected "
3861 "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.3, RFC7540 8.1.2.6)"),
3862 {ok, Socket} = do_handshake(Config),
3863 %% Send a HEADERS frame without a :method pseudo-header.
3864 {HeadersBlock, _} = cow_hpack:encode([
3865 {<<":scheme">>, <<"http">>},
3866 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3867 {<<":path">>, <<"/">>}
3868 ]),
3869 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
3870 %% Receive a PROTOCOL_ERROR stream error.
3871 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
3872 ok.
3873
3874 reject_many_pseudo_header_method(Config) ->
3875 doc("A request containing more than one method component must be rejected "
3876 "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.3, RFC7540 8.1.2.6)"),
3877 {ok, Socket} = do_handshake(Config),
3878 %% Send a HEADERS frame with more than one :method pseudo-header.
3879 {HeadersBlock, _} = cow_hpack:encode([
3880 {<<":method">>, <<"GET">>},
3881 {<<":method">>, <<"GET">>},
3882 {<<":scheme">>, <<"http">>},
3883 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3884 {<<":path">>, <<"/">>}
3885 ]),
3886 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
3887 %% Receive a PROTOCOL_ERROR stream error.
3888 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
3889 ok.
3890
3891 reject_invalid_pseudo_header_scheme_crlf(Config) ->
3892 doc("A request with an invalid scheme component must be rejected "
3893 "with a PROTOCOL_ERROR stream error. (RFC9110 5.5)"),
3894 {ok, Socket} = do_handshake(Config),
3895 %% Send a HEADERS frame with an invalid :method pseudo-header.
3896 {HeadersBlock, _} = cow_hpack:encode([
3897 {<<":method">>, <<"GET\r\n">>},
3898 {<<":scheme">>, <<"http">>},
3899 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3900 {<<":path">>, <<"/">>}
3901 ]),
3902 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
3903 %% Receive a PROTOCOL_ERROR stream error.
3904 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
3905 ok.
3906
3907 reject_invalid_pseudo_header_scheme_nul(Config) ->
3908 doc("A request with an invalid scheme component must be rejected "
3909 "with a PROTOCOL_ERROR stream error. (RFC9110 5.5)"),
3910 {ok, Socket} = do_handshake(Config),
3911 %% Send a HEADERS frame with an invalid :method pseudo-header.
3912 {HeadersBlock, _} = cow_hpack:encode([
3913 {<<":method">>, <<"GET\0">>},
3914 {<<":scheme">>, <<"http">>},
3915 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3916 {<<":path">>, <<"/">>}
3917 ]),
3918 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
3919 %% Receive a PROTOCOL_ERROR stream error.
3920 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
3921 ok.
3922
3923 reject_missing_pseudo_header_scheme(Config) ->
3924 doc("A request without a scheme component must be rejected "
3925 "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.3, RFC7540 8.1.2.6)"),
3926 {ok, Socket} = do_handshake(Config),
3927 %% Send a HEADERS frame without a :scheme pseudo-header.
3928 {HeadersBlock, _} = cow_hpack:encode([
3929 {<<":method">>, <<"GET">>},
3930 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3931 {<<":path">>, <<"/">>}
3932 ]),
3933 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
3934 %% Receive a PROTOCOL_ERROR stream error.
3935 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
3936 ok.
3937
3938 reject_many_pseudo_header_scheme(Config) ->
3939 doc("A request containing more than one scheme component must be rejected "
3940 "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.3, RFC7540 8.1.2.6)"),
3941 {ok, Socket} = do_handshake(Config),
3942 %% Send a HEADERS frame with more than one :scheme pseudo-header.
3943 {HeadersBlock, _} = cow_hpack:encode([
3944 {<<":method">>, <<"GET">>},
3945 {<<":scheme">>, <<"http">>},
3946 {<<":scheme">>, <<"http">>},
3947 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
3948 {<<":path">>, <<"/">>}
3949 ]),
3950 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
3951 %% Receive a PROTOCOL_ERROR stream error.
3952 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
3953 ok.
3954
3955 reject_invalid_pseudo_header_authority_crlf(Config) ->
3956 doc("A request with an invalid authority component must be rejected "
3957 "with a PROTOCOL_ERROR stream error. (RFC9110 5.5)"),
3958 {ok, Socket} = do_handshake(Config),
3959 %% Send a HEADERS frame with an invalid :authority pseudo-header.
3960 {HeadersBlock, _} = cow_hpack:encode([
3961 {<<":method">>, <<"GET">>},
3962 {<<":scheme">>, <<"http">>},
3963 {<<":authority">>, <<"localhost\r\n">>}, %% @todo Correct port number.
3964 {<<":path">>, <<"/">>}
3965 ]),
3966 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
3967 %% Receive a PROTOCOL_ERROR stream error.
3968 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
3969 ok.
3970
3971 reject_invalid_pseudo_header_authority_nul(Config) ->
3972 doc("A request with an invalid authority component must be rejected "
3973 "with a PROTOCOL_ERROR stream error. (RFC9110 5.5)"),
3974 {ok, Socket} = do_handshake(Config),
3975 %% Send a HEADERS frame with an invalid :authority pseudo-header.
3976 {HeadersBlock, _} = cow_hpack:encode([
3977 {<<":method">>, <<"GET">>},
3978 {<<":scheme">>, <<"http">>},
3979 {<<":authority">>, <<"localhost\0">>}, %% @todo Correct port number.
3980 {<<":path">>, <<"/">>}
3981 ]),
3982 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
3983 %% Receive a PROTOCOL_ERROR stream error.
3984 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
3985 ok.
3986
3987 reject_missing_pseudo_header_authority(Config) ->
3988 doc("A request without an authority or host component must be rejected "
3989 "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.3, RFC7540 8.1.2.6)"),
3990 {ok, Socket} = do_handshake(Config),
3991 %% Send a HEADERS frame without an :authority pseudo-header.
3992 {HeadersBlock, _} = cow_hpack:encode([
3993 {<<":method">>, <<"GET">>},
3994 {<<":scheme">>, <<"http">>},
3995 {<<":path">>, <<"/">>}
3996 ]),
3997 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
3998 %% Receive a PROTOCOL_ERROR stream error.
3999 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
4000 ok.
4001
4002 accept_host_header_on_missing_pseudo_header_authority(Config) ->
4003 doc("A request without an authority but with a host header must be accepted. "
4004 "(RFC7540 8.1.2.3, RFC7540 8.1.3)"),
4005 {ok, Socket} = do_handshake(Config),
4006 %% Send a HEADERS frame with host header and without an :authority pseudo-header.
4007 {HeadersBlock, _} = cow_hpack:encode([
4008 {<<":method">>, <<"GET">>},
4009 {<<":scheme">>, <<"http">>},
4010 {<<":path">>, <<"/">>},
4011 {<<"host">>, <<"localhost">>}
4012 ]),
4013 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
4014 %% Receive a 200 response.
4015 {ok, << Len:24, 1:8, _:8, _:32 >>} = gen_tcp:recv(Socket, 9, 6000),
4016 {ok, RespHeadersBlock} = gen_tcp:recv(Socket, Len, 6000),
4017 {RespHeaders, _} = cow_hpack:decode(RespHeadersBlock),
4018 {_, <<"200">>} = lists:keyfind(<<":status">>, 1, RespHeaders),
4019 ok.
4020
4021 accept_host_header_same_as_pseudo_header_authority(Config) ->
4022 doc("A request with both an authority and host component "
4023 "that are equal must be accepted. (RFC9113 8.3.1)"),
4024 {ok, Socket} = do_handshake(Config),
4025 %% Send a HEADERS frame with same :authority pseudo-header and host header.
4026 {HeadersBlock, _} = cow_hpack:encode([
4027 {<<":method">>, <<"GET">>},
4028 {<<":scheme">>, <<"http">>},
4029 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
4030 {<<":path">>, <<"/">>},
4031 {<<"host">>, <<"localhost">>} %% @todo Correct port number.
4032 ]),
4033 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
4034 %% Receive a 200 response.
4035 {ok, << Len:24, 1:8, _:8, _:32 >>} = gen_tcp:recv(Socket, 9, 6000),
4036 {ok, RespHeadersBlock} = gen_tcp:recv(Socket, Len, 6000),
4037 {RespHeaders, _} = cow_hpack:decode(RespHeadersBlock),
4038 {_, <<"200">>} = lists:keyfind(<<":status">>, 1, RespHeaders),
4039 ok.
4040
4041 reject_host_header_different_than_pseudo_header_authority(Config) ->
4042 doc("A request with both an authority and host component "
4043 "that are not equal must be rejected with a PROTOCOL_ERROR "
4044 "stream error. (RFC9113 8.3.1)"),
4045 {ok, Socket} = do_handshake(Config),
4046 %% Send a HEADERS frame with different :authority pseudo-header and host header.
4047 {HeadersBlock, _} = cow_hpack:encode([
4048 {<<":method">>, <<"GET">>},
4049 {<<":scheme">>, <<"http">>},
4050 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
4051 {<<":path">>, <<"/">>},
4052 {<<"host">>, <<"localhost.localdomain">>} %% @todo Correct port number.
4053 ]),
4054 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
4055 %% Receive a PROTOCOL_ERROR stream error.
4056 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
4057 ok.
4058
4059 reject_many_pseudo_header_authority(Config) ->
4060 doc("A request containing more than one authority component must be rejected "
4061 "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.3, RFC7540 8.1.2.6)"),
4062 {ok, Socket} = do_handshake(Config),
4063 %% Send a HEADERS frame with more than one :authority pseudo-header.
4064 {HeadersBlock, _} = cow_hpack:encode([
4065 {<<":method">>, <<"GET">>},
4066 {<<":scheme">>, <<"http">>},
4067 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
4068 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
4069 {<<":path">>, <<"/">>}
4070 ]),
4071 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
4072 %% Receive a PROTOCOL_ERROR stream error.
4073 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
4074 ok.
4075
4076 reject_missing_pseudo_header_path(Config) ->
4077 doc("A request without a path component must be rejected "
4078 "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.3, RFC7540 8.1.2.6)"),
4079 {ok, Socket} = do_handshake(Config),
4080 %% Send a HEADERS frame without a :path pseudo-header.
4081 {HeadersBlock, _} = cow_hpack:encode([
4082 {<<":method">>, <<"GET">>},
4083 {<<":scheme">>, <<"http">>},
4084 {<<":authority">>, <<"localhost">>} %% @todo Correct port number.
4085 ]),
4086 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
4087 %% Receive a PROTOCOL_ERROR stream error.
4088 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
4089 ok.
4090
4091 reject_invalid_pseudo_header_path_crlf(Config) ->
4092 doc("A request with an invalid path component must be rejected "
4093 "with a PROTOCOL_ERROR stream error. (RFC9110 5.5)"),
4094 {ok, Socket} = do_handshake(Config),
4095 %% Send a HEADERS frame with an invalid :path pseudo-header.
4096 {HeadersBlock, _} = cow_hpack:encode([
4097 {<<":method">>, <<"GET">>},
4098 {<<":scheme">>, <<"http">>},
4099 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
4100 {<<":path">>, <<"/\r\n">>}
4101 ]),
4102 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
4103 %% Receive a PROTOCOL_ERROR stream error.
4104 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
4105 ok.
4106
4107 reject_invalid_pseudo_header_path_nul(Config) ->
4108 doc("A request with an invalid path component must be rejected "
4109 "with a PROTOCOL_ERROR stream error. (RFC9110 5.5)"),
4110 {ok, Socket} = do_handshake(Config),
4111 %% Send a HEADERS frame with an invalid :path pseudo-header.
4112 {HeadersBlock, _} = cow_hpack:encode([
4113 {<<":method">>, <<"GET">>},
4114 {<<":scheme">>, <<"http">>},
4115 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
4116 {<<":path">>, <<"/\0">>}
4117 ]),
4118 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
4119 %% Receive a PROTOCOL_ERROR stream error.
4120 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
4121 ok.
4122
4123 reject_invalid_pseudo_header_path_fragment(Config) ->
4124 doc("A request with an invalid path component must be rejected "
4125 "with a PROTOCOL_ERROR stream error. (RFC9110 5.5)"),
4126 {ok, Socket} = do_handshake(Config),
4127 %% Send a HEADERS frame with an invalid :path pseudo-header.
4128 {HeadersBlock, _} = cow_hpack:encode([
4129 {<<":method">>, <<"GET">>},
4130 {<<":scheme">>, <<"http">>},
4131 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
4132 {<<":path">>, <<"/#fragment">>}
4133 ]),
4134 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
4135 %% Receive a PROTOCOL_ERROR stream error.
4136 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
4137 ok.
4138
4139 reject_many_pseudo_header_path(Config) ->
4140 doc("A request containing more than one path component must be rejected "
4141 "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.3, RFC7540 8.1.2.6)"),
4142 {ok, Socket} = do_handshake(Config),
4143 %% Send a HEADERS frame with more than one :path pseudo-header.
4144 {HeadersBlock, _} = cow_hpack:encode([
4145 {<<":method">>, <<"GET">>},
4146 {<<":scheme">>, <<"http">>},
4147 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
4148 {<<":path">>, <<"/">>},
4149 {<<":path">>, <<"/">>}
4150 ]),
4151 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
4152 %% Receive a PROTOCOL_ERROR stream error.
4153 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
4154 ok.
4155
4156 %% (RFC7540 8.1.2.4)
4157 % For HTTP/2 responses, a single ":status" pseudo-header field is
4158 % defined that carries the HTTP status code field (see [RFC7231],
4159 % Section 6). This pseudo-header field MUST be included in all
4160 % responses; otherwise, the response is malformed (Section 8.1.2.6).
4161
4162 %% (RFC7540 8.1.2.5)
4163 % To allow for better compression efficiency, the Cookie header field
4164 % MAY be split into separate header fields, each with one or more
4165 % cookie-pairs. If there are multiple Cookie header fields after
4166 % decompression, these MUST be concatenated into a single octet string
4167 % using the two-octet delimiter of 0x3B, 0x20 (the ASCII string "; ")
4168 % before being passed into a non-HTTP/2 context, such as an HTTP/1.1
4169 % connection, or a generic HTTP server application.
4170
4171 reject_data_size_smaller_than_content_length(Config) ->
4172 doc("Requests that have a content-length header whose value does not "
4173 "match the total length of the DATA frames must be rejected with "
4174 "a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.6)"),
4175 {ok, Socket} = do_handshake(Config),
4176 %% Send a HEADERS frame with a content-length header different
4177 %% than the sum of the DATA frame sizes.
4178 {HeadersBlock, _} = cow_hpack:encode([
4179 {<<":method">>, <<"POST">>},
4180 {<<":scheme">>, <<"http">>},
4181 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
4182 {<<":path">>, <<"/long_polling">>},
4183 {<<"content-length">>, <<"12">>}
4184 ]),
4185 ok = gen_tcp:send(Socket, [
4186 cow_http2:headers(1, nofin, HeadersBlock),
4187 cow_http2:data(1, fin, <<"Hello!">>)
4188 ]),
4189 %% Receive a PROTOCOL_ERROR stream error.
4190 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
4191 ok.
4192
4193 reject_data_size_larger_than_content_length(Config) ->
4194 doc("Requests that have a content-length header whose value does not "
4195 "match the total length of the DATA frames must be rejected with "
4196 "a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.6)"),
4197 {ok, Socket} = do_handshake(Config),
4198 %% Send a HEADERS frame with a content-length header different
4199 %% than the sum of the DATA frame sizes.
4200 {HeadersBlock, _} = cow_hpack:encode([
4201 {<<":method">>, <<"POST">>},
4202 {<<":scheme">>, <<"http">>},
4203 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
4204 {<<":path">>, <<"/long_polling">>},
4205 {<<"content-length">>, <<"12">>}
4206 ]),
4207 ok = gen_tcp:send(Socket, [
4208 cow_http2:headers(1, nofin, HeadersBlock),
4209 cow_http2:data(1, nofin, <<"Hello! World! Universe!">>),
4210 cow_http2:data(1, fin, <<"Multiverse!">>)
4211 ]),
4212 %% Receive a PROTOCOL_ERROR stream error.
4213 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
4214 ok.
4215
4216 reject_content_length_without_data(Config) ->
4217 doc("Requests that have a content-length header whose value does not "
4218 "match the total length of the DATA frames must be rejected with "
4219 "a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.6)"),
4220 {ok, Socket} = do_handshake(Config),
4221 %% Send a HEADERS frame with a content-length header different
4222 %% than the sum of the DATA frame sizes.
4223 {HeadersBlock, _} = cow_hpack:encode([
4224 {<<":method">>, <<"POST">>},
4225 {<<":scheme">>, <<"http">>},
4226 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
4227 {<<":path">>, <<"/long_polling">>},
4228 {<<"content-length">>, <<"12">>}
4229 ]),
4230 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
4231 %% Receive a PROTOCOL_ERROR stream error.
4232 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
4233 ok.
4234
4235 reject_data_size_different_than_content_length_with_trailers(Config) ->
4236 doc("Requests that have a content-length header whose value does not "
4237 "match the total length of the DATA frames must be rejected with "
4238 "a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.6)"),
4239 {ok, Socket} = do_handshake(Config),
4240 %% Send a HEADERS frame with a content-length header different
4241 %% than the sum of the DATA frame sizes.
4242 {HeadersBlock, EncodeState} = cow_hpack:encode([
4243 {<<":method">>, <<"POST">>},
4244 {<<":scheme">>, <<"http">>},
4245 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
4246 {<<":path">>, <<"/long_polling">>},
4247 {<<"content-length">>, <<"12">>},
4248 {<<"trailer">>, <<"x-checksum">>}
4249 ]),
4250 {TrailersBlock, _} = cow_hpack:encode([
4251 {<<"x-checksum">>, <<"md5:4cc909a007407f3706399b6496babec3">>}
4252 ], EncodeState),
4253 ok = gen_tcp:send(Socket, [
4254 cow_http2:headers(1, nofin, HeadersBlock),
4255 cow_http2:data(1, nofin, <<"Hello!">>),
4256 cow_http2:headers(1, fin, TrailersBlock)
4257 ]),
4258 %% Receive a PROTOCOL_ERROR stream error.
4259 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
4260 ok.
4261
4262 reject_duplicate_content_length_header(Config) ->
4263 doc("A request with duplicate content-length headers must be rejected "
4264 "with a PROTOCOL_ERROR stream error. (RFC7230 3.3.2, RFC7540 8.1.2.6)"),
4265 {ok, Socket} = do_handshake(Config),
4266 %% Send a HEADERS frame with more than one content-length header.
4267 {HeadersBlock, _} = cow_hpack:encode([
4268 {<<":method">>, <<"GET">>},
4269 {<<":scheme">>, <<"http">>},
4270 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
4271 {<<":path">>, <<"/">>},
4272 {<<"content-length">>, <<"12">>},
4273 {<<"content-length">>, <<"12">>}
4274 ]),
4275 ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, HeadersBlock)),
4276 %% Receive a PROTOCOL_ERROR stream error.
4277 {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
4278 ok.
4279
4280 % Intermediaries that process HTTP requests or responses (i.e., any
4281 % intermediary not acting as a tunnel) MUST NOT forward a malformed
4282 % request or response. Malformed requests or responses that are
4283 % detected MUST be treated as a stream error (Section 5.4.2) of type
4284 % PROTOCOL_ERROR.
4285 %
4286 % For malformed requests, a server MAY send an HTTP response prior to
4287 % closing or resetting the stream. Clients MUST NOT accept a malformed
4288 % response. Note that these requirements are intended to protect
4289 % against several types of common attacks against HTTP; they are
4290 % deliberately strict because being permissive can expose
4291 % implementations to these vulnerabilities.
4292
4293 %% @todo It migh be worth reproducing the good examples. (RFC7540 8.1.3)
4294
4295 %% (RFC7540 8.1.4)
4296 % A server MUST NOT indicate that a stream has not been processed
4297 % unless it can guarantee that fact. If frames that are on a stream
4298 % are passed to the application layer for any stream, then
4299 % REFUSED_STREAM MUST NOT be used for that stream, and a GOAWAY frame
4300 % MUST include a stream identifier that is greater than or equal to the
4301 % given stream identifier.
4302
4303 %% (RFC7540 8.2)
4304 % Promised requests MUST be cacheable (see [RFC7231], Section 4.2.3),
4305 % MUST be safe (see [RFC7231], Section 4.2.1), and MUST NOT include a
4306 % request body.
4307 %
4308 % The server MUST include a value in the ":authority" pseudo-header
4309 % field for which the server is authoritative (see Section 10.1).
4310 %
4311 % A client cannot push. Thus, servers MUST treat the receipt of a
4312 % PUSH_PROMISE frame as a connection error (Section 5.4.1) of type
4313 % PROTOCOL_ERROR.
4314
4315 push_has_no_request_body(Config) ->
4316 doc("PUSH_PROMISE frames include the complete set of request headers "
4317 "and the request can never include a body. (RFC7540 8.2.1)"),
4318 ConnPid = gun_open(Config),
4319 Ref = gun:get(ConnPid, "/resp/push/read_body"),
4320 {push, PushRef, <<"GET">>, _, _} = gun:await(ConnPid, Ref),
4321 {response, fin, 200, _} = gun:await(ConnPid, Ref),
4322 %% We should not get a body in the pushed resource
4323 %% since there was no body in the request.
4324 {response, fin, 200, _} = gun:await(ConnPid, PushRef),
4325 ok.
4326
4327 %% (RFC7540 8.2.1)
4328 % The header fields in PUSH_PROMISE and any subsequent CONTINUATION
4329 % frames MUST be a valid and complete set of request header fields
4330 % (Section 8.1.2.3). The server MUST include a method in the ":method"
4331 % pseudo-header field that is safe and cacheable. If a client receives
4332 % a PUSH_PROMISE that does not include a complete and valid set of
4333 % header fields or the ":method" pseudo-header field identifies a
4334 % method that is not safe, it MUST respond with a stream error
4335 % (Section 5.4.2) of type PROTOCOL_ERROR.
4336 %
4337 %% @todo This probably should be documented.
4338 % The server SHOULD send PUSH_PROMISE (Section 6.6) frames prior to
4339 % sending any frames that reference the promised responses. This
4340 % avoids a race where clients issue requests prior to receiving any
4341 % PUSH_PROMISE frames.
4342 %
4343 % PUSH_PROMISE frames MUST NOT be sent by the client.
4344 %
4345 % PUSH_PROMISE frames can be sent by the server in response to any
4346 % client-initiated stream, but the stream MUST be in either the "open"
4347 % or "half-closed (remote)" state with respect to the server.
4348 % PUSH_PROMISE frames are interspersed with the frames that comprise a
4349 % response, though they cannot be interspersed with HEADERS and
4350 % CONTINUATION frames that comprise a single header block.
4351
4352 %% (RFC7540 8.2.2)
4353 % If the client determines, for any reason, that it does not wish to
4354 % receive the pushed response from the server or if the server takes
4355 % too long to begin sending the promised response, the client can send
4356 % a RST_STREAM frame, using either the CANCEL or REFUSED_STREAM code
4357 % and referencing the pushed stream's identifier.
4358 %
4359 % A client can use the SETTINGS_MAX_CONCURRENT_STREAMS setting to limit
4360 % the number of responses that can be concurrently pushed by a server.
4361 % Advertising a SETTINGS_MAX_CONCURRENT_STREAMS value of zero disables
4362 % server push by preventing the server from creating the necessary
4363 % streams. This does not prohibit a server from sending PUSH_PROMISE
4364 % frames; clients need to reset any promised streams that are not
4365 % wanted.
4366
4367 %% @todo Implement CONNECT. (RFC7540 8.3)
4368
4369 status_code_421(Config) ->
4370 doc("The 421 Misdirected Request status code can be sent. (RFC7540 9.1.2)"),
4371 ConnPid = gun_open(Config),
4372 Ref = gun:get(ConnPid, "/resp/reply2/421"),
4373 {response, fin, 421, _} = gun:await(ConnPid, Ref),
4374 ok.
4375
4376 %% @todo Review (RFC7540 9.2, 9.2.1, 9.2.2) TLS 1.2 usage.
4377 %% We probably want different ways to enforce these to simplify the life
4378 %% of users. A function cowboy:start_h2_tls could do the same as start_tls
4379 %% but with the security requirements of HTTP/2 enforced. Another way is to
4380 %% have an option at the establishment of the connection that checks that
4381 %% the security of the connection is adequate.
4382
4383 %% (RFC7540 10.3)
4384 % The HTTP/2 header field encoding allows the expression of names that
4385 % are not valid field names in the Internet Message Syntax used by
4386 % HTTP/1.1. Requests or responses containing invalid header field
4387 % names MUST be treated as malformed (Section 8.1.2.6).
4388 %
4389 % Similarly, HTTP/2 allows header field values that are not valid.
4390 % While most of the values that can be encoded will not alter header
4391 % field parsing, carriage return (CR, ASCII 0xd), line feed (LF, ASCII
4392 % 0xa), and the zero character (NUL, ASCII 0x0) might be exploited by
4393 % an attacker if they are translated verbatim. Any request or response
4394 % that contains a character not permitted in a header field value MUST
4395 % be treated as malformed (Section 8.1.2.6). Valid characters are
4396 % defined by the "field-content" ABNF rule in Section 3.2 of [RFC7230].
4397
4398 %% (RFC7540 10.5) Denial-of-Service Considerations
4399 % An endpoint that doesn't monitor this behavior exposes itself to a
4400 % risk of denial-of-service attack. Implementations SHOULD track the
4401 % use of these features and set limits on their use. An endpoint MAY
4402 % treat activity that is suspicious as a connection error
4403 % (Section 5.4.1) of type ENHANCE_YOUR_CALM.
4404
4405 %% (RFC7540 10.5.1)
4406 % A server that receives a larger header block than it is willing to
4407 % handle can send an HTTP 431 (Request Header Fields Too Large) status
4408 % code [RFC6585]. A client can discard responses that it cannot
4409 % process. The header block MUST be processed to ensure a consistent
4410 % connection state, unless the connection is closed.
4411
4412 %% @todo Implement CONNECT and limit the number of CONNECT streams (RFC7540 10.5.2).
4413
4414 %% @todo This probably should be documented. (RFC7540 10.6)
4415 % Implementations communicating on a secure channel MUST NOT compress
4416 % content that includes both confidential and attacker-controlled data
4417 % unless separate compression dictionaries are used for each source of
4418 % data. Compression MUST NOT be used if the source of data cannot be
4419 % reliably determined. Generic stream compression, such as that
4420 % provided by TLS, MUST NOT be used with HTTP/2 (see Section 9.2).
4421
4422 %% (RFC7540 A)
4423 % An HTTP/2 implementation MAY treat the negotiation of any of the
4424 % following cipher suites with TLS 1.2 as a connection error
4425 % (Section 5.4.1) of type INADEQUATE_SECURITY.