cowboy.erl 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 Convenience API to start and stop HTTP/HTTPS listeners.
  15. -module(cowboy).
  16. -export([start_http/4]).
  17. -export([start_https/4]).
  18. -export([stop_listener/1]).
  19. -export([set_env/3]).
  20. -type http_headers() :: [{binary(), iodata()}].
  21. -export_type([http_headers/0]).
  22. -type http_status() :: non_neg_integer() | binary().
  23. -export_type([http_status/0]).
  24. -type http_version() :: 'HTTP/1.1' | 'HTTP/1.0'.
  25. -export_type([http_version/0]).
  26. -type onrequest_fun() :: fun((Req) -> Req).
  27. -export_type([onrequest_fun/0]).
  28. -type onresponse_fun() ::
  29. fun((http_status(), http_headers(), iodata(), Req) -> Req).
  30. -export_type([onresponse_fun/0]).
  31. %% @doc Start an HTTP listener.
  32. -spec start_http(ranch:ref(), non_neg_integer(), ranch_tcp:opts(),
  33. cowboy_protocol:opts()) -> {ok, pid()}.
  34. start_http(Ref, NbAcceptors, TransOpts, ProtoOpts)
  35. when is_integer(NbAcceptors), NbAcceptors > 0 ->
  36. ranch:start_listener(Ref, NbAcceptors,
  37. ranch_tcp, TransOpts, cowboy_protocol, ProtoOpts).
  38. %% @doc Start an HTTPS listener.
  39. -spec start_https(ranch:ref(), non_neg_integer(), ranch_ssl:opts(),
  40. cowboy_protocol:opts()) -> {ok, pid()}.
  41. start_https(Ref, NbAcceptors, TransOpts, ProtoOpts)
  42. when is_integer(NbAcceptors), NbAcceptors > 0 ->
  43. ranch:start_listener(Ref, NbAcceptors,
  44. ranch_ssl, TransOpts, cowboy_protocol, ProtoOpts).
  45. %% @doc Stop a listener.
  46. -spec stop_listener(ranch:ref()) -> ok.
  47. stop_listener(Ref) ->
  48. ranch:stop_listener(Ref).
  49. %% @doc Convenience function for setting an environment value.
  50. %%
  51. %% Allows you to update live an environment value used by middlewares.
  52. %% This function is primarily intended to simplify updating the dispatch
  53. %% list used for routing.
  54. -spec set_env(ranch:ref(), atom(), any()) -> ok.
  55. set_env(Ref, Name, Value) ->
  56. Opts = ranch:get_protocol_options(Ref),
  57. {_, Env} = lists:keyfind(env, 1, Opts),
  58. Env2 = [{Name, Value}|lists:keydelete(Name, 1, Env)],
  59. Opts2 = lists:keyreplace(env, 1, Opts, {env, Env2}),
  60. ok = ranch:set_protocol_options(Ref, Opts2).