cow_http.erl 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. %% Copyright (c) 2013-2014, 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_fullhost/1]).
  19. -export([parse_fullpath/1]).
  20. -export([parse_version/1]).
  21. -export([request/4]).
  22. -export([version/1]).
  23. -type version() :: 'HTTP/1.0' | 'HTTP/1.1'.
  24. -type status() :: 100..999.
  25. -type headers() :: [{binary(), iodata()}].
  26. -include("cow_inline.hrl").
  27. %% @doc Parse the status line.
  28. -spec parse_status_line(binary()) -> {version(), status(), binary(), binary()}.
  29. parse_status_line(<< "HTTP/1.1 200 OK\r\n", Rest/bits >>) ->
  30. {'HTTP/1.1', 200, <<"OK">>, Rest};
  31. parse_status_line(<< "HTTP/1.1 404 Not Found\r\n", Rest/bits >>) ->
  32. {'HTTP/1.1', 404, <<"Not Found">>, Rest};
  33. parse_status_line(<< "HTTP/1.1 500 Internal Server Error\r\n", Rest/bits >>) ->
  34. {'HTTP/1.1', 500, <<"Internal Server Error">>, Rest};
  35. parse_status_line(<< "HTTP/1.1 ", Status/bits >>) ->
  36. parse_status_line(Status, 'HTTP/1.1');
  37. parse_status_line(<< "HTTP/1.0 ", Status/bits >>) ->
  38. parse_status_line(Status, 'HTTP/1.0').
  39. parse_status_line(<< H, T, U, " ", Rest/bits >>, Version)
  40. when $0 =< H, H =< $9, $0 =< T, T =< $9, $0 =< U, U =< $9 ->
  41. Status = (H - $0) * 100 + (T - $0) * 10 + (U - $0),
  42. {Pos, _} = binary:match(Rest, <<"\r">>),
  43. << StatusStr:Pos/binary, "\r\n", Rest2/bits >> = Rest,
  44. {Version, Status, StatusStr, Rest2}.
  45. -ifdef(TEST).
  46. parse_status_line_test_() ->
  47. Tests = [
  48. {<<"HTTP/1.1 200 OK\r\nRest">>,
  49. {'HTTP/1.1', 200, <<"OK">>, <<"Rest">>}},
  50. {<<"HTTP/1.0 404 Not Found\r\nRest">>,
  51. {'HTTP/1.0', 404, <<"Not Found">>, <<"Rest">>}},
  52. {<<"HTTP/1.1 500 Something very funny here\r\nRest">>,
  53. {'HTTP/1.1', 500, <<"Something very funny here">>, <<"Rest">>}},
  54. {<<"HTTP/1.1 200 \r\nRest">>,
  55. {'HTTP/1.1', 200, <<>>, <<"Rest">>}}
  56. ],
  57. [{V, fun() -> R = parse_status_line(V) end}
  58. || {V, R} <- Tests].
  59. parse_status_line_error_test_() ->
  60. Tests = [
  61. <<>>,
  62. <<"HTTP/1.1">>,
  63. <<"HTTP/1.1 200\r\n">>,
  64. <<"HTTP/1.1 200 OK">>,
  65. <<"HTTP/1.1 200 OK\r">>,
  66. <<"HTTP/1.1 200 OK\n">>,
  67. <<"HTTP/0.9 200 OK\r\n">>,
  68. <<"HTTP/1.1 42 Answer\r\n">>,
  69. <<"HTTP/1.1 999999999 More than OK\r\n">>,
  70. <<"content-type: text/plain\r\n">>,
  71. <<0:80, "\r\n">>
  72. ],
  73. [{V, fun() -> {'EXIT', _} = (catch parse_status_line(V)) end}
  74. || V <- Tests].
  75. -endif.
  76. -ifdef(PERF).
  77. horse_parse_status_line_200() ->
  78. horse:repeat(200000,
  79. parse_status_line(<<"HTTP/1.1 200 OK\r\n">>)
  80. ).
  81. horse_parse_status_line_404() ->
  82. horse:repeat(200000,
  83. parse_status_line(<<"HTTP/1.1 404 Not Found\r\n">>)
  84. ).
  85. horse_parse_status_line_500() ->
  86. horse:repeat(200000,
  87. parse_status_line(<<"HTTP/1.1 500 Internal Server Error\r\n">>)
  88. ).
  89. horse_parse_status_line_other() ->
  90. horse:repeat(200000,
  91. parse_status_line(<<"HTTP/1.1 416 Requested range not satisfiable\r\n">>)
  92. ).
  93. -endif.
  94. %% @doc Parse the list of headers.
  95. -spec parse_headers(binary()) -> {[{binary(), binary()}], binary()}.
  96. parse_headers(Data) ->
  97. parse_header(Data, []).
  98. parse_header(<< $\r, $\n, Rest/bits >>, Acc) ->
  99. {lists:reverse(Acc), Rest};
  100. parse_header(Data, Acc) ->
  101. parse_hd_name(Data, Acc, <<>>).
  102. parse_hd_name(<< C, Rest/bits >>, Acc, SoFar) ->
  103. case C of
  104. $: -> parse_hd_before_value(Rest, Acc, SoFar);
  105. $\s -> parse_hd_name_ws(Rest, Acc, SoFar);
  106. $\t -> parse_hd_name_ws(Rest, Acc, SoFar);
  107. ?INLINE_LOWERCASE(parse_hd_name, Rest, Acc, SoFar)
  108. end.
  109. parse_hd_name_ws(<< C, Rest/bits >>, Acc, Name) ->
  110. case C of
  111. $: -> parse_hd_before_value(Rest, Acc, Name);
  112. $\s -> parse_hd_name_ws(Rest, Acc, Name);
  113. $\t -> parse_hd_name_ws(Rest, Acc, Name)
  114. end.
  115. parse_hd_before_value(<< $\s, Rest/bits >>, Acc, Name) ->
  116. parse_hd_before_value(Rest, Acc, Name);
  117. parse_hd_before_value(<< $\t, Rest/bits >>, Acc, Name) ->
  118. parse_hd_before_value(Rest, Acc, Name);
  119. parse_hd_before_value(Data, Acc, Name) ->
  120. parse_hd_value(Data, Acc, Name, <<>>).
  121. parse_hd_value(<< $\r, Rest/bits >>, Acc, Name, SoFar) ->
  122. case Rest of
  123. << $\n, C, Rest2/bits >> when C =:= $\s; C =:= $\t ->
  124. parse_hd_value(Rest2, Acc, Name, << SoFar/binary, C >>);
  125. << $\n, Rest2/bits >> ->
  126. parse_header(Rest2, [{Name, SoFar}|Acc])
  127. end;
  128. parse_hd_value(<< C, Rest/bits >>, Acc, Name, SoFar) ->
  129. parse_hd_value(Rest, Acc, Name, << SoFar/binary, C >>).
  130. -ifdef(TEST).
  131. parse_headers_test_() ->
  132. Tests = [
  133. {<<"\r\nRest">>,
  134. {[], <<"Rest">>}},
  135. {<<"Server: Erlang/R17\r\n"
  136. "Date: Sun, 23 Feb 2014 09:30:39 GMT\r\n"
  137. "Multiline-Header: why hello!\r\n"
  138. " I didn't see you all the way over there!\r\n"
  139. "Content-Length: 12\r\n"
  140. "Content-Type: text/plain\r\n"
  141. "\r\nRest">>,
  142. {[{<<"server">>, <<"Erlang/R17">>},
  143. {<<"date">>, <<"Sun, 23 Feb 2014 09:30:39 GMT">>},
  144. {<<"multiline-header">>,
  145. <<"why hello! I didn't see you all the way over there!">>},
  146. {<<"content-length">>, <<"12">>},
  147. {<<"content-type">>, <<"text/plain">>}],
  148. <<"Rest">>}}
  149. ],
  150. [{V, fun() -> R = parse_headers(V) end}
  151. || {V, R} <- Tests].
  152. parse_headers_error_test_() ->
  153. Tests = [
  154. <<>>,
  155. <<"\r">>,
  156. <<"Malformed\r\n\r\n">>,
  157. <<"content-type: text/plain\r\nMalformed\r\n\r\n">>,
  158. <<"HTTP/1.1 200 OK\r\n\r\n">>,
  159. <<0:80, "\r\n\r\n">>,
  160. <<"content-type: text/plain\r\ncontent-length: 12\r\n">>
  161. ],
  162. [{V, fun() -> {'EXIT', _} = (catch parse_headers(V)) end}
  163. || V <- Tests].
  164. -endif.
  165. -ifdef(PERF).
  166. horse_parse_headers() ->
  167. horse:repeat(50000,
  168. parse_headers(<<"Server: Erlang/R17\r\n"
  169. "Date: Sun, 23 Feb 2014 09:30:39 GMT\r\n"
  170. "Multiline-Header: why hello!\r\n"
  171. " I didn't see you all the way over there!\r\n"
  172. "Content-Length: 12\r\n"
  173. "Content-Type: text/plain\r\n"
  174. "\r\nRest">>)
  175. ).
  176. -endif.
  177. %% @doc Extract host and port from a binary.
  178. %%
  179. %% Because the hostname is case insensitive it is converted
  180. %% to lowercase.
  181. -spec parse_fullhost(binary()) -> {binary(), undefined | non_neg_integer()}.
  182. parse_fullhost(Fullhost) ->
  183. parse_fullhost(Fullhost, false, <<>>).
  184. parse_fullhost(<< $[, Rest/bits >>, false, <<>>) ->
  185. parse_fullhost(Rest, true, << $[ >>);
  186. parse_fullhost(<<>>, false, Acc) ->
  187. {Acc, undefined};
  188. %% @todo Optimize.
  189. parse_fullhost(<< $:, Rest/bits >>, false, Acc) ->
  190. {Acc, list_to_integer(binary_to_list(Rest))};
  191. parse_fullhost(<< $], Rest/bits >>, true, Acc) ->
  192. parse_fullhost(Rest, false, << Acc/binary, $] >>);
  193. parse_fullhost(<< C, Rest/bits >>, E, Acc) ->
  194. case C of
  195. ?INLINE_LOWERCASE(parse_fullhost, Rest, E, Acc)
  196. end.
  197. -ifdef(TEST).
  198. parse_fullhost_test() ->
  199. {<<"example.org">>, 8080} = parse_fullhost(<<"example.org:8080">>),
  200. {<<"example.org">>, undefined} = parse_fullhost(<<"example.org">>),
  201. {<<"192.0.2.1">>, 8080} = parse_fullhost(<<"192.0.2.1:8080">>),
  202. {<<"192.0.2.1">>, undefined} = parse_fullhost(<<"192.0.2.1">>),
  203. {<<"[2001:db8::1]">>, 8080} = parse_fullhost(<<"[2001:db8::1]:8080">>),
  204. {<<"[2001:db8::1]">>, undefined} = parse_fullhost(<<"[2001:db8::1]">>),
  205. {<<"[::ffff:192.0.2.1]">>, 8080}
  206. = parse_fullhost(<<"[::ffff:192.0.2.1]:8080">>),
  207. {<<"[::ffff:192.0.2.1]">>, undefined}
  208. = parse_fullhost(<<"[::ffff:192.0.2.1]">>),
  209. ok.
  210. -endif.
  211. %% @doc Extract path and query string from a binary.
  212. -spec parse_fullpath(binary()) -> {binary(), binary()}.
  213. parse_fullpath(Fullpath) ->
  214. parse_fullpath(Fullpath, <<>>).
  215. parse_fullpath(<<>>, Path) ->
  216. {Path, <<>>};
  217. parse_fullpath(<< $?, Qs/binary >>, Path) ->
  218. {Path, Qs};
  219. parse_fullpath(<< C, Rest/binary >>, SoFar) ->
  220. parse_fullpath(Rest, << SoFar/binary, C >>).
  221. -ifdef(TEST).
  222. parse_fullpath_test() ->
  223. {<<"*">>, <<>>} = parse_fullpath(<<"*">>),
  224. {<<"/">>, <<>>} = parse_fullpath(<<"/">>),
  225. {<<"/path/to/resource">>, <<>>} = parse_fullpath(<<"/path/to/resource">>),
  226. {<<"/">>, <<>>} = parse_fullpath(<<"/?">>),
  227. {<<"/">>, <<"q=cowboy">>} = parse_fullpath(<<"/?q=cowboy">>),
  228. {<<"/path/to/resource">>, <<"q=cowboy">>}
  229. = parse_fullpath(<<"/path/to/resource?q=cowboy">>),
  230. ok.
  231. -endif.
  232. %% @doc Convert an HTTP version to atom.
  233. -spec parse_version(binary()) -> version().
  234. parse_version(<<"HTTP/1.1">>) -> 'HTTP/1.1';
  235. parse_version(<<"HTTP/1.0">>) -> 'HTTP/1.0'.
  236. -ifdef(TEST).
  237. parse_version_test() ->
  238. 'HTTP/1.1' = parse_version(<<"HTTP/1.1">>),
  239. 'HTTP/1.0' = parse_version(<<"HTTP/1.0">>),
  240. {'EXIT', _} = (catch parse_version(<<"HTTP/1.2">>)),
  241. ok.
  242. -endif.
  243. %% @doc Return formatted request-line and headers.
  244. %% @todo Add tests when the corresponding reverse functions are added.
  245. -spec request(binary(), iodata(), version(), headers()) -> iodata().
  246. request(Method, Path, Version, Headers) ->
  247. [Method, <<" ">>, Path, <<" ">>, version(Version), <<"\r\n">>,
  248. [[N, <<": ">>, V, <<"\r\n">>] || {N, V} <- Headers],
  249. <<"\r\n">>].
  250. %% @doc Return the version as a binary.
  251. -spec version(version()) -> binary().
  252. version('HTTP/1.1') -> <<"HTTP/1.1">>;
  253. version('HTTP/1.0') -> <<"HTTP/1.0">>.
  254. -ifdef(TEST).
  255. version_test() ->
  256. <<"HTTP/1.1">> = version('HTTP/1.1'),
  257. <<"HTTP/1.0">> = version('HTTP/1.0'),
  258. {'EXIT', _} = (catch version('HTTP/1.2')),
  259. ok.
  260. -endif.