rfc7230_SUITE.erl 73 KB

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