cowboy_http2.erl 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. %% Copyright (c) 2015-2017, 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(cowboy_http2).
  15. -export([init/6]).
  16. -export([init/10]).
  17. -export([init/12]).
  18. -export([system_continue/3]).
  19. -export([system_terminate/4]).
  20. -export([system_code_change/4]).
  21. -type opts() :: #{
  22. active_n => pos_integer(),
  23. compress_buffering => boolean(),
  24. compress_threshold => non_neg_integer(),
  25. connection_type => worker | supervisor,
  26. connection_window_margin_size => 0..16#7fffffff,
  27. connection_window_update_threshold => 0..16#7fffffff,
  28. enable_connect_protocol => boolean(),
  29. env => cowboy_middleware:env(),
  30. idle_timeout => timeout(),
  31. inactivity_timeout => timeout(),
  32. initial_connection_window_size => 65535..16#7fffffff,
  33. initial_stream_window_size => 0..16#7fffffff,
  34. linger_timeout => timeout(),
  35. logger => module(),
  36. max_concurrent_streams => non_neg_integer() | infinity,
  37. max_connection_buffer_size => non_neg_integer(),
  38. max_connection_window_size => 0..16#7fffffff,
  39. max_decode_table_size => non_neg_integer(),
  40. max_encode_table_size => non_neg_integer(),
  41. max_frame_size_received => 16384..16777215,
  42. max_frame_size_sent => 16384..16777215 | infinity,
  43. max_received_frame_rate => {pos_integer(), timeout()},
  44. max_reset_stream_rate => {pos_integer(), timeout()},
  45. max_stream_buffer_size => non_neg_integer(),
  46. max_stream_window_size => 0..16#7fffffff,
  47. metrics_callback => cowboy_metrics_h:metrics_callback(),
  48. metrics_req_filter => fun((cowboy_req:req()) -> map()),
  49. metrics_resp_headers_filter => fun((cowboy:http_headers()) -> cowboy:http_headers()),
  50. middlewares => [module()],
  51. preface_timeout => timeout(),
  52. proxy_header => boolean(),
  53. sendfile => boolean(),
  54. settings_timeout => timeout(),
  55. shutdown_timeout => timeout(),
  56. stream_handlers => [module()],
  57. stream_window_data_threshold => 0..16#7fffffff,
  58. stream_window_margin_size => 0..16#7fffffff,
  59. stream_window_update_threshold => 0..16#7fffffff,
  60. tracer_callback => cowboy_tracer_h:tracer_callback(),
  61. tracer_flags => [atom()],
  62. tracer_match_specs => cowboy_tracer_h:tracer_match_specs(),
  63. %% Open ended because configured stream handlers might add options.
  64. _ => _
  65. }.
  66. -export_type([opts/0]).
  67. -record(stream, {
  68. %% Whether the stream is currently stopping.
  69. status = running :: running | stopping,
  70. %% Flow requested for this stream.
  71. flow = 0 :: non_neg_integer(),
  72. %% Stream state.
  73. state :: {module, any()}
  74. }).
  75. -record(state, {
  76. parent = undefined :: pid(),
  77. ref :: ranch:ref(),
  78. socket = undefined :: inet:socket(),
  79. transport :: module(),
  80. proxy_header :: undefined | ranch_proxy_header:proxy_info(),
  81. opts = #{} :: opts(),
  82. %% Timer for idle_timeout.
  83. timer = undefined :: undefined | reference(),
  84. %% Remote address and port for the connection.
  85. peer = undefined :: {inet:ip_address(), inet:port_number()},
  86. %% Local address and port for the connection.
  87. sock = undefined :: {inet:ip_address(), inet:port_number()},
  88. %% Client certificate (TLS only).
  89. cert :: undefined | binary(),
  90. %% HTTP/2 state machine.
  91. http2_status :: sequence | settings | upgrade | connected | closing,
  92. http2_machine :: cow_http2_machine:http2_machine(),
  93. %% HTTP/2 frame rate flood protection.
  94. frame_rate_num :: undefined | pos_integer(),
  95. frame_rate_time :: undefined | integer(),
  96. %% HTTP/2 reset stream flood protection.
  97. reset_rate_num :: undefined | pos_integer(),
  98. reset_rate_time :: undefined | integer(),
  99. %% Flow requested for all streams.
  100. flow = 0 :: non_neg_integer(),
  101. %% Currently active HTTP/2 streams. Streams may be initiated either
  102. %% by the client or by the server through PUSH_PROMISE frames.
  103. streams = #{} :: #{cow_http2:streamid() => #stream{}},
  104. %% Streams can spawn zero or more children which are then managed
  105. %% by this module if operating as a supervisor.
  106. children = cowboy_children:init() :: cowboy_children:children()
  107. }).
  108. -spec init(pid(), ranch:ref(), inet:socket(), module(),
  109. ranch_proxy_header:proxy_info() | undefined, cowboy:opts()) -> ok.
  110. init(Parent, Ref, Socket, Transport, ProxyHeader, Opts) ->
  111. Peer0 = Transport:peername(Socket),
  112. Sock0 = Transport:sockname(Socket),
  113. Cert1 = case Transport:name() of
  114. ssl ->
  115. case ssl:peercert(Socket) of
  116. {error, no_peercert} ->
  117. {ok, undefined};
  118. Cert0 ->
  119. Cert0
  120. end;
  121. _ ->
  122. {ok, undefined}
  123. end,
  124. case {Peer0, Sock0, Cert1} of
  125. {{ok, Peer}, {ok, Sock}, {ok, Cert}} ->
  126. init(Parent, Ref, Socket, Transport, ProxyHeader, Opts, Peer, Sock, Cert, <<>>);
  127. {{error, Reason}, _, _} ->
  128. terminate(undefined, {socket_error, Reason,
  129. 'A socket error occurred when retrieving the peer name.'});
  130. {_, {error, Reason}, _} ->
  131. terminate(undefined, {socket_error, Reason,
  132. 'A socket error occurred when retrieving the sock name.'});
  133. {_, _, {error, Reason}} ->
  134. terminate(undefined, {socket_error, Reason,
  135. 'A socket error occurred when retrieving the client TLS certificate.'})
  136. end.
  137. -spec init(pid(), ranch:ref(), inet:socket(), module(),
  138. ranch_proxy_header:proxy_info() | undefined, cowboy:opts(),
  139. {inet:ip_address(), inet:port_number()}, {inet:ip_address(), inet:port_number()},
  140. binary() | undefined, binary()) -> ok.
  141. init(Parent, Ref, Socket, Transport, ProxyHeader, Opts, Peer, Sock, Cert, Buffer) ->
  142. {ok, Preface, HTTP2Machine} = cow_http2_machine:init(server, Opts),
  143. State = set_timeout(init_rate_limiting(#state{parent=Parent, ref=Ref, socket=Socket,
  144. transport=Transport, proxy_header=ProxyHeader,
  145. opts=Opts, peer=Peer, sock=Sock, cert=Cert,
  146. http2_status=sequence, http2_machine=HTTP2Machine})),
  147. Transport:send(Socket, Preface),
  148. setopts_active(State),
  149. case Buffer of
  150. <<>> -> loop(State, Buffer);
  151. _ -> parse(State, Buffer)
  152. end.
  153. init_rate_limiting(State=#state{opts=Opts}) ->
  154. {FrameRateNum, FrameRatePeriod} = maps:get(max_received_frame_rate, Opts, {10000, 10000}),
  155. {ResetRateNum, ResetRatePeriod} = maps:get(max_reset_stream_rate, Opts, {10, 10000}),
  156. CurrentTime = erlang:monotonic_time(millisecond),
  157. State#state{
  158. frame_rate_num=FrameRateNum, frame_rate_time=add_period(CurrentTime, FrameRatePeriod),
  159. reset_rate_num=ResetRateNum, reset_rate_time=add_period(CurrentTime, ResetRatePeriod)
  160. }.
  161. add_period(_, infinity) -> infinity;
  162. add_period(Time, Period) -> Time + Period.
  163. %% @todo Add an argument for the request body.
  164. -spec init(pid(), ranch:ref(), inet:socket(), module(),
  165. ranch_proxy_header:proxy_info() | undefined, cowboy:opts(),
  166. {inet:ip_address(), inet:port_number()}, {inet:ip_address(), inet:port_number()},
  167. binary() | undefined, binary(), map() | undefined, cowboy_req:req()) -> ok.
  168. init(Parent, Ref, Socket, Transport, ProxyHeader, Opts, Peer, Sock, Cert, Buffer,
  169. _Settings, Req=#{method := Method}) ->
  170. {ok, Preface, HTTP2Machine0} = cow_http2_machine:init(server, Opts),
  171. {ok, StreamID, HTTP2Machine}
  172. = cow_http2_machine:init_upgrade_stream(Method, HTTP2Machine0),
  173. State0 = #state{parent=Parent, ref=Ref, socket=Socket,
  174. transport=Transport, proxy_header=ProxyHeader,
  175. opts=Opts, peer=Peer, sock=Sock, cert=Cert,
  176. http2_status=upgrade, http2_machine=HTTP2Machine},
  177. State1 = headers_frame(State0#state{
  178. http2_machine=HTTP2Machine}, StreamID, Req),
  179. %% We assume that the upgrade will be applied. A stream handler
  180. %% must not prevent the normal operations of the server.
  181. State2 = info(State1, 1, {switch_protocol, #{
  182. <<"connection">> => <<"Upgrade">>,
  183. <<"upgrade">> => <<"h2c">>
  184. }, ?MODULE, undefined}), %% @todo undefined or #{}?
  185. State = set_timeout(init_rate_limiting(State2#state{http2_status=sequence})),
  186. Transport:send(Socket, Preface),
  187. setopts_active(State),
  188. case Buffer of
  189. <<>> -> loop(State, Buffer);
  190. _ -> parse(State, Buffer)
  191. end.
  192. %% Because HTTP/2 has flow control and Cowboy has other rate limiting
  193. %% mechanisms implemented, a very large active_n value should be fine,
  194. %% as long as the stream handlers do their work in a timely manner.
  195. setopts_active(#state{socket=Socket, transport=Transport, opts=Opts}) ->
  196. N = maps:get(active_n, Opts, 100),
  197. Transport:setopts(Socket, [{active, N}]).
  198. loop(State=#state{parent=Parent, socket=Socket, transport=Transport,
  199. opts=Opts, timer=TimerRef, children=Children}, Buffer) ->
  200. Messages = Transport:messages(),
  201. InactivityTimeout = maps:get(inactivity_timeout, Opts, 300000),
  202. receive
  203. %% Socket messages.
  204. {OK, Socket, Data} when OK =:= element(1, Messages) ->
  205. parse(set_timeout(State), << Buffer/binary, Data/binary >>);
  206. {Closed, Socket} when Closed =:= element(2, Messages) ->
  207. terminate(State, {socket_error, closed, 'The socket has been closed.'});
  208. {Error, Socket, Reason} when Error =:= element(3, Messages) ->
  209. terminate(State, {socket_error, Reason, 'An error has occurred on the socket.'});
  210. {Passive, Socket} when Passive =:= element(4, Messages);
  211. %% Hardcoded for compatibility with Ranch 1.x.
  212. Passive =:= tcp_passive; Passive =:= ssl_passive ->
  213. setopts_active(State),
  214. loop(State, Buffer);
  215. %% System messages.
  216. {'EXIT', Parent, Reason} ->
  217. %% @todo Graceful shutdown here as well?
  218. terminate(State, {stop, {exit, Reason}, 'Parent process terminated.'});
  219. {system, From, Request} ->
  220. sys:handle_system_msg(Request, From, Parent, ?MODULE, [], {State, Buffer});
  221. %% Timeouts.
  222. {timeout, TimerRef, idle_timeout} ->
  223. terminate(State, {stop, timeout,
  224. 'Connection idle longer than configuration allows.'});
  225. {timeout, Ref, {shutdown, Pid}} ->
  226. cowboy_children:shutdown_timeout(Children, Ref, Pid),
  227. loop(State, Buffer);
  228. {timeout, TRef, {cow_http2_machine, Name}} ->
  229. loop(timeout(State, Name, TRef), Buffer);
  230. %% Messages pertaining to a stream.
  231. {{Pid, StreamID}, Msg} when Pid =:= self() ->
  232. loop(info(State, StreamID, Msg), Buffer);
  233. %% Exit signal from children.
  234. Msg = {'EXIT', Pid, _} ->
  235. loop(down(State, Pid, Msg), Buffer);
  236. %% Calls from supervisor module.
  237. {'$gen_call', From, Call} ->
  238. cowboy_children:handle_supervisor_call(Call, From, Children, ?MODULE),
  239. loop(State, Buffer);
  240. Msg ->
  241. cowboy:log(warning, "Received stray message ~p.", [Msg], Opts),
  242. loop(State, Buffer)
  243. after InactivityTimeout ->
  244. terminate(State, {internal_error, timeout, 'No message or data received before timeout.'})
  245. end.
  246. set_timeout(State=#state{opts=Opts, timer=TimerRef0}) ->
  247. ok = case TimerRef0 of
  248. undefined -> ok;
  249. _ -> erlang:cancel_timer(TimerRef0, [{async, true}, {info, false}])
  250. end,
  251. TimerRef = case maps:get(idle_timeout, Opts, 60000) of
  252. infinity -> undefined;
  253. Timeout -> erlang:start_timer(Timeout, self(), idle_timeout)
  254. end,
  255. State#state{timer=TimerRef}.
  256. %% HTTP/2 protocol parsing.
  257. parse(State=#state{http2_status=sequence}, Data) ->
  258. case cow_http2:parse_sequence(Data) of
  259. {ok, Rest} ->
  260. parse(State#state{http2_status=settings}, Rest);
  261. more ->
  262. loop(State, Data);
  263. Error = {connection_error, _, _} ->
  264. terminate(State, Error)
  265. end;
  266. parse(State=#state{http2_status=Status, http2_machine=HTTP2Machine, streams=Streams}, Data) ->
  267. MaxFrameSize = cow_http2_machine:get_local_setting(max_frame_size, HTTP2Machine),
  268. case cow_http2:parse(Data, MaxFrameSize) of
  269. {ok, Frame, Rest} ->
  270. parse(frame_rate(State, Frame), Rest);
  271. {ignore, Rest} ->
  272. parse(frame_rate(State, ignore), Rest);
  273. {stream_error, StreamID, Reason, Human, Rest} ->
  274. parse(reset_stream(State, StreamID, {stream_error, Reason, Human}), Rest);
  275. Error = {connection_error, _, _} ->
  276. terminate(State, Error);
  277. %% Terminate the connection if we are closing and all streams have completed.
  278. more when Status =:= closing, Streams =:= #{} ->
  279. terminate(State, {stop, normal, 'The connection is going away.'});
  280. more ->
  281. loop(State, Data)
  282. end.
  283. %% Frame rate flood protection.
  284. frame_rate(State0=#state{opts=Opts, frame_rate_num=Num0, frame_rate_time=Time}, Frame) ->
  285. {Result, State} = case Num0 - 1 of
  286. 0 ->
  287. CurrentTime = erlang:monotonic_time(millisecond),
  288. if
  289. CurrentTime < Time ->
  290. {error, State0};
  291. true ->
  292. %% When the option has a period of infinity we cannot reach this clause.
  293. {Num, Period} = maps:get(max_received_frame_rate, Opts, {1000, 10000}),
  294. {ok, State0#state{frame_rate_num=Num, frame_rate_time=CurrentTime + Period}}
  295. end;
  296. Num ->
  297. {ok, State0#state{frame_rate_num=Num}}
  298. end,
  299. case {Result, Frame} of
  300. {ok, ignore} -> ignored_frame(State);
  301. {ok, _} -> frame(State, Frame);
  302. {error, _} -> terminate(State, {connection_error, enhance_your_calm,
  303. 'Frame rate larger than configuration allows. Flood? (CVE-2019-9512, CVE-2019-9515, CVE-2019-9518)'})
  304. end.
  305. %% Frames received.
  306. %% We do nothing when receiving a lingering DATA frame.
  307. %% We already removed the stream flow from the connection
  308. %% flow and are therefore already accounting for the window
  309. %% being reduced by these frames.
  310. frame(State=#state{http2_machine=HTTP2Machine0}, Frame) ->
  311. case cow_http2_machine:frame(Frame, HTTP2Machine0) of
  312. {ok, HTTP2Machine} ->
  313. maybe_ack(State#state{http2_machine=HTTP2Machine}, Frame);
  314. {ok, {data, StreamID, IsFin, Data}, HTTP2Machine} ->
  315. data_frame(State#state{http2_machine=HTTP2Machine}, StreamID, IsFin, Data);
  316. {ok, {headers, StreamID, IsFin, Headers, PseudoHeaders, BodyLen}, HTTP2Machine} ->
  317. headers_frame(State#state{http2_machine=HTTP2Machine},
  318. StreamID, IsFin, Headers, PseudoHeaders, BodyLen);
  319. {ok, {trailers, _StreamID, _Trailers}, HTTP2Machine} ->
  320. %% @todo Propagate trailers.
  321. State#state{http2_machine=HTTP2Machine};
  322. {ok, {rst_stream, StreamID, Reason}, HTTP2Machine} ->
  323. rst_stream_frame(State#state{http2_machine=HTTP2Machine}, StreamID, Reason);
  324. {ok, GoAway={goaway, _, _, _}, HTTP2Machine} ->
  325. goaway(State#state{http2_machine=HTTP2Machine}, GoAway);
  326. {send, SendData, HTTP2Machine} ->
  327. %% We may need to send an alarm for each of the streams sending data.
  328. lists:foldl(
  329. fun({StreamID, _, _}, S) -> maybe_send_data_alarm(S, HTTP2Machine0, StreamID) end,
  330. send_data(maybe_ack(State#state{http2_machine=HTTP2Machine}, Frame), SendData, []),
  331. SendData);
  332. {error, {stream_error, StreamID, Reason, Human}, HTTP2Machine} ->
  333. reset_stream(State#state{http2_machine=HTTP2Machine},
  334. StreamID, {stream_error, Reason, Human});
  335. {error, Error={connection_error, _, _}, HTTP2Machine} ->
  336. terminate(State#state{http2_machine=HTTP2Machine}, Error)
  337. end.
  338. %% We use this opportunity to mark the HTTP/2 status as connected
  339. %% if we were still waiting for a SETTINGS frame.
  340. maybe_ack(State=#state{http2_status=settings}, Frame) ->
  341. maybe_ack(State#state{http2_status=connected}, Frame);
  342. maybe_ack(State=#state{socket=Socket, transport=Transport}, Frame) ->
  343. case Frame of
  344. {settings, _} -> Transport:send(Socket, cow_http2:settings_ack());
  345. {ping, Opaque} -> Transport:send(Socket, cow_http2:ping_ack(Opaque));
  346. _ -> ok
  347. end,
  348. State.
  349. data_frame(State0=#state{opts=Opts, flow=Flow, streams=Streams}, StreamID, IsFin, Data) ->
  350. case Streams of
  351. #{StreamID := Stream=#stream{status=running, flow=StreamFlow, state=StreamState0}} ->
  352. try cowboy_stream:data(StreamID, IsFin, Data, StreamState0) of
  353. {Commands, StreamState} ->
  354. %% Remove the amount of data received from the flow.
  355. %% We may receive more data than we requested. We ensure
  356. %% that the flow value doesn't go lower than 0.
  357. Size = byte_size(Data),
  358. State = update_window(State0#state{flow=max(0, Flow - Size),
  359. streams=Streams#{StreamID => Stream#stream{
  360. flow=max(0, StreamFlow - Size), state=StreamState}}},
  361. StreamID),
  362. commands(State, StreamID, Commands)
  363. catch Class:Exception:Stacktrace ->
  364. cowboy:log(cowboy_stream:make_error_log(data,
  365. [StreamID, IsFin, Data, StreamState0],
  366. Class, Exception, Stacktrace), Opts),
  367. reset_stream(State0, StreamID, {internal_error, {Class, Exception},
  368. 'Unhandled exception in cowboy_stream:data/4.'})
  369. end;
  370. %% We ignore DATA frames for streams that are stopping.
  371. #{} ->
  372. State0
  373. end.
  374. headers_frame(State, StreamID, IsFin, Headers,
  375. PseudoHeaders=#{method := <<"CONNECT">>}, _)
  376. when map_size(PseudoHeaders) =:= 2 ->
  377. early_error(State, StreamID, IsFin, Headers, PseudoHeaders, 501,
  378. 'The CONNECT method is currently not implemented. (RFC7231 4.3.6)');
  379. headers_frame(State, StreamID, IsFin, Headers,
  380. PseudoHeaders=#{method := <<"TRACE">>}, _) ->
  381. early_error(State, StreamID, IsFin, Headers, PseudoHeaders, 501,
  382. 'The TRACE method is currently not implemented. (RFC7231 4.3.8)');
  383. headers_frame(State, StreamID, IsFin, Headers, PseudoHeaders=#{authority := Authority}, BodyLen) ->
  384. headers_frame_parse_host(State, StreamID, IsFin, Headers, PseudoHeaders, BodyLen, Authority);
  385. headers_frame(State, StreamID, IsFin, Headers, PseudoHeaders, BodyLen) ->
  386. case lists:keyfind(<<"host">>, 1, Headers) of
  387. {_, Authority} ->
  388. headers_frame_parse_host(State, StreamID, IsFin, Headers, PseudoHeaders, BodyLen, Authority);
  389. _ ->
  390. reset_stream(State, StreamID, {stream_error, protocol_error,
  391. 'Requests translated from HTTP/1.1 must include a host header. (RFC7540 8.1.2.3, RFC7230 5.4)'})
  392. end.
  393. headers_frame_parse_host(State=#state{ref=Ref, peer=Peer, sock=Sock, cert=Cert, proxy_header=ProxyHeader},
  394. StreamID, IsFin, Headers, PseudoHeaders=#{method := Method, scheme := Scheme, path := PathWithQs},
  395. BodyLen, Authority) ->
  396. try cow_http_hd:parse_host(Authority) of
  397. {Host, Port0} ->
  398. Port = ensure_port(Scheme, Port0),
  399. try cow_http:parse_fullpath(PathWithQs) of
  400. {<<>>, _} ->
  401. reset_stream(State, StreamID, {stream_error, protocol_error,
  402. 'The path component must not be empty. (RFC7540 8.1.2.3)'});
  403. {Path, Qs} ->
  404. Req0 = #{
  405. ref => Ref,
  406. pid => self(),
  407. streamid => StreamID,
  408. peer => Peer,
  409. sock => Sock,
  410. cert => Cert,
  411. method => Method,
  412. scheme => Scheme,
  413. host => Host,
  414. port => Port,
  415. path => Path,
  416. qs => Qs,
  417. version => 'HTTP/2',
  418. headers => headers_to_map(Headers, #{}),
  419. has_body => IsFin =:= nofin,
  420. body_length => BodyLen
  421. },
  422. %% We add the PROXY header information if any.
  423. Req1 = case ProxyHeader of
  424. undefined -> Req0;
  425. _ -> Req0#{proxy_header => ProxyHeader}
  426. end,
  427. %% We add the protocol information for extended CONNECTs.
  428. Req = case PseudoHeaders of
  429. #{protocol := Protocol} -> Req1#{protocol => Protocol};
  430. _ -> Req1
  431. end,
  432. headers_frame(State, StreamID, Req)
  433. catch _:_ ->
  434. reset_stream(State, StreamID, {stream_error, protocol_error,
  435. 'The :path pseudo-header is invalid. (RFC7540 8.1.2.3)'})
  436. end
  437. catch _:_ ->
  438. reset_stream(State, StreamID, {stream_error, protocol_error,
  439. 'The :authority pseudo-header is invalid. (RFC7540 8.1.2.3)'})
  440. end.
  441. ensure_port(<<"http">>, undefined) -> 80;
  442. ensure_port(<<"https">>, undefined) -> 443;
  443. ensure_port(_, Port) -> Port.
  444. %% This function is necessary to properly handle duplicate headers
  445. %% and the special-case cookie header.
  446. headers_to_map([], Acc) ->
  447. Acc;
  448. headers_to_map([{Name, Value}|Tail], Acc0) ->
  449. Acc = case Acc0 of
  450. %% The cookie header does not use proper HTTP header lists.
  451. #{Name := Value0} when Name =:= <<"cookie">> ->
  452. Acc0#{Name => << Value0/binary, "; ", Value/binary >>};
  453. #{Name := Value0} ->
  454. Acc0#{Name => << Value0/binary, ", ", Value/binary >>};
  455. _ ->
  456. Acc0#{Name => Value}
  457. end,
  458. headers_to_map(Tail, Acc).
  459. headers_frame(State=#state{opts=Opts, streams=Streams}, StreamID, Req) ->
  460. try cowboy_stream:init(StreamID, Req, Opts) of
  461. {Commands, StreamState} ->
  462. commands(State#state{
  463. streams=Streams#{StreamID => #stream{state=StreamState}}},
  464. StreamID, Commands)
  465. catch Class:Exception:Stacktrace ->
  466. cowboy:log(cowboy_stream:make_error_log(init,
  467. [StreamID, Req, Opts],
  468. Class, Exception, Stacktrace), Opts),
  469. reset_stream(State, StreamID, {internal_error, {Class, Exception},
  470. 'Unhandled exception in cowboy_stream:init/3.'})
  471. end.
  472. early_error(State0=#state{ref=Ref, opts=Opts, peer=Peer},
  473. StreamID, _IsFin, Headers, #{method := Method},
  474. StatusCode0, HumanReadable) ->
  475. %% We automatically terminate the stream but it is not an error
  476. %% per se (at least not in the first implementation).
  477. Reason = {stream_error, no_error, HumanReadable},
  478. %% The partial Req is minimal for now. We only have one case
  479. %% where it can be called (when a method is completely disabled).
  480. %% @todo Fill in the other elements.
  481. PartialReq = #{
  482. ref => Ref,
  483. peer => Peer,
  484. method => Method,
  485. headers => headers_to_map(Headers, #{})
  486. },
  487. Resp = {response, StatusCode0, RespHeaders0=#{<<"content-length">> => <<"0">>}, <<>>},
  488. try cowboy_stream:early_error(StreamID, Reason, PartialReq, Resp, Opts) of
  489. {response, StatusCode, RespHeaders, RespBody} ->
  490. send_response(State0, StreamID, StatusCode, RespHeaders, RespBody)
  491. catch Class:Exception:Stacktrace ->
  492. cowboy:log(cowboy_stream:make_error_log(early_error,
  493. [StreamID, Reason, PartialReq, Resp, Opts],
  494. Class, Exception, Stacktrace), Opts),
  495. %% We still need to send an error response, so send what we initially
  496. %% wanted to send. It's better than nothing.
  497. send_headers(State0, StreamID, fin, StatusCode0, RespHeaders0)
  498. end.
  499. rst_stream_frame(State=#state{streams=Streams0, children=Children0}, StreamID, Reason) ->
  500. case maps:take(StreamID, Streams0) of
  501. {#stream{state=StreamState}, Streams} ->
  502. terminate_stream_handler(State, StreamID, Reason, StreamState),
  503. Children = cowboy_children:shutdown(Children0, StreamID),
  504. State#state{streams=Streams, children=Children};
  505. error ->
  506. State
  507. end.
  508. ignored_frame(State=#state{http2_machine=HTTP2Machine0}) ->
  509. case cow_http2_machine:ignored_frame(HTTP2Machine0) of
  510. {ok, HTTP2Machine} ->
  511. State#state{http2_machine=HTTP2Machine};
  512. {error, Error={connection_error, _, _}, HTTP2Machine} ->
  513. terminate(State#state{http2_machine=HTTP2Machine}, Error)
  514. end.
  515. %% HTTP/2 timeouts.
  516. timeout(State=#state{http2_machine=HTTP2Machine0}, Name, TRef) ->
  517. case cow_http2_machine:timeout(Name, TRef, HTTP2Machine0) of
  518. {ok, HTTP2Machine} ->
  519. State#state{http2_machine=HTTP2Machine};
  520. {error, Error={connection_error, _, _}, HTTP2Machine} ->
  521. terminate(State#state{http2_machine=HTTP2Machine}, Error)
  522. end.
  523. %% Erlang messages.
  524. down(State=#state{opts=Opts, children=Children0}, Pid, Msg) ->
  525. case cowboy_children:down(Children0, Pid) of
  526. %% The stream was terminated already.
  527. {ok, undefined, Children} ->
  528. State#state{children=Children};
  529. %% The stream is still running.
  530. {ok, StreamID, Children} ->
  531. info(State#state{children=Children}, StreamID, Msg);
  532. %% The process was unknown.
  533. error ->
  534. cowboy:log(warning, "Received EXIT signal ~p for unknown process ~p.~n",
  535. [Msg, Pid], Opts),
  536. State
  537. end.
  538. info(State=#state{opts=Opts, http2_machine=HTTP2Machine, streams=Streams}, StreamID, Msg) ->
  539. case Streams of
  540. #{StreamID := Stream=#stream{state=StreamState0}} ->
  541. try cowboy_stream:info(StreamID, Msg, StreamState0) of
  542. {Commands, StreamState} ->
  543. commands(State#state{streams=Streams#{StreamID => Stream#stream{state=StreamState}}},
  544. StreamID, Commands)
  545. catch Class:Exception:Stacktrace ->
  546. cowboy:log(cowboy_stream:make_error_log(info,
  547. [StreamID, Msg, StreamState0],
  548. Class, Exception, Stacktrace), Opts),
  549. reset_stream(State, StreamID, {internal_error, {Class, Exception},
  550. 'Unhandled exception in cowboy_stream:info/3.'})
  551. end;
  552. _ ->
  553. case cow_http2_machine:is_lingering_stream(StreamID, HTTP2Machine) of
  554. true ->
  555. ok;
  556. false ->
  557. cowboy:log(warning, "Received message ~p for unknown stream ~p.",
  558. [Msg, StreamID], Opts)
  559. end,
  560. State
  561. end.
  562. %% Stream handler commands.
  563. %%
  564. %% @todo Kill the stream if it tries to send a response, headers,
  565. %% data or push promise when the stream is closed or half-closed.
  566. commands(State, _, []) ->
  567. State;
  568. %% Error responses are sent only if a response wasn't sent already.
  569. commands(State=#state{http2_machine=HTTP2Machine}, StreamID,
  570. [{error_response, StatusCode, Headers, Body}|Tail]) ->
  571. case cow_http2_machine:get_stream_local_state(StreamID, HTTP2Machine) of
  572. {ok, idle, _} ->
  573. commands(State, StreamID, [{response, StatusCode, Headers, Body}|Tail]);
  574. _ ->
  575. commands(State, StreamID, Tail)
  576. end;
  577. %% Send an informational response.
  578. commands(State0, StreamID, [{inform, StatusCode, Headers}|Tail]) ->
  579. State = send_headers(State0, StreamID, idle, StatusCode, Headers),
  580. commands(State, StreamID, Tail);
  581. %% Send response headers.
  582. commands(State0, StreamID, [{response, StatusCode, Headers, Body}|Tail]) ->
  583. State = send_response(State0, StreamID, StatusCode, Headers, Body),
  584. commands(State, StreamID, Tail);
  585. %% Send response headers.
  586. commands(State0, StreamID, [{headers, StatusCode, Headers}|Tail]) ->
  587. State = send_headers(State0, StreamID, nofin, StatusCode, Headers),
  588. commands(State, StreamID, Tail);
  589. %% Send a response body chunk.
  590. commands(State0, StreamID, [{data, IsFin, Data}|Tail]) ->
  591. State = maybe_send_data(State0, StreamID, IsFin, Data, []),
  592. commands(State, StreamID, Tail);
  593. %% Send trailers.
  594. commands(State0, StreamID, [{trailers, Trailers}|Tail]) ->
  595. State = maybe_send_data(State0, StreamID, fin, {trailers, maps:to_list(Trailers)}, []),
  596. commands(State, StreamID, Tail);
  597. %% Send a push promise.
  598. %%
  599. %% @todo Responses sent as a result of a push_promise request
  600. %% must not send push_promise frames themselves.
  601. %%
  602. %% @todo We should not send push_promise frames when we are
  603. %% in the closing http2_status.
  604. commands(State0=#state{socket=Socket, transport=Transport, http2_machine=HTTP2Machine0},
  605. StreamID, [{push, Method, Scheme, Host, Port, Path, Qs, Headers0}|Tail]) ->
  606. Authority = case {Scheme, Port} of
  607. {<<"http">>, 80} -> Host;
  608. {<<"https">>, 443} -> Host;
  609. _ -> iolist_to_binary([Host, $:, integer_to_binary(Port)])
  610. end,
  611. PathWithQs = iolist_to_binary(case Qs of
  612. <<>> -> Path;
  613. _ -> [Path, $?, Qs]
  614. end),
  615. PseudoHeaders = #{
  616. method => Method,
  617. scheme => Scheme,
  618. authority => Authority,
  619. path => PathWithQs
  620. },
  621. %% We need to make sure the header value is binary before we can
  622. %% create the Req object, as it expects them to be flat.
  623. Headers = maps:to_list(maps:map(fun(_, V) -> iolist_to_binary(V) end, Headers0)),
  624. State = case cow_http2_machine:prepare_push_promise(StreamID, HTTP2Machine0,
  625. PseudoHeaders, Headers) of
  626. {ok, PromisedStreamID, HeaderBlock, HTTP2Machine} ->
  627. Transport:send(Socket, cow_http2:push_promise(
  628. StreamID, PromisedStreamID, HeaderBlock)),
  629. headers_frame(State0#state{http2_machine=HTTP2Machine},
  630. PromisedStreamID, fin, Headers, PseudoHeaders, 0);
  631. {error, no_push} ->
  632. State0
  633. end,
  634. commands(State, StreamID, Tail);
  635. %% Read the request body.
  636. commands(State0=#state{flow=Flow, streams=Streams}, StreamID, [{flow, Size}|Tail]) ->
  637. #{StreamID := Stream=#stream{flow=StreamFlow}} = Streams,
  638. State = update_window(State0#state{flow=Flow + Size,
  639. streams=Streams#{StreamID => Stream#stream{flow=StreamFlow + Size}}},
  640. StreamID),
  641. commands(State, StreamID, Tail);
  642. %% Supervise a child process.
  643. commands(State=#state{children=Children}, StreamID, [{spawn, Pid, Shutdown}|Tail]) ->
  644. commands(State#state{children=cowboy_children:up(Children, Pid, StreamID, Shutdown)},
  645. StreamID, Tail);
  646. %% Error handling.
  647. commands(State, StreamID, [Error = {internal_error, _, _}|_Tail]) ->
  648. %% @todo Do we want to run the commands after an internal_error?
  649. %% @todo Do we even allow commands after?
  650. %% @todo Only reset when the stream still exists.
  651. reset_stream(State, StreamID, Error);
  652. %% Upgrade to HTTP/2. This is triggered by cowboy_http2 itself.
  653. commands(State=#state{socket=Socket, transport=Transport, http2_status=upgrade},
  654. StreamID, [{switch_protocol, Headers, ?MODULE, _}|Tail]) ->
  655. %% @todo This 101 response needs to be passed through stream handlers.
  656. Transport:send(Socket, cow_http:response(101, 'HTTP/1.1', maps:to_list(Headers))),
  657. commands(State, StreamID, Tail);
  658. %% Use a different protocol within the stream (CONNECT :protocol).
  659. %% @todo Make sure we error out when the feature is disabled.
  660. commands(State0, StreamID, [{switch_protocol, Headers, _Mod, _ModState}|Tail]) ->
  661. State = info(State0, StreamID, {headers, 200, Headers}),
  662. commands(State, StreamID, Tail);
  663. %% Set options dynamically.
  664. commands(State, StreamID, [{set_options, _Opts}|Tail]) ->
  665. commands(State, StreamID, Tail);
  666. commands(State, StreamID, [stop|_Tail]) ->
  667. %% @todo Do we want to run the commands after a stop?
  668. %% @todo Do we even allow commands after?
  669. stop_stream(State, StreamID);
  670. %% Log event.
  671. commands(State=#state{opts=Opts}, StreamID, [Log={log, _, _, _}|Tail]) ->
  672. cowboy:log(Log, Opts),
  673. commands(State, StreamID, Tail).
  674. %% Tentatively update the window after the flow was updated.
  675. update_window(State=#state{socket=Socket, transport=Transport,
  676. http2_machine=HTTP2Machine0, flow=Flow, streams=Streams}, StreamID) ->
  677. #{StreamID := #stream{flow=StreamFlow}} = Streams,
  678. {Data1, HTTP2Machine2} = case cow_http2_machine:ensure_window(Flow, HTTP2Machine0) of
  679. ok -> {<<>>, HTTP2Machine0};
  680. {ok, Increment1, HTTP2Machine1} -> {cow_http2:window_update(Increment1), HTTP2Machine1}
  681. end,
  682. {Data2, HTTP2Machine} = case cow_http2_machine:ensure_window(StreamID, StreamFlow, HTTP2Machine2) of
  683. ok -> {<<>>, HTTP2Machine2};
  684. {ok, Increment2, HTTP2Machine3} -> {cow_http2:window_update(StreamID, Increment2), HTTP2Machine3}
  685. end,
  686. case {Data1, Data2} of
  687. {<<>>, <<>>} -> ok;
  688. _ -> Transport:send(Socket, [Data1, Data2])
  689. end,
  690. State#state{http2_machine=HTTP2Machine}.
  691. %% Send the response, trailers or data.
  692. send_response(State0=#state{http2_machine=HTTP2Machine0}, StreamID, StatusCode, Headers, Body) ->
  693. Size = case Body of
  694. {sendfile, _, Bytes, _} -> Bytes;
  695. _ -> iolist_size(Body)
  696. end,
  697. case Size of
  698. 0 ->
  699. State = send_headers(State0, StreamID, fin, StatusCode, Headers),
  700. maybe_terminate_stream(State, StreamID, fin);
  701. _ ->
  702. %% @todo Add a test for HEAD to make sure we don't send the body when
  703. %% returning {response...} from a stream handler (or {headers...} then {data...}).
  704. {ok, _IsFin, HeaderBlock, HTTP2Machine}
  705. = cow_http2_machine:prepare_headers(StreamID, HTTP2Machine0, nofin,
  706. #{status => cow_http:status_to_integer(StatusCode)},
  707. headers_to_list(Headers)),
  708. maybe_send_data(State0#state{http2_machine=HTTP2Machine}, StreamID, fin, Body,
  709. [cow_http2:headers(StreamID, nofin, HeaderBlock)])
  710. end.
  711. send_headers(State=#state{socket=Socket, transport=Transport,
  712. http2_machine=HTTP2Machine0}, StreamID, IsFin0, StatusCode, Headers) ->
  713. {ok, IsFin, HeaderBlock, HTTP2Machine}
  714. = cow_http2_machine:prepare_headers(StreamID, HTTP2Machine0, IsFin0,
  715. #{status => cow_http:status_to_integer(StatusCode)},
  716. headers_to_list(Headers)),
  717. Transport:send(Socket, cow_http2:headers(StreamID, IsFin, HeaderBlock)),
  718. State#state{http2_machine=HTTP2Machine}.
  719. %% The set-cookie header is special; we can only send one cookie per header.
  720. headers_to_list(Headers0=#{<<"set-cookie">> := SetCookies}) ->
  721. Headers = maps:to_list(maps:remove(<<"set-cookie">>, Headers0)),
  722. Headers ++ [{<<"set-cookie">>, Value} || Value <- SetCookies];
  723. headers_to_list(Headers) ->
  724. maps:to_list(Headers).
  725. maybe_send_data(State0=#state{socket=Socket, transport=Transport,
  726. http2_machine=HTTP2Machine0}, StreamID, IsFin, Data0, Prefix) ->
  727. Data = case is_tuple(Data0) of
  728. false -> {data, Data0};
  729. true -> Data0
  730. end,
  731. case cow_http2_machine:send_or_queue_data(StreamID, HTTP2Machine0, IsFin, Data) of
  732. {ok, HTTP2Machine} ->
  733. %% If we have prefix data (like a HEADERS frame) we need to send it
  734. %% even if we do not send any DATA frames.
  735. case Prefix of
  736. [] -> ok;
  737. _ -> Transport:send(Socket, Prefix)
  738. end,
  739. maybe_send_data_alarm(State0#state{http2_machine=HTTP2Machine}, HTTP2Machine0, StreamID);
  740. {send, SendData, HTTP2Machine} ->
  741. State = #state{http2_status=Status, streams=Streams}
  742. = send_data(State0#state{http2_machine=HTTP2Machine}, SendData, Prefix),
  743. %% Terminate the connection if we are closing and all streams have completed.
  744. if
  745. Status =:= closing, Streams =:= #{} ->
  746. terminate(State, {stop, normal, 'The connection is going away.'});
  747. true ->
  748. maybe_send_data_alarm(State, HTTP2Machine0, StreamID)
  749. end
  750. end.
  751. send_data(State0=#state{socket=Socket, transport=Transport, opts=Opts}, SendData, Prefix) ->
  752. {Acc, State} = prepare_data(State0, SendData, [], Prefix),
  753. _ = [case Data of
  754. {sendfile, Offset, Bytes, Path} ->
  755. %% When sendfile is disabled we explicitly use the fallback.
  756. _ = case maps:get(sendfile, Opts, true) of
  757. true -> Transport:sendfile(Socket, Path, Offset, Bytes);
  758. false -> ranch_transport:sendfile(Transport, Socket, Path, Offset, Bytes, [])
  759. end;
  760. _ ->
  761. Transport:send(Socket, Data)
  762. end || Data <- Acc],
  763. State.
  764. prepare_data(State, [], Acc, []) ->
  765. {lists:reverse(Acc), State};
  766. prepare_data(State, [], Acc, Buffer) ->
  767. {lists:reverse([lists:reverse(Buffer)|Acc]), State};
  768. prepare_data(State0, [{StreamID, IsFin, SendData}|Tail], Acc0, Buffer0) ->
  769. {Acc, Buffer, State} = prepare_data(State0, StreamID, IsFin, SendData, Acc0, Buffer0),
  770. prepare_data(State, Tail, Acc, Buffer).
  771. prepare_data(State0, StreamID, IsFin, [], Acc, Buffer) ->
  772. State = maybe_terminate_stream(State0, StreamID, IsFin),
  773. {Acc, Buffer, State};
  774. prepare_data(State0, StreamID, IsFin, [FrameData|Tail], Acc, Buffer) ->
  775. FrameIsFin = case Tail of
  776. [] -> IsFin;
  777. _ -> nofin
  778. end,
  779. case prepare_data_frame(State0, StreamID, FrameIsFin, FrameData) of
  780. {{MoreData, Sendfile}, State} when is_tuple(Sendfile) ->
  781. case Buffer of
  782. [] ->
  783. prepare_data(State, StreamID, IsFin, Tail,
  784. [Sendfile, MoreData|Acc], []);
  785. _ ->
  786. prepare_data(State, StreamID, IsFin, Tail,
  787. [Sendfile, lists:reverse([MoreData|Buffer])|Acc], [])
  788. end;
  789. {MoreData, State} ->
  790. prepare_data(State, StreamID, IsFin, Tail,
  791. Acc, [MoreData|Buffer])
  792. end.
  793. prepare_data_frame(State, StreamID, IsFin, {data, Data}) ->
  794. {cow_http2:data(StreamID, IsFin, Data),
  795. State};
  796. prepare_data_frame(State, StreamID, IsFin, Sendfile={sendfile, _, Bytes, _}) ->
  797. {{cow_http2:data_header(StreamID, IsFin, Bytes), Sendfile},
  798. State};
  799. %% The stream is terminated in cow_http2_machine:prepare_trailers.
  800. prepare_data_frame(State=#state{http2_machine=HTTP2Machine0},
  801. StreamID, nofin, {trailers, Trailers}) ->
  802. {ok, HeaderBlock, HTTP2Machine}
  803. = cow_http2_machine:prepare_trailers(StreamID, HTTP2Machine0, Trailers),
  804. {cow_http2:headers(StreamID, fin, HeaderBlock),
  805. State#state{http2_machine=HTTP2Machine}}.
  806. %% After we have sent or queued data we may need to set or clear an alarm.
  807. %% We do this by comparing the HTTP2Machine buffer state before/after for
  808. %% the relevant streams.
  809. maybe_send_data_alarm(State=#state{opts=Opts, http2_machine=HTTP2Machine}, HTTP2Machine0, StreamID) ->
  810. ConnBufferSizeBefore = cow_http2_machine:get_connection_local_buffer_size(HTTP2Machine0),
  811. ConnBufferSizeAfter = cow_http2_machine:get_connection_local_buffer_size(HTTP2Machine),
  812. {ok, StreamBufferSizeBefore} = cow_http2_machine:get_stream_local_buffer_size(StreamID, HTTP2Machine0),
  813. %% When the stream ends up closed after it finished sending data,
  814. %% we do not want to trigger an alarm. We act as if the buffer
  815. %% size did not change.
  816. StreamBufferSizeAfter = case cow_http2_machine:get_stream_local_buffer_size(StreamID, HTTP2Machine) of
  817. {ok, BSA} -> BSA;
  818. {error, closed} -> StreamBufferSizeBefore
  819. end,
  820. MaxConnBufferSize = maps:get(max_connection_buffer_size, Opts, 16000000),
  821. MaxStreamBufferSize = maps:get(max_stream_buffer_size, Opts, 8000000),
  822. %% I do not want to document these internal events yet. I am not yet
  823. %% convinced it should be {alarm, Name, on|off} and not {internal_event, E}
  824. %% or something else entirely. Though alarms are probably right.
  825. if
  826. ConnBufferSizeBefore >= MaxConnBufferSize, ConnBufferSizeAfter < MaxConnBufferSize ->
  827. connection_alarm(State, connection_buffer_full, off);
  828. ConnBufferSizeBefore < MaxConnBufferSize, ConnBufferSizeAfter >= MaxConnBufferSize ->
  829. connection_alarm(State, connection_buffer_full, on);
  830. StreamBufferSizeBefore >= MaxStreamBufferSize, StreamBufferSizeAfter < MaxStreamBufferSize ->
  831. stream_alarm(State, StreamID, stream_buffer_full, off);
  832. StreamBufferSizeBefore < MaxStreamBufferSize, StreamBufferSizeAfter >= MaxStreamBufferSize ->
  833. stream_alarm(State, StreamID, stream_buffer_full, on);
  834. true ->
  835. State
  836. end.
  837. connection_alarm(State0=#state{streams=Streams}, Name, Value) ->
  838. lists:foldl(fun(StreamID, State) ->
  839. stream_alarm(State, StreamID, Name, Value)
  840. end, State0, maps:keys(Streams)).
  841. stream_alarm(State, StreamID, Name, Value) ->
  842. info(State, StreamID, {alarm, Name, Value}).
  843. %% Terminate a stream or the connection.
  844. %% We may have to cancel streams even if we receive multiple
  845. %% GOAWAY frames as the LastStreamID value may be lower than
  846. %% the one previously received.
  847. goaway(State0=#state{socket=Socket, transport=Transport, http2_machine=HTTP2Machine,
  848. http2_status=Status, streams=Streams0}, {goaway, LastStreamID, Reason, _})
  849. when Status =:= connected; Status =:= closing ->
  850. Streams = goaway_streams(State0, maps:to_list(Streams0), LastStreamID,
  851. {stop, {goaway, Reason}, 'The connection is going away.'}, []),
  852. State = State0#state{streams=maps:from_list(Streams)},
  853. case Status of
  854. connected ->
  855. Transport:send(Socket, cow_http2:goaway(
  856. cow_http2_machine:get_last_streamid(HTTP2Machine),
  857. no_error, <<>>)),
  858. State#state{http2_status=closing};
  859. _ ->
  860. State
  861. end;
  862. %% We terminate the connection immediately if it hasn't fully been initialized.
  863. goaway(State, {goaway, _, Reason, _}) ->
  864. terminate(State, {stop, {goaway, Reason}, 'The connection is going away.'}).
  865. %% Cancel client-initiated streams that are above LastStreamID.
  866. goaway_streams(_, [], _, _, Acc) ->
  867. Acc;
  868. goaway_streams(State, [{StreamID, #stream{state=StreamState}}|Tail], LastStreamID, Reason, Acc)
  869. when StreamID > LastStreamID, (StreamID rem 2) =:= 0 ->
  870. terminate_stream_handler(State, StreamID, Reason, StreamState),
  871. goaway_streams(State, Tail, LastStreamID, Reason, Acc);
  872. goaway_streams(State, [Stream|Tail], LastStreamID, Reason, Acc) ->
  873. goaway_streams(State, Tail, LastStreamID, Reason, [Stream|Acc]).
  874. -spec terminate(#state{}, _) -> no_return().
  875. terminate(undefined, Reason) ->
  876. exit({shutdown, Reason});
  877. terminate(State=#state{socket=Socket, transport=Transport, http2_status=Status,
  878. http2_machine=HTTP2Machine, streams=Streams, children=Children}, Reason)
  879. when Status =:= connected; Status =:= closing ->
  880. %% @todo We might want to optionally send the Reason value
  881. %% as debug data in the GOAWAY frame here. Perhaps more.
  882. case Status of
  883. connected ->
  884. Transport:send(Socket, cow_http2:goaway(
  885. cow_http2_machine:get_last_streamid(HTTP2Machine),
  886. terminate_reason(Reason), <<>>));
  887. %% We already sent the GOAWAY frame.
  888. closing ->
  889. ok
  890. end,
  891. terminate_all_streams(State, maps:to_list(Streams), Reason),
  892. cowboy_children:terminate(Children),
  893. terminate_linger(State),
  894. exit({shutdown, Reason});
  895. terminate(#state{socket=Socket, transport=Transport}, Reason) ->
  896. Transport:close(Socket),
  897. exit({shutdown, Reason}).
  898. terminate_reason({connection_error, Reason, _}) -> Reason;
  899. terminate_reason({stop, _, _}) -> no_error;
  900. terminate_reason({socket_error, _, _}) -> internal_error;
  901. terminate_reason({internal_error, _, _}) -> internal_error.
  902. terminate_all_streams(_, [], _) ->
  903. ok;
  904. terminate_all_streams(State, [{StreamID, #stream{state=StreamState}}|Tail], Reason) ->
  905. terminate_stream_handler(State, StreamID, Reason, StreamState),
  906. terminate_all_streams(State, Tail, Reason).
  907. %% This code is copied from cowboy_http.
  908. terminate_linger(State=#state{socket=Socket, transport=Transport, opts=Opts}) ->
  909. case Transport:shutdown(Socket, write) of
  910. ok ->
  911. case maps:get(linger_timeout, Opts, 1000) of
  912. 0 ->
  913. ok;
  914. infinity ->
  915. terminate_linger_before_loop(State, undefined, Transport:messages());
  916. Timeout ->
  917. TimerRef = erlang:start_timer(Timeout, self(), linger_timeout),
  918. terminate_linger_before_loop(State, TimerRef, Transport:messages())
  919. end;
  920. {error, _} ->
  921. ok
  922. end.
  923. terminate_linger_before_loop(State, TimerRef, Messages) ->
  924. %% We may already be in active mode when we do this
  925. %% but it's OK because we are shutting down anyway.
  926. case setopts_active(State) of
  927. ok ->
  928. terminate_linger_loop(State, TimerRef, Messages);
  929. {error, _} ->
  930. ok
  931. end.
  932. terminate_linger_loop(State=#state{socket=Socket}, TimerRef, Messages) ->
  933. receive
  934. {OK, Socket, _} when OK =:= element(1, Messages) ->
  935. terminate_linger_loop(State, TimerRef, Messages);
  936. {Closed, Socket} when Closed =:= element(2, Messages) ->
  937. ok;
  938. {Error, Socket, _} when Error =:= element(3, Messages) ->
  939. ok;
  940. {Passive, Socket} when Passive =:= tcp_passive; Passive =:= ssl_passive ->
  941. terminate_linger_before_loop(State, TimerRef, Messages);
  942. {timeout, TimerRef, linger_timeout} ->
  943. ok;
  944. _ ->
  945. terminate_linger_loop(State, TimerRef, Messages)
  946. end.
  947. %% @todo Don't send an RST_STREAM if one was already sent.
  948. reset_stream(State0=#state{socket=Socket, transport=Transport,
  949. http2_machine=HTTP2Machine0}, StreamID, Error) ->
  950. Reason = case Error of
  951. {internal_error, _, _} -> internal_error;
  952. {stream_error, Reason0, _} -> Reason0
  953. end,
  954. Transport:send(Socket, cow_http2:rst_stream(StreamID, Reason)),
  955. State1 = case cow_http2_machine:reset_stream(StreamID, HTTP2Machine0) of
  956. {ok, HTTP2Machine} ->
  957. terminate_stream(State0#state{http2_machine=HTTP2Machine}, StreamID, Error);
  958. {error, not_found} ->
  959. terminate_stream(State0, StreamID, Error)
  960. end,
  961. case reset_rate(State1) of
  962. {ok, State} ->
  963. State;
  964. error ->
  965. terminate(State1, {connection_error, enhance_your_calm,
  966. 'Stream reset rate larger than configuration allows. Flood? (CVE-2019-9514)'})
  967. end.
  968. reset_rate(State0=#state{opts=Opts, reset_rate_num=Num0, reset_rate_time=Time}) ->
  969. case Num0 - 1 of
  970. 0 ->
  971. CurrentTime = erlang:monotonic_time(millisecond),
  972. if
  973. CurrentTime < Time ->
  974. error;
  975. true ->
  976. %% When the option has a period of infinity we cannot reach this clause.
  977. {Num, Period} = maps:get(max_reset_stream_rate, Opts, {10, 10000}),
  978. {ok, State0#state{reset_rate_num=Num, reset_rate_time=CurrentTime + Period}}
  979. end;
  980. Num ->
  981. {ok, State0#state{reset_rate_num=Num}}
  982. end.
  983. stop_stream(State=#state{http2_machine=HTTP2Machine}, StreamID) ->
  984. case cow_http2_machine:get_stream_local_state(StreamID, HTTP2Machine) of
  985. %% When the stream terminates normally (without sending RST_STREAM)
  986. %% and no response was sent, we need to send a proper response back to the client.
  987. %% We delay the termination of the stream until the response is fully sent.
  988. {ok, idle, _} ->
  989. info(stopping(State, StreamID), StreamID, {response, 204, #{}, <<>>});
  990. %% When a response was sent but not terminated, we need to close the stream.
  991. %% We delay the termination of the stream until the response is fully sent.
  992. {ok, nofin, fin} ->
  993. stopping(State, StreamID);
  994. %% We only send a final DATA frame if there isn't one queued yet.
  995. {ok, nofin, _} ->
  996. info(stopping(State, StreamID), StreamID, {data, fin, <<>>});
  997. %% When a response was sent fully we can terminate the stream,
  998. %% regardless of the stream being in half-closed or closed state.
  999. _ ->
  1000. terminate_stream(State, StreamID)
  1001. end.
  1002. stopping(State=#state{streams=Streams}, StreamID) ->
  1003. #{StreamID := Stream} = Streams,
  1004. State#state{streams=Streams#{StreamID => Stream#stream{status=stopping}}}.
  1005. %% If we finished sending data and the stream is stopping, terminate it.
  1006. maybe_terminate_stream(State=#state{streams=Streams}, StreamID, fin) ->
  1007. case Streams of
  1008. #{StreamID := #stream{status=stopping}} ->
  1009. terminate_stream(State, StreamID);
  1010. _ ->
  1011. State
  1012. end;
  1013. maybe_terminate_stream(State, _, _) ->
  1014. State.
  1015. %% When the stream stops normally without reading the request
  1016. %% body fully we need to tell the client to stop sending it.
  1017. %% We do this by sending an RST_STREAM with reason NO_ERROR. (RFC7540 8.1.0)
  1018. terminate_stream(State0=#state{socket=Socket, transport=Transport,
  1019. http2_machine=HTTP2Machine0}, StreamID) ->
  1020. State = case cow_http2_machine:get_stream_local_state(StreamID, HTTP2Machine0) of
  1021. {ok, fin, _} ->
  1022. Transport:send(Socket, cow_http2:rst_stream(StreamID, no_error)),
  1023. {ok, HTTP2Machine} = cow_http2_machine:reset_stream(StreamID, HTTP2Machine0),
  1024. State0#state{http2_machine=HTTP2Machine};
  1025. {error, closed} ->
  1026. State0
  1027. end,
  1028. terminate_stream(State, StreamID, normal).
  1029. %% We remove the stream flow from the connection flow. Any further
  1030. %% data received for this stream is therefore fully contained within
  1031. %% the extra window we allocated for this stream.
  1032. terminate_stream(State=#state{flow=Flow, streams=Streams0, children=Children0}, StreamID, Reason) ->
  1033. case maps:take(StreamID, Streams0) of
  1034. {#stream{flow=StreamFlow, state=StreamState}, Streams} ->
  1035. terminate_stream_handler(State, StreamID, Reason, StreamState),
  1036. Children = cowboy_children:shutdown(Children0, StreamID),
  1037. State#state{flow=Flow - StreamFlow, streams=Streams, children=Children};
  1038. error ->
  1039. State
  1040. end.
  1041. terminate_stream_handler(#state{opts=Opts}, StreamID, Reason, StreamState) ->
  1042. try
  1043. cowboy_stream:terminate(StreamID, Reason, StreamState)
  1044. catch Class:Exception:Stacktrace ->
  1045. cowboy:log(cowboy_stream:make_error_log(terminate,
  1046. [StreamID, Reason, StreamState],
  1047. Class, Exception, Stacktrace), Opts)
  1048. end.
  1049. %% System callbacks.
  1050. -spec system_continue(_, _, {#state{}, binary()}) -> ok.
  1051. system_continue(_, _, {State, Buffer}) ->
  1052. loop(State, Buffer).
  1053. -spec system_terminate(any(), _, _, {#state{}, binary()}) -> no_return().
  1054. system_terminate(Reason, _, _, {State, _}) ->
  1055. %% @todo Graceful shutdown here as well?
  1056. terminate(State, {stop, {exit, Reason}, 'sys:terminate/2,3 was called.'}).
  1057. -spec system_code_change(Misc, _, _, _) -> {ok, Misc} when Misc::{#state{}, binary()}.
  1058. system_code_change(Misc, _, _, _) ->
  1059. {ok, Misc}.