cowboy_http2.erl 50 KB

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