cowboy_http_handler.erl 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. %% Copyright (c) 2011, Loïc Hoguin <essen@dev-extend.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 Handler for HTTP requests.
  15. %%
  16. %% HTTP handlers must implement three callbacks: <em>init/3</em>,
  17. %% <em>handle/2</em> and <em>terminate/2</em>, called one after another in
  18. %% that order.
  19. %%
  20. %% <em>init/3</em> is meant for initialization. It receives information about
  21. %% the transport and protocol used, along with the handler options from the
  22. %% dispatch list, and allows you to upgrade the protocol if needed. You can
  23. %% define a request-wide state here.
  24. %%
  25. %% <em>handle/2</em> is meant for handling the request. It receives the
  26. %% request and the state previously defined.
  27. %%
  28. %% <em>terminate/2</em> is meant for cleaning up. It also receives the
  29. %% request and the state previously defined.
  30. %%
  31. %% You do not have to read the request body or even send a reply if you do
  32. %% not need to. Cowboy will properly handle these cases and clean-up afterwards.
  33. %% In doubt it'll simply close the connection.
  34. %%
  35. %% Note that when upgrading the connection to WebSocket you do not need to
  36. %% define the <em>handle/2</em> and <em>terminate/2</em> callbacks.
  37. -module(cowboy_http_handler).
  38. -export([behaviour_info/1]).
  39. %% @private
  40. -spec behaviour_info(_)
  41. -> undefined | [{handle, 2} | {init, 3} | {terminate, 2}, ...].
  42. behaviour_info(callbacks) ->
  43. [{init, 3}, {handle, 2}, {terminate, 2}];
  44. behaviour_info(_Other) ->
  45. undefined.