cowboy_http_protocol.erl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. %% <dt>urldecode</dt><dd>Function and options argument to use when decoding
  25. %% URL encoded strings. Defaults to `{fun cowboy_http:urldecode/2, crash}'.
  26. %% </dd>
  27. %% </dl>
  28. %%
  29. %% Note that there is no need to monitor these processes when using Cowboy as
  30. %% an application as it already supervises them under the listener supervisor.
  31. %%
  32. %% @see cowboy_dispatcher
  33. %% @see cowboy_http_handler
  34. -module(cowboy_http_protocol).
  35. -behaviour(cowboy_protocol).
  36. -export([start_link/4]). %% API.
  37. -export([init/4, parse_request/1, handler_loop/3]). %% FSM.
  38. -include("include/http.hrl").
  39. -include_lib("eunit/include/eunit.hrl").
  40. -record(state, {
  41. listener :: pid(),
  42. socket :: inet:socket(),
  43. transport :: module(),
  44. dispatch :: cowboy_dispatcher:dispatch_rules(),
  45. handler :: {module(), any()},
  46. urldecode :: {fun((binary(), T) -> binary()), T},
  47. req_empty_lines = 0 :: integer(),
  48. max_empty_lines :: integer(),
  49. max_line_length :: integer(),
  50. timeout :: timeout(),
  51. buffer = <<>> :: binary(),
  52. hibernate = false :: boolean(),
  53. loop_timeout = infinity :: timeout(),
  54. loop_timeout_ref :: undefined | reference()
  55. }).
  56. %% API.
  57. %% @doc Start an HTTP protocol process.
  58. -spec start_link(pid(), inet:socket(), module(), any()) -> {ok, pid()}.
  59. start_link(ListenerPid, Socket, Transport, Opts) ->
  60. Pid = spawn_link(?MODULE, init, [ListenerPid, Socket, Transport, Opts]),
  61. {ok, Pid}.
  62. %% FSM.
  63. %% @private
  64. -spec init(pid(), inet:socket(), module(), any()) -> ok | none().
  65. init(ListenerPid, Socket, Transport, Opts) ->
  66. Dispatch = proplists:get_value(dispatch, Opts, []),
  67. MaxEmptyLines = proplists:get_value(max_empty_lines, Opts, 5),
  68. MaxLineLength = proplists:get_value(max_line_length, Opts, 4096),
  69. Timeout = proplists:get_value(timeout, Opts, 5000),
  70. URLDecDefault = {fun cowboy_http:urldecode/2, crash},
  71. URLDec = proplists:get_value(urldecode, Opts, URLDecDefault),
  72. receive shoot -> ok end,
  73. wait_request(#state{listener=ListenerPid, socket=Socket, transport=Transport,
  74. dispatch=Dispatch, max_empty_lines=MaxEmptyLines,
  75. max_line_length=MaxLineLength, timeout=Timeout, urldecode=URLDec}).
  76. %% @private
  77. -spec parse_request(#state{}) -> ok | none().
  78. %% We limit the length of the Request-line to MaxLength to avoid endlessly
  79. %% reading from the socket and eventually crashing.
  80. parse_request(State=#state{buffer=Buffer, max_line_length=MaxLength}) ->
  81. case erlang:decode_packet(http_bin, Buffer, []) of
  82. {ok, Request, Rest} -> request(Request, State#state{buffer=Rest});
  83. {more, _Length} when byte_size(Buffer) > MaxLength ->
  84. error_terminate(413, State);
  85. {more, _Length} -> wait_request(State);
  86. {error, _Reason} -> error_terminate(400, State)
  87. end.
  88. -spec wait_request(#state{}) -> ok | none().
  89. wait_request(State=#state{socket=Socket, transport=Transport,
  90. timeout=T, buffer=Buffer}) ->
  91. case Transport:recv(Socket, 0, T) of
  92. {ok, Data} -> parse_request(State#state{
  93. buffer= << Buffer/binary, Data/binary >>});
  94. {error, _Reason} -> terminate(State)
  95. end.
  96. -spec request({http_request, http_method(), http_uri(),
  97. http_version()}, #state{}) -> ok | none().
  98. %% @todo We probably want to handle some things differently between versions.
  99. request({http_request, _Method, _URI, Version}, State)
  100. when Version =/= {1, 0}, Version =/= {1, 1} ->
  101. error_terminate(505, State);
  102. %% @todo We need to cleanup the URI properly.
  103. request({http_request, Method, {abs_path, AbsPath}, Version},
  104. State=#state{socket=Socket, transport=Transport,
  105. urldecode={URLDecFun, URLDecArg}=URLDec}) ->
  106. URLDecode = fun(Bin) -> URLDecFun(Bin, URLDecArg) end,
  107. {Path, RawPath, Qs} = cowboy_dispatcher:split_path(AbsPath, URLDecode),
  108. ConnAtom = version_to_connection(Version),
  109. parse_header(#http_req{socket=Socket, transport=Transport,
  110. connection=ConnAtom, method=Method, version=Version,
  111. path=Path, raw_path=RawPath, raw_qs=Qs, urldecode=URLDec}, State);
  112. request({http_request, Method, '*', Version},
  113. State=#state{socket=Socket, transport=Transport, urldecode=URLDec}) ->
  114. ConnAtom = version_to_connection(Version),
  115. parse_header(#http_req{socket=Socket, transport=Transport,
  116. connection=ConnAtom, method=Method, version=Version,
  117. path='*', raw_path= <<"*">>, raw_qs= <<>>, urldecode=URLDec}, State);
  118. request({http_request, _Method, _URI, _Version}, State) ->
  119. error_terminate(501, State);
  120. request({http_error, <<"\r\n">>},
  121. State=#state{req_empty_lines=N, max_empty_lines=N}) ->
  122. error_terminate(400, State);
  123. request({http_error, <<"\r\n">>}, State=#state{req_empty_lines=N}) ->
  124. parse_request(State#state{req_empty_lines=N + 1});
  125. request(_Any, State) ->
  126. error_terminate(400, State).
  127. -spec parse_header(#http_req{}, #state{}) -> ok | none().
  128. parse_header(Req, State=#state{buffer=Buffer, max_line_length=MaxLength}) ->
  129. case erlang:decode_packet(httph_bin, Buffer, []) of
  130. {ok, Header, Rest} -> header(Header, Req, State#state{buffer=Rest});
  131. {more, _Length} when byte_size(Buffer) > MaxLength ->
  132. error_terminate(413, State);
  133. {more, _Length} -> wait_header(Req, State);
  134. {error, _Reason} -> error_terminate(400, State)
  135. end.
  136. -spec wait_header(#http_req{}, #state{}) -> ok | none().
  137. wait_header(Req, State=#state{socket=Socket,
  138. transport=Transport, timeout=T, buffer=Buffer}) ->
  139. case Transport:recv(Socket, 0, T) of
  140. {ok, Data} -> parse_header(Req, State#state{
  141. buffer= << Buffer/binary, Data/binary >>});
  142. {error, timeout} -> error_terminate(408, State);
  143. {error, closed} -> terminate(State)
  144. end.
  145. -spec header({http_header, integer(), http_header(), any(), binary()}
  146. | http_eoh, #http_req{}, #state{}) -> ok | none().
  147. header({http_header, _I, 'Host', _R, RawHost}, Req=#http_req{
  148. transport=Transport, host=undefined}, State) ->
  149. RawHost2 = cowboy_bstr:to_lower(RawHost),
  150. case catch cowboy_dispatcher:split_host(RawHost2) of
  151. {Host, RawHost3, undefined} ->
  152. Port = default_port(Transport:name()),
  153. dispatch(fun parse_header/2, Req#http_req{
  154. host=Host, raw_host=RawHost3, port=Port,
  155. headers=[{'Host', RawHost3}|Req#http_req.headers]}, State);
  156. {Host, RawHost3, Port} ->
  157. dispatch(fun parse_header/2, Req#http_req{
  158. host=Host, raw_host=RawHost3, port=Port,
  159. headers=[{'Host', RawHost3}|Req#http_req.headers]}, State);
  160. {'EXIT', _Reason} ->
  161. error_terminate(400, State)
  162. end;
  163. %% Ignore Host headers if we already have it.
  164. header({http_header, _I, 'Host', _R, _V}, Req, State) ->
  165. parse_header(Req, State);
  166. header({http_header, _I, 'Connection', _R, Connection},
  167. Req=#http_req{headers=Headers}, State) ->
  168. Req2 = Req#http_req{headers=[{'Connection', Connection}|Headers]},
  169. {ConnTokens, Req3}
  170. = cowboy_http_req:parse_header('Connection', Req2),
  171. ConnAtom = cowboy_http:connection_to_atom(ConnTokens),
  172. parse_header(Req3#http_req{connection=ConnAtom}, State);
  173. header({http_header, _I, Field, _R, Value}, Req, State) ->
  174. Field2 = format_header(Field),
  175. parse_header(Req#http_req{headers=[{Field2, Value}|Req#http_req.headers]},
  176. State);
  177. %% The Host header is required in HTTP/1.1.
  178. header(http_eoh, #http_req{version={1, 1}, host=undefined}, State) ->
  179. error_terminate(400, State);
  180. %% It is however optional in HTTP/1.0.
  181. header(http_eoh, Req=#http_req{version={1, 0}, transport=Transport,
  182. host=undefined}, State=#state{buffer=Buffer}) ->
  183. Port = default_port(Transport:name()),
  184. dispatch(fun handler_init/2, Req#http_req{host=[], raw_host= <<>>,
  185. port=Port, buffer=Buffer}, State#state{buffer= <<>>});
  186. header(http_eoh, Req, State=#state{buffer=Buffer}) ->
  187. handler_init(Req#http_req{buffer=Buffer}, State#state{buffer= <<>>});
  188. header({http_error, _Bin}, _Req, State) ->
  189. error_terminate(500, State).
  190. -spec dispatch(fun((#http_req{}, #state{}) -> ok),
  191. #http_req{}, #state{}) -> ok | none().
  192. dispatch(Next, Req=#http_req{host=Host, path=Path},
  193. State=#state{dispatch=Dispatch}) ->
  194. %% @todo We probably want to filter the Host and Path here to allow
  195. %% things like url rewriting.
  196. case cowboy_dispatcher:match(Host, Path, Dispatch) of
  197. {ok, Handler, Opts, Binds, HostInfo, PathInfo} ->
  198. Next(Req#http_req{host_info=HostInfo, path_info=PathInfo,
  199. bindings=Binds}, State#state{handler={Handler, Opts}});
  200. {error, notfound, host} ->
  201. error_terminate(400, State);
  202. {error, notfound, path} ->
  203. error_terminate(404, State)
  204. end.
  205. -spec handler_init(#http_req{}, #state{}) -> ok | none().
  206. handler_init(Req, State=#state{listener=ListenerPid,
  207. transport=Transport, handler={Handler, Opts}}) ->
  208. try Handler:init({Transport:name(), http}, Req, Opts) of
  209. {ok, Req2, HandlerState} ->
  210. handler_handle(HandlerState, Req2, State);
  211. {loop, Req2, HandlerState} ->
  212. handler_before_loop(HandlerState, Req2, State);
  213. {loop, Req2, HandlerState, hibernate} ->
  214. handler_before_loop(HandlerState, Req2,
  215. State#state{hibernate=true});
  216. {loop, Req2, HandlerState, Timeout} ->
  217. handler_before_loop(HandlerState, Req2,
  218. State#state{loop_timeout=Timeout});
  219. {loop, Req2, HandlerState, Timeout, hibernate} ->
  220. handler_before_loop(HandlerState, Req2,
  221. State#state{hibernate=true, loop_timeout=Timeout});
  222. {shutdown, Req2, HandlerState} ->
  223. handler_terminate(HandlerState, Req2, State);
  224. %% @todo {upgrade, transport, Module}
  225. {upgrade, protocol, Module} ->
  226. Module:upgrade(ListenerPid, Handler, Opts, Req)
  227. catch Class:Reason ->
  228. error_terminate(500, State),
  229. error_logger:error_msg(
  230. "** Handler ~p terminating in init/3~n"
  231. " for the reason ~p:~p~n"
  232. "** Options were ~p~n"
  233. "** Request was ~p~n** Stacktrace: ~p~n~n",
  234. [Handler, Class, Reason, Opts, Req, erlang:get_stacktrace()])
  235. end.
  236. -spec handler_handle(any(), #http_req{}, #state{}) -> ok | none().
  237. handler_handle(HandlerState, Req, State=#state{handler={Handler, Opts}}) ->
  238. try Handler:handle(Req, HandlerState) of
  239. {ok, Req2, HandlerState2} ->
  240. next_request(HandlerState2, Req2, State)
  241. catch Class:Reason ->
  242. error_logger:error_msg(
  243. "** Handler ~p terminating in handle/2~n"
  244. " for the reason ~p:~p~n"
  245. "** Options were ~p~n** Handler state was ~p~n"
  246. "** Request was ~p~n** Stacktrace: ~p~n~n",
  247. [Handler, Class, Reason, Opts,
  248. HandlerState, Req, erlang:get_stacktrace()]),
  249. handler_terminate(HandlerState, Req, State),
  250. terminate(State)
  251. end.
  252. %% We don't listen for Transport closes because that would force us
  253. %% to receive data and buffer it indefinitely.
  254. -spec handler_before_loop(any(), #http_req{}, #state{}) -> ok | none().
  255. handler_before_loop(HandlerState, Req, State=#state{hibernate=true}) ->
  256. State2 = handler_loop_timeout(State),
  257. erlang:hibernate(?MODULE, handler_loop,
  258. [HandlerState, Req, State2#state{hibernate=false}]);
  259. handler_before_loop(HandlerState, Req, State) ->
  260. State2 = handler_loop_timeout(State),
  261. handler_loop(HandlerState, Req, State2).
  262. %% Almost the same code can be found in cowboy_http_websocket.
  263. -spec handler_loop_timeout(#state{}) -> #state{}.
  264. handler_loop_timeout(State=#state{loop_timeout=infinity}) ->
  265. State#state{loop_timeout_ref=undefined};
  266. handler_loop_timeout(State=#state{loop_timeout=Timeout,
  267. loop_timeout_ref=PrevRef}) ->
  268. _ = case PrevRef of undefined -> ignore; PrevRef ->
  269. erlang:cancel_timer(PrevRef) end,
  270. TRef = make_ref(),
  271. erlang:send_after(Timeout, self(), {?MODULE, timeout, TRef}),
  272. State#state{loop_timeout_ref=TRef}.
  273. -spec handler_loop(any(), #http_req{}, #state{}) -> ok | none().
  274. handler_loop(HandlerState, Req, State=#state{loop_timeout_ref=TRef}) ->
  275. receive
  276. {?MODULE, timeout, TRef} ->
  277. next_request(HandlerState, Req, State);
  278. {?MODULE, timeout, OlderTRef} when is_reference(OlderTRef) ->
  279. handler_loop(HandlerState, Req, State);
  280. Message ->
  281. handler_call(HandlerState, Req, State, Message)
  282. end.
  283. -spec handler_call(any(), #http_req{}, #state{}, any()) -> ok | none().
  284. handler_call(HandlerState, Req, State=#state{handler={Handler, Opts}},
  285. Message) ->
  286. try Handler:info(Message, Req, HandlerState) of
  287. {ok, Req2, HandlerState2} ->
  288. next_request(HandlerState2, Req2, State);
  289. {loop, Req2, HandlerState2} ->
  290. handler_before_loop(HandlerState2, Req2, State);
  291. {loop, Req2, HandlerState2, hibernate} ->
  292. handler_before_loop(HandlerState2, Req2,
  293. State#state{hibernate=true})
  294. catch Class:Reason ->
  295. error_logger:error_msg(
  296. "** Handler ~p terminating in info/3~n"
  297. " for the reason ~p:~p~n"
  298. "** Options were ~p~n** Handler state was ~p~n"
  299. "** Request was ~p~n** Stacktrace: ~p~n~n",
  300. [Handler, Class, Reason, Opts,
  301. HandlerState, Req, erlang:get_stacktrace()])
  302. end.
  303. -spec handler_terminate(any(), #http_req{}, #state{}) -> ok.
  304. handler_terminate(HandlerState, Req, #state{handler={Handler, Opts}}) ->
  305. try
  306. Handler:terminate(Req#http_req{resp_state=locked}, HandlerState)
  307. catch Class:Reason ->
  308. error_logger:error_msg(
  309. "** Handler ~p terminating in terminate/2~n"
  310. " for the reason ~p:~p~n"
  311. "** Options were ~p~n** Handler state was ~p~n"
  312. "** Request was ~p~n** Stacktrace: ~p~n~n",
  313. [Handler, Class, Reason, Opts,
  314. HandlerState, Req, erlang:get_stacktrace()])
  315. end.
  316. -spec next_request(any(), #http_req{}, #state{}) -> ok | none().
  317. next_request(HandlerState, Req=#http_req{connection=Conn, buffer=Buffer},
  318. State) ->
  319. HandlerRes = handler_terminate(HandlerState, Req, State),
  320. BodyRes = ensure_body_processed(Req),
  321. RespRes = ensure_response(Req),
  322. case {HandlerRes, BodyRes, RespRes, Conn} of
  323. {ok, ok, ok, keepalive} ->
  324. ?MODULE:parse_request(State#state{
  325. buffer=Buffer, req_empty_lines=0});
  326. _Closed ->
  327. terminate(State)
  328. end.
  329. -spec ensure_body_processed(#http_req{}) -> ok | close.
  330. ensure_body_processed(#http_req{body_state=done}) ->
  331. ok;
  332. ensure_body_processed(Req=#http_req{body_state=waiting}) ->
  333. case cowboy_http_req:body(Req) of
  334. {error, badarg} -> ok; %% No body.
  335. {error, _Reason} -> close;
  336. _Any -> ok
  337. end.
  338. -spec ensure_response(#http_req{}) -> ok.
  339. %% The handler has already fully replied to the client.
  340. ensure_response(#http_req{resp_state=done}) ->
  341. ok;
  342. %% No response has been sent but everything apparently went fine.
  343. %% Reply with 204 No Content to indicate this.
  344. ensure_response(Req=#http_req{resp_state=waiting}) ->
  345. _ = cowboy_http_req:reply(204, [], [], Req),
  346. ok;
  347. %% Close the chunked reply.
  348. ensure_response(#http_req{method='HEAD', resp_state=chunks}) ->
  349. close;
  350. ensure_response(#http_req{socket=Socket, transport=Transport,
  351. resp_state=chunks}) ->
  352. Transport:send(Socket, <<"0\r\n\r\n">>),
  353. close.
  354. -spec error_terminate(http_status(), #state{}) -> ok.
  355. error_terminate(Code, State=#state{socket=Socket, transport=Transport}) ->
  356. _ = cowboy_http_req:reply(Code, [], [], #http_req{
  357. socket=Socket, transport=Transport,
  358. connection=close, resp_state=waiting}),
  359. terminate(State).
  360. -spec terminate(#state{}) -> ok.
  361. terminate(#state{socket=Socket, transport=Transport}) ->
  362. Transport:close(Socket),
  363. ok.
  364. %% Internal.
  365. -spec version_to_connection(http_version()) -> keepalive | close.
  366. version_to_connection({1, 1}) -> keepalive;
  367. version_to_connection(_Any) -> close.
  368. -spec default_port(atom()) -> 80 | 443.
  369. default_port(ssl) -> 443;
  370. default_port(_) -> 80.
  371. %% @todo While 32 should be enough for everybody, we should probably make
  372. %% this configurable or something.
  373. -spec format_header(atom()) -> atom(); (binary()) -> binary().
  374. format_header(Field) when is_atom(Field) ->
  375. Field;
  376. format_header(Field) when byte_size(Field) =< 20; byte_size(Field) > 32 ->
  377. Field;
  378. format_header(Field) ->
  379. format_header(Field, true, <<>>).
  380. -spec format_header(binary(), boolean(), binary()) -> binary().
  381. format_header(<<>>, _Any, Acc) ->
  382. Acc;
  383. %% Replicate a bug in OTP for compatibility reasons when there's a - right
  384. %% after another. Proper use should always be 'true' instead of 'not Bool'.
  385. format_header(<< $-, Rest/bits >>, Bool, Acc) ->
  386. format_header(Rest, not Bool, << Acc/binary, $- >>);
  387. format_header(<< C, Rest/bits >>, true, Acc) ->
  388. format_header(Rest, false, << Acc/binary, (cowboy_bstr:char_to_upper(C)) >>);
  389. format_header(<< C, Rest/bits >>, false, Acc) ->
  390. format_header(Rest, false, << Acc/binary, (cowboy_bstr:char_to_lower(C)) >>).
  391. %% Tests.
  392. -ifdef(TEST).
  393. format_header_test_() ->
  394. %% {Header, Result}
  395. Tests = [
  396. {<<"Sec-Websocket-Version">>, <<"Sec-Websocket-Version">>},
  397. {<<"Sec-WebSocket-Version">>, <<"Sec-Websocket-Version">>},
  398. {<<"sec-websocket-version">>, <<"Sec-Websocket-Version">>},
  399. {<<"SEC-WEBSOCKET-VERSION">>, <<"Sec-Websocket-Version">>},
  400. %% These last tests ensures we're formatting headers exactly like OTP.
  401. %% Even though it's dumb, it's better for compatibility reasons.
  402. {<<"Sec-WebSocket--Version">>, <<"Sec-Websocket--version">>},
  403. {<<"Sec-WebSocket---Version">>, <<"Sec-Websocket---Version">>}
  404. ],
  405. [{H, fun() -> R = format_header(H) end} || {H, R} <- Tests].
  406. -endif.