cowboy_middleware.erl 1.5 KB

123456789101112131415161718192021222324252627282930313233343536
  1. %% Copyright (c) 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 middlewares.
  15. %%
  16. %% Only one function needs to be implemented, <em>execute/2</em>.
  17. %% It receives the Req and the environment and returns them
  18. %% optionally modified. It can decide to stop the processing with
  19. %% or without an error. It is also possible to hibernate the process
  20. %% if needed.
  21. %%
  22. %% A middleware can perform any operation. Make sure you always return
  23. %% the last modified Req so that Cowboy has the up to date information
  24. %% about the request.
  25. -module(cowboy_middleware).
  26. -type env() :: [{atom(), any()}].
  27. -export_type([env/0]).
  28. -callback execute(Req, Env)
  29. -> {ok, Req, Env}
  30. | {suspend, module(), atom(), any()}
  31. | {halt, Req}
  32. | {error, cowboy_http:status(), Req}
  33. when Req::cowboy_req:req(), Env::env().