rfc7230_SUITE.erl 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233
  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_http09(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/0.9\r\n"
  592. "Host: localhost\r\n"
  593. "\r\n").
  594. reject_invalid_version_http100(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.00\r\n"
  599. "Host: localhost\r\n"
  600. "\r\n").
  601. reject_invalid_version_http111(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.11\r\n"
  606. "Host: localhost\r\n"
  607. "\r\n").
  608. reject_invalid_version_http12(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/1.2\r\n"
  613. "Host: localhost\r\n"
  614. "\r\n").
  615. reject_invalid_version_http2(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. (RFC7230 2.6, RFC7230 A.2)"),
  618. #{code := 505} = do_raw(Config,
  619. "GET / HTTP/2\r\n"
  620. "Host: localhost\r\n"
  621. "\r\n").
  622. reject_empty_version(Config) ->
  623. doc("Any version number other than HTTP/1.0 or HTTP/1.1 must be "
  624. "rejected by a server or intermediary with a 505 status code. "
  625. "(RFC7230 2.6, RFC7230 A, RFC7230 A.2)"),
  626. #{code := 505} = do_raw(Config,
  627. "GET / \r\n"
  628. "Host: localhost\r\n"
  629. "\r\n").
  630. reject_invalid_whitespace_after_version(Config) ->
  631. doc("A request that has whitespace different than CRLF following the "
  632. "version must be rejected with a 400 status code and the closing "
  633. "of the connection. (RFC7230 3.1.1)"),
  634. #{code := 400, client := Client} = do_raw(Config,
  635. "GET / HTTP/1.1 \r\n"
  636. "Host: localhost\r\n"
  637. "\r\n"),
  638. {error, closed} = raw_recv(Client, 0, 1000).
  639. %% Request headers.
  640. invalid_header_name(Config) ->
  641. doc("Header field names are tokens. (RFC7230 3.2)"),
  642. #{code := 400} = do_raw(Config, [
  643. "GET / HTTP/1.1\r\n"
  644. "Host\0: localhost\r\n"
  645. "\r\n"]).
  646. invalid_header_value(Config) ->
  647. doc("Header field values are made of printable characters, "
  648. "horizontal tab or space. (RFC7230 3.2)"),
  649. #{code := 400} = do_raw(Config, [
  650. "GET / HTTP/1.1\r\n"
  651. "Host: localhost\0rm rf the world\r\n"
  652. "\r\n"]).
  653. lower_case_header(Config) ->
  654. doc("The header field name is case insensitive. (RFC7230 3.2)"),
  655. #{code := 200} = do_raw(Config, [
  656. "GET / HTTP/1.1\r\n"
  657. "host: localhost\r\n"
  658. "\r\n"]).
  659. upper_case_header(Config) ->
  660. doc("The header field name is case insensitive. (RFC7230 3.2)"),
  661. #{code := 200} = do_raw(Config, [
  662. "GET / HTTP/1.1\r\n"
  663. "HOST: localhost\r\n"
  664. "\r\n"]).
  665. mixed_case_header(Config) ->
  666. doc("The header field name is case insensitive. (RFC7230 3.2)"),
  667. #{code := 200} = do_raw(Config, [
  668. "GET / HTTP/1.1\r\n"
  669. "hOsT: localhost\r\n"
  670. "\r\n"]).
  671. reject_whitespace_before_header_name(Config) ->
  672. doc("Messages that contain whitespace before the header name must "
  673. "be rejected with a 400 status code and the closing of the "
  674. "connection. (RFC7230 3.2.4)"),
  675. #{code := 400, client := Client1} = do_raw(Config, [
  676. "GET / HTTP/1.1\r\n"
  677. " Host: localhost\r\n"
  678. "\r\n"]),
  679. {error, closed} = raw_recv(Client1, 0, 1000),
  680. #{code := 400, client := Client2} = do_raw(Config, [
  681. "GET / HTTP/1.1\r\n"
  682. "\tHost: localhost\r\n"
  683. "\r\n"]),
  684. {error, closed} = raw_recv(Client2, 0, 1000).
  685. reject_whitespace_between_header_name_and_colon(Config) ->
  686. doc("Messages that contain whitespace between the header name and "
  687. "colon must be rejected with a 400 status code and the closing "
  688. "of the connection. (RFC7230 3.2.4)"),
  689. #{code := 400, client := Client1} = do_raw(Config, [
  690. "GET / HTTP/1.1\r\n"
  691. "Host : localhost\r\n"
  692. "\r\n"]),
  693. {error, closed} = raw_recv(Client1, 0, 1000),
  694. #{code := 400, client := Client2} = do_raw(Config, [
  695. "GET / HTTP/1.1\r\n"
  696. "Host\t: localhost\r\n"
  697. "\r\n"]),
  698. {error, closed} = raw_recv(Client2, 0, 1000).
  699. reject_header_name_without_colon(Config) ->
  700. doc("Messages that contain a header name that is not followed by a "
  701. "colon must be rejected with a 400 status code and the closing "
  702. "of the connection. (RFC7230 3.2.4)"),
  703. #{code := 400, client := Client1} = do_raw(Config, [
  704. "GET / HTTP/1.1\r\n"
  705. "Host\r\n"
  706. "\r\n"]),
  707. {error, closed} = raw_recv(Client1, 0, 1000),
  708. #{code := 400, client := Client2} = do_raw(Config, [
  709. "GET / HTTP/1.1\r\n"
  710. "Host localhost\r\n"
  711. "\r\n"]),
  712. {error, closed} = raw_recv(Client2, 0, 1000),
  713. #{code := 400, client := Client3} = do_raw(Config, [
  714. "GET / HTTP/1.1\r\n"
  715. "Host\r\n"
  716. " : localhost\r\n"
  717. "\r\n"]),
  718. {error, closed} = raw_recv(Client3, 0, 1000).
  719. limit_header_name(Config) ->
  720. doc("The header name must be subject to a configurable limit. A "
  721. "good default is 50 characters, well above the longest registered "
  722. "header. Such a request must be rejected with a 431 status code "
  723. "and the closing of the connection. "
  724. "(RFC7230 3.2.5, RFC6585 5, IANA Message Headers registry)"),
  725. #{code := 431, client := Client} = do_raw(Config, [
  726. "GET / HTTP/1.1\r\n"
  727. "Host: localhost\r\n",
  728. binary:copy(<<$a>>, 32768), ": bad\r\n"
  729. "\r\n"]),
  730. {error, closed} = raw_recv(Client, 0, 1000).
  731. limit_header_value(Config) ->
  732. doc("The header value and the optional whitespace around it must be "
  733. "subject to a configurable limit. There is no recommendations "
  734. "for the default. 4096 characters is known to work well. Such "
  735. "a request must be rejected with a 431 status code and the closing "
  736. "of the connection. (RFC7230 3.2.5, RFC6585 5)"),
  737. #{code := 431, client := Client} = do_raw(Config, [
  738. "GET / HTTP/1.1\r\n"
  739. "Host: localhost\r\n"
  740. "bad: ", binary:copy(<<$a>>, 32768), "\r\n"
  741. "\r\n"]),
  742. {error, closed} = raw_recv(Client, 0, 1000).
  743. drop_whitespace_before_header_value(Config) ->
  744. doc("Optional whitespace before and after the header value is not "
  745. "part of the value and must be dropped."),
  746. #{code := 200} = do_raw(Config, [
  747. "POST / HTTP/1.1\r\n"
  748. "Host: localhost\r\n"
  749. "Content-length: \t 12\r\n"
  750. "\r\n"
  751. "Hello world!"]).
  752. drop_whitespace_after_header_value(Config) ->
  753. doc("Optional whitespace before and after the header value is not "
  754. "part of the value and must be dropped."),
  755. #{code := 200} = do_raw(Config, [
  756. "POST / HTTP/1.1\r\n"
  757. "Host: localhost\r\n"
  758. "Content-length: 12 \t \r\n"
  759. "\r\n"
  760. "Hello world!"]).
  761. reject_lf_line_breaks(Config) ->
  762. doc("A server may accept header names separated by a single LF, instead of "
  763. "CRLF. Cowboy rejects all requests that use LF as separator. (RFC7230 3.5)"),
  764. #{code := 400, client := Client} = do_raw(Config, [
  765. "POST /echo/read_body HTTP/1.1\r\n"
  766. "Host: localhost\n"
  767. "Transfer-encoding: chunked\r\n"
  768. "\r\n"
  769. "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  770. {error, closed} = raw_recv(Client, 0, 1000).
  771. %@todo
  772. %The order of header fields with differing names is not significant. (RFC7230 3.2.2)
  773. %
  774. %@todo
  775. %The normal procedure for parsing headers is to read each header
  776. %field into a hash table by field name until the empty line. (RFC7230 3)
  777. reject_duplicate_content_length_header(Config) ->
  778. doc("Requests with duplicate content-length headers must be rejected "
  779. "with a 400 status code and the closing of the connection. (RFC7230 3.3.2)"),
  780. #{code := 400, client := Client} = do_raw(Config, [
  781. "POST / HTTP/1.1\r\n"
  782. "Host: localhost\r\n"
  783. "Content-length: 12\r\n"
  784. "Content-length: 12\r\n"
  785. "\r\n"
  786. "Hello world!"]),
  787. {error, closed} = raw_recv(Client, 0, 1000).
  788. reject_duplicate_host_header(Config) ->
  789. doc("Requests with duplicate host headers must be rejected "
  790. "with a 400 status code and the closing of the connection. (RFC7230 3.3.2)"),
  791. #{code := 400, client := Client} = do_raw(Config, [
  792. "POST / HTTP/1.1\r\n"
  793. "Host: localhost\r\n"
  794. "Host: localhost\r\n"
  795. "\r\n"
  796. "Hello world!"]),
  797. {error, closed} = raw_recv(Client, 0, 1000).
  798. combine_duplicate_headers(Config) ->
  799. doc("Other duplicate header fields must be combined by inserting a comma "
  800. "between the values in the order they were received. (RFC7230 3.2.2)"),
  801. #{code := 200, body := Body} = do_raw(Config, [
  802. "GET /echo/headers HTTP/1.1\r\n"
  803. "Host: localhost\r\n"
  804. "Accept-encoding: gzip\r\n"
  805. "Accept-encoding: brotli\r\n"
  806. "\r\n"]),
  807. <<"#{<<\"accept-encoding\">> => <<\"gzip, brotli\">>,", _/bits>> = Body,
  808. ok.
  809. %Duplicate header field names are only allowed when their value is
  810. %a comma-separated list. In practice there is no need to perform
  811. %a check while reading the headers as the value will become invalid
  812. %and the error can be handled while parsing the header later on. (RFC7230 3.2.2)
  813. %
  814. %wait_for_eoh_before_processing_request(Config) ->
  815. %The request must not be processed until all headers have arrived. (RFC7230 3.2.2)
  816. limit_headers(Config) ->
  817. doc("The number of headers allowed in a request must be subject to "
  818. "a configurable limit. There is no recommendations for the default. "
  819. "100 headers is known to work well. Such a request must be rejected "
  820. "with a 431 status code and the closing of the connection. (RFC7230 3.2.5, RFC6585 5)"),
  821. %% 100 headers.
  822. #{code := 200} = do_raw(Config, [
  823. "GET / HTTP/1.1\r\n"
  824. "Host: localhost\r\n",
  825. [["H-", integer_to_list(N), ": value\r\n"] || N <- lists:seq(1, 99)],
  826. "\r\n"]),
  827. %% 101 headers.
  828. #{code := 431, client := Client} = do_raw(Config, [
  829. "GET / HTTP/1.1\r\n"
  830. "Host: localhost\r\n",
  831. [["H-", integer_to_list(N), ": value\r\n"] || N <- lists:seq(1, 100)],
  832. "\r\n"]),
  833. {error, closed} = raw_recv(Client, 0, 1000).
  834. %ignore_header_empty_list_elements(Config) ->
  835. %When parsing header field values, the server must ignore empty
  836. %list elements, and not count those as the count of elements present. (RFC7230 7)
  837. %
  838. %@todo
  839. %The information in the via header is largely unreliable. (RFC7230 5.7.1)
  840. %% Request body.
  841. %@todo
  842. %The message body is the octets after decoding any transfer
  843. %codings. (RFC7230 3.3)
  844. no_request_body(Config) ->
  845. doc("A request has a message body only if it includes a transfer-encoding "
  846. "header or a non-zero content-length header. (RFC7230 3.3)"),
  847. #{code := 200, body := <<"false">>} = do_raw(Config, [
  848. "POST /echo/has_body HTTP/1.1\r\n"
  849. "Host: localhost\r\n"
  850. "\r\n"]),
  851. #{code := 200, body := <<>>} = do_raw(Config, [
  852. "POST /echo/read_body HTTP/1.1\r\n"
  853. "Host: localhost\r\n"
  854. "\r\n"]),
  855. ok.
  856. no_request_body_content_length_zero(Config) ->
  857. doc("A request has a message body only if it includes a transfer-encoding "
  858. "header or a non-zero content-length header. (RFC7230 3.3)"),
  859. #{code := 200, body := <<"false">>} = do_raw(Config, [
  860. "POST /echo/has_body HTTP/1.1\r\n"
  861. "Host: localhost\r\n"
  862. "Content-length: 0\r\n"
  863. "\r\n"]),
  864. #{code := 200, body := <<>>} = do_raw(Config, [
  865. "POST /echo/read_body HTTP/1.1\r\n"
  866. "Host: localhost\r\n"
  867. "Content-length: 0\r\n"
  868. "\r\n"]),
  869. ok.
  870. request_body_content_length(Config) ->
  871. doc("A request has a message body only if it includes a transfer-encoding "
  872. "header or a non-zero content-length header. (RFC7230 3.3)"),
  873. #{code := 200, body := <<"true">>} = do_raw(Config, [
  874. "POST /echo/has_body HTTP/1.1\r\n"
  875. "Host: localhost\r\n"
  876. "Content-length: 12\r\n"
  877. "\r\n"
  878. "Hello world!"]),
  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. "Content-length: 12\r\n"
  883. "\r\n"
  884. "Hello world!"]),
  885. ok.
  886. request_body_transfer_encoding(Config) ->
  887. doc("A request has a message body only if it includes a transfer-encoding "
  888. "header or a non-zero content-length header. (RFC7230 3.3)"),
  889. #{code := 200, body := <<"true">>} = do_raw(Config, [
  890. "POST /echo/has_body HTTP/1.1\r\n"
  891. "Host: localhost\r\n"
  892. "Transfer-encoding: chunked\r\n"
  893. "\r\n"
  894. "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  895. #{code := 200, body := <<"Hello world!">>} = do_raw(Config, [
  896. "POST /echo/read_body HTTP/1.1\r\n"
  897. "Host: localhost\r\n"
  898. "Transfer-encoding: chunked\r\n"
  899. "\r\n"
  900. "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  901. ok.
  902. %```
  903. %Transfer-Encoding = 1#transfer-coding
  904. %
  905. %transfer-coding = "chunked" / "compress" / "deflate" / "gzip" / transfer-extension
  906. %transfer-extension = token *( OWS ";" OWS transfer-parameter )
  907. %transfer-parameter = token BWS "=" BWS ( token / quoted-string )
  908. %```
  909. case_insensitive_transfer_encoding(Config) ->
  910. doc("The transfer-coding is case insensitive. (RFC7230 4)"),
  911. #{code := 200, body := <<"Hello world!">>} = do_raw(Config, [
  912. "POST /echo/read_body HTTP/1.1\r\n"
  913. "Host: localhost\r\n"
  914. "Transfer-encoding: ChUnKeD\r\n"
  915. "\r\n"
  916. "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  917. ok.
  918. %@todo
  919. %There are no known other transfer-extension with the exception of
  920. %deprecated aliases "x-compress" and "x-gzip". (IANA HTTP Transfer Coding Registry,
  921. %RFC7230 4.2.1, RFC7230 4.2.3, RFC7230 8.4.2)
  922. %% This is the exact same test as request_body_transfer_encoding.
  923. must_understand_chunked(Config) ->
  924. doc("A server must be able to handle at least chunked transfer-encoding. "
  925. "This is also the only coding that sees widespread use. (RFC7230 3.3.1, RFC7230 4.1)"),
  926. #{code := 200, body := <<"Hello world!">>} = do_raw(Config, [
  927. "POST /echo/read_body HTTP/1.1\r\n"
  928. "Host: localhost\r\n"
  929. "Transfer-encoding: chunked\r\n"
  930. "\r\n"
  931. "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  932. ok.
  933. reject_double_chunked_encoding(Config) ->
  934. doc("Messages encoded more than once with chunked transfer-encoding "
  935. "must be rejected with a 400 status code and the closing of the "
  936. "connection. (RFC7230 3.3.1)"),
  937. #{code := 400, client := Client} = do_raw(Config, [
  938. "POST / HTTP/1.1\r\n"
  939. "Host: localhost\r\n"
  940. "Transfer-encoding: chunked, chunked\r\n"
  941. "\r\n"
  942. "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"]),
  943. {error, closed} = raw_recv(Client, 0, 1000).
  944. reject_non_terminal_chunked(Config) ->
  945. doc("Messages where chunked, when present, is not the last "
  946. "transfer-encoding must be rejected with a 400 status code "
  947. "and the closing of the connection. (RFC7230 3.3.3)"),
  948. #{code := 400, client := Client1} = do_raw(Config, [
  949. "POST / HTTP/1.1\r\n"
  950. "Host: localhost\r\n"
  951. "Transfer-encoding: chunked, gzip\r\n"
  952. "\r\n",
  953. zlib:gzip(<<"6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n">>)]),
  954. {error, closed} = raw_recv(Client1, 0, 1000),
  955. #{code := 400, client := Client2} = do_raw(Config, [
  956. "POST / HTTP/1.1\r\n"
  957. "Host: localhost\r\n"
  958. "Transfer-encoding: chunked\r\n"
  959. "Transfer-encoding: gzip\r\n"
  960. "\r\n",
  961. zlib:gzip(<<"6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n">>)]),
  962. {error, closed} = raw_recv(Client2, 0, 1000).
  963. %@todo
  964. %Some non-conformant implementations send the "deflate" compressed
  965. %data without the zlib wrapper. (RFC7230 4.2.2)
  966. reject_unknown_transfer_encoding(Config) ->
  967. doc("Messages encoded with a transfer-encoding the server does not "
  968. "understand must be rejected with a 501 status code and the "
  969. "closing of the connection. (RFC7230 3.3.1)"),
  970. #{code := 400, client := Client1} = do_raw(Config, [
  971. "POST / HTTP/1.1\r\n"
  972. "Host: localhost\r\n"
  973. "Transfer-encoding: unknown, chunked\r\n"
  974. "\r\n",
  975. "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  976. {error, closed} = raw_recv(Client1, 0, 1000),
  977. #{code := 400, client := Client2} = do_raw(Config, [
  978. "POST / HTTP/1.1\r\n"
  979. "Host: localhost\r\n"
  980. "Transfer-encoding: unknown\r\n"
  981. "Transfer-encoding: chunked\r\n"
  982. "\r\n",
  983. "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  984. {error, closed} = raw_recv(Client2, 0, 1000).
  985. %@todo
  986. %A server may reject requests with a body and no content-length
  987. %header with a 411 status code. (RFC7230 3.3.3)
  988. %```
  989. %Content-Length = 1*DIGIT
  990. %```
  991. reject_invalid_content_length(Config) ->
  992. doc("A request with an invalid content-length header must be rejected "
  993. "with a 400 status code and the closing of the connection. (RFC7230 3.3.3)"),
  994. #{code := 400, client := Client1} = do_raw(Config, [
  995. "POST / HTTP/1.1\r\n"
  996. "Host: localhost\r\n"
  997. "Content-length: 12,12\r\n"
  998. "\r\n"
  999. "Hello world!"]),
  1000. {error, closed} = raw_recv(Client1, 0, 1000),
  1001. #{code := 400, client := Client2} = do_raw(Config, [
  1002. "POST / HTTP/1.1\r\n"
  1003. "Host: localhost\r\n"
  1004. "Content-length: NaN\r\n"
  1005. "\r\n"
  1006. "Hello world!"]),
  1007. {error, closed} = raw_recv(Client2, 0, 1000).
  1008. %@todo
  1009. %The content-length header ranges from 0 to infinity. Requests
  1010. %with a message body too large must be rejected with a 413 status
  1011. %code and the closing of the connection. (RFC7230 3.3.2)
  1012. ignore_content_length_when_transfer_encoding(Config) ->
  1013. doc("When a message includes both transfer-encoding and content-length "
  1014. "headers, the content-length header must be removed before processing "
  1015. "the request. (RFC7230 3.3.3)"),
  1016. #{code := 200, body := <<"Hello world!">>} = do_raw(Config, [
  1017. "POST /echo/read_body HTTP/1.1\r\n"
  1018. "Host: localhost\r\n"
  1019. "Transfer-encoding: chunked\r\n"
  1020. "Content-length: 12\r\n"
  1021. "\r\n"
  1022. "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  1023. ok.
  1024. %socket_error_while_reading_body(Config) ->
  1025. %If a socket error occurs while reading the body the server
  1026. %must send a 400 status code response and close the connection. (RFC7230 3.3.3, RFC7230 3.4)
  1027. %
  1028. %timeout_while_reading_body(Config) ->
  1029. %If a timeout occurs while reading the body the server must
  1030. %send a 408 status code response and close the connection. (RFC7230 3.3.3, RFC7230 3.4)
  1031. %% Body length.
  1032. body_length_chunked_before(Config) ->
  1033. doc("The length of a message with a transfer-encoding header can "
  1034. "only be determined on decoding completion. (RFC7230 3.3.3)"),
  1035. #{code := 200, body := <<"undefined">>} = do_raw(Config, [
  1036. "POST /echo/body_length HTTP/1.1\r\n"
  1037. "Host: localhost\r\n"
  1038. "Transfer-encoding: chunked\r\n"
  1039. "\r\n"
  1040. "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  1041. ok.
  1042. body_length_chunked_after(Config) ->
  1043. doc("Upon completion of chunk decoding the server must add a content-length "
  1044. "header with the value set to the total length of data read. (RFC7230 4.1.3)"),
  1045. #{code := 200, body := <<"12">>} = do_raw(Config, [
  1046. "POST /length/echo/read_body HTTP/1.1\r\n"
  1047. "Host: localhost\r\n"
  1048. "Transfer-encoding: chunked\r\n"
  1049. "\r\n"
  1050. "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  1051. ok.
  1052. body_length_content_length(Config) ->
  1053. doc("The length of a message with a content-length header is "
  1054. "the numeric value in octets found in the header. (RFC7230 3.3.3)"),
  1055. #{code := 200, body := <<"12">>} = do_raw(Config, [
  1056. "POST /echo/body_length HTTP/1.1\r\n"
  1057. "Host: localhost\r\n"
  1058. "Content-length: 12\r\n"
  1059. "\r\n"
  1060. "Hello world!"]),
  1061. ok.
  1062. body_length_zero(Config) ->
  1063. doc("A message with no transfer-encoding or content-length header "
  1064. "has a body length of 0. (RFC7230 3.3.3)"),
  1065. #{code := 200, body := <<"0">>} = do_raw(Config, [
  1066. "POST /echo/body_length HTTP/1.1\r\n"
  1067. "Host: localhost\r\n"
  1068. "\r\n"]),
  1069. ok.
  1070. %% Chunked transfer-encoding.
  1071. reject_invalid_chunk_size(Config) ->
  1072. doc("A request with an invalid chunk size must be rejected "
  1073. "with a 400 status code and the closing of the connection. (RFC7230 4.1)"),
  1074. #{code := 400, client := Client} = do_raw(Config, [
  1075. "POST /echo/read_body HTTP/1.1\r\n"
  1076. "Host: localhost\r\n"
  1077. "Transfer-encoding: chunked\r\n"
  1078. "\r\n"
  1079. "6\r\nHello \r\nFIVE\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  1080. {error, closed} = raw_recv(Client, 0, 1000).
  1081. %```
  1082. %chunked-body = *chunk last-chunk trailer-part CRLF
  1083. %
  1084. %chunk = chunk-size [ chunk-ext ] CRLF chunk-data CRLF
  1085. %chunk-size = 1*HEXDIG
  1086. %chunk-data = 1*OCTET ; a sequence of chunk-size octets
  1087. %
  1088. %last-chunk = 1*("0") [ chunk-ext ] CRLF
  1089. %```
  1090. %
  1091. %The chunk-size field is a string of hex digits indicating the size of
  1092. %the chunk-data in octets.
  1093. %
  1094. %```
  1095. %chunk-ext = *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
  1096. %chunk-ext-name = token
  1097. %chunk-ext-val = token / quoted-string
  1098. %```
  1099. ignore_unknown_chunk_extensions(Config) ->
  1100. doc("Unknown chunk extensions must be ignored. (RFC7230 4.1.1)"),
  1101. #{code := 200, body := <<"Hello world!">>} = do_raw(Config, [
  1102. "POST /echo/read_body HTTP/1.1\r\n"
  1103. "Host: localhost\r\n"
  1104. "Transfer-encoding: chunked\r\n"
  1105. "\r\n"
  1106. "6; hello=\"cool world\"\r\nHello \r\n"
  1107. "5 ; one ; two ; three;four;five\r\nworld"
  1108. "\r\n1;ok\r\n!\r\n0\r\n\r\n"]),
  1109. ok.
  1110. %% Since we skip everything right now, the only reason
  1111. %% we might reject chunk extensions is if they are too large.
  1112. limit_chunk_size_line(Config) ->
  1113. doc("A request with chunk extensions larger than the server allows must be rejected "
  1114. "with a 400 status code and the closing of the connection. (RFC7230 4.1.1)"),
  1115. #{code := 200, body := <<"Hello world!">>} = do_raw(Config, [
  1116. "POST /echo/read_body HTTP/1.1\r\n"
  1117. "Host: localhost\r\n"
  1118. "Transfer-encoding: chunked\r\n"
  1119. "\r\n"
  1120. "6; hello=\"cool world\"\r\nHello \r\n"
  1121. "5;", lists:duplicate(128, $a), "\r\nworld"
  1122. "\r\n1;ok\r\n!\r\n0\r\n\r\n"]),
  1123. #{code := 400, client := Client} = do_raw(Config, [
  1124. "POST /echo/read_body HTTP/1.1\r\n"
  1125. "Host: localhost\r\n"
  1126. "Transfer-encoding: chunked\r\n"
  1127. "\r\n"
  1128. "6; hello=\"cool world\"\r\nHello \r\n"
  1129. "5;", lists:duplicate(129, $a), "\r\nworld"
  1130. "\r\n1;ok\r\n!\r\n0\r\n\r\n"]),
  1131. {error, closed} = raw_recv(Client, 0, 1000).
  1132. reject_invalid_chunk_size_crlf(Config) ->
  1133. doc("A request with an invalid line break after the chunk size must be rejected "
  1134. "with a 400 status code and the closing of the connection. (RFC7230 4.1)"),
  1135. #{code := 400, client := Client1} = do_raw(Config, [
  1136. "POST /echo/read_body HTTP/1.1\r\n"
  1137. "Host: localhost\r\n"
  1138. "Transfer-encoding: chunked\r\n"
  1139. "\r\n"
  1140. "6\rHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  1141. {error, closed} = raw_recv(Client1, 0, 1000),
  1142. #{code := 400, client := Client2} = do_raw(Config, [
  1143. "POST /echo/read_body HTTP/1.1\r\n"
  1144. "Host: localhost\r\n"
  1145. "Transfer-encoding: chunked\r\n"
  1146. "\r\n"
  1147. "6\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  1148. {error, closed} = raw_recv(Client2, 0, 1000),
  1149. #{code := 400, client := Client3} = do_raw(Config, [
  1150. "POST /echo/read_body HTTP/1.1\r\n"
  1151. "Host: localhost\r\n"
  1152. "Transfer-encoding: chunked\r\n"
  1153. "\r\n"
  1154. "6Hello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  1155. {error, closed} = raw_recv(Client3, 0, 1000).
  1156. reject_invalid_chunk_ext_crlf(Config) ->
  1157. doc("A request with an invalid line break after chunk extensions must be rejected "
  1158. "with a 400 status code and the closing of the connection. (RFC7230 4.1)"),
  1159. #{code := 400, client := Client1} = do_raw(Config, [
  1160. "POST /echo/read_body HTTP/1.1\r\n"
  1161. "Host: localhost\r\n"
  1162. "Transfer-encoding: chunked\r\n"
  1163. "\r\n"
  1164. "6; extensions\rHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  1165. {error, closed} = raw_recv(Client1, 0, 1000),
  1166. #{code := 400, client := Client2} = do_raw(Config, [
  1167. "POST /echo/read_body HTTP/1.1\r\n"
  1168. "Host: localhost\r\n"
  1169. "Transfer-encoding: chunked\r\n"
  1170. "\r\n"
  1171. "6; extensions\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  1172. {error, closed} = raw_recv(Client2, 0, 1000),
  1173. #{code := 400, client := Client3} = do_raw(Config, [
  1174. "POST /echo/read_body HTTP/1.1\r\n"
  1175. "Host: localhost\r\n"
  1176. "Transfer-encoding: chunked\r\n"
  1177. "\r\n"
  1178. "6; extensionsHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  1179. {error, closed} = raw_recv(Client3, 0, 1000).
  1180. reject_invalid_chunk_data_crlf(Config) ->
  1181. doc("A request with an invalid line break after the chunk data must be rejected "
  1182. "with a 400 status code and the closing of the connection. (RFC7230 4.1)"),
  1183. #{code := 400, client := Client1} = do_raw(Config, [
  1184. "POST /echo/read_body HTTP/1.1\r\n"
  1185. "Host: localhost\r\n"
  1186. "Transfer-encoding: chunked\r\n"
  1187. "\r\n"
  1188. "6\r\nHello \r5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  1189. {error, closed} = raw_recv(Client1, 0, 1000),
  1190. #{code := 400, client := Client2} = do_raw(Config, [
  1191. "POST /echo/read_body HTTP/1.1\r\n"
  1192. "Host: localhost\r\n"
  1193. "Transfer-encoding: chunked\r\n"
  1194. "\r\n"
  1195. "6\r\nHello \n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  1196. {error, closed} = raw_recv(Client2, 0, 1000),
  1197. #{code := 400, client := Client3} = do_raw(Config, [
  1198. "POST /echo/read_body HTTP/1.1\r\n"
  1199. "Host: localhost\r\n"
  1200. "Transfer-encoding: chunked\r\n"
  1201. "\r\n"
  1202. "6\r\nHello 5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  1203. {error, closed} = raw_recv(Client3, 0, 1000).
  1204. %```
  1205. %trailer-part = *( header-field CRLF )
  1206. %```
  1207. %
  1208. %%% @todo see headers above and reject the same way, space etc.
  1209. %reject_invalid_request_trailer(Config) ->
  1210. %
  1211. %ignore_request_trailer_transfer_encoding(Config) ->
  1212. %ignore_request_trailer_content_length(Config) ->
  1213. %ignore_request_trailer_host(Config) ->
  1214. %ignore_request_trailer_cache_control(Config) ->
  1215. %ignore_request_trailer_expect(Config) ->
  1216. %ignore_request_trailer_max_forwards(Config) ->
  1217. %ignore_request_trailer_pragma(Config) ->
  1218. %ignore_request_trailer_range(Config) ->
  1219. %ignore_request_trailer_te(Config) ->
  1220. %ignore_request_trailer_if_match(Config) ->
  1221. %ignore_request_trailer_if_none_match(Config) ->
  1222. %ignore_request_trailer_if_modified_since(Config) ->
  1223. %ignore_request_trailer_if_unmodified_since(Config) ->
  1224. %ignore_request_trailer_if_range(Config) ->
  1225. %ignore_request_trailer_www_authenticate(Config) ->
  1226. %ignore_request_trailer_authorization(Config) ->
  1227. %ignore_request_trailer_proxy_authenticate(Config) ->
  1228. %ignore_request_trailer_proxy_authorization(Config) ->
  1229. %ignore_request_trailer_content_encoding(Config) ->
  1230. %ignore_request_trailer_content_type(Config) ->
  1231. %ignore_request_trailer_content_range(Config) ->
  1232. %ignore_request_trailer_trailer(Config) ->
  1233. %
  1234. %ignore_response_trailer_header(Config, Header) ->
  1235. %Trailing headers must not include transfer-encoding, content-length,
  1236. %host, cache-control, expect, max-forwards, pragma, range, te,
  1237. %if-match, if-none-match, if-modified-since, if-unmodified-since,
  1238. %if-range, www-authenticate, authorization, proxy-authenticate,
  1239. %proxy-authorization, age, cache-control, expires, date, location,
  1240. %retry-after, vary, warning, content-encoding, content-type,
  1241. %content-range, or trailer. (RFC7230 4.1.2)
  1242. %
  1243. %When trailer headers are processed, invalid headers must be ignored.
  1244. %Valid headers must be added to the list of headers of the request. (RFC7230 4.1.2)
  1245. %
  1246. %ignore_request_trailers(Config) ->
  1247. %Trailer headers can be ignored safely. (RFC7230 4.1.2)
  1248. %
  1249. %limit_request_trailer_headers(Config) ->
  1250. %The number of trailer headers must be subject to configuration.
  1251. %There is no known recommendations for the default. A value of 10
  1252. %should cover most cases. Requests with too many trailer headers
  1253. %must be rejected with a 431 status code and the closing of the
  1254. %connection. (RFC6585 5)
  1255. %% We remove the header immediately so there's no need
  1256. %% to try to read the body before checking.
  1257. remove_transfer_encoding_chunked_after_body_read(Config) ->
  1258. doc("Upon completion of chunk decoding the server must remove \"chunked\" "
  1259. "from the transfer-encoding header. This header must be removed if "
  1260. "it becomes empty following this removal. (RFC7230 4.1.3)"),
  1261. #{code := 200, body := <<"undefined">>} = do_raw(Config, [
  1262. "POST /echo/header/transfer-encoding HTTP/1.1\r\n"
  1263. "Host: localhost\r\n"
  1264. "Transfer-encoding: chunked\r\n"
  1265. "\r\n"
  1266. "6\r\nHello \r\n5\r\nworld\r\n1\r\n!\r\n0\r\n\r\n"]),
  1267. ok.
  1268. %remove_trailer_after_body_read(Config) ->
  1269. %Upon completion of chunk decoding the server must remove the trailer
  1270. %header from the list of headers. (RFC7230 4.1.3)
  1271. %
  1272. %```
  1273. %Trailer = 1#field-name
  1274. %```
  1275. %
  1276. %ignore_chunked_headers_not_in_trailer(Config) ->
  1277. %The trailer header can be used to list the headers found in the
  1278. %trailer. A server must have the option of ignoring trailer headers
  1279. %that were not listed in the trailer header. (RFC7230 4.4)
  1280. %
  1281. %ignore_chunked_headers_if_trailer_not_in_connection(Config) ->
  1282. %The trailer header must be listed in the connection header field.
  1283. %Trailers must be ignored otherwise.
  1284. %
  1285. %%% @todo Though we need a compatibility mode as some clients don't send it...
  1286. %reject_chunked_missing_end_crlf(Config) ->
  1287. %@todo ending CRLF
  1288. %% Connection management.
  1289. %@todo can probably test using auth
  1290. %Never assume any two requests on a single connection come
  1291. %from the same user agent. (RFC7230 2.3)
  1292. %
  1293. %```
  1294. %Connection = 1#token ; case-insensitive
  1295. %```
  1296. %
  1297. %The connection token is either case insensitive "close", "keep-alive"
  1298. %or a header field name.
  1299. %
  1300. %There are no corresponding "close" or "keep-alive" headers. (RFC7230 8.1, RFC7230 A.2)
  1301. %
  1302. %The connection header is valid only for the immediate connection,
  1303. %alongside any header field it lists. (RFC7230 6.1)
  1304. %
  1305. %The server must determine if the connection is persistent for
  1306. %every message received by looking at the connection header and
  1307. %HTTP version. (RFC7230 6.3)
  1308. no_connection_header_keepalive(Config) ->
  1309. doc("HTTP/1.1 requests with no \"close\" option "
  1310. "indicate the connection will persist. (RFC7230 6.1, RFC7230 6.3)"),
  1311. #{code := 200, headers := RespHeaders, client := Client} = do_raw(Config, [
  1312. "GET / HTTP/1.1\r\n"
  1313. "Host: localhost\r\n"
  1314. "\r\n"]),
  1315. false = lists:keyfind(<<"connection">>, 1, RespHeaders),
  1316. {error, timeout} = raw_recv(Client, 0, 1000).
  1317. http10_connection_keepalive(Config) ->
  1318. doc("HTTP/1.0 requests with the \"keep-alive\" option "
  1319. "indicate the connection will persist. "
  1320. "(RFC7230 6.1, RFC7230 6.3, RFC7230 A.1.2)"),
  1321. #{code := 200, headers := RespHeaders, client := Client} = do_raw(Config, [
  1322. "GET / HTTP/1.0\r\n"
  1323. "Host: localhost\r\n"
  1324. "Connection: keep-alive\r\n"
  1325. "\r\n"]),
  1326. {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, RespHeaders),
  1327. {error, timeout} = raw_recv(Client, 0, 1000).
  1328. connection_close(Config) ->
  1329. doc("HTTP/1.1 requests with the \"close\" option and HTTP/1.0 with no "
  1330. "\"keep-alive\" option indicate the connection will be closed "
  1331. "upon reception of the response by the client. (RFC7230 6.1, RFC7230 6.3)"),
  1332. #{code := 200, headers := RespHeaders, client := Client} = do_raw(Config, [
  1333. "GET / HTTP/1.1\r\n"
  1334. "Host: localhost\r\n"
  1335. "Connection: close\r\n"
  1336. "\r\n"]),
  1337. {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, RespHeaders),
  1338. {error, closed} = raw_recv(Client, 0, 1000).
  1339. http10_no_connection_header_close(Config) ->
  1340. doc("HTTP/1.0 with no \"keep-alive\" option indicate "
  1341. "the connection will be closed upon reception of "
  1342. "the response by the client. (RFC7230 6.1, RFC7230 6.3, RFC7230 A.1.2)"),
  1343. #{code := 200, headers := RespHeaders, client := Client} = do_raw(Config, [
  1344. "GET / HTTP/1.0\r\n"
  1345. "Host: localhost\r\n"
  1346. "\r\n"]),
  1347. %% Cowboy always sends a close header back to HTTP/1.0 clients
  1348. %% that support keep-alive, even though it is not required.
  1349. {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, RespHeaders),
  1350. {error, closed} = raw_recv(Client, 0, 1000).
  1351. limit_requests_keepalive(Config) ->
  1352. doc("The maximum number of requests sent using a persistent connection "
  1353. "must be subject to configuration. The connection must be closed "
  1354. "when the limit is reached. (RFC7230 6.3)"),
  1355. ConnPid = gun_open(Config),
  1356. _ = [begin
  1357. Ref = gun:get(ConnPid, "/"),
  1358. {response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref),
  1359. {ok, <<"Hello world!">>} = gun:await_body(ConnPid, Ref),
  1360. false = lists:keyfind(<<"connection">>, 1, RespHeaders)
  1361. end || _ <- lists:seq(1,99)],
  1362. %% Final request closes the connection.
  1363. Ref = gun:get(ConnPid, "/"),
  1364. {response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref),
  1365. {ok, <<"Hello world!">>} = gun:await_body(ConnPid, Ref),
  1366. {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, RespHeaders),
  1367. gun_down(ConnPid).
  1368. accept_at_least_1_empty_line_keepalive(Config) ->
  1369. doc("A configurable number of empty lines (CRLF) preceding the request "
  1370. "must be ignored. At least 1 empty line must be ignored. (RFC7230 3.5)"),
  1371. #{code := 200, client := Client} = do_raw(Config,
  1372. "GET / HTTP/1.1\r\n"
  1373. "Host: localhost\r\n"
  1374. "\r\n"
  1375. %% We send an extra CRLF that must be ignored.
  1376. "\r\n"),
  1377. ok = raw_send(Client,
  1378. "GET / HTTP/1.1\r\n"
  1379. "Host: localhost\r\n"
  1380. "\r\n"),
  1381. {'HTTP/1.1', 200, _, _} = cow_http:parse_status_line(raw_recv_head(Client)),
  1382. ok.
  1383. %skip_request_body_by_closing_connection(Config) ->
  1384. %%A server that doesn't want to read the entire body of a message
  1385. %%must close the connection, if possible after sending the "close"
  1386. %%connection option in the response. (RFC7230 6.3)
  1387. pipeline(Config) ->
  1388. doc("A server can receive more than one request before any response "
  1389. "is sent. This is called pipelining. Responses must be sent "
  1390. "in the same order as the requests. (RFC7230 6.3.2)"),
  1391. ConnPid = gun_open(Config),
  1392. Refs = [{
  1393. gun:get(ConnPid, "/"),
  1394. gun:delete(ConnPid, "/echo/method")
  1395. } || _ <- lists:seq(1, 25)],
  1396. _ = [begin
  1397. {response, nofin, 200, _} = gun:await(ConnPid, Ref1),
  1398. {ok, <<"Hello world!">>} = gun:await_body(ConnPid, Ref1),
  1399. {response, nofin, 200, _} = gun:await(ConnPid, Ref2),
  1400. {ok, <<"DELETE">>} = gun:await_body(ConnPid, Ref2)
  1401. end || {Ref1, Ref2} <- Refs],
  1402. ok.
  1403. %% @todo pipeline_parallel (safe methods can, others can't)
  1404. %The requests can be processed in parallel if they all have safe methods.
  1405. %@todo
  1406. %A server that does parallel pipelining must send responses in the
  1407. %same order as the requests came in. (RFC7230 5.6)
  1408. %@todo
  1409. %The server must reject abusive traffic by closing the connection.
  1410. %Abusive traffic can come from the form of too many requests in a
  1411. %given amount of time, or too many concurrent connections. Limits
  1412. %must be subject to configuration. (RFC7230 6.4)
  1413. close_inactive_connections(Config) ->
  1414. doc("The server must close inactive connections. The timeout "
  1415. "must be subject to configuration. (RFC7230 6.5)"),
  1416. Client = raw_open(Config),
  1417. {error, closed} = raw_recv(Client, 0, 6000).
  1418. %@todo
  1419. %The server must monitor connections for the close signal
  1420. %and close the socket on its end accordingly. (RFC7230 6.5)
  1421. %
  1422. %@todo
  1423. %A connection close may occur at any time. (RFC7230 6.5)
  1424. ignore_requests_after_request_connection_close(Config) ->
  1425. doc("The server must not process any request after "
  1426. "receiving the \"close\" connection option. (RFC7230 6.6)"),
  1427. Self = self(),
  1428. #{code := 200, client := Client} = do_raw(Config, [
  1429. "GET / HTTP/1.1\r\n"
  1430. "Host: localhost\r\n"
  1431. "Connection: close\r\n"
  1432. "\r\n"
  1433. "GET /send_message HTTP/1.1\r\n"
  1434. "Host: localhost\r\n"
  1435. "x-test-pid: ", pid_to_list(Self), "\r\n"
  1436. "\r\n"]),
  1437. {error, closed} = raw_recv(Client, 0, 1000),
  1438. %% We receive a message if the second request is wrongly processed.
  1439. receive
  1440. {Self, _, init, Req, Opts} ->
  1441. error({init, Req, Opts})
  1442. after 1000 ->
  1443. ok
  1444. end.
  1445. ignore_requests_after_response_connection_close(Config) ->
  1446. doc("The server must not process any request after "
  1447. "sending the \"close\" connection option. (RFC7230 6.6)"),
  1448. Self = self(),
  1449. Client = raw_open(Config),
  1450. ok = raw_send(Client, [
  1451. [
  1452. "GET / HTTP/1.1\r\n"
  1453. "Host: localhost\r\n"
  1454. "\r\n"
  1455. || _ <- lists:seq(1, 100)],
  1456. "GET /send_message HTTP/1.1\r\n"
  1457. "Host: localhost\r\n"
  1458. "x-test-pid: ", pid_to_list(Self), "\r\n"
  1459. "\r\n"]),
  1460. %% We have a separate test for the connection close so we don't
  1461. %% double check the connection gets closed here. We only need to
  1462. %% know whether the 101st request was wrongly processed.
  1463. receive
  1464. {Self, _, init, Req, Opts} ->
  1465. error({init, Req, Opts})
  1466. after 1000 ->
  1467. ok
  1468. end.
  1469. %@todo
  1470. %The server must close the connection in stages to avoid the
  1471. %TCP reset problem. The server starts by closing the write
  1472. %side of the socket. The server then reads until it detects
  1473. %the socket has been closed, until it can be certain its
  1474. %last response has been received by the client, or until
  1475. %a close or timeout occurs. The server then fully close the
  1476. %connection. (6.6)
  1477. %% Routing.
  1478. %```
  1479. %Host = authority ; same as authority-form
  1480. %```
  1481. reject_missing_host(Config) ->
  1482. doc("An HTTP/1.1 request that lacks a host header must be rejected with "
  1483. "a 400 status code and the closing of the connection. (RFC7230 5.4)"),
  1484. #{code := 400, client := Client} = do_raw(Config, [
  1485. "GET / HTTP/1.1\r\n"
  1486. "\r\n"]),
  1487. {error, closed} = raw_recv(Client, 0, 1000).
  1488. http10_allow_missing_host(Config0) ->
  1489. doc("An HTTP/1.0 request that lacks a host header may be accepted. "
  1490. "(RFC7230 5.4, RFC7230 5.5, RFC7230 A.1.1)"),
  1491. Routes = [{'_', [{"/echo/:key[/:arg]", echo_h, []}]}],
  1492. Config = cowboy_test:init_http(?FUNCTION_NAME, #{
  1493. env => #{dispatch => cowboy_router:compile(Routes)}
  1494. }, Config0),
  1495. try
  1496. #{code := 200, body := <<>>} = do_raw(Config, [
  1497. "GET /echo/host HTTP/1.0\r\n"
  1498. "\r\n"])
  1499. after
  1500. cowboy:stop_listener(?FUNCTION_NAME)
  1501. end.
  1502. reject_invalid_host(Config) ->
  1503. doc("A request with an invalid host header must be rejected with a "
  1504. "400 status code and the closing of the connection. (RFC7230 5.4)"),
  1505. #{code := 400, client := Client} = do_raw(Config, [
  1506. "GET / HTTP/1.1\r\n"
  1507. "Host: localhost:port\r\n"
  1508. "\r\n"]),
  1509. {error, closed} = raw_recv(Client, 0, 1000).
  1510. reject_userinfo(Config) ->
  1511. doc("An authority component with a userinfo component (and its "
  1512. "\"@\" delimiter) is invalid. The request must be rejected with "
  1513. "a 400 status code and the closing of the connection. (RFC7230 2.7.1)"),
  1514. #{code := 400, client := Client} = do_raw(Config, [
  1515. "GET / HTTP/1.1\r\n"
  1516. "Host: user@localhost\r\n"
  1517. "\r\n"]),
  1518. {error, closed} = raw_recv(Client, 0, 1000).
  1519. reject_absolute_form_different_host(Config) ->
  1520. doc("When using absolute-form the URI authority component must be "
  1521. "identical to the host header. Invalid requests must be rejected "
  1522. "with a 400 status code and the closing of the connection. (RFC7230 5.4)"),
  1523. #{code := 400, client := Client} = do_raw(Config, [
  1524. "GET http://example.org/ HTTP/1.1\r\n"
  1525. "Host: localhost\r\n"
  1526. "\r\n"]),
  1527. {error, closed} = raw_recv(Client, 0, 1000).
  1528. %reject_authority_form_different_host(Config) ->
  1529. %When using authority-form the URI authority component must be
  1530. %identical to the host header. Invalid requests must be rejected
  1531. %with a 400 status code and the closing of the connection.
  1532. empty_host(Config0) ->
  1533. doc("The host header is empty when the authority component is undefined. (RFC7230 5.4)"),
  1534. Routes = [{'_', [{"/echo/:key[/:arg]", echo_h, []}]}],
  1535. Config = cowboy_test:init_http(?FUNCTION_NAME, #{
  1536. env => #{dispatch => cowboy_router:compile(Routes)}
  1537. }, Config0),
  1538. try
  1539. #{code := 200, body := <<>>} = do_raw(Config, [
  1540. "GET /echo/host HTTP/1.1\r\n"
  1541. "Host:\r\n"
  1542. "\r\n"]),
  1543. #{code := 200, body := <<>>} = do_raw(Config, [
  1544. "GET /echo/host HTTP/1.1\r\n"
  1545. "Host: \r\n"
  1546. "\r\n"])
  1547. after
  1548. cowboy:stop_listener(?FUNCTION_NAME)
  1549. end.
  1550. %% The effective request URI can be rebuilt by concatenating scheme,
  1551. %% "://", authority, path and query components. (RFC7230 5.5)
  1552. %%
  1553. %% This is covered in req_SUITE in the tests for cowboy_req:uri/1,2.
  1554. reject_non_authoritative_host(Config) ->
  1555. doc("A request with a host header for which the origin server is "
  1556. "not authoritative must be rejected with a 400 status code. "
  1557. "(RFC7230 5.5, RFC7230 9.1)"),
  1558. #{code := 400} = do_raw(Config, [
  1559. "GET / HTTP/1.1\r\n"
  1560. "Host: ninenines.eu\r\n"
  1561. "\r\n"]),
  1562. ok.
  1563. %@todo
  1564. %Resources with identical URI except for the scheme component
  1565. %must be treated as different. (RFC7230 2.7.2)
  1566. %% Response.
  1567. %@todo
  1568. %A server can send more than one response per request only when a
  1569. %1xx response is sent preceding the final response. (RFC7230 5.6)
  1570. %
  1571. %```
  1572. %HTTP-response = status-line *( header-field CRLF ) CRLF [ message-body ]
  1573. %```
  1574. %
  1575. %@todo
  1576. %The response format must be followed strictly.
  1577. %
  1578. %```
  1579. %status-line = HTTP-version SP status-code SP reason-phrase CRLF
  1580. %status-code = 3DIGIT
  1581. %reason-phrase = *( HTAB / SP / VCHAR / obs-text )
  1582. %```
  1583. http10_request_http11_response(Config) ->
  1584. doc("A server must send its own HTTP version in responses. (RFC7230 2.6)"),
  1585. #{code := 200, version := 'HTTP/1.1'} = do_raw(Config, [
  1586. "GET / HTTP/1.0\r\n"
  1587. "Host: localhost\r\n"
  1588. "\r\n"]),
  1589. ok.
  1590. %@todo
  1591. %An HTTP/1.1 server may send an HTTP/1.0 version for compatibility purposes. (RFC7230 2.6)
  1592. %
  1593. %@todo
  1594. %RFC6585 defines additional status code a server can use to reject
  1595. %messages. (RFC7230 9.3, RFC6585)
  1596. %% Response headers.
  1597. %@todo
  1598. %In responses, OWS must be generated as SP or not generated
  1599. %at all. RWS must be generated as SP. BWS must not be
  1600. %generated. (RFC7230 3.2.3)
  1601. %
  1602. %```
  1603. %header-field = field-name ":" SP field-value
  1604. %
  1605. %field-name = token ; case-insensitive
  1606. %field-value = *( SP / %21-7E / %80-FF )
  1607. %```
  1608. %
  1609. %@todo
  1610. %In quoted-string found in field-value, quoted-pair must only be
  1611. %used for DQUOTE and backslash. (RFC7230 3.2.6)
  1612. %
  1613. %@todo
  1614. %HTTP header values must use US-ASCII encoding and must only send
  1615. %printable characters or SP. (RFC7230 3.2.4, RFC7230 9.4)
  1616. %
  1617. %@todo
  1618. %The server must not generate empty list elements in headers. (RFC7230 7)
  1619. %
  1620. %@todo
  1621. %When encoding an URI as part of a response, only characters that
  1622. %are reserved need to be percent-encoded. (RFC7230 2.7.3)
  1623. special_set_cookie_handling(Config) ->
  1624. doc("The set-cookie header must be handled as a special case. There "
  1625. "must be exactly one set-cookie header field per cookie. (RFC7230 3.2.2)"),
  1626. #{code := 200, headers := RespHeaders} = do_raw(Config, [
  1627. "GET /resp/set_resp_cookie3/multiple HTTP/1.1\r\n"
  1628. "Host: localhost\r\n"
  1629. "\r\n"]),
  1630. [_, _] = [H || H={<<"set-cookie">>, _} <- RespHeaders],
  1631. ok.
  1632. %@todo
  1633. %The server must list headers for or about the immediate connection
  1634. %in the connection header field. (RFC7230 6.1)
  1635. %
  1636. %@todo
  1637. %A server that does not support persistent connections must
  1638. %send "close" in every non-1xx response. (RFC7230 6.1)
  1639. %
  1640. %no_close_in_100_response(Config) ->
  1641. %no_close_in_101_response(Config) ->
  1642. %no_close_in_102_response(Config) ->
  1643. %A server must not send a "close" connection option
  1644. %in 1xx responses. (RFC7230 6.1)
  1645. %
  1646. %@todo
  1647. %The "close" connection must be sent in a message when the
  1648. %sender knows it will close the connection after fully sending
  1649. %the response. (RFC7230 6.6)
  1650. %
  1651. %@todo
  1652. %A server must close the connection after sending or
  1653. %receiving a "close" once the response has been sent. (RFC7230 6.6)
  1654. close_request_close_response(Config) ->
  1655. doc("A server must send a \"close\" in a response to a request "
  1656. "containing a \"close\". (RFC7230 6.6)"),
  1657. #{code := 200, headers := RespHeaders} = do_raw(Config, [
  1658. "GET / HTTP/1.1\r\n"
  1659. "Host: localhost\r\n"
  1660. "Connection: close\r\n"
  1661. "\r\n"]),
  1662. {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, RespHeaders),
  1663. ok.
  1664. %% Response body.
  1665. no_body_in_head_response(Config) ->
  1666. doc("Responses to HEAD requests never include a message body. (RFC7230 3.3)"),
  1667. Client = raw_open(Config),
  1668. ok = raw_send(Client, [
  1669. "HEAD / HTTP/1.1\r\n"
  1670. "Host: localhost\r\n"
  1671. "\r\n"]),
  1672. {_, 200, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),
  1673. {Headers, <<>>} = cow_http:parse_headers(Rest),
  1674. {_, LengthBin} = lists:keyfind(<<"content-length">>, 1, Headers),
  1675. Length = binary_to_integer(LengthBin),
  1676. {error, timeout} = raw_recv(Client, Length, 1000),
  1677. ok.
  1678. %% @todo test different ways to send a body in response
  1679. %%% @todo Implement CONNECT
  1680. %2xx responses to CONNECT requests never include a message
  1681. %body. (RFC7230 3.3)
  1682. %
  1683. %no_body_in_100_response(Config) ->
  1684. %no_body_in_101_response(Config) ->
  1685. %no_body_in_102_response(Config) ->
  1686. %1xx responses never include a message body. (RFC7230 3.3)
  1687. no_body_in_204_response(Config) ->
  1688. doc("204 responses never include a message body. (RFC7230 3.3)"),
  1689. Client = raw_open(Config),
  1690. ok = raw_send(Client, [
  1691. "GET /resp/reply2/204 HTTP/1.1\r\n"
  1692. "Host: localhost\r\n"
  1693. "\r\n"]),
  1694. {_, 204, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),
  1695. {_, <<>>} = cow_http:parse_headers(Rest),
  1696. {error, timeout} = raw_recv(Client, 1, 1000),
  1697. ok.
  1698. no_body_in_204_response_stream(Config) ->
  1699. doc("204 responses never include a message body. (RFC7230 3.3)"),
  1700. Client = raw_open(Config),
  1701. ok = raw_send(Client, [
  1702. "GET /resp/stream_reply2/204 HTTP/1.1\r\n"
  1703. "Host: localhost\r\n"
  1704. "\r\n"]),
  1705. {_, 204, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),
  1706. {_, <<>>} = cow_http:parse_headers(Rest),
  1707. {error, timeout} = raw_recv(Client, 1, 1000),
  1708. ok.
  1709. no_body_in_304_response(Config) ->
  1710. doc("304 responses never include a message body. (RFC7230 3.3)"),
  1711. Client = raw_open(Config),
  1712. ok = raw_send(Client, [
  1713. "GET /resp/reply2/304 HTTP/1.1\r\n"
  1714. "Host: localhost\r\n"
  1715. "\r\n"]),
  1716. {_, 304, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),
  1717. {_, <<>>} = cow_http:parse_headers(Rest),
  1718. {error, timeout} = raw_recv(Client, 1, 1000),
  1719. ok.
  1720. no_body_in_304_response_stream(Config) ->
  1721. doc("304 responses never include a message body. (RFC7230 3.3)"),
  1722. Client = raw_open(Config),
  1723. ok = raw_send(Client, [
  1724. "GET /resp/stream_reply2/304 HTTP/1.1\r\n"
  1725. "Host: localhost\r\n"
  1726. "\r\n"]),
  1727. {_, 304, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),
  1728. {_, <<>>} = cow_http:parse_headers(Rest),
  1729. {error, timeout} = raw_recv(Client, 1, 1000),
  1730. ok.
  1731. same_content_length_as_get_in_head_response(Config) ->
  1732. doc("Responses to HEAD requests can include a content-length header. "
  1733. "Its value must be the same as if the request was an unconditional "
  1734. "GET. (RFC7230 3.3, RFC7230 3.3.1, RFC7230 3.3.2)"),
  1735. Client = raw_open(Config),
  1736. ok = raw_send(Client, [
  1737. "HEAD / HTTP/1.1\r\n"
  1738. "Host: localhost\r\n"
  1739. "\r\n"]),
  1740. {_, 200, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),
  1741. {Headers, <<>>} = cow_http:parse_headers(Rest),
  1742. {_, <<"12">>} = lists:keyfind(<<"content-length">>, 1, Headers),
  1743. ok.
  1744. same_transfer_encoding_as_get_in_head_response(Config) ->
  1745. doc("Responses to HEAD requests can include a transfer-encoding header. "
  1746. "Its value must be the same as if the request was an unconditional "
  1747. "GET. (RFC7230 3.3, RFC7230 3.3.1, RFC7230 3.3.2)"),
  1748. Client = raw_open(Config),
  1749. ok = raw_send(Client, [
  1750. "HEAD /resp/stream_reply2/200 HTTP/1.1\r\n"
  1751. "Host: localhost\r\n"
  1752. "\r\n"]),
  1753. {_, 200, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),
  1754. {Headers, <<>>} = cow_http:parse_headers(Rest),
  1755. {_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  1756. ok.
  1757. %same_content_length_as_200_in_304_response(Config) ->
  1758. %same_transfer_encoding_as_200_in_304_response(Config) ->
  1759. %304 responses can include a
  1760. %content-length or transfer-encoding header. Their value must
  1761. %be the same as if the request was an unconditional GET. (RFC7230 3.3, RFC7230 3.3.1, RFC7230 3.3.2)
  1762. %
  1763. %no_content_length_in_100_response(Config) ->
  1764. %no_content_length_in_101_response(Config) ->
  1765. %no_content_length_in_102_response(Config) ->
  1766. %1xx, 204 responses and "2xx responses to CONNECT requests" must
  1767. %not include a content-length or transfer-encoding header. (RFC7230 3.3.1, RFC7230 3.3.2)
  1768. no_content_length_in_204_response(Config) ->
  1769. doc("204 responses must not include a content-length header. "
  1770. "(RFC7230 3.3.1, RFC7230 3.3.2)"),
  1771. Client = raw_open(Config),
  1772. ok = raw_send(Client, [
  1773. "GET /resp/reply3/204 HTTP/1.1\r\n"
  1774. "Host: localhost\r\n"
  1775. "\r\n"]),
  1776. {_, 204, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),
  1777. {Headers, <<>>} = cow_http:parse_headers(Rest),
  1778. false = lists:keyfind(<<"content-length">>, 1, Headers),
  1779. ok.
  1780. no_content_length_in_empty_304_response(Config) ->
  1781. doc("304 responses should not include a content-length header, "
  1782. "unless it matches the resource's and was therefore set "
  1783. "explicitly by the user. (RFC7230 3.3.1, RFC7230 3.3.2)"),
  1784. Client = raw_open(Config),
  1785. ok = raw_send(Client, [
  1786. "GET /resp/reply3/304 HTTP/1.1\r\n"
  1787. "Host: localhost\r\n"
  1788. "\r\n"]),
  1789. {_, 304, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),
  1790. {Headers, <<>>} = cow_http:parse_headers(Rest),
  1791. false = lists:keyfind(<<"content-length">>, 1, Headers),
  1792. ok.
  1793. %%% @todo CONNECT no_content_length_in_2xx_response_to_connect_request(Config) ->
  1794. %no_transfer_encoding_in_100_response(Config) ->
  1795. %no_transfer_encoding_in_101_response(Config) ->
  1796. %no_transfer_encoding_in_102_response(Config) ->
  1797. %1xx, 204 responses and "2xx responses to CONNECT requests" must
  1798. %not include a content-length or transfer-encoding header. (RFC7230 3.3.1, RFC7230 3.3.2)
  1799. %% We only send transfer-encoding when streaming a response body.
  1800. %% We therefore need a streamed response in order to see a potential bug.
  1801. no_transfer_encoding_in_204_response(Config) ->
  1802. doc("204 responses must not include a transfer-encoding header. "
  1803. "(RFC7230 3.3.1, RFC7230 3.3.2)"),
  1804. Client = raw_open(Config),
  1805. ok = raw_send(Client, [
  1806. "GET /resp/stream_reply2/204 HTTP/1.1\r\n"
  1807. "Host: localhost\r\n"
  1808. "\r\n"]),
  1809. {_, 204, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),
  1810. {Headers, <<>>} = cow_http:parse_headers(Rest),
  1811. false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  1812. ok.
  1813. %%% @todo CONNECT no_transfer_encoding_in_2xx_response_to_connect_request(Config) ->
  1814. %1xx, 204 responses and "2xx responses to CONNECT requests" must
  1815. %not include a content-length or transfer-encoding header. (RFC7230 3.3.1, RFC7230 3.3.2)
  1816. %
  1817. %```
  1818. %message-body = *OCTET
  1819. %```
  1820. %
  1821. %The message body is the octets after decoding any transfer
  1822. %codings. (RFC7230 3.3)
  1823. content_length_0_when_no_body(Config) ->
  1824. doc("When the length is known in advance, the server must send a "
  1825. "content-length header, including if the length is 0. (RFC7230 3.3.2, RFC7230 3.3.3)"),
  1826. #{code := 200, headers := RespHeaders} = do_raw(Config, [
  1827. "GET /resp/reply2/200 HTTP/1.1\r\n"
  1828. "Host: localhost\r\n"
  1829. "\r\n"]),
  1830. {_, <<"0">>} = lists:keyfind(<<"content-length">>, 1, RespHeaders),
  1831. ok.
  1832. content_length_response(Config) ->
  1833. doc("When the length is known in advance, the server must send a "
  1834. "content-length header. (RFC7230 3.3.2, RFC7230 3.3.3)"),
  1835. #{code := 200, headers := RespHeaders} = do_raw(Config, [
  1836. "GET / HTTP/1.1\r\n"
  1837. "Host: localhost\r\n"
  1838. "\r\n"]),
  1839. {_, <<"12">>} = lists:keyfind(<<"content-length">>, 1, RespHeaders),
  1840. ok.
  1841. chunked_response(Config) ->
  1842. doc("When the length is not known in advance, the chunked transfer-encoding "
  1843. "must be used. (RFC7230 3.3.2, RFC7230 3.3.3)"),
  1844. #{code := 200, headers := RespHeaders} = do_raw(Config, [
  1845. "GET /resp/stream_reply2/200 HTTP/1.1\r\n"
  1846. "Host: localhost\r\n"
  1847. "\r\n"]),
  1848. {_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, RespHeaders),
  1849. %% @todo We probably want to check the body received too.
  1850. ok.
  1851. %compat_no_content_length_or_transfer_encoding_close_on_body_end(Config) ->
  1852. %For compatibility purposes a server can send no content-length or
  1853. %transfer-encoding header. In this case the connection must be
  1854. %closed after the response has been sent fully. (RFC7230 3.3.2, RFC7230 3.3.3)
  1855. no_content_length_if_transfer_encoding(Config) ->
  1856. doc("The content-length header must not be sent when a transfer-encoding "
  1857. "header already exists. (RFC7230 3.3.2)"),
  1858. #{code := 200, headers := RespHeaders} = do_raw(Config, [
  1859. "GET /resp/stream_reply2/200 HTTP/1.1\r\n"
  1860. "Host: localhost\r\n"
  1861. "\r\n"]),
  1862. false = lists:keyfind(<<"content-length">>, 1, RespHeaders),
  1863. ok.
  1864. %@todo
  1865. %The server must not apply the chunked transfer-encoding more than
  1866. %once. (RFC7230 3.3.1)
  1867. %
  1868. %@todo
  1869. %The server must apply the chunked transfer-encoding last. (RFC7230 3.3.1)
  1870. http10_request_no_transfer_encoding_in_response(Config) ->
  1871. doc("The transfer-encoding header must not be sent in responses to "
  1872. "HTTP/1.0 requests, or in responses that use the HTTP/1.0 version. "
  1873. "No transfer codings must be applied in these cases. "
  1874. "(RFC7230 3.3.1, RFC7230 A.1.3)"),
  1875. Client = raw_open(Config),
  1876. ok = raw_send(Client, [
  1877. "GET /resp/stream_reply2/200 HTTP/1.0\r\n"
  1878. "Host: localhost\r\n"
  1879. "\r\n"]),
  1880. {_, 200, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),
  1881. {RespHeaders, Body0} = cow_http:parse_headers(Rest),
  1882. false = lists:keyfind(<<"content-length">>, 1, RespHeaders),
  1883. false = lists:keyfind(<<"transfer-encoding">>, 1, RespHeaders),
  1884. Body = <<0:8000000>>,
  1885. {ok, Body1} = raw_recv(Client, byte_size(Body) - byte_size(Body0), 5000),
  1886. Body = << Body0/binary, Body1/binary >>,
  1887. %% The end of body is indicated by a connection close.
  1888. {error, closed} = raw_recv(Client, 0, 1000),
  1889. ok.
  1890. no_te_no_trailers(Config) ->
  1891. doc("Trailers can only be sent if the request includes a TE header "
  1892. "containing \"trailers\". (RFC7230 4.1.2)"),
  1893. #{code := 200, headers := RespHeaders} = do_raw(Config, [
  1894. "GET /resp/stream_trailers HTTP/1.1\r\n"
  1895. "Host: localhost\r\n"
  1896. "\r\n"]),
  1897. {_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, RespHeaders),
  1898. false = lists:keyfind(<<"trailer">>, 1, RespHeaders),
  1899. %% @todo We probably want to check the body received too.
  1900. ok.
  1901. te_trailers(Config) ->
  1902. doc("Trailers can only be sent if the request includes a TE header "
  1903. "containing \"trailers\". (RFC7230 4.1.2)"),
  1904. #{code := 200, headers := RespHeaders} = do_raw(Config, [
  1905. "GET /resp/stream_trailers HTTP/1.1\r\n"
  1906. "Host: localhost\r\n"
  1907. "TE: trailers\r\n"
  1908. "\r\n"]),
  1909. {_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, RespHeaders),
  1910. {_, <<"grpc-status">>} = lists:keyfind(<<"trailer">>, 1, RespHeaders),
  1911. %% @todo We probably want to check the body received too.
  1912. ok.
  1913. te_ignore_chunked(Config) ->
  1914. doc("The presence of \"chunked\" in a TE header must be ignored as it "
  1915. "is always acceptable with HTTP/1.1. (RFC7230 4.3)"),
  1916. #{code := 200, headers := RespHeaders} = do_raw(Config, [
  1917. "GET /resp/stream_reply2/200 HTTP/1.1\r\n"
  1918. "Host: localhost\r\n"
  1919. "TE: chunked\r\n"
  1920. "\r\n"]),
  1921. {_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, RespHeaders),
  1922. %% @todo We probably want to check the body received too.
  1923. ok.
  1924. te_ignore_chunked_0(Config) ->
  1925. doc("The presence of \"chunked\" in a TE header must be ignored as it "
  1926. "is always acceptable with HTTP/1.1. (RFC7230 4.3)"),
  1927. #{code := 200, headers := RespHeaders} = do_raw(Config, [
  1928. "GET /resp/stream_reply2/200 HTTP/1.1\r\n"
  1929. "Host: localhost\r\n"
  1930. "TE: chunked;q=0\r\n"
  1931. "\r\n"]),
  1932. {_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, RespHeaders),
  1933. %% @todo We probably want to check the body received too.
  1934. ok.
  1935. %%% @todo te_not_acceptable_coding(Config) ->
  1936. %A qvalue of 0 in the TE header means "not acceptable". (RFC7230 4.3)
  1937. %
  1938. %@todo
  1939. %The lack of a TE header or an empty TE header means only "chunked"
  1940. %(with no trailers) or no transfer-encoding is acceptable. (RFC7230 4.3)
  1941. %
  1942. %@todo
  1943. %Trailer headers must be listed in the trailer header field value. (RFC7230 4.4)
  1944. %% Upgrade.
  1945. %```
  1946. %Upgrade = 1#protocol
  1947. %
  1948. %protocol = protocol-name ["/" protocol-version]
  1949. %protocol-name = token
  1950. %protocol-version = token
  1951. %```
  1952. %
  1953. %The upgrade header contains the list of protocols the
  1954. %client wishes to upgrade to, in order of preference. (RFC7230 6.7)
  1955. upgrade_safely_ignored(Config) ->
  1956. doc("The upgrade header can be safely ignored. (RFC7230 6.7)"),
  1957. #{code := 200} = do_raw(Config,
  1958. "GET / HTTP/1.1\r\n"
  1959. "Host: localhost\r\n"
  1960. "Connection: upgrade\r\n"
  1961. "Upgrade: websocket\r\n"
  1962. "\r\n").
  1963. %upgrade_must_be_in_connection_header(Config) ->
  1964. %The upgrade header must be listed under the connection header,
  1965. %or must be ignored otherwise. (RFC7230 6.7)
  1966. %
  1967. %@todo
  1968. %A server accepting an upgrade request must send a 101 status
  1969. %code with a upgrade header listing the protocol(s) it upgrades
  1970. %to, in layer-ascending order. In addition the upgrade header
  1971. %must be listed in the connection header. (RFC7230 6.7)
  1972. %
  1973. %%A server must not switch to a protocol not listed in the
  1974. %%request's upgrade header. (RFC7230 6.7)
  1975. %
  1976. %@todo
  1977. %A server that sends a 426 status code must include a upgrade
  1978. %header listing acceptable protocols in order of preference. (RFC7230 6.7)
  1979. %
  1980. %@todo
  1981. %A server can send a upgrade header to any response to advertise
  1982. %its support for other protocols listed in order of preference. (RFC7230 6.7)
  1983. %
  1984. %@todo
  1985. %Immediately after a server responds with a 101 status code
  1986. %it must respond to the original request using the new protocol. (RFC7230 6.7)
  1987. %
  1988. %@todo
  1989. %%A server must not switch protocols unless the original message's
  1990. %%semantics can be honored by the new protocol. OPTIONS requests
  1991. %%can be honored by any protocol. (RFC7230 6.7)
  1992. %
  1993. %http10_ignore_upgrade_header(Config) ->
  1994. %A server must ignore an upgrade header received by an HTTP/1.0
  1995. %request. (RFC7230 6.7)
  1996. %
  1997. %expect_then_upgrade(Config) ->
  1998. %A server receiving both an upgrade header and an expect header
  1999. %containing "100-continue" must send a 100 response before the
  2000. %101 response. (RFC7230 6.7)
  2001. %
  2002. %The upgrade header field cannot be used for switching the
  2003. %connection protocol (e.g. TCP) or switching connections. (RFC7230 6.7)
  2004. %% Compatibility.
  2005. %@todo
  2006. %A server can choose to be non-conformant to the specifications
  2007. %for the sake of compatibility. Such behavior can be enabled
  2008. %through configuration and/or software identification. (RFC7230 2.5)