rfc7230_SUITE.erl 81 KB

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