rfc7230_SUITE.erl 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487
  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. -import(ct_helper, [doc/1]).
  17. -import(cowboy_test, [raw_open/1]).
  18. -import(cowboy_test, [raw_send/2]).
  19. -import(cowboy_test, [raw_recv_head/1]).
  20. -import(cowboy_test, [raw_recv/3]).
  21. all() -> [{group, http}].
  22. groups() -> [{http, [parallel], ct_helper:all(?MODULE)}]. %% @todo parallel
  23. init_per_group(Name = http, Config) ->
  24. cowboy_test:init_http(Name = http, #{
  25. env => #{dispatch => cowboy_router:compile(init_routes(Config))}
  26. }, Config).
  27. end_per_group(Name, _) ->
  28. ok = cowboy:stop_listener(Name).
  29. init_routes(_) -> [
  30. {"localhost", [
  31. {"/", hello_h, []},
  32. {"/echo/:key", echo_h, []}
  33. %% @todo Something is clearly wrong about routing * right now.
  34. %% {"*", asterisk_h, []}
  35. ]},
  36. {"127.0.0.1", [{"/echo/:key", echo_h, []}]},
  37. {"example.org", [{"/echo/:key", echo_h, []}]}
  38. %% @todo Add IPv6 addresses support to the router. This fails:
  39. %% {"[2001:db8:85a3::8a2e:370:7334]", [{"/echo/:key", echo_h, []}]}
  40. ].
  41. do_raw(Config, Data) ->
  42. Client = raw_open(Config),
  43. ok = raw_send(Client, Data),
  44. {Version, Code, Reason, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),
  45. {Headers, Rest2} = cow_http:parse_headers(Rest),
  46. case lists:keyfind(<<"content-length">>, 1, Headers) of
  47. {_, LengthBin} when LengthBin =/= <<"0">> ->
  48. Length = binary_to_integer(LengthBin),
  49. Body = if
  50. byte_size(Rest2) =:= Length -> Rest2;
  51. true ->
  52. {ok, Body0} = raw_recv(Client, binary_to_integer(LengthBin) - byte_size(Rest2), 5000),
  53. << Rest2/bits, Body0/bits >>
  54. end,
  55. #{client => Client, version => Version, code => Code, reason => Reason, headers => Headers, body => Body};
  56. _ ->
  57. #{client => Client, version => Version, code => Code, reason => Reason, headers => Headers, body => <<>>}
  58. end.
  59. %% Listener.
  60. %% @todo Add to documentation.
  61. %The default port for "http" connections is 80. The connection
  62. %uses plain TCP. (RFC7230 2.7.1)
  63. %
  64. %The default port for "https" connections is 443. The connection
  65. %uses TLS. (RFC7230 2.7.2)
  66. %
  67. %Any other port may be used for either of them.
  68. %% Before the request.
  69. accept_at_least_1_empty_line(Config) ->
  70. doc("A configurable number of empty lines (CRLF) preceding the request "
  71. "must be ignored. At least 1 empty line must be ignored. (RFC7230 3.5)"),
  72. #{code := 200} = do_raw(Config,
  73. "\r\n"
  74. "GET / HTTP/1.1\r\n"
  75. "Host: localhost\r\n"
  76. "\r\n").
  77. reject_response(Config) ->
  78. doc("When receiving a response instead of a request, identified by the "
  79. "status-line which starts with the HTTP version, the server must "
  80. "reject the message with a 400 status code and close the connection. (RFC7230 3.1)"),
  81. #{code := 400, client := Client} = do_raw(Config,
  82. "HTTP/1.1 200 OK\r\n"
  83. "\r\n"),
  84. {error, closed} = raw_recv(Client, 0, 1000).
  85. %% Request.
  86. only_parse_necessary_elements(Config) ->
  87. doc("It is only necessary to parse elements required to process the request. (RFC7230 2.5)"),
  88. #{code := 200} = do_raw(Config,
  89. "GET / HTTP/1.1\r\n"
  90. "Host: localhost\r\n"
  91. "Content-type: purposefully bad header value\r\n"
  92. "\r\n").
  93. %% @todo Add to documentation.
  94. %Parsed elements are subject to configurable limits. A server must
  95. %be able to parse elements at least as long as it generates. (RFC7230 2.5)
  96. no_empty_line_after_request_line(Config) ->
  97. doc("The general format of HTTP requests is strict. No empty line is "
  98. "allowed in-between components except for the empty line "
  99. "indicating the end of the list of headers."),
  100. #{code := 400} = do_raw(Config,
  101. "GET / HTTP/1.1\r\n"
  102. "\r\n"
  103. "Host: localhost\r\n"
  104. "\r\n").
  105. no_empty_line_in_headers(Config) ->
  106. doc("The general format of HTTP requests is strict. No empty line is "
  107. "allowed in-between components except for the empty line "
  108. "indicating the end of the list of headers."),
  109. #{code := 400} = do_raw(Config,
  110. "GET / HTTP/1.1\r\n"
  111. "User-Agent: RFC7230\r\n"
  112. "\r\n"
  113. "Host: localhost\r\n"
  114. "\r\n").
  115. timeout_before_request_line(Config) ->
  116. doc("The time the request (request line and headers) takes to be "
  117. "received by the server must be limited and subject to configuration. "
  118. "No response must be sent before closing if no request was initiated "
  119. "by the reception of a complete request-line."),
  120. Client = raw_open(Config),
  121. ok = raw_send(Client, "GET / HTTP/1.1\r"),
  122. {error, closed} = raw_recv(Client, 0, 6000).
  123. timeout_after_request_line(Config) ->
  124. doc("The time the request (request line and headers) takes to be "
  125. "received by the server must be limited and subject to configuration. "
  126. "A 408 status code must be sent if the request line was received."),
  127. #{code := 408, client := Client} = do_raw(Config, "GET / HTTP/1.1\r\n"),
  128. {error, closed} = raw_recv(Client, 0, 6000).
  129. %% @todo Add an HTTP/1.0 test suite.
  130. %An HTTP/1.1 server must understand any valid HTTP/1.0 request,
  131. %and respond to those with an HTTP/1.1 message that only use
  132. %features understood or safely ignored by HTTP/1.0 clients. (RFC7230 A)
  133. %% Request line.
  134. limit_request_line_8000(Config) ->
  135. doc("It is recommended to limit the request-line length to a configurable "
  136. "limit of at least 8000 octets."),
  137. LongPath = ["/long-path" || _ <- lists:seq(1, 799)],
  138. #{code := 200} = do_raw(Config, [
  139. "GET /?qs=", LongPath, " HTTP/1.1\r\n"
  140. "Host: localhost\r\n"
  141. "\r\n"]).
  142. limit_request_line_9000(Config) ->
  143. doc("It is recommended to limit the request-line length to a configurable "
  144. "limit of at least 8000 octets. A request line too long must be rejected "
  145. "with a 414 status code and the closing of the connection. (RFC7230 3.1.1)"),
  146. LongPath = ["/long-path" || _ <- lists:seq(1, 899)],
  147. #{code := 414, client := Client} = do_raw(Config, [
  148. "GET /very", LongPath, " HTTP/1.1\r\n"
  149. "Host: localhost\r\n"
  150. "\r\n"]),
  151. {error, closed} = raw_recv(Client, 0, 1000).
  152. %% Method.
  153. reject_invalid_method(Config) ->
  154. doc("The request method is defined as 1+ token characters. An invalid "
  155. "method must be rejected with a 400 status code and the "
  156. "closing of the connection. (RFC7230 3.1.1, RFC7230 3.2.6)"),
  157. #{code := 400, client := Client} = do_raw(Config,
  158. "GET\0 / HTTP/1.1\r\n"
  159. "Host: localhost\r\n"
  160. "\r\n"),
  161. {error, closed} = raw_recv(Client, 0, 1000).
  162. reject_empty_method(Config) ->
  163. doc("The request method is defined as 1+ token characters. An empty "
  164. "method must be rejected with a 400 status code and the "
  165. "closing of the connection. (RFC7230 3.1.1, RFC7230 3.2.6)"),
  166. #{code := 400, client := Client} = do_raw(Config,
  167. " / HTTP/1.1\r\n"
  168. "Host: localhost\r\n"
  169. "\r\n"),
  170. {error, closed} = raw_recv(Client, 0, 1000).
  171. %% @todo We probably want to directly match commonly used methods.
  172. %In practice the only characters in use by registered methods are
  173. %uppercase letters [A-Z] and the dash "-". (IANA HTTP Method Registry)
  174. limit_method_name(Config) ->
  175. doc("The length of the method must be subject to a configurable limit. "
  176. "A method too long must be rejected with a 501 status code and the "
  177. "closing of the connection. A good default for the method length limit "
  178. "is the longest method length the server implements. (RFC7230 3.1.1)"),
  179. LongMethod = [$G || _ <- lists:seq(1, 1000)],
  180. #{code := 501, client := Client} = do_raw(Config, [
  181. LongMethod, " / HTTP/1.1\r\n"
  182. "Host: localhost\r\n"
  183. "\r\n"]),
  184. {error, closed} = raw_recv(Client, 0, 1000).
  185. %% Between method and request-target.
  186. reject_tab_between_method_and_request_target(Config) ->
  187. doc("A request that uses anything other than SP as separator between "
  188. "the method and the request-target must be rejected with a 400 "
  189. "status code and the closing of the connection. (RFC7230 3.1.1, RFC7230 3.5)"),
  190. #{code := 400, client := Client} = do_raw(Config,
  191. "GET\t/ HTTP/1.1\r\n"
  192. "Host: localhost\r\n"
  193. "\r\n"),
  194. {error, closed} = raw_recv(Client, 0, 1000).
  195. reject_two_sp_between_method_and_request_target(Config) ->
  196. doc("A request that uses anything other than SP as separator between "
  197. "the method and the request-target must be rejected with a 400 "
  198. "status code and the closing of the connection. (RFC7230 3.1.1, RFC7230 3.5)"),
  199. #{code := 400, client := Client} = do_raw(Config,
  200. "GET / HTTP/1.1\r\n"
  201. "Host: localhost\r\n"
  202. "\r\n"),
  203. {error, closed} = raw_recv(Client, 0, 1000).
  204. %% Request target.
  205. ignore_uri_fragment_after_path(Config) ->
  206. doc("The fragment part of the target URI is not sent. It must be "
  207. "ignored by a server receiving it. (RFC7230 5.1)"),
  208. Echo = <<"http://localhost/echo/url">>,
  209. #{code := 200, body := Echo} = do_raw(Config,
  210. "GET /echo/url#fragment HTTP/1.1\r\n"
  211. "Host: localhost\r\n"
  212. "\r\n").
  213. ignore_uri_fragment_after_query(Config) ->
  214. doc("The fragment part of the target URI is not sent. It must be "
  215. "ignored by a server receiving it. (RFC7230 5.1)"),
  216. Echo = <<"http://localhost/echo/url?key=value">>,
  217. #{code := 200, body := Echo} = do_raw(Config,
  218. "GET /echo/url?key=value#fragment HTTP/1.1\r\n"
  219. "Host: localhost\r\n"
  220. "\r\n").
  221. %% Request target: origin-form.
  222. must_understand_origin_form(Config) ->
  223. doc("A server must be able to handle at least origin-form and absolute-form. (RFC7230 5.3.2)"),
  224. #{code := 200} = do_raw(Config,
  225. "GET / HTTP/1.1\r\n"
  226. "Host: localhost\r\n"
  227. "\r\n").
  228. origin_form_reject_if_connect(Config) ->
  229. doc("origin-form is used when the client does not connect to a proxy, "
  230. "does not use the CONNECT method and does not issue a site-wide "
  231. "OPTIONS request. (RFC7230 5.3.1)"),
  232. #{code := 400, client := Client} = do_raw(Config,
  233. "CONNECT / HTTP/1.1\r\n"
  234. "Host: localhost\r\n"
  235. "\r\n"),
  236. {error, closed} = raw_recv(Client, 0, 1000).
  237. %% @todo Equivalent test for https.
  238. origin_form_tcp_scheme(Config) ->
  239. doc("The scheme is either resolved from configuration or is \"https\" "
  240. "when on a TLS connection and \"http\" otherwise. (RFC7230 5.5)"),
  241. Echo = <<"http://localhost/echo/url">>,
  242. #{code := 200, body := Echo} = do_raw(Config,
  243. "GET /echo/url HTTP/1.1\r\n"
  244. "Host: localhost\r\n"
  245. "\r\n").
  246. origin_form_path(Config) ->
  247. doc("The absolute-path always starts with \"/\" and ends with either \"?\", \"#\" "
  248. "or the end of the URI. (RFC3986 3.3)"),
  249. Echo = <<"/echo/path">>,
  250. #{code := 200, body := Echo} = do_raw(Config,
  251. "GET /echo/path HTTP/1.1\r\n"
  252. "Host: localhost\r\n"
  253. "\r\n").
  254. origin_form_path_query(Config) ->
  255. doc("The absolute-path always starts with \"/\" and ends with either \"?\", \"#\" "
  256. "or the end of the URI. (RFC3986 3.3)"),
  257. Echo = <<"/echo/path">>,
  258. #{code := 200, body := Echo} = do_raw(Config,
  259. "GET /echo/path?key=value HTTP/1.1\r\n"
  260. "Host: localhost\r\n"
  261. "\r\n").
  262. origin_form_path_fragment(Config) ->
  263. doc("The absolute-path always starts with \"/\" and ends with either \"?\", \"#\" "
  264. "or the end of the URI. (RFC3986 3.3)"),
  265. Echo = <<"/echo/path">>,
  266. #{code := 200, body := Echo} = do_raw(Config,
  267. "GET /echo/path#fragment HTTP/1.1\r\n"
  268. "Host: localhost\r\n"
  269. "\r\n").
  270. origin_form_query(Config) ->
  271. doc("The query starts with \"?\" and ends with \"#\" or the end of the URI. (RFC3986 3.4)"),
  272. Echo = <<"key=value">>,
  273. #{code := 200, body := Echo} = do_raw(Config,
  274. "GET /echo/qs?key=value HTTP/1.1\r\n"
  275. "Host: localhost\r\n"
  276. "\r\n").
  277. origin_form_query_fragment(Config) ->
  278. doc("The query starts with \"?\" and ends with \"#\" or the end of the URI. (RFC3986 3.4)"),
  279. Echo = <<"key=value">>,
  280. #{code := 200, body := Echo} = do_raw(Config,
  281. "GET /echo/qs?key=value#fragment HTTP/1.1\r\n"
  282. "Host: localhost\r\n"
  283. "\r\n").
  284. %% @todo origin_form: reject paths with too large depth or query strings with too many keys
  285. %% Request target: absolute-form.
  286. must_understand_absolute_form(Config) ->
  287. doc("A server must be able to handle at least origin-form and absolute-form. (RFC7230 5.3.2)"),
  288. #{code := 200} = do_raw(Config,
  289. "GET http://localhost HTTP/1.1\r\n"
  290. "Host: localhost\r\n"
  291. "\r\n").
  292. absolute_form_case_insensitive_scheme(Config) ->
  293. doc("The scheme is case insensitive and normally provided in lowercase. (RFC7230 2.7.3)"),
  294. Echo = <<"http://localhost/echo/url">>,
  295. #{code := 200, body := Echo} = do_raw(Config,
  296. "GET HttP://localhost/echo/url HTTP/1.1\r\n"
  297. "Host: localhost\r\n"
  298. "\r\n").
  299. absolute_form_case_insensitive_host(Config) ->
  300. doc("The host is case insensitive and normally provided in lowercase. (RFC7230 2.7.3)"),
  301. Echo = <<"http://localhost/echo/url">>,
  302. #{code := 200, body := Echo} = do_raw(Config,
  303. "GET http://LoCaLHOsT/echo/url HTTP/1.1\r\n"
  304. "Host: localhost\r\n"
  305. "\r\n").
  306. absolute_form_reject_unknown_schemes(Config) ->
  307. doc("Unknown schemes must be rejected with a 400 status code and the closing of the connection."),
  308. #{code := 400, client := Client} = do_raw(Config,
  309. "GET bad://localhost/ HTTP/1.1\r\n"
  310. "Host: localhost\r\n"
  311. "\r\n"),
  312. {error, closed} = raw_recv(Client, 0, 1000).
  313. %% @todo Equivalent test for https.
  314. absolute_form_drop_scheme_tcp(Config) ->
  315. doc("The scheme provided with the request must be dropped. The effective "
  316. "scheme is either resolved from configuration or is \"https\" when on "
  317. "a TLS connection and \"http\" otherwise. (RFC7230 5.5)"),
  318. Echo = <<"http://localhost/echo/url">>,
  319. #{code := 200, body := Echo} = do_raw(Config,
  320. "GET https://localhost/echo/url HTTP/1.1\r\n"
  321. "Host: localhost\r\n"
  322. "\r\n").
  323. absolute_form_reject_userinfo(Config) ->
  324. doc("An authority component with a userinfo component (and its "
  325. "\"@\" delimiter) is invalid. The request must be rejected with "
  326. "a 400 status code and the closing of the connection. (RFC7230 2.7.1)"),
  327. #{code := 400, client := Client} = do_raw(Config,
  328. "GET http://username:password@localhost HTTP/1.1\r\n"
  329. "Host: localhost\r\n"
  330. "\r\n"),
  331. {error, closed} = raw_recv(Client, 0, 1000).
  332. absolute_form_reject_missing_host_without_path(Config) ->
  333. doc("A URI with a missing host identifier is invalid. The request must "
  334. "be rejected with a 400 status code and the closing of the connection. (RFC7230 2.7.1)"),
  335. #{code := 400, client := Client} = do_raw(Config,
  336. "GET http:// HTTP/1.1\r\n"
  337. "Host: localhost\r\n"
  338. "\r\n"),
  339. {error, closed} = raw_recv(Client, 0, 1000).
  340. absolute_form_reject_missing_host_with_path(Config) ->
  341. doc("A URI with a missing host identifier is invalid. The request must "
  342. "be rejected with a 400 status code and the closing of the connection. (RFC7230 2.7.1)"),
  343. #{code := 400, client := Client} = do_raw(Config,
  344. "GET http:/// HTTP/1.1\r\n"
  345. "Host: localhost\r\n"
  346. "\r\n"),
  347. {error, closed} = raw_recv(Client, 0, 1000).
  348. absolute_form_ipv4(Config) ->
  349. doc("Absolute form with an IPv4 address for the host. (RFC3986 3.2.2)"),
  350. Echo = <<"127.0.0.1">>,
  351. #{code := 200, body := Echo} = do_raw(Config,
  352. "GET http://127.0.0.1/echo/host HTTP/1.1\r\n"
  353. "Host: 127.0.0.1\r\n"
  354. "\r\n").
  355. absolute_form_ipv4_port(Config) ->
  356. doc("Absolute form with an IPv4 address for the host and a port number. (RFC3986 3.2.2)"),
  357. Host = <<"127.0.0.1">>,
  358. #{code := 200, body := Host} = do_raw(Config,
  359. "GET http://127.0.0.1:8080/echo/host HTTP/1.1\r\n"
  360. "Host: 127.0.0.1:8080\r\n"
  361. "\r\n"),
  362. Port = <<"8080">>,
  363. #{code := 200, body := Port} = do_raw(Config,
  364. "GET http://127.0.0.1:8080/echo/port HTTP/1.1\r\n"
  365. "Host: 127.0.0.1:8080\r\n"
  366. "\r\n").
  367. %% @todo We need the router to support IPv6 addresses to write proper tests for these:
  368. %absolute_form_ipv6(Config) ->
  369. %absolute_form_ipv6_ipv4(Config) ->
  370. %absolute_form_ipv6_zoneid(Config) ->
  371. absolute_form_reg_name(Config) ->
  372. doc("Absolute form with a regular name for the host. (RFC3986 3.2.2)"),
  373. Echo = <<"example.org">>,
  374. #{code := 200, body := Echo} = do_raw(Config,
  375. "GET http://example.org/echo/host HTTP/1.1\r\n"
  376. "Host: example.org\r\n"
  377. "\r\n").
  378. absolute_form_reg_name_port(Config) ->
  379. doc("Absolute form with an IPv4 address for the host and a port number. (RFC3986 3.2.2)"),
  380. Host = <<"example.org">>,
  381. #{code := 200, body := Host} = do_raw(Config,
  382. "GET http://example.org:8080/echo/host HTTP/1.1\r\n"
  383. "Host: example.org:8080\r\n"
  384. "\r\n"),
  385. Port = <<"8080">>,
  386. #{code := 200, body := Port} = do_raw(Config,
  387. "GET http://example.org:8080/echo/port HTTP/1.1\r\n"
  388. "Host: example.org:8080\r\n"
  389. "\r\n").
  390. absolute_form_limit_host(Config) ->
  391. doc("The maximum length for the host component of the URI must be subject "
  392. "to a configurable limit. A good default is 255 characters. "
  393. "(RFC7230 3.1.1, RFC3986 3.2.2, RFC1034 3.1)"),
  394. LongHost = ["host." || _ <- lists:seq(1, 100)],
  395. #{code := 414, client := Client} = do_raw(Config, [
  396. "GET http://", LongHost, "/ HTTP/1.1\r\n"
  397. "Host: ", LongHost, "\r\n"
  398. "\r\n"]),
  399. {error, closed} = raw_recv(Client, 0, 1000).
  400. absolute_form_invalid_port_0(Config) ->
  401. doc("Port number 0 is reserved. The request must be rejected and the connection closed."),
  402. #{code := 400, client := Client} = do_raw(Config,
  403. "GET http://localhost:0/ HTTP/1.1\r\n"
  404. "Host: localhost:0\r\n"
  405. "\r\n"),
  406. {error, closed} = raw_recv(Client, 0, 1000).
  407. absolute_form_invalid_port_65536(Config) ->
  408. doc("Port numbers above 65535 are invalid. The request must be rejected and the connection closed."),
  409. #{code := 400, client := Client} = do_raw(Config,
  410. "GET http://localhost:65536/ HTTP/1.1\r\n"
  411. "Host: localhost:65536\r\n"
  412. "\r\n"),
  413. {error, closed} = raw_recv(Client, 0, 1000).
  414. %% @todo The RFC says to discard the Host header if we are a proxy,
  415. %% and replace it with the content of absolute-form. This means
  416. %% that we should probably keep the absolute-form value when
  417. %% operating in proxy mode. Otherwise the absolute-form value
  418. %% is simply dropped and the Host header is used.
  419. %% @todo The authority is sent both in the URI and in the host header.
  420. %% The authority from the URI must be dropped, and the host header
  421. %% must be used instead. (RFC7230 5.5)
  422. %%
  423. %% It is not possible to test that the absolute-form value is dropped
  424. %% because one of the Host header test ensures that the authority
  425. %% is the same in both, and errors out otherwise.
  426. absolute_form_path(Config) ->
  427. doc("The path always starts with \"/\" and ends with either \"?\", \"#\" "
  428. "or the end of the URI. (RFC3986 3.3)"),
  429. Echo = <<"/echo/path">>,
  430. #{code := 200, body := Echo} = do_raw(Config,
  431. "GET http://localhost/echo/path HTTP/1.1\r\n"
  432. "Host: localhost\r\n"
  433. "\r\n").
  434. absolute_form_path_query(Config) ->
  435. doc("The path always starts with \"/\" and ends with either \"?\", \"#\" "
  436. "or the end of the URI. (RFC3986 3.3)"),
  437. Echo = <<"/echo/path">>,
  438. #{code := 200, body := Echo} = do_raw(Config,
  439. "GET http://localhost/echo/path?key=value HTTP/1.1\r\n"
  440. "Host: localhost\r\n"
  441. "\r\n").
  442. absolute_form_path_fragment(Config) ->
  443. doc("The path always starts with \"/\" and ends with either \"?\", \"#\" "
  444. "or the end of the URI. (RFC3986 3.3)"),
  445. Echo = <<"/echo/path">>,
  446. #{code := 200, body := Echo} = do_raw(Config,
  447. "GET http://localhost/echo/path#fragment HTTP/1.1\r\n"
  448. "Host: localhost\r\n"
  449. "\r\n").
  450. absolute_form_no_path(Config) ->
  451. doc("An empty path component is equivalent to \"/\". (RFC7230 2.7.3)"),
  452. #{code := 200, body := <<"Hello world!">>} = do_raw(Config,
  453. "GET http://localhost HTTP/1.1\r\n"
  454. "Host: localhost\r\n"
  455. "\r\n").
  456. absolute_form_no_path_then_query(Config) ->
  457. doc("An empty path component is equivalent to \"/\". (RFC7230 2.7.3)"),
  458. #{code := 200, body := <<"Hello world!">>} = do_raw(Config,
  459. "GET http://localhost?key=value HTTP/1.1\r\n"
  460. "Host: localhost\r\n"
  461. "\r\n").
  462. absolute_form_no_path_then_fragment(Config) ->
  463. doc("An empty path component is equivalent to \"/\". (RFC7230 2.7.3)"),
  464. #{code := 200, body := <<"Hello world!">>} = do_raw(Config,
  465. "GET http://localhost#fragment HTTP/1.1\r\n"
  466. "Host: localhost\r\n"
  467. "\r\n").
  468. absolute_form_query(Config) ->
  469. doc("The query starts with \"?\" and ends with \"#\" or the end of the URI. (RFC3986 3.4)"),
  470. Echo = <<"key=value">>,
  471. #{code := 200, body := Echo} = do_raw(Config,
  472. "GET http://localhost/echo/qs?key=value HTTP/1.1\r\n"
  473. "Host: localhost\r\n"
  474. "\r\n").
  475. absolute_form_query_fragment(Config) ->
  476. doc("The query starts with \"?\" and ends with \"#\" or the end of the URI. (RFC3986 3.4)"),
  477. Echo = <<"key=value">>,
  478. #{code := 200, body := Echo} = do_raw(Config,
  479. "GET http://localhost/echo/qs?key=value#fragment HTTP/1.1\r\n"
  480. "Host: localhost\r\n"
  481. "\r\n").
  482. %% @todo absolute_form: reject paths with too large depth or query strings with too many keys
  483. %% Request-target: authority-form.
  484. authority_form_reject_if_not_connect(Config) ->
  485. doc("When the method is CONNECT, authority-form must be used. This "
  486. "form does not apply to any other methods which must reject the "
  487. "request with a 400 status code and the closing of the connection. (RFC7230 5.3.3)"),
  488. #{code := 400, client := Client} = do_raw(Config,
  489. "GET localhost:80 HTTP/1.1\r\n"
  490. "Host: localhost\r\n"
  491. "\r\n"),
  492. {error, closed} = raw_recv(Client, 0, 1000).
  493. %% @todo Implement CONNECT.
  494. %authority_form_reject_userinfo(Config) ->
  495. %An authority component with a userinfo component (and its
  496. %"@" delimiter) is invalid. The request must be rejected with
  497. %a 400 status code and the closing of the connection. (RFC7230 2.7.1)
  498. %
  499. %authority_form_limit_host(Config) ->
  500. %authority_form_limit_port0(Config) ->
  501. %authority_form_limit_port65536(Config) ->
  502. %
  503. %A request with a too long component of authority-form must be rejected with
  504. %a 414 status code and the closing of the connection. (RFC7230 3.1.1)
  505. %
  506. %The authority is either resolved from configuration or is taken
  507. %directly from authority-form. (RFC7230 5.5)
  508. %
  509. %authority_form_empty_path(Config) ->
  510. %authority_form_empty_query(Config) ->
  511. %The path and query are empty when using authority-form. (RFC7230 5.5)
  512. %% Request-target: asterisk-form.
  513. asterisk_form_reject_if_not_options(Config) ->
  514. doc("asterisk-form is used for server-wide OPTIONS requests. "
  515. "It is invalid with any other methods which must reject the "
  516. "request with a 400 status code and the closing of the connection. (RFC7230 5.3.4)"),
  517. #{code := 400, client := Client} = do_raw(Config,
  518. "GET * HTTP/1.1\r\n"
  519. "Host: localhost\r\n"
  520. "\r\n"),
  521. {error, closed} = raw_recv(Client, 0, 1000).
  522. asterisk_form_empty_path(Config) ->
  523. doc("The path is empty when using asterisk-form. (RFC7230 5.5)"),
  524. #{code := 200, body := <<>>} = do_raw(Config,
  525. "OPTIONS * HTTP/1.1\r\n"
  526. "Host: localhost\r\n"
  527. "X-Echo: path\r\n"
  528. "\r\n").
  529. asterisk_form_empty_query(Config) ->
  530. doc("The query is empty when using asterisk-form. (RFC7230 5.5)"),
  531. #{code := 200, body := <<>>} = do_raw(Config,
  532. "OPTIONS * HTTP/1.1\r\n"
  533. "Host: localhost\r\n"
  534. "X-Echo: query\r\n"
  535. "\r\n").
  536. %% Invalid request-target.
  537. invalid_request_target(Config) ->
  538. doc("Any other form is invalid and must be rejected with a 400 status code "
  539. "and the closing of the connection."),
  540. #{code := 400, client := Client} = do_raw(Config,
  541. "GET \0 HTTP/1.1\r\n"
  542. "Host: localhost\r\n"
  543. "\r\n"),
  544. {error, closed} = raw_recv(Client, 0, 1000).
  545. %% Between request-target and version.
  546. reject_tab_between_request_target_and_version(Config) ->
  547. doc("A request that uses anything other than SP as separator between "
  548. "the request-target and the version must be rejected with a 400 "
  549. "status code and the closing of the connection. (RFC7230 3.1.1, RFC7230 3.5)"),
  550. #{code := 400, client := Client} = do_raw(Config,
  551. "GET /\tHTTP/1.1\r\n"
  552. "Host: localhost\r\n"
  553. "\r\n"),
  554. {error, closed} = raw_recv(Client, 0, 1000).
  555. reject_two_sp_between_request_target_and_version(Config) ->
  556. doc("A request that uses anything other than SP as separator between "
  557. "the request-target and the version must be rejected with a 400 "
  558. "status code and the closing of the connection. (RFC7230 3.1.1, RFC7230 3.5)"),
  559. #{code := 400, client := Client} = do_raw(Config,
  560. "GET / HTTP/1.1\r\n"
  561. "Host: localhost\r\n"
  562. "\r\n"),
  563. {error, closed} = raw_recv(Client, 0, 1000).
  564. %% Request version.
  565. reject_invalid_version_http100(Config) ->
  566. doc("Any version number other than HTTP/1.0 or HTTP/1.1 must be "
  567. "rejected by a server or intermediary with a 505 status code. (RFC7230 2.6, RFC7230 A.2)"),
  568. #{code := 505} = do_raw(Config,
  569. "GET / HTTP/1.00\r\n"
  570. "Host: localhost\r\n"
  571. "\r\n").
  572. reject_invalid_version_http111(Config) ->
  573. doc("Any version number other than HTTP/1.0 or HTTP/1.1 must be "
  574. "rejected by a server or intermediary with a 505 status code. (RFC7230 2.6, RFC7230 A.2)"),
  575. #{code := 505} = do_raw(Config,
  576. "GET / HTTP/1.11\r\n"
  577. "Host: localhost\r\n"
  578. "\r\n").
  579. reject_invalid_version_http12(Config) ->
  580. doc("Any version number other than HTTP/1.0 or HTTP/1.1 must be "
  581. "rejected by a server or intermediary with a 505 status code. (RFC7230 2.6, RFC7230 A.2)"),
  582. #{code := 505} = do_raw(Config,
  583. "GET / HTTP/1.2\r\n"
  584. "Host: localhost\r\n"
  585. "\r\n").
  586. reject_invalid_version_http2(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/2\r\n"
  591. "Host: localhost\r\n"
  592. "\r\n").
  593. reject_empty_version(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 / \r\n"
  598. "Host: localhost\r\n"
  599. "\r\n").
  600. reject_invalid_whitespace_after_version(Config) ->
  601. doc("A request that has whitespace different than CRLF following the "
  602. "version must be rejected with a 400 status code and the closing "
  603. "of the connection. (RFC7230 3.1.1)"),
  604. #{code := 400, client := Client} = do_raw(Config,
  605. "GET / HTTP/1.1 \r\n"
  606. "Host: localhost\r\n"
  607. "\r\n"),
  608. {error, closed} = raw_recv(Client, 0, 1000).
  609. %% Request headers.
  610. %invalid_header(Config) ->
  611. %```
  612. %headers = *( header-field CRLF ) CRLF
  613. %header-field = field-name ":" OWS field-value OWS
  614. %
  615. %field-name = token
  616. %field-value = *( SP / HTAB / %21-7E / %80-FF )
  617. %
  618. %OWS = *( SP / HTAB )
  619. %```
  620. lower_case_header(Config) ->
  621. doc("The header field name is case insensitive. (RFC7230 3.2)"),
  622. #{code := 200} = do_raw(Config, [
  623. "GET / HTTP/1.1\r\n"
  624. "host: localhost\r\n"
  625. "\r\n"]).
  626. upper_case_header(Config) ->
  627. doc("The header field name is case insensitive. (RFC7230 3.2)"),
  628. #{code := 200} = do_raw(Config, [
  629. "GET / HTTP/1.1\r\n"
  630. "HOST: localhost\r\n"
  631. "\r\n"]).
  632. mixed_case_header(Config) ->
  633. doc("The header field name is case insensitive. (RFC7230 3.2)"),
  634. #{code := 200} = do_raw(Config, [
  635. "GET / HTTP/1.1\r\n"
  636. "hOsT: localhost\r\n"
  637. "\r\n"]).
  638. reject_whitespace_before_header_name(Config) ->
  639. doc("Messages that contain whitespace before the header name must "
  640. "be rejected with a 400 status code and the closing of the "
  641. "connection. (RFC7230 3.2.4)"),
  642. #{code := 400, client := Client} = do_raw(Config, [
  643. "GET / HTTP/1.1\r\n"
  644. " Host: localhost\r\n"
  645. "\r\n"]),
  646. {error, closed} = raw_recv(Client, 0, 1000).
  647. reject_whitespace_between_header_name_and_colon(Config) ->
  648. doc("Messages that contain whitespace between the header name and "
  649. "colon must be rejected with a 400 status code and the closing "
  650. "of the connection. (RFC7230 3.2.4)"),
  651. #{code := 400, client := Client} = do_raw(Config, [
  652. "GET / HTTP/1.1\r\n"
  653. "Host : localhost\r\n"
  654. "\r\n"]),
  655. {error, closed} = raw_recv(Client, 0, 1000).
  656. limit_header_name(Config) ->
  657. doc("The header name must be subject to a configurable limit. A "
  658. "good default is 50 characters, well above the longest registered "
  659. "header. Such a request must be rejected with a 431 status code "
  660. "and the closing of the connection. "
  661. "(RFC7230 3.2.5, RFC6585 5, IANA Message Headers registry)"),
  662. #{code := 431, client := Client} = do_raw(Config, [
  663. "GET / HTTP/1.1\r\n"
  664. "Host: localhost\r\n",
  665. binary:copy(<<$a>>, 32768), ": bad\r\n"
  666. "\r\n"]),
  667. {error, closed} = raw_recv(Client, 0, 1000).
  668. limit_header_value(Config) ->
  669. doc("The header value and the optional whitespace around it must be "
  670. "subject to a configurable limit. There is no recommendations "
  671. "for the default. 4096 characters is known to work well. Such "
  672. "a request must be rejected with a 431 status code and the closing "
  673. "of the connection. (RFC7230 3.2.5, RFC6585 5)"),
  674. #{code := 431, client := Client} = do_raw(Config, [
  675. "GET / HTTP/1.1\r\n"
  676. "Host: localhost\r\n"
  677. "bad: ", binary:copy(<<$a>>, 32768), "\r\n"
  678. "\r\n"]),
  679. {error, closed} = raw_recv(Client, 0, 1000).
  680. drop_whitespace_before_header_value(Config) ->
  681. doc("Optional whitespace before and after the header value is not "
  682. "part of the value and must be dropped."),
  683. #{code := 200} = do_raw(Config, [
  684. "POST / HTTP/1.1\r\n"
  685. "Host: localhost\r\n"
  686. "Content-length: \t 12\r\n"
  687. "\r\n"
  688. "Hello world!"]).
  689. drop_whitespace_after_header_value(Config) ->
  690. doc("Optional whitespace before and after the header value is not "
  691. "part of the value and must be dropped."),
  692. #{code := 200} = do_raw(Config, [
  693. "POST / HTTP/1.1\r\n"
  694. "Host: localhost\r\n"
  695. "Content-length: 12 \t \r\n"
  696. "\r\n"
  697. "Hello world!"]).
  698. %@todo
  699. %The order of header fields with differing names is not significant. (RFC7230 3.2.2)
  700. %
  701. %@todo
  702. %The normal procedure for parsing headers is to read each header
  703. %field into a hash table by field name until the empty line. (RFC7230 3)
  704. reject_duplicate_content_length_header(Config) ->
  705. doc("Requests with duplicate content-length headers must be rejected "
  706. "with a 400 status code and the closing of the connection. (RFC7230 3.3.2)"),
  707. #{code := 400, client := Client} = do_raw(Config, [
  708. "POST / HTTP/1.1\r\n"
  709. "Host: localhost\r\n"
  710. "Content-length: 12\r\n"
  711. "Content-length: 12\r\n"
  712. "\r\n"
  713. "Hello world!"]),
  714. {error, closed} = raw_recv(Client, 0, 1000).
  715. reject_duplicate_host_header(Config) ->
  716. doc("Requests with duplicate host headers must be rejected "
  717. "with a 400 status code and the closing of the connection. (RFC7230 3.3.2)"),
  718. #{code := 400, client := Client} = do_raw(Config, [
  719. "POST / HTTP/1.1\r\n"
  720. "Host: localhost\r\n"
  721. "Host: localhost\r\n"
  722. "\r\n"
  723. "Hello world!"]),
  724. {error, closed} = raw_recv(Client, 0, 1000).
  725. %combine_duplicate_headers(Config) ->
  726. %Other duplicate header fields must be combined by inserting a comma
  727. %between the values in the order they were received. (RFC7230 3.2.2)
  728. %
  729. %Duplicate header field names are only allowed when their value is
  730. %a comma-separated list. In practice there is no need to perform
  731. %a check while reading the headers as the value will become invalid
  732. %and the error can be handled while parsing the header later on. (RFC7230 3.2.2)
  733. %
  734. %wait_for_eoh_before_processing_request(Config) ->
  735. %The request must not be processed until all headers have arrived. (RFC7230 3.2.2)
  736. limit_headers(Config) ->
  737. doc("The number of headers allowed in a request must be subject to "
  738. "a configurable limit. There is no recommendations for the default. "
  739. "100 headers is known to work well. Such a request must be rejected "
  740. "with a 431 status code and the closing of the connection. (RFC7230 3.2.5, RFC6585 5)"),
  741. %% 100 headers.
  742. #{code := 200} = do_raw(Config, [
  743. "GET / HTTP/1.1\r\n"
  744. "Host: localhost\r\n",
  745. [["H-", integer_to_list(N), ": value\r\n"] || N <- lists:seq(1, 99)],
  746. "\r\n"]),
  747. %% 101 headers.
  748. #{code := 431, client := Client} = do_raw(Config, [
  749. "GET / HTTP/1.1\r\n"
  750. "Host: localhost\r\n",
  751. [["H-", integer_to_list(N), ": value\r\n"] || N <- lists:seq(1, 100)],
  752. "\r\n"]),
  753. {error, closed} = raw_recv(Client, 0, 1000).
  754. %@todo
  755. %When parsing header field values, the server must ignore empty
  756. %list elements, and not count those as the count of elements present. (RFC7230 7)
  757. %
  758. %@todo
  759. %The information in the via header is largely unreliable. (RFC7230 5.7.1)
  760. %
  761. %%% Request body.
  762. %
  763. %@todo
  764. %The message body is the octets after decoding any transfer
  765. %codings. (RFC7230 3.3)
  766. %
  767. %no_request_body(Config) ->
  768. %no_request_body_content_length_zero(Config) ->
  769. %request_body_content_length(Config) ->
  770. %request_body_transfer_encoding(Config) ->
  771. %A request has a message body only if it includes a transfer-encoding
  772. %header or a non-zero content-length header. (RFC7230 3.3)
  773. %
  774. %```
  775. %Transfer-Encoding = 1#transfer-coding
  776. %
  777. %transfer-coding = "chunked" / "compress" / "deflate" / "gzip" / transfer-extension
  778. %transfer-extension = token *( OWS ";" OWS transfer-parameter )
  779. %transfer-parameter = token BWS "=" BWS ( token / quoted-string )
  780. %```
  781. %
  782. %case_insensitive_transfer_encoding(Config) ->
  783. %The transfer-coding is case insensitive. (RFC7230 4)
  784. %
  785. %@todo
  786. %There are no known other transfer-extension with the exception of
  787. %deprecated aliases "x-compress" and "x-gzip". (IANA HTTP Transfer Coding Registry,
  788. %RFC7230 4.2.1, RFC7230 4.2.3, RFC7230 8.4.2)
  789. %
  790. %must_understand_chunked(Config) ->
  791. %A server must be able to handle at least chunked transfer-encoding.
  792. %This is also the only coding that sees widespread use. (RFC7230 3.3.1, RFC7230 4.1)
  793. %
  794. %reject_double_chunked_encoding(Config) ->
  795. %Messages encoded more than once with chunked transfer-encoding
  796. %must be rejected with a 400 status code and the closing of the
  797. %connection. (RFC7230 3.3.1)
  798. %
  799. %reject_non_terminal_chunked(Config) ->
  800. %Messages where chunked, when present, is not the last
  801. %transfer-encoding must be rejected with a 400 status code
  802. %and the closing of the connection. (RFC7230 3.3.3)
  803. %
  804. %@todo
  805. %Some non-conformant implementations send the "deflate" compressed
  806. %data without the zlib wrapper. (RFC7230 4.2.2)
  807. %
  808. %reject_unknown_transfer_encoding(Config) ->
  809. %Messages encoded with a transfer-encoding the server does not
  810. %understand must be rejected with a 501 status code and the
  811. %closing of the connection. (RFC7230 3.3.1)
  812. %
  813. %@todo
  814. %A server can reject requests with a body and no content-length
  815. %header with a 411 status code. (RFC7230 3.3.3)
  816. %
  817. %```
  818. %Content-Length = 1*DIGIT
  819. %```
  820. reject_invalid_content_length(Config) ->
  821. doc("A request with an invalid content-length header must be rejected "
  822. "with a 400 status code and the closing of the connection. (RFC7230 3.3.3)"),
  823. #{code := 400, client := Client1} = do_raw(Config, [
  824. "POST / HTTP/1.1\r\n"
  825. "Host: localhost\r\n"
  826. "Content-length: 12,12\r\n"
  827. "\r\n"
  828. "Hello world!"]),
  829. {error, closed} = raw_recv(Client1, 0, 1000),
  830. #{code := 400, client := Client2} = do_raw(Config, [
  831. "POST / HTTP/1.1\r\n"
  832. "Host: localhost\r\n"
  833. "Content-length: NaN\r\n"
  834. "\r\n"
  835. "Hello world!"]),
  836. {error, closed} = raw_recv(Client2, 0, 1000).
  837. %@todo
  838. %The content-length header ranges from 0 to infinity. Requests
  839. %with a message body too large must be rejected with a 413 status
  840. %code and the closing of the connection. (RFC7230 3.3.2)
  841. %
  842. %ignore_content_length_when_transfer_encoding(Config) ->
  843. %When a message includes both transfer-encoding and content-length
  844. %headers, the content-length header must be removed before processing
  845. %the request. (RFC7230 3.3.3)
  846. %
  847. %socket_error_while_reading_body(Config) ->
  848. %If a socket error occurs while reading the body the server
  849. %must send a 400 status code response and close the connection. (RFC7230 3.3.3, RFC7230 3.4)
  850. %
  851. %timeout_while_reading_body(Config) ->
  852. %If a timeout occurs while reading the body the server must
  853. %send a 408 status code response and close the connection. (RFC7230 3.3.3, RFC7230 3.4)
  854. %
  855. %%% Body length.
  856. %
  857. %body_length_chunked_before(Config) ->
  858. %The length of a message with a transfer-encoding header can
  859. %only be determined on decoding completion. (RFC7230 3.3.3)
  860. %
  861. %body_length_chunked_after(Config) ->
  862. %Upon completion of chunk decoding the server must add a content-length
  863. %header with the value set to the total length of data read. (RFC7230 4.1.3)
  864. %
  865. %body_length_content_length(Config) ->
  866. %The length of a message with a content-length header is
  867. %the numeric value in octets found in the header. (RFC7230 3.3.3)
  868. %
  869. %body_length_zero(Config) ->
  870. %A message with no transfer-encoding or content-length header
  871. %has a body length of 0. (RFC7230 3.3.3)
  872. %
  873. %%% Chunked transfer-encoding.
  874. %
  875. %reject_invalid_chunk_size(Config) ->
  876. %
  877. %```
  878. %chunked-body = *chunk last-chunk trailer-part CRLF
  879. %
  880. %chunk = chunk-size [ chunk-ext ] CRLF chunk-data CRLF
  881. %chunk-size = 1*HEXDIG
  882. %chunk-data = 1*OCTET ; a sequence of chunk-size octets
  883. %
  884. %last-chunk = 1*("0") [ chunk-ext ] CRLF
  885. %```
  886. %
  887. %The chunk-size field is a string of hex digits indicating the size of
  888. %the chunk-data in octets.
  889. %
  890. %```
  891. %chunk-ext = *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
  892. %chunk-ext-name = token
  893. %chunk-ext-val = token / quoted-string
  894. %```
  895. %
  896. %ignore_unknown_chunk_extensions(Config) ->
  897. %Unknown chunk extensions must be ignored. (RFC7230 4.1.1)
  898. %
  899. %reject_invalid_chunk_extensions(Config) ->
  900. %
  901. %limit_chunk_size_line(Config) ->
  902. %The chunk-size line length must be subject to configuration.
  903. %There are no recommended defaults, although 100 octets should work.
  904. %Requests with a too long line must be rejected with a 400 status
  905. %code and the closing of the connection.
  906. %
  907. %reject_invalid_chunk_line_crlf(Config) ->
  908. %reject_invalid_chunk_data_crlf(Config) ->
  909. %
  910. %```
  911. %trailer-part = *( header-field CRLF )
  912. %```
  913. %
  914. %%% @todo see headers above and reject the same way, space etc.
  915. %reject_invalid_trailer_part(Config) ->
  916. %
  917. %ignore_trailer_transfer_encoding(Config) ->
  918. %ignore_trailer_content_length(Config) ->
  919. %ignore_trailer_host(Config) ->
  920. %ignore_trailer_cache_control(Config) ->
  921. %ignore_trailer_expect(Config) ->
  922. %ignore_trailer_max_forwards(Config) ->
  923. %ignore_trailer_pragma(Config) ->
  924. %ignore_trailer_range(Config) ->
  925. %ignore_trailer_te(Config) ->
  926. %ignore_trailer_if_match(Config) ->
  927. %ignore_trailer_if_none_match(Config) ->
  928. %ignore_trailer_if_modified_since(Config) ->
  929. %ignore_trailer_if_unmodified_since(Config) ->
  930. %ignore_trailer_if_range(Config) ->
  931. %ignore_trailer_www_authenticate(Config) ->
  932. %ignore_trailer_authorization(Config) ->
  933. %ignore_trailer_proxy_authenticate(Config) ->
  934. %ignore_trailer_proxy_authorization(Config) ->
  935. %ignore_trailer_content_encoding(Config) ->
  936. %ignore_trailer_content_type(Config) ->
  937. %ignore_trailer_content_range(Config) ->
  938. %ignore_trailer_trailer(Config) ->
  939. %
  940. %ignore_trailer_header(Config, Header) ->
  941. %Trailing headers must not include transfer-encoding, content-length,
  942. %host, cache-control, expect, max-forwards, pragma, range, te,
  943. %if-match, if-none-match, if-modified-since, if-unmodified-since,
  944. %if-range, www-authenticate, authorization, proxy-authenticate,
  945. %proxy-authorization, age, cache-control, expires, date, location,
  946. %retry-after, vary, warning, content-encoding, content-type,
  947. %content-range, or trailer. (RFC7230 4.1.2)
  948. %
  949. %Trailer headers can be ignored safely. (RFC7230 4.1.2)
  950. %
  951. %When trailer headers are processed, invalid headers must be ignored.
  952. %Valid headers must be added to the list of headers of the request. (RFC7230 4.1.2)
  953. %
  954. %limit_trailer_headers(Config) ->
  955. %The number of trailer headers must be subject to configuration.
  956. %There is no known recommendations for the default. A value of 10
  957. %should cover most cases. Requests with too many trailer headers
  958. %must be rejected with a 431 status code and the closing of the
  959. %connection. (RFC6585 5)
  960. %
  961. %remove_transfer_encoding_chunked_after_body_read(Config) ->
  962. %Upon completion of chunk decoding the server must remove "chunked"
  963. %from the transfer-encoding header. This header must be removed if
  964. %it becomes empty following this removal. (RFC7230 4.1.3)
  965. %
  966. %remove_trailer_after_body_read(Config) ->
  967. %Upon completion of chunk decoding the server must remove the trailer
  968. %header from the list of headers. (RFC7230 4.1.3)
  969. %
  970. %```
  971. %Trailer = 1#field-name
  972. %```
  973. %
  974. %ignore_chunked_headers_not_in_trailer(Config) ->
  975. %The trailer header can be used to list the headers found in the
  976. %trailer. A server must have the option of ignoring trailer headers
  977. %that were not listed in the trailer header. (RFC7230 4.4)
  978. %
  979. %ignore_chunked_headers_if_trailer_not_in_connection(Config) ->
  980. %The trailer header must be listed in the connection header field.
  981. %Trailers must be ignored otherwise.
  982. %
  983. %%% @todo Though we need a compatibility mode as some clients don't send it...
  984. %reject_chunked_missing_end_crlf(Config) ->
  985. %@todo ending CRLF
  986. %
  987. %%% Connection management.
  988. %
  989. %@todo can probably test using auth
  990. %Never assume any two requests on a single connection come
  991. %from the same user agent. (RFC7230 2.3)
  992. %
  993. %```
  994. %Connection = 1#token ; case-insensitive
  995. %```
  996. %
  997. %The connection token is either case insensitive "close", "keep-alive"
  998. %or a header field name.
  999. %
  1000. %There are no corresponding "close" or "keep-alive" headers. (RFC7230 8.1, RFC7230 A.2)
  1001. %
  1002. %The connection header is valid only for the immediate connection,
  1003. %alongside any header field it lists. (RFC7230 6.1)
  1004. %
  1005. %The server must determine if the connection is persistent for
  1006. %every message received by looking at the connection header and
  1007. %HTTP version. (RFC7230 6.3)
  1008. %
  1009. %no_connection_header_keepalive(Config) ->
  1010. %%% @todo http/1.0 suite? connection_keepalive(Config) ->
  1011. %HTTP/1.1 requests with no "close" option and HTTP/1.0 with the
  1012. %"keep-alive" option indicate the connection will persist. (RFC7230 6.1, RFC7230 6.3)
  1013. %
  1014. %connection_close(Config) ->
  1015. %%% @todo http/1.0 suite? no_connection_close(Config) ->
  1016. %HTTP/1.1 requests with the "close" option and HTTP/1.0 with no
  1017. %"keep-alive" option indicate the connection will be closed
  1018. %upon reception of the response by the client. (RFC7230 6.1, RFC7230 6.3)
  1019. %
  1020. %limit_requests_keepalive(Config) ->
  1021. %The maximum number of requests sent using a persistent connection
  1022. %must be subject to configuration. The connection must be closed
  1023. %when the limit is reached. (RFC7230 6.3)
  1024. %
  1025. %skip_request_body_by_closing_connection(Config) ->
  1026. %%A server that doesn't want to read the entire body of a message
  1027. %%must close the connection, if possible after sending the "close"
  1028. %%connection option in the response. (RFC7230 6.3)
  1029. %
  1030. %pipeline(Config) ->
  1031. %%% @todo pipeline_parallel (safe methods can, others can't)
  1032. %A server can receive more than one request before any response
  1033. %is sent. This is called pipelining. The requests can be processed
  1034. %in parallel if they all have safe methods. Responses must be sent
  1035. %in the same order as the requests. (RFC7230 6.3.2)
  1036. %
  1037. %@todo
  1038. %The server must reject abusive traffic by closing the connection.
  1039. %Abusive traffic can come from the form of too many requests in a
  1040. %given amount of time, or too many concurrent connections. Limits
  1041. %must be subject to configuration. (RFC7230 6.4)
  1042. %
  1043. %close_inactive_connections(Config) ->
  1044. %The server must close inactive connections. The timeout
  1045. %must be subject to configuration. (RFC7230 6.5)
  1046. %
  1047. %@todo
  1048. %The server must monitor connections for the close signal
  1049. %and close the socket on its end accordingly. (RFC7230 6.5)
  1050. %
  1051. %@todo
  1052. %A connection close may occur at any time. (RFC7230 6.5)
  1053. %
  1054. %ignore_requests_after_connection_close(Config) ->
  1055. %The server must not process any request after sending or
  1056. %receiving the "close" connection option. (RFC7230 6.6)
  1057. %
  1058. %@todo
  1059. %The server must close the connection in stages to avoid the
  1060. %TCP reset problem. The server starts by closing the write
  1061. %side of the socket. The server then reads until it detects
  1062. %the socket has been closed, until it can be certain its
  1063. %last response has been received by the client, or until
  1064. %a close or timeout occurs. The server then fully close the
  1065. %connection. (6.6)
  1066. %
  1067. %%% Routing.
  1068. %
  1069. %```
  1070. %Host = authority ; same as authority-form
  1071. %```
  1072. %
  1073. %reject_missing_host(Config) ->
  1074. %An HTTP/1.1 request that lacks a host header must be rejected with
  1075. %a 400 status code and the closing of the connection. (RFC7230 5.4)
  1076. %
  1077. %%% @todo http/1.0 missing_host(Config) ->
  1078. %An HTTP/1.0 request that lack a host header is valid. Behavior
  1079. %for these requests is configuration dependent. (RFC7230 5.5)
  1080. %
  1081. %reject_invalid_host(Config) ->
  1082. %A request with an invalid host header must be rejected with a
  1083. %400 status code and the closing of the connection. (RFC7230 5.4)
  1084. %
  1085. %reject_userinfo(Config) ->
  1086. %An authority component with a userinfo component (and its
  1087. %"@" delimiter) is invalid. The request must be rejected with
  1088. %a 400 status code and the closing of the connection. (RFC7230 2.7.1)
  1089. %
  1090. %reject_absolute_form_different_host(Config) ->
  1091. %When using absolute-form the URI authority component must be
  1092. %identical to the host header. Invalid requests must be rejected
  1093. %with a 400 status code and the closing of the connection. (RFC7230 5.4)
  1094. %
  1095. %reject_authority_form_different_host(Config) ->
  1096. %When using authority-form the URI authority component must be
  1097. %identical to the host header. Invalid requests must be rejected
  1098. %with a 400 status code and the closing of the connection.
  1099. %
  1100. %empty_host(Config) ->
  1101. %The host header is empty when the authority component is undefined. (RFC7230 5.4)
  1102. %
  1103. %@todo
  1104. %The effective request URI can be rebuilt by concatenating scheme,
  1105. %"://", authority, path and query components. (RFC7230 5.5)
  1106. %
  1107. %@todo
  1108. %Resources with identical URI except for the scheme component
  1109. %must be treated as different. (RFC7230 2.7.2)
  1110. %
  1111. %%% Response.
  1112. %
  1113. %@todo
  1114. %A server can send more than one response per request only when a
  1115. %1xx response is sent preceding the final response. (RFC7230 5.6)
  1116. %
  1117. %@todo
  1118. %A server that does parallel pipelining must send responses in the
  1119. %same order as the requests came in. (RFC7230 5.6)
  1120. %
  1121. %```
  1122. %HTTP-response = status-line *( header-field CRLF ) CRLF [ message-body ]
  1123. %```
  1124. %
  1125. %@todo
  1126. %The response format must be followed strictly.
  1127. %
  1128. %```
  1129. %status-line = HTTP-version SP status-code SP reason-phrase CRLF
  1130. %status-code = 3DIGIT
  1131. %reason-phrase = *( HTAB / SP / VCHAR / obs-text )
  1132. %```
  1133. %
  1134. %http10_request_http11_response(Config) ->
  1135. %A server must send its own version. (RFC7230 2.6)
  1136. %
  1137. %@todo
  1138. %An HTTP/1.1 server may send an HTTP/1.0 version for compatibility purposes. (RFC7230 2.6)
  1139. %
  1140. %@todo
  1141. %RFC6585 defines additional status code a server can use to reject
  1142. %messages. (RFC7230 9.3, RFC6585)
  1143. %
  1144. %%% Response headers.
  1145. %
  1146. %@todo
  1147. %In responses, OWS must be generated as SP or not generated
  1148. %at all. RWS must be generated as SP. BWS must not be
  1149. %generated. (RFC7230 3.2.3)
  1150. %
  1151. %```
  1152. %header-field = field-name ":" SP field-value
  1153. %
  1154. %field-name = token ; case-insensitive
  1155. %field-value = *( SP / %21-7E / %80-FF )
  1156. %```
  1157. %
  1158. %@todo
  1159. %In quoted-string found in field-value, quoted-pair must only be
  1160. %used for DQUOTE and backslash. (RFC7230 3.2.6)
  1161. %
  1162. %@todo
  1163. %HTTP header values must use US-ASCII encoding and must only send
  1164. %printable characters or SP. (RFC7230 3.2.4, RFC7230 9.4)
  1165. %
  1166. %@todo
  1167. %The server must not generate empty list elements in headers. (RFC7230 7)
  1168. %
  1169. %@todo
  1170. %When encoding an URI as part of a response, only characters that
  1171. %are reserved need to be percent-encoded. (RFC7230 2.7.3)
  1172. %
  1173. %special_set_cookie_handling(Config) ->
  1174. %The set-cookie header must be handled as a special case. There
  1175. %must be exactly one set-cookie header field per cookie. (RFC7230 3.2.2)
  1176. %
  1177. %@todo
  1178. %The server must list headers for or about the immediate connection
  1179. %in the connection header field. (RFC7230 6.1)
  1180. %
  1181. %@todo
  1182. %A server that does not support persistent connections must
  1183. %send "close" in every non-1xx response. (RFC7230 6.1)
  1184. %
  1185. %no_close_in_100_response(Config) ->
  1186. %no_close_in_101_response(Config) ->
  1187. %no_close_in_102_response(Config) ->
  1188. %A server must not send a "close" connection option
  1189. %in 1xx responses. (RFC7230 6.1)
  1190. %
  1191. %@todo
  1192. %The "close" connection must be sent in a message when the
  1193. %sender knows it will close the connection after fully sending
  1194. %the response. (RFC7230 6.6)
  1195. %
  1196. %@todo
  1197. %A server must close the connection after sending or
  1198. %receiving a "close" once the response has been sent. (RFC7230 6.6)
  1199. %
  1200. %close_request_close_response(Config) ->
  1201. %A server must send a "close" in a response to a request
  1202. %containing a "close". (RFC7230 6.6)
  1203. %
  1204. %%% Response body.
  1205. %
  1206. %no_body_in_head_response(Config) -> %% @todo test different ways to send a body in response
  1207. %Responses to HEAD requests never include a message body. (RFC7230 3.3)
  1208. %
  1209. %%% @todo Implement CONNECT
  1210. %2xx responses to CONNECT requests never include a message
  1211. %body. (RFC7230 3.3)
  1212. %
  1213. %no_body_in_100_response(Config) ->
  1214. %no_body_in_101_response(Config) ->
  1215. %no_body_in_102_response(Config) ->
  1216. %no_body_in_204_response(Config) ->
  1217. %no_body_in_304_response(Config) ->
  1218. %1xx, 204 and 304 responses never include a message body. (RFC7230 3.3)
  1219. %
  1220. %same_content_length_as_get_in_head_response(Config) ->
  1221. %same_transfer_encoding_as_get_in_head_response(Config) ->
  1222. %same_content_length_as_200_in_304_response(Config) ->
  1223. %same_transfer_encoding_as_200_in_304_response(Config) ->
  1224. %Responses to HEAD requests and 304 responses can include a
  1225. %content-length or transfer-encoding header. Their value must
  1226. %be the same as if the request was an unconditional GET. (RFC7230 3.3, RFC7230 3.3.1, RFC7230 3.3.2)
  1227. %
  1228. %no_transfer_encoding_in_100_response(Config) ->
  1229. %no_transfer_encoding_in_101_response(Config) ->
  1230. %no_transfer_encoding_in_102_response(Config) ->
  1231. %no_transfer_encoding_in_204_response(Config) ->
  1232. %%% @todo CONNECT no_transfer_encoding_in_2xx_response_to_connect_request(Config) ->
  1233. %no_content_length_in_100_response(Config) ->
  1234. %no_content_length_in_101_response(Config) ->
  1235. %no_content_length_in_102_response(Config) ->
  1236. %no_content_length_in_204_response(Config) ->
  1237. %%% @todo CONNECT no_content_length_in_2xx_response_to_connect_request(Config) ->
  1238. %1xx, 204 responses and "2xx responses to CONNECT requests" must
  1239. %not include a content-length or transfer-encoding header. (RFC7230 3.3.1, RFC7230 3.3.2)
  1240. %
  1241. %```
  1242. %message-body = *OCTET
  1243. %```
  1244. %
  1245. %The message body is the octets after decoding any transfer
  1246. %codings. (RFC7230 3.3)
  1247. %
  1248. %content_length_0_when_no_body(Config) ->
  1249. %content_length_response(Config) ->
  1250. %When the length is known in advance, the server must send a
  1251. %content-length header, including if the length is 0. (RFC7230 3.3.2, RFC7230 3.3.3)
  1252. %
  1253. %chunked_response(Config) ->
  1254. %When the length is not known in advance, the chunked transfer-encoding
  1255. %must be used. (RFC7230 3.3.2, RFC7230 3.3.3)
  1256. %
  1257. %compat_no_content_length_or_transfer_encoding_close_on_body_end(Config) ->
  1258. %For compatibility purposes a server can send no content-length or
  1259. %transfer-encoding header. In this case the connection must be
  1260. %closed after the response has been sent fully. (RFC7230 3.3.2, RFC7230 3.3.3)
  1261. %
  1262. %no_content_length_if_transfer_encoding(Config) ->
  1263. %The content-length header must not be sent when a transfer-encoding
  1264. %header already exists. (RFC7230 3.3.2)
  1265. %
  1266. %@todo
  1267. %The server must not apply the chunked transfer-encoding more than
  1268. %once. (RFC7230 3.3.1)
  1269. %
  1270. %@todo
  1271. %The server must apply the chunked transfer-encoding last. (RFC7230 3.3.1)
  1272. %
  1273. %http10_request_no_transfer_encoding_in_response(Config) ->
  1274. %The transfer-encoding header must not be sent in responses to
  1275. %HTTP/1.0 requests, or in responses that use the HTTP/1.0 version.
  1276. %No transfer codings must be applied in these cases. (RFC7230 3.3.1)
  1277. %
  1278. %```
  1279. %TE = #t-codings
  1280. %
  1281. %t-codings = "trailers" / ( transfer-coding [ t-ranking ] )
  1282. %t-ranking = OWS ";" OWS "q=" rank
  1283. %rank = ( "0" [ "." 0*3DIGIT ] ) / ( "1" [ "." 0*3("0") ] )
  1284. %```
  1285. %
  1286. %no_te_no_trailers(Config) ->
  1287. %te_trailers(Config) ->
  1288. %Trailers can only be sent if the request includes a TE header
  1289. %containing "trailers". (RFC7230 4.1.2)
  1290. %
  1291. %te_ignore_chunked(Config) ->
  1292. %te_ignore_chunked_0(Config) ->
  1293. %The presence of "chunked" in a TE header must be ignored as it
  1294. %is always acceptable with HTTP/1.1. (RFC7230 4.3)
  1295. %
  1296. %%% @todo te_not_acceptable_coding(Config) ->
  1297. %A qvalue of 0 in the TE header means "not acceptable". (RFC7230 4.3)
  1298. %
  1299. %@todo
  1300. %The lack of a TE header or an empty TE header means only "chunked"
  1301. %(with no trailers) or no transfer-encoding is acceptable. (RFC7230 4.3)
  1302. %
  1303. %ignore_te_if_not_in_connection_header(Config) ->
  1304. %The TE header must be listed in the connection header field,
  1305. %or must be ignored otherwise.
  1306. %
  1307. %@todo
  1308. %Trailer headers must be listed in the trailer header field value. (RFC7230 4.4)
  1309. %
  1310. %@todo
  1311. %When defined, the trailer header must also be listed in the connection header. (RFC7230 4.4)
  1312. %
  1313. %:: Upgrade
  1314. %
  1315. %```
  1316. %Upgrade = 1#protocol
  1317. %
  1318. %protocol = protocol-name ["/" protocol-version]
  1319. %protocol-name = token
  1320. %protocol-version = token
  1321. %```
  1322. %
  1323. %The upgrade header contains the list of protocols the
  1324. %client wishes to upgrade to, in order of preference. (RFC7230 6.7)
  1325. %
  1326. %upgrade_safely_ignored(Config) ->
  1327. %The upgrade header can be safely ignored. (RFC7230 6.7)
  1328. %
  1329. %upgrade_must_be_in_connection_header(Config) ->
  1330. %The upgrade header must be listed under the connection header,
  1331. %or must be ignored otherwise. (RFC7230 6.7)
  1332. %
  1333. %@todo
  1334. %A server accepting an upgrade request must send a 101 status
  1335. %code with a upgrade header listing the protocol(s) it upgrades
  1336. %to, in layer-ascending order. In addition the upgrade header
  1337. %must be listed in the connection header. (RFC7230 6.7)
  1338. %
  1339. %%A server must not switch to a protocol not listed in the
  1340. %%request's upgrade header. (RFC7230 6.7)
  1341. %
  1342. %@todo
  1343. %A server that sends a 426 status code must include a upgrade
  1344. %header listing acceptable protocols in order of preference. (RFC7230 6.7)
  1345. %
  1346. %@todo
  1347. %A server can send a upgrade header to any response to advertise
  1348. %its support for other protocols listed in order of preference. (RFC7230 6.7)
  1349. %
  1350. %@todo
  1351. %Immediately after a server responds with a 101 status code
  1352. %it must respond to the original request using the new protocol. (RFC7230 6.7)
  1353. %
  1354. %@todo
  1355. %%A server must not switch protocols unless the original message's
  1356. %%semantics can be honored by the new protocol. OPTIONS requests
  1357. %%can be honored by any protocol. (RFC7230 6.7)
  1358. %
  1359. %http10_ignore_upgrade_header(Config) ->
  1360. %A server must ignore an upgrade header received by an HTTP/1.0
  1361. %request. (RFC7230 6.7)
  1362. %
  1363. %expect_then_upgrade(Config) ->
  1364. %A server receiving both an upgrade header and an expect header
  1365. %containing "100-continue" must send a 100 response before the
  1366. %101 response. (RFC7230 6.7)
  1367. %
  1368. %The upgrade header field cannot be used for switching the
  1369. %connection protocol (e.g. TCP) or switching connections. (RFC7230 6.7)
  1370. %
  1371. %%% Compatibility.
  1372. %
  1373. %@todo
  1374. %A server can choose to be non-conformant to the specifications
  1375. %for the sake of compatibility. Such behavior can be enabled
  1376. %through configuration and/or software identification. (RFC7230 2.5)