cow_http.erl 11 KB

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