Debian Codegraph @

test/rfc7230_SUITE.erl

raw
1 %% Copyright (c) Loïc Hoguin <[email protected]>
2 %%
3 %% Permission to use, copy, modify, and/or distribute this software for any
4 %% purpose with or without fee is hereby granted, provided that the above
5 %% copyright notice and this permission notice appear in all copies.
6 %%
7 %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13 %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15 -module(rfc7230_SUITE).
16 -compile(export_all).
17 -compile(nowarn_export_all).
18
19 -import(ct_helper, [doc/1]).
20 -import(cowboy_test, [gun_open/1]).
21 -import(cowboy_test, [gun_down/1]).
22 -import(cowboy_test, [raw_open/1]).
23 -import(cowboy_test, [raw_send/2]).
24 -import(cowboy_test, [raw_recv_head/1]).
25 -import(cowboy_test, [raw_recv_rest/3]).
26 -import(cowboy_test, [raw_recv/3]).
27
28 suite() ->
29 [{timetrap, 30000}].
30
31 all() -> [{group, http}].
32
33 groups() -> [{http, [parallel], ct_helper:all(?MODULE)}].
34
35 init_per_group(Name = http, Config) ->
36 cowboy_test:init_http(Name = http, #{
37 env => #{dispatch => cowboy_router:compile(init_routes(Config))},
38 max_keepalive => 100
39 }, Config).
40
41 end_per_group(Name, _) ->
42 ok = cowboy:stop_listener(Name).
43
44 init_routes(_) -> [
45 {"localhost", [
46 {"/", hello_h, []},
47 {"/echo/:key[/:arg]", echo_h, []},
48 {"/full/:key[/:arg]", echo_h, []},
49 {"/length/echo/:key", echo_h, []},
50 {"/resp/:key[/:arg]", resp_h, []},
51 {"/send_message", send_message_h, []},
52 {"*", asterisk_h, []}
53 ]},
54 {"127.0.0.1", [{"/echo/:key", echo_h, []}]},
55 {"example.org", [{"/echo/:key", echo_h, []}]}
56 %% @todo Add IPv6 addresses support to the router. This fails:
57 %% {"[2001:db8:85a3::8a2e:370:7334]", [{"/echo/:key", echo_h, []}]}
58 ].
59
60 do_raw(Config, Data) ->
61 Client = raw_open(Config),
62 ok = raw_send(Client, Data),
63 {Version, Code, Reason, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),
64 {Headers, Rest2} = cow_http:parse_headers(Rest),
65 case lists:keyfind(<<"content-length">>, 1, Headers) of
66 {_, LengthBin} when LengthBin =/= <<"0">> ->
67 Body = raw_recv_rest(Client, binary_to_integer(LengthBin), Rest2),
68 #{client => Client, version => Version, code => Code, reason => Reason, headers => Headers, body => Body};
69 _ ->
70 #{client => Client, version => Version, code => Code, reason => Reason, headers => Headers, body => <<>>}
71 end.
72
73 %% Listener.
74
75 %% @todo Add to documentation.
76 %The default port for "http" connections is 80. The connection
77 %uses plain TCP. (RFC7230 2.7.1)
78 %
79 %The default port for "https" connections is 443. The connection
80 %uses TLS. (RFC7230 2.7.2)
81 %
82 %Any other port may be used for either of them.
83
84 %% Before the request.
85
86 accept_at_least_1_empty_line(Config) ->
87 doc("A configurable number of empty lines (CRLF) preceding the request "
88 "must be ignored. At least 1 empty line must be ignored. (RFC7230 3.5)"),
89 #{code := 200} = do_raw(Config,
90 "\r\n"
91 "GET / HTTP/1.1\r\n"
92 "Host: localhost\r\n"
93 "\r\n").
94
95 reject_response(Config) ->
96 doc("When receiving a response instead of a request, identified by the "
97 "status-line which starts with the HTTP version, the server must "
98 "reject the message with a 400 status code and close the connection. (RFC7230 3.1)"),
99 #{code := 400, client := Client} = do_raw(Config,
100 "HTTP/1.1 200 OK\r\n"
101 "\r\n"),
102 {error, closed} = raw_recv(Client, 0, 1000).
103
104 %% Request.
105
106 only_parse_necessary_elements(Config) ->
107 doc("It is only necessary to parse elements required to process the request. (RFC7230 2.5)"),
108 #{code := 200} = do_raw(Config,
109 "GET / HTTP/1.1\r\n"
110 "Host: localhost\r\n"
111 "Content-type: purposefully bad header value\r\n"
112 "\r\n").
113
114 %% @todo Add to documentation.
115 %Parsed elements are subject to configurable limits. A server must
116 %be able to parse elements at least as long as it generates. (RFC7230 2.5)
117
118 no_empty_line_after_request_line(Config) ->
119 doc("The general format of HTTP requests is strict. No empty line is "
120 "allowed in-between components except for the empty line "
121 "indicating the end of the list of headers."),
122 #{code := 400} = do_raw(Config,
123 "GET / HTTP/1.1\r\n"
124 "\r\n"
125 "Host: localhost\r\n"
126 "\r\n").
127
128 no_empty_line_in_headers(Config) ->
129 doc("The general format of HTTP requests is strict. No empty line is "
130 "allowed in-between components except for the empty line "
131 "indicating the end of the list of headers."),
132 #{code := 400} = do_raw(Config,
133 "GET / HTTP/1.1\r\n"
134 "User-Agent: RFC7230\r\n"
135 "\r\n"
136 "Host: localhost\r\n"
137 "\r\n").
138
139 timeout_before_request_line(Config) ->
140 doc("The time the request (request line and headers) takes to be "
141 "received by the server must be limited and subject to configuration. "
142 "No response must be sent before closing if no request was initiated "
143 "by the reception of a complete request-line."),
144 Client = raw_open(Config),
145 ok = raw_send(Client, "GET / HTTP/1.1\r"),
146 {error, closed} = raw_recv(Client, 0, 6000).
147
148 timeout_after_request_line(Config) ->
149 doc("The time the request (request line and headers) takes to be "
150 "received by the server must be limited and subject to configuration. "
151 "A 408 status code must be sent if the request line was received."),
152 #{code := 408, client := Client1} = do_raw(Config, "GET / HTTP/1.1\r\n"),
153 {error, closed} = raw_recv(Client1, 0, 6000).
154
155 timeout_after_request_line_host(Config) ->
156 doc("The time the request (request line and headers) takes to be "
157 "received by the server must be limited and subject to configuration. "
158 "A 408 status code must be sent if the request line was received."),
159 #{code := 408, client := Client2} = do_raw(Config, "GET / HTTP/1.1\r\nHost: localhost"),
160 {error, closed} = raw_recv(Client2, 0, 6000).
161
162 timeout_after_request_line_host_crlf(Config) ->
163 doc("The time the request (request line and headers) takes to be "
164 "received by the server must be limited and subject to configuration. "
165 "A 408 status code must be sent if the request line was received."),
166 #{code := 408, client := Client3} = do_raw(Config, "GET / HTTP/1.1\r\nHost: localhost\r\n"),
167 {error, closed} = raw_recv(Client3, 0, 6000).
168
169 timeout_after_request_line_host_crlfcr(Config) ->
170 doc("The time the request (request line and headers) takes to be "
171 "received by the server must be limited and subject to configuration. "
172 "A 408 status code must be sent if the request line was received."),
173 #{code := 408, client := Client4} = do_raw(Config, "GET / HTTP/1.1\r\nHost: localhost\r\n\r"),
174 {error, closed} = raw_recv(Client4, 0, 6000).
175
176 %% Request line.
177
178 limit_request_line_8000(Config) ->
179 doc("It is recommended to limit the request-line length to a configurable "
180 "limit of at least 8000 octets."),
181 LongPath = ["/long-path" || _ <- lists:seq(1, 799)],
182 #{code := 200} = do_raw(Config, [
183 "GET /?qs=", LongPath, " HTTP/1.1\r\n"
184 "Host: localhost\r\n"
185 "\r\n"]).
186
187 limit_request_line_9000(Config) ->
188 doc("It is recommended to limit the request-line length to a configurable "
189 "limit of at least 8000 octets. A request line too long must be rejected "
190 "with a 414 status code and the closing of the connection. (RFC7230 3.1.1)"),
191 LongPath = ["/long-path" || _ <- lists:seq(1, 899)],
192 #{code := 414, client := Client} = do_raw(Config, [
193 "GET /very", LongPath, " HTTP/1.1\r\n"
194 "Host: localhost\r\n"
195 "\r\n"]),
196 {error, closed} = raw_recv(Client, 0, 1000).
197
198 reject_lf_line_breaks_request_line(Config) ->
199 doc("A server may accept header names separated by a single LF, instead of "
200 "CRLF. Cowboy rejects all requests that use LF as separator. (RFC7230 3.5)"),
201 #{code := 400, client := Client} = do_raw(Config, [
202 "POST /echo/read_body HTTP/1.1\n"
203 "Host: localhost\r\n"
204 "Transfer-encoding: chunked\r\n"
205 "\r\n"
206 "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
207 {error, closed} = raw_recv(Client, 0, 1000).
208
209 %% Method.
210
211 reject_invalid_method(Config) ->
212 doc("The request method is defined as 1+ token characters. An invalid "
213 "method must be rejected with a 400 status code and the "
214 "closing of the connection. (RFC7230 3.1.1, RFC7230 3.2.6)"),
215 #{code := 400, client := Client} = do_raw(Config,
216 "GET\0 / HTTP/1.1\r\n"
217 "Host: localhost\r\n"
218 "\r\n"),
219 {error, closed} = raw_recv(Client, 0, 1000).
220
221 reject_empty_method(Config) ->
222 doc("The request method is defined as 1+ token characters. An empty "
223 "method must be rejected with a 400 status code and the "
224 "closing of the connection. (RFC7230 3.1.1, RFC7230 3.2.6)"),
225 #{code := 400, client := Client} = do_raw(Config,
226 " / HTTP/1.1\r\n"
227 "Host: localhost\r\n"
228 "\r\n"),
229 {error, closed} = raw_recv(Client, 0, 1000).
230
231 %% @todo We probably want to directly match commonly used methods.
232 %In practice the only characters in use by registered methods are
233 %uppercase letters [A-Z] and the dash "-". (IANA HTTP Method Registry)
234
235 limit_method_name(Config) ->
236 doc("The length of the method must be subject to a configurable limit. "
237 "A method too long must be rejected with a 501 status code and the "
238 "closing of the connection. A good default for the method length limit "
239 "is the longest method length the server implements. (RFC7230 3.1.1)"),
240 LongMethod = [$G || _ <- lists:seq(1, 1000)],
241 #{code := 501, client := Client} = do_raw(Config, [
242 LongMethod, " / HTTP/1.1\r\n"
243 "Host: localhost\r\n"
244 "\r\n"]),
245 {error, closed} = raw_recv(Client, 0, 1000).
246
247 %% Between method and request-target.
248
249 reject_tab_between_method_and_request_target(Config) ->
250 doc("A request that uses anything other than SP as separator between "
251 "the method and the request-target must be rejected with a 400 "
252 "status code and the closing of the connection. (RFC7230 3.1.1, RFC7230 3.5)"),
253 #{code := 400, client := Client} = do_raw(Config,
254 "GET\t/ HTTP/1.1\r\n"
255 "Host: localhost\r\n"
256 "\r\n"),
257 {error, closed} = raw_recv(Client, 0, 1000).
258
259 reject_two_sp_between_method_and_request_target(Config) ->
260 doc("A request that uses anything other than SP as separator between "
261 "the method and the request-target must be rejected with a 400 "
262 "status code and the closing of the connection. (RFC7230 3.1.1, RFC7230 3.5)"),
263 #{code := 400, client := Client} = do_raw(Config,
264 "GET / HTTP/1.1\r\n"
265 "Host: localhost\r\n"
266 "\r\n"),
267 {error, closed} = raw_recv(Client, 0, 1000).
268
269 %% Request target.
270
271 reject_uri_fragment_after_path(Config) ->
272 doc("A target URI containing a fragment must be rejected with a 400 "
273 "error and the closing of the connection. (RFC9110 4.1, RFC9112 3.2)"),
274 #{code := 400} = do_raw(Config,
275 "GET /echo/uri#fragment HTTP/1.1\r\n"
276 "Host: localhost\r\n"
277 "\r\n").
278
279 reject_uri_fragment_after_query(Config) ->
280 doc("A target URI containing a fragment must be rejected with a 400 "
281 "error and the closing of the connection. (RFC9110 4.1, RFC9112 3.2)"),
282 #{code := 400} = do_raw(Config,
283 "GET /echo/uri?key=value#fragment HTTP/1.1\r\n"
284 "Host: localhost\r\n"
285 "\r\n").
286
287 %% Request target: origin-form.
288
289 must_understand_origin_form(Config) ->
290 doc("A server must be able to handle at least origin-form and absolute-form. (RFC7230 5.3.2)"),
291 #{code := 200} = do_raw(Config,
292 "GET / HTTP/1.1\r\n"
293 "Host: localhost\r\n"
294 "\r\n").
295
296 %% @todo Reenable this test once support for CONNECT is added.
297 %origin_form_reject_if_connect(Config) ->
298 % doc("origin-form is used when the client does not connect to a proxy, "
299 % "does not use the CONNECT method and does not issue a site-wide "
300 % "OPTIONS request. (RFC7230 5.3.1)"),
301 % #{code := 400, client := Client} = do_raw(Config,
302 % "CONNECT / HTTP/1.1\r\n"
303 % "Host: localhost\r\n"
304 % "\r\n"),
305 % {error, closed} = raw_recv(Client, 0, 1000).
306
307 %% @todo Equivalent test for https.
308 origin_form_tcp_scheme(Config) ->
309 doc("The scheme is either resolved from configuration or is \"https\" "
310 "when on a TLS connection and \"http\" otherwise. (RFC7230 5.5)"),
311 Echo = <<"http://localhost/echo/uri">>,
312 #{code := 200, body := Echo} = do_raw(Config,
313 "GET /echo/uri HTTP/1.1\r\n"
314 "Host: localhost\r\n"
315 "\r\n").
316
317 origin_form_path(Config) ->
318 doc("The absolute-path always starts with \"/\" and ends with either \"?\" "
319 "or the end of the URI. (RFC3986 3.3)"),
320 Echo = <<"/echo/path">>,
321 #{code := 200, body := Echo} = do_raw(Config,
322 "GET /echo/path HTTP/1.1\r\n"
323 "Host: localhost\r\n"
324 "\r\n").
325
326 origin_form_reject_invalid_path(Config) ->
327 doc("An invalid request target must be rejected with a 400 status code "
328 "and the closing of the connection. (RFC9110 5.5)"),
329 #{code := 400, client := Client} = do_raw(Config,
330 "GET /echo\0/path HTTP/1.1\r\n"
331 "Host: localhost\r\n"
332 "\r\n"),
333 {error, closed} = raw_recv(Client, 0, 1000).
334
335 origin_form_path_query(Config) ->
336 doc("The absolute-path always starts with \"/\" and ends with either \"?\" "
337 "or the end of the URI. (RFC3986 3.3)"),
338 Echo = <<"/echo/path">>,
339 #{code := 200, body := Echo} = do_raw(Config,
340 "GET /echo/path?key=value HTTP/1.1\r\n"
341 "Host: localhost\r\n"
342 "\r\n").
343
344 origin_form_path_fragment(Config) ->
345 doc("A target URI containing a fragment must be rejected with a 400 "
346 "error and the closing of the connection. (RFC9110 4.1, RFC9112 3.2)"),
347 #{code := 400} = do_raw(Config,
348 "GET /echo/path#fragment HTTP/1.1\r\n"
349 "Host: localhost\r\n"
350 "\r\n").
351
352 origin_form_query(Config) ->
353 doc("The query starts with \"?\" and ends with the end of the URI. (RFC3986 3.4)"),
354 Echo = <<"key=value">>,
355 #{code := 200, body := Echo} = do_raw(Config,
356 "GET /echo/qs?key=value HTTP/1.1\r\n"
357 "Host: localhost\r\n"
358 "\r\n").
359
360 origin_form_reject_invalid_query(Config) ->
361 doc("An invalid request target must be rejected with a 400 status code "
362 "and the closing of the connection. (RFC9110 5.5)"),
363 #{code := 400, client := Client} = do_raw(Config,
364 "GET /echo/qs?key=\0value HTTP/1.1\r\n"
365 "Host: localhost\r\n"
366 "\r\n"),
367 {error, closed} = raw_recv(Client, 0, 1000).
368
369 origin_form_query_fragment(Config) ->
370 doc("A target URI containing a fragment must be rejected with a 400 "
371 "error and the closing of the connection. (RFC9110 4.1, RFC9112 3.2)"),
372 #{code := 400} = do_raw(Config,
373 "GET /echo/qs?key=value#fragment HTTP/1.1\r\n"
374 "Host: localhost\r\n"
375 "\r\n").
376
377 %% @todo origin_form: reject paths with too large depth or query strings with too many keys
378
379 %% Request target: absolute-form.
380
381 must_understand_absolute_form(Config) ->
382 doc("A server must be able to handle at least origin-form and absolute-form. (RFC7230 5.3.2)"),
383 #{code := 200} = do_raw(Config,
384 "GET http://localhost HTTP/1.1\r\n"
385 "Host: localhost\r\n"
386 "\r\n").
387
388 absolute_form_case_insensitive_scheme(Config) ->
389 doc("The scheme is case insensitive and normally provided in lowercase. (RFC7230 2.7.3)"),
390 Echo = <<"http://localhost/echo/uri">>,
391 #{code := 200, body := Echo} = do_raw(Config,
392 "GET HttP://localhost/echo/uri HTTP/1.1\r\n"
393 "Host: localhost\r\n"
394 "\r\n").
395
396 absolute_form_case_insensitive_host(Config) ->
397 doc("The host is case insensitive and normally provided in lowercase. (RFC7230 2.7.3)"),
398 Echo = <<"http://localhost/echo/uri">>,
399 #{code := 200, body := Echo} = do_raw(Config,
400 "GET http://LoCaLHOsT/echo/uri HTTP/1.1\r\n"
401 "Host: LoCaLHOsT\r\n"
402 "\r\n").
403
404 absolute_form_reject_unknown_scheme(Config) ->
405 doc("Unknown schemes must be rejected with a 400 status code and the closing of the connection."),
406 #{code := 400, client := Client} = do_raw(Config,
407 "GET bad://localhost/ HTTP/1.1\r\n"
408 "Host: localhost\r\n"
409 "\r\n"),
410 {error, closed} = raw_recv(Client, 0, 1000).
411
412 absolute_form_reject_invalid_scheme(Config) ->
413 doc("An invalid request target must be rejected with a 400 status code "
414 "and the closing of the connection. (RFC9110 5.5)"),
415 #{code := 400, client := Client} = do_raw(Config,
416 "GET ht\0tp://localhost/ HTTP/1.1\r\n"
417 "Host: localhost\r\n"
418 "\r\n"),
419 {error, closed} = raw_recv(Client, 0, 1000).
420
421 absolute_form_reject_invalid_scheme_delimiter(Config) ->
422 doc("An invalid request target must be rejected with a 400 status code "
423 "and the closing of the connection. (RFC9110 5.5)"),
424 #{code := 400, client := Client} = do_raw(Config,
425 "GET http:/\0/localhost/ HTTP/1.1\r\n"
426 "Host: localhost\r\n"
427 "\r\n"),
428 {error, closed} = raw_recv(Client, 0, 1000).
429
430 %% @todo Equivalent test for https.
431 absolute_form_drop_scheme_tcp(Config) ->
432 doc("The scheme provided with the request must be dropped. The effective "
433 "scheme is either resolved from configuration or is \"https\" when on "
434 "a TLS connection and \"http\" otherwise. (RFC7230 5.5)"),
435 Echo = <<"http://localhost/echo/uri">>,
436 #{code := 200, body := Echo} = do_raw(Config,
437 "GET https://localhost/echo/uri HTTP/1.1\r\n"
438 "Host: localhost\r\n"
439 "\r\n").
440
441 absolute_form_reject_userinfo(Config) ->
442 doc("An authority component with a userinfo component (and its "
443 "\"@\" delimiter) is invalid. The request must be rejected with "
444 "a 400 status code and the closing of the connection. (RFC7230 2.7.1)"),
445 #{code := 400, client := Client} = do_raw(Config,
446 "GET http://username:password@localhost HTTP/1.1\r\n"
447 "Host: localhost\r\n"
448 "\r\n"),
449 {error, closed} = raw_recv(Client, 0, 1000).
450
451 absolute_form_reject_missing_host_without_path(Config) ->
452 doc("A URI with a missing host identifier is invalid. The request must "
453 "be rejected with a 400 status code and the closing of the connection. (RFC7230 2.7.1)"),
454 #{code := 400, client := Client} = do_raw(Config,
455 "GET http:// HTTP/1.1\r\n"
456 "Host: localhost\r\n"
457 "\r\n"),
458 {error, closed} = raw_recv(Client, 0, 1000).
459
460 absolute_form_reject_missing_host_with_path(Config) ->
461 doc("A URI with a missing host identifier is invalid. The request must "
462 "be rejected with a 400 status code and the closing of the connection. (RFC7230 2.7.1)"),
463 #{code := 400, client := Client} = do_raw(Config,
464 "GET http:/// HTTP/1.1\r\n"
465 "Host: localhost\r\n"
466 "\r\n"),
467 {error, closed} = raw_recv(Client, 0, 1000).
468
469 absolute_form_reject_invalid_host(Config) ->
470 doc("An invalid request target must be rejected with a 400 status code "
471 "and the closing of the connection. (RFC9110 5.5)"),
472 #{code := 400, client := Client} = do_raw(Config,
473 "GET http://local\0host/ HTTP/1.1\r\n"
474 "Host: localhost\r\n"
475 "\r\n"),
476 {error, closed} = raw_recv(Client, 0, 1000).
477
478 absolute_form_ipv4(Config) ->
479 doc("Absolute form with an IPv4 address for the host. (RFC3986 3.2.2)"),
480 Echo = <<"127.0.0.1">>,
481 #{code := 200, body := Echo} = do_raw(Config,
482 "GET http://127.0.0.1/echo/host HTTP/1.1\r\n"
483 "Host: 127.0.0.1\r\n"
484 "\r\n").
485
486 absolute_form_ipv4_port(Config) ->
487 doc("Absolute form with an IPv4 address for the host and a port number. (RFC3986 3.2.2)"),
488 Host = <<"127.0.0.1">>,
489 #{code := 200, body := Host} = do_raw(Config,
490 "GET http://127.0.0.1:8080/echo/host HTTP/1.1\r\n"
491 "Host: 127.0.0.1:8080\r\n"
492 "\r\n"),
493 Port = <<"8080">>,
494 #{code := 200, body := Port} = do_raw(Config,
495 "GET http://127.0.0.1:8080/echo/port HTTP/1.1\r\n"
496 "Host: 127.0.0.1:8080\r\n"
497 "\r\n").
498
499 %% @todo We need the router to support IPv6 addresses to write proper tests for these:
500 %absolute_form_ipv6(Config) ->
501 %absolute_form_ipv6_ipv4(Config) ->
502 %absolute_form_ipv6_zoneid(Config) ->
503
504 absolute_form_reg_name(Config) ->
505 doc("Absolute form with a regular name for the host. (RFC3986 3.2.2)"),
506 Echo = <<"example.org">>,
507 #{code := 200, body := Echo} = do_raw(Config,
508 "GET http://example.org/echo/host HTTP/1.1\r\n"
509 "Host: example.org\r\n"
510 "\r\n").
511
512 absolute_form_reg_name_port(Config) ->
513 doc("Absolute form with an IPv4 address for the host and a port number. (RFC3986 3.2.2)"),
514 Host = <<"example.org">>,
515 #{code := 200, body := Host} = do_raw(Config,
516 "GET http://example.org:8080/echo/host HTTP/1.1\r\n"
517 "Host: example.org:8080\r\n"
518 "\r\n"),
519 Port = <<"8080">>,
520 #{code := 200, body := Port} = do_raw(Config,
521 "GET http://example.org:8080/echo/port HTTP/1.1\r\n"
522 "Host: example.org:8080\r\n"
523 "\r\n").
524
525 absolute_form_limit_host(Config) ->
526 doc("The maximum length for the host component of the URI must be subject "
527 "to a configurable limit. A good default is 255 characters. "
528 "(RFC7230 3.1.1, RFC3986 3.2.2, RFC1034 3.1)"),
529 LongHost = ["host." || _ <- lists:seq(1, 100)],
530 #{code := 414, client := Client} = do_raw(Config, [
531 "GET http://", LongHost, "/ HTTP/1.1\r\n"
532 "Host: ", LongHost, "\r\n"
533 "\r\n"]),
534 {error, closed} = raw_recv(Client, 0, 1000).
535
536 absolute_form_reject_invalid_port_0(Config) ->
537 doc("Port number 0 is reserved. The request must be rejected and the connection closed."),
538 #{code := 400, client := Client} = do_raw(Config,
539 "GET http://localhost:0/ HTTP/1.1\r\n"
540 "Host: localhost:0\r\n"
541 "\r\n"),
542 {error, closed} = raw_recv(Client, 0, 1000).
543
544 absolute_form_reject_invalid_port_65536(Config) ->
545 doc("Port numbers above 65535 are invalid. The request must be rejected "
546 "and the connection closed."),
547 #{code := 400, client := Client} = do_raw(Config,
548 "GET http://localhost:65536/ HTTP/1.1\r\n"
549 "Host: localhost:65536\r\n"
550 "\r\n"),
551 {error, closed} = raw_recv(Client, 0, 1000).
552
553 absolute_form_reject_invalid_port_nul(Config) ->
554 doc("An invalid request target must be rejected with a 400 status code "
555 "and the closing of the connection. (RFC9110 5.5)"),
556 #{code := 400, client := Client} = do_raw(Config,
557 "GET http://localhost:80\080/ HTTP/1.1\r\n"
558 "Host: localhost:8080\r\n"
559 "\r\n"),
560 {error, closed} = raw_recv(Client, 0, 1000).
561
562 %% @todo The RFC says to discard the Host header if we are a proxy,
563 %% and replace it with the content of absolute-form. This means
564 %% that we should probably keep the absolute-form value when
565 %% operating in proxy mode. Otherwise the absolute-form value
566 %% is simply dropped and the Host header is used.
567
568 %% @todo The authority is sent both in the URI and in the host header.
569 %% The authority from the URI must be dropped, and the host header
570 %% must be used instead. (RFC7230 5.5)
571 %%
572 %% It is not possible to test that the absolute-form value is dropped
573 %% because one of the Host header test ensures that the authority
574 %% is the same in both, and errors out otherwise.
575
576 absolute_form_path(Config) ->
577 doc("The path always starts with \"/\" and ends with either \"?\" "
578 "or the end of the URI. (RFC3986 3.3)"),
579 Echo = <<"/echo/path">>,
580 #{code := 200, body := Echo} = do_raw(Config,
581 "GET http://localhost/echo/path HTTP/1.1\r\n"
582 "Host: localhost\r\n"
583 "\r\n").
584
585 absolute_form_reject_invalid_path(Config) ->
586 doc("An invalid request target must be rejected with a 400 status code "
587 "and the closing of the connection. (RFC9110 5.5)"),
588 #{code := 400, client := Client} = do_raw(Config,
589 "GET http://localhost/echo\0/path HTTP/1.1\r\n"
590 "Host: localhost\r\n"
591 "\r\n"),
592 {error, closed} = raw_recv(Client, 0, 1000).
593
594 absolute_form_path_query(Config) ->
595 doc("The path always starts with \"/\" and ends with either \"?\" "
596 "or the end of the URI. (RFC3986 3.3)"),
597 Echo = <<"/echo/path">>,
598 #{code := 200, body := Echo} = do_raw(Config,
599 "GET http://localhost/echo/path?key=value HTTP/1.1\r\n"
600 "Host: localhost\r\n"
601 "\r\n").
602
603 absolute_form_reject_invalid_query(Config) ->
604 doc("An invalid request target must be rejected with a 400 status code "
605 "and the closing of the connection. (RFC9110 5.5)"),
606 #{code := 400, client := Client} = do_raw(Config,
607 "GET http://localhost/echo/qs?key=\0value HTTP/1.1\r\n"
608 "Host: localhost\r\n"
609 "\r\n"),
610 {error, closed} = raw_recv(Client, 0, 1000).
611
612 absolute_form_path_fragment(Config) ->
613 doc("A target URI containing a fragment must be rejected with a 400 "
614 "error and the closing of the connection. (RFC9110 4.1, RFC9112 3.2)"),
615 #{code := 400} = do_raw(Config,
616 "GET http://localhost/echo/path#fragment HTTP/1.1\r\n"
617 "Host: localhost\r\n"
618 "\r\n").
619
620 absolute_form_no_path(Config) ->
621 doc("An empty path component is equivalent to \"/\". (RFC7230 2.7.3)"),
622 #{code := 200, body := <<"Hello world!">>} = do_raw(Config,
623 "GET http://localhost HTTP/1.1\r\n"
624 "Host: localhost\r\n"
625 "\r\n").
626
627 absolute_form_no_path_then_query(Config) ->
628 doc("An empty path component is equivalent to \"/\". (RFC7230 2.7.3)"),
629 #{code := 200, body := <<"Hello world!">>} = do_raw(Config,
630 "GET http://localhost?key=value HTTP/1.1\r\n"
631 "Host: localhost\r\n"
632 "\r\n").
633
634 absolute_form_no_path_then_fragment(Config) ->
635 doc("A target URI containing a fragment must be rejected with a 400 "
636 "error and the closing of the connection. (RFC9110 4.1, RFC9112 3.2)"),
637 #{code := 400} = do_raw(Config,
638 "GET http://localhost#fragment HTTP/1.1\r\n"
639 "Host: localhost\r\n"
640 "\r\n").
641
642 absolute_form_query(Config) ->
643 doc("The query starts with \"?\" and ends with the end of the URI. (RFC3986 3.4)"),
644 Echo = <<"key=value">>,
645 #{code := 200, body := Echo} = do_raw(Config,
646 "GET http://localhost/echo/qs?key=value HTTP/1.1\r\n"
647 "Host: localhost\r\n"
648 "\r\n").
649
650 absolute_form_query_fragment(Config) ->
651 doc("A target URI containing a fragment must be rejected with a 400 "
652 "error and the closing of the connection. (RFC9110 4.1, RFC9112 3.2)"),
653 #{code := 400} = do_raw(Config,
654 "GET http://localhost/echo/qs?key=value#fragment HTTP/1.1\r\n"
655 "Host: localhost\r\n"
656 "\r\n").
657
658 %% @todo absolute_form: reject paths with too large depth or query strings with too many keys
659
660 %% Request-target: authority-form.
661
662 authority_form_reject_if_not_connect(Config) ->
663 doc("When the method is CONNECT, authority-form must be used. This "
664 "form does not apply to any other methods which must reject the "
665 "request with a 400 status code and the closing of the connection. (RFC7230 5.3.3)"),
666 #{code := 400, client := Client} = do_raw(Config,
667 "GET localhost:80 HTTP/1.1\r\n"
668 "Host: localhost\r\n"
669 "\r\n"),
670 {error, closed} = raw_recv(Client, 0, 1000).
671
672 %% @todo Implement CONNECT.
673 %authority_form_reject_userinfo(Config) ->
674 %An authority component with a userinfo component (and its
675 %"@" delimiter) is invalid. The request must be rejected with
676 %a 400 status code and the closing of the connection. (RFC7230 2.7.1)
677 %
678 %authority_form_limit_host(Config) ->
679 %authority_form_limit_port0(Config) ->
680 %authority_form_limit_port65536(Config) ->
681 %
682 %A request with a too long component of authority-form must be rejected with
683 %a 414 status code and the closing of the connection. (RFC7230 3.1.1)
684 %
685 %The authority is either resolved from configuration or is taken
686 %directly from authority-form. (RFC7230 5.5)
687 %
688 %authority_form_empty_path(Config) ->
689 %authority_form_empty_query(Config) ->
690 %The path and query are empty when using authority-form. (RFC7230 5.5)
691
692 %% Request-target: asterisk-form.
693
694 asterisk_form_reject_if_not_options(Config) ->
695 doc("asterisk-form is used for server-wide OPTIONS requests. "
696 "It is invalid with any other methods which must reject the "
697 "request with a 400 status code and the closing of the connection. (RFC7230 5.3.4)"),
698 #{code := 400, client := Client} = do_raw(Config,
699 "GET * HTTP/1.1\r\n"
700 "Host: localhost\r\n"
701 "\r\n"),
702 {error, closed} = raw_recv(Client, 0, 1000).
703
704 asterisk_form_empty_path_query(Config) ->
705 doc("The path and query components are empty when using asterisk-form. (RFC7230 5.5)"),
706 #{code := 200, body := <<"http://localhost">>} = do_raw(Config,
707 "OPTIONS * HTTP/1.1\r\n"
708 "Host: localhost\r\n"
709 "X-Echo: uri\r\n"
710 "\r\n").
711
712 asterisk_form_reject_invalid(Config) ->
713 doc("An invalid request target must be rejected with a 400 status code "
714 "and the closing of the connection. (RFC9110 5.5)"),
715 #{code := 400, client := Client} = do_raw(Config,
716 "OPTIONS *\0 HTTP/1.1\r\n"
717 "Host: localhost\r\n"
718 "\r\n"),
719 {error, closed} = raw_recv(Client, 0, 1000).
720
721 %% Invalid request-target.
722
723 reject_invalid_request_target(Config) ->
724 doc("An invalid request target must be rejected with a 400 status code "
725 "and the closing of the connection. (RFC9110 5.5)"),
726 #{code := 400, client := Client} = do_raw(Config,
727 "GET \0 HTTP/1.1\r\n"
728 "Host: localhost\r\n"
729 "\r\n"),
730 {error, closed} = raw_recv(Client, 0, 1000).
731
732 missing_request_target(Config) ->
733 doc("The lack of request target must be rejected with a 400 status code "
734 "and the closing of the connection."),
735 #{code := 400, client := Client} = do_raw(Config,
736 "GET HTTP/1.1\r\n"
737 "Host: localhost\r\n"
738 "\r\n"),
739 {error, closed} = raw_recv(Client, 0, 1000).
740
741 %% Between request-target and version.
742
743 reject_tab_between_request_target_and_version(Config) ->
744 doc("A request that uses anything other than SP as separator between "
745 "the request-target and the version must be rejected with a 400 "
746 "status code and the closing of the connection. (RFC7230 3.1.1, RFC7230 3.5)"),
747 #{code := 400, client := Client} = do_raw(Config,
748 "GET /\tHTTP/1.1\r\n"
749 "Host: localhost\r\n"
750 "\r\n"),
751 {error, closed} = raw_recv(Client, 0, 1000).
752
753 reject_two_sp_between_request_target_and_version(Config) ->
754 doc("A request that uses anything other than SP as separator between "
755 "the request-target and the version must be rejected with a 400 "
756 "status code and the closing of the connection. (RFC7230 3.1.1, RFC7230 3.5)"),
757 #{code := 400, client := Client} = do_raw(Config,
758 "GET / HTTP/1.1\r\n"
759 "Host: localhost\r\n"
760 "\r\n"),
761 {error, closed} = raw_recv(Client, 0, 1000).
762
763 %% Request version.
764
765 reject_invalid_version_http09(Config) ->
766 doc("Any version number other than HTTP/1.0 or HTTP/1.1 must be "
767 "rejected by a server or intermediary with a 505 status code. (RFC7230 2.6, RFC7230 A.2)"),
768 #{code := 505} = do_raw(Config,
769 "GET / HTTP/0.9\r\n"
770 "Host: localhost\r\n"
771 "\r\n").
772
773 reject_invalid_version_http100(Config) ->
774 doc("Any version number other than HTTP/1.0 or HTTP/1.1 must be "
775 "rejected by a server or intermediary with a 505 status code. (RFC7230 2.6, RFC7230 A.2)"),
776 #{code := 505} = do_raw(Config,
777 "GET / HTTP/1.00\r\n"
778 "Host: localhost\r\n"
779 "\r\n").
780
781 reject_invalid_version_http111(Config) ->
782 doc("Any version number other than HTTP/1.0 or HTTP/1.1 must be "
783 "rejected by a server or intermediary with a 505 status code. (RFC7230 2.6, RFC7230 A.2)"),
784 #{code := 505} = do_raw(Config,
785 "GET / HTTP/1.11\r\n"
786 "Host: localhost\r\n"
787 "\r\n").
788
789 reject_invalid_version_http12(Config) ->
790 doc("Any version number other than HTTP/1.0 or HTTP/1.1 must be "
791 "rejected by a server or intermediary with a 505 status code. (RFC7230 2.6, RFC7230 A.2)"),
792 #{code := 505} = do_raw(Config,
793 "GET / HTTP/1.2\r\n"
794 "Host: localhost\r\n"
795 "\r\n").
796
797 reject_invalid_version_http2(Config) ->
798 doc("Any version number other than HTTP/1.0 or HTTP/1.1 must be "
799 "rejected by a server or intermediary with a 505 status code. (RFC7230 2.6, RFC7230 A.2)"),
800 #{code := 505} = do_raw(Config,
801 "GET / HTTP/2\r\n"
802 "Host: localhost\r\n"
803 "\r\n").
804
805 reject_empty_version(Config) ->
806 doc("Any version number other than HTTP/1.0 or HTTP/1.1 must be "
807 "rejected by a server or intermediary with a 505 status code. "
808 "(RFC7230 2.6, RFC7230 A, RFC7230 A.2)"),
809 #{code := 505} = do_raw(Config,
810 "GET / \r\n"
811 "Host: localhost\r\n"
812 "\r\n").
813
814 reject_invalid_whitespace_after_version(Config) ->
815 doc("A request that has whitespace different than CRLF following the "
816 "version must be rejected with a 400 status code and the closing "
817 "of the connection. (RFC7230 3.1.1)"),
818 #{code := 400, client := Client} = do_raw(Config,
819 "GET / HTTP/1.1 \r\n"
820 "Host: localhost\r\n"
821 "\r\n"),
822 {error, closed} = raw_recv(Client, 0, 1000).
823
824 %% Request headers.
825
826 reject_invalid_header_name(Config) ->
827 doc("An invalid header field name must be rejected with a 400 status code "
828 "and the closing of the connection. (RFC9110 5.5)"),
829 #{code := 400, client := Client} = do_raw(Config,
830 "GET / HTTP/1.1\r\n"
831 "Host: localhost\r\n"
832 "X-\0Bad: fooled you\r\n"
833 "\r\n"),
834 {error, closed} = raw_recv(Client, 0, 1000).
835
836 reject_invalid_header_value_cr(Config) ->
837 doc("An invalid header field value must be rejected with a 400 status code "
838 "and the closing of the connection. (RFC9110 5.5)"),
839 #{code := 400, client := Client} = do_raw(Config,
840 "GET / HTTP/1.1\r\n"
841 "Host: localhost\r\n"
842 "X-Bad: value\rrm rf the world\r\n"
843 "\r\n"),
844 {error, closed} = raw_recv(Client, 0, 1000).
845
846 reject_invalid_header_value_nul(Config) ->
847 doc("An invalid header field value must be rejected with a 400 status code "
848 "and the closing of the connection. (RFC9110 5.5)"),
849 #{code := 400, client := Client} = do_raw(Config,
850 "GET / HTTP/1.1\r\n"
851 "Host: localhost\r\n"
852 "X-Bad: value\0rm rf the world\r\n"
853 "\r\n"),
854 {error, closed} = raw_recv(Client, 0, 1000).
855
856 lower_case_header(Config) ->
857 doc("The header field name is case insensitive. (RFC7230 3.2)"),
858 #{code := 200} = do_raw(Config, [
859 "GET / HTTP/1.1\r\n"
860 "host: localhost\r\n"
861 "\r\n"]).
862
863 upper_case_header(Config) ->
864 doc("The header field name is case insensitive. (RFC7230 3.2)"),
865 #{code := 200} = do_raw(Config, [
866 "GET / HTTP/1.1\r\n"
867 "HOST: localhost\r\n"
868 "\r\n"]).
869
870 mixed_case_header(Config) ->
871 doc("The header field name is case insensitive. (RFC7230 3.2)"),
872 #{code := 200} = do_raw(Config, [
873 "GET / HTTP/1.1\r\n"
874 "hOsT: localhost\r\n"
875 "\r\n"]).
876
877 reject_whitespace_before_header_name(Config) ->
878 doc("Messages that contain whitespace before the header name must "
879 "be rejected with a 400 status code and the closing of the "
880 "connection. (RFC7230 3.2.4)"),
881 #{code := 400, client := Client1} = do_raw(Config, [
882 "GET / HTTP/1.1\r\n"
883 " Host: localhost\r\n"
884 "\r\n"]),
885 {error, closed} = raw_recv(Client1, 0, 1000),
886 #{code := 400, client := Client2} = do_raw(Config, [
887 "GET / HTTP/1.1\r\n"
888 "\tHost: localhost\r\n"
889 "\r\n"]),
890 {error, closed} = raw_recv(Client2, 0, 1000).
891
892 reject_whitespace_between_header_name_and_colon(Config) ->
893 doc("Messages that contain whitespace between the header name and "
894 "colon must be rejected with a 400 status code and the closing "
895 "of the connection. (RFC7230 3.2.4)"),
896 #{code := 400, client := Client1} = do_raw(Config, [
897 "GET / HTTP/1.1\r\n"
898 "Host : localhost\r\n"
899 "\r\n"]),
900 {error, closed} = raw_recv(Client1, 0, 1000),
901 #{code := 400, client := Client2} = do_raw(Config, [
902 "GET / HTTP/1.1\r\n"
903 "Host\t: localhost\r\n"
904 "\r\n"]),
905 {error, closed} = raw_recv(Client2, 0, 1000).
906
907 reject_header_name_without_colon(Config) ->
908 doc("Messages that contain a header name that is not followed by a "
909 "colon must be rejected with a 400 status code and the closing "
910 "of the connection. (RFC7230 3.2.4)"),
911 #{code := 400, client := Client1} = do_raw(Config, [
912 "GET / HTTP/1.1\r\n"
913 "Host\r\n"
914 "\r\n"]),
915 {error, closed} = raw_recv(Client1, 0, 1000),
916 #{code := 400, client := Client2} = do_raw(Config, [
917 "GET / HTTP/1.1\r\n"
918 "Host localhost\r\n"
919 "\r\n"]),
920 {error, closed} = raw_recv(Client2, 0, 1000),
921 #{code := 400, client := Client3} = do_raw(Config, [
922 "GET / HTTP/1.1\r\n"
923 "Host\r\n"
924 " : localhost\r\n"
925 "\r\n"]),
926 {error, closed} = raw_recv(Client3, 0, 1000),
927 #{code := 400, client := Client4} = do_raw(Config, [
928 "GET / HTTP/1.1\r\n"
929 "Content-Encoding\r\nX-Invalid: hello\r\n"
930 "Host: localhost\r\n"
931 "\r\n"]),
932 {error, closed} = raw_recv(Client4, 0, 1000).
933
934 limit_header_name(Config) ->
935 doc("The header name must be subject to a configurable limit. A "
936 "good default is 50 characters, well above the longest registered "
937 "header. Such a request must be rejected with a 431 status code "
938 "and the closing of the connection. "
939 "(RFC7230 3.2.5, RFC6585 5, IANA Message Headers registry)"),
940 #{code := 431, client := Client} = do_raw(Config, [
941 "GET / HTTP/1.1\r\n"
942 "Host: localhost\r\n",
943 binary:copy(<<$a>>, 32768), ": bad\r\n"
944 "\r\n"]),
945 {error, closed} = raw_recv(Client, 0, 1000).
946
947 limit_header_value(Config) ->
948 doc("The header value and the optional whitespace around it must be "
949 "subject to a configurable limit. There is no recommendations "
950 "for the default. 4096 characters is known to work well. Such "
951 "a request must be rejected with a 431 status code and the closing "
952 "of the connection. (RFC7230 3.2.5, RFC6585 5)"),
953 #{code := 431, client := Client} = do_raw(Config, [
954 "GET / HTTP/1.1\r\n"
955 "Host: localhost\r\n"
956 "bad: ", binary:copy(<<$a>>, 32768), "\r\n"
957 "\r\n"]),
958 {error, closed} = raw_recv(Client, 0, 1000).
959
960 drop_whitespace_before_header_value(Config) ->
961 doc("Optional whitespace before and after the header value is not "
962 "part of the value and must be dropped."),
963 #{code := 200} = do_raw(Config, [
964 "POST / HTTP/1.1\r\n"
965 "Host: localhost\r\n"
966 "Content-length: \t 12\r\n"
967 "\r\n"
968 "Hello world!"]).
969
970 drop_whitespace_after_header_value(Config) ->
971 doc("Optional whitespace before and after the header value is not "
972 "part of the value and must be dropped."),
973 #{code := 200} = do_raw(Config, [
974 "POST / HTTP/1.1\r\n"
975 "Host: localhost\r\n"
976 "Content-length: 12 \t \r\n"
977 "\r\n"
978 "Hello world!"]).
979
980 reject_lf_line_breaks_after_header_value(Config) ->
981 doc("A server may accept header names separated by a single LF, instead of "
982 "CRLF. Cowboy rejects all requests that use LF as separator. (RFC7230 3.5)"),
983 #{code := 400, client := Client} = do_raw(Config, [
984 "POST /echo/read_body HTTP/1.1\r\n"
985 "Host: localhost\n"
986 "Transfer-encoding: chunked\r\n"
987 "\r\n"
988 "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
989 {error, closed} = raw_recv(Client, 0, 1000).
990
991 reject_lf_line_breaks_after_headers_block(Config) ->
992 doc("A server may accept header names separated by a single LF, instead of "
993 "CRLF. Cowboy rejects all requests that use LF as separator. (RFC7230 3.5)"),
994 #{code := 400, client := Client} = do_raw(Config, [
995 "POST /echo/read_body HTTP/1.1\r\n"
996 "Host: localhost\r\n"
997 "Transfer-encoding: chunked\r\n"
998 "\n"
999 "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
1000 {error, closed} = raw_recv(Client, 0, 1000).
1001
1002 %@todo
1003 %The order of header fields with differing names is not significant. (RFC7230 3.2.2)
1004 %
1005 %@todo
1006 %The normal procedure for parsing headers is to read each header
1007 %field into a hash table by field name until the empty line. (RFC7230 3)
1008
1009 reject_duplicate_content_length_header(Config) ->
1010 doc("Requests with duplicate content-length headers must be rejected "
1011 "with a 400 status code and the closing of the connection. (RFC7230 3.3.2)"),
1012 #{code := 400, client := Client} = do_raw(Config, [
1013 "POST / HTTP/1.1\r\n"
1014 "Host: localhost\r\n"
1015 "Content-length: 12\r\n"
1016 "Content-length: 12\r\n"
1017 "\r\n"
1018 "Hello world!"]),
1019 {error, closed} = raw_recv(Client, 0, 1000).
1020
1021 reject_duplicate_host_header(Config) ->
1022 doc("Requests with duplicate host headers must be rejected "
1023 "with a 400 status code and the closing of the connection. (RFC7230 3.3.2)"),
1024 #{code := 400, client := Client} = do_raw(Config, [
1025 "POST / HTTP/1.1\r\n"
1026 "Host: localhost\r\n"
1027 "Host: localhost\r\n"
1028 "\r\n"
1029 "Hello world!"]),
1030 {error, closed} = raw_recv(Client, 0, 1000).
1031
1032 combine_duplicate_headers(Config) ->
1033 doc("Other duplicate header fields must be combined by inserting a comma "
1034 "between the values in the order they were received. (RFC7230 3.2.2)"),
1035 #{code := 200, body := Body} = do_raw(Config, [
1036 "GET /echo/headers HTTP/1.1\r\n"
1037 "Host: localhost\r\n"
1038 "Accept-encoding: gzip\r\n"
1039 "Accept-encoding: brotli\r\n"
1040 "\r\n"]),
1041 <<"#{<<\"accept-encoding\">> => <<\"gzip, brotli\">>,", _/bits>> = Body,
1042 ok.
1043
1044 %Duplicate header field names are only allowed when their value is
1045 %a comma-separated list. In practice there is no need to perform
1046 %a check while reading the headers as the value will become invalid
1047 %and the error can be handled while parsing the header later on. (RFC7230 3.2.2)
1048 %
1049 %wait_for_eoh_before_processing_request(Config) ->
1050 %The request must not be processed until all headers have arrived. (RFC7230 3.2.2)
1051
1052 limit_headers(Config) ->
1053 doc("The number of headers allowed in a request must be subject to "
1054 "a configurable limit. There is no recommendations for the default. "
1055 "100 headers is known to work well. Such a request must be rejected "
1056 "with a 431 status code and the closing of the connection. (RFC7230 3.2.5, RFC6585 5)"),
1057 %% 100 headers.
1058 #{code := 200} = do_raw(Config, [
1059 "GET / HTTP/1.1\r\n"
1060 "Host: localhost\r\n",
1061 [["H-", integer_to_list(N), ": value\r\n"] || N <- lists:seq(1, 99)],
1062 "\r\n"]),
1063 %% 101 headers.
1064 #{code := 431, client := Client} = do_raw(Config, [
1065 "GET / HTTP/1.1\r\n"
1066 "Host: localhost\r\n",
1067 [["H-", integer_to_list(N), ": value\r\n"] || N <- lists:seq(1, 100)],
1068 "\r\n"]),
1069 {error, closed} = raw_recv(Client, 0, 1000),
1070 %% 100 header lines with duplicates.
1071 #{code := 200} = do_raw(Config, [
1072 "GET / HTTP/1.1\r\n"
1073 "Host: localhost\r\n",
1074 ["X: a\r\n" || _ <- lists:seq(1, 99)],
1075 "\r\n"]),
1076 %% 101 header lines with duplicates.
1077 #{code := 431, client := Client2} = do_raw(Config, [
1078 "GET / HTTP/1.1\r\n"
1079 "Host: localhost\r\n",
1080 ["X: a\r\n" || _ <- lists:seq(1, 100)],
1081 "\r\n"]),
1082 {error, closed} = raw_recv(Client2, 0, 1000),
1083 %% 101 header lines with duplicate cookie header.
1084 #{code := 431, client := Client3} = do_raw(Config, [
1085 "GET / HTTP/1.1\r\n"
1086 "Host: localhost\r\n",
1087 ["Cookie: a=b\r\n" || _ <- lists:seq(1, 100)],
1088 "\r\n"]),
1089 {error, closed} = raw_recv(Client3, 0, 1000).
1090
1091 %ignore_header_empty_list_elements(Config) ->
1092 %When parsing header field values, the server must ignore empty
1093 %list elements, and not count those as the count of elements present. (RFC7230 7)
1094 %
1095 %@todo
1096 %The information in the via header is largely unreliable. (RFC7230 5.7.1)
1097
1098 %% Request body.
1099
1100 %@todo
1101 %The message body is the octets after decoding any transfer
1102 %codings. (RFC7230 3.3)
1103
1104 no_request_body(Config) ->
1105 doc("A request has a message body only if it includes a transfer-encoding "
1106 "header or a non-zero content-length header. (RFC7230 3.3)"),
1107 #{code := 200, body := <<"false">>} = do_raw(Config, [
1108 "POST /echo/has_body HTTP/1.1\r\n"
1109 "Host: localhost\r\n"
1110 "\r\n"]),
1111 #{code := 200, body := <<>>} = do_raw(Config, [
1112 "POST /echo/read_body HTTP/1.1\r\n"
1113 "Host: localhost\r\n"
1114 "\r\n"]),
1115 ok.
1116
1117 no_request_body_content_length_zero(Config) ->
1118 doc("A request has a message body only if it includes a transfer-encoding "
1119 "header or a non-zero content-length header. (RFC7230 3.3)"),
1120 #{code := 200, body := <<"false">>} = do_raw(Config, [
1121 "POST /echo/has_body HTTP/1.1\r\n"
1122 "Host: localhost\r\n"
1123 "Content-length: 0\r\n"
1124 "\r\n"]),
1125 #{code := 200, body := <<>>} = do_raw(Config, [
1126 "POST /echo/read_body HTTP/1.1\r\n"
1127 "Host: localhost\r\n"
1128 "Content-length: 0\r\n"
1129 "\r\n"]),
1130 ok.
1131
1132 request_body_content_length(Config) ->
1133 doc("A request has a message body only if it includes a transfer-encoding "
1134 "header or a non-zero content-length header. (RFC7230 3.3)"),
1135 #{code := 200, body := <<"true">>} = do_raw(Config, [
1136 "POST /echo/has_body HTTP/1.1\r\n"
1137 "Host: localhost\r\n"
1138 "Content-length: 12\r\n"
1139 "\r\n"
1140 "Hello world!"]),
1141 #{code := 200, body := <<"Hello world!">>} = do_raw(Config, [
1142 "POST /echo/read_body HTTP/1.1\r\n"
1143 "Host: localhost\r\n"
1144 "Content-length: 12\r\n"
1145 "\r\n"
1146 "Hello world!"]),
1147 ok.
1148
1149 request_body_transfer_encoding(Config) ->
1150 doc("A request has a message body only if it includes a transfer-encoding "
1151 "header or a non-zero content-length header. (RFC7230 3.3)"),
1152 #{code := 200, body := <<"true">>} = do_raw(Config, [
1153 "POST /echo/has_body HTTP/1.1\r\n"
1154 "Host: localhost\r\n"
1155 "Transfer-encoding: chunked\r\n"
1156 "\r\n"
1157 "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
1158 #{code := 200, body := <<"Hello world!">>} = do_raw(Config, [
1159 "POST /echo/read_body HTTP/1.1\r\n"
1160 "Host: localhost\r\n"
1161 "Transfer-encoding: chunked\r\n"
1162 "\r\n"
1163 "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
1164 ok.
1165
1166 %```
1167 %Transfer-Encoding = 1#transfer-coding
1168 %
1169 %transfer-coding = "chunked" / "compress" / "deflate" / "gzip" / transfer-extension
1170 %transfer-extension = token *( OWS ";" OWS transfer-parameter )
1171 %transfer-parameter = token BWS "=" BWS ( token / quoted-string )
1172 %```
1173
1174 case_insensitive_transfer_encoding(Config) ->
1175 doc("The transfer-coding is case insensitive. (RFC7230 4)"),
1176 #{code := 200, body := <<"Hello world!">>} = do_raw(Config, [
1177 "POST /echo/read_body HTTP/1.1\r\n"
1178 "Host: localhost\r\n"
1179 "Transfer-encoding: ChUnKeD\r\n"
1180 "\r\n"
1181 "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
1182 ok.
1183
1184 %@todo
1185 %There are no known other transfer-extension with the exception of
1186 %deprecated aliases "x-compress" and "x-gzip". (IANA HTTP Transfer Coding Registry,
1187 %RFC7230 4.2.1, RFC7230 4.2.3, RFC7230 8.4.2)
1188
1189 %% This is the exact same test as request_body_transfer_encoding.
1190 must_understand_chunked(Config) ->
1191 doc("A server must be able to handle at least chunked transfer-encoding. "
1192 "This is also the only coding that sees widespread use. (RFC7230 3.3.1, RFC7230 4.1)"),
1193 #{code := 200, body := <<"Hello world!">>} = do_raw(Config, [
1194 "POST /echo/read_body HTTP/1.1\r\n"
1195 "Host: localhost\r\n"
1196 "Transfer-encoding: chunked\r\n"
1197 "\r\n"
1198 "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
1199 ok.
1200
1201 reject_double_chunked_encoding(Config) ->
1202 doc("Messages encoded more than once with chunked transfer-encoding "
1203 "must be rejected with a 400 status code and the closing of the "
1204 "connection. (RFC7230 3.3.1)"),
1205 #{code := 400, client := Client} = do_raw(Config, [
1206 "POST / HTTP/1.1\r\n"
1207 "Host: localhost\r\n"
1208 "Transfer-encoding: chunked, chunked\r\n"
1209 "\r\n"
1210 "20\r\n6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n\r\n0\r\n\r\n"]),
1211 {error, closed} = raw_recv(Client, 0, 1000).
1212
1213 reject_non_terminal_chunked(Config) ->
1214 doc("Messages where chunked, when present, is not the last "
1215 "transfer-encoding must be rejected with a 400 status code "
1216 "and the closing of the connection. (RFC7230 3.3.3)"),
1217 #{code := 400, client := Client1} = do_raw(Config, [
1218 "POST / HTTP/1.1\r\n"
1219 "Host: localhost\r\n"
1220 "Transfer-encoding: chunked, gzip\r\n"
1221 "\r\n",
1222 zlib:gzip(<<"6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n">>)]),
1223 {error, closed} = raw_recv(Client1, 0, 1000),
1224 #{code := 400, client := Client2} = do_raw(Config, [
1225 "POST / HTTP/1.1\r\n"
1226 "Host: localhost\r\n"
1227 "Transfer-encoding: chunked\r\n"
1228 "Transfer-encoding: gzip\r\n"
1229 "\r\n",
1230 zlib:gzip(<<"6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n">>)]),
1231 {error, closed} = raw_recv(Client2, 0, 1000).
1232
1233 %@todo
1234 %Some non-conformant implementations send the "deflate" compressed
1235 %data without the zlib wrapper. (RFC7230 4.2.2)
1236
1237 reject_unknown_transfer_encoding(Config) ->
1238 doc("Messages encoded with a transfer-encoding the server does not "
1239 "understand must be rejected with a 501 status code and the "
1240 "closing of the connection. (RFC7230 3.3.1)"),
1241 #{code := 400, client := Client1} = do_raw(Config, [
1242 "POST / HTTP/1.1\r\n"
1243 "Host: localhost\r\n"
1244 "Transfer-encoding: unknown, chunked\r\n"
1245 "\r\n",
1246 "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
1247 {error, closed} = raw_recv(Client1, 0, 1000),
1248 #{code := 400, client := Client2} = do_raw(Config, [
1249 "POST / HTTP/1.1\r\n"
1250 "Host: localhost\r\n"
1251 "Transfer-encoding: unknown\r\n"
1252 "Transfer-encoding: chunked\r\n"
1253 "\r\n",
1254 "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
1255 {error, closed} = raw_recv(Client2, 0, 1000).
1256
1257 %@todo
1258 %A server may reject requests with a body and no content-length
1259 %header with a 411 status code. (RFC7230 3.3.3)
1260
1261 %```
1262 %Content-Length = 1*DIGIT
1263 %```
1264
1265 reject_invalid_content_length(Config) ->
1266 doc("A request with an invalid content-length header must be rejected "
1267 "with a 400 status code and the closing of the connection. (RFC7230 3.3.3)"),
1268 #{code := 400, client := Client1} = do_raw(Config, [
1269 "POST / HTTP/1.1\r\n"
1270 "Host: localhost\r\n"
1271 "Content-length: 12,12\r\n"
1272 "\r\n"
1273 "Hello world!"]),
1274 {error, closed} = raw_recv(Client1, 0, 1000),
1275 #{code := 400, client := Client2} = do_raw(Config, [
1276 "POST / HTTP/1.1\r\n"
1277 "Host: localhost\r\n"
1278 "Content-length: NaN\r\n"
1279 "\r\n"
1280 "Hello world!"]),
1281 {error, closed} = raw_recv(Client2, 0, 1000).
1282
1283 %@todo
1284 %The content-length header ranges from 0 to infinity. Requests
1285 %with a message body too large must be rejected with a 413 status
1286 %code and the closing of the connection. (RFC7230 3.3.2)
1287
1288 reject_when_both_content_length_and_transfer_encoding(Config) ->
1289 doc("When a message includes both transfer-encoding and content-length "
1290 "headers, the message may be an attempt at request smuggling. It "
1291 "must be rejected with a 400 status code and the closing of the "
1292 "connection. (RFC7230 3.3.3)"),
1293 #{code := 400, client := Client} = do_raw(Config, [
1294 "POST /echo/read_body HTTP/1.1\r\n"
1295 "Host: localhost\r\n"
1296 "Transfer-encoding: chunked\r\n"
1297 "Content-length: 12\r\n"
1298 "\r\n"
1299 "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
1300 {error, closed} = raw_recv(Client, 0, 1000).
1301
1302 %socket_error_while_reading_body(Config) ->
1303 %If a socket error occurs while reading the body the server
1304 %must send a 400 status code response and close the connection. (RFC7230 3.3.3, RFC7230 3.4)
1305 %
1306 %timeout_while_reading_body(Config) ->
1307 %If a timeout occurs while reading the body the server must
1308 %send a 408 status code response and close the connection. (RFC7230 3.3.3, RFC7230 3.4)
1309
1310 %% Body length.
1311
1312 body_length_chunked_before(Config) ->
1313 doc("The length of a message with a transfer-encoding header can "
1314 "only be determined on decoding completion. (RFC7230 3.3.3)"),
1315 #{code := 200, body := <<"undefined">>} = do_raw(Config, [
1316 "POST /echo/body_length HTTP/1.1\r\n"
1317 "Host: localhost\r\n"
1318 "Transfer-encoding: chunked\r\n"
1319 "\r\n"
1320 "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
1321 ok.
1322
1323 body_length_chunked_after(Config) ->
1324 doc("Upon completion of chunk decoding the server must add a content-length "
1325 "header with the value set to the total length of data read. (RFC7230 4.1.3)"),
1326 #{code := 200, body := <<"12">>} = do_raw(Config, [
1327 "POST /length/echo/read_body HTTP/1.1\r\n"
1328 "Host: localhost\r\n"
1329 "Transfer-encoding: chunked\r\n"
1330 "\r\n"
1331 "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
1332 ok.
1333
1334 body_length_content_length(Config) ->
1335 doc("The length of a message with a content-length header is "
1336 "the numeric value in octets found in the header. (RFC7230 3.3.3)"),
1337 #{code := 200, body := <<"12">>} = do_raw(Config, [
1338 "POST /echo/body_length HTTP/1.1\r\n"
1339 "Host: localhost\r\n"
1340 "Content-length: 12\r\n"
1341 "\r\n"
1342 "Hello world!"]),
1343 ok.
1344
1345 body_length_zero(Config) ->
1346 doc("A message with no transfer-encoding or content-length header "
1347 "has a body length of 0. (RFC7230 3.3.3)"),
1348 #{code := 200, body := <<"0">>} = do_raw(Config, [
1349 "POST /echo/body_length HTTP/1.1\r\n"
1350 "Host: localhost\r\n"
1351 "\r\n"]),
1352 ok.
1353
1354 %% Chunked transfer-encoding.
1355
1356 reject_invalid_chunk_size(Config) ->
1357 doc("A request with an invalid chunk size must be rejected "
1358 "with a 400 status code and the closing of the connection. (RFC7230 4.1)"),
1359 #{code := 400, client := Client} = do_raw(Config, [
1360 "POST /echo/read_body HTTP/1.1\r\n"
1361 "Host: localhost\r\n"
1362 "Transfer-encoding: chunked\r\n"
1363 "\r\n"
1364 "6\r\nHello \r\nFIVE\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
1365 {error, closed} = raw_recv(Client, 0, 1000).
1366
1367 %```
1368 %chunked-body = *chunk last-chunk trailer-part CRLF
1369 %
1370 %chunk = chunk-size [ chunk-ext ] CRLF chunk-data CRLF
1371 %chunk-size = 1*HEXDIG
1372 %chunk-data = 1*OCTET ; a sequence of chunk-size octets
1373 %
1374 %last-chunk = 1*("0") [ chunk-ext ] CRLF
1375 %```
1376 %
1377 %The chunk-size field is a string of hex digits indicating the size of
1378 %the chunk-data in octets.
1379 %
1380 %```
1381 %chunk-ext = *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
1382 %chunk-ext-name = token
1383 %chunk-ext-val = token / quoted-string
1384 %```
1385
1386 ignore_unknown_chunk_extensions(Config) ->
1387 doc("Unknown chunk extensions must be ignored. (RFC7230 4.1.1)"),
1388 #{code := 200, body := <<"Hello world!">>} = do_raw(Config, [
1389 "POST /echo/read_body HTTP/1.1\r\n"
1390 "Host: localhost\r\n"
1391 "Transfer-encoding: chunked\r\n"
1392 "\r\n"
1393 "6; hello=\"cool world\"\r\nHello \r\n"
1394 "5 ; one ; two ; three;four;five\r\nworld"
1395 "\r\n1;ok\r\n!\r\n0\r\n\r\n"]),
1396 ok.
1397
1398 %% Since we skip everything right now, the only reason
1399 %% we might reject chunk extensions is if they are too large.
1400 limit_chunk_size_line(Config) ->
1401 doc("A request with chunk extensions larger than the server allows must be rejected "
1402 "with a 400 status code and the closing of the connection. (RFC7230 4.1.1)"),
1403 #{code := 200, body := <<"Hello world!">>} = do_raw(Config, [
1404 "POST /echo/read_body HTTP/1.1\r\n"
1405 "Host: localhost\r\n"
1406 "Transfer-encoding: chunked\r\n"
1407 "\r\n"
1408 "6; hello=\"cool world\"\r\nHello \r\n"
1409 "5;", lists:duplicate(128, $a), "\r\nworld"
1410 "\r\n1;ok\r\n!\r\n0\r\n\r\n"]),
1411 #{code := 400, client := Client} = do_raw(Config, [
1412 "POST /echo/read_body HTTP/1.1\r\n"
1413 "Host: localhost\r\n"
1414 "Transfer-encoding: chunked\r\n"
1415 "\r\n"
1416 "6; hello=\"cool world\"\r\nHello \r\n"
1417 "5;", lists:duplicate(129, $a), "\r\nworld"
1418 "\r\n1;ok\r\n!\r\n0\r\n\r\n"]),
1419 {error, closed} = raw_recv(Client, 0, 1000).
1420
1421 reject_invalid_chunk_size_crlf(Config) ->
1422 doc("A request with an invalid line break after the chunk size must be rejected "
1423 "with a 400 status code and the closing of the connection. (RFC7230 4.1)"),
1424 #{code := 400, client := Client1} = do_raw(Config, [
1425 "POST /echo/read_body HTTP/1.1\r\n"
1426 "Host: localhost\r\n"
1427 "Transfer-encoding: chunked\r\n"
1428 "\r\n"
1429 "6\rHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
1430 {error, closed} = raw_recv(Client1, 0, 1000),
1431 #{code := 400, client := Client2} = do_raw(Config, [
1432 "POST /echo/read_body HTTP/1.1\r\n"
1433 "Host: localhost\r\n"
1434 "Transfer-encoding: chunked\r\n"
1435 "\r\n"
1436 "6\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
1437 {error, closed} = raw_recv(Client2, 0, 1000),
1438 #{code := 400, client := Client3} = do_raw(Config, [
1439 "POST /echo/read_body HTTP/1.1\r\n"
1440 "Host: localhost\r\n"
1441 "Transfer-encoding: chunked\r\n"
1442 "\r\n"
1443 "6Hello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
1444 {error, closed} = raw_recv(Client3, 0, 1000).
1445
1446 reject_invalid_chunk_ext_crlf(Config) ->
1447 doc("A request with an invalid line break after chunk extensions must be rejected "
1448 "with a 400 status code and the closing of the connection. (RFC7230 4.1)"),
1449 #{code := 400, client := Client1} = do_raw(Config, [
1450 "POST /echo/read_body HTTP/1.1\r\n"
1451 "Host: localhost\r\n"
1452 "Transfer-encoding: chunked\r\n"
1453 "\r\n"
1454 "6; extensions\rHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
1455 {error, closed} = raw_recv(Client1, 0, 1000),
1456 #{code := 400, client := Client2} = do_raw(Config, [
1457 "POST /echo/read_body HTTP/1.1\r\n"
1458 "Host: localhost\r\n"
1459 "Transfer-encoding: chunked\r\n"
1460 "\r\n"
1461 "6; extensions\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
1462 {error, closed} = raw_recv(Client2, 0, 1000),
1463 #{code := 400, client := Client3} = do_raw(Config, [
1464 "POST /echo/read_body HTTP/1.1\r\n"
1465 "Host: localhost\r\n"
1466 "Transfer-encoding: chunked\r\n"
1467 "\r\n"
1468 "6; extensionsHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
1469 {error, closed} = raw_recv(Client3, 0, 1000).
1470
1471 reject_invalid_chunk_data_crlf(Config) ->
1472 doc("A request with an invalid line break after the chunk data must be rejected "
1473 "with a 400 status code and the closing of the connection. (RFC7230 4.1)"),
1474 #{code := 400, client := Client1} = do_raw(Config, [
1475 "POST /echo/read_body HTTP/1.1\r\n"
1476 "Host: localhost\r\n"
1477 "Transfer-encoding: chunked\r\n"
1478 "\r\n"
1479 "6\r\nHello \r5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
1480 {error, closed} = raw_recv(Client1, 0, 1000),
1481 #{code := 400, client := Client2} = do_raw(Config, [
1482 "POST /echo/read_body HTTP/1.1\r\n"
1483 "Host: localhost\r\n"
1484 "Transfer-encoding: chunked\r\n"
1485 "\r\n"
1486 "6\r\nHello \n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
1487 {error, closed} = raw_recv(Client2, 0, 1000),
1488 #{code := 400, client := Client3} = do_raw(Config, [
1489 "POST /echo/read_body HTTP/1.1\r\n"
1490 "Host: localhost\r\n"
1491 "Transfer-encoding: chunked\r\n"
1492 "\r\n"
1493 "6\r\nHello 5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
1494 {error, closed} = raw_recv(Client3, 0, 1000).
1495
1496 %```
1497 %trailer-part = *( header-field CRLF )
1498 %```
1499 %
1500 %%% @todo see headers above and reject the same way, space etc.
1501 %reject_invalid_request_trailer(Config) ->
1502 %
1503 %ignore_request_trailer_transfer_encoding(Config) ->
1504 %ignore_request_trailer_content_length(Config) ->
1505 %ignore_request_trailer_host(Config) ->
1506 %ignore_request_trailer_cache_control(Config) ->
1507 %ignore_request_trailer_expect(Config) ->
1508 %ignore_request_trailer_max_forwards(Config) ->
1509 %ignore_request_trailer_pragma(Config) ->
1510 %ignore_request_trailer_range(Config) ->
1511 %ignore_request_trailer_te(Config) ->
1512 %ignore_request_trailer_if_match(Config) ->
1513 %ignore_request_trailer_if_none_match(Config) ->
1514 %ignore_request_trailer_if_modified_since(Config) ->
1515 %ignore_request_trailer_if_unmodified_since(Config) ->
1516 %ignore_request_trailer_if_range(Config) ->
1517 %ignore_request_trailer_www_authenticate(Config) ->
1518 %ignore_request_trailer_authorization(Config) ->
1519 %ignore_request_trailer_proxy_authenticate(Config) ->
1520 %ignore_request_trailer_proxy_authorization(Config) ->
1521 %ignore_request_trailer_content_encoding(Config) ->
1522 %ignore_request_trailer_content_type(Config) ->
1523 %ignore_request_trailer_content_range(Config) ->
1524 %ignore_request_trailer_trailer(Config) ->
1525 %
1526 %ignore_response_trailer_header(Config, Header) ->
1527 %Trailing headers must not include transfer-encoding, content-length,
1528 %host, cache-control, expect, max-forwards, pragma, range, te,
1529 %if-match, if-none-match, if-modified-since, if-unmodified-since,
1530 %if-range, www-authenticate, authorization, proxy-authenticate,
1531 %proxy-authorization, age, cache-control, expires, date, location,
1532 %retry-after, vary, warning, content-encoding, content-type,
1533 %content-range, or trailer. (RFC7230 4.1.2)
1534 %
1535 %When trailer headers are processed, invalid headers must be ignored.
1536 %Valid headers must be added to the list of headers of the request. (RFC7230 4.1.2)
1537 %
1538 %ignore_request_trailers(Config) ->
1539 %Trailer headers can be ignored safely. (RFC7230 4.1.2)
1540 %
1541 %limit_request_trailer_headers(Config) ->
1542 %The number of trailer headers must be subject to configuration.
1543 %There is no known recommendations for the default. A value of 10
1544 %should cover most cases. Requests with too many trailer headers
1545 %must be rejected with a 431 status code and the closing of the
1546 %connection. (RFC6585 5)
1547
1548 %% We remove the header immediately so there's no need
1549 %% to try to read the body before checking.
1550 remove_transfer_encoding_chunked_after_body_read(Config) ->
1551 doc("Upon completion of chunk decoding the server must remove \"chunked\" "
1552 "from the transfer-encoding header. This header must be removed if "
1553 "it becomes empty following this removal. (RFC7230 4.1.3)"),
1554 #{code := 200, body := <<"undefined">>} = do_raw(Config, [
1555 "POST /echo/header/transfer-encoding HTTP/1.1\r\n"
1556 "Host: localhost\r\n"
1557 "Transfer-encoding: chunked\r\n"
1558 "\r\n"
1559 "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
1560 ok.
1561
1562 %remove_trailer_after_body_read(Config) ->
1563 %Upon completion of chunk decoding the server must remove the trailer
1564 %header from the list of headers. (RFC7230 4.1.3)
1565 %
1566 %```
1567 %Trailer = 1#field-name
1568 %```
1569 %
1570 %ignore_chunked_headers_not_in_trailer(Config) ->
1571 %The trailer header can be used to list the headers found in the
1572 %trailer. A server must have the option of ignoring trailer headers
1573 %that were not listed in the trailer header. (RFC7230 4.4)
1574 %
1575 %ignore_chunked_headers_if_trailer_not_in_connection(Config) ->
1576 %The trailer header must be listed in the connection header field.
1577 %Trailers must be ignored otherwise.
1578
1579 %% @todo Enable when request trailers are supported.
1580 %reject_invalid_trailer_name(Config) ->
1581 % doc("An invalid trailer field name must be rejected with a 400 status code "
1582 % "and the closing of the connection. (RFC9110 5.5)"),
1583 % #{code := 400, client := Client} = do_raw(Config,
1584 % "POST /echo/read_body HTTP/1.1\r\n"
1585 % "Host: localhost\r\n"
1586 % "Transfer-Encoding: chunked\r\n"
1587 % "Trailer: x-bad\r\n"
1588 % "\r\n"
1589 % "5\r\nHello\r\n"
1590 % "0\r\n"
1591 % "X\0-Bad: v\r\n"
1592 % "\r\n"),
1593 % {error, closed} = raw_recv(Client, 0, 1000).
1594 %
1595 %reject_invalid_trailer_value(Config) ->
1596 % doc("An invalid trailer field value must be rejected with a 400 status code "
1597 % "and the closing of the connection. (RFC9110 5.5)"),
1598 % #{code := 400, client := Client} = do_raw(Config,
1599 % "POST /echo/read_body HTTP/1.1\r\n"
1600 % "Host: localhost\r\n"
1601 % "Transfer-Encoding: chunked\r\n"
1602 % "Trailer: x-bad\r\n"
1603 % "\r\n"
1604 % "5\r\nHello\r\n"
1605 % "0\r\n"
1606 % "X-Bad: val\0ue\r\n"
1607 % "\r\n"),
1608 % {error, closed} = raw_recv(Client, 0, 1000).
1609
1610 %%% @todo Though we need a compatibility mode as some clients don't send it...
1611 %reject_chunked_missing_end_crlf(Config) ->
1612 %@todo ending CRLF
1613
1614 %% Connection management.
1615
1616 %@todo can probably test using auth
1617 %Never assume any two requests on a single connection come
1618 %from the same user agent. (RFC7230 2.3)
1619 %
1620 %```
1621 %Connection = 1#token ; case-insensitive
1622 %```
1623 %
1624 %The connection token is either case insensitive "close", "keep-alive"
1625 %or a header field name.
1626 %
1627 %There are no corresponding "close" or "keep-alive" headers. (RFC7230 8.1, RFC7230 A.2)
1628 %
1629 %The connection header is valid only for the immediate connection,
1630 %alongside any header field it lists. (RFC7230 6.1)
1631 %
1632 %The server must determine if the connection is persistent for
1633 %every message received by looking at the connection header and
1634 %HTTP version. (RFC7230 6.3)
1635
1636 no_connection_header_keepalive(Config) ->
1637 doc("HTTP/1.1 requests with no \"close\" option "
1638 "indicate the connection will persist. (RFC7230 6.1, RFC7230 6.3)"),
1639 #{code := 200, headers := RespHeaders, client := Client} = do_raw(Config, [
1640 "GET / HTTP/1.1\r\n"
1641 "Host: localhost\r\n"
1642 "\r\n"]),
1643 false = lists:keyfind(<<"connection">>, 1, RespHeaders),
1644 {error, timeout} = raw_recv(Client, 0, 1000).
1645
1646 http10_connection_keepalive(Config) ->
1647 doc("HTTP/1.0 requests with the \"keep-alive\" option "
1648 "indicate the connection will persist. "
1649 "(RFC7230 6.1, RFC7230 6.3, RFC7230 A.1.2)"),
1650 #{code := 200, headers := RespHeaders, client := Client} = do_raw(Config, [
1651 "GET / HTTP/1.0\r\n"
1652 "Host: localhost\r\n"
1653 "Connection: keep-alive\r\n"
1654 "\r\n"]),
1655 {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, RespHeaders),
1656 {error, timeout} = raw_recv(Client, 0, 1000).
1657
1658 connection_close(Config) ->
1659 doc("HTTP/1.1 requests with the \"close\" option and HTTP/1.0 with no "
1660 "\"keep-alive\" option indicate the connection will be closed "
1661 "upon reception of the response by the client. (RFC7230 6.1, RFC7230 6.3)"),
1662 #{code := 200, headers := RespHeaders, client := Client} = do_raw(Config, [
1663 "GET / HTTP/1.1\r\n"
1664 "Host: localhost\r\n"
1665 "Connection: close\r\n"
1666 "\r\n"]),
1667 {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, RespHeaders),
1668 {error, closed} = raw_recv(Client, 0, 1000).
1669
1670 http10_no_connection_header_close(Config) ->
1671 doc("HTTP/1.0 with no \"keep-alive\" option indicate "
1672 "the connection will be closed upon reception of "
1673 "the response by the client. (RFC7230 6.1, RFC7230 6.3, RFC7230 A.1.2)"),
1674 #{code := 200, headers := RespHeaders, client := Client} = do_raw(Config, [
1675 "GET / HTTP/1.0\r\n"
1676 "Host: localhost\r\n"
1677 "\r\n"]),
1678 %% Cowboy always sends a close header back to HTTP/1.0 clients
1679 %% that support keep-alive, even though it is not required.
1680 {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, RespHeaders),
1681 {error, closed} = raw_recv(Client, 0, 1000).
1682
1683 connection_invalid(Config) ->
1684 doc("HTTP/1.1 requests with an invalid Connection header "
1685 "must be rejected with a 400 status code and the closing "
1686 "of the connection. (RFC7230 6.1)"),
1687 #{code := 400, client := Client} = do_raw(Config, [
1688 "GET / HTTP/1.1\r\n"
1689 "Host: localhost\r\n"
1690 "Connection: jndi{ldap127\r\n"
1691 "\r\n"]),
1692 {error, closed} = raw_recv(Client, 0, 1000).
1693
1694 http10_connection_invalid(Config) ->
1695 doc("HTTP/1.0 requests with an invalid Connection header "
1696 "must be rejected with a 400 status code and the closing "
1697 "of the connection. (RFC7230 6.1)"),
1698 #{code := 400, client := Client} = do_raw(Config, [
1699 "GET / HTTP/1.0\r\n"
1700 "Host: localhost\r\n"
1701 "Connection: jndi{ldap127\r\n"
1702 "\r\n"]),
1703 {error, closed} = raw_recv(Client, 0, 1000).
1704
1705 limit_requests_keepalive(Config) ->
1706 doc("The maximum number of requests sent using a persistent connection "
1707 "must be subject to configuration. The connection must be closed "
1708 "when the limit is reached. (RFC7230 6.3)"),
1709 ConnPid = gun_open(Config),
1710 _ = [begin
1711 Ref = gun:get(ConnPid, "/"),
1712 {response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref),
1713 {ok, <<"Hello world!">>} = gun:await_body(ConnPid, Ref),
1714 false = lists:keyfind(<<"connection">>, 1, RespHeaders)
1715 end || _ <- lists:seq(1,99)],
1716 %% Final request closes the connection.
1717 Ref = gun:get(ConnPid, "/"),
1718 {response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref),
1719 {ok, <<"Hello world!">>} = gun:await_body(ConnPid, Ref),
1720 {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, RespHeaders),
1721 gun_down(ConnPid).
1722
1723 accept_at_least_1_empty_line_keepalive(Config) ->
1724 doc("A configurable number of empty lines (CRLF) preceding the request "
1725 "must be ignored. At least 1 empty line must be ignored. (RFC7230 3.5)"),
1726 #{code := 200, client := Client} = do_raw(Config,
1727 "GET / HTTP/1.1\r\n"
1728 "Host: localhost\r\n"
1729 "\r\n"
1730 %% We send an extra CRLF that must be ignored.
1731 "\r\n"),
1732 ok = raw_send(Client,
1733 "GET / HTTP/1.1\r\n"
1734 "Host: localhost\r\n"
1735 "\r\n"),
1736 {'HTTP/1.1', 200, _, _} = cow_http:parse_status_line(raw_recv_head(Client)),
1737 ok.
1738
1739 reject_lf_line_breaks_empty_line(Config) ->
1740 #{code := 200, client := Client} = do_raw(Config,
1741 "GET / HTTP/1.1\r\n"
1742 "Host: localhost\r\n"
1743 "\r\n"),
1744 %% We send an extra LF that must be rejected.
1745 ok = raw_send(Client, "\n"),
1746 {_, 400, _, _} = cow_http:parse_status_line(raw_recv_head(Client)),
1747 {error, closed} = raw_recv(Client, 0, 1000).
1748
1749 %skip_request_body_by_closing_connection(Config) ->
1750 %%A server that doesn't want to read the entire body of a message
1751 %%must close the connection, if possible after sending the "close"
1752 %%connection option in the response. (RFC7230 6.3)
1753
1754 pipeline(Config) ->
1755 doc("A server can receive more than one request before any response "
1756 "is sent. This is called pipelining. Responses must be sent "
1757 "in the same order as the requests. (RFC7230 6.3.2)"),
1758 ConnPid = gun_open(Config),
1759 Refs = [{
1760 gun:get(ConnPid, "/"),
1761 gun:post(ConnPid, "/full/read_body", [], <<0:80000>>)
1762 } || _ <- lists:seq(1, 25)],
1763 _ = [begin
1764 {response, nofin, 200, _} = gun:await(ConnPid, Ref1, infinity),
1765 {ok, <<"Hello world!">>} = gun:await_body(ConnPid, Ref1, infinity),
1766 {response, nofin, 200, _} = gun:await(ConnPid, Ref2, infinity),
1767 {ok, <<0:80000>>} = gun:await_body(ConnPid, Ref2, infinity)
1768 end || {Ref1, Ref2} <- Refs],
1769 ok.
1770
1771 %% @todo pipeline_parallel (safe methods can, others can't)
1772 %The requests can be processed in parallel if they all have safe methods.
1773
1774 %@todo
1775 %A server that does parallel pipelining must send responses in the
1776 %same order as the requests came in. (RFC7230 5.6)
1777
1778 %@todo
1779 %The server must reject abusive traffic by closing the connection.
1780 %Abusive traffic can come from the form of too many requests in a
1781 %given amount of time, or too many concurrent connections. Limits
1782 %must be subject to configuration. (RFC7230 6.4)
1783
1784 close_inactive_connections(Config) ->
1785 doc("The server must close inactive connections. The timeout "
1786 "must be subject to configuration. (RFC7230 6.5)"),
1787 Client = raw_open(Config),
1788 {error, closed} = raw_recv(Client, 0, 6000).
1789
1790 %@todo
1791 %The server must monitor connections for the close signal
1792 %and close the socket on its end accordingly. (RFC7230 6.5)
1793 %
1794 %@todo
1795 %A connection close may occur at any time. (RFC7230 6.5)
1796
1797 ignore_requests_after_request_connection_close(Config) ->
1798 doc("The server must not process any request after "
1799 "receiving the \"close\" connection option. (RFC7230 6.6)"),
1800 Self = self(),
1801 #{code := 200, client := Client} = do_raw(Config, [
1802 "GET / HTTP/1.1\r\n"
1803 "Host: localhost\r\n"
1804 "Connection: close\r\n"
1805 "\r\n"
1806 "GET /send_message HTTP/1.1\r\n"
1807 "Host: localhost\r\n"
1808 "x-test-pid: ", pid_to_list(Self), "\r\n"
1809 "\r\n"]),
1810 {error, closed} = raw_recv(Client, 0, 1000),
1811 %% We receive a message if the second request is wrongly processed.
1812 receive
1813 {Self, _, init, Req, Opts} ->
1814 error({init, Req, Opts})
1815 after 1000 ->
1816 ok
1817 end.
1818
1819 ignore_requests_after_response_connection_close(Config) ->
1820 doc("The server must not process any request after "
1821 "sending the \"close\" connection option. (RFC7230 6.6)"),
1822 Self = self(),
1823 Client = raw_open(Config),
1824 ok = raw_send(Client, [
1825 [
1826 "GET / HTTP/1.1\r\n"
1827 "Host: localhost\r\n"
1828 "\r\n"
1829 || _ <- lists:seq(1, 100)],
1830 "GET /send_message HTTP/1.1\r\n"
1831 "Host: localhost\r\n"
1832 "x-test-pid: ", pid_to_list(Self), "\r\n"
1833 "\r\n"]),
1834 %% We have a separate test for the connection close so we don't
1835 %% double check the connection gets closed here. We only need to
1836 %% know whether the 101st request was wrongly processed.
1837 receive
1838 {Self, _, init, Req, Opts} ->
1839 error({init, Req, Opts})
1840 after 1000 ->
1841 ok
1842 end.
1843
1844 %@todo
1845 %The server must close the connection in stages to avoid the
1846 %TCP reset problem. The server starts by closing the write
1847 %side of the socket. The server then reads until it detects
1848 %the socket has been closed, until it can be certain its
1849 %last response has been received by the client, or until
1850 %a close or timeout occurs. The server then fully close the
1851 %connection. (6.6)
1852
1853 %% Routing.
1854
1855 %```
1856 %Host = authority ; same as authority-form
1857 %```
1858
1859 reject_missing_host(Config) ->
1860 doc("An HTTP/1.1 request that lacks a host header must be rejected with "
1861 "a 400 status code and the closing of the connection. (RFC7230 5.4)"),
1862 #{code := 400, client := Client} = do_raw(Config, [
1863 "GET / HTTP/1.1\r\n"
1864 "\r\n"]),
1865 {error, closed} = raw_recv(Client, 0, 1000).
1866
1867 http10_allow_missing_host(Config0) ->
1868 doc("An HTTP/1.0 request that lacks a host header may be accepted. "
1869 "(RFC7230 5.4, RFC7230 5.5, RFC7230 A.1.1)"),
1870 Routes = [{'_', [{"/echo/:key[/:arg]", echo_h, []}]}],
1871 Config = cowboy_test:init_http(?FUNCTION_NAME, #{
1872 env => #{dispatch => cowboy_router:compile(Routes)}
1873 }, Config0),
1874 try
1875 #{code := 200, body := <<>>} = do_raw(Config, [
1876 "GET /echo/host HTTP/1.0\r\n"
1877 "\r\n"])
1878 after
1879 cowboy:stop_listener(?FUNCTION_NAME)
1880 end.
1881
1882 reject_invalid_host(Config) ->
1883 doc("A request with an invalid host header must be rejected with a "
1884 "400 status code and the closing of the connection. (RFC7230 5.4)"),
1885 #{code := 400, client := Client} = do_raw(Config, [
1886 "GET / HTTP/1.1\r\n"
1887 "Host: localhost:port\r\n"
1888 "\r\n"]),
1889 {error, closed} = raw_recv(Client, 0, 1000).
1890
1891 reject_userinfo(Config) ->
1892 doc("An authority component with a userinfo component (and its "
1893 "\"@\" delimiter) is invalid. The request must be rejected with "
1894 "a 400 status code and the closing of the connection. (RFC7230 2.7.1)"),
1895 #{code := 400, client := Client} = do_raw(Config, [
1896 "GET / HTTP/1.1\r\n"
1897 "Host: user@localhost\r\n"
1898 "\r\n"]),
1899 {error, closed} = raw_recv(Client, 0, 1000).
1900
1901 reject_absolute_form_different_host(Config) ->
1902 doc("When using absolute-form the URI authority component must be "
1903 "identical to the host header. Invalid requests must be rejected "
1904 "with a 400 status code and the closing of the connection. (RFC7230 5.4)"),
1905 #{code := 400, client := Client} = do_raw(Config, [
1906 "GET http://example.org/ HTTP/1.1\r\n"
1907 "Host: localhost\r\n"
1908 "\r\n"]),
1909 {error, closed} = raw_recv(Client, 0, 1000).
1910
1911 %reject_authority_form_different_host(Config) ->
1912 %When using authority-form the URI authority component must be
1913 %identical to the host header. Invalid requests must be rejected
1914 %with a 400 status code and the closing of the connection.
1915
1916 empty_host(Config0) ->
1917 doc("The host header is empty when the authority component is undefined. (RFC7230 5.4)"),
1918 Routes = [{'_', [{"/echo/:key[/:arg]", echo_h, []}]}],
1919 Config = cowboy_test:init_http(?FUNCTION_NAME, #{
1920 env => #{dispatch => cowboy_router:compile(Routes)}
1921 }, Config0),
1922 try
1923 #{code := 200, body := <<>>} = do_raw(Config, [
1924 "GET /echo/host HTTP/1.1\r\n"
1925 "Host:\r\n"
1926 "\r\n"]),
1927 #{code := 200, body := <<>>} = do_raw(Config, [
1928 "GET /echo/host HTTP/1.1\r\n"
1929 "Host: \r\n"
1930 "\r\n"])
1931 after
1932 cowboy:stop_listener(?FUNCTION_NAME)
1933 end.
1934
1935 %% The effective request URI can be rebuilt by concatenating scheme,
1936 %% "://", authority, path and query components. (RFC7230 5.5)
1937 %%
1938 %% This is covered in req_SUITE in the tests for cowboy_req:uri/1,2.
1939
1940 reject_non_authoritative_host(Config) ->
1941 doc("A request with a host header for which the origin server is "
1942 "not authoritative must be rejected with a 400 status code. "
1943 "(RFC7230 5.5, RFC7230 9.1)"),
1944 #{code := 400} = do_raw(Config, [
1945 "GET / HTTP/1.1\r\n"
1946 "Host: ninenines.eu\r\n"
1947 "\r\n"]),
1948 ok.
1949
1950 %@todo
1951 %Resources with identical URI except for the scheme component
1952 %must be treated as different. (RFC7230 2.7.2)
1953
1954 %% Response.
1955
1956 %@todo
1957 %A server can send more than one response per request only when a
1958 %1xx response is sent preceding the final response. (RFC7230 5.6)
1959 %
1960 %```
1961 %HTTP-response = status-line *( header-field CRLF ) CRLF [ message-body ]
1962 %```
1963 %
1964 %@todo
1965 %The response format must be followed strictly.
1966 %
1967 %```
1968 %status-line = HTTP-version SP status-code SP reason-phrase CRLF
1969 %status-code = 3DIGIT
1970 %reason-phrase = *( HTAB / SP / VCHAR / obs-text )
1971 %```
1972
1973 http10_request_http11_response(Config) ->
1974 doc("A server must send its own HTTP version in responses. (RFC7230 2.6)"),
1975 #{code := 200, version := 'HTTP/1.1'} = do_raw(Config, [
1976 "GET / HTTP/1.0\r\n"
1977 "Host: localhost\r\n"
1978 "\r\n"]),
1979 ok.
1980
1981 %@todo
1982 %An HTTP/1.1 server may send an HTTP/1.0 version for compatibility purposes. (RFC7230 2.6)
1983 %
1984 %@todo
1985 %RFC6585 defines additional status code a server can use to reject
1986 %messages. (RFC7230 9.3, RFC6585)
1987
1988 %% Response headers.
1989
1990 %@todo
1991 %In responses, OWS must be generated as SP or not generated
1992 %at all. RWS must be generated as SP. BWS must not be
1993 %generated. (RFC7230 3.2.3)
1994 %
1995 %```
1996 %header-field = field-name ":" SP field-value
1997 %
1998 %field-name = token ; case-insensitive
1999 %field-value = *( SP / %21-7E / %80-FF )
2000 %```
2001 %
2002 %@todo
2003 %In quoted-string found in field-value, quoted-pair must only be
2004 %used for DQUOTE and backslash. (RFC7230 3.2.6)
2005 %
2006 %@todo
2007 %HTTP header values must use US-ASCII encoding and must only send
2008 %printable characters or SP. (RFC7230 3.2.4, RFC7230 9.4)
2009 %
2010 %@todo
2011 %The server must not generate empty list elements in headers. (RFC7230 7)
2012 %
2013 %@todo
2014 %When encoding an URI as part of a response, only characters that
2015 %are reserved need to be percent-encoded. (RFC7230 2.7.3)
2016
2017 special_set_cookie_handling(Config) ->
2018 doc("The set-cookie header must be handled as a special case. There "
2019 "must be exactly one set-cookie header field per cookie. (RFC7230 3.2.2)"),
2020 #{code := 200, headers := RespHeaders} = do_raw(Config, [
2021 "GET /resp/set_resp_cookie3/multiple HTTP/1.1\r\n"
2022 "Host: localhost\r\n"
2023 "\r\n"]),
2024 [_, _] = [H || H={<<"set-cookie">>, _} <- RespHeaders],
2025 ok.
2026
2027 %@todo
2028 %The server must list headers for or about the immediate connection
2029 %in the connection header field. (RFC7230 6.1)
2030 %
2031 %@todo
2032 %A server that does not support persistent connections must
2033 %send "close" in every non-1xx response. (RFC7230 6.1)
2034 %
2035 %no_close_in_100_response(Config) ->
2036 %no_close_in_101_response(Config) ->
2037 %no_close_in_102_response(Config) ->
2038 %A server must not send a "close" connection option
2039 %in 1xx responses. (RFC7230 6.1)
2040 %
2041 %@todo
2042 %The "close" connection must be sent in a message when the
2043 %sender knows it will close the connection after fully sending
2044 %the response. (RFC7230 6.6)
2045 %
2046 %@todo
2047 %A server must close the connection after sending or
2048 %receiving a "close" once the response has been sent. (RFC7230 6.6)
2049
2050 close_request_close_response(Config) ->
2051 doc("A server must send a \"close\" in a response to a request "
2052 "containing a \"close\". (RFC7230 6.6)"),
2053 #{code := 200, headers := RespHeaders} = do_raw(Config, [
2054 "GET / HTTP/1.1\r\n"
2055 "Host: localhost\r\n"
2056 "Connection: close\r\n"
2057 "\r\n"]),
2058 {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, RespHeaders),
2059 ok.
2060
2061 %% Response body.
2062
2063 no_body_in_head_response(Config) ->
2064 doc("Responses to HEAD requests never include a message body. (RFC7230 3.3)"),
2065 Client = raw_open(Config),
2066 ok = raw_send(Client, [
2067 "HEAD / HTTP/1.1\r\n"
2068 "Host: localhost\r\n"
2069 "\r\n"]),
2070 {_, 200, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),
2071 {Headers, <<>>} = cow_http:parse_headers(Rest),
2072 {_, LengthBin} = lists:keyfind(<<"content-length">>, 1, Headers),
2073 Length = binary_to_integer(LengthBin),
2074 {error, timeout} = raw_recv(Client, Length, 1000),
2075 ok.
2076
2077 %% @todo test different ways to send a body in response
2078
2079 %%% @todo Implement CONNECT
2080 %2xx responses to CONNECT requests never include a message
2081 %body. (RFC7230 3.3)
2082 %
2083 %no_body_in_100_response(Config) ->
2084 %no_body_in_101_response(Config) ->
2085 %no_body_in_102_response(Config) ->
2086 %1xx responses never include a message body. (RFC7230 3.3)
2087
2088 no_body_in_204_response(Config) ->
2089 doc("204 responses never include a message body. Cowboy produces "
2090 "a 500 error response when attempting to do so. (RFC7230 3.3)"),
2091 Client = raw_open(Config),
2092 ok = raw_send(Client, [
2093 "GET /resp/reply4/204body HTTP/1.1\r\n"
2094 "Host: localhost\r\n"
2095 "\r\n"]),
2096 {_, 500, _, _} = cow_http:parse_status_line(raw_recv_head(Client)),
2097 ok.
2098
2099 no_body_in_204_response_stream(Config) ->
2100 doc("204 responses never include a message body. Attempting to "
2101 "stream the body produces a crash on the server-side. (RFC7230 3.3)"),
2102 Client = raw_open(Config),
2103 ok = raw_send(Client, [
2104 "GET /resp/stream_reply2/204body HTTP/1.1\r\n"
2105 "Host: localhost\r\n"
2106 "\r\n"]),
2107 {_, 204, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),
2108 {_, <<>>} = cow_http:parse_headers(Rest),
2109 {error, timeout} = raw_recv(Client, 1, 1000),
2110 ok.
2111
2112 no_body_in_304_response(Config) ->
2113 doc("304 responses never include a message body. Cowboy produces "
2114 "a 500 error response when attempting to do so. (RFC7230 3.3)"),
2115 Client = raw_open(Config),
2116 ok = raw_send(Client, [
2117 "GET /resp/reply4/304body HTTP/1.1\r\n"
2118 "Host: localhost\r\n"
2119 "\r\n"]),
2120 {_, 500, _, _} = cow_http:parse_status_line(raw_recv_head(Client)),
2121 ok.
2122
2123 no_body_in_304_response_stream(Config) ->
2124 doc("304 responses never include a message body. Attempting to "
2125 "stream the body produces a crash on the server-side. (RFC7230 3.3)"),
2126 Client = raw_open(Config),
2127 ok = raw_send(Client, [
2128 "GET /resp/stream_reply2/304body HTTP/1.1\r\n"
2129 "Host: localhost\r\n"
2130 "\r\n"]),
2131 {_, 304, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),
2132 {_, <<>>} = cow_http:parse_headers(Rest),
2133 {error, timeout} = raw_recv(Client, 1, 1000),
2134 ok.
2135
2136 same_content_length_as_get_in_head_response(Config) ->
2137 doc("Responses to HEAD requests can include a content-length header. "
2138 "Its value must be the same as if the request was an unconditional "
2139 "GET. (RFC7230 3.3, RFC7230 3.3.1, RFC7230 3.3.2)"),
2140 Client = raw_open(Config),
2141 ok = raw_send(Client, [
2142 "HEAD / HTTP/1.1\r\n"
2143 "Host: localhost\r\n"
2144 "\r\n"]),
2145 {_, 200, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),
2146 {Headers, <<>>} = cow_http:parse_headers(Rest),
2147 {_, <<"12">>} = lists:keyfind(<<"content-length">>, 1, Headers),
2148 ok.
2149
2150 same_transfer_encoding_as_get_in_head_response(Config) ->
2151 doc("Responses to HEAD requests can include a transfer-encoding header. "
2152 "Its value must be the same as if the request was an unconditional "
2153 "GET. (RFC7230 3.3, RFC7230 3.3.1, RFC7230 3.3.2)"),
2154 Client = raw_open(Config),
2155 ok = raw_send(Client, [
2156 "HEAD /resp/stream_reply2/200 HTTP/1.1\r\n"
2157 "Host: localhost\r\n"
2158 "\r\n"]),
2159 {_, 200, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),
2160 {Headers, <<>>} = cow_http:parse_headers(Rest),
2161 {_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
2162 ok.
2163
2164 %same_content_length_as_200_in_304_response(Config) ->
2165 %same_transfer_encoding_as_200_in_304_response(Config) ->
2166 %304 responses can include a
2167 %content-length or transfer-encoding header. Their value must
2168 %be the same as if the request was an unconditional GET. (RFC7230 3.3, RFC7230 3.3.1, RFC7230 3.3.2)
2169 %
2170 %no_content_length_in_100_response(Config) ->
2171 %no_content_length_in_101_response(Config) ->
2172 %no_content_length_in_102_response(Config) ->
2173 %1xx, 204 responses and "2xx responses to CONNECT requests" must
2174 %not include a content-length or transfer-encoding header. (RFC7230 3.3.1, RFC7230 3.3.2)
2175
2176 no_content_length_in_204_response(Config) ->
2177 doc("204 responses must not include a content-length header. "
2178 "(RFC7230 3.3.1, RFC7230 3.3.2)"),
2179 Client = raw_open(Config),
2180 ok = raw_send(Client, [
2181 "GET /resp/reply3/204 HTTP/1.1\r\n"
2182 "Host: localhost\r\n"
2183 "\r\n"]),
2184 {_, 204, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),
2185 {Headers, <<>>} = cow_http:parse_headers(Rest),
2186 false = lists:keyfind(<<"content-length">>, 1, Headers),
2187 ok.
2188
2189 no_content_length_in_empty_304_response(Config) ->
2190 doc("304 responses should not include a content-length header, "
2191 "unless it matches the resource's and was therefore set "
2192 "explicitly by the user. (RFC7230 3.3.1, RFC7230 3.3.2)"),
2193 Client = raw_open(Config),
2194 ok = raw_send(Client, [
2195 "GET /resp/reply3/304 HTTP/1.1\r\n"
2196 "Host: localhost\r\n"
2197 "\r\n"]),
2198 {_, 304, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),
2199 {Headers, <<>>} = cow_http:parse_headers(Rest),
2200 false = lists:keyfind(<<"content-length">>, 1, Headers),
2201 ok.
2202
2203 %%% @todo CONNECT no_content_length_in_2xx_response_to_connect_request(Config) ->
2204 %no_transfer_encoding_in_100_response(Config) ->
2205 %no_transfer_encoding_in_101_response(Config) ->
2206 %no_transfer_encoding_in_102_response(Config) ->
2207 %1xx, 204 responses and "2xx responses to CONNECT requests" must
2208 %not include a content-length or transfer-encoding header. (RFC7230 3.3.1, RFC7230 3.3.2)
2209
2210 %% We only send transfer-encoding when streaming a response body.
2211 %% We therefore need a streamed response in order to see a potential bug.
2212 no_transfer_encoding_in_204_response(Config) ->
2213 doc("204 responses must not include a transfer-encoding header. "
2214 "(RFC7230 3.3.1, RFC7230 3.3.2)"),
2215 Client = raw_open(Config),
2216 ok = raw_send(Client, [
2217 "GET /resp/stream_reply2/204 HTTP/1.1\r\n"
2218 "Host: localhost\r\n"
2219 "\r\n"]),
2220 {_, 204, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),
2221 {Headers, <<>>} = cow_http:parse_headers(Rest),
2222 false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
2223 ok.
2224
2225 %%% @todo CONNECT no_transfer_encoding_in_2xx_response_to_connect_request(Config) ->
2226 %1xx, 204 responses and "2xx responses to CONNECT requests" must
2227 %not include a content-length or transfer-encoding header. (RFC7230 3.3.1, RFC7230 3.3.2)
2228 %
2229 %```
2230 %message-body = *OCTET
2231 %```
2232 %
2233 %The message body is the octets after decoding any transfer
2234 %codings. (RFC7230 3.3)
2235
2236 content_length_0_when_no_body(Config) ->
2237 doc("When the length is known in advance, the server must send a "
2238 "content-length header, including if the length is 0. (RFC7230 3.3.2, RFC7230 3.3.3)"),
2239 #{code := 200, headers := RespHeaders} = do_raw(Config, [
2240 "GET /resp/reply2/200 HTTP/1.1\r\n"
2241 "Host: localhost\r\n"
2242 "\r\n"]),
2243 {_, <<"0">>} = lists:keyfind(<<"content-length">>, 1, RespHeaders),
2244 ok.
2245
2246 content_length_response(Config) ->
2247 doc("When the length is known in advance, the server must send a "
2248 "content-length header. (RFC7230 3.3.2, RFC7230 3.3.3)"),
2249 #{code := 200, headers := RespHeaders} = do_raw(Config, [
2250 "GET / HTTP/1.1\r\n"
2251 "Host: localhost\r\n"
2252 "\r\n"]),
2253 {_, <<"12">>} = lists:keyfind(<<"content-length">>, 1, RespHeaders),
2254 ok.
2255
2256 chunked_response(Config) ->
2257 doc("When the length is not known in advance, the chunked transfer-encoding "
2258 "must be used. (RFC7230 3.3.2, RFC7230 3.3.3)"),
2259 #{code := 200, headers := RespHeaders} = do_raw(Config, [
2260 "GET /resp/stream_reply2/200 HTTP/1.1\r\n"
2261 "Host: localhost\r\n"
2262 "\r\n"]),
2263 {_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, RespHeaders),
2264 %% @todo We probably want to check the body received too.
2265 ok.
2266
2267 %compat_no_content_length_or_transfer_encoding_close_on_body_end(Config) ->
2268 %For compatibility purposes a server can send no content-length or
2269 %transfer-encoding header. In this case the connection must be
2270 %closed after the response has been sent fully. (RFC7230 3.3.2, RFC7230 3.3.3)
2271
2272 no_content_length_if_transfer_encoding(Config) ->
2273 doc("The content-length header must not be sent when a transfer-encoding "
2274 "header already exists. (RFC7230 3.3.2)"),
2275 #{code := 200, headers := RespHeaders} = do_raw(Config, [
2276 "GET /resp/stream_reply2/200 HTTP/1.1\r\n"
2277 "Host: localhost\r\n"
2278 "\r\n"]),
2279 false = lists:keyfind(<<"content-length">>, 1, RespHeaders),
2280 ok.
2281
2282 %@todo
2283 %The server must not apply the chunked transfer-encoding more than
2284 %once. (RFC7230 3.3.1)
2285 %
2286 %@todo
2287 %The server must apply the chunked transfer-encoding last. (RFC7230 3.3.1)
2288
2289 http10_request_no_transfer_encoding_in_response(Config) ->
2290 doc("The transfer-encoding header must not be sent in responses to "
2291 "HTTP/1.0 requests, or in responses that use the HTTP/1.0 version. "
2292 "No transfer codings must be applied in these cases. "
2293 "(RFC7230 3.3.1, RFC7230 A.1.3)"),
2294 Client = raw_open(Config),
2295 ok = raw_send(Client, [
2296 "GET /resp/stream_reply2/200 HTTP/1.0\r\n"
2297 "Host: localhost\r\n"
2298 "\r\n"]),
2299 {_, 200, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),
2300 {RespHeaders, Body0} = cow_http:parse_headers(Rest),
2301 false = lists:keyfind(<<"content-length">>, 1, RespHeaders),
2302 false = lists:keyfind(<<"transfer-encoding">>, 1, RespHeaders),
2303 Body = <<0:8000000>>,
2304 {ok, Body1} = raw_recv(Client, byte_size(Body) - byte_size(Body0), 5000),
2305 Body = << Body0/binary, Body1/binary >>,
2306 %% The end of body is indicated by a connection close.
2307 {error, closed} = raw_recv(Client, 0, 1000),
2308 ok.
2309
2310 no_te_no_trailers(Config) ->
2311 doc("Trailers can only be sent if the request includes a TE header "
2312 "containing \"trailers\". (RFC7230 4.1.2)"),
2313 #{code := 200, headers := RespHeaders} = do_raw(Config, [
2314 "GET /resp/stream_trailers HTTP/1.1\r\n"
2315 "Host: localhost\r\n"
2316 "\r\n"]),
2317 {_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, RespHeaders),
2318 false = lists:keyfind(<<"trailer">>, 1, RespHeaders),
2319 %% @todo We probably want to check the body received too.
2320 ok.
2321
2322 te_trailers(Config) ->
2323 doc("Trailers can only be sent if the request includes a TE header "
2324 "containing \"trailers\". (RFC7230 4.1.2)"),
2325 #{code := 200, headers := RespHeaders} = do_raw(Config, [
2326 "GET /resp/stream_trailers HTTP/1.1\r\n"
2327 "Host: localhost\r\n"
2328 "TE: trailers\r\n"
2329 "\r\n"]),
2330 {_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, RespHeaders),
2331 {_, <<"grpc-status">>} = lists:keyfind(<<"trailer">>, 1, RespHeaders),
2332 %% @todo We probably want to check the body received too.
2333 ok.
2334
2335 te_ignore_chunked(Config) ->
2336 doc("The presence of \"chunked\" in a TE header must be ignored as it "
2337 "is always acceptable with HTTP/1.1. (RFC7230 4.3)"),
2338 #{code := 200, headers := RespHeaders} = do_raw(Config, [
2339 "GET /resp/stream_reply2/200 HTTP/1.1\r\n"
2340 "Host: localhost\r\n"
2341 "TE: chunked\r\n"
2342 "\r\n"]),
2343 {_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, RespHeaders),
2344 %% @todo We probably want to check the body received too.
2345 ok.
2346
2347 te_ignore_chunked_0(Config) ->
2348 doc("The presence of \"chunked\" in a TE header must be ignored as it "
2349 "is always acceptable with HTTP/1.1. (RFC7230 4.3)"),
2350 #{code := 200, headers := RespHeaders} = do_raw(Config, [
2351 "GET /resp/stream_reply2/200 HTTP/1.1\r\n"
2352 "Host: localhost\r\n"
2353 "TE: chunked;q=0\r\n"
2354 "\r\n"]),
2355 {_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, RespHeaders),
2356 %% @todo We probably want to check the body received too.
2357 ok.
2358
2359 %%% @todo te_not_acceptable_coding(Config) ->
2360 %A qvalue of 0 in the TE header means "not acceptable". (RFC7230 4.3)
2361 %
2362 %@todo
2363 %The lack of a TE header or an empty TE header means only "chunked"
2364 %(with no trailers) or no transfer-encoding is acceptable. (RFC7230 4.3)
2365 %
2366 %@todo
2367 %Trailer headers must be listed in the trailer header field value. (RFC7230 4.4)
2368
2369 %% Upgrade.
2370
2371 %```
2372 %Upgrade = 1#protocol
2373 %
2374 %protocol = protocol-name ["/" protocol-version]
2375 %protocol-name = token
2376 %protocol-version = token
2377 %```
2378 %
2379 %The upgrade header contains the list of protocols the
2380 %client wishes to upgrade to, in order of preference. (RFC7230 6.7)
2381
2382 upgrade_safely_ignored(Config) ->
2383 doc("The upgrade header can be safely ignored. (RFC7230 6.7)"),
2384 #{code := 200} = do_raw(Config,
2385 "GET / HTTP/1.1\r\n"
2386 "Host: localhost\r\n"
2387 "Connection: upgrade\r\n"
2388 "Upgrade: websocket\r\n"
2389 "\r\n").
2390
2391 %upgrade_must_be_in_connection_header(Config) ->
2392 %The upgrade header must be listed under the connection header,
2393 %or must be ignored otherwise. (RFC7230 6.7)
2394 %
2395 %@todo
2396 %A server accepting an upgrade request must send a 101 status
2397 %code with a upgrade header listing the protocol(s) it upgrades
2398 %to, in layer-ascending order. In addition the upgrade header
2399 %must be listed in the connection header. (RFC7230 6.7)
2400 %
2401 %%A server must not switch to a protocol not listed in the
2402 %%request's upgrade header. (RFC7230 6.7)
2403 %
2404 %@todo
2405 %A server that sends a 426 status code must include a upgrade
2406 %header listing acceptable protocols in order of preference. (RFC7230 6.7)
2407 %
2408 %@todo
2409 %A server can send a upgrade header to any response to advertise
2410 %its support for other protocols listed in order of preference. (RFC7230 6.7)
2411 %
2412 %@todo
2413 %Immediately after a server responds with a 101 status code
2414 %it must respond to the original request using the new protocol. (RFC7230 6.7)
2415 %
2416 %@todo
2417 %%A server must not switch protocols unless the original message's
2418 %%semantics can be honored by the new protocol. OPTIONS requests
2419 %%can be honored by any protocol. (RFC7230 6.7)
2420 %
2421 %http10_ignore_upgrade_header(Config) ->
2422 %A server must ignore an upgrade header received by an HTTP/1.0
2423 %request. (RFC7230 6.7)
2424 %
2425 %expect_then_upgrade(Config) ->
2426 %A server receiving both an upgrade header and an expect header
2427 %containing "100-continue" must send a 100 response before the
2428 %101 response. (RFC7230 6.7)
2429 %
2430 %The upgrade header field cannot be used for switching the
2431 %connection protocol (e.g. TCP) or switching connections. (RFC7230 6.7)
2432
2433 %% Compatibility.
2434
2435 %@todo
2436 %A server can choose to be non-conformant to the specifications
2437 %for the sake of compatibility. Such behavior can be enabled
2438 %through configuration and/or software identification. (RFC7230 2.5)