rfc7230_SUITE.erl 74 KB

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