cowboy.erl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. %% Copyright (c) 2011-2014, 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. -module(cowboy).
  15. -export([start_http/4]).
  16. -export([start_https/4]).
  17. -export([start_spdy/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. -spec start_http(ranch:ref(), non_neg_integer(), ranch_tcp:opts(),
  32. cowboy_protocol:opts()) -> {ok, pid()} | {error, any()}.
  33. start_http(Ref, NbAcceptors, TransOpts, ProtoOpts)
  34. when is_integer(NbAcceptors), NbAcceptors > 0 ->
  35. ranch:start_listener(Ref, NbAcceptors,
  36. ranch_tcp, TransOpts, cowboy_protocol, ProtoOpts).
  37. -spec start_https(ranch:ref(), non_neg_integer(), ranch_ssl:opts(),
  38. cowboy_protocol:opts()) -> {ok, pid()} | {error, any()}.
  39. start_https(Ref, NbAcceptors, TransOpts, ProtoOpts)
  40. when is_integer(NbAcceptors), NbAcceptors > 0 ->
  41. ranch:start_listener(Ref, NbAcceptors,
  42. ranch_ssl, TransOpts, cowboy_protocol, ProtoOpts).
  43. -spec start_spdy(ranch:ref(), non_neg_integer(), ranch_ssl:opts(),
  44. cowboy_spdy:opts()) -> {ok, pid()} | {error, any()}.
  45. start_spdy(Ref, NbAcceptors, TransOpts, ProtoOpts)
  46. when is_integer(NbAcceptors), NbAcceptors > 0 ->
  47. TransOpts2 = [
  48. {connection_type, supervisor},
  49. {next_protocols_advertised,
  50. [<<"spdy/3">>, <<"http/1.1">>, <<"http/1.0">>]}
  51. |TransOpts],
  52. ranch:start_listener(Ref, NbAcceptors,
  53. ranch_ssl, TransOpts2, cowboy_spdy, ProtoOpts).
  54. -spec stop_listener(ranch:ref()) -> ok | {error, not_found}.
  55. stop_listener(Ref) ->
  56. ranch:stop_listener(Ref).
  57. -spec set_env(ranch:ref(), atom(), any()) -> ok.
  58. set_env(Ref, Name, Value) ->
  59. Opts = ranch:get_protocol_options(Ref),
  60. {_, Env} = lists:keyfind(env, 1, Opts),
  61. Opts2 = lists:keyreplace(env, 1, Opts,
  62. {env, lists:keystore(Name, 1, Env, {Name, Value})}),
  63. ok = ranch:set_protocol_options(Ref, Opts2).