rfc7230_SUITE.erl 77 KB

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