123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786 |
- %% Copyright (c) 2015-2017, Loïc Hoguin <essen@ninenines.eu>
- %%
- %% Permission to use, copy, modify, and/or distribute this software for any
- %% purpose with or without fee is hereby granted, provided that the above
- %% copyright notice and this permission notice appear in all copies.
- %%
- %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- -module(cowboy_http2).
- -ifdef(OTP_RELEASE).
- -compile({nowarn_deprecated_function, [{erlang, get_stacktrace, 0}]}).
- -endif.
- -export([init/5]).
- -export([init/9]).
- -export([init/11]).
- -export([system_continue/3]).
- -export([system_terminate/4]).
- -export([system_code_change/4]).
- -type opts() :: #{
- connection_type => worker | supervisor,
- enable_connect_protocol => boolean(),
- env => cowboy_middleware:env(),
- inactivity_timeout => timeout(),
- initial_connection_window_size => 65535..16#7fffffff,
- initial_stream_window_size => 0..16#7fffffff,
- logger => module(),
- max_concurrent_streams => non_neg_integer() | infinity,
- max_decode_table_size => non_neg_integer(),
- max_encode_table_size => non_neg_integer(),
- max_frame_size_received => 16384..16777215,
- max_frame_size_sent => 16384..16777215 | infinity,
- middlewares => [module()],
- preface_timeout => timeout(),
- settings_timeout => timeout(),
- shutdown_timeout => timeout(),
- stream_handlers => [module()]
- }.
- -export_type([opts/0]).
- -record(state, {
- parent = undefined :: pid(),
- ref :: ranch:ref(),
- socket = undefined :: inet:socket(),
- transport :: module(),
- opts = #{} :: opts(),
- %% Remote address and port for the connection.
- peer = undefined :: {inet:ip_address(), inet:port_number()},
- %% Local address and port for the connection.
- sock = undefined :: {inet:ip_address(), inet:port_number()},
- %% Client certificate (TLS only).
- cert :: undefined | binary(),
- %% HTTP/2 state machine.
- http2_init :: sequence | settings | upgrade | complete,
- http2_machine :: cow_http2_machine:http2_machine(),
- %% Currently active HTTP/2 streams. Streams may be initiated either
- %% by the client or by the server through PUSH_PROMISE frames.
- streams = #{} :: #{cow_http2:streamid() => {running | stopping, {module, any()}}},
- %% Streams can spawn zero or more children which are then managed
- %% by this module if operating as a supervisor.
- children = cowboy_children:init() :: cowboy_children:children()
- }).
- -spec init(pid(), ranch:ref(), inet:socket(), module(), cowboy:opts()) -> ok.
- init(Parent, Ref, Socket, Transport, Opts) ->
- Peer0 = Transport:peername(Socket),
- Sock0 = Transport:sockname(Socket),
- Cert1 = case Transport:name() of
- ssl ->
- case ssl:peercert(Socket) of
- {error, no_peercert} ->
- {ok, undefined};
- Cert0 ->
- Cert0
- end;
- _ ->
- {ok, undefined}
- end,
- case {Peer0, Sock0, Cert1} of
- {{ok, Peer}, {ok, Sock}, {ok, Cert}} ->
- init(Parent, Ref, Socket, Transport, Opts, Peer, Sock, Cert, <<>>);
- {{error, Reason}, _, _} ->
- terminate(undefined, {socket_error, Reason,
- 'A socket error occurred when retrieving the peer name.'});
- {_, {error, Reason}, _} ->
- terminate(undefined, {socket_error, Reason,
- 'A socket error occurred when retrieving the sock name.'});
- {_, _, {error, Reason}} ->
- terminate(undefined, {socket_error, Reason,
- 'A socket error occurred when retrieving the client TLS certificate.'})
- end.
- -spec init(pid(), ranch:ref(), inet:socket(), module(), cowboy:opts(),
- {inet:ip_address(), inet:port_number()}, {inet:ip_address(), inet:port_number()},
- binary() | undefined, binary()) -> ok.
- init(Parent, Ref, Socket, Transport, Opts, Peer, Sock, Cert, Buffer) ->
- {ok, Preface, HTTP2Machine} = cow_http2_machine:init(server, Opts),
- State = #state{parent=Parent, ref=Ref, socket=Socket,
- transport=Transport, opts=Opts, peer=Peer, sock=Sock, cert=Cert,
- http2_init=sequence, http2_machine=HTTP2Machine},
- Transport:send(Socket, Preface),
- case Buffer of
- <<>> -> before_loop(State, Buffer);
- _ -> parse(State, Buffer)
- end.
- %% @todo Add an argument for the request body.
- -spec init(pid(), ranch:ref(), inet:socket(), module(), cowboy:opts(),
- {inet:ip_address(), inet:port_number()}, {inet:ip_address(), inet:port_number()},
- binary() | undefined, binary(), map() | undefined, cowboy_req:req()) -> ok.
- init(Parent, Ref, Socket, Transport, Opts, Peer, Sock, Cert, Buffer,
- _Settings, Req=#{method := Method}) ->
- {ok, Preface, HTTP2Machine0} = cow_http2_machine:init(server, Opts),
- {ok, StreamID, HTTP2Machine}
- = cow_http2_machine:init_upgrade_stream(Method, HTTP2Machine0),
- State0 = #state{parent=Parent, ref=Ref, socket=Socket,
- transport=Transport, opts=Opts, peer=Peer, sock=Sock, cert=Cert,
- http2_init=upgrade, http2_machine=HTTP2Machine},
- State1 = headers_frame(State0#state{
- http2_machine=HTTP2Machine}, StreamID, Req),
- %% We assume that the upgrade will be applied. A stream handler
- %% must not prevent the normal operations of the server.
- State2 = info(State1, 1, {switch_protocol, #{
- <<"connection">> => <<"Upgrade">>,
- <<"upgrade">> => <<"h2c">>
- }, ?MODULE, undefined}), %% @todo undefined or #{}?
- State = State2#state{http2_init=sequence},
- Transport:send(Socket, Preface),
- case Buffer of
- <<>> -> before_loop(State, Buffer);
- _ -> parse(State, Buffer)
- end.
- %% @todo Add the timeout for last time since we heard of connection.
- before_loop(State, Buffer) ->
- loop(State, Buffer).
- loop(State=#state{parent=Parent, socket=Socket, transport=Transport,
- opts=Opts, children=Children}, Buffer) ->
- Transport:setopts(Socket, [{active, once}]),
- {OK, Closed, Error} = Transport:messages(),
- InactivityTimeout = maps:get(inactivity_timeout, Opts, 300000),
- receive
- %% Socket messages.
- {OK, Socket, Data} ->
- parse(State, << Buffer/binary, Data/binary >>);
- {Closed, Socket} ->
- terminate(State, {socket_error, closed, 'The socket has been closed.'});
- {Error, Socket, Reason} ->
- terminate(State, {socket_error, Reason, 'An error has occurred on the socket.'});
- %% System messages.
- {'EXIT', Parent, Reason} ->
- terminate(State, {stop, {exit, Reason}, 'Parent process terminated.'});
- {system, From, Request} ->
- sys:handle_system_msg(Request, From, Parent, ?MODULE, [], {State, Buffer});
- %% Timeouts.
- {timeout, Ref, {shutdown, Pid}} ->
- cowboy_children:shutdown_timeout(Children, Ref, Pid),
- loop(State, Buffer);
- {timeout, TRef, {cow_http2_machine, Name}} ->
- loop(timeout(State, Name, TRef), Buffer);
- %% Messages pertaining to a stream.
- {{Pid, StreamID}, Msg} when Pid =:= self() ->
- loop(info(State, StreamID, Msg), Buffer);
- %% Exit signal from children.
- Msg = {'EXIT', Pid, _} ->
- loop(down(State, Pid, Msg), Buffer);
- %% Calls from supervisor module.
- {'$gen_call', From, Call} ->
- cowboy_children:handle_supervisor_call(Call, From, Children, ?MODULE),
- loop(State, Buffer);
- Msg ->
- cowboy:log(warning, "Received stray message ~p.", [Msg], Opts),
- loop(State, Buffer)
- after InactivityTimeout ->
- terminate(State, {internal_error, timeout, 'No message or data received before timeout.'})
- end.
- %% HTTP/2 protocol parsing.
- parse(State=#state{http2_init=sequence}, Data) ->
- case cow_http2:parse_sequence(Data) of
- {ok, Rest} ->
- parse(State#state{http2_init=settings}, Rest);
- more ->
- before_loop(State, Data);
- Error = {connection_error, _, _} ->
- terminate(State, Error)
- end;
- parse(State=#state{http2_machine=HTTP2Machine}, Data) ->
- MaxFrameSize = cow_http2_machine:get_local_setting(max_frame_size, HTTP2Machine),
- case cow_http2:parse(Data, MaxFrameSize) of
- {ok, Frame, Rest} ->
- parse(frame(State, Frame), Rest);
- {ignore, Rest} ->
- parse(ignored_frame(State), Rest);
- {stream_error, StreamID, Reason, Human, Rest} ->
- parse(stream_reset(State, StreamID, {stream_error, Reason, Human}), Rest);
- Error = {connection_error, _, _} ->
- terminate(State, Error);
- more ->
- before_loop(State, Data)
- end.
- %% Frames received.
- frame(State=#state{http2_machine=HTTP2Machine0}, Frame) ->
- case cow_http2_machine:frame(Frame, HTTP2Machine0) of
- {ok, HTTP2Machine} ->
- maybe_ack(State#state{http2_machine=HTTP2Machine}, Frame);
- {ok, {data, StreamID, IsFin, Data}, HTTP2Machine} ->
- data_frame(State#state{http2_machine=HTTP2Machine}, StreamID, IsFin, Data);
- {ok, {headers, StreamID, IsFin, Headers, PseudoHeaders, BodyLen}, HTTP2Machine} ->
- headers_frame(State#state{http2_machine=HTTP2Machine},
- StreamID, IsFin, Headers, PseudoHeaders, BodyLen);
- {ok, {trailers, _StreamID, _Trailers}, HTTP2Machine} ->
- %% @todo Propagate trailers.
- State#state{http2_machine=HTTP2Machine};
- {ok, {rst_stream, StreamID, Reason}, HTTP2Machine} ->
- rst_stream_frame(State#state{http2_machine=HTTP2Machine}, StreamID, Reason);
- {ok, Frame={goaway, _StreamID, _Reason, _Data}, HTTP2Machine} ->
- terminate(State#state{http2_machine=HTTP2Machine},
- {stop, Frame, 'Client is going away.'});
- {send, SendData, HTTP2Machine} ->
- send_data(maybe_ack(State#state{http2_machine=HTTP2Machine}, Frame), SendData);
- {error, {stream_error, StreamID, Reason, Human}, HTTP2Machine} ->
- stream_reset(State#state{http2_machine=HTTP2Machine},
- StreamID, {stream_error, Reason, Human});
- {error, Error={connection_error, _, _}, HTTP2Machine} ->
- terminate(State#state{http2_machine=HTTP2Machine}, Error)
- end.
- %% We use this opportunity to mark the HTTP/2 initialization
- %% as complete if we were still waiting for a SETTINGS frame.
- maybe_ack(State=#state{http2_init=settings}, Frame) ->
- maybe_ack(State#state{http2_init=complete}, Frame);
- maybe_ack(State=#state{socket=Socket, transport=Transport}, Frame) ->
- case Frame of
- {settings, _} -> Transport:send(Socket, cow_http2:settings_ack());
- {ping, Opaque} -> Transport:send(Socket, cow_http2:ping_ack(Opaque));
- _ -> ok
- end,
- State.
- data_frame(State=#state{opts=Opts, streams=Streams}, StreamID, IsFin, Data) ->
- case Streams of
- #{StreamID := {running, StreamState0}} ->
- try cowboy_stream:data(StreamID, IsFin, Data, StreamState0) of
- {Commands, StreamState} ->
- commands(State#state{streams=Streams#{StreamID => {running, StreamState}}},
- StreamID, Commands)
- catch Class:Exception ->
- cowboy:log(cowboy_stream:make_error_log(data,
- [StreamID, IsFin, Data, StreamState0],
- Class, Exception, erlang:get_stacktrace()), Opts),
- stream_reset(State, StreamID, {internal_error, {Class, Exception},
- 'Unhandled exception in cowboy_stream:data/4.'})
- end;
- %% We ignore DATA frames for streams that are stopping.
- #{} ->
- State
- end.
- headers_frame(State, StreamID, IsFin, Headers,
- PseudoHeaders=#{method := <<"CONNECT">>}, _)
- when map_size(PseudoHeaders) =:= 2 ->
- early_error(State, StreamID, IsFin, Headers, PseudoHeaders, 501,
- 'The CONNECT method is currently not implemented. (RFC7231 4.3.6)');
- headers_frame(State, StreamID, IsFin, Headers,
- PseudoHeaders=#{method := <<"TRACE">>}, _) ->
- early_error(State, StreamID, IsFin, Headers, PseudoHeaders, 501,
- 'The TRACE method is currently not implemented. (RFC7231 4.3.8)');
- headers_frame(State=#state{ref=Ref, peer=Peer, sock=Sock, cert=Cert},
- StreamID, IsFin, Headers, PseudoHeaders=#{method := Method, scheme := Scheme,
- authority := Authority, path := PathWithQs}, BodyLen) ->
- try cow_http_hd:parse_host(Authority) of
- {Host, Port0} ->
- Port = ensure_port(Scheme, Port0),
- try cow_http:parse_fullpath(PathWithQs) of
- {<<>>, _} ->
- malformed_request(State, StreamID,
- 'The path component must not be empty. (RFC7540 8.1.2.3)');
- {Path, Qs} ->
- Req0 = #{
- ref => Ref,
- pid => self(),
- streamid => StreamID,
- peer => Peer,
- sock => Sock,
- cert => Cert,
- method => Method,
- scheme => Scheme,
- host => Host,
- port => Port,
- path => Path,
- qs => Qs,
- version => 'HTTP/2',
- headers => headers_to_map(Headers, #{}),
- has_body => IsFin =:= nofin,
- body_length => BodyLen
- },
- %% We add the protocol information for extended CONNECTs.
- Req = case PseudoHeaders of
- #{protocol := Protocol} ->
- Req0#{protocol => Protocol};
- _ ->
- Req0
- end,
- headers_frame(State, StreamID, Req)
- catch _:_ ->
- malformed_request(State, StreamID,
- 'The :path pseudo-header is invalid. (RFC7540 8.1.2.3)')
- end
- catch _:_ ->
- malformed_request(State, StreamID,
- 'The :authority pseudo-header is invalid. (RFC7540 8.1.2.3)')
- end.
- ensure_port(<<"http">>, undefined) -> 80;
- ensure_port(<<"https">>, undefined) -> 443;
- ensure_port(_, Port) -> Port.
- %% This function is necessary to properly handle duplicate headers
- %% and the special-case cookie header.
- headers_to_map([], Acc) ->
- Acc;
- headers_to_map([{Name, Value}|Tail], Acc0) ->
- Acc = case Acc0 of
- %% The cookie header does not use proper HTTP header lists.
- #{Name := Value0} when Name =:= <<"cookie">> ->
- Acc0#{Name => << Value0/binary, "; ", Value/binary >>};
- #{Name := Value0} ->
- Acc0#{Name => << Value0/binary, ", ", Value/binary >>};
- _ ->
- Acc0#{Name => Value}
- end,
- headers_to_map(Tail, Acc).
- %% @todo Probably not a very useful function, just use stream_reset.
- malformed_request(State=#state{socket=Socket, transport=Transport,
- http2_machine=HTTP2Machine0}, StreamID, _) ->
- Transport:send(Socket, cow_http2:rst_stream(StreamID, protocol_error)),
- {ok, HTTP2Machine} = cow_http2_machine:reset_stream(StreamID, HTTP2Machine0),
- State#state{http2_machine=HTTP2Machine}.
- headers_frame(State=#state{opts=Opts, streams=Streams}, StreamID, Req) ->
- try cowboy_stream:init(StreamID, Req, Opts) of
- {Commands, StreamState} ->
- commands(State#state{
- streams=Streams#{StreamID => {running, StreamState}}},
- StreamID, Commands)
- catch Class:Exception ->
- cowboy:log(cowboy_stream:make_error_log(init,
- [StreamID, Req, Opts],
- Class, Exception, erlang:get_stacktrace()), Opts),
- stream_reset(State, StreamID, {internal_error, {Class, Exception},
- 'Unhandled exception in cowboy_stream:init/3.'})
- end.
- early_error(State0=#state{ref=Ref, opts=Opts, peer=Peer},
- StreamID, _IsFin, _Headers, #{method := Method},
- StatusCode0, HumanReadable) ->
- %% We automatically terminate the stream but it is not an error
- %% per se (at least not in the first implementation).
- Reason = {stream_error, no_error, HumanReadable},
- %% The partial Req is minimal for now. We only have one case
- %% where it can be called (when a method is completely disabled).
- %% @todo Fill in the other elements.
- PartialReq = #{
- ref => Ref,
- peer => Peer,
- method => Method
- },
- Resp = {response, StatusCode0, RespHeaders0=#{<<"content-length">> => <<"0">>}, <<>>},
- try cowboy_stream:early_error(StreamID, Reason, PartialReq, Resp, Opts) of
- {response, StatusCode, RespHeaders, RespBody} ->
- send_response(State0, StreamID, StatusCode, RespHeaders, RespBody)
- catch Class:Exception ->
- cowboy:log(cowboy_stream:make_error_log(early_error,
- [StreamID, Reason, PartialReq, Resp, Opts],
- Class, Exception, erlang:get_stacktrace()), Opts),
- %% We still need to send an error response, so send what we initially
- %% wanted to send. It's better than nothing.
- send_headers(State0, StreamID, fin, StatusCode0, RespHeaders0)
- end.
- rst_stream_frame(State=#state{streams=Streams0, children=Children0}, StreamID, Reason) ->
- case maps:take(StreamID, Streams0) of
- {{_, StreamState}, Streams} ->
- stream_call_terminate(StreamID, Reason, StreamState, State),
- Children = cowboy_children:shutdown(Children0, StreamID),
- State#state{streams=Streams, children=Children};
- error ->
- State
- end.
- ignored_frame(State=#state{http2_machine=HTTP2Machine0}) ->
- case cow_http2_machine:ignored_frame(HTTP2Machine0) of
- {ok, HTTP2Machine} ->
- State#state{http2_machine=HTTP2Machine};
- {error, Error={connection_error, _, _}, HTTP2Machine} ->
- terminate(State#state{http2_machine=HTTP2Machine}, Error)
- end.
- %% HTTP/2 timeouts.
- timeout(State=#state{http2_machine=HTTP2Machine0}, Name, TRef) ->
- case cow_http2_machine:timeout(Name, TRef, HTTP2Machine0) of
- {ok, HTTP2Machine} ->
- State#state{http2_machine=HTTP2Machine};
- {error, Error={connection_error, _, _}, HTTP2Machine} ->
- terminate(State#state{http2_machine=HTTP2Machine}, Error)
- end.
- %% Erlang messages.
- down(State=#state{opts=Opts, children=Children0}, Pid, Msg) ->
- case cowboy_children:down(Children0, Pid) of
- %% The stream was terminated already.
- {ok, undefined, Children} ->
- State#state{children=Children};
- %% The stream is still running.
- {ok, StreamID, Children} ->
- info(State#state{children=Children}, StreamID, Msg);
- %% The process was unknown.
- error ->
- cowboy:log(warning, "Received EXIT signal ~p for unknown process ~p.~n",
- [Msg, Pid], Opts),
- State
- end.
- info(State=#state{opts=Opts, streams=Streams}, StreamID, Msg) ->
- case Streams of
- #{StreamID := {IsRunning, StreamState0}} ->
- try cowboy_stream:info(StreamID, Msg, StreamState0) of
- {Commands, StreamState} ->
- commands(State#state{streams=Streams#{StreamID => {IsRunning, StreamState}}},
- StreamID, Commands)
- catch Class:Exception ->
- cowboy:log(cowboy_stream:make_error_log(info,
- [StreamID, Msg, StreamState0],
- Class, Exception, erlang:get_stacktrace()), Opts),
- stream_reset(State, StreamID, {internal_error, {Class, Exception},
- 'Unhandled exception in cowboy_stream:info/3.'})
- end;
- _ ->
- cowboy:log(warning, "Received message ~p for unknown or terminated stream ~p.",
- [Msg, StreamID], Opts),
- State
- end.
- %% Stream handler commands.
- %%
- %% @todo Kill the stream if it tries to send a response, headers,
- %% data or push promise when the stream is closed or half-closed.
- commands(State, _, []) ->
- State;
- %% Error responses are sent only if a response wasn't sent already.
- commands(State=#state{http2_machine=HTTP2Machine}, StreamID,
- [{error_response, StatusCode, Headers, Body}|Tail]) ->
- case cow_http2_machine:get_stream_local_state(StreamID, HTTP2Machine) of
- {ok, idle, _} ->
- commands(State, StreamID, [{response, StatusCode, Headers, Body}|Tail]);
- _ ->
- commands(State, StreamID, Tail)
- end;
- %% Send an informational response.
- commands(State0, StreamID, [{inform, StatusCode, Headers}|Tail]) ->
- State = send_headers(State0, StreamID, idle, StatusCode, Headers),
- commands(State, StreamID, Tail);
- %% Send response headers.
- commands(State0, StreamID, [{response, StatusCode, Headers, Body}|Tail]) ->
- State = send_response(State0, StreamID, StatusCode, Headers, Body),
- commands(State, StreamID, Tail);
- %% Send response headers.
- commands(State0, StreamID, [{headers, StatusCode, Headers}|Tail]) ->
- State = send_headers(State0, StreamID, nofin, StatusCode, Headers),
- commands(State, StreamID, Tail);
- %% Send a response body chunk.
- commands(State0, StreamID, [{data, IsFin, Data}|Tail]) ->
- State = maybe_send_data(State0, StreamID, IsFin, Data),
- commands(State, StreamID, Tail);
- %% Send trailers.
- commands(State0, StreamID, [{trailers, Trailers}|Tail]) ->
- State = maybe_send_data(State0, StreamID, fin, {trailers, maps:to_list(Trailers)}),
- commands(State, StreamID, Tail);
- %% Send a file.
- %% @todo Add the sendfile command.
- %commands(State0, Stream0=#stream{local=nofin},
- % [{sendfile, IsFin, Offset, Bytes, Path}|Tail]) ->
- % {State, Stream} = maybe_send_data(State0, Stream0, IsFin, {sendfile, Offset, Bytes, Path}),
- % commands(State, Stream, Tail);
- %% Send a push promise.
- %%
- %% @todo Responses sent as a result of a push_promise request
- %% must not send push_promise frames themselves.
- commands(State0=#state{socket=Socket, transport=Transport, http2_machine=HTTP2Machine0},
- StreamID, [{push, Method, Scheme, Host, Port, Path, Qs, Headers0}|Tail]) ->
- Authority = case {Scheme, Port} of
- {<<"http">>, 80} -> Host;
- {<<"https">>, 443} -> Host;
- _ -> iolist_to_binary([Host, $:, integer_to_binary(Port)])
- end,
- PathWithQs = iolist_to_binary(case Qs of
- <<>> -> Path;
- _ -> [Path, $?, Qs]
- end),
- PseudoHeaders = #{
- method => Method,
- scheme => Scheme,
- authority => Authority,
- path => PathWithQs
- },
- %% We need to make sure the header value is binary before we can
- %% create the Req object, as it expects them to be flat.
- Headers = maps:to_list(maps:map(fun(_, V) -> iolist_to_binary(V) end, Headers0)),
- State = case cow_http2_machine:prepare_push_promise(StreamID, HTTP2Machine0,
- PseudoHeaders, Headers) of
- {ok, PromisedStreamID, HeaderBlock, HTTP2Machine} ->
- Transport:send(Socket, cow_http2:push_promise(
- StreamID, PromisedStreamID, HeaderBlock)),
- headers_frame(State0#state{http2_machine=HTTP2Machine},
- PromisedStreamID, fin, Headers, PseudoHeaders, 0);
- {error, no_push} ->
- State0
- end,
- commands(State, StreamID, Tail);
- commands(State=#state{socket=Socket, transport=Transport, http2_machine=HTTP2Machine0},
- StreamID, [{flow, Size}|Tail]) ->
- Transport:send(Socket, [
- cow_http2:window_update(Size),
- cow_http2:window_update(StreamID, Size)
- ]),
- HTTP2Machine1 = cow_http2_machine:update_window(Size, HTTP2Machine0),
- HTTP2Machine = cow_http2_machine:update_window(StreamID, Size, HTTP2Machine1),
- commands(State#state{http2_machine=HTTP2Machine}, StreamID, Tail);
- %% Supervise a child process.
- commands(State=#state{children=Children}, StreamID, [{spawn, Pid, Shutdown}|Tail]) ->
- commands(State#state{children=cowboy_children:up(Children, Pid, StreamID, Shutdown)},
- StreamID, Tail);
- %% Error handling.
- commands(State, StreamID, [Error = {internal_error, _, _}|_Tail]) ->
- %% @todo Do we want to run the commands after an internal_error?
- %% @todo Do we even allow commands after?
- %% @todo Only reset when the stream still exists.
- stream_reset(State, StreamID, Error);
- %% Upgrade to HTTP/2. This is triggered by cowboy_http2 itself.
- commands(State=#state{socket=Socket, transport=Transport, http2_init=upgrade},
- StreamID, [{switch_protocol, Headers, ?MODULE, _}|Tail]) ->
- %% @todo This 101 response needs to be passed through stream handlers.
- Transport:send(Socket, cow_http:response(101, 'HTTP/1.1', maps:to_list(Headers))),
- commands(State, StreamID, Tail);
- %% Use a different protocol within the stream (CONNECT :protocol).
- %% @todo Make sure we error out when the feature is disabled.
- commands(State0, StreamID, [{switch_protocol, Headers, _Mod, _ModState}|Tail]) ->
- State = info(State0, StreamID, {headers, 200, Headers}),
- commands(State, StreamID, Tail);
- commands(State, StreamID, [stop|_Tail]) ->
- %% @todo Do we want to run the commands after a stop?
- %% @todo Do we even allow commands after?
- stop_stream(State, StreamID);
- %% Log event.
- commands(State=#state{opts=Opts}, StreamID, [Log={log, _, _, _}|Tail]) ->
- cowboy:log(Log, Opts),
- commands(State, StreamID, Tail).
- %% Send the response, trailers or data.
- send_response(State0, StreamID, StatusCode, Headers, Body) ->
- Size = case Body of
- {sendfile, _, Bytes, _} -> Bytes;
- _ -> iolist_size(Body)
- end,
- case Size of
- 0 ->
- State = send_headers(State0, StreamID, fin, StatusCode, Headers),
- maybe_terminate_stream(State, StreamID, fin);
- _ ->
- State = send_headers(State0, StreamID, nofin, StatusCode, Headers),
- maybe_send_data(State, StreamID, fin, Body)
- end.
- send_headers(State=#state{socket=Socket, transport=Transport,
- http2_machine=HTTP2Machine0}, StreamID, IsFin0, StatusCode, Headers) ->
- {ok, IsFin, HeaderBlock, HTTP2Machine}
- = cow_http2_machine:prepare_headers(StreamID, HTTP2Machine0, IsFin0,
- #{status => cow_http:status_to_integer(StatusCode)},
- headers_to_list(Headers)),
- Transport:send(Socket, cow_http2:headers(StreamID, IsFin, HeaderBlock)),
- State#state{http2_machine=HTTP2Machine}.
- %% The set-cookie header is special; we can only send one cookie per header.
- headers_to_list(Headers0=#{<<"set-cookie">> := SetCookies}) ->
- Headers = maps:to_list(maps:remove(<<"set-cookie">>, Headers0)),
- Headers ++ [{<<"set-cookie">>, Value} || Value <- SetCookies];
- headers_to_list(Headers) ->
- maps:to_list(Headers).
- maybe_send_data(State=#state{http2_machine=HTTP2Machine0}, StreamID, IsFin, Data0) ->
- Data = case is_tuple(Data0) of
- false -> {data, Data0};
- true -> Data0
- end,
- case cow_http2_machine:send_or_queue_data(StreamID, HTTP2Machine0, IsFin, Data) of
- {ok, HTTP2Machine} ->
- State#state{http2_machine=HTTP2Machine};
- {send, SendData, HTTP2Machine} ->
- send_data(State#state{http2_machine=HTTP2Machine}, SendData)
- end.
- send_data(State, []) ->
- State;
- send_data(State0, [{StreamID, IsFin, SendData}|Tail]) ->
- State = send_data(State0, StreamID, IsFin, SendData),
- send_data(State, Tail).
- send_data(State0, StreamID, IsFin, [Data]) ->
- State = send_data_frame(State0, StreamID, IsFin, Data),
- maybe_terminate_stream(State, StreamID, IsFin);
- send_data(State0, StreamID, IsFin, [Data|Tail]) ->
- State = send_data_frame(State0, StreamID, nofin, Data),
- send_data(State, StreamID, IsFin, Tail).
- send_data_frame(State=#state{socket=Socket, transport=Transport},
- StreamID, IsFin, {data, Data}) ->
- Transport:send(Socket, cow_http2:data(StreamID, IsFin, Data)),
- State;
- send_data_frame(State=#state{socket=Socket, transport=Transport},
- StreamID, IsFin, {sendfile, Offset, Bytes, Path}) ->
- Transport:send(Socket, cow_http2:data_header(StreamID, IsFin, Bytes)),
- Transport:sendfile(Socket, Path, Offset, Bytes),
- State;
- %% The stream is terminated in cow_http2_machine:prepare_trailers.
- send_data_frame(State=#state{socket=Socket, transport=Transport,
- http2_machine=HTTP2Machine0}, StreamID, nofin, {trailers, Trailers}) ->
- {ok, HeaderBlock, HTTP2Machine}
- = cow_http2_machine:prepare_trailers(StreamID, HTTP2Machine0, Trailers),
- Transport:send(Socket, cow_http2:headers(StreamID, fin, HeaderBlock)),
- State#state{http2_machine=HTTP2Machine}.
- %% Terminate a stream or the connection.
- -spec terminate(#state{}, _) -> no_return().
- terminate(undefined, Reason) ->
- exit({shutdown, Reason});
- terminate(State=#state{socket=Socket, transport=Transport, http2_init=complete,
- http2_machine=HTTP2Machine, streams=Streams, children=Children}, Reason) ->
- %% @todo We might want to optionally send the Reason value
- %% as debug data in the GOAWAY frame here. Perhaps more.
- Transport:send(Socket, cow_http2:goaway(
- cow_http2_machine:get_last_streamid(HTTP2Machine),
- terminate_reason(Reason), <<>>)),
- terminate_all_streams(State, maps:to_list(Streams), Reason),
- cowboy_children:terminate(Children),
- Transport:close(Socket),
- exit({shutdown, Reason});
- terminate(#state{socket=Socket, transport=Transport}, Reason) ->
- Transport:close(Socket),
- exit({shutdown, Reason}).
- terminate_reason({connection_error, Reason, _}) -> Reason;
- terminate_reason({stop, _, _}) -> no_error;
- terminate_reason({socket_error, _, _}) -> internal_error;
- terminate_reason({internal_error, _, _}) -> internal_error.
- terminate_all_streams(_, [], _) ->
- ok;
- terminate_all_streams(State, [{StreamID, {_, StreamState}}|Tail], Reason) ->
- stream_call_terminate(StreamID, Reason, StreamState, State),
- terminate_all_streams(State, Tail, Reason).
- %% @todo Don't send an RST_STREAM if one was already sent.
- %% @todo Maybe rename reset_stream.
- stream_reset(State=#state{socket=Socket, transport=Transport,
- http2_machine=HTTP2Machine0}, StreamID, Error) ->
- Reason = case Error of
- {internal_error, _, _} -> internal_error;
- {stream_error, Reason0, _} -> Reason0
- end,
- Transport:send(Socket, cow_http2:rst_stream(StreamID, Reason)),
- case cow_http2_machine:reset_stream(StreamID, HTTP2Machine0) of
- {ok, HTTP2Machine} ->
- terminate_stream(State#state{http2_machine=HTTP2Machine}, StreamID, Error);
- {error, not_found} ->
- terminate_stream(State, StreamID, Error)
- end.
- stop_stream(State=#state{http2_machine=HTTP2Machine}, StreamID) ->
- case cow_http2_machine:get_stream_local_state(StreamID, HTTP2Machine) of
- %% When the stream terminates normally (without sending RST_STREAM)
- %% and no response was sent, we need to send a proper response back to the client.
- %% We delay the termination of the stream until the response is fully sent.
- {ok, idle, _} ->
- info(stopping(State, StreamID), StreamID, {response, 204, #{}, <<>>});
- %% When a response was sent but not terminated, we need to close the stream.
- %% We delay the termination of the stream until the response is fully sent.
- {ok, nofin, fin} ->
- stopping(State, StreamID);
- %% We only send a final DATA frame if there isn't one queued yet.
- {ok, nofin, _} ->
- info(stopping(State, StreamID), StreamID, {data, fin, <<>>});
- %% When a response was sent fully we can terminate the stream,
- %% regardless of the stream being in half-closed or closed state.
- _ ->
- terminate_stream(State, StreamID)
- end.
- stopping(State=#state{streams=Streams}, StreamID) ->
- #{StreamID := {_, StreamState}} = Streams,
- State#state{streams=Streams#{StreamID => {stopping, StreamState}}}.
- %% If we finished sending data and the stream is stopping, terminate it.
- maybe_terminate_stream(State=#state{streams=Streams}, StreamID, fin) ->
- case Streams of
- #{StreamID := {stopping, _}} ->
- terminate_stream(State, StreamID);
- _ ->
- State
- end;
- maybe_terminate_stream(State, _, _) ->
- State.
- %% When the stream stops normally without reading the request
- %% body fully we need to tell the client to stop sending it.
- %% We do this by sending an RST_STREAM with reason NO_ERROR. (RFC7540 8.1.0)
- terminate_stream(State0=#state{socket=Socket, transport=Transport,
- http2_machine=HTTP2Machine0}, StreamID) ->
- State = case cow_http2_machine:get_stream_local_state(StreamID, HTTP2Machine0) of
- {ok, fin, _} ->
- Transport:send(Socket, cow_http2:rst_stream(StreamID, no_error)),
- {ok, HTTP2Machine} = cow_http2_machine:reset_stream(StreamID, HTTP2Machine0),
- State0#state{http2_machine=HTTP2Machine};
- {error, closed} ->
- State0
- end,
- terminate_stream(State, StreamID, normal).
- terminate_stream(State=#state{streams=Streams0, children=Children0}, StreamID, Reason) ->
- case maps:take(StreamID, Streams0) of
- {{_, StreamState}, Streams} ->
- stream_call_terminate(StreamID, Reason, StreamState, State),
- Children = cowboy_children:shutdown(Children0, StreamID),
- State#state{streams=Streams, children=Children};
- error ->
- State
- end.
- %% @todo Maybe put State first.
- stream_call_terminate(StreamID, Reason, StreamState, #state{opts=Opts}) ->
- try
- cowboy_stream:terminate(StreamID, Reason, StreamState)
- catch Class:Exception ->
- cowboy:log(cowboy_stream:make_error_log(terminate,
- [StreamID, Reason, StreamState],
- Class, Exception, erlang:get_stacktrace()), Opts)
- end.
- %% System callbacks.
- -spec system_continue(_, _, {#state{}, binary()}) -> ok.
- system_continue(_, _, {State, Buffer}) ->
- loop(State, Buffer).
- -spec system_terminate(any(), _, _, {#state{}, binary()}) -> no_return().
- system_terminate(Reason, _, _, {State, _}) ->
- terminate(State, {stop, {exit, Reason}, 'sys:terminate/2,3 was called.'}).
- -spec system_code_change(Misc, _, _, _) -> {ok, Misc} when Misc::{#state{}, binary()}.
- system_code_change(Misc, _, _, _) ->
- {ok, Misc}.
|