rfc7230_SUITE.erl 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767
  1. %% Copyright (c) 2015-2017, Loïc Hoguin <essen@ninenines.eu>
  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. -module(rfc7230_SUITE).
  15. -compile(export_all).
  16. -import(ct_helper, [doc/1]).
  17. -import(cowboy_test, [gun_open/1]).
  18. -import(cowboy_test, [raw_open/1]).
  19. -import(cowboy_test, [raw_send/2]).
  20. -import(cowboy_test, [raw_recv_head/1]).
  21. -import(cowboy_test, [raw_recv/3]).
  22. all() -> [{group, http}].
  23. groups() -> [{http, [parallel], ct_helper:all(?MODULE)}]. %% @todo parallel
  24. init_per_group(Name = http, Config) ->
  25. cowboy_test:init_http(Name = http, #{
  26. env => #{dispatch => cowboy_router:compile(init_routes(Config))}
  27. }, Config).
  28. end_per_group(Name, _) ->
  29. ok = cowboy:stop_listener(Name).
  30. init_routes(_) -> [
  31. {"localhost", [
  32. {"/", hello_h, []},
  33. {"/echo/:key[/:arg]", echo_h, []},
  34. {"/length/echo/:key", echo_h, []}
  35. %% @todo Something is clearly wrong about routing * right now.
  36. %% {"*", asterisk_h, []}
  37. ]},
  38. {"127.0.0.1", [{"/echo/:key", echo_h, []}]},
  39. {"example.org", [{"/echo/:key", echo_h, []}]}
  40. %% @todo Add IPv6 addresses support to the router. This fails:
  41. %% {"[2001:db8:85a3::8a2e:370:7334]", [{"/echo/:key", echo_h, []}]}
  42. ].
  43. do_raw(Config, Data) ->
  44. Client = raw_open(Config),
  45. ok = raw_send(Client, Data),
  46. {Version, Code, Reason, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),
  47. {Headers, Rest2} = cow_http:parse_headers(Rest),
  48. case lists:keyfind(<<"content-length">>, 1, Headers) of
  49. {_, LengthBin} when LengthBin =/= <<"0">> ->
  50. Length = binary_to_integer(LengthBin),
  51. Body = if
  52. byte_size(Rest2) =:= Length -> Rest2;
  53. true ->
  54. {ok, Body0} = raw_recv(Client, binary_to_integer(LengthBin) - byte_size(Rest2), 5000),
  55. << Rest2/bits, Body0/bits >>
  56. end,
  57. #{client => Client, version => Version, code => Code, reason => Reason, headers => Headers, body => Body};
  58. _ ->
  59. #{client => Client, version => Version, code => Code, reason => Reason, headers => Headers, body => <<>>}
  60. end.
  61. %% Listener.
  62. %% @todo Add to documentation.
  63. %The default port for "http" connections is 80. The connection
  64. %uses plain TCP. (RFC7230 2.7.1)
  65. %
  66. %The default port for "https" connections is 443. The connection
  67. %uses TLS. (RFC7230 2.7.2)
  68. %
  69. %Any other port may be used for either of them.
  70. %% Before the request.
  71. accept_at_least_1_empty_line(Config) ->
  72. doc("A configurable number of empty lines (CRLF) preceding the request "
  73. "must be ignored. At least 1 empty line must be ignored. (RFC7230 3.5)"),
  74. #{code := 200} = do_raw(Config,
  75. "\r\n"
  76. "GET / HTTP/1.1\r\n"
  77. "Host: localhost\r\n"
  78. "\r\n").
  79. reject_response(Config) ->
  80. doc("When receiving a response instead of a request, identified by the "
  81. "status-line which starts with the HTTP version, the server must "
  82. "reject the message with a 400 status code and close the connection. (RFC7230 3.1)"),
  83. #{code := 400, client := Client} = do_raw(Config,
  84. "HTTP/1.1 200 OK\r\n"
  85. "\r\n"),
  86. {error, closed} = raw_recv(Client, 0, 1000).
  87. %% Request.
  88. only_parse_necessary_elements(Config) ->
  89. doc("It is only necessary to parse elements required to process the request. (RFC7230 2.5)"),
  90. #{code := 200} = do_raw(Config,
  91. "GET / HTTP/1.1\r\n"
  92. "Host: localhost\r\n"
  93. "Content-type: purposefully bad header value\r\n"
  94. "\r\n").
  95. %% @todo Add to documentation.
  96. %Parsed elements are subject to configurable limits. A server must
  97. %be able to parse elements at least as long as it generates. (RFC7230 2.5)
  98. no_empty_line_after_request_line(Config) ->
  99. doc("The general format of HTTP requests is strict. No empty line is "
  100. "allowed in-between components except for the empty line "
  101. "indicating the end of the list of headers."),
  102. #{code := 400} = do_raw(Config,
  103. "GET / HTTP/1.1\r\n"
  104. "\r\n"
  105. "Host: localhost\r\n"
  106. "\r\n").
  107. no_empty_line_in_headers(Config) ->
  108. doc("The general format of HTTP requests is strict. No empty line is "
  109. "allowed in-between components except for the empty line "
  110. "indicating the end of the list of headers."),
  111. #{code := 400} = do_raw(Config,
  112. "GET / HTTP/1.1\r\n"
  113. "User-Agent: RFC7230\r\n"
  114. "\r\n"
  115. "Host: localhost\r\n"
  116. "\r\n").
  117. timeout_before_request_line(Config) ->
  118. doc("The time the request (request line and headers) takes to be "
  119. "received by the server must be limited and subject to configuration. "
  120. "No response must be sent before closing if no request was initiated "
  121. "by the reception of a complete request-line."),
  122. Client = raw_open(Config),
  123. ok = raw_send(Client, "GET / HTTP/1.1\r"),
  124. {error, closed} = raw_recv(Client, 0, 6000).
  125. timeout_after_request_line(Config) ->
  126. doc("The time the request (request line and headers) takes to be "
  127. "received by the server must be limited and subject to configuration. "
  128. "A 408 status code must be sent if the request line was received."),
  129. #{code := 408, client := Client} = do_raw(Config, "GET / HTTP/1.1\r\n"),
  130. {error, closed} = raw_recv(Client, 0, 6000).
  131. %% @todo Add an HTTP/1.0 test suite.
  132. %An HTTP/1.1 server must understand any valid HTTP/1.0 request,
  133. %and respond to those with an HTTP/1.1 message that only use
  134. %features understood or safely ignored by HTTP/1.0 clients. (RFC7230 A)
  135. %% Request line.
  136. limit_request_line_8000(Config) ->
  137. doc("It is recommended to limit the request-line length to a configurable "
  138. "limit of at least 8000 octets."),
  139. LongPath = ["/long-path" || _ <- lists:seq(1, 799)],
  140. #{code := 200} = do_raw(Config, [
  141. "GET /?qs=", LongPath, " HTTP/1.1\r\n"
  142. "Host: localhost\r\n"
  143. "\r\n"]).
  144. limit_request_line_9000(Config) ->
  145. doc("It is recommended to limit the request-line length to a configurable "
  146. "limit of at least 8000 octets. A request line too long must be rejected "
  147. "with a 414 status code and the closing of the connection. (RFC7230 3.1.1)"),
  148. LongPath = ["/long-path" || _ <- lists:seq(1, 899)],
  149. #{code := 414, client := Client} = do_raw(Config, [
  150. "GET /very", LongPath, " HTTP/1.1\r\n"
  151. "Host: localhost\r\n"
  152. "\r\n"]),
  153. {error, closed} = raw_recv(Client, 0, 1000).
  154. %% Method.
  155. reject_invalid_method(Config) ->
  156. doc("The request method is defined as 1+ token characters. An invalid "
  157. "method must be rejected with a 400 status code and the "
  158. "closing of the connection. (RFC7230 3.1.1, RFC7230 3.2.6)"),
  159. #{code := 400, client := Client} = do_raw(Config,
  160. "GET\0 / HTTP/1.1\r\n"
  161. "Host: localhost\r\n"
  162. "\r\n"),
  163. {error, closed} = raw_recv(Client, 0, 1000).
  164. reject_empty_method(Config) ->
  165. doc("The request method is defined as 1+ token characters. An empty "
  166. "method must be rejected with a 400 status code and the "
  167. "closing of the connection. (RFC7230 3.1.1, RFC7230 3.2.6)"),
  168. #{code := 400, client := Client} = do_raw(Config,
  169. " / HTTP/1.1\r\n"
  170. "Host: localhost\r\n"
  171. "\r\n"),
  172. {error, closed} = raw_recv(Client, 0, 1000).
  173. %% @todo We probably want to directly match commonly used methods.
  174. %In practice the only characters in use by registered methods are
  175. %uppercase letters [A-Z] and the dash "-". (IANA HTTP Method Registry)
  176. limit_method_name(Config) ->
  177. doc("The length of the method must be subject to a configurable limit. "
  178. "A method too long must be rejected with a 501 status code and the "
  179. "closing of the connection. A good default for the method length limit "
  180. "is the longest method length the server implements. (RFC7230 3.1.1)"),
  181. LongMethod = [$G || _ <- lists:seq(1, 1000)],
  182. #{code := 501, client := Client} = do_raw(Config, [
  183. LongMethod, " / HTTP/1.1\r\n"
  184. "Host: localhost\r\n"
  185. "\r\n"]),
  186. {error, closed} = raw_recv(Client, 0, 1000).
  187. %% Between method and request-target.
  188. reject_tab_between_method_and_request_target(Config) ->
  189. doc("A request that uses anything other than SP as separator between "
  190. "the method and the request-target must be rejected with a 400 "
  191. "status code and the closing of the connection. (RFC7230 3.1.1, RFC7230 3.5)"),
  192. #{code := 400, client := Client} = do_raw(Config,
  193. "GET\t/ HTTP/1.1\r\n"
  194. "Host: localhost\r\n"
  195. "\r\n"),
  196. {error, closed} = raw_recv(Client, 0, 1000).
  197. reject_two_sp_between_method_and_request_target(Config) ->
  198. doc("A request that uses anything other than SP as separator between "
  199. "the method and the request-target must be rejected with a 400 "
  200. "status code and the closing of the connection. (RFC7230 3.1.1, RFC7230 3.5)"),
  201. #{code := 400, client := Client} = do_raw(Config,
  202. "GET / HTTP/1.1\r\n"
  203. "Host: localhost\r\n"
  204. "\r\n"),
  205. {error, closed} = raw_recv(Client, 0, 1000).
  206. %% Request target.
  207. ignore_uri_fragment_after_path(Config) ->
  208. doc("The fragment part of the target URI is not sent. It must be "
  209. "ignored by a server receiving it. (RFC7230 5.1)"),
  210. Echo = <<"http://localhost/echo/url">>,
  211. #{code := 200, body := Echo} = do_raw(Config,
  212. "GET /echo/url#fragment HTTP/1.1\r\n"
  213. "Host: localhost\r\n"
  214. "\r\n").
  215. ignore_uri_fragment_after_query(Config) ->
  216. doc("The fragment part of the target URI is not sent. It must be "
  217. "ignored by a server receiving it. (RFC7230 5.1)"),
  218. Echo = <<"http://localhost/echo/url?key=value">>,
  219. #{code := 200, body := Echo} = do_raw(Config,
  220. "GET /echo/url?key=value#fragment HTTP/1.1\r\n"
  221. "Host: localhost\r\n"
  222. "\r\n").
  223. %% Request target: origin-form.
  224. must_understand_origin_form(Config) ->
  225. doc("A server must be able to handle at least origin-form and absolute-form. (RFC7230 5.3.2)"),
  226. #{code := 200} = do_raw(Config,
  227. "GET / HTTP/1.1\r\n"
  228. "Host: localhost\r\n"
  229. "\r\n").
  230. origin_form_reject_if_connect(Config) ->
  231. doc("origin-form is used when the client does not connect to a proxy, "
  232. "does not use the CONNECT method and does not issue a site-wide "
  233. "OPTIONS request. (RFC7230 5.3.1)"),
  234. #{code := 400, client := Client} = do_raw(Config,
  235. "CONNECT / HTTP/1.1\r\n"
  236. "Host: localhost\r\n"
  237. "\r\n"),
  238. {error, closed} = raw_recv(Client, 0, 1000).
  239. %% @todo Equivalent test for https.
  240. origin_form_tcp_scheme(Config) ->
  241. doc("The scheme is either resolved from configuration or is \"https\" "
  242. "when on a TLS connection and \"http\" otherwise. (RFC7230 5.5)"),
  243. Echo = <<"http://localhost/echo/url">>,
  244. #{code := 200, body := Echo} = do_raw(Config,
  245. "GET /echo/url HTTP/1.1\r\n"
  246. "Host: localhost\r\n"
  247. "\r\n").
  248. origin_form_path(Config) ->
  249. doc("The absolute-path always starts with \"/\" and ends with either \"?\", \"#\" "
  250. "or the end of the URI. (RFC3986 3.3)"),
  251. Echo = <<"/echo/path">>,
  252. #{code := 200, body := Echo} = do_raw(Config,
  253. "GET /echo/path HTTP/1.1\r\n"
  254. "Host: localhost\r\n"
  255. "\r\n").
  256. origin_form_path_query(Config) ->
  257. doc("The absolute-path always starts with \"/\" and ends with either \"?\", \"#\" "
  258. "or the end of the URI. (RFC3986 3.3)"),
  259. Echo = <<"/echo/path">>,
  260. #{code := 200, body := Echo} = do_raw(Config,
  261. "GET /echo/path?key=value HTTP/1.1\r\n"
  262. "Host: localhost\r\n"
  263. "\r\n").
  264. origin_form_path_fragment(Config) ->
  265. doc("The absolute-path always starts with \"/\" and ends with either \"?\", \"#\" "
  266. "or the end of the URI. (RFC3986 3.3)"),
  267. Echo = <<"/echo/path">>,
  268. #{code := 200, body := Echo} = do_raw(Config,
  269. "GET /echo/path#fragment HTTP/1.1\r\n"
  270. "Host: localhost\r\n"
  271. "\r\n").
  272. origin_form_query(Config) ->
  273. doc("The query starts with \"?\" and ends with \"#\" or the end of the URI. (RFC3986 3.4)"),
  274. Echo = <<"key=value">>,
  275. #{code := 200, body := Echo} = do_raw(Config,
  276. "GET /echo/qs?key=value HTTP/1.1\r\n"
  277. "Host: localhost\r\n"
  278. "\r\n").
  279. origin_form_query_fragment(Config) ->
  280. doc("The query starts with \"?\" and ends with \"#\" or the end of the URI. (RFC3986 3.4)"),
  281. Echo = <<"key=value">>,
  282. #{code := 200, body := Echo} = do_raw(Config,
  283. "GET /echo/qs?key=value#fragment HTTP/1.1\r\n"
  284. "Host: localhost\r\n"
  285. "\r\n").
  286. %% @todo origin_form: reject paths with too large depth or query strings with too many keys
  287. %% Request target: absolute-form.
  288. must_understand_absolute_form(Config) ->
  289. doc("A server must be able to handle at least origin-form and absolute-form. (RFC7230 5.3.2)"),
  290. #{code := 200} = do_raw(Config,
  291. "GET http://localhost HTTP/1.1\r\n"
  292. "Host: localhost\r\n"
  293. "\r\n").
  294. absolute_form_case_insensitive_scheme(Config) ->
  295. doc("The scheme is case insensitive and normally provided in lowercase. (RFC7230 2.7.3)"),
  296. Echo = <<"http://localhost/echo/url">>,
  297. #{code := 200, body := Echo} = do_raw(Config,
  298. "GET HttP://localhost/echo/url HTTP/1.1\r\n"
  299. "Host: localhost\r\n"
  300. "\r\n").
  301. absolute_form_case_insensitive_host(Config) ->
  302. doc("The host is case insensitive and normally provided in lowercase. (RFC7230 2.7.3)"),
  303. Echo = <<"http://localhost/echo/url">>,
  304. #{code := 200, body := Echo} = do_raw(Config,
  305. "GET http://LoCaLHOsT/echo/url HTTP/1.1\r\n"
  306. "Host: localhost\r\n"
  307. "\r\n").
  308. absolute_form_reject_unknown_schemes(Config) ->
  309. doc("Unknown schemes must be rejected with a 400 status code and the closing of the connection."),
  310. #{code := 400, client := Client} = do_raw(Config,
  311. "GET bad://localhost/ HTTP/1.1\r\n"
  312. "Host: localhost\r\n"
  313. "\r\n"),
  314. {error, closed} = raw_recv(Client, 0, 1000).
  315. %% @todo Equivalent test for https.
  316. absolute_form_drop_scheme_tcp(Config) ->
  317. doc("The scheme provided with the request must be dropped. The effective "
  318. "scheme is either resolved from configuration or is \"https\" when on "
  319. "a TLS connection and \"http\" otherwise. (RFC7230 5.5)"),
  320. Echo = <<"http://localhost/echo/url">>,
  321. #{code := 200, body := Echo} = do_raw(Config,
  322. "GET https://localhost/echo/url HTTP/1.1\r\n"
  323. "Host: localhost\r\n"
  324. "\r\n").
  325. absolute_form_reject_userinfo(Config) ->
  326. doc("An authority component with a userinfo component (and its "
  327. "\"@\" delimiter) is invalid. The request must be rejected with "
  328. "a 400 status code and the closing of the connection. (RFC7230 2.7.1)"),
  329. #{code := 400, client := Client} = do_raw(Config,
  330. "GET http://username:password@localhost HTTP/1.1\r\n"
  331. "Host: localhost\r\n"
  332. "\r\n"),
  333. {error, closed} = raw_recv(Client, 0, 1000).
  334. absolute_form_reject_missing_host_without_path(Config) ->
  335. doc("A URI with a missing host identifier is invalid. The request must "
  336. "be rejected with a 400 status code and the closing of the connection. (RFC7230 2.7.1)"),
  337. #{code := 400, client := Client} = do_raw(Config,
  338. "GET http:// HTTP/1.1\r\n"
  339. "Host: localhost\r\n"
  340. "\r\n"),
  341. {error, closed} = raw_recv(Client, 0, 1000).
  342. absolute_form_reject_missing_host_with_path(Config) ->
  343. doc("A URI with a missing host identifier is invalid. The request must "
  344. "be rejected with a 400 status code and the closing of the connection. (RFC7230 2.7.1)"),
  345. #{code := 400, client := Client} = do_raw(Config,
  346. "GET http:/// HTTP/1.1\r\n"
  347. "Host: localhost\r\n"
  348. "\r\n"),
  349. {error, closed} = raw_recv(Client, 0, 1000).
  350. absolute_form_ipv4(Config) ->
  351. doc("Absolute form with an IPv4 address for the host. (RFC3986 3.2.2)"),
  352. Echo = <<"127.0.0.1">>,
  353. #{code := 200, body := Echo} = do_raw(Config,
  354. "GET http://127.0.0.1/echo/host HTTP/1.1\r\n"
  355. "Host: 127.0.0.1\r\n"
  356. "\r\n").
  357. absolute_form_ipv4_port(Config) ->
  358. doc("Absolute form with an IPv4 address for the host and a port number. (RFC3986 3.2.2)"),
  359. Host = <<"127.0.0.1">>,
  360. #{code := 200, body := Host} = do_raw(Config,
  361. "GET http://127.0.0.1:8080/echo/host HTTP/1.1\r\n"
  362. "Host: 127.0.0.1:8080\r\n"
  363. "\r\n"),
  364. Port = <<"8080">>,
  365. #{code := 200, body := Port} = do_raw(Config,
  366. "GET http://127.0.0.1:8080/echo/port HTTP/1.1\r\n"
  367. "Host: 127.0.0.1:8080\r\n"
  368. "\r\n").
  369. %% @todo We need the router to support IPv6 addresses to write proper tests for these:
  370. %absolute_form_ipv6(Config) ->
  371. %absolute_form_ipv6_ipv4(Config) ->
  372. %absolute_form_ipv6_zoneid(Config) ->
  373. absolute_form_reg_name(Config) ->
  374. doc("Absolute form with a regular name for the host. (RFC3986 3.2.2)"),
  375. Echo = <<"example.org">>,
  376. #{code := 200, body := Echo} = do_raw(Config,
  377. "GET http://example.org/echo/host HTTP/1.1\r\n"
  378. "Host: example.org\r\n"
  379. "\r\n").
  380. absolute_form_reg_name_port(Config) ->
  381. doc("Absolute form with an IPv4 address for the host and a port number. (RFC3986 3.2.2)"),
  382. Host = <<"example.org">>,
  383. #{code := 200, body := Host} = do_raw(Config,
  384. "GET http://example.org:8080/echo/host HTTP/1.1\r\n"
  385. "Host: example.org:8080\r\n"
  386. "\r\n"),
  387. Port = <<"8080">>,
  388. #{code := 200, body := Port} = do_raw(Config,
  389. "GET http://example.org:8080/echo/port HTTP/1.1\r\n"
  390. "Host: example.org:8080\r\n"
  391. "\r\n").
  392. absolute_form_limit_host(Config) ->
  393. doc("The maximum length for the host component of the URI must be subject "
  394. "to a configurable limit. A good default is 255 characters. "
  395. "(RFC7230 3.1.1, RFC3986 3.2.2, RFC1034 3.1)"),
  396. LongHost = ["host." || _ <- lists:seq(1, 100)],
  397. #{code := 414, client := Client} = do_raw(Config, [
  398. "GET http://", LongHost, "/ HTTP/1.1\r\n"
  399. "Host: ", LongHost, "\r\n"
  400. "\r\n"]),
  401. {error, closed} = raw_recv(Client, 0, 1000).
  402. absolute_form_invalid_port_0(Config) ->
  403. doc("Port number 0 is reserved. The request must be rejected and the connection closed."),
  404. #{code := 400, client := Client} = do_raw(Config,
  405. "GET http://localhost:0/ HTTP/1.1\r\n"
  406. "Host: localhost:0\r\n"
  407. "\r\n"),
  408. {error, closed} = raw_recv(Client, 0, 1000).
  409. absolute_form_invalid_port_65536(Config) ->
  410. doc("Port numbers above 65535 are invalid. The request must be rejected and the connection closed."),
  411. #{code := 400, client := Client} = do_raw(Config,
  412. "GET http://localhost:65536/ HTTP/1.1\r\n"
  413. "Host: localhost:65536\r\n"
  414. "\r\n"),
  415. {error, closed} = raw_recv(Client, 0, 1000).
  416. %% @todo The RFC says to discard the Host header if we are a proxy,
  417. %% and replace it with the content of absolute-form. This means
  418. %% that we should probably keep the absolute-form value when
  419. %% operating in proxy mode. Otherwise the absolute-form value
  420. %% is simply dropped and the Host header is used.
  421. %% @todo The authority is sent both in the URI and in the host header.
  422. %% The authority from the URI must be dropped, and the host header
  423. %% must be used instead. (RFC7230 5.5)
  424. %%
  425. %% It is not possible to test that the absolute-form value is dropped
  426. %% because one of the Host header test ensures that the authority
  427. %% is the same in both, and errors out otherwise.
  428. absolute_form_path(Config) ->
  429. doc("The path always starts with \"/\" and ends with either \"?\", \"#\" "
  430. "or the end of the URI. (RFC3986 3.3)"),
  431. Echo = <<"/echo/path">>,
  432. #{code := 200, body := Echo} = do_raw(Config,
  433. "GET http://localhost/echo/path HTTP/1.1\r\n"
  434. "Host: localhost\r\n"
  435. "\r\n").
  436. absolute_form_path_query(Config) ->
  437. doc("The path always starts with \"/\" and ends with either \"?\", \"#\" "
  438. "or the end of the URI. (RFC3986 3.3)"),
  439. Echo = <<"/echo/path">>,
  440. #{code := 200, body := Echo} = do_raw(Config,
  441. "GET http://localhost/echo/path?key=value HTTP/1.1\r\n"
  442. "Host: localhost\r\n"
  443. "\r\n").
  444. absolute_form_path_fragment(Config) ->
  445. doc("The path always starts with \"/\" and ends with either \"?\", \"#\" "
  446. "or the end of the URI. (RFC3986 3.3)"),
  447. Echo = <<"/echo/path">>,
  448. #{code := 200, body := Echo} = do_raw(Config,
  449. "GET http://localhost/echo/path#fragment HTTP/1.1\r\n"
  450. "Host: localhost\r\n"
  451. "\r\n").
  452. absolute_form_no_path(Config) ->
  453. doc("An empty path component is equivalent to \"/\". (RFC7230 2.7.3)"),
  454. #{code := 200, body := <<"Hello world!">>} = do_raw(Config,
  455. "GET http://localhost HTTP/1.1\r\n"
  456. "Host: localhost\r\n"
  457. "\r\n").
  458. absolute_form_no_path_then_query(Config) ->
  459. doc("An empty path component is equivalent to \"/\". (RFC7230 2.7.3)"),
  460. #{code := 200, body := <<"Hello world!">>} = do_raw(Config,
  461. "GET http://localhost?key=value HTTP/1.1\r\n"
  462. "Host: localhost\r\n"
  463. "\r\n").
  464. absolute_form_no_path_then_fragment(Config) ->
  465. doc("An empty path component is equivalent to \"/\". (RFC7230 2.7.3)"),
  466. #{code := 200, body := <<"Hello world!">>} = do_raw(Config,
  467. "GET http://localhost#fragment HTTP/1.1\r\n"
  468. "Host: localhost\r\n"
  469. "\r\n").
  470. absolute_form_query(Config) ->
  471. doc("The query starts with \"?\" and ends with \"#\" or the end of the URI. (RFC3986 3.4)"),
  472. Echo = <<"key=value">>,
  473. #{code := 200, body := Echo} = do_raw(Config,
  474. "GET http://localhost/echo/qs?key=value HTTP/1.1\r\n"
  475. "Host: localhost\r\n"
  476. "\r\n").
  477. absolute_form_query_fragment(Config) ->
  478. doc("The query starts with \"?\" and ends with \"#\" or the end of the URI. (RFC3986 3.4)"),
  479. Echo = <<"key=value">>,
  480. #{code := 200, body := Echo} = do_raw(Config,
  481. "GET http://localhost/echo/qs?key=value#fragment HTTP/1.1\r\n"
  482. "Host: localhost\r\n"
  483. "\r\n").
  484. %% @todo absolute_form: reject paths with too large depth or query strings with too many keys
  485. %% Request-target: authority-form.
  486. authority_form_reject_if_not_connect(Config) ->
  487. doc("When the method is CONNECT, authority-form must be used. This "
  488. "form does not apply to any other methods which must reject the "
  489. "request with a 400 status code and the closing of the connection. (RFC7230 5.3.3)"),
  490. #{code := 400, client := Client} = do_raw(Config,
  491. "GET localhost:80 HTTP/1.1\r\n"
  492. "Host: localhost\r\n"
  493. "\r\n"),
  494. {error, closed} = raw_recv(Client, 0, 1000).
  495. %% @todo Implement CONNECT.
  496. %authority_form_reject_userinfo(Config) ->
  497. %An authority component with a userinfo component (and its
  498. %"@" delimiter) is invalid. The request must be rejected with
  499. %a 400 status code and the closing of the connection. (RFC7230 2.7.1)
  500. %
  501. %authority_form_limit_host(Config) ->
  502. %authority_form_limit_port0(Config) ->
  503. %authority_form_limit_port65536(Config) ->
  504. %
  505. %A request with a too long component of authority-form must be rejected with
  506. %a 414 status code and the closing of the connection. (RFC7230 3.1.1)
  507. %
  508. %The authority is either resolved from configuration or is taken
  509. %directly from authority-form. (RFC7230 5.5)
  510. %
  511. %authority_form_empty_path(Config) ->
  512. %authority_form_empty_query(Config) ->
  513. %The path and query are empty when using authority-form. (RFC7230 5.5)
  514. %% Request-target: asterisk-form.
  515. asterisk_form_reject_if_not_options(Config) ->
  516. doc("asterisk-form is used for server-wide OPTIONS requests. "
  517. "It is invalid with any other methods which must reject the "
  518. "request with a 400 status code and the closing of the connection. (RFC7230 5.3.4)"),
  519. #{code := 400, client := Client} = do_raw(Config,
  520. "GET * HTTP/1.1\r\n"
  521. "Host: localhost\r\n"
  522. "\r\n"),
  523. {error, closed} = raw_recv(Client, 0, 1000).
  524. asterisk_form_empty_path(Config) ->
  525. doc("The path is empty when using asterisk-form. (RFC7230 5.5)"),
  526. #{code := 200, body := <<>>} = do_raw(Config,
  527. "OPTIONS * HTTP/1.1\r\n"
  528. "Host: localhost\r\n"
  529. "X-Echo: path\r\n"
  530. "\r\n").
  531. asterisk_form_empty_query(Config) ->
  532. doc("The query is empty when using asterisk-form. (RFC7230 5.5)"),
  533. #{code := 200, body := <<>>} = do_raw(Config,
  534. "OPTIONS * HTTP/1.1\r\n"
  535. "Host: localhost\r\n"
  536. "X-Echo: query\r\n"
  537. "\r\n").
  538. %% Invalid request-target.
  539. invalid_request_target(Config) ->
  540. doc("Any other form is invalid and must be rejected with a 400 status code "
  541. "and the closing of the connection."),
  542. #{code := 400, client := Client} = do_raw(Config,
  543. "GET \0 HTTP/1.1\r\n"
  544. "Host: localhost\r\n"
  545. "\r\n"),
  546. {error, closed} = raw_recv(Client, 0, 1000).
  547. %% Between request-target and version.
  548. reject_tab_between_request_target_and_version(Config) ->
  549. doc("A request that uses anything other than SP as separator between "
  550. "the request-target and the version must be rejected with a 400 "
  551. "status code and the closing of the connection. (RFC7230 3.1.1, RFC7230 3.5)"),
  552. #{code := 400, client := Client} = do_raw(Config,
  553. "GET /\tHTTP/1.1\r\n"
  554. "Host: localhost\r\n"
  555. "\r\n"),
  556. {error, closed} = raw_recv(Client, 0, 1000).
  557. reject_two_sp_between_request_target_and_version(Config) ->
  558. doc("A request that uses anything other than SP as separator between "
  559. "the request-target and the version must be rejected with a 400 "
  560. "status code and the closing of the connection. (RFC7230 3.1.1, RFC7230 3.5)"),
  561. #{code := 400, client := Client} = do_raw(Config,
  562. "GET / HTTP/1.1\r\n"
  563. "Host: localhost\r\n"
  564. "\r\n"),
  565. {error, closed} = raw_recv(Client, 0, 1000).
  566. %% Request version.
  567. reject_invalid_version_http100(Config) ->
  568. doc("Any version number other than HTTP/1.0 or HTTP/1.1 must be "
  569. "rejected by a server or intermediary with a 505 status code. (RFC7230 2.6, RFC7230 A.2)"),
  570. #{code := 505} = do_raw(Config,
  571. "GET / HTTP/1.00\r\n"
  572. "Host: localhost\r\n"
  573. "\r\n").
  574. reject_invalid_version_http111(Config) ->
  575. doc("Any version number other than HTTP/1.0 or HTTP/1.1 must be "
  576. "rejected by a server or intermediary with a 505 status code. (RFC7230 2.6, RFC7230 A.2)"),
  577. #{code := 505} = do_raw(Config,
  578. "GET / HTTP/1.11\r\n"
  579. "Host: localhost\r\n"
  580. "\r\n").
  581. reject_invalid_version_http12(Config) ->
  582. doc("Any version number other than HTTP/1.0 or HTTP/1.1 must be "
  583. "rejected by a server or intermediary with a 505 status code. (RFC7230 2.6, RFC7230 A.2)"),
  584. #{code := 505} = do_raw(Config,
  585. "GET / HTTP/1.2\r\n"
  586. "Host: localhost\r\n"
  587. "\r\n").
  588. reject_invalid_version_http2(Config) ->
  589. doc("Any version number other than HTTP/1.0 or HTTP/1.1 must be "
  590. "rejected by a server or intermediary with a 505 status code. (RFC7230 2.6, RFC7230 A.2)"),
  591. #{code := 505} = do_raw(Config,
  592. "GET / HTTP/2\r\n"
  593. "Host: localhost\r\n"
  594. "\r\n").
  595. reject_empty_version(Config) ->
  596. doc("Any version number other than HTTP/1.0 or HTTP/1.1 must be "
  597. "rejected by a server or intermediary with a 505 status code. (RFC7230 2.6, RFC7230 A.2)"),
  598. #{code := 505} = do_raw(Config,
  599. "GET / \r\n"
  600. "Host: localhost\r\n"
  601. "\r\n").
  602. reject_invalid_whitespace_after_version(Config) ->
  603. doc("A request that has whitespace different than CRLF following the "
  604. "version must be rejected with a 400 status code and the closing "
  605. "of the connection. (RFC7230 3.1.1)"),
  606. #{code := 400, client := Client} = do_raw(Config,
  607. "GET / HTTP/1.1 \r\n"
  608. "Host: localhost\r\n"
  609. "\r\n"),
  610. {error, closed} = raw_recv(Client, 0, 1000).
  611. %% Request headers.
  612. invalid_header_name(Config) ->
  613. doc("Header field names are tokens. (RFC7230 3.2)"),
  614. #{code := 400} = do_raw(Config, [
  615. "GET / HTTP/1.1\r\n"
  616. "Host\0: localhost\r\n"
  617. "\r\n"]).
  618. invalid_header_value(Config) ->
  619. doc("Header field values are made of printable characters, "
  620. "horizontal tab or space. (RFC7230 3.2)"),
  621. #{code := 400} = do_raw(Config, [
  622. "GET / HTTP/1.1\r\n"
  623. "Host: localhost\0rm rf the world\r\n"
  624. "\r\n"]).
  625. lower_case_header(Config) ->
  626. doc("The header field name is case insensitive. (RFC7230 3.2)"),
  627. #{code := 200} = do_raw(Config, [
  628. "GET / HTTP/1.1\r\n"
  629. "host: localhost\r\n"
  630. "\r\n"]).
  631. upper_case_header(Config) ->
  632. doc("The header field name is case insensitive. (RFC7230 3.2)"),
  633. #{code := 200} = do_raw(Config, [
  634. "GET / HTTP/1.1\r\n"
  635. "HOST: localhost\r\n"
  636. "\r\n"]).
  637. mixed_case_header(Config) ->
  638. doc("The header field name is case insensitive. (RFC7230 3.2)"),
  639. #{code := 200} = do_raw(Config, [
  640. "GET / HTTP/1.1\r\n"
  641. "hOsT: localhost\r\n"
  642. "\r\n"]).
  643. reject_whitespace_before_header_name(Config) ->
  644. doc("Messages that contain whitespace before the header name must "
  645. "be rejected with a 400 status code and the closing of the "
  646. "connection. (RFC7230 3.2.4)"),
  647. #{code := 400, client := Client} = do_raw(Config, [
  648. "GET / HTTP/1.1\r\n"
  649. " Host: localhost\r\n"
  650. "\r\n"]),
  651. {error, closed} = raw_recv(Client, 0, 1000).
  652. reject_whitespace_between_header_name_and_colon(Config) ->
  653. doc("Messages that contain whitespace between the header name and "
  654. "colon must be rejected with a 400 status code and the closing "
  655. "of the connection. (RFC7230 3.2.4)"),
  656. #{code := 400, client := Client} = do_raw(Config, [
  657. "GET / HTTP/1.1\r\n"
  658. "Host : localhost\r\n"
  659. "\r\n"]),
  660. {error, closed} = raw_recv(Client, 0, 1000).
  661. limit_header_name(Config) ->
  662. doc("The header name must be subject to a configurable limit. A "
  663. "good default is 50 characters, well above the longest registered "
  664. "header. Such a request must be rejected with a 431 status code "
  665. "and the closing of the connection. "
  666. "(RFC7230 3.2.5, RFC6585 5, IANA Message Headers registry)"),
  667. #{code := 431, client := Client} = do_raw(Config, [
  668. "GET / HTTP/1.1\r\n"
  669. "Host: localhost\r\n",
  670. binary:copy(<<$a>>, 32768), ": bad\r\n"
  671. "\r\n"]),
  672. {error, closed} = raw_recv(Client, 0, 1000).
  673. limit_header_value(Config) ->
  674. doc("The header value and the optional whitespace around it must be "
  675. "subject to a configurable limit. There is no recommendations "
  676. "for the default. 4096 characters is known to work well. Such "
  677. "a request must be rejected with a 431 status code and the closing "
  678. "of the connection. (RFC7230 3.2.5, RFC6585 5)"),
  679. #{code := 431, client := Client} = do_raw(Config, [
  680. "GET / HTTP/1.1\r\n"
  681. "Host: localhost\r\n"
  682. "bad: ", binary:copy(<<$a>>, 32768), "\r\n"
  683. "\r\n"]),
  684. {error, closed} = raw_recv(Client, 0, 1000).
  685. drop_whitespace_before_header_value(Config) ->
  686. doc("Optional whitespace before and after the header value is not "
  687. "part of the value and must be dropped."),
  688. #{code := 200} = do_raw(Config, [
  689. "POST / HTTP/1.1\r\n"
  690. "Host: localhost\r\n"
  691. "Content-length: \t 12\r\n"
  692. "\r\n"
  693. "Hello world!"]).
  694. drop_whitespace_after_header_value(Config) ->
  695. doc("Optional whitespace before and after the header value is not "
  696. "part of the value and must be dropped."),
  697. #{code := 200} = do_raw(Config, [
  698. "POST / HTTP/1.1\r\n"
  699. "Host: localhost\r\n"
  700. "Content-length: 12 \t \r\n"
  701. "\r\n"
  702. "Hello world!"]).
  703. %@todo
  704. %The order of header fields with differing names is not significant. (RFC7230 3.2.2)
  705. %
  706. %@todo
  707. %The normal procedure for parsing headers is to read each header
  708. %field into a hash table by field name until the empty line. (RFC7230 3)
  709. reject_duplicate_content_length_header(Config) ->
  710. doc("Requests with duplicate content-length headers must be rejected "
  711. "with a 400 status code and the closing of the connection. (RFC7230 3.3.2)"),
  712. #{code := 400, client := Client} = do_raw(Config, [
  713. "POST / HTTP/1.1\r\n"
  714. "Host: localhost\r\n"
  715. "Content-length: 12\r\n"
  716. "Content-length: 12\r\n"
  717. "\r\n"
  718. "Hello world!"]),
  719. {error, closed} = raw_recv(Client, 0, 1000).
  720. reject_duplicate_host_header(Config) ->
  721. doc("Requests with duplicate host headers must be rejected "
  722. "with a 400 status code and the closing of the connection. (RFC7230 3.3.2)"),
  723. #{code := 400, client := Client} = do_raw(Config, [
  724. "POST / HTTP/1.1\r\n"
  725. "Host: localhost\r\n"
  726. "Host: localhost\r\n"
  727. "\r\n"
  728. "Hello world!"]),
  729. {error, closed} = raw_recv(Client, 0, 1000).
  730. combine_duplicate_headers(Config) ->
  731. doc("Other duplicate header fields must be combined by inserting a comma "
  732. "between the values in the order they were received. (RFC7230 3.2.2)"),
  733. #{code := 200, body := Body} = do_raw(Config, [
  734. "GET /echo/headers HTTP/1.1\r\n"
  735. "Host: localhost\r\n"
  736. "Accept-encoding: gzip\r\n"
  737. "Accept-encoding: brotli\r\n"
  738. "\r\n"]),
  739. <<"#{<<\"accept-encoding\">> => <<\"gzip, brotli\">>,", _/bits>> = Body,
  740. ok.
  741. %Duplicate header field names are only allowed when their value is
  742. %a comma-separated list. In practice there is no need to perform
  743. %a check while reading the headers as the value will become invalid
  744. %and the error can be handled while parsing the header later on. (RFC7230 3.2.2)
  745. %
  746. %wait_for_eoh_before_processing_request(Config) ->
  747. %The request must not be processed until all headers have arrived. (RFC7230 3.2.2)
  748. limit_headers(Config) ->
  749. doc("The number of headers allowed in a request must be subject to "
  750. "a configurable limit. There is no recommendations for the default. "
  751. "100 headers is known to work well. Such a request must be rejected "
  752. "with a 431 status code and the closing of the connection. (RFC7230 3.2.5, RFC6585 5)"),
  753. %% 100 headers.
  754. #{code := 200} = do_raw(Config, [
  755. "GET / HTTP/1.1\r\n"
  756. "Host: localhost\r\n",
  757. [["H-", integer_to_list(N), ": value\r\n"] || N <- lists:seq(1, 99)],
  758. "\r\n"]),
  759. %% 101 headers.
  760. #{code := 431, client := Client} = do_raw(Config, [
  761. "GET / HTTP/1.1\r\n"
  762. "Host: localhost\r\n",
  763. [["H-", integer_to_list(N), ": value\r\n"] || N <- lists:seq(1, 100)],
  764. "\r\n"]),
  765. {error, closed} = raw_recv(Client, 0, 1000).
  766. %ignore_header_empty_list_elements(Config) ->
  767. %When parsing header field values, the server must ignore empty
  768. %list elements, and not count those as the count of elements present. (RFC7230 7)
  769. %
  770. %@todo
  771. %The information in the via header is largely unreliable. (RFC7230 5.7.1)
  772. %% Request body.
  773. %@todo
  774. %The message body is the octets after decoding any transfer
  775. %codings. (RFC7230 3.3)
  776. no_request_body(Config) ->
  777. doc("A request has a message body only if it includes a transfer-encoding "
  778. "header or a non-zero content-length header. (RFC7230 3.3)"),
  779. #{code := 200, body := <<"false">>} = do_raw(Config, [
  780. "POST /echo/has_body HTTP/1.1\r\n"
  781. "Host: localhost\r\n"
  782. "\r\n"]),
  783. #{code := 200, body := <<>>} = do_raw(Config, [
  784. "POST /echo/read_body HTTP/1.1\r\n"
  785. "Host: localhost\r\n"
  786. "\r\n"]),
  787. ok.
  788. no_request_body_content_length_zero(Config) ->
  789. doc("A request has a message body only if it includes a transfer-encoding "
  790. "header or a non-zero content-length header. (RFC7230 3.3)"),
  791. #{code := 200, body := <<"false">>} = do_raw(Config, [
  792. "POST /echo/has_body HTTP/1.1\r\n"
  793. "Host: localhost\r\n"
  794. "Content-length: 0\r\n"
  795. "\r\n"]),
  796. #{code := 200, body := <<>>} = do_raw(Config, [
  797. "POST /echo/read_body HTTP/1.1\r\n"
  798. "Host: localhost\r\n"
  799. "Content-length: 0\r\n"
  800. "\r\n"]),
  801. ok.
  802. request_body_content_length(Config) ->
  803. doc("A request has a message body only if it includes a transfer-encoding "
  804. "header or a non-zero content-length header. (RFC7230 3.3)"),
  805. #{code := 200, body := <<"true">>} = do_raw(Config, [
  806. "POST /echo/has_body HTTP/1.1\r\n"
  807. "Host: localhost\r\n"
  808. "Content-length: 12\r\n"
  809. "\r\n"
  810. "Hello world!"]),
  811. #{code := 200, body := <<"Hello world!">>} = do_raw(Config, [
  812. "POST /echo/read_body HTTP/1.1\r\n"
  813. "Host: localhost\r\n"
  814. "Content-length: 12\r\n"
  815. "\r\n"
  816. "Hello world!"]),
  817. ok.
  818. request_body_transfer_encoding(Config) ->
  819. doc("A request has a message body only if it includes a transfer-encoding "
  820. "header or a non-zero content-length header. (RFC7230 3.3)"),
  821. #{code := 200, body := <<"true">>} = do_raw(Config, [
  822. "POST /echo/has_body HTTP/1.1\r\n"
  823. "Host: localhost\r\n"
  824. "Transfer-encoding: chunked\r\n"
  825. "\r\n"
  826. "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  827. #{code := 200, body := <<"Hello world!">>} = do_raw(Config, [
  828. "POST /echo/read_body HTTP/1.1\r\n"
  829. "Host: localhost\r\n"
  830. "Transfer-encoding: chunked\r\n"
  831. "\r\n"
  832. "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  833. ok.
  834. %```
  835. %Transfer-Encoding = 1#transfer-coding
  836. %
  837. %transfer-coding = "chunked" / "compress" / "deflate" / "gzip" / transfer-extension
  838. %transfer-extension = token *( OWS ";" OWS transfer-parameter )
  839. %transfer-parameter = token BWS "=" BWS ( token / quoted-string )
  840. %```
  841. case_insensitive_transfer_encoding(Config) ->
  842. doc("The transfer-coding is case insensitive. (RFC7230 4)"),
  843. #{code := 200, body := <<"Hello world!">>} = do_raw(Config, [
  844. "POST /echo/read_body HTTP/1.1\r\n"
  845. "Host: localhost\r\n"
  846. "Transfer-encoding: ChUnKeD\r\n"
  847. "\r\n"
  848. "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  849. ok.
  850. %@todo
  851. %There are no known other transfer-extension with the exception of
  852. %deprecated aliases "x-compress" and "x-gzip". (IANA HTTP Transfer Coding Registry,
  853. %RFC7230 4.2.1, RFC7230 4.2.3, RFC7230 8.4.2)
  854. %% This is the exact same test as request_body_transfer_encoding.
  855. must_understand_chunked(Config) ->
  856. doc("A server must be able to handle at least chunked transfer-encoding. "
  857. "This is also the only coding that sees widespread use. (RFC7230 3.3.1, RFC7230 4.1)"),
  858. #{code := 200, body := <<"Hello world!">>} = do_raw(Config, [
  859. "POST /echo/read_body HTTP/1.1\r\n"
  860. "Host: localhost\r\n"
  861. "Transfer-encoding: chunked\r\n"
  862. "\r\n"
  863. "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  864. ok.
  865. reject_double_chunked_encoding(Config) ->
  866. doc("Messages encoded more than once with chunked transfer-encoding "
  867. "must be rejected with a 400 status code and the closing of the "
  868. "connection. (RFC7230 3.3.1)"),
  869. #{code := 400, client := Client} = do_raw(Config, [
  870. "POST / HTTP/1.1\r\n"
  871. "Host: localhost\r\n"
  872. "Transfer-encoding: chunked, chunked\r\n"
  873. "\r\n"
  874. "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"]),
  875. {error, closed} = raw_recv(Client, 0, 1000).
  876. reject_non_terminal_chunked(Config) ->
  877. doc("Messages where chunked, when present, is not the last "
  878. "transfer-encoding must be rejected with a 400 status code "
  879. "and the closing of the connection. (RFC7230 3.3.3)"),
  880. #{code := 400, client := Client} = do_raw(Config, [
  881. "POST / HTTP/1.1\r\n"
  882. "Host: localhost\r\n"
  883. "Transfer-encoding: chunked, gzip\r\n"
  884. "\r\n",
  885. zlib:gzip(<<"6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n">>)]),
  886. {error, closed} = raw_recv(Client, 0, 1000).
  887. %@todo
  888. %Some non-conformant implementations send the "deflate" compressed
  889. %data without the zlib wrapper. (RFC7230 4.2.2)
  890. reject_unknown_transfer_encoding(Config) ->
  891. doc("Messages encoded with a transfer-encoding the server does not "
  892. "understand must be rejected with a 501 status code and the "
  893. "closing of the connection. (RFC7230 3.3.1)"),
  894. #{code := 400, client := Client} = do_raw(Config, [
  895. "POST / HTTP/1.1\r\n"
  896. "Host: localhost\r\n"
  897. "Transfer-encoding: unknown, chunked\r\n"
  898. "\r\n",
  899. "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  900. {error, closed} = raw_recv(Client, 0, 1000).
  901. %@todo
  902. %A server may reject requests with a body and no content-length
  903. %header with a 411 status code. (RFC7230 3.3.3)
  904. %```
  905. %Content-Length = 1*DIGIT
  906. %```
  907. reject_invalid_content_length(Config) ->
  908. doc("A request with an invalid content-length header must be rejected "
  909. "with a 400 status code and the closing of the connection. (RFC7230 3.3.3)"),
  910. #{code := 400, client := Client1} = do_raw(Config, [
  911. "POST / HTTP/1.1\r\n"
  912. "Host: localhost\r\n"
  913. "Content-length: 12,12\r\n"
  914. "\r\n"
  915. "Hello world!"]),
  916. {error, closed} = raw_recv(Client1, 0, 1000),
  917. #{code := 400, client := Client2} = do_raw(Config, [
  918. "POST / HTTP/1.1\r\n"
  919. "Host: localhost\r\n"
  920. "Content-length: NaN\r\n"
  921. "\r\n"
  922. "Hello world!"]),
  923. {error, closed} = raw_recv(Client2, 0, 1000).
  924. %@todo
  925. %The content-length header ranges from 0 to infinity. Requests
  926. %with a message body too large must be rejected with a 413 status
  927. %code and the closing of the connection. (RFC7230 3.3.2)
  928. ignore_content_length_when_transfer_encoding(Config) ->
  929. doc("When a message includes both transfer-encoding and content-length "
  930. "headers, the content-length header must be removed before processing "
  931. "the request. (RFC7230 3.3.3)"),
  932. #{code := 200, body := <<"Hello world!">>} = do_raw(Config, [
  933. "POST /echo/read_body HTTP/1.1\r\n"
  934. "Host: localhost\r\n"
  935. "Transfer-encoding: chunked\r\n"
  936. "Content-length: 12\r\n"
  937. "\r\n"
  938. "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  939. ok.
  940. %socket_error_while_reading_body(Config) ->
  941. %If a socket error occurs while reading the body the server
  942. %must send a 400 status code response and close the connection. (RFC7230 3.3.3, RFC7230 3.4)
  943. %
  944. %timeout_while_reading_body(Config) ->
  945. %If a timeout occurs while reading the body the server must
  946. %send a 408 status code response and close the connection. (RFC7230 3.3.3, RFC7230 3.4)
  947. %% Body length.
  948. body_length_chunked_before(Config) ->
  949. doc("The length of a message with a transfer-encoding header can "
  950. "only be determined on decoding completion. (RFC7230 3.3.3)"),
  951. #{code := 200, body := <<"undefined">>} = do_raw(Config, [
  952. "POST /echo/body_length HTTP/1.1\r\n"
  953. "Host: localhost\r\n"
  954. "Transfer-encoding: chunked\r\n"
  955. "\r\n"
  956. "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  957. ok.
  958. body_length_chunked_after(Config) ->
  959. doc("Upon completion of chunk decoding the server must add a content-length "
  960. "header with the value set to the total length of data read. (RFC7230 4.1.3)"),
  961. #{code := 200, body := <<"12">>} = do_raw(Config, [
  962. "POST /length/echo/read_body HTTP/1.1\r\n"
  963. "Host: localhost\r\n"
  964. "Transfer-encoding: chunked\r\n"
  965. "\r\n"
  966. "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  967. ok.
  968. body_length_content_length(Config) ->
  969. doc("The length of a message with a content-length header is "
  970. "the numeric value in octets found in the header. (RFC7230 3.3.3)"),
  971. #{code := 200, body := <<"12">>} = do_raw(Config, [
  972. "POST /echo/body_length HTTP/1.1\r\n"
  973. "Host: localhost\r\n"
  974. "Content-length: 12\r\n"
  975. "\r\n"
  976. "Hello world!"]),
  977. ok.
  978. body_length_zero(Config) ->
  979. doc("A message with no transfer-encoding or content-length header "
  980. "has a body length of 0. (RFC7230 3.3.3)"),
  981. #{code := 200, body := <<"0">>} = do_raw(Config, [
  982. "POST /echo/body_length HTTP/1.1\r\n"
  983. "Host: localhost\r\n"
  984. "\r\n"]),
  985. ok.
  986. %% Chunked transfer-encoding.
  987. reject_invalid_chunk_size(Config) ->
  988. doc("A request with an invalid chunk size must be rejected "
  989. "with a 400 status code and the closing of the connection. (RFC7230 4.1)"),
  990. #{code := 400, client := Client} = do_raw(Config, [
  991. "POST /echo/read_body HTTP/1.1\r\n"
  992. "Host: localhost\r\n"
  993. "Transfer-encoding: chunked\r\n"
  994. "\r\n"
  995. "6\r\nHello \r\nFIVE\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  996. {error, closed} = raw_recv(Client, 0, 1000).
  997. %```
  998. %chunked-body = *chunk last-chunk trailer-part CRLF
  999. %
  1000. %chunk = chunk-size [ chunk-ext ] CRLF chunk-data CRLF
  1001. %chunk-size = 1*HEXDIG
  1002. %chunk-data = 1*OCTET ; a sequence of chunk-size octets
  1003. %
  1004. %last-chunk = 1*("0") [ chunk-ext ] CRLF
  1005. %```
  1006. %
  1007. %The chunk-size field is a string of hex digits indicating the size of
  1008. %the chunk-data in octets.
  1009. %
  1010. %```
  1011. %chunk-ext = *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
  1012. %chunk-ext-name = token
  1013. %chunk-ext-val = token / quoted-string
  1014. %```
  1015. ignore_unknown_chunk_extensions(Config) ->
  1016. doc("Unknown chunk extensions must be ignored. (RFC7230 4.1.1)"),
  1017. #{code := 200, body := <<"Hello world!">>} = do_raw(Config, [
  1018. "POST /echo/read_body HTTP/1.1\r\n"
  1019. "Host: localhost\r\n"
  1020. "Transfer-encoding: chunked\r\n"
  1021. "\r\n"
  1022. "6; hello=\"cool world\"\r\nHello \r\n"
  1023. "5 ; one ; two ; three;four;five\r\nworld"
  1024. "\r\n1;ok\r\n!\r\n0\r\n\r\n"]),
  1025. ok.
  1026. %% Since we skip everything right now, the only reason
  1027. %% we might reject chunk extensions is if they are too large.
  1028. limit_chunk_size_line(Config) ->
  1029. doc("A request with chunk extensions larger than the server allows must be rejected "
  1030. "with a 400 status code and the closing of the connection. (RFC7230 4.1.1)"),
  1031. #{code := 200, body := <<"Hello world!">>} = do_raw(Config, [
  1032. "POST /echo/read_body HTTP/1.1\r\n"
  1033. "Host: localhost\r\n"
  1034. "Transfer-encoding: chunked\r\n"
  1035. "\r\n"
  1036. "6; hello=\"cool world\"\r\nHello \r\n"
  1037. "5;", lists:duplicate(128, $a), "\r\nworld"
  1038. "\r\n1;ok\r\n!\r\n0\r\n\r\n"]),
  1039. #{code := 400, client := Client} = do_raw(Config, [
  1040. "POST /echo/read_body HTTP/1.1\r\n"
  1041. "Host: localhost\r\n"
  1042. "Transfer-encoding: chunked\r\n"
  1043. "\r\n"
  1044. "6; hello=\"cool world\"\r\nHello \r\n"
  1045. "5;", lists:duplicate(129, $a), "\r\nworld"
  1046. "\r\n1;ok\r\n!\r\n0\r\n\r\n"]),
  1047. {error, closed} = raw_recv(Client, 0, 1000).
  1048. reject_invalid_chunk_size_crlf(Config) ->
  1049. doc("A request with an invalid line break after the chunk size must be rejected "
  1050. "with a 400 status code and the closing of the connection. (RFC7230 4.1)"),
  1051. #{code := 400, client := Client1} = do_raw(Config, [
  1052. "POST /echo/read_body HTTP/1.1\r\n"
  1053. "Host: localhost\r\n"
  1054. "Transfer-encoding: chunked\r\n"
  1055. "\r\n"
  1056. "6\rHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  1057. {error, closed} = raw_recv(Client1, 0, 1000),
  1058. #{code := 400, client := Client2} = do_raw(Config, [
  1059. "POST /echo/read_body HTTP/1.1\r\n"
  1060. "Host: localhost\r\n"
  1061. "Transfer-encoding: chunked\r\n"
  1062. "\r\n"
  1063. "6\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  1064. {error, closed} = raw_recv(Client2, 0, 1000),
  1065. #{code := 400, client := Client3} = do_raw(Config, [
  1066. "POST /echo/read_body HTTP/1.1\r\n"
  1067. "Host: localhost\r\n"
  1068. "Transfer-encoding: chunked\r\n"
  1069. "\r\n"
  1070. "6Hello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  1071. {error, closed} = raw_recv(Client3, 0, 1000).
  1072. reject_invalid_chunk_ext_crlf(Config) ->
  1073. doc("A request with an invalid line break after chunk extensions must be rejected "
  1074. "with a 400 status code and the closing of the connection. (RFC7230 4.1)"),
  1075. #{code := 400, client := Client1} = do_raw(Config, [
  1076. "POST /echo/read_body HTTP/1.1\r\n"
  1077. "Host: localhost\r\n"
  1078. "Transfer-encoding: chunked\r\n"
  1079. "\r\n"
  1080. "6; extensions\rHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  1081. {error, closed} = raw_recv(Client1, 0, 1000),
  1082. #{code := 400, client := Client2} = do_raw(Config, [
  1083. "POST /echo/read_body HTTP/1.1\r\n"
  1084. "Host: localhost\r\n"
  1085. "Transfer-encoding: chunked\r\n"
  1086. "\r\n"
  1087. "6; extensions\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  1088. {error, closed} = raw_recv(Client2, 0, 1000),
  1089. #{code := 400, client := Client3} = do_raw(Config, [
  1090. "POST /echo/read_body HTTP/1.1\r\n"
  1091. "Host: localhost\r\n"
  1092. "Transfer-encoding: chunked\r\n"
  1093. "\r\n"
  1094. "6; extensionsHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  1095. {error, closed} = raw_recv(Client3, 0, 1000).
  1096. reject_invalid_chunk_data_crlf(Config) ->
  1097. doc("A request with an invalid line break after the chunk data must be rejected "
  1098. "with a 400 status code and the closing of the connection. (RFC7230 4.1)"),
  1099. #{code := 400, client := Client1} = do_raw(Config, [
  1100. "POST /echo/read_body HTTP/1.1\r\n"
  1101. "Host: localhost\r\n"
  1102. "Transfer-encoding: chunked\r\n"
  1103. "\r\n"
  1104. "6\r\nHello \r5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  1105. {error, closed} = raw_recv(Client1, 0, 1000),
  1106. #{code := 400, client := Client2} = do_raw(Config, [
  1107. "POST /echo/read_body HTTP/1.1\r\n"
  1108. "Host: localhost\r\n"
  1109. "Transfer-encoding: chunked\r\n"
  1110. "\r\n"
  1111. "6\r\nHello \n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  1112. {error, closed} = raw_recv(Client2, 0, 1000),
  1113. #{code := 400, client := Client3} = do_raw(Config, [
  1114. "POST /echo/read_body HTTP/1.1\r\n"
  1115. "Host: localhost\r\n"
  1116. "Transfer-encoding: chunked\r\n"
  1117. "\r\n"
  1118. "6\r\nHello 5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  1119. {error, closed} = raw_recv(Client3, 0, 1000).
  1120. %```
  1121. %trailer-part = *( header-field CRLF )
  1122. %```
  1123. %
  1124. %%% @todo see headers above and reject the same way, space etc.
  1125. %reject_invalid_request_trailer(Config) ->
  1126. %
  1127. %ignore_request_trailer_transfer_encoding(Config) ->
  1128. %ignore_request_trailer_content_length(Config) ->
  1129. %ignore_request_trailer_host(Config) ->
  1130. %ignore_request_trailer_cache_control(Config) ->
  1131. %ignore_request_trailer_expect(Config) ->
  1132. %ignore_request_trailer_max_forwards(Config) ->
  1133. %ignore_request_trailer_pragma(Config) ->
  1134. %ignore_request_trailer_range(Config) ->
  1135. %ignore_request_trailer_te(Config) ->
  1136. %ignore_request_trailer_if_match(Config) ->
  1137. %ignore_request_trailer_if_none_match(Config) ->
  1138. %ignore_request_trailer_if_modified_since(Config) ->
  1139. %ignore_request_trailer_if_unmodified_since(Config) ->
  1140. %ignore_request_trailer_if_range(Config) ->
  1141. %ignore_request_trailer_www_authenticate(Config) ->
  1142. %ignore_request_trailer_authorization(Config) ->
  1143. %ignore_request_trailer_proxy_authenticate(Config) ->
  1144. %ignore_request_trailer_proxy_authorization(Config) ->
  1145. %ignore_request_trailer_content_encoding(Config) ->
  1146. %ignore_request_trailer_content_type(Config) ->
  1147. %ignore_request_trailer_content_range(Config) ->
  1148. %ignore_request_trailer_trailer(Config) ->
  1149. %
  1150. %ignore_response_trailer_header(Config, Header) ->
  1151. %Trailing headers must not include transfer-encoding, content-length,
  1152. %host, cache-control, expect, max-forwards, pragma, range, te,
  1153. %if-match, if-none-match, if-modified-since, if-unmodified-since,
  1154. %if-range, www-authenticate, authorization, proxy-authenticate,
  1155. %proxy-authorization, age, cache-control, expires, date, location,
  1156. %retry-after, vary, warning, content-encoding, content-type,
  1157. %content-range, or trailer. (RFC7230 4.1.2)
  1158. %
  1159. %When trailer headers are processed, invalid headers must be ignored.
  1160. %Valid headers must be added to the list of headers of the request. (RFC7230 4.1.2)
  1161. %
  1162. %ignore_request_trailers(Config) ->
  1163. %Trailer headers can be ignored safely. (RFC7230 4.1.2)
  1164. %
  1165. %limit_request_trailer_headers(Config) ->
  1166. %The number of trailer headers must be subject to configuration.
  1167. %There is no known recommendations for the default. A value of 10
  1168. %should cover most cases. Requests with too many trailer headers
  1169. %must be rejected with a 431 status code and the closing of the
  1170. %connection. (RFC6585 5)
  1171. %% We remove the header immediately so there's no need
  1172. %% to try to read the body before checking.
  1173. remove_transfer_encoding_chunked_after_body_read(Config) ->
  1174. doc("Upon completion of chunk decoding the server must remove \"chunked\" "
  1175. "from the transfer-encoding header. This header must be removed if "
  1176. "it becomes empty following this removal. (RFC7230 4.1.3)"),
  1177. #{code := 200, body := <<"undefined">>} = do_raw(Config, [
  1178. "POST /echo/header/transfer-encoding HTTP/1.1\r\n"
  1179. "Host: localhost\r\n"
  1180. "Transfer-encoding: chunked\r\n"
  1181. "\r\n"
  1182. "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  1183. ok.
  1184. %remove_trailer_after_body_read(Config) ->
  1185. %Upon completion of chunk decoding the server must remove the trailer
  1186. %header from the list of headers. (RFC7230 4.1.3)
  1187. %
  1188. %```
  1189. %Trailer = 1#field-name
  1190. %```
  1191. %
  1192. %ignore_chunked_headers_not_in_trailer(Config) ->
  1193. %The trailer header can be used to list the headers found in the
  1194. %trailer. A server must have the option of ignoring trailer headers
  1195. %that were not listed in the trailer header. (RFC7230 4.4)
  1196. %
  1197. %ignore_chunked_headers_if_trailer_not_in_connection(Config) ->
  1198. %The trailer header must be listed in the connection header field.
  1199. %Trailers must be ignored otherwise.
  1200. %
  1201. %%% @todo Though we need a compatibility mode as some clients don't send it...
  1202. %reject_chunked_missing_end_crlf(Config) ->
  1203. %@todo ending CRLF
  1204. %% Connection management.
  1205. %@todo can probably test using auth
  1206. %Never assume any two requests on a single connection come
  1207. %from the same user agent. (RFC7230 2.3)
  1208. %
  1209. %```
  1210. %Connection = 1#token ; case-insensitive
  1211. %```
  1212. %
  1213. %The connection token is either case insensitive "close", "keep-alive"
  1214. %or a header field name.
  1215. %
  1216. %There are no corresponding "close" or "keep-alive" headers. (RFC7230 8.1, RFC7230 A.2)
  1217. %
  1218. %The connection header is valid only for the immediate connection,
  1219. %alongside any header field it lists. (RFC7230 6.1)
  1220. %
  1221. %The server must determine if the connection is persistent for
  1222. %every message received by looking at the connection header and
  1223. %HTTP version. (RFC7230 6.3)
  1224. no_connection_header_keepalive(Config) ->
  1225. doc("HTTP/1.1 requests with no \"close\" option and HTTP/1.0 with the "
  1226. "\"keep-alive\" option indicate the connection will persist. (RFC7230 6.1, RFC7230 6.3)"),
  1227. #{code := 200, client := Client} = do_raw(Config, [
  1228. "GET / HTTP/1.1\r\n"
  1229. "Host: localhost\r\n"
  1230. "\r\n"]),
  1231. {error, timeout} = raw_recv(Client, 0, 1000).
  1232. %% @todo http/1.0 suite? connection_keepalive(Config) ->
  1233. connection_close(Config) ->
  1234. doc("HTTP/1.1 requests with the \"close\" option and HTTP/1.0 with no "
  1235. "\"keep-alive\" option indicate the connection will be closed "
  1236. "upon reception of the response by the client. (RFC7230 6.1, RFC7230 6.3)"),
  1237. #{code := 200, client := Client} = do_raw(Config, [
  1238. "GET / HTTP/1.1\r\n"
  1239. "Host: localhost\r\n"
  1240. "Connection: close\r\n"
  1241. "\r\n"]),
  1242. {error, closed} = raw_recv(Client, 0, 1000).
  1243. %% @todo http/1.0 suite? no_connection_close(Config) ->
  1244. limit_requests_keepalive(Config) ->
  1245. doc("The maximum number of requests sent using a persistent connection "
  1246. "must be subject to configuration. The connection must be closed "
  1247. "when the limit is reached. (RFC7230 6.3)"),
  1248. ConnPid = gun_open(Config),
  1249. _ = [begin
  1250. Ref = gun:get(ConnPid, "/"),
  1251. {response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref),
  1252. {ok, <<"Hello world!">>} = gun:await_body(ConnPid, Ref),
  1253. false = lists:keyfind(<<"connection">>, 1, RespHeaders)
  1254. end || _ <- lists:seq(1,99)],
  1255. %% Final request closes the connection.
  1256. Ref = gun:get(ConnPid, "/"),
  1257. {response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref),
  1258. {ok, <<"Hello world!">>} = gun:await_body(ConnPid, Ref),
  1259. {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, RespHeaders),
  1260. ok.
  1261. %skip_request_body_by_closing_connection(Config) ->
  1262. %%A server that doesn't want to read the entire body of a message
  1263. %%must close the connection, if possible after sending the "close"
  1264. %%connection option in the response. (RFC7230 6.3)
  1265. %
  1266. %pipeline(Config) ->
  1267. %%% @todo pipeline_parallel (safe methods can, others can't)
  1268. %A server can receive more than one request before any response
  1269. %is sent. This is called pipelining. The requests can be processed
  1270. %in parallel if they all have safe methods. Responses must be sent
  1271. %in the same order as the requests. (RFC7230 6.3.2)
  1272. %
  1273. %@todo
  1274. %The server must reject abusive traffic by closing the connection.
  1275. %Abusive traffic can come from the form of too many requests in a
  1276. %given amount of time, or too many concurrent connections. Limits
  1277. %must be subject to configuration. (RFC7230 6.4)
  1278. %
  1279. %close_inactive_connections(Config) ->
  1280. %The server must close inactive connections. The timeout
  1281. %must be subject to configuration. (RFC7230 6.5)
  1282. %
  1283. %@todo
  1284. %The server must monitor connections for the close signal
  1285. %and close the socket on its end accordingly. (RFC7230 6.5)
  1286. %
  1287. %@todo
  1288. %A connection close may occur at any time. (RFC7230 6.5)
  1289. %
  1290. %ignore_requests_after_connection_close(Config) ->
  1291. %The server must not process any request after sending or
  1292. %receiving the "close" connection option. (RFC7230 6.6)
  1293. %
  1294. %@todo
  1295. %The server must close the connection in stages to avoid the
  1296. %TCP reset problem. The server starts by closing the write
  1297. %side of the socket. The server then reads until it detects
  1298. %the socket has been closed, until it can be certain its
  1299. %last response has been received by the client, or until
  1300. %a close or timeout occurs. The server then fully close the
  1301. %connection. (6.6)
  1302. %
  1303. %%% Routing.
  1304. %
  1305. %```
  1306. %Host = authority ; same as authority-form
  1307. %```
  1308. %
  1309. %reject_missing_host(Config) ->
  1310. %An HTTP/1.1 request that lacks a host header must be rejected with
  1311. %a 400 status code and the closing of the connection. (RFC7230 5.4)
  1312. %
  1313. %%% @todo http/1.0 missing_host(Config) ->
  1314. %An HTTP/1.0 request that lack a host header is valid. Behavior
  1315. %for these requests is configuration dependent. (RFC7230 5.5)
  1316. %
  1317. %reject_invalid_host(Config) ->
  1318. %A request with an invalid host header must be rejected with a
  1319. %400 status code and the closing of the connection. (RFC7230 5.4)
  1320. %
  1321. %reject_userinfo(Config) ->
  1322. %An authority component with a userinfo component (and its
  1323. %"@" delimiter) is invalid. The request must be rejected with
  1324. %a 400 status code and the closing of the connection. (RFC7230 2.7.1)
  1325. %
  1326. %reject_absolute_form_different_host(Config) ->
  1327. %When using absolute-form the URI authority component must be
  1328. %identical to the host header. Invalid requests must be rejected
  1329. %with a 400 status code and the closing of the connection. (RFC7230 5.4)
  1330. %
  1331. %reject_authority_form_different_host(Config) ->
  1332. %When using authority-form the URI authority component must be
  1333. %identical to the host header. Invalid requests must be rejected
  1334. %with a 400 status code and the closing of the connection.
  1335. %
  1336. %empty_host(Config) ->
  1337. %The host header is empty when the authority component is undefined. (RFC7230 5.4)
  1338. %
  1339. %@todo
  1340. %The effective request URI can be rebuilt by concatenating scheme,
  1341. %"://", authority, path and query components. (RFC7230 5.5)
  1342. %
  1343. %@todo
  1344. %Resources with identical URI except for the scheme component
  1345. %must be treated as different. (RFC7230 2.7.2)
  1346. %
  1347. %%% Response.
  1348. %
  1349. %@todo
  1350. %A server can send more than one response per request only when a
  1351. %1xx response is sent preceding the final response. (RFC7230 5.6)
  1352. %
  1353. %@todo
  1354. %A server that does parallel pipelining must send responses in the
  1355. %same order as the requests came in. (RFC7230 5.6)
  1356. %
  1357. %```
  1358. %HTTP-response = status-line *( header-field CRLF ) CRLF [ message-body ]
  1359. %```
  1360. %
  1361. %@todo
  1362. %The response format must be followed strictly.
  1363. %
  1364. %```
  1365. %status-line = HTTP-version SP status-code SP reason-phrase CRLF
  1366. %status-code = 3DIGIT
  1367. %reason-phrase = *( HTAB / SP / VCHAR / obs-text )
  1368. %```
  1369. %
  1370. %http10_request_http11_response(Config) ->
  1371. %A server must send its own version. (RFC7230 2.6)
  1372. %
  1373. %@todo
  1374. %An HTTP/1.1 server may send an HTTP/1.0 version for compatibility purposes. (RFC7230 2.6)
  1375. %
  1376. %@todo
  1377. %RFC6585 defines additional status code a server can use to reject
  1378. %messages. (RFC7230 9.3, RFC6585)
  1379. %
  1380. %%% Response headers.
  1381. %
  1382. %@todo
  1383. %In responses, OWS must be generated as SP or not generated
  1384. %at all. RWS must be generated as SP. BWS must not be
  1385. %generated. (RFC7230 3.2.3)
  1386. %
  1387. %```
  1388. %header-field = field-name ":" SP field-value
  1389. %
  1390. %field-name = token ; case-insensitive
  1391. %field-value = *( SP / %21-7E / %80-FF )
  1392. %```
  1393. %
  1394. %@todo
  1395. %In quoted-string found in field-value, quoted-pair must only be
  1396. %used for DQUOTE and backslash. (RFC7230 3.2.6)
  1397. %
  1398. %@todo
  1399. %HTTP header values must use US-ASCII encoding and must only send
  1400. %printable characters or SP. (RFC7230 3.2.4, RFC7230 9.4)
  1401. %
  1402. %@todo
  1403. %The server must not generate empty list elements in headers. (RFC7230 7)
  1404. %
  1405. %@todo
  1406. %When encoding an URI as part of a response, only characters that
  1407. %are reserved need to be percent-encoded. (RFC7230 2.7.3)
  1408. %
  1409. %special_set_cookie_handling(Config) ->
  1410. %The set-cookie header must be handled as a special case. There
  1411. %must be exactly one set-cookie header field per cookie. (RFC7230 3.2.2)
  1412. %
  1413. %@todo
  1414. %The server must list headers for or about the immediate connection
  1415. %in the connection header field. (RFC7230 6.1)
  1416. %
  1417. %@todo
  1418. %A server that does not support persistent connections must
  1419. %send "close" in every non-1xx response. (RFC7230 6.1)
  1420. %
  1421. %no_close_in_100_response(Config) ->
  1422. %no_close_in_101_response(Config) ->
  1423. %no_close_in_102_response(Config) ->
  1424. %A server must not send a "close" connection option
  1425. %in 1xx responses. (RFC7230 6.1)
  1426. %
  1427. %@todo
  1428. %The "close" connection must be sent in a message when the
  1429. %sender knows it will close the connection after fully sending
  1430. %the response. (RFC7230 6.6)
  1431. %
  1432. %@todo
  1433. %A server must close the connection after sending or
  1434. %receiving a "close" once the response has been sent. (RFC7230 6.6)
  1435. %
  1436. %close_request_close_response(Config) ->
  1437. %A server must send a "close" in a response to a request
  1438. %containing a "close". (RFC7230 6.6)
  1439. %
  1440. %%% Response body.
  1441. %
  1442. %no_body_in_head_response(Config) -> %% @todo test different ways to send a body in response
  1443. %Responses to HEAD requests never include a message body. (RFC7230 3.3)
  1444. %
  1445. %%% @todo Implement CONNECT
  1446. %2xx responses to CONNECT requests never include a message
  1447. %body. (RFC7230 3.3)
  1448. %
  1449. %no_body_in_100_response(Config) ->
  1450. %no_body_in_101_response(Config) ->
  1451. %no_body_in_102_response(Config) ->
  1452. %no_body_in_204_response(Config) ->
  1453. %no_body_in_304_response(Config) ->
  1454. %1xx, 204 and 304 responses never include a message body. (RFC7230 3.3)
  1455. %
  1456. %same_content_length_as_get_in_head_response(Config) ->
  1457. %same_transfer_encoding_as_get_in_head_response(Config) ->
  1458. %same_content_length_as_200_in_304_response(Config) ->
  1459. %same_transfer_encoding_as_200_in_304_response(Config) ->
  1460. %Responses to HEAD requests and 304 responses can include a
  1461. %content-length or transfer-encoding header. Their value must
  1462. %be the same as if the request was an unconditional GET. (RFC7230 3.3, RFC7230 3.3.1, RFC7230 3.3.2)
  1463. %
  1464. %no_transfer_encoding_in_100_response(Config) ->
  1465. %no_transfer_encoding_in_101_response(Config) ->
  1466. %no_transfer_encoding_in_102_response(Config) ->
  1467. %no_transfer_encoding_in_204_response(Config) ->
  1468. %%% @todo CONNECT no_transfer_encoding_in_2xx_response_to_connect_request(Config) ->
  1469. %no_content_length_in_100_response(Config) ->
  1470. %no_content_length_in_101_response(Config) ->
  1471. %no_content_length_in_102_response(Config) ->
  1472. %no_content_length_in_204_response(Config) ->
  1473. %%% @todo CONNECT no_content_length_in_2xx_response_to_connect_request(Config) ->
  1474. %1xx, 204 responses and "2xx responses to CONNECT requests" must
  1475. %not include a content-length or transfer-encoding header. (RFC7230 3.3.1, RFC7230 3.3.2)
  1476. %
  1477. %```
  1478. %message-body = *OCTET
  1479. %```
  1480. %
  1481. %The message body is the octets after decoding any transfer
  1482. %codings. (RFC7230 3.3)
  1483. %
  1484. %content_length_0_when_no_body(Config) ->
  1485. %content_length_response(Config) ->
  1486. %When the length is known in advance, the server must send a
  1487. %content-length header, including if the length is 0. (RFC7230 3.3.2, RFC7230 3.3.3)
  1488. %
  1489. %chunked_response(Config) ->
  1490. %When the length is not known in advance, the chunked transfer-encoding
  1491. %must be used. (RFC7230 3.3.2, RFC7230 3.3.3)
  1492. %
  1493. %compat_no_content_length_or_transfer_encoding_close_on_body_end(Config) ->
  1494. %For compatibility purposes a server can send no content-length or
  1495. %transfer-encoding header. In this case the connection must be
  1496. %closed after the response has been sent fully. (RFC7230 3.3.2, RFC7230 3.3.3)
  1497. %
  1498. %no_content_length_if_transfer_encoding(Config) ->
  1499. %The content-length header must not be sent when a transfer-encoding
  1500. %header already exists. (RFC7230 3.3.2)
  1501. %
  1502. %@todo
  1503. %The server must not apply the chunked transfer-encoding more than
  1504. %once. (RFC7230 3.3.1)
  1505. %
  1506. %@todo
  1507. %The server must apply the chunked transfer-encoding last. (RFC7230 3.3.1)
  1508. %
  1509. %http10_request_no_transfer_encoding_in_response(Config) ->
  1510. %The transfer-encoding header must not be sent in responses to
  1511. %HTTP/1.0 requests, or in responses that use the HTTP/1.0 version.
  1512. %No transfer codings must be applied in these cases. (RFC7230 3.3.1)
  1513. %
  1514. %```
  1515. %TE = #t-codings
  1516. %
  1517. %t-codings = "trailers" / ( transfer-coding [ t-ranking ] )
  1518. %t-ranking = OWS ";" OWS "q=" rank
  1519. %rank = ( "0" [ "." 0*3DIGIT ] ) / ( "1" [ "." 0*3("0") ] )
  1520. %```
  1521. %
  1522. %no_te_no_trailers(Config) ->
  1523. %te_trailers(Config) ->
  1524. %Trailers can only be sent if the request includes a TE header
  1525. %containing "trailers". (RFC7230 4.1.2)
  1526. %
  1527. %te_ignore_chunked(Config) ->
  1528. %te_ignore_chunked_0(Config) ->
  1529. %The presence of "chunked" in a TE header must be ignored as it
  1530. %is always acceptable with HTTP/1.1. (RFC7230 4.3)
  1531. %
  1532. %%% @todo te_not_acceptable_coding(Config) ->
  1533. %A qvalue of 0 in the TE header means "not acceptable". (RFC7230 4.3)
  1534. %
  1535. %@todo
  1536. %The lack of a TE header or an empty TE header means only "chunked"
  1537. %(with no trailers) or no transfer-encoding is acceptable. (RFC7230 4.3)
  1538. %
  1539. %ignore_te_if_not_in_connection_header(Config) ->
  1540. %The TE header must be listed in the connection header field,
  1541. %or must be ignored otherwise.
  1542. %
  1543. %@todo
  1544. %Trailer headers must be listed in the trailer header field value. (RFC7230 4.4)
  1545. %
  1546. %@todo
  1547. %When defined, the trailer header must also be listed in the connection header. (RFC7230 4.4)
  1548. %
  1549. %:: Upgrade
  1550. %
  1551. %```
  1552. %Upgrade = 1#protocol
  1553. %
  1554. %protocol = protocol-name ["/" protocol-version]
  1555. %protocol-name = token
  1556. %protocol-version = token
  1557. %```
  1558. %
  1559. %The upgrade header contains the list of protocols the
  1560. %client wishes to upgrade to, in order of preference. (RFC7230 6.7)
  1561. %
  1562. %upgrade_safely_ignored(Config) ->
  1563. %The upgrade header can be safely ignored. (RFC7230 6.7)
  1564. %
  1565. %upgrade_must_be_in_connection_header(Config) ->
  1566. %The upgrade header must be listed under the connection header,
  1567. %or must be ignored otherwise. (RFC7230 6.7)
  1568. %
  1569. %@todo
  1570. %A server accepting an upgrade request must send a 101 status
  1571. %code with a upgrade header listing the protocol(s) it upgrades
  1572. %to, in layer-ascending order. In addition the upgrade header
  1573. %must be listed in the connection header. (RFC7230 6.7)
  1574. %
  1575. %%A server must not switch to a protocol not listed in the
  1576. %%request's upgrade header. (RFC7230 6.7)
  1577. %
  1578. %@todo
  1579. %A server that sends a 426 status code must include a upgrade
  1580. %header listing acceptable protocols in order of preference. (RFC7230 6.7)
  1581. %
  1582. %@todo
  1583. %A server can send a upgrade header to any response to advertise
  1584. %its support for other protocols listed in order of preference. (RFC7230 6.7)
  1585. %
  1586. %@todo
  1587. %Immediately after a server responds with a 101 status code
  1588. %it must respond to the original request using the new protocol. (RFC7230 6.7)
  1589. %
  1590. %@todo
  1591. %%A server must not switch protocols unless the original message's
  1592. %%semantics can be honored by the new protocol. OPTIONS requests
  1593. %%can be honored by any protocol. (RFC7230 6.7)
  1594. %
  1595. %http10_ignore_upgrade_header(Config) ->
  1596. %A server must ignore an upgrade header received by an HTTP/1.0
  1597. %request. (RFC7230 6.7)
  1598. %
  1599. %expect_then_upgrade(Config) ->
  1600. %A server receiving both an upgrade header and an expect header
  1601. %containing "100-continue" must send a 100 response before the
  1602. %101 response. (RFC7230 6.7)
  1603. %
  1604. %The upgrade header field cannot be used for switching the
  1605. %connection protocol (e.g. TCP) or switching connections. (RFC7230 6.7)
  1606. %
  1607. %%% Compatibility.
  1608. %
  1609. %@todo
  1610. %A server can choose to be non-conformant to the specifications
  1611. %for the sake of compatibility. Such behavior can be enabled
  1612. %through configuration and/or software identification. (RFC7230 2.5)