rfc7230_SUITE.erl 73 KB

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