cowboy_http_handler.erl 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 short-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. %% <em>handle/2</em> call will simply be skipped.
  20. %%
  21. %% <em>handle/2</em> allows you to handle the request. It receives the
  22. %% state previously defined.
  23. %%
  24. %% <em>terminate/3</em> allows you to clean up. It receives the
  25. %% termination reason and the state previously defined.
  26. %%
  27. %% There is no required operation to perform in any of these callbacks
  28. %% other than returning the proper values. Make sure you always return
  29. %% the last modified Req so that Cowboy has the up to date information
  30. %% about the request.
  31. -module(cowboy_http_handler).
  32. -type opts() :: any().
  33. -type state() :: any().
  34. -type terminate_reason() :: {normal, shutdown}
  35. | {normal, timeout} %% Only occurs in loop handlers.
  36. | {error, atom()}.
  37. -callback init({atom(), http}, Req, opts())
  38. -> {ok, Req, state()}
  39. | {loop, Req, state()}
  40. | {loop, Req, state(), hibernate}
  41. | {loop, Req, state(), timeout()}
  42. | {loop, Req, state(), timeout(), hibernate}
  43. | {shutdown, Req, state()}
  44. | {upgrade, protocol, module()}
  45. | {upgrade, protocol, module(), Req, opts()}
  46. when Req::cowboy_req:req().
  47. -callback handle(Req, State) -> {ok, Req, State}
  48. when Req::cowboy_req:req(), State::state().
  49. -callback terminate(terminate_reason(), cowboy_req:req(), state()) -> ok.