cow_http.erl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. %% Copyright (c) 2013-2018, 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(cow_http).
  15. -export([parse_request_line/1]).
  16. -export([parse_status_line/1]).
  17. -export([status_to_integer/1]).
  18. -export([parse_headers/1]).
  19. -export([parse_fullpath/1]).
  20. -export([parse_version/1]).
  21. -export([request/4]).
  22. -export([response/3]).
  23. -export([headers/1]).
  24. -export([version/1]).
  25. -type version() :: 'HTTP/1.0' | 'HTTP/1.1'.
  26. -export_type([version/0]).
  27. -type status() :: 100..999.
  28. -export_type([status/0]).
  29. -type headers() :: [{binary(), iodata()}].
  30. -export_type([headers/0]).
  31. -include("cow_inline.hrl").
  32. %% @doc Parse the request line.
  33. -spec parse_request_line(binary()) -> {binary(), binary(), version(), binary()}.
  34. parse_request_line(Data) ->
  35. {Pos, _} = binary:match(Data, <<"\r">>),
  36. <<RequestLine:Pos/binary, "\r\n", Rest/bits>> = Data,
  37. [Method, Target, Version0] = binary:split(RequestLine, <<$\s>>, [trim_all, global]),
  38. Version = case Version0 of
  39. <<"HTTP/1.1">> -> 'HTTP/1.1';
  40. <<"HTTP/1.0">> -> 'HTTP/1.0'
  41. end,
  42. {Method, Target, Version, Rest}.
  43. -ifdef(TEST).
  44. parse_request_line_test_() ->
  45. Tests = [
  46. {<<"GET /path HTTP/1.0\r\nRest">>,
  47. {<<"GET">>, <<"/path">>, 'HTTP/1.0', <<"Rest">>}},
  48. {<<"GET /path HTTP/1.1\r\nRest">>,
  49. {<<"GET">>, <<"/path">>, 'HTTP/1.1', <<"Rest">>}},
  50. {<<"CONNECT proxy.example.org:1080 HTTP/1.1\r\nRest">>,
  51. {<<"CONNECT">>, <<"proxy.example.org:1080">>, 'HTTP/1.1', <<"Rest">>}}
  52. ],
  53. [{V, fun() -> R = parse_request_line(V) end}
  54. || {V, R} <- Tests].
  55. parse_request_line_error_test_() ->
  56. Tests = [
  57. <<>>,
  58. <<"GET">>,
  59. <<"GET /path\r\n">>,
  60. <<"GET /path HTTP/1.1">>,
  61. <<"GET /path HTTP/1.1\r">>,
  62. <<"GET /path HTTP/1.1\n">>,
  63. <<"GET /path HTTP/0.9\r\n">>,
  64. <<"content-type: text/plain\r\n">>,
  65. <<0:80, "\r\n">>
  66. ],
  67. [{V, fun() -> {'EXIT', _} = (catch parse_request_line(V)) end}
  68. || V <- Tests].
  69. horse_parse_request_line_get_path() ->
  70. horse:repeat(200000,
  71. parse_request_line(<<"GET /path HTTP/1.1\r\n">>)
  72. ).
  73. -endif.
  74. %% @doc Parse the status line.
  75. -spec parse_status_line(binary()) -> {version(), status(), binary(), binary()}.
  76. parse_status_line(<< "HTTP/1.1 200 OK\r\n", Rest/bits >>) ->
  77. {'HTTP/1.1', 200, <<"OK">>, Rest};
  78. parse_status_line(<< "HTTP/1.1 404 Not Found\r\n", Rest/bits >>) ->
  79. {'HTTP/1.1', 404, <<"Not Found">>, Rest};
  80. parse_status_line(<< "HTTP/1.1 500 Internal Server Error\r\n", Rest/bits >>) ->
  81. {'HTTP/1.1', 500, <<"Internal Server Error">>, Rest};
  82. parse_status_line(<< "HTTP/1.1 ", Status/bits >>) ->
  83. parse_status_line(Status, 'HTTP/1.1');
  84. parse_status_line(<< "HTTP/1.0 ", Status/bits >>) ->
  85. parse_status_line(Status, 'HTTP/1.0').
  86. parse_status_line(<<H, T, U, " ", Rest/bits>>, Version) ->
  87. Status = status_to_integer(H, T, U),
  88. {Pos, _} = binary:match(Rest, <<"\r">>),
  89. << StatusStr:Pos/binary, "\r\n", Rest2/bits >> = Rest,
  90. {Version, Status, StatusStr, Rest2}.
  91. -spec status_to_integer(status() | binary()) -> status().
  92. status_to_integer(Status) when is_integer(Status) ->
  93. Status;
  94. status_to_integer(Status) ->
  95. case Status of
  96. <<H, T, U>> ->
  97. status_to_integer(H, T, U);
  98. <<H, T, U, " ", _/bits>> ->
  99. status_to_integer(H, T, U)
  100. end.
  101. status_to_integer(H, T, U)
  102. when $0 =< H, H =< $9, $0 =< T, T =< $9, $0 =< U, U =< $9 ->
  103. (H - $0) * 100 + (T - $0) * 10 + (U - $0).
  104. -ifdef(TEST).
  105. parse_status_line_test_() ->
  106. Tests = [
  107. {<<"HTTP/1.1 200 OK\r\nRest">>,
  108. {'HTTP/1.1', 200, <<"OK">>, <<"Rest">>}},
  109. {<<"HTTP/1.0 404 Not Found\r\nRest">>,
  110. {'HTTP/1.0', 404, <<"Not Found">>, <<"Rest">>}},
  111. {<<"HTTP/1.1 500 Something very funny here\r\nRest">>,
  112. {'HTTP/1.1', 500, <<"Something very funny here">>, <<"Rest">>}},
  113. {<<"HTTP/1.1 200 \r\nRest">>,
  114. {'HTTP/1.1', 200, <<>>, <<"Rest">>}}
  115. ],
  116. [{V, fun() -> R = parse_status_line(V) end}
  117. || {V, R} <- Tests].
  118. parse_status_line_error_test_() ->
  119. Tests = [
  120. <<>>,
  121. <<"HTTP/1.1">>,
  122. <<"HTTP/1.1 200\r\n">>,
  123. <<"HTTP/1.1 200 OK">>,
  124. <<"HTTP/1.1 200 OK\r">>,
  125. <<"HTTP/1.1 200 OK\n">>,
  126. <<"HTTP/0.9 200 OK\r\n">>,
  127. <<"HTTP/1.1 42 Answer\r\n">>,
  128. <<"HTTP/1.1 999999999 More than OK\r\n">>,
  129. <<"content-type: text/plain\r\n">>,
  130. <<0:80, "\r\n">>
  131. ],
  132. [{V, fun() -> {'EXIT', _} = (catch parse_status_line(V)) end}
  133. || V <- Tests].
  134. horse_parse_status_line_200() ->
  135. horse:repeat(200000,
  136. parse_status_line(<<"HTTP/1.1 200 OK\r\n">>)
  137. ).
  138. horse_parse_status_line_404() ->
  139. horse:repeat(200000,
  140. parse_status_line(<<"HTTP/1.1 404 Not Found\r\n">>)
  141. ).
  142. horse_parse_status_line_500() ->
  143. horse:repeat(200000,
  144. parse_status_line(<<"HTTP/1.1 500 Internal Server Error\r\n">>)
  145. ).
  146. horse_parse_status_line_other() ->
  147. horse:repeat(200000,
  148. parse_status_line(<<"HTTP/1.1 416 Requested range not satisfiable\r\n">>)
  149. ).
  150. -endif.
  151. %% @doc Parse the list of headers.
  152. -spec parse_headers(binary()) -> {[{binary(), binary()}], binary()}.
  153. parse_headers(Data) ->
  154. parse_header(Data, []).
  155. parse_header(<< $\r, $\n, Rest/bits >>, Acc) ->
  156. {lists:reverse(Acc), Rest};
  157. parse_header(Data, Acc) ->
  158. parse_hd_name(Data, Acc, <<>>).
  159. parse_hd_name(<< C, Rest/bits >>, Acc, SoFar) ->
  160. case C of
  161. $: -> parse_hd_before_value(Rest, Acc, SoFar);
  162. $\s -> parse_hd_name_ws(Rest, Acc, SoFar);
  163. $\t -> parse_hd_name_ws(Rest, Acc, SoFar);
  164. _ -> ?LOWER(parse_hd_name, Rest, Acc, SoFar)
  165. end.
  166. parse_hd_name_ws(<< C, Rest/bits >>, Acc, Name) ->
  167. case C of
  168. $: -> parse_hd_before_value(Rest, Acc, Name);
  169. $\s -> parse_hd_name_ws(Rest, Acc, Name);
  170. $\t -> parse_hd_name_ws(Rest, Acc, Name)
  171. end.
  172. parse_hd_before_value(<< $\s, Rest/bits >>, Acc, Name) ->
  173. parse_hd_before_value(Rest, Acc, Name);
  174. parse_hd_before_value(<< $\t, Rest/bits >>, Acc, Name) ->
  175. parse_hd_before_value(Rest, Acc, Name);
  176. parse_hd_before_value(Data, Acc, Name) ->
  177. parse_hd_value(Data, Acc, Name, <<>>).
  178. parse_hd_value(<< $\r, Rest/bits >>, Acc, Name, SoFar) ->
  179. case Rest of
  180. << $\n, C, Rest2/bits >> when C =:= $\s; C =:= $\t ->
  181. parse_hd_value(Rest2, Acc, Name, << SoFar/binary, C >>);
  182. << $\n, Rest2/bits >> ->
  183. Value = clean_value_ws_end(SoFar, byte_size(SoFar) - 1),
  184. parse_header(Rest2, [{Name, Value}|Acc])
  185. end;
  186. parse_hd_value(<< C, Rest/bits >>, Acc, Name, SoFar) ->
  187. parse_hd_value(Rest, Acc, Name, << SoFar/binary, C >>).
  188. %% This function has been copied from cowboy_http.
  189. clean_value_ws_end(_, -1) ->
  190. <<>>;
  191. clean_value_ws_end(Value, N) ->
  192. case binary:at(Value, N) of
  193. $\s -> clean_value_ws_end(Value, N - 1);
  194. $\t -> clean_value_ws_end(Value, N - 1);
  195. _ ->
  196. S = N + 1,
  197. << Value2:S/binary, _/bits >> = Value,
  198. Value2
  199. end.
  200. -ifdef(TEST).
  201. parse_headers_test_() ->
  202. Tests = [
  203. {<<"\r\nRest">>,
  204. {[], <<"Rest">>}},
  205. {<<"Server: Erlang/R17 \r\n\r\n">>,
  206. {[{<<"server">>, <<"Erlang/R17">>}], <<>>}},
  207. {<<"Server: Erlang/R17\r\n"
  208. "Date: Sun, 23 Feb 2014 09:30:39 GMT\r\n"
  209. "Multiline-Header: why hello!\r\n"
  210. " I didn't see you all the way over there!\r\n"
  211. "Content-Length: 12\r\n"
  212. "Content-Type: text/plain\r\n"
  213. "\r\nRest">>,
  214. {[{<<"server">>, <<"Erlang/R17">>},
  215. {<<"date">>, <<"Sun, 23 Feb 2014 09:30:39 GMT">>},
  216. {<<"multiline-header">>,
  217. <<"why hello! I didn't see you all the way over there!">>},
  218. {<<"content-length">>, <<"12">>},
  219. {<<"content-type">>, <<"text/plain">>}],
  220. <<"Rest">>}}
  221. ],
  222. [{V, fun() -> R = parse_headers(V) end}
  223. || {V, R} <- Tests].
  224. parse_headers_error_test_() ->
  225. Tests = [
  226. <<>>,
  227. <<"\r">>,
  228. <<"Malformed\r\n\r\n">>,
  229. <<"content-type: text/plain\r\nMalformed\r\n\r\n">>,
  230. <<"HTTP/1.1 200 OK\r\n\r\n">>,
  231. <<0:80, "\r\n\r\n">>,
  232. <<"content-type: text/plain\r\ncontent-length: 12\r\n">>
  233. ],
  234. [{V, fun() -> {'EXIT', _} = (catch parse_headers(V)) end}
  235. || V <- Tests].
  236. horse_parse_headers() ->
  237. horse:repeat(50000,
  238. parse_headers(<<"Server: Erlang/R17\r\n"
  239. "Date: Sun, 23 Feb 2014 09:30:39 GMT\r\n"
  240. "Multiline-Header: why hello!\r\n"
  241. " I didn't see you all the way over there!\r\n"
  242. "Content-Length: 12\r\n"
  243. "Content-Type: text/plain\r\n"
  244. "\r\nRest">>)
  245. ).
  246. -endif.
  247. %% @doc Extract path and query string from a binary,
  248. %% removing any fragment component.
  249. -spec parse_fullpath(binary()) -> {binary(), binary()}.
  250. parse_fullpath(Fullpath) ->
  251. parse_fullpath(Fullpath, <<>>).
  252. parse_fullpath(<<>>, Path) -> {Path, <<>>};
  253. parse_fullpath(<< $#, _/bits >>, Path) -> {Path, <<>>};
  254. parse_fullpath(<< $?, Qs/bits >>, Path) -> parse_fullpath_query(Qs, Path, <<>>);
  255. parse_fullpath(<< C, Rest/bits >>, SoFar) -> parse_fullpath(Rest, << SoFar/binary, C >>).
  256. parse_fullpath_query(<<>>, Path, Query) -> {Path, Query};
  257. parse_fullpath_query(<< $#, _/bits >>, Path, Query) -> {Path, Query};
  258. parse_fullpath_query(<< C, Rest/bits >>, Path, SoFar) ->
  259. parse_fullpath_query(Rest, Path, << SoFar/binary, C >>).
  260. -ifdef(TEST).
  261. parse_fullpath_test() ->
  262. {<<"*">>, <<>>} = parse_fullpath(<<"*">>),
  263. {<<"/">>, <<>>} = parse_fullpath(<<"/">>),
  264. {<<"/path/to/resource">>, <<>>} = parse_fullpath(<<"/path/to/resource#fragment">>),
  265. {<<"/path/to/resource">>, <<>>} = parse_fullpath(<<"/path/to/resource">>),
  266. {<<"/">>, <<>>} = parse_fullpath(<<"/?">>),
  267. {<<"/">>, <<"q=cowboy">>} = parse_fullpath(<<"/?q=cowboy#fragment">>),
  268. {<<"/">>, <<"q=cowboy">>} = parse_fullpath(<<"/?q=cowboy">>),
  269. {<<"/path/to/resource">>, <<"q=cowboy">>}
  270. = parse_fullpath(<<"/path/to/resource?q=cowboy">>),
  271. ok.
  272. -endif.
  273. %% @doc Convert an HTTP version to atom.
  274. -spec parse_version(binary()) -> version().
  275. parse_version(<<"HTTP/1.1">>) -> 'HTTP/1.1';
  276. parse_version(<<"HTTP/1.0">>) -> 'HTTP/1.0'.
  277. -ifdef(TEST).
  278. parse_version_test() ->
  279. 'HTTP/1.1' = parse_version(<<"HTTP/1.1">>),
  280. 'HTTP/1.0' = parse_version(<<"HTTP/1.0">>),
  281. {'EXIT', _} = (catch parse_version(<<"HTTP/1.2">>)),
  282. ok.
  283. -endif.
  284. %% @doc Return formatted request-line and headers.
  285. %% @todo Add tests when the corresponding reverse functions are added.
  286. -spec request(binary(), iodata(), version(), headers()) -> iodata().
  287. request(Method, Path, Version, Headers) ->
  288. [Method, <<" ">>, Path, <<" ">>, version(Version), <<"\r\n">>,
  289. [[N, <<": ">>, V, <<"\r\n">>] || {N, V} <- Headers],
  290. <<"\r\n">>].
  291. -spec response(status() | binary(), version(), headers()) -> iodata().
  292. response(Status, Version, Headers) ->
  293. [version(Version), <<" ">>, status(Status), <<"\r\n">>,
  294. headers(Headers), <<"\r\n">>].
  295. -spec headers(headers()) -> iodata().
  296. headers(Headers) ->
  297. [[N, <<": ">>, V, <<"\r\n">>] || {N, V} <- Headers].
  298. %% @doc Return the version as a binary.
  299. -spec version(version()) -> binary().
  300. version('HTTP/1.1') -> <<"HTTP/1.1">>;
  301. version('HTTP/1.0') -> <<"HTTP/1.0">>.
  302. -ifdef(TEST).
  303. version_test() ->
  304. <<"HTTP/1.1">> = version('HTTP/1.1'),
  305. <<"HTTP/1.0">> = version('HTTP/1.0'),
  306. {'EXIT', _} = (catch version('HTTP/1.2')),
  307. ok.
  308. -endif.
  309. %% @doc Return the status code and string as binary.
  310. -spec status(status() | binary()) -> binary().
  311. status(100) -> <<"100 Continue">>;
  312. status(101) -> <<"101 Switching Protocols">>;
  313. status(102) -> <<"102 Processing">>;
  314. status(103) -> <<"103 Early Hints">>;
  315. status(200) -> <<"200 OK">>;
  316. status(201) -> <<"201 Created">>;
  317. status(202) -> <<"202 Accepted">>;
  318. status(203) -> <<"203 Non-Authoritative Information">>;
  319. status(204) -> <<"204 No Content">>;
  320. status(205) -> <<"205 Reset Content">>;
  321. status(206) -> <<"206 Partial Content">>;
  322. status(207) -> <<"207 Multi-Status">>;
  323. status(226) -> <<"226 IM Used">>;
  324. status(300) -> <<"300 Multiple Choices">>;
  325. status(301) -> <<"301 Moved Permanently">>;
  326. status(302) -> <<"302 Found">>;
  327. status(303) -> <<"303 See Other">>;
  328. status(304) -> <<"304 Not Modified">>;
  329. status(305) -> <<"305 Use Proxy">>;
  330. status(306) -> <<"306 Switch Proxy">>;
  331. status(307) -> <<"307 Temporary Redirect">>;
  332. status(308) -> <<"308 Permanent Redirect">>;
  333. status(400) -> <<"400 Bad Request">>;
  334. status(401) -> <<"401 Unauthorized">>;
  335. status(402) -> <<"402 Payment Required">>;
  336. status(403) -> <<"403 Forbidden">>;
  337. status(404) -> <<"404 Not Found">>;
  338. status(405) -> <<"405 Method Not Allowed">>;
  339. status(406) -> <<"406 Not Acceptable">>;
  340. status(407) -> <<"407 Proxy Authentication Required">>;
  341. status(408) -> <<"408 Request Timeout">>;
  342. status(409) -> <<"409 Conflict">>;
  343. status(410) -> <<"410 Gone">>;
  344. status(411) -> <<"411 Length Required">>;
  345. status(412) -> <<"412 Precondition Failed">>;
  346. status(413) -> <<"413 Request Entity Too Large">>;
  347. status(414) -> <<"414 Request-URI Too Long">>;
  348. status(415) -> <<"415 Unsupported Media Type">>;
  349. status(416) -> <<"416 Requested Range Not Satisfiable">>;
  350. status(417) -> <<"417 Expectation Failed">>;
  351. status(418) -> <<"418 I'm a teapot">>;
  352. status(421) -> <<"421 Misdirected Request">>;
  353. status(422) -> <<"422 Unprocessable Entity">>;
  354. status(423) -> <<"423 Locked">>;
  355. status(424) -> <<"424 Failed Dependency">>;
  356. status(425) -> <<"425 Unordered Collection">>;
  357. status(426) -> <<"426 Upgrade Required">>;
  358. status(428) -> <<"428 Precondition Required">>;
  359. status(429) -> <<"429 Too Many Requests">>;
  360. status(431) -> <<"431 Request Header Fields Too Large">>;
  361. status(451) -> <<"451 Unavailable For Legal Reasons">>;
  362. status(500) -> <<"500 Internal Server Error">>;
  363. status(501) -> <<"501 Not Implemented">>;
  364. status(502) -> <<"502 Bad Gateway">>;
  365. status(503) -> <<"503 Service Unavailable">>;
  366. status(504) -> <<"504 Gateway Timeout">>;
  367. status(505) -> <<"505 HTTP Version Not Supported">>;
  368. status(506) -> <<"506 Variant Also Negotiates">>;
  369. status(507) -> <<"507 Insufficient Storage">>;
  370. status(510) -> <<"510 Not Extended">>;
  371. status(511) -> <<"511 Network Authentication Required">>;
  372. status(B) when is_binary(B) -> B.