| 1 |
%% Copyright (c) Loïc Hoguin <[email protected]> |
| 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 |
|
| 15 |
-module(cowboy_stream). |
| 16 |
|
| 17 |
-type state() :: any(). |
| 18 |
-type human_reason() :: atom(). |
| 19 |
|
| 20 |
-type streamid() :: any(). |
| 21 |
-export_type([streamid/0]). |
| 22 |
|
| 23 |
-type fin() :: fin | nofin. |
| 24 |
-export_type([fin/0]). |
| 25 |
|
| 26 |
%% @todo Perhaps it makes more sense to have resp_body in this module? |
| 27 |
|
| 28 |
-type resp_command() |
| 29 |
:: {response, cowboy:http_status(), cowboy:http_headers(), cowboy_req:resp_body()}. |
| 30 |
-export_type([resp_command/0]). |
| 31 |
|
| 32 |
-type commands() :: [{inform, cowboy:http_status(), cowboy:http_headers()} |
| 33 |
| resp_command() |
| 34 |
| {headers, cowboy:http_status(), cowboy:http_headers()} |
| 35 |
| {data, fin(), cowboy_req:resp_body()} |
| 36 |
| {trailers, cowboy:http_headers()} |
| 37 |
| {push, binary(), binary(), binary(), inet:port_number(), |
| 38 |
binary(), binary(), cowboy:http_headers()} |
| 39 |
| {flow, pos_integer()} |
| 40 |
| {spawn, pid(), timeout()} |
| 41 |
| {error_response, cowboy:http_status(), cowboy:http_headers(), iodata()} |
| 42 |
| {switch_protocol, cowboy:http_headers(), module(), state()} |
| 43 |
| {internal_error, any(), human_reason()} |
| 44 |
| {set_options, map()} |
| 45 |
| {log, logger:level(), io:format(), list()} |
| 46 |
| stop]. |
| 47 |
-export_type([commands/0]). |
| 48 |
|
| 49 |
-type reason() :: normal | switch_protocol |
| 50 |
| {internal_error, timeout | {error | exit | throw, any()}, human_reason()} |
| 51 |
| {socket_error, closed | atom(), human_reason()} |
| 52 |
%% @todo Or cow_http3:error(). |
| 53 |
| {stream_error, cow_http2:error(), human_reason()} |
| 54 |
| {connection_error, cow_http2:error(), human_reason()} |
| 55 |
| {stop, cow_http2:frame() | {exit, any()}, human_reason()}. |
| 56 |
-export_type([reason/0]). |
| 57 |
|
| 58 |
-type partial_req() :: map(). %% @todo Take what's in cowboy_req with everything? optional. |
| 59 |
-export_type([partial_req/0]). |
| 60 |
|
| 61 |
-callback init(streamid(), cowboy_req:req(), cowboy:opts()) -> {commands(), state()}. |
| 62 |
-callback data(streamid(), fin(), binary(), State) -> {commands(), State} when State::state(). |
| 63 |
-callback info(streamid(), any(), State) -> {commands(), State} when State::state(). |
| 64 |
-callback terminate(streamid(), reason(), state()) -> any(). |
| 65 |
-callback early_error(streamid(), reason(), partial_req(), Resp, cowboy:opts()) |
| 66 |
-> Resp when Resp::resp_command(). |
| 67 |
|
| 68 |
%% @todo To optimize the number of active timers we could have a command |
| 69 |
%% that enables a timeout that is called in the absence of any other call, |
| 70 |
%% similar to what gen_server does. However the nice thing about this is |
| 71 |
%% that the connection process can keep a single timer around (the same |
| 72 |
%% one that would be used to detect half-closed sockets) and use this |
| 73 |
%% timer and other events to trigger the timeout in streams at their |
| 74 |
%% intended time. |
| 75 |
%% |
| 76 |
%% This same timer can be used to try and send PING frames to help detect |
| 77 |
%% that the connection is indeed unresponsive. |
| 78 |
|
| 79 |
-export([init/3]). |
| 80 |
-export([data/4]). |
| 81 |
-export([info/3]). |
| 82 |
-export([terminate/3]). |
| 83 |
-export([early_error/5]). |
| 84 |
-export([make_error_log/5]). |
| 85 |
|
| 86 |
%% Note that this and other functions in this module do NOT catch |
| 87 |
%% exceptions. We want the exception to go all the way down to the |
| 88 |
%% protocol code. |
| 89 |
%% |
| 90 |
%% OK the failure scenario is not so clear. The problem is |
| 91 |
%% that the failure at any point in init/3 will result in the |
| 92 |
%% corresponding state being lost. I am unfortunately not |
| 93 |
%% confident we can do anything about this. If the crashing |
| 94 |
%% handler just created a process, we'll never know about it. |
| 95 |
%% Therefore at this time I choose to leave all failure handling |
| 96 |
%% to the protocol process. |
| 97 |
%% |
| 98 |
%% Note that a failure in init/3 will result in terminate/3 |
| 99 |
%% NOT being called. This is because the state is not available. |
| 100 |
|
| 101 |
-spec init(streamid(), cowboy_req:req(), cowboy:opts()) |
| 102 |
-> {commands(), {module(), state()} | undefined}. |
| 103 |
init(StreamID, Req, Opts) -> |
| 104 |
case maps:get(stream_handlers, Opts, [cowboy_stream_h]) of |
| 105 |
[] -> |
| 106 |
{[], undefined}; |
| 107 |
[Handler|Tail] -> |
| 108 |
%% We call the next handler and remove it from the list of |
| 109 |
%% stream handlers. This means that handlers that run after |
| 110 |
%% it have no knowledge it exists. Should user require this |
| 111 |
%% knowledge they can just define a separate option that will |
| 112 |
%% be left untouched. |
| 113 |
{Commands, State} = Handler:init(StreamID, Req, Opts#{stream_handlers => Tail}), |
| 114 |
{Commands, {Handler, State}} |
| 115 |
end. |
| 116 |
|
| 117 |
-spec data(streamid(), fin(), binary(), {Handler, State} | undefined) |
| 118 |
-> {commands(), {Handler, State} | undefined} |
| 119 |
when Handler::module(), State::state(). |
| 120 |
data(_, _, _, undefined) -> |
| 121 |
{[], undefined}; |
| 122 |
data(StreamID, IsFin, Data, {Handler, State0}) -> |
| 123 |
{Commands, State} = Handler:data(StreamID, IsFin, Data, State0), |
| 124 |
{Commands, {Handler, State}}. |
| 125 |
|
| 126 |
-spec info(streamid(), any(), {Handler, State} | undefined) |
| 127 |
-> {commands(), {Handler, State} | undefined} |
| 128 |
when Handler::module(), State::state(). |
| 129 |
info(_, _, undefined) -> |
| 130 |
{[], undefined}; |
| 131 |
info(StreamID, Info, {Handler, State0}) -> |
| 132 |
{Commands, State} = Handler:info(StreamID, Info, State0), |
| 133 |
{Commands, {Handler, State}}. |
| 134 |
|
| 135 |
-spec terminate(streamid(), reason(), {module(), state()} | undefined) -> ok. |
| 136 |
terminate(_, _, undefined) -> |
| 137 |
ok; |
| 138 |
terminate(StreamID, Reason, {Handler, State}) -> |
| 139 |
_ = Handler:terminate(StreamID, Reason, State), |
| 140 |
ok. |
| 141 |
|
| 142 |
-spec early_error(streamid(), reason(), partial_req(), Resp, cowboy:opts()) |
| 143 |
-> Resp when Resp::resp_command(). |
| 144 |
early_error(StreamID, Reason, PartialReq, Resp, Opts) -> |
| 145 |
case maps:get(stream_handlers, Opts, [cowboy_stream_h]) of |
| 146 |
[] -> |
| 147 |
Resp; |
| 148 |
[Handler|Tail] -> |
| 149 |
%% This is the same behavior as in init/3. |
| 150 |
Handler:early_error(StreamID, Reason, |
| 151 |
PartialReq, Resp, Opts#{stream_handlers => Tail}) |
| 152 |
end. |
| 153 |
|
| 154 |
-spec make_error_log(init | data | info | terminate | early_error, |
| 155 |
list(), error | exit | throw, any(), list()) |
| 156 |
-> {log, error, string(), list()}. |
| 157 |
make_error_log(init, [StreamID, Req, Opts], Class, Exception, Stacktrace) -> |
| 158 |
{log, error, |
| 159 |
"Unhandled exception ~p:~p in cowboy_stream:init(~p, Req, Opts)~n" |
| 160 |
"Stacktrace: ~p~n" |
| 161 |
"Req: ~p~n" |
| 162 |
"Opts: ~p~n", |
| 163 |
[Class, Exception, StreamID, Stacktrace, hide_cert(Req), Opts]}; |
| 164 |
make_error_log(data, [StreamID, IsFin, Data, State], Class, Exception, Stacktrace) -> |
| 165 |
{log, error, |
| 166 |
"Unhandled exception ~p:~p in cowboy_stream:data(~p, ~p, Data, State)~n" |
| 167 |
"Stacktrace: ~p~n" |
| 168 |
"Data: ~p~n" |
| 169 |
"State: ~p~n", |
| 170 |
[Class, Exception, StreamID, IsFin, Stacktrace, Data, State]}; |
| 171 |
make_error_log(info, [StreamID, Msg, State], Class, Exception, Stacktrace) -> |
| 172 |
{log, error, |
| 173 |
"Unhandled exception ~p:~p in cowboy_stream:info(~p, Msg, State)~n" |
| 174 |
"Stacktrace: ~p~n" |
| 175 |
"Msg: ~p~n" |
| 176 |
"State: ~p~n", |
| 177 |
[Class, Exception, StreamID, Stacktrace, Msg, State]}; |
| 178 |
make_error_log(terminate, [StreamID, Reason, State], Class, Exception, Stacktrace) -> |
| 179 |
{log, error, |
| 180 |
"Unhandled exception ~p:~p in cowboy_stream:terminate(~p, Reason, State)~n" |
| 181 |
"Stacktrace: ~p~n" |
| 182 |
"Reason: ~p~n" |
| 183 |
"State: ~p~n", |
| 184 |
[Class, Exception, StreamID, Stacktrace, Reason, State]}; |
| 185 |
make_error_log(early_error, [StreamID, Reason, PartialReq, Resp, Opts], |
| 186 |
Class, Exception, Stacktrace) -> |
| 187 |
{log, error, |
| 188 |
"Unhandled exception ~p:~p in cowboy_stream:early_error(~p, Reason, PartialReq, Resp, Opts)~n" |
| 189 |
"Stacktrace: ~p~n" |
| 190 |
"Reason: ~p~n" |
| 191 |
"PartialReq: ~p~n" |
| 192 |
"Resp: ~p~n" |
| 193 |
"Opts: ~p~n", |
| 194 |
[Class, Exception, StreamID, Stacktrace, Reason, hide_cert(PartialReq), Resp, Opts]}. |
| 195 |
|
| 196 |
hide_cert(Req=#{cert := Cert}) when is_binary(Cert) -> |
| 197 |
Req#{cert => '...'}; |
| 198 |
hide_cert(Req) -> |
| 199 |
Req. |
| 200 |
|
| 201 |
-ifdef(TEST). |
| 202 |
|
| 203 |
hide_cert_test() -> |
| 204 |
#{cert := '...'} = hide_cert(#{cert => <<"cert">>}), |
| 205 |
#{cert := undefined} = hide_cert(#{cert => undefined}), |
| 206 |
true = #{} =:= hide_cert(#{}), |
| 207 |
ok. |
| 208 |
|
| 209 |
-endif. |