123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401 |
- -module(rfc7230_SUITE).
- -compile(export_all).
- -import(ct_helper, [doc/1]).
- -import(cowboy_test, [raw_open/1]).
- -import(cowboy_test, [raw_send/2]).
- -import(cowboy_test, [raw_recv_head/1]).
- -import(cowboy_test, [raw_recv/3]).
- all() -> [{group, http}].
- groups() -> [{http, [parallel], ct_helper:all(?MODULE)}].
- init_per_group(Name = http, Config) ->
- cowboy_test:init_http(Name = http, #{
- env => #{dispatch => cowboy_router:compile(init_routes(Config))}
- }, Config).
- end_per_group(Name, _) ->
- ok = cowboy:stop_listener(Name).
- init_routes(_) -> [
- {"localhost", [
- {"/", hello_h, []},
- {"/echo/:key", echo_h, []}
- ]},
- {"127.0.0.1", [{"/echo/:key", echo_h, []}]},
- {"example.org", [{"/echo/:key", echo_h, []}]}
- ].
- do_raw(Config, Data) ->
- Client = raw_open(Config),
- ok = raw_send(Client, Data),
- {Version, Code, Reason, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),
- {Headers, Rest2} = cow_http:parse_headers(Rest),
- case lists:keyfind(<<"content-length">>, 1, Headers) of
- {_, LengthBin} when LengthBin =/= <<"0">> ->
- Length = binary_to_integer(LengthBin),
- Body = if
- byte_size(Rest2) =:= Length -> Rest2;
- true ->
- {ok, Body0} = raw_recv(Client, binary_to_integer(LengthBin) - byte_size(Rest2), 5000),
- << Rest2/bits, Body0/bits >>
- end,
- #{client => Client, version => Version, code => Code, reason => Reason, headers => Headers, body => Body};
- _ ->
- #{client => Client, version => Version, code => Code, reason => Reason, headers => Headers, body => <<>>}
- end.
- accept_at_least_1_empty_line(Config) ->
- doc("A configurable number of empty lines (CRLF) preceding the request "
- "must be ignored. At least 1 empty line must be ignored. (RFC7230 3.5)"),
- #{code := 200} = do_raw(Config,
- "\r\n"
- "GET / HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n").
- reject_response(Config) ->
- doc("When receiving a response instead of a request, identified by the "
- "status-line which starts with the HTTP version, the server must "
- "reject the message with a 400 status code and close the connection. (RFC7230 3.1)"),
- #{code := 400, client := Client} = do_raw(Config,
- "HTTP/1.1 200 OK\r\n"
- "\r\n"),
- {error, closed} = raw_recv(Client, 0, 1000).
- only_parse_necessary_elements(Config) ->
- doc("It is only necessary to parse elements required to process the request. (RFC7230 2.5)"),
- #{code := 200} = do_raw(Config,
- "GET / HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "Content-type: purposefully bad header value\r\n"
- "\r\n").
- no_empty_line_after_request_line(Config) ->
- doc("The general format of HTTP requests is strict. No empty line is "
- "allowed in-between components except for the empty line "
- "indicating the end of the list of headers."),
- #{code := 400} = do_raw(Config,
- "GET / HTTP/1.1\r\n"
- "\r\n"
- "Host: localhost\r\n"
- "\r\n").
- no_empty_line_in_headers(Config) ->
- doc("The general format of HTTP requests is strict. No empty line is "
- "allowed in-between components except for the empty line "
- "indicating the end of the list of headers."),
- #{code := 400} = do_raw(Config,
- "GET / HTTP/1.1\r\n"
- "User-Agent: RFC7230\r\n"
- "\r\n"
- "Host: localhost\r\n"
- "\r\n").
- timeout_before_request_line(Config) ->
- doc("The time the request (request line and headers) takes to be "
- "received by the server must be limited and subject to configuration. "
- "No response must be sent before closing if no request was initiated "
- "by the reception of a complete request-line."),
- Client = raw_open(Config),
- ok = raw_send(Client, "GET / HTTP/1.1\r"),
- {error, closed} = raw_recv(Client, 0, 6000).
- timeout_after_request_line(Config) ->
- doc("The time the request (request line and headers) takes to be "
- "received by the server must be limited and subject to configuration. "
- "A 408 status code must be sent if the request line was received."),
- #{code := 408, client := Client} = do_raw(Config, "GET / HTTP/1.1\r\n"),
- {error, closed} = raw_recv(Client, 0, 6000).
- limit_request_line_8000(Config) ->
- doc("It is recommended to limit the request-line length to a configurable "
- "limit of at least 8000 octets."),
- LongPath = ["/long-path" || _ <- lists:seq(1, 799)],
- #{code := 200} = do_raw(Config, [
- "GET /?qs=", LongPath, " HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"]).
- limit_request_line_9000(Config) ->
- doc("It is recommended to limit the request-line length to a configurable "
- "limit of at least 8000 octets. A request line too long must be rejected "
- "with a 414 status code and the closing of the connection. (RFC7230 3.1.1)"),
- LongPath = ["/long-path" || _ <- lists:seq(1, 899)],
- #{code := 414, client := Client} = do_raw(Config, [
- "GET /very", LongPath, " HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"]),
- {error, closed} = raw_recv(Client, 0, 1000).
- reject_invalid_method(Config) ->
- doc("The request method is defined as 1+ token characters. An invalid "
- "method must be rejected with a 400 status code and the "
- "closing of the connection. (RFC7230 3.1.1, RFC7230 3.2.6)"),
- #{code := 400, client := Client} = do_raw(Config,
- "GET\0 / HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- {error, closed} = raw_recv(Client, 0, 1000).
- reject_empty_method(Config) ->
- doc("The request method is defined as 1+ token characters. An empty "
- "method must be rejected with a 400 status code and the "
- "closing of the connection. (RFC7230 3.1.1, RFC7230 3.2.6)"),
- #{code := 400, client := Client} = do_raw(Config,
- " / HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- {error, closed} = raw_recv(Client, 0, 1000).
- limit_method_name(Config) ->
- doc("The length of the method must be subject to a configurable limit. "
- "A method too long must be rejected with a 501 status code and the "
- "closing of the connection. A good default for the method length limit "
- "is the longest method length the server implements. (RFC7230 3.1.1)"),
- LongMethod = [$G || _ <- lists:seq(1, 1000)],
- #{code := 501, client := Client} = do_raw(Config, [
- LongMethod, " / HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"]),
- {error, closed} = raw_recv(Client, 0, 1000).
- reject_tab_between_method_and_request_target(Config) ->
- doc("A request that uses anything other than SP as separator between "
- "the method and the request-target must be rejected with a 400 "
- "status code and the closing of the connection. (RFC7230 3.1.1, RFC7230 3.5)"),
- #{code := 400, client := Client} = do_raw(Config,
- "GET\t/ HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- {error, closed} = raw_recv(Client, 0, 1000).
- reject_two_sp_between_method_and_request_target(Config) ->
- doc("A request that uses anything other than SP as separator between "
- "the method and the request-target must be rejected with a 400 "
- "status code and the closing of the connection. (RFC7230 3.1.1, RFC7230 3.5)"),
- #{code := 400, client := Client} = do_raw(Config,
- "GET / HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- {error, closed} = raw_recv(Client, 0, 1000).
- ignore_uri_fragment_after_path(Config) ->
- doc("The fragment part of the target URI is not sent. It must be "
- "ignored by a server receiving it. (RFC7230 5.1)"),
- Echo = <<"http://localhost/echo/url">>,
- #{code := 200, body := Echo} = do_raw(Config,
- "GET /echo/url#fragment HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n").
- ignore_uri_fragment_after_query(Config) ->
- doc("The fragment part of the target URI is not sent. It must be "
- "ignored by a server receiving it. (RFC7230 5.1)"),
- Echo = <<"http://localhost/echo/url?key=value">>,
- #{code := 200, body := Echo} = do_raw(Config,
- "GET /echo/url?key=value#fragment HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n").
- must_understand_origin_form(Config) ->
- doc("A server must be able to handle at least origin-form and absolute-form. (RFC7230 5.3.2)"),
- #{code := 200} = do_raw(Config,
- "GET / HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n").
- origin_form_reject_if_connect(Config) ->
- doc("origin-form is used when the client does not connect to a proxy, "
- "does not use the CONNECT method and does not issue a site-wide "
- "OPTIONS request. (RFC7230 5.3.1)"),
- #{code := 400, client := Client} = do_raw(Config,
- "CONNECT / HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- {error, closed} = raw_recv(Client, 0, 1000).
- origin_form_tcp_scheme(Config) ->
- doc("The scheme is either resolved from configuration or is \"https\" "
- "when on a TLS connection and \"http\" otherwise. (RFC7230 5.5)"),
- Echo = <<"http://localhost/echo/url">>,
- #{code := 200, body := Echo} = do_raw(Config,
- "GET /echo/url HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n").
- origin_form_path(Config) ->
- doc("The absolute-path always starts with \"/\" and ends with either \"?\", \"#\" "
- "or the end of the URI. (RFC3986 3.3)"),
- Echo = <<"/echo/path">>,
- #{code := 200, body := Echo} = do_raw(Config,
- "GET /echo/path HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n").
- origin_form_path_query(Config) ->
- doc("The absolute-path always starts with \"/\" and ends with either \"?\", \"#\" "
- "or the end of the URI. (RFC3986 3.3)"),
- Echo = <<"/echo/path">>,
- #{code := 200, body := Echo} = do_raw(Config,
- "GET /echo/path?key=value HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n").
- origin_form_path_fragment(Config) ->
- doc("The absolute-path always starts with \"/\" and ends with either \"?\", \"#\" "
- "or the end of the URI. (RFC3986 3.3)"),
- Echo = <<"/echo/path">>,
- #{code := 200, body := Echo} = do_raw(Config,
- "GET /echo/path#fragment HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n").
- origin_form_query(Config) ->
- doc("The query starts with \"?\" and ends with \"#\" or the end of the URI. (RFC3986 3.4)"),
- Echo = <<"key=value">>,
- #{code := 200, body := Echo} = do_raw(Config,
- "GET /echo/qs?key=value HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n").
- origin_form_query_fragment(Config) ->
- doc("The query starts with \"?\" and ends with \"#\" or the end of the URI. (RFC3986 3.4)"),
- Echo = <<"key=value">>,
- #{code := 200, body := Echo} = do_raw(Config,
- "GET /echo/qs?key=value#fragment HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n").
- must_understand_absolute_form(Config) ->
- doc("A server must be able to handle at least origin-form and absolute-form. (RFC7230 5.3.2)"),
- #{code := 200} = do_raw(Config,
- "GET http://localhost HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n").
- absolute_form_case_insensitive_scheme(Config) ->
- doc("The scheme is case insensitive and normally provided in lowercase. (RFC7230 2.7.3)"),
- Echo = <<"http://localhost/echo/url">>,
- #{code := 200, body := Echo} = do_raw(Config,
- "GET HttP://localhost/echo/url HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n").
- absolute_form_case_insensitive_host(Config) ->
- doc("The host is case insensitive and normally provided in lowercase. (RFC7230 2.7.3)"),
- Echo = <<"http://localhost/echo/url">>,
- #{code := 200, body := Echo} = do_raw(Config,
- "GET http://LoCaLHOsT/echo/url HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n").
- absolute_form_reject_unknown_schemes(Config) ->
- doc("Unknown schemes must be rejected with a 400 status code and the closing of the connection."),
- #{code := 400, client := Client} = do_raw(Config,
- "GET bad://localhost/ HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- {error, closed} = raw_recv(Client, 0, 1000).
- absolute_form_drop_scheme_tcp(Config) ->
- doc("The scheme provided with the request must be dropped. The effective "
- "scheme is either resolved from configuration or is \"https\" when on "
- "a TLS connection and \"http\" otherwise. (RFC7230 5.5)"),
- Echo = <<"http://localhost/echo/url">>,
- #{code := 200, body := Echo} = do_raw(Config,
- "GET https://localhost/echo/url HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n").
- absolute_form_reject_userinfo(Config) ->
- doc("An authority component with a userinfo component (and its "
- "\"@\" delimiter) is invalid. The request must be rejected with "
- "a 400 status code and the closing of the connection. (RFC7230 2.7.1)"),
- #{code := 400, client := Client} = do_raw(Config,
- "GET http://username:password@localhost HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- {error, closed} = raw_recv(Client, 0, 1000).
- absolute_form_reject_missing_host_without_path(Config) ->
- doc("A URI with a missing host identifier is invalid. The request must "
- "be rejected with a 400 status code and the closing of the connection. (RFC7230 2.7.1)"),
- #{code := 400, client := Client} = do_raw(Config,
- "GET http:// HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- {error, closed} = raw_recv(Client, 0, 1000).
- absolute_form_reject_missing_host_with_path(Config) ->
- doc("A URI with a missing host identifier is invalid. The request must "
- "be rejected with a 400 status code and the closing of the connection. (RFC7230 2.7.1)"),
- #{code := 400, client := Client} = do_raw(Config,
- "GET http:/// HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- {error, closed} = raw_recv(Client, 0, 1000).
- absolute_form_ipv4(Config) ->
- doc("Absolute form with an IPv4 address for the host. (RFC3986 3.2.2)"),
- Echo = <<"127.0.0.1">>,
- #{code := 200, body := Echo} = do_raw(Config,
- "GET http://127.0.0.1/echo/host HTTP/1.1\r\n"
- "Host: 127.0.0.1\r\n"
- "\r\n").
- absolute_form_ipv4_port(Config) ->
- doc("Absolute form with an IPv4 address for the host and a port number. (RFC3986 3.2.2)"),
- Host = <<"127.0.0.1">>,
- #{code := 200, body := Host} = do_raw(Config,
- "GET http://127.0.0.1:8080/echo/host HTTP/1.1\r\n"
- "Host: 127.0.0.1:8080\r\n"
- "\r\n"),
- Port = <<"8080">>,
- #{code := 200, body := Port} = do_raw(Config,
- "GET http://127.0.0.1:8080/echo/port HTTP/1.1\r\n"
- "Host: 127.0.0.1:8080\r\n"
- "\r\n").
- absolute_form_reg_name(Config) ->
- doc("Absolute form with a regular name for the host. (RFC3986 3.2.2)"),
- Echo = <<"example.org">>,
- #{code := 200, body := Echo} = do_raw(Config,
- "GET http://example.org/echo/host HTTP/1.1\r\n"
- "Host: example.org\r\n"
- "\r\n").
- absolute_form_reg_name_port(Config) ->
- doc("Absolute form with an IPv4 address for the host and a port number. (RFC3986 3.2.2)"),
- Host = <<"example.org">>,
- #{code := 200, body := Host} = do_raw(Config,
- "GET http://example.org:8080/echo/host HTTP/1.1\r\n"
- "Host: example.org:8080\r\n"
- "\r\n"),
- Port = <<"8080">>,
- #{code := 200, body := Port} = do_raw(Config,
- "GET http://example.org:8080/echo/port HTTP/1.1\r\n"
- "Host: example.org:8080\r\n"
- "\r\n").
- absolute_form_limit_host(Config) ->
- doc("The maximum length for the host component of the URI must be subject "
- "to a configurable limit. A good default is 255 characters. "
- "(RFC7230 3.1.1, RFC3986 3.2.2, RFC1034 3.1)"),
- LongHost = ["host." || _ <- lists:seq(1, 100)],
- #{code := 414, client := Client} = do_raw(Config, [
- "GET http://", LongHost, "/ HTTP/1.1\r\n"
- "Host: ", LongHost, "\r\n"
- "\r\n"]),
- {error, closed} = raw_recv(Client, 0, 1000).
- absolute_form_invalid_port_0(Config) ->
- doc("Port number 0 is reserved. The request must be rejected and the connection closed."),
- #{code := 400, client := Client} = do_raw(Config,
- "GET http://localhost:0/ HTTP/1.1\r\n"
- "Host: localhost:0\r\n"
- "\r\n"),
- {error, closed} = raw_recv(Client, 0, 1000).
- absolute_form_invalid_port_65536(Config) ->
- doc("Port numbers above 65535 are invalid. The request must be rejected and the connection closed."),
- #{code := 400, client := Client} = do_raw(Config,
- "GET http://localhost:65536/ HTTP/1.1\r\n"
- "Host: localhost:65536\r\n"
- "\r\n"),
- {error, closed} = raw_recv(Client, 0, 1000).
- absolute_form_path(Config) ->
- doc("The path always starts with \"/\" and ends with either \"?\", \"#\" "
- "or the end of the URI. (RFC3986 3.3)"),
- Echo = <<"/echo/path">>,
- #{code := 200, body := Echo} = do_raw(Config,
- "GET http://localhost/echo/path HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n").
- absolute_form_path_query(Config) ->
- doc("The path always starts with \"/\" and ends with either \"?\", \"#\" "
- "or the end of the URI. (RFC3986 3.3)"),
- Echo = <<"/echo/path">>,
- #{code := 200, body := Echo} = do_raw(Config,
- "GET http://localhost/echo/path?key=value HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n").
- absolute_form_path_fragment(Config) ->
- doc("The path always starts with \"/\" and ends with either \"?\", \"#\" "
- "or the end of the URI. (RFC3986 3.3)"),
- Echo = <<"/echo/path">>,
- #{code := 200, body := Echo} = do_raw(Config,
- "GET http://localhost/echo/path#fragment HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n").
- absolute_form_no_path(Config) ->
- doc("An empty path component is equivalent to \"/\". (RFC7230 2.7.3)"),
- #{code := 200, body := <<"Hello world!">>} = do_raw(Config,
- "GET http://localhost HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n").
- absolute_form_no_path_then_query(Config) ->
- doc("An empty path component is equivalent to \"/\". (RFC7230 2.7.3)"),
- #{code := 200, body := <<"Hello world!">>} = do_raw(Config,
- "GET http://localhost?key=value HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n").
- absolute_form_no_path_then_fragment(Config) ->
- doc("An empty path component is equivalent to \"/\". (RFC7230 2.7.3)"),
- #{code := 200, body := <<"Hello world!">>} = do_raw(Config,
- "GET http://localhost#fragment HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n").
- absolute_form_query(Config) ->
- doc("The query starts with \"?\" and ends with \"#\" or the end of the URI. (RFC3986 3.4)"),
- Echo = <<"key=value">>,
- #{code := 200, body := Echo} = do_raw(Config,
- "GET http://localhost/echo/qs?key=value HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n").
- absolute_form_query_fragment(Config) ->
- doc("The query starts with \"?\" and ends with \"#\" or the end of the URI. (RFC3986 3.4)"),
- Echo = <<"key=value">>,
- #{code := 200, body := Echo} = do_raw(Config,
- "GET http://localhost/echo/qs?key=value#fragment HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n").
- authority_form_reject_if_not_connect(Config) ->
- doc("When the method is CONNECT, authority-form must be used. This "
- "form does not apply to any other methods which must reject the "
- "request with a 400 status code and the closing of the connection. (RFC7230 5.3.3)"),
- #{code := 400, client := Client} = do_raw(Config,
- "GET localhost:80 HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- {error, closed} = raw_recv(Client, 0, 1000).
- asterisk_form_reject_if_not_options(Config) ->
- doc("asterisk-form is used for server-wide OPTIONS requests. "
- "It is invalid with any other methods which must reject the "
- "request with a 400 status code and the closing of the connection. (RFC7230 5.3.4)"),
- #{code := 400, client := Client} = do_raw(Config,
- "GET * HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- {error, closed} = raw_recv(Client, 0, 1000).
- asterisk_form_empty_path(Config) ->
- doc("The path is empty when using asterisk-form. (RFC7230 5.5)"),
- #{code := 200, body := <<>>} = do_raw(Config,
- "OPTIONS * HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "X-Echo: path\r\n"
- "\r\n").
- asterisk_form_empty_query(Config) ->
- doc("The query is empty when using asterisk-form. (RFC7230 5.5)"),
- #{code := 200, body := <<>>} = do_raw(Config,
- "OPTIONS * HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "X-Echo: query\r\n"
- "\r\n").
- invalid_request_target(Config) ->
- doc("Any other form is invalid and must be rejected with a 400 status code "
- "and the closing of the connection."),
- #{code := 400, client := Client} = do_raw(Config,
- "GET \0 HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- {error, closed} = raw_recv(Client, 0, 1000).
- reject_tab_between_request_target_and_version(Config) ->
- doc("A request that uses anything other than SP as separator between "
- "the request-target and the version must be rejected with a 400 "
- "status code and the closing of the connection. (RFC7230 3.1.1, RFC7230 3.5)"),
- #{code := 400, client := Client} = do_raw(Config,
- "GET /\tHTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- {error, closed} = raw_recv(Client, 0, 1000).
- reject_two_sp_between_request_target_and_version(Config) ->
- doc("A request that uses anything other than SP as separator between "
- "the request-target and the version must be rejected with a 400 "
- "status code and the closing of the connection. (RFC7230 3.1.1, RFC7230 3.5)"),
- #{code := 400, client := Client} = do_raw(Config,
- "GET / HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- {error, closed} = raw_recv(Client, 0, 1000).
- reject_invalid_version_http100(Config) ->
- doc("Any version number other than HTTP/1.0 or HTTP/1.1 must be "
- "rejected by a server or intermediary with a 505 status code. (RFC7230 2.6, RFC7230 A.2)"),
- #{code := 505} = do_raw(Config,
- "GET / HTTP/1.00\r\n"
- "Host: localhost\r\n"
- "\r\n").
- reject_invalid_version_http111(Config) ->
- doc("Any version number other than HTTP/1.0 or HTTP/1.1 must be "
- "rejected by a server or intermediary with a 505 status code. (RFC7230 2.6, RFC7230 A.2)"),
- #{code := 505} = do_raw(Config,
- "GET / HTTP/1.11\r\n"
- "Host: localhost\r\n"
- "\r\n").
- reject_invalid_version_http12(Config) ->
- doc("Any version number other than HTTP/1.0 or HTTP/1.1 must be "
- "rejected by a server or intermediary with a 505 status code. (RFC7230 2.6, RFC7230 A.2)"),
- #{code := 505} = do_raw(Config,
- "GET / HTTP/1.2\r\n"
- "Host: localhost\r\n"
- "\r\n").
- reject_invalid_version_http2(Config) ->
- doc("Any version number other than HTTP/1.0 or HTTP/1.1 must be "
- "rejected by a server or intermediary with a 505 status code. (RFC7230 2.6, RFC7230 A.2)"),
- #{code := 505} = do_raw(Config,
- "GET / HTTP/2\r\n"
- "Host: localhost\r\n"
- "\r\n").
- reject_empty_version(Config) ->
- doc("Any version number other than HTTP/1.0 or HTTP/1.1 must be "
- "rejected by a server or intermediary with a 505 status code. (RFC7230 2.6, RFC7230 A.2)"),
- #{code := 505} = do_raw(Config,
- "GET / \r\n"
- "Host: localhost\r\n"
- "\r\n").
- reject_invalid_whitespace_after_version(Config) ->
- doc("A request that has whitespace different than CRLF following the "
- "version must be rejected with a 400 status code and the closing "
- "of the connection. (RFC7230 3.1.1)"),
- #{code := 400, client := Client} = do_raw(Config,
- "GET / HTTP/1.1 \r\n"
- "Host: localhost\r\n"
- "\r\n"),
- {error, closed} = raw_recv(Client, 0, 1000).
- limit_header_name(Config) ->
- doc("The header name must be subject to a configurable limit. A "
- "good default is 50 characters, well above the longest registered "
- "header. Such a request must be rejected with a 431 status code "
- "and the closing of the connection. "
- "(RFC7230 3.2.5, RFC6585 5, IANA Message Headers registry)"),
- #{code := 431, client := Client} = do_raw(Config, [
- "GET / HTTP/1.1\r\n"
- "Host: localhost\r\n",
- binary:copy(<<$a>>, 32768), ": bad\r\n"
- "\r\n"]),
- {error, closed} = raw_recv(Client, 0, 1000).
- limit_header_value(Config) ->
- doc("The header value and the optional whitespace around it must be "
- "subject to a configurable limit. There is no recommendations "
- "for the default. 4096 characters is known to work well. Such "
- "a request must be rejected with a 431 status code and the closing "
- "of the connection. (RFC7230 3.2.5, RFC6585 5)"),
- #{code := 431, client := Client} = do_raw(Config, [
- "GET / HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "bad: ", binary:copy(<<$a>>, 32768), "\r\n"
- "\r\n"]),
- {error, closed} = raw_recv(Client, 0, 1000).
|