cowboy_stream.erl 7.7 KB

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