cowboy_http_protocol.erl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. %% Copyright (c) 2011, Loïc Hoguin <essen@dev-extend.eu>
  2. %% Copyright (c) 2011, Anthony Ramine <nox@dev-extend.eu>
  3. %%
  4. %% Permission to use, copy, modify, and/or distribute this software for any
  5. %% purpose with or without fee is hereby granted, provided that the above
  6. %% copyright notice and this permission notice appear in all copies.
  7. %%
  8. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. %% @doc HTTP protocol handler.
  16. %%
  17. %% The available options are:
  18. %% <dl>
  19. %% <dt>dispatch</dt><dd>The dispatch list for this protocol.</dd>
  20. %% <dt>max_empty_lines</dt><dd>Max number of empty lines before a request.
  21. %% Defaults to 5.</dd>
  22. %% <dt>timeout</dt><dd>Time in milliseconds before an idle
  23. %% connection is closed. Defaults to 5000 milliseconds.</dd>
  24. %% </dl>
  25. %%
  26. %% Note that there is no need to monitor these processes when using Cowboy as
  27. %% an application as it already supervises them under the listener supervisor.
  28. %%
  29. %% @see cowboy_dispatcher
  30. %% @see cowboy_http_handler
  31. -module(cowboy_http_protocol).
  32. -behaviour(cowboy_protocol).
  33. -export([start_link/4]). %% API.
  34. -export([init/4, parse_request/1]). %% FSM.
  35. -include("include/http.hrl").
  36. -include_lib("eunit/include/eunit.hrl").
  37. -record(state, {
  38. listener :: pid(),
  39. socket :: inet:socket(),
  40. transport :: module(),
  41. dispatch :: cowboy_dispatcher:dispatch_rules(),
  42. handler :: {module(), any()},
  43. req_empty_lines = 0 :: integer(),
  44. max_empty_lines :: integer(),
  45. timeout :: timeout(),
  46. buffer = <<>> :: binary()
  47. }).
  48. %% API.
  49. %% @doc Start an HTTP protocol process.
  50. -spec start_link(pid(), inet:socket(), module(), any()) -> {ok, pid()}.
  51. start_link(ListenerPid, Socket, Transport, Opts) ->
  52. Pid = spawn_link(?MODULE, init, [ListenerPid, Socket, Transport, Opts]),
  53. {ok, Pid}.
  54. %% FSM.
  55. %% @private
  56. -spec init(pid(), inet:socket(), module(), any()) -> ok.
  57. init(ListenerPid, Socket, Transport, Opts) ->
  58. Dispatch = proplists:get_value(dispatch, Opts, []),
  59. MaxEmptyLines = proplists:get_value(max_empty_lines, Opts, 5),
  60. Timeout = proplists:get_value(timeout, Opts, 5000),
  61. receive shoot -> ok end,
  62. wait_request(#state{listener=ListenerPid, socket=Socket, transport=Transport,
  63. dispatch=Dispatch, max_empty_lines=MaxEmptyLines, timeout=Timeout}).
  64. %% @private
  65. -spec parse_request(#state{}) -> ok.
  66. %% @todo Use decode_packet options to limit length?
  67. parse_request(State=#state{buffer=Buffer}) ->
  68. case erlang:decode_packet(http_bin, Buffer, []) of
  69. {ok, Request, Rest} -> request(Request, State#state{buffer=Rest});
  70. {more, _Length} -> wait_request(State);
  71. {error, _Reason} -> error_terminate(400, State)
  72. end.
  73. -spec wait_request(#state{}) -> ok.
  74. wait_request(State=#state{socket=Socket, transport=Transport,
  75. timeout=T, buffer=Buffer}) ->
  76. case Transport:recv(Socket, 0, T) of
  77. {ok, Data} -> parse_request(State#state{
  78. buffer= << Buffer/binary, Data/binary >>});
  79. {error, _Reason} -> terminate(State)
  80. end.
  81. -spec request({http_request, http_method(), http_uri(),
  82. http_version()}, #state{}) -> ok.
  83. %% @todo We probably want to handle some things differently between versions.
  84. request({http_request, _Method, _URI, Version}, State)
  85. when Version =/= {1, 0}, Version =/= {1, 1} ->
  86. error_terminate(505, State);
  87. %% @todo We need to cleanup the URI properly.
  88. request({http_request, Method, {abs_path, AbsPath}, Version},
  89. State=#state{socket=Socket, transport=Transport}) ->
  90. {Path, RawPath, Qs} = cowboy_dispatcher:split_path(AbsPath),
  91. ConnAtom = version_to_connection(Version),
  92. parse_header(#http_req{socket=Socket, transport=Transport,
  93. connection=ConnAtom, method=Method, version=Version,
  94. path=Path, raw_path=RawPath, raw_qs=Qs}, State);
  95. request({http_request, Method, '*', Version},
  96. State=#state{socket=Socket, transport=Transport}) ->
  97. ConnAtom = version_to_connection(Version),
  98. parse_header(#http_req{socket=Socket, transport=Transport,
  99. connection=ConnAtom, method=Method, version=Version,
  100. path='*', raw_path= <<"*">>, raw_qs= <<>>}, State);
  101. request({http_request, _Method, _URI, _Version}, State) ->
  102. error_terminate(501, State);
  103. request({http_error, <<"\r\n">>},
  104. State=#state{req_empty_lines=N, max_empty_lines=N}) ->
  105. error_terminate(400, State);
  106. request({http_error, <<"\r\n">>}, State=#state{req_empty_lines=N}) ->
  107. parse_request(State#state{req_empty_lines=N + 1});
  108. request({http_error, _Any}, State) ->
  109. error_terminate(400, State).
  110. -spec parse_header(#http_req{}, #state{}) -> ok.
  111. parse_header(Req, State=#state{buffer=Buffer}) ->
  112. case erlang:decode_packet(httph_bin, Buffer, []) of
  113. {ok, Header, Rest} -> header(Header, Req, State#state{buffer=Rest});
  114. {more, _Length} -> wait_header(Req, State);
  115. {error, _Reason} -> error_terminate(400, State)
  116. end.
  117. -spec wait_header(#http_req{}, #state{}) -> ok.
  118. wait_header(Req, State=#state{socket=Socket,
  119. transport=Transport, timeout=T, buffer=Buffer}) ->
  120. case Transport:recv(Socket, 0, T) of
  121. {ok, Data} -> parse_header(Req, State#state{
  122. buffer= << Buffer/binary, Data/binary >>});
  123. {error, timeout} -> error_terminate(408, State);
  124. {error, closed} -> terminate(State)
  125. end.
  126. -spec header({http_header, integer(), http_header(), any(), binary()}
  127. | http_eoh, #http_req{}, #state{}) -> ok.
  128. header({http_header, _I, 'Host', _R, RawHost}, Req=#http_req{
  129. transport=Transport, host=undefined}, State) ->
  130. RawHost2 = binary_to_lower(RawHost),
  131. case catch cowboy_dispatcher:split_host(RawHost2) of
  132. {Host, RawHost3, undefined} ->
  133. Port = default_port(Transport:name()),
  134. dispatch(fun parse_header/2, Req#http_req{
  135. host=Host, raw_host=RawHost3, port=Port,
  136. headers=[{'Host', RawHost3}|Req#http_req.headers]}, State);
  137. {Host, RawHost3, Port} ->
  138. dispatch(fun parse_header/2, Req#http_req{
  139. host=Host, raw_host=RawHost3, port=Port,
  140. headers=[{'Host', RawHost3}|Req#http_req.headers]}, State);
  141. {'EXIT', _Reason} ->
  142. error_terminate(400, State)
  143. end;
  144. %% Ignore Host headers if we already have it.
  145. header({http_header, _I, 'Host', _R, _V}, Req, State) ->
  146. parse_header(Req, State);
  147. header({http_header, _I, 'Connection', _R, Connection}, Req, State) ->
  148. ConnAtom = connection_to_atom(Connection),
  149. parse_header(Req#http_req{connection=ConnAtom,
  150. headers=[{'Connection', Connection}|Req#http_req.headers]}, State);
  151. header({http_header, _I, Field, _R, Value}, Req, State) ->
  152. Field2 = format_header(Field),
  153. parse_header(Req#http_req{headers=[{Field2, Value}|Req#http_req.headers]},
  154. State);
  155. %% The Host header is required in HTTP/1.1.
  156. header(http_eoh, #http_req{version={1, 1}, host=undefined}, State) ->
  157. error_terminate(400, State);
  158. %% It is however optional in HTTP/1.0.
  159. header(http_eoh, Req=#http_req{version={1, 0}, transport=Transport,
  160. host=undefined}, State=#state{buffer=Buffer}) ->
  161. Port = default_port(Transport:name()),
  162. dispatch(fun handler_init/2, Req#http_req{host=[], raw_host= <<>>,
  163. port=Port, buffer=Buffer}, State#state{buffer= <<>>});
  164. header(http_eoh, Req, State=#state{buffer=Buffer}) ->
  165. handler_init(Req#http_req{buffer=Buffer}, State#state{buffer= <<>>});
  166. header({http_error, _Bin}, _Req, State) ->
  167. error_terminate(500, State).
  168. -spec dispatch(fun((#http_req{}, #state{}) -> ok),
  169. #http_req{}, #state{}) -> ok.
  170. dispatch(Next, Req=#http_req{host=Host, path=Path},
  171. State=#state{dispatch=Dispatch}) ->
  172. %% @todo We probably want to filter the Host and Path here to allow
  173. %% things like url rewriting.
  174. case cowboy_dispatcher:match(Host, Path, Dispatch) of
  175. {ok, Handler, Opts, Binds, HostInfo, PathInfo} ->
  176. Next(Req#http_req{host_info=HostInfo, path_info=PathInfo,
  177. bindings=Binds}, State#state{handler={Handler, Opts}});
  178. {error, notfound, host} ->
  179. error_terminate(400, State);
  180. {error, notfound, path} ->
  181. error_terminate(404, State)
  182. end.
  183. -spec handler_init(#http_req{}, #state{}) -> ok.
  184. handler_init(Req, State=#state{listener=ListenerPid,
  185. transport=Transport, handler={Handler, Opts}}) ->
  186. try Handler:init({Transport:name(), http}, Req, Opts) of
  187. {ok, Req2, HandlerState} ->
  188. handler_loop(HandlerState, Req2, State);
  189. %% @todo {upgrade, transport, Module}
  190. {upgrade, protocol, Module} ->
  191. Module:upgrade(ListenerPid, Handler, Opts, Req)
  192. catch Class:Reason ->
  193. error_terminate(500, State),
  194. error_logger:error_msg(
  195. "** Handler ~p terminating in init/3~n"
  196. " for the reason ~p:~p~n"
  197. "** Options were ~p~n"
  198. "** Request was ~p~n** Stacktrace: ~p~n~n",
  199. [Handler, Class, Reason, Opts, Req, erlang:get_stacktrace()])
  200. end.
  201. -spec handler_loop(any(), #http_req{}, #state{}) -> ok.
  202. handler_loop(HandlerState, Req, State=#state{handler={Handler, Opts}}) ->
  203. try Handler:handle(Req#http_req{resp_state=waiting}, HandlerState) of
  204. {ok, Req2, HandlerState2} ->
  205. next_request(HandlerState2, Req2, State)
  206. catch Class:Reason ->
  207. error_logger:error_msg(
  208. "** Handler ~p terminating in handle/2~n"
  209. " for the reason ~p:~p~n"
  210. "** Options were ~p~n** Handler state was ~p~n"
  211. "** Request was ~p~n** Stacktrace: ~p~n~n",
  212. [Handler, Class, Reason, Opts,
  213. HandlerState, Req, erlang:get_stacktrace()]),
  214. handler_terminate(HandlerState, Req, State),
  215. terminate(State)
  216. end.
  217. -spec handler_terminate(any(), #http_req{}, #state{}) -> ok | error.
  218. handler_terminate(HandlerState, Req, #state{handler={Handler, Opts}}) ->
  219. try
  220. Handler:terminate(Req#http_req{resp_state=locked}, HandlerState)
  221. catch Class:Reason ->
  222. error_logger:error_msg(
  223. "** Handler ~p terminating in terminate/2~n"
  224. " for the reason ~p:~p~n"
  225. "** Options were ~p~n** Handler state was ~p~n"
  226. "** Request was ~p~n** Stacktrace: ~p~n~n",
  227. [Handler, Class, Reason, Opts,
  228. HandlerState, Req, erlang:get_stacktrace()]),
  229. error
  230. end.
  231. -spec next_request(any(), #http_req{}, #state{}) -> ok.
  232. next_request(HandlerState, Req=#http_req{connection=Conn, buffer=Buffer},
  233. State) ->
  234. HandlerRes = handler_terminate(HandlerState, Req, State),
  235. BodyRes = ensure_body_processed(Req),
  236. RespRes = ensure_response(Req),
  237. case {HandlerRes, BodyRes, RespRes, Conn} of
  238. {ok, ok, ok, keepalive} ->
  239. ?MODULE:parse_request(State#state{
  240. buffer=Buffer, req_empty_lines=0});
  241. _Closed ->
  242. terminate(State)
  243. end.
  244. -spec ensure_body_processed(#http_req{}) -> ok | close.
  245. ensure_body_processed(#http_req{body_state=done}) ->
  246. ok;
  247. ensure_body_processed(Req=#http_req{body_state=waiting}) ->
  248. case cowboy_http_req:body(Req) of
  249. {error, badarg} -> ok; %% No body.
  250. {error, _Reason} -> close;
  251. _Any -> ok
  252. end.
  253. -spec ensure_response(#http_req{}) -> ok.
  254. %% The handler has already fully replied to the client.
  255. ensure_response(#http_req{resp_state=done}) ->
  256. ok;
  257. %% No response has been sent but everything apparently went fine.
  258. %% Reply with 204 No Content to indicate this.
  259. ensure_response(Req=#http_req{resp_state=waiting}) ->
  260. _ = cowboy_http_req:reply(204, [], [], Req),
  261. ok;
  262. %% Close the chunked reply.
  263. ensure_response(#http_req{socket=Socket, transport=Transport,
  264. resp_state=chunks}) ->
  265. Transport:send(Socket, <<"0\r\n\r\n">>),
  266. close.
  267. -spec error_terminate(http_status(), #state{}) -> ok.
  268. error_terminate(Code, State=#state{socket=Socket, transport=Transport}) ->
  269. _ = cowboy_http_req:reply(Code, [], [], #http_req{
  270. socket=Socket, transport=Transport,
  271. connection=close, resp_state=waiting}),
  272. terminate(State).
  273. -spec terminate(#state{}) -> ok.
  274. terminate(#state{socket=Socket, transport=Transport}) ->
  275. Transport:close(Socket),
  276. ok.
  277. %% Internal.
  278. -spec version_to_connection(http_version()) -> keepalive | close.
  279. version_to_connection({1, 1}) -> keepalive;
  280. version_to_connection(_Any) -> close.
  281. %% @todo Connection can take more than one value.
  282. -spec connection_to_atom(binary()) -> keepalive | close.
  283. connection_to_atom(<<"keep-alive">>) ->
  284. keepalive;
  285. connection_to_atom(<<"close">>) ->
  286. close;
  287. connection_to_atom(Connection) ->
  288. case binary_to_lower(Connection) of
  289. <<"close">> -> close;
  290. _Any -> keepalive
  291. end.
  292. -spec default_port(atom()) -> 80 | 443.
  293. default_port(ssl) -> 443;
  294. default_port(_) -> 80.
  295. %% @todo While 32 should be enough for everybody, we should probably make
  296. %% this configurable or something.
  297. -spec format_header(atom()) -> atom(); (binary()) -> binary().
  298. format_header(Field) when is_atom(Field) ->
  299. Field;
  300. format_header(Field) when byte_size(Field) =< 20; byte_size(Field) > 32 ->
  301. Field;
  302. format_header(Field) ->
  303. format_header(Field, true, <<>>).
  304. -spec format_header(binary(), boolean(), binary()) -> binary().
  305. format_header(<<>>, _Any, Acc) ->
  306. Acc;
  307. %% Replicate a bug in OTP for compatibility reasons when there's a - right
  308. %% after another. Proper use should always be 'true' instead of 'not Bool'.
  309. format_header(<< $-, Rest/bits >>, Bool, Acc) ->
  310. format_header(Rest, not Bool, << Acc/binary, $- >>);
  311. format_header(<< C, Rest/bits >>, true, Acc) ->
  312. format_header(Rest, false, << Acc/binary, (char_to_upper(C)) >>);
  313. format_header(<< C, Rest/bits >>, false, Acc) ->
  314. format_header(Rest, false, << Acc/binary, (char_to_lower(C)) >>).
  315. %% We are excluding a few characters on purpose.
  316. -spec binary_to_lower(binary()) -> binary().
  317. binary_to_lower(L) ->
  318. << << (char_to_lower(C)) >> || << C >> <= L >>.
  319. %% We gain noticeable speed by matching each value directly.
  320. -spec char_to_lower(char()) -> char().
  321. char_to_lower($A) -> $a;
  322. char_to_lower($B) -> $b;
  323. char_to_lower($C) -> $c;
  324. char_to_lower($D) -> $d;
  325. char_to_lower($E) -> $e;
  326. char_to_lower($F) -> $f;
  327. char_to_lower($G) -> $g;
  328. char_to_lower($H) -> $h;
  329. char_to_lower($I) -> $i;
  330. char_to_lower($J) -> $j;
  331. char_to_lower($K) -> $k;
  332. char_to_lower($L) -> $l;
  333. char_to_lower($M) -> $m;
  334. char_to_lower($N) -> $n;
  335. char_to_lower($O) -> $o;
  336. char_to_lower($P) -> $p;
  337. char_to_lower($Q) -> $q;
  338. char_to_lower($R) -> $r;
  339. char_to_lower($S) -> $s;
  340. char_to_lower($T) -> $t;
  341. char_to_lower($U) -> $u;
  342. char_to_lower($V) -> $v;
  343. char_to_lower($W) -> $w;
  344. char_to_lower($X) -> $x;
  345. char_to_lower($Y) -> $y;
  346. char_to_lower($Z) -> $z;
  347. char_to_lower(Ch) -> Ch.
  348. -spec char_to_upper(char()) -> char().
  349. char_to_upper($a) -> $A;
  350. char_to_upper($b) -> $B;
  351. char_to_upper($c) -> $C;
  352. char_to_upper($d) -> $D;
  353. char_to_upper($e) -> $E;
  354. char_to_upper($f) -> $F;
  355. char_to_upper($g) -> $G;
  356. char_to_upper($h) -> $H;
  357. char_to_upper($i) -> $I;
  358. char_to_upper($j) -> $J;
  359. char_to_upper($k) -> $K;
  360. char_to_upper($l) -> $L;
  361. char_to_upper($m) -> $M;
  362. char_to_upper($n) -> $N;
  363. char_to_upper($o) -> $O;
  364. char_to_upper($p) -> $P;
  365. char_to_upper($q) -> $Q;
  366. char_to_upper($r) -> $R;
  367. char_to_upper($s) -> $S;
  368. char_to_upper($t) -> $T;
  369. char_to_upper($u) -> $U;
  370. char_to_upper($v) -> $V;
  371. char_to_upper($w) -> $W;
  372. char_to_upper($x) -> $X;
  373. char_to_upper($y) -> $Y;
  374. char_to_upper($z) -> $Z;
  375. char_to_upper(Ch) -> Ch.
  376. %% Tests.
  377. -ifdef(TEST).
  378. format_header_test_() ->
  379. %% {Header, Result}
  380. Tests = [
  381. {<<"Sec-Websocket-Version">>, <<"Sec-Websocket-Version">>},
  382. {<<"Sec-WebSocket-Version">>, <<"Sec-Websocket-Version">>},
  383. {<<"sec-websocket-version">>, <<"Sec-Websocket-Version">>},
  384. {<<"SEC-WEBSOCKET-VERSION">>, <<"Sec-Websocket-Version">>},
  385. %% These last tests ensures we're formatting headers exactly like OTP.
  386. %% Even though it's dumb, it's better for compatibility reasons.
  387. {<<"Sec-WebSocket--Version">>, <<"Sec-Websocket--version">>},
  388. {<<"Sec-WebSocket---Version">>, <<"Sec-Websocket---Version">>}
  389. ],
  390. [{H, fun() -> R = format_header(H) end} || {H, R} <- Tests].
  391. -endif.