cowboy_http2.erl 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  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/5]).
  16. -export([init/9]).
  17. -export([init/11]).
  18. -export([system_continue/3]).
  19. -export([system_terminate/4]).
  20. -export([system_code_change/4]).
  21. -type opts() :: #{
  22. connection_type => worker | supervisor,
  23. enable_connect_protocol => boolean(),
  24. env => cowboy_middleware:env(),
  25. inactivity_timeout => timeout(),
  26. max_concurrent_streams => non_neg_integer() | infinity,
  27. max_decode_table_size => non_neg_integer(),
  28. max_encode_table_size => non_neg_integer(),
  29. middlewares => [module()],
  30. preface_timeout => timeout(),
  31. shutdown_timeout => timeout(),
  32. stream_handlers => [module()]
  33. }.
  34. -export_type([opts/0]).
  35. -record(stream, {
  36. id = undefined :: cowboy_stream:streamid(),
  37. %% Stream handlers and their state.
  38. state = undefined :: {module(), any()} | flush,
  39. %% Request method.
  40. method = undefined :: binary(),
  41. %% Whether we finished sending data.
  42. local = idle :: idle | upgrade | cowboy_stream:fin() | flush,
  43. %% Local flow control window (how much we can send).
  44. local_window :: integer(),
  45. %% Buffered data waiting for the flow control window to increase.
  46. local_buffer = queue:new() :: queue:queue(
  47. {cowboy_stream:fin(), non_neg_integer(), iolist()
  48. | {sendfile, non_neg_integer(), pos_integer(), file:name_all()}}),
  49. local_buffer_size = 0 :: non_neg_integer(),
  50. local_trailers = undefined :: undefined | cowboy:http_headers(),
  51. %% Whether we finished receiving data.
  52. remote = nofin :: cowboy_stream:fin(),
  53. %% Remote flow control window (how much we accept to receive).
  54. remote_window :: integer(),
  55. %% Unparsed te header. Used to know if we can send trailers.
  56. te :: undefined | binary()
  57. }).
  58. -type stream() :: #stream{}.
  59. -record(state, {
  60. parent = undefined :: pid(),
  61. ref :: ranch:ref(),
  62. socket = undefined :: inet:socket(),
  63. transport :: module(),
  64. opts = #{} :: opts(),
  65. %% Remote address and port for the connection.
  66. peer = undefined :: {inet:ip_address(), inet:port_number()},
  67. %% Local address and port for the connection.
  68. sock = undefined :: {inet:ip_address(), inet:port_number()},
  69. %% Client certificate (TLS only).
  70. cert :: undefined | binary(),
  71. %% Settings are separate for each endpoint. In addition, settings
  72. %% must be acknowledged before they can be expected to be applied.
  73. %%
  74. %% @todo Since the ack is required, we must timeout if we don't receive it.
  75. %% @todo I haven't put as much thought as I should have on this,
  76. %% the final settings handling will be very different.
  77. local_settings = #{
  78. % header_table_size => 4096,
  79. % enable_push => false, %% We are the server. Push is never enabled.
  80. % max_concurrent_streams => infinity,
  81. initial_window_size => 65535,
  82. max_frame_size => 16384
  83. % max_header_list_size => infinity
  84. } :: map(),
  85. %% @todo We need a TimerRef to do SETTINGS_TIMEOUT errors.
  86. %% We need to be careful there. It's well possible that we send
  87. %% two SETTINGS frames before we receive a SETTINGS ack.
  88. next_settings = undefined :: undefined | map(),
  89. remote_settings = #{
  90. initial_window_size => 65535
  91. } :: map(),
  92. %% Connection-wide flow control window.
  93. local_window = 65535 :: integer(), %% How much we can send.
  94. remote_window = 65535 :: integer(), %% How much we accept to receive.
  95. %% Stream identifiers.
  96. client_streamid = 0 :: non_neg_integer(),
  97. server_streamid = 2 :: pos_integer(),
  98. %% Currently active HTTP/2 streams. Streams may be initiated either
  99. %% by the client or by the server through PUSH_PROMISE frames.
  100. streams = [] :: [stream()],
  101. %% HTTP/2 streams that have been reset recently. We are expected
  102. %% to keep receiving additional frames after sending an RST_STREAM.
  103. lingering_streams = [] :: [cowboy_stream:streamid()],
  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. %% The client starts by sending a sequence of bytes as a preface,
  108. %% followed by a potentially empty SETTINGS frame. Then the connection
  109. %% is established and continues normally. An exception is when a HEADERS
  110. %% frame is sent followed by CONTINUATION frames: no other frame can be
  111. %% sent in between.
  112. parse_state = undefined :: {preface, sequence, undefined | reference()}
  113. | {preface, settings, reference()}
  114. | normal
  115. | {continuation, cowboy_stream:streamid(), cowboy_stream:fin(), binary()},
  116. %% HPACK decoding and encoding state.
  117. decode_state = cow_hpack:init() :: cow_hpack:state(),
  118. encode_state = cow_hpack:init() :: cow_hpack:state()
  119. }).
  120. -spec init(pid(), ranch:ref(), inet:socket(), module(), cowboy:opts()) -> ok.
  121. init(Parent, Ref, Socket, Transport, Opts) ->
  122. Peer0 = Transport:peername(Socket),
  123. Sock0 = Transport:sockname(Socket),
  124. Cert1 = case Transport:name() of
  125. ssl ->
  126. case ssl:peercert(Socket) of
  127. {error, no_peercert} ->
  128. {ok, undefined};
  129. Cert0 ->
  130. Cert0
  131. end;
  132. _ ->
  133. {ok, undefined}
  134. end,
  135. case {Peer0, Sock0, Cert1} of
  136. {{ok, Peer}, {ok, Sock}, {ok, Cert}} ->
  137. init(Parent, Ref, Socket, Transport, Opts, Peer, Sock, Cert, <<>>);
  138. {{error, Reason}, _, _} ->
  139. terminate(undefined, {socket_error, Reason,
  140. 'A socket error occurred when retrieving the peer name.'});
  141. {_, {error, Reason}, _} ->
  142. terminate(undefined, {socket_error, Reason,
  143. 'A socket error occurred when retrieving the sock name.'});
  144. {_, _, {error, Reason}} ->
  145. terminate(undefined, {socket_error, Reason,
  146. 'A socket error occurred when retrieving the client TLS certificate.'})
  147. end.
  148. -spec init(pid(), ranch:ref(), inet:socket(), module(), cowboy:opts(),
  149. {inet:ip_address(), inet:port_number()}, {inet:ip_address(), inet:port_number()},
  150. binary() | undefined, binary()) -> ok.
  151. init(Parent, Ref, Socket, Transport, Opts, Peer, Sock, Cert, Buffer) ->
  152. State0 = #state{parent=Parent, ref=Ref, socket=Socket,
  153. transport=Transport, opts=Opts, peer=Peer, sock=Sock, cert=Cert,
  154. parse_state={preface, sequence, preface_timeout(Opts)}},
  155. State = settings_init(State0, Opts),
  156. preface(State),
  157. case Buffer of
  158. <<>> -> before_loop(State, Buffer);
  159. _ -> parse(State, Buffer)
  160. end.
  161. %% @todo Add an argument for the request body.
  162. -spec init(pid(), ranch:ref(), inet:socket(), module(), cowboy:opts(),
  163. {inet:ip_address(), inet:port_number()}, {inet:ip_address(), inet:port_number()},
  164. binary() | undefined, binary(), map() | undefined, cowboy_req:req()) -> ok.
  165. init(Parent, Ref, Socket, Transport, Opts, Peer, Sock, Cert, Buffer, _Settings, Req) ->
  166. State0 = #state{parent=Parent, ref=Ref, socket=Socket,
  167. transport=Transport, opts=Opts, peer=Peer, sock=Sock, cert=Cert,
  168. parse_state={preface, sequence, preface_timeout(Opts)}},
  169. %% @todo Apply settings.
  170. %% StreamID from HTTP/1.1 Upgrade requests is always 1.
  171. %% The stream is always in the half-closed (remote) state.
  172. State1 = stream_handler_init(State0, 1, fin, upgrade, Req),
  173. %% We assume that the upgrade will be applied. A stream handler
  174. %% must not prevent the normal operations of the server.
  175. State2 = info(State1, 1, {switch_protocol, #{
  176. <<"connection">> => <<"Upgrade">>,
  177. <<"upgrade">> => <<"h2c">>
  178. }, ?MODULE, undefined}), %% @todo undefined or #{}?
  179. State = settings_init(State2, Opts),
  180. preface(State),
  181. case Buffer of
  182. <<>> -> before_loop(State, Buffer);
  183. _ -> parse(State, Buffer)
  184. end.
  185. settings_init(State, Opts) ->
  186. S0 = setting_from_opt(#{}, Opts, max_decode_table_size,
  187. header_table_size, 4096),
  188. S1 = setting_from_opt(S0, Opts, max_concurrent_streams,
  189. max_concurrent_streams, infinity),
  190. %% @todo initial_window_size
  191. %% @todo max_frame_size
  192. %% @todo max_header_list_size
  193. Settings = setting_from_opt(S1, Opts, enable_connect_protocol,
  194. enable_connect_protocol, false),
  195. State#state{next_settings=Settings}.
  196. setting_from_opt(Settings, Opts, OptName, SettingName, Default) ->
  197. case maps:get(OptName, Opts, Default) of
  198. Default -> Settings;
  199. Value -> Settings#{SettingName => Value}
  200. end.
  201. preface(#state{socket=Socket, transport=Transport, next_settings=Settings}) ->
  202. %% We send next_settings and use defaults until we get a ack.
  203. Transport:send(Socket, cow_http2:settings(Settings)).
  204. preface_timeout(Opts) ->
  205. case maps:get(preface_timeout, Opts, 5000) of
  206. infinity -> undefined;
  207. PrefaceTimeout -> erlang:start_timer(PrefaceTimeout, self(), preface_timeout)
  208. end.
  209. %% @todo Add the timeout for last time since we heard of connection.
  210. before_loop(State, Buffer) ->
  211. loop(State, Buffer).
  212. loop(State=#state{parent=Parent, socket=Socket, transport=Transport,
  213. opts=Opts, children=Children, parse_state=PS}, Buffer) ->
  214. Transport:setopts(Socket, [{active, once}]),
  215. {OK, Closed, Error} = Transport:messages(),
  216. InactivityTimeout = maps:get(inactivity_timeout, Opts, 300000),
  217. receive
  218. %% Socket messages.
  219. {OK, Socket, Data} ->
  220. parse(State, << Buffer/binary, Data/binary >>);
  221. {Closed, Socket} ->
  222. terminate(State, {socket_error, closed, 'The socket has been closed.'});
  223. {Error, Socket, Reason} ->
  224. terminate(State, {socket_error, Reason, 'An error has occurred on the socket.'});
  225. %% System messages.
  226. {'EXIT', Parent, Reason} ->
  227. %% @todo We should exit gracefully.
  228. exit(Reason);
  229. {system, From, Request} ->
  230. sys:handle_system_msg(Request, From, Parent, ?MODULE, [], {State, Buffer});
  231. %% Timeouts.
  232. {timeout, Ref, {shutdown, Pid}} ->
  233. cowboy_children:shutdown_timeout(Children, Ref, Pid),
  234. loop(State, Buffer);
  235. {timeout, TRef, preface_timeout} ->
  236. case PS of
  237. {preface, _, TRef} ->
  238. terminate(State, {connection_error, protocol_error,
  239. 'The preface was not received in a reasonable amount of time.'});
  240. _ ->
  241. loop(State, Buffer)
  242. end;
  243. %% Messages pertaining to a stream.
  244. {{Pid, StreamID}, Msg} when Pid =:= self() ->
  245. loop(info(State, StreamID, Msg), Buffer);
  246. %% Exit signal from children.
  247. Msg = {'EXIT', Pid, _} ->
  248. loop(down(State, Pid, Msg), Buffer);
  249. %% Calls from supervisor module.
  250. {'$gen_call', From, Call} ->
  251. cowboy_children:handle_supervisor_call(Call, From, Children, ?MODULE),
  252. loop(State, Buffer);
  253. Msg ->
  254. error_logger:error_msg("Received stray message ~p.", [Msg]),
  255. loop(State, Buffer)
  256. after InactivityTimeout ->
  257. terminate(State, {internal_error, timeout, 'No message or data received before timeout.'})
  258. end.
  259. parse(State=#state{socket=Socket, transport=Transport, parse_state={preface, sequence, TRef}}, Data) ->
  260. case Data of
  261. << "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", Rest/bits >> ->
  262. parse(State#state{parse_state={preface, settings, TRef}}, Rest);
  263. _ when byte_size(Data) >= 24 ->
  264. Transport:close(Socket),
  265. exit({shutdown, {connection_error, protocol_error,
  266. 'The connection preface was invalid. (RFC7540 3.5)'}});
  267. _ ->
  268. Len = byte_size(Data),
  269. << Preface:Len/binary, _/bits >> = <<"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n">>,
  270. case Data of
  271. Preface ->
  272. before_loop(State, Data);
  273. _ ->
  274. Transport:close(Socket),
  275. exit({shutdown, {connection_error, protocol_error,
  276. 'The connection preface was invalid. (RFC7540 3.5)'}})
  277. end
  278. end;
  279. %% @todo Perhaps instead of just more we can have {more, Len} to avoid all the checks.
  280. parse(State=#state{local_settings=#{max_frame_size := MaxFrameSize},
  281. parse_state=ParseState}, Data) ->
  282. case cow_http2:parse(Data, MaxFrameSize) of
  283. {ok, Frame, Rest} ->
  284. case ParseState of
  285. normal ->
  286. parse(frame(State, Frame), Rest);
  287. {preface, settings, TRef} ->
  288. parse_settings_preface(State, Frame, Rest, TRef);
  289. {continuation, _, _, _} ->
  290. parse(continuation_frame(State, Frame), Rest)
  291. end;
  292. {ignore, _} when element(1, ParseState) =:= continuation ->
  293. terminate(State, {connection_error, protocol_error,
  294. 'An invalid frame was received in the middle of a header block. (RFC7540 6.2)'});
  295. {ignore, Rest} ->
  296. parse(State, Rest);
  297. {stream_error, StreamID, Reason, Human, Rest} ->
  298. parse(stream_reset(State, StreamID, {stream_error, Reason, Human}), Rest);
  299. Error = {connection_error, _, _} ->
  300. terminate(State, Error);
  301. more ->
  302. before_loop(State, Data)
  303. end.
  304. parse_settings_preface(State, Frame={settings, _}, Rest, TRef) ->
  305. ok = case TRef of
  306. undefined -> ok;
  307. _ -> erlang:cancel_timer(TRef, [{async, true}, {info, false}])
  308. end,
  309. parse(frame(State#state{parse_state=normal}, Frame), Rest);
  310. parse_settings_preface(State, _, _, _) ->
  311. terminate(State, {connection_error, protocol_error,
  312. 'The preface sequence must be followed by a SETTINGS frame. (RFC7540 3.5)'}).
  313. %% @todo When we get a 'fin' we need to check if the stream had a 'fin' sent back
  314. %% and terminate the stream if this is the end of it.
  315. %% DATA frame.
  316. frame(State=#state{client_streamid=LastStreamID}, {data, StreamID, _, _})
  317. when StreamID > LastStreamID ->
  318. terminate(State, {connection_error, protocol_error,
  319. 'DATA frame received on a stream in idle state. (RFC7540 5.1)'});
  320. frame(State0=#state{remote_window=ConnWindow, streams=Streams, lingering_streams=Lingering},
  321. {data, StreamID, IsFin, Data}) ->
  322. DataLen = byte_size(Data),
  323. State = State0#state{remote_window=ConnWindow - DataLen},
  324. case lists:keyfind(StreamID, #stream.id, Streams) of
  325. Stream = #stream{state=flush, remote=nofin, remote_window=StreamWindow} ->
  326. after_commands(State, Stream#stream{remote=IsFin, remote_window=StreamWindow - DataLen});
  327. Stream = #stream{state=StreamState0, remote=nofin, remote_window=StreamWindow} ->
  328. try cowboy_stream:data(StreamID, IsFin, Data, StreamState0) of
  329. {Commands, StreamState} ->
  330. commands(State, Stream#stream{state=StreamState, remote=IsFin,
  331. remote_window=StreamWindow - DataLen}, Commands)
  332. catch Class:Exception ->
  333. cowboy_stream:report_error(data,
  334. [StreamID, IsFin, Data, StreamState0],
  335. Class, Exception, erlang:get_stacktrace()),
  336. stream_reset(State, StreamID, {internal_error, {Class, Exception},
  337. 'Unhandled exception in cowboy_stream:data/4.'})
  338. end;
  339. #stream{remote=fin} ->
  340. stream_reset(State, StreamID, {stream_error, stream_closed,
  341. 'DATA frame received for a half-closed (remote) stream. (RFC7540 5.1)'});
  342. false ->
  343. %% After we send an RST_STREAM frame and terminate a stream,
  344. %% the client still might be sending us some more frames
  345. %% until it can process this RST_STREAM. We therefore ignore
  346. %% DATA frames received for such lingering streams.
  347. case lists:member(StreamID, Lingering) of
  348. true ->
  349. State0;
  350. false ->
  351. terminate(State, {connection_error, stream_closed,
  352. 'DATA frame received for a closed stream. (RFC7540 5.1)'})
  353. end
  354. end;
  355. %% HEADERS frame with invalid even-numbered streamid.
  356. frame(State, {headers, StreamID, _, _, _}) when StreamID rem 2 =:= 0 ->
  357. terminate(State, {connection_error, protocol_error,
  358. 'HEADERS frame received with even-numbered streamid. (RFC7540 5.1.1)'});
  359. %% HEADERS frame received on (half-)closed stream.
  360. %%
  361. %% We always close the connection here to avoid having to decode
  362. %% the headers to not waste resources on non-compliant clients.
  363. frame(State=#state{client_streamid=LastStreamID}, {headers, StreamID, _, _, _})
  364. when StreamID =< LastStreamID ->
  365. terminate(State, {connection_error, stream_closed,
  366. 'HEADERS frame received on a stream in closed or half-closed state. (RFC7540 5.1)'});
  367. %% Single HEADERS frame headers block.
  368. frame(State, {headers, StreamID, IsFin, head_fin, HeaderBlock}) ->
  369. %% @todo We probably need to validate StreamID here and in 4 next clauses.
  370. stream_decode_init(State, StreamID, IsFin, HeaderBlock);
  371. %% HEADERS frame starting a headers block. Enter continuation mode.
  372. frame(State, {headers, StreamID, IsFin, head_nofin, HeaderBlockFragment}) ->
  373. State#state{parse_state={continuation, StreamID, IsFin, HeaderBlockFragment}};
  374. %% Single HEADERS frame headers block with priority.
  375. frame(State, {headers, StreamID, IsFin, head_fin,
  376. _IsExclusive, _DepStreamID, _Weight, HeaderBlock}) ->
  377. %% @todo Handle priority.
  378. stream_decode_init(State, StreamID, IsFin, HeaderBlock);
  379. %% HEADERS frame starting a headers block. Enter continuation mode.
  380. frame(State, {headers, StreamID, IsFin, head_nofin,
  381. _IsExclusive, _DepStreamID, _Weight, HeaderBlockFragment}) ->
  382. %% @todo Handle priority.
  383. State#state{parse_state={continuation, StreamID, IsFin, HeaderBlockFragment}};
  384. %% PRIORITY frame.
  385. frame(State, {priority, _StreamID, _IsExclusive, _DepStreamID, _Weight}) ->
  386. %% @todo Validate StreamID?
  387. %% @todo Handle priority.
  388. State;
  389. %% RST_STREAM frame.
  390. frame(State=#state{client_streamid=LastStreamID}, {rst_stream, StreamID, _})
  391. when StreamID > LastStreamID ->
  392. terminate(State, {connection_error, protocol_error,
  393. 'RST_STREAM frame received on a stream in idle state. (RFC7540 5.1)'});
  394. frame(State, {rst_stream, StreamID, Reason}) ->
  395. stream_terminate(State, StreamID, {stream_error, Reason, 'Stream reset requested by client.'});
  396. %% SETTINGS frame.
  397. frame(State0=#state{socket=Socket, transport=Transport, opts=Opts,
  398. remote_settings=Settings0}, {settings, Settings}) ->
  399. Transport:send(Socket, cow_http2:settings_ack()),
  400. State1 = State0#state{remote_settings=maps:merge(Settings0, Settings)},
  401. maps:fold(fun
  402. (header_table_size, NewSize, State=#state{encode_state=EncodeState0}) ->
  403. MaxSize = maps:get(max_encode_table_size, Opts, 4096),
  404. EncodeState = cow_hpack:set_max_size(min(NewSize, MaxSize), EncodeState0),
  405. State#state{encode_state=EncodeState};
  406. (initial_window_size, NewWindowSize, State) ->
  407. OldWindowSize = maps:get(initial_window_size, Settings0, 65535),
  408. update_stream_windows(State, NewWindowSize - OldWindowSize);
  409. (_, _, State) ->
  410. State
  411. end, State1, Settings);
  412. %% Ack for a previously sent SETTINGS frame.
  413. frame(State0=#state{local_settings=Local0, next_settings=NextSettings}, settings_ack) ->
  414. Local = maps:merge(Local0, NextSettings),
  415. State1 = State0#state{local_settings=Local, next_settings=#{}},
  416. maps:fold(fun
  417. (header_table_size, MaxSize, State=#state{decode_state=DecodeState0}) ->
  418. DecodeState = cow_hpack:set_max_size(MaxSize, DecodeState0),
  419. State#state{decode_state=DecodeState};
  420. (_, _, State) ->
  421. State
  422. end, State1, NextSettings);
  423. %% Unexpected PUSH_PROMISE frame.
  424. frame(State, {push_promise, _, _, _, _}) ->
  425. terminate(State, {connection_error, protocol_error,
  426. 'PUSH_PROMISE frames MUST only be sent on a peer-initiated stream. (RFC7540 6.6)'});
  427. %% PING frame.
  428. frame(State=#state{socket=Socket, transport=Transport}, {ping, Opaque}) ->
  429. Transport:send(Socket, cow_http2:ping_ack(Opaque)),
  430. State;
  431. %% Ack for a previously sent PING frame.
  432. %%
  433. %% @todo Might want to check contents but probably a waste of time.
  434. frame(State, {ping_ack, _Opaque}) ->
  435. State;
  436. %% GOAWAY frame.
  437. frame(State, Frame={goaway, _, _, _}) ->
  438. terminate(State, {stop, Frame, 'Client is going away.'});
  439. %% Connection-wide WINDOW_UPDATE frame.
  440. frame(State=#state{local_window=ConnWindow}, {window_update, Increment})
  441. when ConnWindow + Increment > 16#7fffffff ->
  442. terminate(State, {connection_error, flow_control_error,
  443. 'The flow control window must not be greater than 2^31-1. (RFC7540 6.9.1)'});
  444. frame(State=#state{local_window=ConnWindow}, {window_update, Increment}) ->
  445. send_data(State#state{local_window=ConnWindow + Increment});
  446. %% Stream-specific WINDOW_UPDATE frame.
  447. frame(State=#state{client_streamid=LastStreamID}, {window_update, StreamID, _})
  448. when StreamID > LastStreamID ->
  449. terminate(State, {connection_error, protocol_error,
  450. 'WINDOW_UPDATE frame received on a stream in idle state. (RFC7540 5.1)'});
  451. frame(State0=#state{streams=Streams0}, {window_update, StreamID, Increment}) ->
  452. case lists:keyfind(StreamID, #stream.id, Streams0) of
  453. #stream{local_window=StreamWindow} when StreamWindow + Increment > 16#7fffffff ->
  454. stream_reset(State0, StreamID, {stream_error, flow_control_error,
  455. 'The flow control window must not be greater than 2^31-1. (RFC7540 6.9.1)'});
  456. Stream0 = #stream{local_window=StreamWindow} ->
  457. {State, Stream} = send_data(State0,
  458. Stream0#stream{local_window=StreamWindow + Increment}),
  459. Streams = lists:keystore(StreamID, #stream.id, Streams0, Stream),
  460. State#state{streams=Streams};
  461. %% @todo We must reject WINDOW_UPDATE frames on RST_STREAM closed streams.
  462. false ->
  463. %% WINDOW_UPDATE frames may be received for a short period of time
  464. %% after a stream is closed. They must be ignored.
  465. State0
  466. end;
  467. %% Unexpected CONTINUATION frame.
  468. frame(State, {continuation, _, _, _}) ->
  469. terminate(State, {connection_error, protocol_error,
  470. 'CONTINUATION frames MUST be preceded by a HEADERS frame. (RFC7540 6.10)'}).
  471. continuation_frame(State=#state{parse_state={continuation, StreamID, IsFin, HeaderBlockFragment0}},
  472. {continuation, StreamID, head_fin, HeaderBlockFragment1}) ->
  473. stream_decode_init(State#state{parse_state=normal}, StreamID, IsFin,
  474. << HeaderBlockFragment0/binary, HeaderBlockFragment1/binary >>);
  475. continuation_frame(State=#state{parse_state={continuation, StreamID, IsFin, HeaderBlockFragment0}},
  476. {continuation, StreamID, head_nofin, HeaderBlockFragment1}) ->
  477. State#state{parse_state={continuation, StreamID, IsFin,
  478. << HeaderBlockFragment0/binary, HeaderBlockFragment1/binary >>}};
  479. continuation_frame(State, _) ->
  480. terminate(State, {connection_error, protocol_error,
  481. 'An invalid frame was received in the middle of a header block. (RFC7540 6.2)'}).
  482. down(State=#state{children=Children0}, Pid, Msg) ->
  483. case cowboy_children:down(Children0, Pid) of
  484. %% The stream was terminated already.
  485. {ok, undefined, Children} ->
  486. State#state{children=Children};
  487. %% The stream is still running.
  488. {ok, StreamID, Children} ->
  489. info(State#state{children=Children}, StreamID, Msg);
  490. %% The process was unknown.
  491. error ->
  492. error_logger:error_msg("Received EXIT signal ~p for unknown process ~p.~n", [Msg, Pid]),
  493. State
  494. end.
  495. info(State=#state{client_streamid=LastStreamID, streams=Streams}, StreamID, Msg) ->
  496. case lists:keyfind(StreamID, #stream.id, Streams) of
  497. #stream{state=flush} ->
  498. error_logger:error_msg("Received message ~p for terminated stream ~p.", [Msg, StreamID]),
  499. State;
  500. Stream = #stream{state=StreamState0} ->
  501. try cowboy_stream:info(StreamID, Msg, StreamState0) of
  502. {Commands, StreamState} ->
  503. commands(State, Stream#stream{state=StreamState}, Commands)
  504. catch Class:Exception ->
  505. cowboy_stream:report_error(info,
  506. [StreamID, Msg, StreamState0],
  507. Class, Exception, erlang:get_stacktrace()),
  508. stream_reset(State, StreamID, {internal_error, {Class, Exception},
  509. 'Unhandled exception in cowboy_stream:info/3.'})
  510. end;
  511. false when StreamID =< LastStreamID ->
  512. %% Streams that were reset by the client or streams that are
  513. %% in the lingering state may still have Erlang messages going
  514. %% around. In these cases we do not want to log anything.
  515. State;
  516. false ->
  517. error_logger:error_msg("Received message ~p for unknown stream ~p.",
  518. [Msg, StreamID]),
  519. State
  520. end.
  521. commands(State, Stream, []) ->
  522. after_commands(State, Stream);
  523. %% Error responses are sent only if a response wasn't sent already.
  524. commands(State, Stream=#stream{local=idle}, [{error_response, StatusCode, Headers, Body}|Tail]) ->
  525. commands(State, Stream, [{response, StatusCode, Headers, Body}|Tail]);
  526. commands(State, Stream, [{error_response, _, _, _}|Tail]) ->
  527. commands(State, Stream, Tail);
  528. %% Send an informational response.
  529. commands(State0, Stream=#stream{local=idle}, [{inform, StatusCode, Headers}|Tail]) ->
  530. State = send_headers(State0, Stream, StatusCode, Headers, nofin),
  531. commands(State, Stream, Tail);
  532. %% Send response headers.
  533. %%
  534. %% @todo Kill the stream if it sent a response when one has already been sent.
  535. %% @todo Keep IsFin in the state.
  536. %% @todo Same two things above apply to DATA, possibly promise too.
  537. commands(State0, Stream0=#stream{local=idle},
  538. [{response, StatusCode, Headers, Body}|Tail]) ->
  539. {State, Stream} = send_response(State0, Stream0, StatusCode, Headers, Body),
  540. commands(State, Stream, Tail);
  541. %% @todo response when local!=idle
  542. %% Send response headers.
  543. commands(State0, Stream=#stream{method=Method, local=idle},
  544. [{headers, StatusCode, Headers}|Tail]) ->
  545. IsFin = case Method of
  546. <<"HEAD">> -> fin;
  547. _ -> nofin
  548. end,
  549. State = send_headers(State0, Stream, StatusCode, Headers, IsFin),
  550. commands(State, Stream#stream{local=IsFin}, Tail);
  551. %% @todo headers when local!=idle
  552. %% Send a response body chunk.
  553. commands(State0, Stream0=#stream{local=nofin}, [{data, IsFin, Data}|Tail]) ->
  554. {State, Stream} = send_data(State0, Stream0, IsFin, Data),
  555. commands(State, Stream, Tail);
  556. %% @todo data when local!=nofin
  557. %% Send trailers.
  558. commands(State0, Stream0=#stream{local=nofin, te=TE0}, [{trailers, Trailers}|Tail]) ->
  559. %% We only accept TE headers containing exactly "trailers" (RFC7540 8.1.2.1).
  560. TE = try cow_http_hd:parse_te(TE0) of
  561. {trailers, []} -> trailers;
  562. _ -> no_trailers
  563. catch _:_ ->
  564. %% If we can't parse the TE header, assume we can't send trailers.
  565. no_trailers
  566. end,
  567. {State, Stream} = case TE of
  568. trailers ->
  569. send_data(State0, Stream0, fin, {trailers, Trailers});
  570. no_trailers ->
  571. send_data(State0, Stream0, fin, <<>>)
  572. end,
  573. commands(State, Stream, Tail);
  574. %% Send a file.
  575. %% @todo Add the sendfile command.
  576. %commands(State0, Stream0=#stream{local=nofin},
  577. % [{sendfile, IsFin, Offset, Bytes, Path}|Tail]) ->
  578. % {State, Stream} = send_data(State0, Stream0, IsFin, {sendfile, Offset, Bytes, Path}),
  579. % commands(State, Stream, Tail);
  580. %% @todo sendfile when local!=nofin
  581. %% Send a push promise.
  582. %%
  583. %% @todo We need to keep track of what promises we made so that we don't
  584. %% end up with an infinite loop of promises.
  585. commands(State0=#state{socket=Socket, transport=Transport, server_streamid=PromisedStreamID,
  586. encode_state=EncodeState0}, Stream=#stream{id=StreamID},
  587. [{push, Method, Scheme, Host, Port, Path, Qs, Headers0}|Tail]) ->
  588. Authority = case {Scheme, Port} of
  589. {<<"http">>, 80} -> Host;
  590. {<<"https">>, 443} -> Host;
  591. _ -> iolist_to_binary([Host, $:, integer_to_binary(Port)])
  592. end,
  593. PathWithQs = iolist_to_binary(case Qs of
  594. <<>> -> Path;
  595. _ -> [Path, $?, Qs]
  596. end),
  597. %% We need to make sure the header value is binary before we can
  598. %% pass it to stream_req_init, as it expects them to be flat.
  599. Headers1 = maps:map(fun(_, V) -> iolist_to_binary(V) end, Headers0),
  600. Headers = Headers1#{
  601. <<":method">> => Method,
  602. <<":scheme">> => Scheme,
  603. <<":authority">> => Authority,
  604. <<":path">> => PathWithQs},
  605. {HeaderBlock, EncodeState} = headers_encode(Headers, EncodeState0),
  606. Transport:send(Socket, cow_http2:push_promise(StreamID, PromisedStreamID, HeaderBlock)),
  607. State = stream_req_init(State0#state{server_streamid=PromisedStreamID + 2,
  608. encode_state=EncodeState}, PromisedStreamID, fin, Headers1, #{
  609. method => Method,
  610. scheme => Scheme,
  611. authority => Authority,
  612. path => PathWithQs
  613. }),
  614. commands(State, Stream, Tail);
  615. commands(State=#state{socket=Socket, transport=Transport, remote_window=ConnWindow},
  616. Stream=#stream{id=StreamID, remote_window=StreamWindow},
  617. [{flow, Size}|Tail]) ->
  618. Transport:send(Socket, [
  619. cow_http2:window_update(Size),
  620. cow_http2:window_update(StreamID, Size)
  621. ]),
  622. commands(State#state{remote_window=ConnWindow + Size},
  623. Stream#stream{remote_window=StreamWindow + Size}, Tail);
  624. %% Supervise a child process.
  625. commands(State=#state{children=Children}, Stream=#stream{id=StreamID},
  626. [{spawn, Pid, Shutdown}|Tail]) ->
  627. commands(State#state{children=cowboy_children:up(Children, Pid, StreamID, Shutdown)},
  628. Stream, Tail);
  629. %% Error handling.
  630. commands(State, Stream=#stream{id=StreamID}, [Error = {internal_error, _, _}|_Tail]) ->
  631. %% @todo Do we want to run the commands after an internal_error?
  632. %% @todo Do we even allow commands after?
  633. %% @todo Only reset when the stream still exists.
  634. stream_reset(after_commands(State, Stream), StreamID, Error);
  635. %% Upgrade to HTTP/2. This is triggered by cowboy_http2 itself.
  636. commands(State=#state{socket=Socket, transport=Transport},
  637. Stream=#stream{local=upgrade}, [{switch_protocol, Headers, ?MODULE, _}|Tail]) ->
  638. Transport:send(Socket, cow_http:response(101, 'HTTP/1.1', maps:to_list(Headers))),
  639. commands(State, Stream#stream{local=idle}, Tail);
  640. %% Use a different protocol within the stream (CONNECT :protocol).
  641. %% @todo Make sure we error out when the feature is disabled.
  642. commands(State0, #stream{id=StreamID}, [{switch_protocol, Headers, _Mod, _ModState}|Tail]) ->
  643. State = #state{streams=Streams} = info(State0, StreamID, {headers, 200, Headers}),
  644. Stream = lists:keyfind(StreamID, #stream.id, Streams),
  645. commands(State, Stream, Tail);
  646. commands(State, Stream=#stream{id=StreamID}, [stop|_Tail]) ->
  647. %% @todo Do we want to run the commands after a stop?
  648. %% @todo Do we even allow commands after?
  649. stream_terminate(after_commands(State, Stream), StreamID, normal).
  650. after_commands(State=#state{streams=Streams0}, Stream=#stream{id=StreamID}) ->
  651. Streams = lists:keystore(StreamID, #stream.id, Streams0, Stream),
  652. State#state{streams=Streams}.
  653. send_response(State0, Stream=#stream{method=Method}, StatusCode, Headers0, Body) ->
  654. if
  655. Method =:= <<"HEAD">>; Body =:= <<>> ->
  656. State = send_headers(State0, Stream, StatusCode, Headers0, fin),
  657. {State, Stream#stream{local=fin}};
  658. true ->
  659. State = send_headers(State0, Stream, StatusCode, Headers0, nofin),
  660. %% send_data works with both sendfile and iolists.
  661. send_data(State, Stream#stream{local=nofin}, fin, Body)
  662. end.
  663. send_headers(State=#state{socket=Socket, transport=Transport, encode_state=EncodeState0},
  664. #stream{id=StreamID}, StatusCode, Headers0, IsFin) ->
  665. Headers = Headers0#{<<":status">> => status(StatusCode)},
  666. {HeaderBlock, EncodeState} = headers_encode(Headers, EncodeState0),
  667. Transport:send(Socket, cow_http2:headers(StreamID, IsFin, HeaderBlock)),
  668. State#state{encode_state=EncodeState}.
  669. status(Status) when is_integer(Status) ->
  670. integer_to_binary(Status);
  671. status(<< H, T, U, _/bits >>) when H >= $1, H =< $9, T >= $0, T =< $9, U >= $0, U =< $9 ->
  672. << H, T, U >>.
  673. %% @todo Should we ever want to implement the PRIORITY mechanism,
  674. %% this would be the place to do it. Right now, we just go over
  675. %% all streams and send what we can until either everything is
  676. %% sent or we run out of space in the window.
  677. send_data(State=#state{streams=Streams}) ->
  678. resume_streams(State, Streams, []).
  679. %% When SETTINGS_INITIAL_WINDOW_SIZE changes we need to update
  680. %% the stream windows for all active streams and perhaps resume
  681. %% sending data.
  682. update_stream_windows(State=#state{streams=Streams0}, Increment) ->
  683. Streams = [
  684. S#stream{local_window=StreamWindow + Increment}
  685. || S=#stream{local_window=StreamWindow} <- Streams0],
  686. resume_streams(State, Streams, []).
  687. resume_streams(State, [], Acc) ->
  688. State#state{streams=lists:reverse(Acc)};
  689. %% While technically we should never get < 0 here, let's be on the safe side.
  690. resume_streams(State=#state{local_window=ConnWindow}, Streams, Acc)
  691. when ConnWindow =< 0 ->
  692. State#state{streams=lists:reverse(Acc, Streams)};
  693. %% We rely on send_data/2 to do all the necessary checks about the stream.
  694. resume_streams(State0, [Stream0|Tail], Acc) ->
  695. {State1, Stream} = send_data(State0, Stream0),
  696. case Stream of
  697. %% We are done flushing, remove the stream.
  698. %% Maybe skip the request body if it was not fully read.
  699. #stream{state=flush, local=fin} ->
  700. State = maybe_skip_body(State1, Stream, normal),
  701. resume_streams(State, Tail, Acc);
  702. %% Keep the stream. Either the stream handler is still running,
  703. %% or we are not finished flushing.
  704. _ ->
  705. resume_streams(State1, Tail, [Stream|Acc])
  706. end.
  707. send_data(State, Stream=#stream{local=Local, local_buffer_size=0, local_trailers=Trailers})
  708. when (Trailers =/= undefined) andalso ((Local =:= idle) orelse (Local =:= nofin)) ->
  709. send_trailers(State, Stream#stream{local_trailers=undefined}, Trailers);
  710. %% @todo We might want to print an error if local=fin.
  711. %%
  712. %% @todo It's possible that the stream terminates. We must remove it.
  713. send_data(State=#state{local_window=ConnWindow},
  714. Stream=#stream{local=IsFin, local_window=StreamWindow, local_buffer_size=BufferSize})
  715. when ConnWindow =< 0; IsFin =:= fin; StreamWindow =< 0; BufferSize =:= 0 ->
  716. {State, Stream};
  717. send_data(State0, Stream0=#stream{local_buffer=Q0, local_buffer_size=BufferSize}) ->
  718. %% We know there is an item in the queue.
  719. {{value, {IsFin, DataSize, Data}}, Q} = queue:out(Q0),
  720. {State, Stream} = send_data(State0,
  721. Stream0#stream{local_buffer=Q, local_buffer_size=BufferSize - DataSize},
  722. IsFin, Data, in_r),
  723. send_data(State, Stream).
  724. send_data(State, Stream, IsFin, Data) ->
  725. send_data(State, Stream, IsFin, Data, in).
  726. %% We can send trailers immediately if the queue is empty, otherwise we queue.
  727. %% We always send trailer frames even if the window is empty.
  728. send_data(State, Stream=#stream{local_buffer_size=0}, fin, {trailers, Trailers}, _) ->
  729. send_trailers(State, Stream, Trailers);
  730. send_data(State, Stream, fin, {trailers, Trailers}, _) ->
  731. {State, Stream#stream{local_trailers=Trailers}};
  732. %% Send data immediately if we can, buffer otherwise.
  733. %% @todo We might want to print an error if local=fin.
  734. send_data(State=#state{local_window=ConnWindow},
  735. Stream=#stream{local_window=StreamWindow}, IsFin, Data, In)
  736. when ConnWindow =< 0; StreamWindow =< 0 ->
  737. {State, queue_data(Stream, IsFin, Data, In)};
  738. send_data(State=#state{socket=Socket, transport=Transport, local_window=ConnWindow},
  739. Stream=#stream{id=StreamID, local_window=StreamWindow}, IsFin, Data, In) ->
  740. MaxFrameSize = 16384, %% @todo Use the real SETTINGS_MAX_FRAME_SIZE set by the client.
  741. MaxSendSize = min(min(ConnWindow, StreamWindow), MaxFrameSize),
  742. case Data of
  743. {sendfile, Offset, Bytes, Path} when Bytes =< MaxSendSize ->
  744. Transport:send(Socket, cow_http2:data_header(StreamID, IsFin, Bytes)),
  745. Transport:sendfile(Socket, Path, Offset, Bytes),
  746. {State#state{local_window=ConnWindow - Bytes},
  747. Stream#stream{local=IsFin, local_window=StreamWindow - Bytes}};
  748. {sendfile, Offset, Bytes, Path} ->
  749. Transport:send(Socket, cow_http2:data_header(StreamID, nofin, MaxSendSize)),
  750. Transport:sendfile(Socket, Path, Offset, MaxSendSize),
  751. send_data(State#state{local_window=ConnWindow - MaxSendSize},
  752. Stream#stream{local_window=StreamWindow - MaxSendSize},
  753. IsFin, {sendfile, Offset + MaxSendSize, Bytes - MaxSendSize, Path}, In);
  754. Iolist0 ->
  755. IolistSize = iolist_size(Iolist0),
  756. if
  757. IolistSize =< MaxSendSize ->
  758. Transport:send(Socket, cow_http2:data(StreamID, IsFin, Iolist0)),
  759. {State#state{local_window=ConnWindow - IolistSize},
  760. Stream#stream{local=IsFin, local_window=StreamWindow - IolistSize}};
  761. true ->
  762. {Iolist, More} = cowboy_iolists:split(MaxSendSize, Iolist0),
  763. Transport:send(Socket, cow_http2:data(StreamID, nofin, Iolist)),
  764. send_data(State#state{local_window=ConnWindow - MaxSendSize},
  765. Stream#stream{local_window=StreamWindow - MaxSendSize},
  766. IsFin, More, In)
  767. end
  768. end.
  769. send_trailers(State=#state{socket=Socket, transport=Transport, encode_state=EncodeState0},
  770. Stream=#stream{id=StreamID}, Trailers) ->
  771. {HeaderBlock, EncodeState} = headers_encode(Trailers, EncodeState0),
  772. Transport:send(Socket, cow_http2:headers(StreamID, fin, HeaderBlock)),
  773. {State#state{encode_state=EncodeState}, Stream#stream{local=fin}}.
  774. queue_data(Stream=#stream{local_buffer=Q0, local_buffer_size=Size0}, IsFin, Data, In) ->
  775. DataSize = case Data of
  776. {sendfile, _, Bytes, _} -> Bytes;
  777. Iolist -> iolist_size(Iolist)
  778. end,
  779. Q = queue:In({IsFin, DataSize, Data}, Q0),
  780. Stream#stream{local_buffer=Q, local_buffer_size=Size0 + DataSize}.
  781. %% The set-cookie header is special; we can only send one cookie per header.
  782. headers_encode(Headers0=#{<<"set-cookie">> := SetCookies}, EncodeState) ->
  783. Headers1 = maps:to_list(maps:remove(<<"set-cookie">>, Headers0)),
  784. Headers = Headers1 ++ [{<<"set-cookie">>, Value} || Value <- SetCookies],
  785. cow_hpack:encode(Headers, EncodeState);
  786. headers_encode(Headers0, EncodeState) ->
  787. Headers = maps:to_list(Headers0),
  788. cow_hpack:encode(Headers, EncodeState).
  789. -spec terminate(#state{}, _) -> no_return().
  790. terminate(undefined, Reason) ->
  791. exit({shutdown, Reason});
  792. terminate(#state{socket=Socket, transport=Transport, parse_state={preface, _, _}}, Reason) ->
  793. Transport:close(Socket),
  794. exit({shutdown, Reason});
  795. terminate(#state{socket=Socket, transport=Transport, client_streamid=LastStreamID,
  796. streams=Streams, children=Children}, Reason) ->
  797. %% @todo We might want to optionally send the Reason value
  798. %% as debug data in the GOAWAY frame here. Perhaps more.
  799. Transport:send(Socket, cow_http2:goaway(LastStreamID, terminate_reason(Reason), <<>>)),
  800. terminate_all_streams(Streams, Reason),
  801. cowboy_children:terminate(Children),
  802. Transport:close(Socket),
  803. exit({shutdown, Reason}).
  804. terminate_reason({connection_error, Reason, _}) -> Reason;
  805. terminate_reason({stop, _, _}) -> no_error;
  806. terminate_reason({socket_error, _, _}) -> internal_error;
  807. terminate_reason({internal_error, _, _}) -> internal_error.
  808. terminate_all_streams([], _) ->
  809. ok;
  810. %% This stream was already terminated and is now just flushing the data out. Skip it.
  811. terminate_all_streams([#stream{state=flush}|Tail], Reason) ->
  812. terminate_all_streams(Tail, Reason);
  813. terminate_all_streams([#stream{id=StreamID, state=StreamState}|Tail], Reason) ->
  814. stream_call_terminate(StreamID, Reason, StreamState),
  815. terminate_all_streams(Tail, Reason).
  816. %% Stream functions.
  817. stream_decode_init(State=#state{decode_state=DecodeState0}, StreamID, IsFin, HeaderBlock) ->
  818. try cow_hpack:decode(HeaderBlock, DecodeState0) of
  819. {Headers, DecodeState} ->
  820. stream_enforce_concurrency_limit(State#state{decode_state=DecodeState},
  821. StreamID, IsFin, Headers)
  822. catch _:_ ->
  823. terminate(State, {connection_error, compression_error,
  824. 'Error while trying to decode HPACK-encoded header block. (RFC7540 4.3)'})
  825. end.
  826. stream_enforce_concurrency_limit(State=#state{opts=Opts, streams=Streams},
  827. StreamID, IsFin, Headers) ->
  828. MaxConcurrentStreams = maps:get(max_concurrent_streams, Opts, infinity),
  829. case length(Streams) < MaxConcurrentStreams of
  830. true ->
  831. stream_pseudo_headers_init(State, StreamID, IsFin, Headers);
  832. false ->
  833. stream_refused(State, StreamID,
  834. 'Maximum number of concurrent streams has been reached. (RFC7540 5.1.2)')
  835. end.
  836. stream_pseudo_headers_init(State=#state{local_settings=LocalSettings},
  837. StreamID, IsFin, Headers0) ->
  838. IsExtendedConnectEnabled = maps:get(enable_connect_protocol, LocalSettings, false),
  839. case pseudo_headers(Headers0, #{}) of
  840. {ok, PseudoHeaders=#{method := <<"CONNECT">>, scheme := _,
  841. authority := _, path := _, protocol := _}, Headers}
  842. when IsExtendedConnectEnabled ->
  843. stream_regular_headers_init(State, StreamID, IsFin, Headers, PseudoHeaders);
  844. {ok, #{method := <<"CONNECT">>, scheme := _,
  845. authority := _, path := _}, _}
  846. when IsExtendedConnectEnabled ->
  847. stream_malformed(State, StreamID,
  848. 'The :protocol pseudo-header MUST be sent with an extended CONNECT. (draft_h2_websockets 4)');
  849. {ok, #{protocol := _}, _} ->
  850. stream_malformed(State, StreamID,
  851. 'The :protocol pseudo-header is only defined for the extended CONNECT. (draft_h2_websockets 4)');
  852. %% @todo Add clause for CONNECT requests (no scheme/path).
  853. {ok, PseudoHeaders=#{method := <<"CONNECT">>}, _} ->
  854. stream_early_error(State, StreamID, IsFin, 501, PseudoHeaders,
  855. 'The CONNECT method is currently not implemented. (RFC7231 4.3.6)');
  856. {ok, PseudoHeaders=#{method := <<"TRACE">>}, _} ->
  857. stream_early_error(State, StreamID, IsFin, 501, PseudoHeaders,
  858. 'The TRACE method is currently not implemented. (RFC7231 4.3.8)');
  859. {ok, PseudoHeaders=#{method := _, scheme := _, authority := _, path := _}, Headers} ->
  860. stream_regular_headers_init(State, StreamID, IsFin, Headers, PseudoHeaders);
  861. {ok, _, _} ->
  862. stream_malformed(State, StreamID,
  863. 'A required pseudo-header was not found. (RFC7540 8.1.2.3)');
  864. {error, HumanReadable} ->
  865. stream_malformed(State, StreamID, HumanReadable)
  866. end.
  867. pseudo_headers([{<<":method">>, _}|_], #{method := _}) ->
  868. {error, 'Multiple :method pseudo-headers were found. (RFC7540 8.1.2.3)'};
  869. pseudo_headers([{<<":method">>, Method}|Tail], PseudoHeaders) ->
  870. pseudo_headers(Tail, PseudoHeaders#{method => Method});
  871. pseudo_headers([{<<":scheme">>, _}|_], #{scheme := _}) ->
  872. {error, 'Multiple :scheme pseudo-headers were found. (RFC7540 8.1.2.3)'};
  873. pseudo_headers([{<<":scheme">>, Scheme}|Tail], PseudoHeaders) ->
  874. pseudo_headers(Tail, PseudoHeaders#{scheme => Scheme});
  875. pseudo_headers([{<<":authority">>, _}|_], #{authority := _}) ->
  876. {error, 'Multiple :authority pseudo-headers were found. (RFC7540 8.1.2.3)'};
  877. pseudo_headers([{<<":authority">>, Authority}|Tail], PseudoHeaders) ->
  878. pseudo_headers(Tail, PseudoHeaders#{authority => Authority});
  879. pseudo_headers([{<<":path">>, _}|_], #{path := _}) ->
  880. {error, 'Multiple :path pseudo-headers were found. (RFC7540 8.1.2.3)'};
  881. pseudo_headers([{<<":path">>, Path}|Tail], PseudoHeaders) ->
  882. pseudo_headers(Tail, PseudoHeaders#{path => Path});
  883. pseudo_headers([{<<":protocol">>, _}|_], #{protocol := _}) ->
  884. {error, 'Multiple :protocol pseudo-headers were found. (RFC7540 8.1.2.3)'};
  885. pseudo_headers([{<<":protocol">>, Protocol}|Tail], PseudoHeaders) ->
  886. pseudo_headers(Tail, PseudoHeaders#{protocol => Protocol});
  887. pseudo_headers([{<<":", _/bits>>, _}|_], _) ->
  888. {error, 'An unknown or invalid pseudo-header was found. (RFC7540 8.1.2.1)'};
  889. pseudo_headers(Headers, PseudoHeaders) ->
  890. {ok, PseudoHeaders, Headers}.
  891. stream_regular_headers_init(State, StreamID, IsFin, Headers, PseudoHeaders) ->
  892. case regular_headers(Headers) of
  893. ok ->
  894. stream_req_init(State, StreamID, IsFin,
  895. headers_to_map(Headers, #{}), PseudoHeaders);
  896. {error, HumanReadable} ->
  897. stream_malformed(State, StreamID, HumanReadable)
  898. end.
  899. regular_headers([{<<":", _/bits>>, _}|_]) ->
  900. {error, 'Pseudo-headers were found after regular headers. (RFC7540 8.1.2.1)'};
  901. regular_headers([{<<"connection">>, _}|_]) ->
  902. {error, 'The connection header is not allowed. (RFC7540 8.1.2.2)'};
  903. regular_headers([{<<"keep-alive">>, _}|_]) ->
  904. {error, 'The keep-alive header is not allowed. (RFC7540 8.1.2.2)'};
  905. regular_headers([{<<"proxy-authenticate">>, _}|_]) ->
  906. {error, 'The proxy-authenticate header is not allowed. (RFC7540 8.1.2.2)'};
  907. regular_headers([{<<"proxy-authorization">>, _}|_]) ->
  908. {error, 'The proxy-authorization header is not allowed. (RFC7540 8.1.2.2)'};
  909. regular_headers([{<<"transfer-encoding">>, _}|_]) ->
  910. {error, 'The transfer-encoding header is not allowed. (RFC7540 8.1.2.2)'};
  911. regular_headers([{<<"upgrade">>, _}|_]) ->
  912. {error, 'The upgrade header is not allowed. (RFC7540 8.1.2.2)'};
  913. regular_headers([{<<"te">>, Value}|_]) when Value =/= <<"trailers">> ->
  914. {error, 'The te header with a value other than "trailers" is not allowed. (RFC7540 8.1.2.2)'};
  915. regular_headers([{Name, _}|Tail]) ->
  916. case cowboy_bstr:to_lower(Name) of
  917. Name -> regular_headers(Tail);
  918. _ -> {error, 'Header names must be lowercase. (RFC7540 8.1.2)'}
  919. end;
  920. regular_headers([]) ->
  921. ok.
  922. %% This function is necessary to properly handle duplicate headers
  923. %% and the special-case cookie header.
  924. headers_to_map([], Acc) ->
  925. Acc;
  926. headers_to_map([{Name, Value}|Tail], Acc0) ->
  927. Acc = case Acc0 of
  928. %% The cookie header does not use proper HTTP header lists.
  929. #{Name := Value0} when Name =:= <<"cookie">> -> Acc0#{Name => << Value0/binary, "; ", Value/binary >>};
  930. #{Name := Value0} -> Acc0#{Name => << Value0/binary, ", ", Value/binary >>};
  931. _ -> Acc0#{Name => Value}
  932. end,
  933. headers_to_map(Tail, Acc).
  934. stream_req_init(State, StreamID, IsFin, Headers, PseudoHeaders) ->
  935. case Headers of
  936. _ when IsFin =:= fin ->
  937. stream_req_init(State, StreamID, IsFin, Headers, PseudoHeaders, 0);
  938. #{<<"content-length">> := <<"0">>} ->
  939. stream_req_init(State, StreamID, IsFin, Headers, PseudoHeaders, 0);
  940. #{<<"content-length">> := BinLength} ->
  941. try
  942. stream_req_init(State, StreamID, IsFin, Headers, PseudoHeaders,
  943. cow_http_hd:parse_content_length(BinLength))
  944. catch _:_ ->
  945. stream_malformed(State, StreamID,
  946. 'The content-length header is invalid. (RFC7230 3.3.2)')
  947. end;
  948. _ ->
  949. stream_req_init(State, StreamID, IsFin, Headers, PseudoHeaders, undefined)
  950. end.
  951. stream_req_init(State=#state{ref=Ref, peer=Peer, sock=Sock, cert=Cert},
  952. StreamID, IsFin, Headers, PseudoHeaders=#{method := Method, scheme := Scheme,
  953. authority := Authority, path := PathWithQs}, BodyLength) ->
  954. try cow_http_hd:parse_host(Authority) of
  955. {Host, Port} ->
  956. try cow_http:parse_fullpath(PathWithQs) of
  957. {<<>>, _} ->
  958. stream_malformed(State, StreamID,
  959. 'The path component must not be empty. (RFC7540 8.1.2.3)');
  960. {Path, Qs} ->
  961. Req0 = #{
  962. ref => Ref,
  963. pid => self(),
  964. streamid => StreamID,
  965. peer => Peer,
  966. sock => Sock,
  967. cert => Cert,
  968. method => Method,
  969. scheme => Scheme,
  970. host => Host,
  971. port => Port,
  972. path => Path,
  973. qs => Qs,
  974. version => 'HTTP/2',
  975. headers => Headers,
  976. has_body => IsFin =:= nofin,
  977. body_length => BodyLength
  978. },
  979. %% We add the protocol information for extended CONNECTs.
  980. Req = case PseudoHeaders of
  981. #{protocol := Protocol} ->
  982. Req0#{protocol => Protocol};
  983. _ ->
  984. Req0
  985. end,
  986. stream_handler_init(State, StreamID, IsFin, idle, Req)
  987. catch _:_ ->
  988. stream_malformed(State, StreamID,
  989. 'The :path pseudo-header is invalid. (RFC7540 8.1.2.3)')
  990. end
  991. catch _:_ ->
  992. stream_malformed(State, StreamID,
  993. 'The :authority pseudo-header is invalid. (RFC7540 8.1.2.3)')
  994. end.
  995. stream_malformed(State=#state{socket=Socket, transport=Transport}, StreamID, _) ->
  996. Transport:send(Socket, cow_http2:rst_stream(StreamID, protocol_error)),
  997. State.
  998. stream_refused(State=#state{socket=Socket, transport=Transport}, StreamID, _) ->
  999. Transport:send(Socket, cow_http2:rst_stream(StreamID, refused_stream)),
  1000. State.
  1001. stream_early_error(State0=#state{ref=Ref, opts=Opts, peer=Peer,
  1002. local_settings=#{initial_window_size := RemoteWindow},
  1003. remote_settings=#{initial_window_size := LocalWindow},
  1004. streams=Streams}, StreamID, IsFin, StatusCode0,
  1005. #{method := Method}, HumanReadable) ->
  1006. %% We automatically terminate the stream but it is not an error
  1007. %% per se (at least not in the first implementation).
  1008. Reason = {stream_error, no_error, HumanReadable},
  1009. %% The partial Req is minimal for now. We only have one case
  1010. %% where it can be called (when a method is completely disabled).
  1011. PartialReq = #{
  1012. ref => Ref,
  1013. peer => Peer,
  1014. method => Method
  1015. },
  1016. Resp = {response, StatusCode0, RespHeaders0=#{<<"content-length">> => <<"0">>}, <<>>},
  1017. %% We need a stream to talk to the send_* functions.
  1018. Stream0 = #stream{id=StreamID, state=flush, method=Method,
  1019. remote=IsFin, local=idle,
  1020. local_window=LocalWindow, remote_window=RemoteWindow},
  1021. try cowboy_stream:early_error(StreamID, Reason, PartialReq, Resp, Opts) of
  1022. {response, StatusCode, RespHeaders, RespBody} ->
  1023. case send_response(State0, Stream0, StatusCode, RespHeaders, RespBody) of
  1024. {State, #stream{local=fin}} ->
  1025. State;
  1026. {State, Stream} ->
  1027. State#state{streams=[Stream|Streams]}
  1028. end
  1029. catch Class:Exception ->
  1030. cowboy_stream:report_error(early_error,
  1031. [StreamID, Reason, PartialReq, Resp, Opts],
  1032. Class, Exception, erlang:get_stacktrace()),
  1033. %% We still need to send an error response, so send what we initially
  1034. %% wanted to send. It's better than nothing.
  1035. send_headers(State0, Stream0, StatusCode0, RespHeaders0, fin)
  1036. end.
  1037. stream_handler_init(State=#state{opts=Opts,
  1038. local_settings=#{initial_window_size := RemoteWindow},
  1039. remote_settings=#{initial_window_size := LocalWindow}},
  1040. StreamID, RemoteIsFin, LocalIsFin,
  1041. Req=#{method := Method, headers := Headers}) ->
  1042. try cowboy_stream:init(StreamID, Req, Opts) of
  1043. {Commands, StreamState} ->
  1044. commands(State#state{client_streamid=StreamID},
  1045. #stream{id=StreamID, state=StreamState,
  1046. method=Method, remote=RemoteIsFin, local=LocalIsFin,
  1047. local_window=LocalWindow, remote_window=RemoteWindow,
  1048. te=maps:get(<<"te">>, Headers, undefined)},
  1049. Commands)
  1050. catch Class:Exception ->
  1051. cowboy_stream:report_error(init,
  1052. [StreamID, Req, Opts],
  1053. Class, Exception, erlang:get_stacktrace()),
  1054. stream_reset(State, StreamID, {internal_error, {Class, Exception},
  1055. 'Unhandled exception in cowboy_stream:init/3.'})
  1056. end.
  1057. %% @todo Don't send an RST_STREAM if one was already sent.
  1058. stream_reset(State=#state{socket=Socket, transport=Transport}, StreamID, StreamError) ->
  1059. Reason = case StreamError of
  1060. {internal_error, _, _} -> internal_error;
  1061. {stream_error, Reason0, _} -> Reason0
  1062. end,
  1063. Transport:send(Socket, cow_http2:rst_stream(StreamID, Reason)),
  1064. stream_terminate(stream_linger(State, StreamID), StreamID, StreamError).
  1065. %% We only keep up to 100 streams in this state. @todo Make it configurable?
  1066. stream_linger(State=#state{lingering_streams=Lingering0}, StreamID) ->
  1067. Lingering = [StreamID|lists:sublist(Lingering0, 100 - 1)],
  1068. State#state{lingering_streams=Lingering}.
  1069. stream_terminate(State0=#state{streams=Streams0, children=Children0}, StreamID, Reason) ->
  1070. case lists:keytake(StreamID, #stream.id, Streams0) of
  1071. %% When the stream terminates normally (without sending RST_STREAM)
  1072. %% and no response was sent, we need to send a proper response back to the client.
  1073. {value, Stream=#stream{local=idle}, Streams} when Reason =:= normal ->
  1074. State1 = #state{streams=Streams1} = info(State0, StreamID, {response, 204, #{}, <<>>}),
  1075. State = maybe_skip_body(State1, Stream, Reason),
  1076. #stream{state=StreamState} = lists:keyfind(StreamID, #stream.id, Streams1),
  1077. stream_call_terminate(StreamID, Reason, StreamState),
  1078. Children = cowboy_children:shutdown(Children0, StreamID),
  1079. State#state{streams=Streams, children=Children};
  1080. %% When a response was sent but not terminated, we need to close the stream.
  1081. {value, Stream=#stream{local=nofin, local_buffer_size=0}, Streams}
  1082. when Reason =:= normal ->
  1083. State1 = #state{streams=Streams1} = info(State0, StreamID, {data, fin, <<>>}),
  1084. State = maybe_skip_body(State1, Stream, Reason),
  1085. #stream{state=StreamState} = lists:keyfind(StreamID, #stream.id, Streams1),
  1086. stream_call_terminate(StreamID, Reason, StreamState),
  1087. Children = cowboy_children:shutdown(Children0, StreamID),
  1088. State#state{streams=Streams, children=Children};
  1089. %% Unless there is still data in the buffer. We can however reset
  1090. %% a few fields and set a special local state to avoid confusion.
  1091. %%
  1092. %% We do not reset the stream in this case (to skip the body)
  1093. %% because we are still sending data via the buffer. We will
  1094. %% reset the stream if necessary once the buffer is empty.
  1095. {value, Stream=#stream{state=StreamState, local=nofin}, Streams} ->
  1096. stream_call_terminate(StreamID, Reason, StreamState),
  1097. Children = cowboy_children:shutdown(Children0, StreamID),
  1098. State0#state{streams=[Stream#stream{state=flush, local=flush}|Streams],
  1099. children=Children};
  1100. %% Otherwise we sent or received an RST_STREAM and/or the stream is already closed.
  1101. {value, Stream=#stream{state=StreamState}, Streams} ->
  1102. State = maybe_skip_body(State0, Stream, Reason),
  1103. stream_call_terminate(StreamID, Reason, StreamState),
  1104. Children = cowboy_children:shutdown(Children0, StreamID),
  1105. State#state{streams=Streams, children=Children};
  1106. %% The stream doesn't exist. This can occur for various reasons.
  1107. %% It can happen before the stream has been created, or because
  1108. %% the cowboy_stream:init call failed, in which case doing nothing
  1109. %% is correct.
  1110. false ->
  1111. State0
  1112. end.
  1113. %% When the stream stops normally without reading the request
  1114. %% body fully we need to tell the client to stop sending it.
  1115. %% We do this by sending an RST_STREAM with reason NO_ERROR. (RFC7540 8.1.0)
  1116. maybe_skip_body(State=#state{socket=Socket, transport=Transport},
  1117. #stream{id=StreamID, remote=nofin}, normal) ->
  1118. Transport:send(Socket, cow_http2:rst_stream(StreamID, no_error)),
  1119. stream_linger(State, StreamID);
  1120. maybe_skip_body(State, _, _) ->
  1121. State.
  1122. stream_call_terminate(StreamID, Reason, StreamState) ->
  1123. try
  1124. cowboy_stream:terminate(StreamID, Reason, StreamState)
  1125. catch Class:Exception ->
  1126. cowboy_stream:report_error(terminate,
  1127. [StreamID, Reason, StreamState],
  1128. Class, Exception, erlang:get_stacktrace())
  1129. end.
  1130. %% System callbacks.
  1131. -spec system_continue(_, _, {#state{}, binary()}) -> ok.
  1132. system_continue(_, _, {State, Buffer}) ->
  1133. loop(State, Buffer).
  1134. -spec system_terminate(any(), _, _, {#state{}, binary()}) -> no_return().
  1135. system_terminate(Reason, _, _, {State, _}) ->
  1136. %% @todo We should exit gracefully, if possible.
  1137. terminate(State, Reason).
  1138. -spec system_code_change(Misc, _, _, _) -> {ok, Misc} when Misc::{#state{}, binary()}.
  1139. system_code_change(Misc, _, _, _) ->
  1140. {ok, Misc}.