cowboy_sub_protocol.erl 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. %% Copyright (c) 2013, James Fish <james@fishcakez.com>
  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 sub protocols.
  15. %%
  16. %% Only one function needs to be implemented, <em>upgrade/4</em>.
  17. %% It receives the Req, the environment, the handler that the request has been
  18. %% routed to and the handler's options. It acts exactly the same as a
  19. %% middleware, so returns the same values a middleware's execute/2.
  20. %%
  21. %% Once the sub protocol has processed the request it should add the result
  22. %% to the environment. This is done by adding the tuple {result, Value} to the
  23. %% environment list. To continue handling requests on the current connection the
  24. %% Value should be the atom ok. Any other value will prevent the processing of
  25. %% subsequent requests.
  26. %%
  27. %% <em>upgrade/4</em> will be called when a handler's init/3 returns
  28. %% {upgrade, protocol, Module}, where Module is the module of the sub protocol.
  29. -module(cowboy_sub_protocol).
  30. -callback upgrade(Req, Env, module(), any())
  31. -> {ok, Req, Env}
  32. | {suspend, module(), atom(), [any()]}
  33. | {halt, Req}
  34. | {error, cowboy:http_status(), Req}
  35. when Req::cowboy_req:req(), Env::cowboy_middleware:env().