cowboy_loop_handler.erl 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. %% Copyright (c) 2011-2013, 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. %% @doc Behaviour for long-lived HTTP handlers.
  15. %%
  16. %% <em>init/3</em> allows you to initialize a state for all subsequent
  17. %% callbacks, and indicate to Cowboy whether you accept to handle the
  18. %% request or want to shutdown without handling it, in which case the
  19. %% receive loop and <em>info/3</em> calls will simply be skipped.
  20. %%
  21. %% <em>info/3</em> allows you to handle the messages this process will
  22. %% receive. It receives the message and the state previously defined.
  23. %% It can decide to stop the receive loop or continue receiving.
  24. %%
  25. %% <em>terminate/3</em> allows you to clean up. It receives the
  26. %% termination reason and the state previously defined.
  27. %%
  28. %% There is no required operation to perform in any of these callbacks
  29. %% other than returning the proper values. Make sure you always return
  30. %% the last modified Req so that Cowboy has the up to date information
  31. %% about the request.
  32. %%
  33. %% It is recommended to use hibernate if this process is not going to
  34. %% receive a lot of messages. It is also recommended to use a timeout
  35. %% value so that the connection gets closed after a long period of
  36. %% inactivity.
  37. -module(cowboy_loop_handler).
  38. -type opts() :: any().
  39. -type state() :: any().
  40. -type terminate_reason() :: {normal, shutdown}
  41. | {normal, timeout}
  42. | {error, closed}
  43. | {error, overflow}
  44. | {error, atom()}.
  45. -callback init({atom(), http}, Req, opts())
  46. -> {ok, Req, state()}
  47. | {loop, Req, state()}
  48. | {loop, Req, state(), hibernate}
  49. | {loop, Req, state(), timeout()}
  50. | {loop, Req, state(), timeout(), hibernate}
  51. | {shutdown, Req, state()}
  52. | {upgrade, protocol, module()}
  53. | {upgrade, protocol, module(), Req, opts()}
  54. when Req::cowboy_req:req().
  55. -callback info(any(), Req, State)
  56. -> {ok, Req, State}
  57. | {loop, Req, State}
  58. | {loop, Req, State, hibernate}
  59. when Req::cowboy_req:req(), State::state().
  60. -callback terminate(terminate_reason(), cowboy_req:req(), state()) -> ok.