cowboy.erl 3.0 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_clear/4]).
  16. -export([start_tls/4]).
  17. -export([stop_listener/1]).
  18. -export([set_env/3]).
  19. -type opts() :: map().
  20. -export_type([opts/0]).
  21. -type fields() :: [atom()
  22. | {atom(), cowboy_constraints:constraint() | [cowboy_constraints:constraint()]}
  23. | {atom(), cowboy_constraints:constraint() | [cowboy_constraints:constraint()], any()}].
  24. -export_type([fields/0]).
  25. -type http_headers() :: [{binary(), iodata()}].
  26. -export_type([http_headers/0]).
  27. -type http_status() :: non_neg_integer() | binary().
  28. -export_type([http_status/0]).
  29. -type http_version() :: 'HTTP/1.1' | 'HTTP/1.0'.
  30. -export_type([http_version/0]).
  31. -type onresponse_fun() ::
  32. fun((http_status(), http_headers(), iodata(), Req) -> Req).
  33. -export_type([onresponse_fun/0]).
  34. -spec start_clear(ranch:ref(), non_neg_integer(), ranch_tcp:opts(),
  35. cowboy_protocol:opts()) -> {ok, pid()} | {error, any()}.
  36. start_clear(Ref, NbAcceptors, TransOpts0, ProtoOpts)
  37. when is_integer(NbAcceptors), NbAcceptors > 0 ->
  38. TransOpts = TransOpts0,%[connection_type(ProtoOpts)|TransOpts0],
  39. ranch:start_listener(Ref, NbAcceptors, ranch_tcp, TransOpts, cowboy_clear, ProtoOpts).
  40. -spec start_tls(ranch:ref(), non_neg_integer(), ranch_ssl:opts(), opts()) -> {ok, pid()} | {error, any()}.
  41. start_tls(Ref, NbAcceptors, TransOpts0, ProtoOpts)
  42. when is_integer(NbAcceptors), NbAcceptors > 0 ->
  43. TransOpts = [
  44. connection_type(ProtoOpts),
  45. {next_protocols_advertised, [<<"h2">>, <<"http/1.1">>]},
  46. {alpn_preferred_protocols, [<<"h2">>, <<"http/1.1">>]}
  47. |TransOpts0],
  48. ranch:start_listener(Ref, NbAcceptors, ranch_ssl, TransOpts, cowboy_tls, ProtoOpts).
  49. -spec connection_type(opts()) -> {connection_type, worker | supervisor}.
  50. connection_type(ProtoOpts) ->
  51. {_, Type} = maps:get(stream_handler, ProtoOpts, {cowboy_stream_h, supervisor}),
  52. {connection_type, Type}.
  53. -spec stop_listener(ranch:ref()) -> ok | {error, not_found}.
  54. stop_listener(Ref) ->
  55. ranch:stop_listener(Ref).
  56. -spec set_env(ranch:ref(), atom(), any()) -> ok.
  57. set_env(Ref, Name, Value) ->
  58. Opts = ranch:get_protocol_options(Ref),
  59. {_, Env} = lists:keyfind(env, 1, Opts),
  60. Opts2 = lists:keyreplace(env, 1, Opts,
  61. {env, lists:keystore(Name, 1, Env, {Name, Value})}),
  62. ok = ranch:set_protocol_options(Ref, Opts2).