cowboy_http2.erl 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. %% Copyright (c) 2015, 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/8]).
  17. -export([init/10]).
  18. -export([system_continue/3]).
  19. -export([system_terminate/4]).
  20. -export([system_code_change/4]).
  21. -record(stream, {
  22. id = undefined :: cowboy_stream:streamid(),
  23. state = undefined :: any(),
  24. %% Whether we finished sending data.
  25. local = idle :: idle | cowboy_stream:fin(),
  26. %% Whether we finished receiving data.
  27. remote = nofin :: cowboy_stream:fin(),
  28. %% Request body length.
  29. body_length = 0 :: non_neg_integer()
  30. }).
  31. -type stream() :: #stream{}.
  32. %% @todo priority: if we receive a message for a stream, do a selective receive
  33. %% to get all messages in the mailbox and prioritize them. (later)
  34. -record(state, {
  35. parent = undefined :: pid(),
  36. ref :: ranch:ref(),
  37. socket = undefined :: inet:socket(),
  38. transport :: module(),
  39. opts = #{} :: map(),
  40. handler :: module(),
  41. %% Remote address and port for the connection.
  42. peer = undefined :: {inet:ip_address(), inet:port_number()},
  43. %% Settings are separate for each endpoint. In addition, settings
  44. %% must be acknowledged before they can be expected to be applied.
  45. %%
  46. %% @todo Since the ack is required, we must timeout if we don't receive it.
  47. %% @todo I haven't put as much thought as I should have on this,
  48. %% the final settings handling will be very different.
  49. local_settings = #{} :: map(),
  50. %% @todo We need a TimerRef to do SETTINGS_TIMEOUT errors.
  51. %% We need to be careful there. It's well possible that we send
  52. %% two SETTINGS frames before we receive a SETTINGS ack.
  53. next_settings = #{} :: undefined | map(), %% @todo perhaps set to undefined by default
  54. remote_settings = #{} :: map(),
  55. %% Stream identifiers.
  56. server_streamid = 2 :: pos_integer(),
  57. %% @todo last known good streamid
  58. %% Currently active HTTP/2 streams. Streams may be initiated either
  59. %% by the client or by the server through PUSH_PROMISE frames.
  60. streams = [] :: [stream()],
  61. %% Streams can spawn zero or more children which are then managed
  62. %% by this module if operating as a supervisor.
  63. children = [] :: [{pid(), cowboy_stream:streamid()}],
  64. %% The client starts by sending a sequence of bytes as a preface,
  65. %% followed by a potentially empty SETTINGS frame. Then the connection
  66. %% is established and continues normally. An exception is when a HEADERS
  67. %% frame is sent followed by CONTINUATION frames: no other frame can be
  68. %% sent in between.
  69. parse_state = undefined :: {preface, sequence, reference()}
  70. | {preface, settings, reference()}
  71. | normal
  72. | {continuation, cowboy_stream:streamid(), cowboy_stream:fin(), binary()},
  73. %% HPACK decoding and encoding state.
  74. decode_state = cow_hpack:init() :: cow_hpack:state(),
  75. encode_state = cow_hpack:init() :: cow_hpack:state()
  76. }).
  77. -spec init(pid(), ranch:ref(), inet:socket(), module(), cowboy:opts(), module()) -> ok.
  78. init(Parent, Ref, Socket, Transport, Opts, Handler) ->
  79. case Transport:peername(Socket) of
  80. {ok, Peer} ->
  81. init(Parent, Ref, Socket, Transport, Opts, Handler, Peer, <<>>);
  82. {error, Reason} ->
  83. %% Couldn't read the peer address; connection is gone.
  84. terminate(undefined, {socket_error, Reason, 'An error has occurred on the socket.'})
  85. end.
  86. -spec init(pid(), ranch:ref(), inet:socket(), module(), cowboy:opts(), module(),
  87. {inet:ip_address(), inet:port_number()}, binary()) -> ok.
  88. init(Parent, Ref, Socket, Transport, Opts, Handler, Peer, Buffer) ->
  89. State = #state{parent=Parent, ref=Ref, socket=Socket,
  90. transport=Transport, opts=Opts, handler=Handler, peer=Peer,
  91. parse_state={preface, sequence, preface_timeout(Opts)}},
  92. preface(State),
  93. case Buffer of
  94. <<>> -> before_loop(State, Buffer);
  95. _ -> parse(State, Buffer)
  96. end.
  97. %% @todo Add an argument for the request body.
  98. -spec init(pid(), ranch:ref(), inet:socket(), module(), cowboy:opts(), module(),
  99. {inet:ip_address(), inet:port_number()}, binary(), binary() | undefined, cowboy_req:req()) -> ok.
  100. init(Parent, Ref, Socket, Transport, Opts, Handler, Peer, Buffer, _Settings, Req) ->
  101. State0 = #state{parent=Parent, ref=Ref, socket=Socket,
  102. transport=Transport, opts=Opts, handler=Handler, peer=Peer,
  103. parse_state={preface, sequence, preface_timeout(Opts)}},
  104. preface(State0),
  105. %% @todo Apply settings.
  106. %% StreamID from HTTP/1.1 Upgrade requests is always 1.
  107. %% The stream is always in the half-closed (remote) state.
  108. State = stream_handler_init(State0, 1, fin, Req),
  109. case Buffer of
  110. <<>> -> before_loop(State, Buffer);
  111. _ -> parse(State, Buffer)
  112. end.
  113. preface(#state{socket=Socket, transport=Transport, next_settings=Settings}) ->
  114. %% We send next_settings and use defaults until we get a ack.
  115. ok = Transport:send(Socket, cow_http2:settings(Settings)).
  116. preface_timeout(Opts) ->
  117. PrefaceTimeout = maps:get(preface_timeout, Opts, 5000),
  118. erlang:start_timer(PrefaceTimeout, self(), preface_timeout).
  119. %% @todo Add the timeout for last time since we heard of connection.
  120. before_loop(State, Buffer) ->
  121. loop(State, Buffer).
  122. loop(State=#state{parent=Parent, socket=Socket, transport=Transport,
  123. children=Children, parse_state=PS}, Buffer) ->
  124. Transport:setopts(Socket, [{active, once}]),
  125. {OK, Closed, Error} = Transport:messages(),
  126. receive
  127. %% Socket messages.
  128. {OK, Socket, Data} ->
  129. parse(State, << Buffer/binary, Data/binary >>);
  130. {Closed, Socket} ->
  131. terminate(State, {socket_error, closed, 'The socket has been closed.'});
  132. {Error, Socket, Reason} ->
  133. terminate(State, {socket_error, Reason, 'An error has occurred on the socket.'});
  134. %% System messages.
  135. {'EXIT', Parent, Reason} ->
  136. exit(Reason);
  137. {system, From, Request} ->
  138. sys:handle_system_msg(Request, From, Parent, ?MODULE, [], {State, Buffer});
  139. {timeout, TRef, preface_timeout} ->
  140. case PS of
  141. {preface, _, TRef} ->
  142. terminate(State, {connection_error, protocol_error,
  143. 'The preface was not received in a reasonable amount of time.'});
  144. _ ->
  145. loop(State, Buffer)
  146. end;
  147. %% Messages pertaining to a stream.
  148. {{Pid, StreamID}, Msg} when Pid =:= self() ->
  149. loop(info(State, StreamID, Msg), Buffer);
  150. %% Exit signal from children.
  151. Msg = {'EXIT', Pid, _} ->
  152. loop(down(State, Pid, Msg), Buffer);
  153. %% Calls from supervisor module.
  154. {'$gen_call', {From, Tag}, which_children} ->
  155. Workers = [{?MODULE, Pid, worker, [?MODULE]} || {Pid, _} <- Children],
  156. From ! {Tag, Workers},
  157. loop(State, Buffer);
  158. {'$gen_call', {From, Tag}, count_children} ->
  159. NbChildren = length(Children),
  160. Counts = [{specs, 1}, {active, NbChildren},
  161. {supervisors, 0}, {workers, NbChildren}],
  162. From ! {Tag, Counts},
  163. loop(State, Buffer);
  164. {'$gen_call', {From, Tag}, _} ->
  165. From ! {Tag, {error, ?MODULE}},
  166. loop(State, Buffer);
  167. Msg ->
  168. error_logger:error_msg("Received stray message ~p.", [Msg]),
  169. loop(State, Buffer)
  170. %% @todo Configurable timeout.
  171. after 60000 ->
  172. terminate(State, {internal_error, timeout, 'No message or data received before timeout.'})
  173. end.
  174. parse(State=#state{socket=Socket, transport=Transport, parse_state={preface, sequence, TRef}}, Data) ->
  175. case Data of
  176. << "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", Rest/bits >> ->
  177. parse(State#state{parse_state={preface, settings, TRef}}, Rest);
  178. _ when byte_size(Data) >= 24 ->
  179. Transport:close(Socket),
  180. exit({shutdown, {connection_error, protocol_error,
  181. 'The connection preface was invalid. (RFC7540 3.5)'}});
  182. _ ->
  183. Len = byte_size(Data),
  184. << Preface:Len/binary, _/bits >> = <<"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n">>,
  185. case Data of
  186. Preface ->
  187. %% @todo OK we should have a timeout when waiting for the preface.
  188. before_loop(State, Data);
  189. _ ->
  190. Transport:close(Socket),
  191. exit({shutdown, {connection_error, protocol_error,
  192. 'The connection preface was invalid. (RFC7540 3.5)'}})
  193. end
  194. end;
  195. %% @todo Perhaps instead of just more we can have {more, Len} to avoid all the checks.
  196. parse(State=#state{parse_state=ParseState}, Data) ->
  197. case cow_http2:parse(Data) of
  198. {ok, Frame, Rest} ->
  199. case ParseState of
  200. normal ->
  201. parse(frame(State, Frame), Rest);
  202. {preface, settings, TRef} ->
  203. parse_settings_preface(State, Frame, Rest, TRef);
  204. {continuation, _, _, _} ->
  205. parse(continuation_frame(State, Frame), Rest)
  206. end;
  207. {stream_error, StreamID, Reason, Human, Rest} ->
  208. parse(stream_reset(State, StreamID, {stream_error, Reason, Human}), Rest);
  209. Error = {connection_error, _, _} ->
  210. terminate(State, Error);
  211. more ->
  212. before_loop(State, Data)
  213. end.
  214. parse_settings_preface(State, Frame={settings, _}, Rest, TRef) ->
  215. erlang:cancel_timer(TRef, [{async, true}, {info, false}]),
  216. parse(frame(State#state{parse_state=normal}, Frame), Rest);
  217. parse_settings_preface(State, _, _, _) ->
  218. terminate(State, {connection_error, protocol_error,
  219. 'The preface sequence must be followed by a SETTINGS frame. (RFC7540 3.5)'}).
  220. %% @todo When we get a 'fin' we need to check if the stream had a 'fin' sent back
  221. %% and terminate the stream if this is the end of it.
  222. %% DATA frame.
  223. frame(State=#state{handler=Handler, streams=Streams}, {data, StreamID, IsFin0, Data}) ->
  224. case lists:keyfind(StreamID, #stream.id, Streams) of
  225. Stream = #stream{state=StreamState0, remote=nofin, body_length=Len0} ->
  226. Len = Len0 + byte_size(Data),
  227. IsFin = case IsFin0 of
  228. fin -> {fin, Len};
  229. nofin -> nofin
  230. end,
  231. try Handler:data(StreamID, IsFin, Data, StreamState0) of
  232. {Commands, StreamState} ->
  233. commands(State, Stream#stream{state=StreamState, body_length=Len}, Commands)
  234. catch Class:Reason ->
  235. error_logger:error_msg("Exception occurred in ~s:data(~p, ~p, ~p, ~p) with reason ~p:~p.",
  236. [Handler, StreamID, IsFin0, Data, StreamState0, Class, Reason]),
  237. stream_reset(State, StreamID, {internal_error, {Class, Reason},
  238. 'Exception occurred in StreamHandler:data/4 call.'})
  239. end;
  240. _ ->
  241. stream_reset(State, StreamID, {stream_error, stream_closed,
  242. 'DATA frame received for a closed or non-existent stream. (RFC7540 6.1)'})
  243. end;
  244. %% Single HEADERS frame headers block.
  245. frame(State, {headers, StreamID, IsFin, head_fin, HeaderBlock}) ->
  246. %% @todo We probably need to validate StreamID here and in 4 next clauses.
  247. stream_init(State, StreamID, IsFin, HeaderBlock);
  248. %% HEADERS frame starting a headers block. Enter continuation mode.
  249. frame(State, {headers, StreamID, IsFin, head_nofin, HeaderBlockFragment}) ->
  250. State#state{parse_state={continuation, StreamID, IsFin, HeaderBlockFragment}};
  251. %% Single HEADERS frame headers block with priority.
  252. frame(State, {headers, StreamID, IsFin, head_fin,
  253. _IsExclusive, _DepStreamID, _Weight, HeaderBlock}) ->
  254. %% @todo Handle priority.
  255. stream_init(State, StreamID, IsFin, HeaderBlock);
  256. %% HEADERS frame starting a headers block. Enter continuation mode.
  257. frame(State, {headers, StreamID, IsFin, head_nofin,
  258. _IsExclusive, _DepStreamID, _Weight, HeaderBlockFragment}) ->
  259. %% @todo Handle priority.
  260. State#state{parse_state={continuation, StreamID, IsFin, HeaderBlockFragment}};
  261. %% PRIORITY frame.
  262. frame(State, {priority, _StreamID, _IsExclusive, _DepStreamID, _Weight}) ->
  263. %% @todo Validate StreamID?
  264. %% @todo Handle priority.
  265. State;
  266. %% RST_STREAM frame.
  267. frame(State, {rst_stream, StreamID, Reason}) ->
  268. stream_reset(State, StreamID, {stream_error, Reason, 'Stream reset requested by client.'});
  269. %% SETTINGS frame.
  270. frame(State=#state{socket=Socket, transport=Transport}, {settings, _Settings}) ->
  271. %% @todo Apply SETTINGS.
  272. Transport:send(Socket, cow_http2:settings_ack()),
  273. State;
  274. %% Ack for a previously sent SETTINGS frame.
  275. frame(State=#state{next_settings=_NextSettings}, settings_ack) ->
  276. %% @todo Apply SETTINGS that require synchronization.
  277. State;
  278. %% Unexpected PUSH_PROMISE frame.
  279. frame(State, {push_promise, _, _, _, _}) ->
  280. terminate(State, {connection_error, protocol_error,
  281. 'PUSH_PROMISE frames MUST only be sent on a peer-initiated stream. (RFC7540 6.6)'});
  282. %% PING frame.
  283. frame(State=#state{socket=Socket, transport=Transport}, {ping, Opaque}) ->
  284. Transport:send(Socket, cow_http2:ping_ack(Opaque)),
  285. State;
  286. %% Ack for a previously sent PING frame.
  287. %%
  288. %% @todo Might want to check contents but probably a waste of time.
  289. frame(State, {ping_ack, _Opaque}) ->
  290. State;
  291. %% GOAWAY frame.
  292. frame(State, Frame={goaway, _, _, _}) ->
  293. terminate(State, {stop, Frame, 'Client is going away.'});
  294. %% Connection-wide WINDOW_UPDATE frame.
  295. frame(State, {window_update, _Increment}) ->
  296. %% @todo control flow
  297. State;
  298. %% Stream-specific WINDOW_UPDATE frame.
  299. frame(State, {window_update, _StreamID, _Increment}) ->
  300. %% @todo stream-specific control flow
  301. State;
  302. %% Unexpected CONTINUATION frame.
  303. frame(State, {continuation, _, _, _}) ->
  304. terminate(State, {connection_error, protocol_error,
  305. 'CONTINUATION frames MUST be preceded by a HEADERS frame. (RFC7540 6.10)'}).
  306. continuation_frame(State=#state{parse_state={continuation, StreamID, IsFin, HeaderBlockFragment0}},
  307. {continuation, StreamID, fin, HeaderBlockFragment1}) ->
  308. stream_init(State#state{parse_state=normal}, StreamID, IsFin,
  309. << HeaderBlockFragment0/binary, HeaderBlockFragment1/binary >>);
  310. continuation_frame(State=#state{parse_state={continuation, StreamID, IsFin, HeaderBlockFragment0}},
  311. {continuation, StreamID, nofin, HeaderBlockFragment1}) ->
  312. State#state{parse_state={continuation, StreamID, IsFin,
  313. << HeaderBlockFragment0/binary, HeaderBlockFragment1/binary >>}};
  314. continuation_frame(State, _) ->
  315. terminate(State, {connection_error, protocol_error,
  316. 'An invalid frame was received while expecting a CONTINUATION frame. (RFC7540 6.2)'}).
  317. down(State=#state{children=Children0}, Pid, Msg) ->
  318. case lists:keytake(Pid, 1, Children0) of
  319. {value, {_, StreamID}, Children} ->
  320. info(State#state{children=Children}, StreamID, Msg);
  321. false ->
  322. error_logger:error_msg("Received EXIT signal ~p for unknown process ~p.", [Msg, Pid]),
  323. State
  324. end.
  325. info(State=#state{handler=Handler, streams=Streams}, StreamID, Msg) ->
  326. case lists:keyfind(StreamID, #stream.id, Streams) of
  327. Stream = #stream{state=StreamState0} ->
  328. try Handler:info(StreamID, Msg, StreamState0) of
  329. {Commands, StreamState} ->
  330. commands(State, Stream#stream{state=StreamState}, Commands)
  331. catch Class:Reason ->
  332. error_logger:error_msg("Exception occurred in ~s:info(~p, ~p, ~p) with reason ~p:~p.",
  333. [Handler, StreamID, Msg, StreamState0, Class, Reason]),
  334. stream_reset(State, StreamID, {internal_error, {Class, Reason},
  335. 'Exception occurred in StreamHandler:info/3 call.'})
  336. end;
  337. false ->
  338. error_logger:error_msg("Received message ~p for unknown stream ~p.", [Msg, StreamID]),
  339. State
  340. end.
  341. commands(State, Stream, []) ->
  342. after_commands(State, Stream);
  343. %% Send response headers.
  344. %%
  345. %% @todo Kill the stream if it sent a response when one has already been sent.
  346. %% @todo Keep IsFin in the state.
  347. %% @todo Same two things above apply to DATA, possibly promise too.
  348. commands(State=#state{socket=Socket, transport=Transport, encode_state=EncodeState0},
  349. Stream=#stream{id=StreamID, local=idle}, [{response, StatusCode, Headers0, Body}|Tail]) ->
  350. Headers = Headers0#{<<":status">> => integer_to_binary(StatusCode)},
  351. {HeaderBlock, EncodeState} = headers_encode(Headers, EncodeState0),
  352. case Body of
  353. <<>> ->
  354. Transport:send(Socket, cow_http2:headers(StreamID, fin, HeaderBlock)),
  355. commands(State#state{encode_state=EncodeState}, Stream#stream{local=fin}, Tail);
  356. {sendfile, O, B, P} ->
  357. Transport:send(Socket, cow_http2:headers(StreamID, nofin, HeaderBlock)),
  358. commands(State#state{encode_state=EncodeState}, Stream#stream{local=nofin},
  359. [{sendfile, fin, O, B, P}|Tail]);
  360. _ ->
  361. Transport:send(Socket, [
  362. cow_http2:headers(StreamID, nofin, HeaderBlock),
  363. cow_http2:data(StreamID, fin, Body)
  364. ]),
  365. commands(State#state{encode_state=EncodeState}, Stream#stream{local=fin}, Tail)
  366. end;
  367. %% @todo response when local!=idle
  368. %% Send response headers and initiate chunked encoding.
  369. commands(State=#state{socket=Socket, transport=Transport, encode_state=EncodeState0},
  370. Stream=#stream{id=StreamID, local=idle}, [{headers, StatusCode, Headers0}|Tail]) ->
  371. Headers = Headers0#{<<":status">> => integer_to_binary(StatusCode)},
  372. {HeaderBlock, EncodeState} = headers_encode(Headers, EncodeState0),
  373. Transport:send(Socket, cow_http2:headers(StreamID, nofin, HeaderBlock)),
  374. commands(State#state{encode_state=EncodeState}, Stream#stream{local=nofin}, Tail);
  375. %% @todo headers when local!=idle
  376. %% Send a response body chunk.
  377. %%
  378. %% @todo WINDOW_UPDATE stuff require us to buffer some data.
  379. %%
  380. %% When the body is sent using sendfile, the current solution is not
  381. %% very good. The body could be too large, blocking the connection.
  382. %% Also sendfile technically only works over TCP, so it's not that
  383. %% useful for HTTP/2. At the very least the sendfile call should be
  384. %% split into multiple calls and flow control should be used to make
  385. %% sure we only send as fast as the client can receive and don't block
  386. %% anything.
  387. commands(State=#state{socket=Socket, transport=Transport}, Stream=#stream{id=StreamID, local=nofin},
  388. [{data, IsFin, Data}|Tail]) ->
  389. Transport:send(Socket, cow_http2:data(StreamID, IsFin, Data)),
  390. commands(State, Stream#stream{local=IsFin}, Tail);
  391. %% @todo data when local!=nofin
  392. %% Send a file.
  393. %%
  394. %% @todo This implementation is terrible. A good implementation would
  395. %% need to check that Bytes is exact (or we need to document that we
  396. %% trust it to be exact), and would need to send the file asynchronously
  397. %% in many data frames. Perhaps a sendfile call should result in a
  398. %% process being created specifically for this purpose. Or perhaps
  399. %% the protocol should be "dumb" and the stream handler be the one
  400. %% to ensure the file is sent in chunks (which would require a better
  401. %% flow control at the stream handler level). One thing for sure, the
  402. %% implementation necessarily varies between HTTP/1.1 and HTTP/2.
  403. commands(State=#state{socket=Socket, transport=Transport}, Stream=#stream{id=StreamID, local=nofin},
  404. [{sendfile, IsFin, Offset, Bytes, Path}|Tail]) ->
  405. Transport:send(Socket, cow_http2:data_header(StreamID, IsFin, Bytes)),
  406. Transport:sendfile(Socket, Path, Offset, Bytes),
  407. commands(State, Stream#stream{local=IsFin}, Tail);
  408. %% @todo sendfile when local!=nofin
  409. %% Send a push promise.
  410. %%
  411. %% @todo We need to keep track of what promises we made so that we don't
  412. %% end up with an infinite loop of promises.
  413. commands(State0=#state{socket=Socket, transport=Transport, server_streamid=PromisedStreamID,
  414. encode_state=EncodeState0}, Stream=#stream{id=StreamID},
  415. [{promise, Method, Scheme, Authority, Path, Headers0}|Tail]) ->
  416. Headers = Headers0#{<<":method">> => Method,
  417. <<":scheme">> => Scheme,
  418. <<":authority">> => Authority,
  419. <<":path">> => Path},
  420. {HeaderBlock, EncodeState} = headers_encode(Headers, EncodeState0),
  421. Transport:send(Socket, cow_http2:push_promise(StreamID, PromisedStreamID, HeaderBlock)),
  422. %% @todo iolist_to_binary(HeaderBlock) isn't optimal. Need a shortcut.
  423. State = stream_init(State0#state{server_streamid=PromisedStreamID + 2, encode_state=EncodeState},
  424. PromisedStreamID, fin, iolist_to_binary(HeaderBlock)),
  425. commands(State, Stream, Tail);
  426. %% @todo Update the flow control state.
  427. commands(State, Stream, [{flow, _Size}|Tail]) ->
  428. commands(State, Stream, Tail);
  429. %% Supervise a child process.
  430. commands(State=#state{children=Children}, Stream=#stream{id=StreamID},
  431. [{spawn, Pid, _Shutdown}|Tail]) -> %% @todo Shutdown
  432. commands(State#state{children=[{Pid, StreamID}|Children]}, Stream, Tail);
  433. %% Error handling.
  434. commands(State, Stream=#stream{id=StreamID}, [Error = {internal_error, _, _}|_Tail]) ->
  435. %% @todo Do we want to run the commands after an internal_error?
  436. %% @todo Do we even allow commands after?
  437. %% @todo Only reset when the stream still exists.
  438. stream_reset(after_commands(State, Stream), StreamID, Error);
  439. %% Upgrade to a new protocol.
  440. %%
  441. %% @todo Implementation.
  442. %% @todo Can only upgrade if: there are no other streams and there are no children left alive.
  443. %% @todo For HTTP/1.1 we should reject upgrading if pipelining is used.
  444. commands(State, Stream, [{upgrade, _Mod, _ModState}]) ->
  445. commands(State, Stream, []);
  446. commands(State, Stream, [{upgrade, _Mod, _ModState}|Tail]) ->
  447. %% @todo This is an error. Not sure what to do here yet.
  448. commands(State, Stream, Tail);
  449. commands(State, Stream=#stream{id=StreamID}, [stop|_Tail]) ->
  450. %% @todo Do we want to run the commands after a stop?
  451. %% @todo Do we even allow commands after?
  452. stream_terminate(after_commands(State, Stream), StreamID, normal).
  453. after_commands(State=#state{streams=Streams0}, Stream=#stream{id=StreamID}) ->
  454. Streams = lists:keystore(StreamID, #stream.id, Streams0, Stream),
  455. State#state{streams=Streams}.
  456. terminate(#state{socket=Socket, transport=Transport, handler=Handler,
  457. streams=Streams, children=Children}, Reason) ->
  458. %% @todo Send GOAWAY frame; need to keep track of last good stream id; how?
  459. terminate_all_streams(Streams, Reason, Handler, Children),
  460. Transport:close(Socket),
  461. exit({shutdown, Reason}).
  462. terminate_all_streams([], _, _, []) ->
  463. ok;
  464. terminate_all_streams([#stream{id=StreamID, state=StreamState}|Tail], Reason, Handler, Children0) ->
  465. stream_call_terminate(StreamID, Reason, Handler, StreamState),
  466. Children = stream_terminate_children(Children0, StreamID, []),
  467. terminate_all_streams(Tail, Reason, Handler, Children).
  468. %% Stream functions.
  469. stream_init(State0=#state{ref=Ref, socket=Socket, transport=Transport, peer=Peer, decode_state=DecodeState0},
  470. StreamID, IsFin, HeaderBlock) ->
  471. %% @todo Add clause for CONNECT requests (no scheme/path).
  472. try headers_decode(HeaderBlock, DecodeState0) of
  473. {Headers0=#{
  474. <<":method">> := Method,
  475. <<":scheme">> := Scheme,
  476. <<":authority">> := Authority,
  477. <<":path">> := PathWithQs}, DecodeState} ->
  478. State = State0#state{decode_state=DecodeState},
  479. Headers = maps:without([<<":method">>, <<":scheme">>, <<":authority">>, <<":path">>], Headers0),
  480. {Host, Port} = cow_http_hd:parse_host(Authority),
  481. {Path, Qs} = cow_http:parse_fullpath(PathWithQs),
  482. Req = #{
  483. ref => Ref,
  484. pid => self(),
  485. streamid => StreamID,
  486. peer => Peer,
  487. method => Method,
  488. scheme => Scheme,
  489. host => Host,
  490. port => Port,
  491. path => Path,
  492. qs => Qs,
  493. version => 'HTTP/2',
  494. headers => Headers,
  495. has_body => IsFin =:= nofin
  496. %% @todo multipart? keep state separate
  497. %% meta values (cowboy_websocket, cowboy_rest)
  498. },
  499. stream_handler_init(State, StreamID, IsFin, Req);
  500. {_, DecodeState} ->
  501. Transport:send(Socket, cow_http2:rst_stream(StreamID, protocol_error)),
  502. State0#state{decode_state=DecodeState}
  503. catch _:_ ->
  504. terminate(State0, {connection_error, compression_error,
  505. 'Error while trying to decode HPACK-encoded header block. (RFC7540 4.3)'})
  506. end.
  507. stream_handler_init(State=#state{handler=Handler, opts=Opts}, StreamID, IsFin, Req) ->
  508. try Handler:init(StreamID, Req, Opts) of
  509. {Commands, StreamState} ->
  510. commands(State, #stream{id=StreamID, state=StreamState, remote=IsFin}, Commands)
  511. catch Class:Reason ->
  512. error_logger:error_msg("Exception occurred in ~s:init(~p, ~p, ~p) "
  513. "with reason ~p:~p.",
  514. [Handler, StreamID, IsFin, Req, Class, Reason]),
  515. stream_reset(State, StreamID, {internal_error, {Class, Reason},
  516. 'Exception occurred in StreamHandler:init/7 call.'}) %% @todo Check final arity.
  517. end.
  518. %% @todo We might need to keep track of which stream has been reset so we don't send lots of them.
  519. stream_reset(State=#state{socket=Socket, transport=Transport}, StreamID,
  520. StreamError={internal_error, _, _}) ->
  521. Transport:send(Socket, cow_http2:rst_stream(StreamID, internal_error)),
  522. stream_terminate(State, StreamID, StreamError);
  523. stream_reset(State=#state{socket=Socket, transport=Transport}, StreamID,
  524. StreamError={stream_error, Reason, _}) ->
  525. Transport:send(Socket, cow_http2:rst_stream(StreamID, Reason)),
  526. stream_terminate(State, StreamID, StreamError).
  527. stream_terminate(State=#state{socket=Socket, transport=Transport, handler=Handler,
  528. streams=Streams0, children=Children0, encode_state=EncodeState0}, StreamID, Reason) ->
  529. case lists:keytake(StreamID, #stream.id, Streams0) of
  530. {value, #stream{state=StreamState, local=idle}, Streams} when Reason =:= normal ->
  531. Headers = #{<<":status">> => <<"204">>},
  532. {HeaderBlock, EncodeState} = headers_encode(Headers, EncodeState0),
  533. Transport:send(Socket, cow_http2:headers(StreamID, fin, HeaderBlock)),
  534. stream_call_terminate(StreamID, Reason, Handler, StreamState),
  535. Children = stream_terminate_children(Children0, StreamID, []),
  536. State#state{streams=Streams, children=Children, encode_state=EncodeState};
  537. {value, #stream{state=StreamState, local=nofin}, Streams} when Reason =:= normal ->
  538. Transport:send(Socket, cow_http2:data(StreamID, fin, <<>>)),
  539. stream_call_terminate(StreamID, Reason, Handler, StreamState),
  540. Children = stream_terminate_children(Children0, StreamID, []),
  541. State#state{streams=Streams, children=Children};
  542. {value, #stream{state=StreamState}, Streams} ->
  543. stream_call_terminate(StreamID, Reason, Handler, StreamState),
  544. Children = stream_terminate_children(Children0, StreamID, []),
  545. State#state{streams=Streams, children=Children};
  546. false ->
  547. %% @todo Unknown stream. Not sure what to do here. Check again once all
  548. %% terminate calls have been written.
  549. State
  550. end.
  551. stream_call_terminate(StreamID, Reason, Handler, StreamState) ->
  552. try
  553. Handler:terminate(StreamID, Reason, StreamState),
  554. ok
  555. catch Class:Reason ->
  556. error_logger:error_msg("Exception occurred in ~s:terminate(~p, ~p, ~p) with reason ~p:~p.",
  557. [Handler, StreamID, Reason, StreamState, Class, Reason])
  558. end.
  559. stream_terminate_children([], _, Acc) ->
  560. Acc;
  561. stream_terminate_children([{Pid, StreamID}|Tail], StreamID, Acc) ->
  562. exit(Pid, kill),
  563. stream_terminate_children(Tail, StreamID, Acc);
  564. stream_terminate_children([Child|Tail], StreamID, Acc) ->
  565. stream_terminate_children(Tail, StreamID, [Child|Acc]).
  566. %% Headers encode/decode.
  567. headers_decode(HeaderBlock, DecodeState0) ->
  568. {Headers, DecodeState} = cow_hpack:decode(HeaderBlock, DecodeState0),
  569. {maps:from_list(Headers), DecodeState}.
  570. %% @todo We will need to special-case the set-cookie header here.
  571. headers_encode(Headers0, EncodeState) ->
  572. Headers = maps:to_list(Headers0),
  573. cow_hpack:encode(Headers, EncodeState).
  574. %% System callbacks.
  575. -spec system_continue(_, _, #state{}) -> ok.
  576. system_continue(_, _, {State, Buffer}) ->
  577. loop(State, Buffer).
  578. -spec system_terminate(any(), _, _, _) -> no_return().
  579. system_terminate(Reason, _, _, _) ->
  580. exit(Reason).
  581. -spec system_code_change(Misc, _, _, _) -> {ok, Misc} when Misc::{#state{}, binary()}.
  582. system_code_change(Misc, _, _, _) ->
  583. {ok, Misc}.