rfc7230_SUITE.erl 81 KB

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