cowboy_http_handler.erl 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. %% Copyright (c) 2011-2012, 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/2</em> allows you to clean up. It receives the state
  25. %% 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. -callback init({atom(), http}, Req, opts())
  35. -> {ok, Req, state()}
  36. | {loop, Req, state()}
  37. | {loop, Req, state(), hibernate}
  38. | {loop, Req, state(), timeout()}
  39. | {loop, Req, state(), timeout(), hibernate}
  40. | {shutdown, Req, state()}
  41. | {upgrade, protocol, module()}
  42. when Req::cowboy_req:req().
  43. -callback handle(Req, State) -> {ok, Req, State}
  44. when Req::cowboy_req:req(), State::state().
  45. -callback terminate(cowboy_req:req(), state()) -> ok.