rfc7230_SUITE.erl 71 KB

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