rfc7230_SUITE.erl 73 KB

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