cowboy_http2.erl 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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. %% Error responses are sent only if a response wasn't sent already.
  344. commands(State, Stream=#stream{local=idle}, [{error_response, StatusCode, Headers, Body}|Tail]) ->
  345. commands(State, Stream, [{response, StatusCode, Headers, Body}|Tail]);
  346. commands(State, Stream, [{error_response, _, _, _}|Tail]) ->
  347. commands(State, Stream, Tail);
  348. %% Send response headers.
  349. %%
  350. %% @todo Kill the stream if it sent a response when one has already been sent.
  351. %% @todo Keep IsFin in the state.
  352. %% @todo Same two things above apply to DATA, possibly promise too.
  353. commands(State=#state{socket=Socket, transport=Transport, encode_state=EncodeState0},
  354. Stream=#stream{id=StreamID, local=idle}, [{response, StatusCode, Headers0, Body}|Tail]) ->
  355. Headers = Headers0#{<<":status">> => status(StatusCode)},
  356. {HeaderBlock, EncodeState} = headers_encode(Headers, EncodeState0),
  357. case Body of
  358. <<>> ->
  359. Transport:send(Socket, cow_http2:headers(StreamID, fin, HeaderBlock)),
  360. commands(State#state{encode_state=EncodeState}, Stream#stream{local=fin}, Tail);
  361. {sendfile, O, B, P} ->
  362. Transport:send(Socket, cow_http2:headers(StreamID, nofin, HeaderBlock)),
  363. commands(State#state{encode_state=EncodeState}, Stream#stream{local=nofin},
  364. [{sendfile, fin, O, B, P}|Tail]);
  365. _ ->
  366. Transport:send(Socket, cow_http2:headers(StreamID, nofin, HeaderBlock)),
  367. %% @todo 16384 is the default SETTINGS_MAX_FRAME_SIZE.
  368. %% Use the length set by the server instead, if any.
  369. %% @todo Would be better if we didn't have to convert to binary.
  370. send_data(Socket, Transport, StreamID, fin, iolist_to_binary(Body), 16384),
  371. commands(State#state{encode_state=EncodeState}, Stream#stream{local=fin}, Tail)
  372. end;
  373. %% @todo response when local!=idle
  374. %% Send response headers and initiate chunked encoding.
  375. commands(State=#state{socket=Socket, transport=Transport, encode_state=EncodeState0},
  376. Stream=#stream{id=StreamID, local=idle}, [{headers, StatusCode, Headers0}|Tail]) ->
  377. Headers = Headers0#{<<":status">> => status(StatusCode)},
  378. {HeaderBlock, EncodeState} = headers_encode(Headers, EncodeState0),
  379. Transport:send(Socket, cow_http2:headers(StreamID, nofin, HeaderBlock)),
  380. commands(State#state{encode_state=EncodeState}, Stream#stream{local=nofin}, Tail);
  381. %% @todo headers when local!=idle
  382. %% Send a response body chunk.
  383. %%
  384. %% @todo WINDOW_UPDATE stuff require us to buffer some data.
  385. %%
  386. %% When the body is sent using sendfile, the current solution is not
  387. %% very good. The body could be too large, blocking the connection.
  388. %% Also sendfile technically only works over TCP, so it's not that
  389. %% useful for HTTP/2. At the very least the sendfile call should be
  390. %% split into multiple calls and flow control should be used to make
  391. %% sure we only send as fast as the client can receive and don't block
  392. %% anything.
  393. commands(State=#state{socket=Socket, transport=Transport}, Stream=#stream{id=StreamID, local=nofin},
  394. [{data, IsFin, Data}|Tail]) ->
  395. Transport:send(Socket, cow_http2:data(StreamID, IsFin, Data)),
  396. commands(State, Stream#stream{local=IsFin}, Tail);
  397. %% @todo data when local!=nofin
  398. %% Send a file.
  399. %%
  400. %% @todo This implementation is terrible. A good implementation would
  401. %% need to check that Bytes is exact (or we need to document that we
  402. %% trust it to be exact), and would need to send the file asynchronously
  403. %% in many data frames. Perhaps a sendfile call should result in a
  404. %% process being created specifically for this purpose. Or perhaps
  405. %% the protocol should be "dumb" and the stream handler be the one
  406. %% to ensure the file is sent in chunks (which would require a better
  407. %% flow control at the stream handler level). One thing for sure, the
  408. %% implementation necessarily varies between HTTP/1.1 and HTTP/2.
  409. commands(State=#state{socket=Socket, transport=Transport}, Stream=#stream{id=StreamID, local=nofin},
  410. [{sendfile, IsFin, Offset, Bytes, Path}|Tail]) ->
  411. Transport:send(Socket, cow_http2:data_header(StreamID, IsFin, Bytes)),
  412. Transport:sendfile(Socket, Path, Offset, Bytes),
  413. commands(State, Stream#stream{local=IsFin}, Tail);
  414. %% @todo sendfile when local!=nofin
  415. %% Send a push promise.
  416. %%
  417. %% @todo We need to keep track of what promises we made so that we don't
  418. %% end up with an infinite loop of promises.
  419. commands(State0=#state{socket=Socket, transport=Transport, server_streamid=PromisedStreamID,
  420. encode_state=EncodeState0}, Stream=#stream{id=StreamID},
  421. [{push, Method, Scheme, Host, Port, Path, Qs, Headers0}|Tail]) ->
  422. Authority = case {Scheme, Port} of
  423. {<<"http">>, 80} -> Host;
  424. {<<"https">>, 443} -> Host;
  425. _ -> [Host, $:, integer_to_binary(Port)]
  426. end,
  427. PathWithQs = case Qs of
  428. <<>> -> Path;
  429. _ -> [Path, $?, Qs]
  430. end,
  431. Headers = Headers0#{<<":method">> => Method,
  432. <<":scheme">> => Scheme,
  433. <<":authority">> => Authority,
  434. <<":path">> => PathWithQs},
  435. {HeaderBlock, EncodeState} = headers_encode(Headers, EncodeState0),
  436. Transport:send(Socket, cow_http2:push_promise(StreamID, PromisedStreamID, HeaderBlock)),
  437. %% @todo iolist_to_binary(HeaderBlock) isn't optimal. Need a shortcut.
  438. State = stream_init(State0#state{server_streamid=PromisedStreamID + 2, encode_state=EncodeState},
  439. PromisedStreamID, fin, iolist_to_binary(HeaderBlock)),
  440. commands(State, Stream, Tail);
  441. %% @todo Update the flow control state.
  442. commands(State, Stream, [{flow, _Size}|Tail]) ->
  443. commands(State, Stream, Tail);
  444. %% Supervise a child process.
  445. commands(State=#state{children=Children}, Stream=#stream{id=StreamID},
  446. [{spawn, Pid, _Shutdown}|Tail]) -> %% @todo Shutdown
  447. commands(State#state{children=[{Pid, StreamID}|Children]}, Stream, Tail);
  448. %% Error handling.
  449. commands(State, Stream=#stream{id=StreamID}, [Error = {internal_error, _, _}|_Tail]) ->
  450. %% @todo Do we want to run the commands after an internal_error?
  451. %% @todo Do we even allow commands after?
  452. %% @todo Only reset when the stream still exists.
  453. stream_reset(after_commands(State, Stream), StreamID, Error);
  454. %% Upgrade to a new protocol.
  455. %%
  456. %% @todo Implementation.
  457. %% @todo Can only upgrade if: there are no other streams and there are no children left alive.
  458. %% @todo For HTTP/1.1 we should reject upgrading if pipelining is used.
  459. commands(State, Stream, [{upgrade, _Mod, _ModState}]) ->
  460. commands(State, Stream, []);
  461. commands(State, Stream, [{upgrade, _Mod, _ModState}|Tail]) ->
  462. %% @todo This is an error. Not sure what to do here yet.
  463. commands(State, Stream, Tail);
  464. commands(State, Stream=#stream{id=StreamID}, [stop|_Tail]) ->
  465. %% @todo Do we want to run the commands after a stop?
  466. %% @todo Do we even allow commands after?
  467. stream_terminate(after_commands(State, Stream), StreamID, normal).
  468. after_commands(State=#state{streams=Streams0}, Stream=#stream{id=StreamID}) ->
  469. Streams = lists:keystore(StreamID, #stream.id, Streams0, Stream),
  470. State#state{streams=Streams}.
  471. status(Status) when is_integer(Status) ->
  472. integer_to_binary(Status);
  473. status(<< H, T, U, _/bits >>) when H >= $1, H =< $9, T >= $0, T =< $9, U >= $0, U =< $9 ->
  474. << H, T, U >>.
  475. %% This same function is found in gun_http2.
  476. send_data(Socket, Transport, StreamID, IsFin, Data, Length) ->
  477. if
  478. Length < byte_size(Data) ->
  479. << Payload:Length/binary, Rest/bits >> = Data,
  480. Transport:send(Socket, cow_http2:data(StreamID, nofin, Payload)),
  481. send_data(Socket, Transport, StreamID, IsFin, Rest, Length);
  482. true ->
  483. Transport:send(Socket, cow_http2:data(StreamID, IsFin, Data))
  484. end.
  485. terminate(#state{socket=Socket, transport=Transport, handler=Handler,
  486. streams=Streams, children=Children}, Reason) ->
  487. %% @todo Send GOAWAY frame; need to keep track of last good stream id; how?
  488. terminate_all_streams(Streams, Reason, Handler, Children),
  489. Transport:close(Socket),
  490. exit({shutdown, Reason}).
  491. terminate_all_streams([], _, _, []) ->
  492. ok;
  493. terminate_all_streams([#stream{id=StreamID, state=StreamState}|Tail], Reason, Handler, Children0) ->
  494. stream_call_terminate(StreamID, Reason, Handler, StreamState),
  495. Children = stream_terminate_children(Children0, StreamID, []),
  496. terminate_all_streams(Tail, Reason, Handler, Children).
  497. %% Stream functions.
  498. stream_init(State0=#state{ref=Ref, socket=Socket, transport=Transport, peer=Peer, decode_state=DecodeState0},
  499. StreamID, IsFin, HeaderBlock) ->
  500. %% @todo Add clause for CONNECT requests (no scheme/path).
  501. try headers_decode(HeaderBlock, DecodeState0) of
  502. {Headers0=#{
  503. <<":method">> := Method,
  504. <<":scheme">> := Scheme,
  505. <<":authority">> := Authority,
  506. <<":path">> := PathWithQs}, DecodeState} ->
  507. State = State0#state{decode_state=DecodeState},
  508. Headers = maps:without([<<":method">>, <<":scheme">>, <<":authority">>, <<":path">>], Headers0),
  509. BodyLength = case Headers of
  510. _ when IsFin =:= fin ->
  511. 0;
  512. #{<<"content-length">> := <<"0">>} ->
  513. 0;
  514. #{<<"content-length">> := BinLength} ->
  515. Length = try
  516. cow_http_hd:parse_content_length(BinLength)
  517. catch _:_ ->
  518. terminate(State0, {stream_error, StreamID, protocol_error,
  519. ''}) %% @todo
  520. %% @todo Err should terminate here...
  521. end,
  522. Length;
  523. _ ->
  524. undefined
  525. end,
  526. {Host, Port} = cow_http_hd:parse_host(Authority),
  527. {Path, Qs} = cow_http:parse_fullpath(PathWithQs),
  528. Req = #{
  529. ref => Ref,
  530. pid => self(),
  531. streamid => StreamID,
  532. peer => Peer,
  533. method => Method,
  534. scheme => Scheme,
  535. host => Host,
  536. port => Port,
  537. path => Path,
  538. qs => Qs,
  539. version => 'HTTP/2',
  540. headers => Headers,
  541. has_body => IsFin =:= nofin,
  542. body_length => BodyLength
  543. %% @todo multipart? keep state separate
  544. %% meta values (cowboy_websocket, cowboy_rest)
  545. },
  546. stream_handler_init(State, StreamID, IsFin, Req);
  547. {_, DecodeState} ->
  548. Transport:send(Socket, cow_http2:rst_stream(StreamID, protocol_error)),
  549. State0#state{decode_state=DecodeState}
  550. catch _:_ ->
  551. terminate(State0, {connection_error, compression_error,
  552. 'Error while trying to decode HPACK-encoded header block. (RFC7540 4.3)'})
  553. end.
  554. stream_handler_init(State=#state{handler=Handler, opts=Opts}, StreamID, IsFin, Req) ->
  555. try Handler:init(StreamID, Req, Opts) of
  556. {Commands, StreamState} ->
  557. commands(State, #stream{id=StreamID, state=StreamState, remote=IsFin}, Commands)
  558. catch Class:Reason ->
  559. error_logger:error_msg("Exception occurred in ~s:init(~p, ~p, ~p) "
  560. "with reason ~p:~p.",
  561. [Handler, StreamID, IsFin, Req, Class, Reason]),
  562. stream_reset(State, StreamID, {internal_error, {Class, Reason},
  563. 'Exception occurred in StreamHandler:init/7 call.'}) %% @todo Check final arity.
  564. end.
  565. %% @todo We might need to keep track of which stream has been reset so we don't send lots of them.
  566. stream_reset(State=#state{socket=Socket, transport=Transport}, StreamID,
  567. StreamError={internal_error, _, _}) ->
  568. Transport:send(Socket, cow_http2:rst_stream(StreamID, internal_error)),
  569. stream_terminate(State, StreamID, StreamError);
  570. stream_reset(State=#state{socket=Socket, transport=Transport}, StreamID,
  571. StreamError={stream_error, Reason, _}) ->
  572. Transport:send(Socket, cow_http2:rst_stream(StreamID, Reason)),
  573. stream_terminate(State, StreamID, StreamError).
  574. stream_terminate(State=#state{socket=Socket, transport=Transport, handler=Handler,
  575. streams=Streams0, children=Children0, encode_state=EncodeState0}, StreamID, Reason) ->
  576. case lists:keytake(StreamID, #stream.id, Streams0) of
  577. {value, #stream{state=StreamState, local=idle}, Streams} when Reason =:= normal ->
  578. Headers = #{<<":status">> => <<"204">>},
  579. {HeaderBlock, EncodeState} = headers_encode(Headers, EncodeState0),
  580. Transport:send(Socket, cow_http2:headers(StreamID, fin, HeaderBlock)),
  581. stream_call_terminate(StreamID, Reason, Handler, StreamState),
  582. Children = stream_terminate_children(Children0, StreamID, []),
  583. State#state{streams=Streams, children=Children, encode_state=EncodeState};
  584. {value, #stream{state=StreamState, local=nofin}, Streams} when Reason =:= normal ->
  585. Transport:send(Socket, cow_http2:data(StreamID, fin, <<>>)),
  586. stream_call_terminate(StreamID, Reason, Handler, StreamState),
  587. Children = stream_terminate_children(Children0, StreamID, []),
  588. State#state{streams=Streams, children=Children};
  589. {value, #stream{state=StreamState}, Streams} ->
  590. stream_call_terminate(StreamID, Reason, Handler, StreamState),
  591. Children = stream_terminate_children(Children0, StreamID, []),
  592. State#state{streams=Streams, children=Children};
  593. false ->
  594. %% @todo Unknown stream. Not sure what to do here. Check again once all
  595. %% terminate calls have been written.
  596. State
  597. end.
  598. stream_call_terminate(StreamID, Reason, Handler, StreamState) ->
  599. try
  600. Handler:terminate(StreamID, Reason, StreamState),
  601. ok
  602. catch Class:Reason ->
  603. error_logger:error_msg("Exception occurred in ~s:terminate(~p, ~p, ~p) with reason ~p:~p.",
  604. [Handler, StreamID, Reason, StreamState, Class, Reason])
  605. end.
  606. stream_terminate_children([], _, Acc) ->
  607. Acc;
  608. stream_terminate_children([{Pid, StreamID}|Tail], StreamID, Acc) ->
  609. exit(Pid, kill),
  610. stream_terminate_children(Tail, StreamID, Acc);
  611. stream_terminate_children([Child|Tail], StreamID, Acc) ->
  612. stream_terminate_children(Tail, StreamID, [Child|Acc]).
  613. %% Headers encode/decode.
  614. headers_decode(HeaderBlock, DecodeState0) ->
  615. {Headers, DecodeState} = cow_hpack:decode(HeaderBlock, DecodeState0),
  616. {headers_to_map(Headers, #{}), DecodeState}.
  617. %% This function is necessary to properly handle duplicate headers
  618. %% and the special-case cookie header.
  619. headers_to_map([], Acc) ->
  620. Acc;
  621. headers_to_map([{Name, Value}|Tail], Acc0) ->
  622. Acc = case Acc0 of
  623. %% The cookie header does not use proper HTTP header lists.
  624. #{Name := Value0} when Name =:= <<"cookie">> -> Acc0#{Name => << Value0/binary, "; ", Value/binary >>};
  625. #{Name := Value0} -> Acc0#{Name => << Value0/binary, ", ", Value/binary >>};
  626. _ -> Acc0#{Name => Value}
  627. end,
  628. headers_to_map(Tail, Acc).
  629. %% The set-cookie header is special; we can only send one cookie per header.
  630. headers_encode(Headers0=#{<<"set-cookie">> := SetCookies}, EncodeState) ->
  631. Headers1 = maps:to_list(maps:remove(<<"set-cookie">>, Headers0)),
  632. Headers = Headers1 ++ [{<<"set-cookie">>, Value} || Value <- SetCookies],
  633. cow_hpack:encode(Headers, EncodeState);
  634. headers_encode(Headers0, EncodeState) ->
  635. Headers = maps:to_list(Headers0),
  636. cow_hpack:encode(Headers, EncodeState).
  637. %% System callbacks.
  638. -spec system_continue(_, _, #state{}) -> ok.
  639. system_continue(_, _, {State, Buffer}) ->
  640. loop(State, Buffer).
  641. -spec system_terminate(any(), _, _, _) -> no_return().
  642. system_terminate(Reason, _, _, _) ->
  643. exit(Reason).
  644. -spec system_code_change(Misc, _, _, _) -> {ok, Misc} when Misc::{#state{}, binary()}.
  645. system_code_change(Misc, _, _, _) ->
  646. {ok, Misc}.