cow_http.erl 12 KB

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