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. %% @todo Detailed opts.
  20. -type opts() :: map().
  21. -export_type([opts/0]).
  22. -type fields() :: [atom()
  23. | {atom(), cowboy_constraints:constraint() | [cowboy_constraints:constraint()]}
  24. | {atom(), cowboy_constraints:constraint() | [cowboy_constraints:constraint()], any()}].
  25. -export_type([fields/0]).
  26. -type http_headers() :: #{binary() => iodata()}.
  27. -export_type([http_headers/0]).
  28. -type http_status() :: non_neg_integer() | binary().
  29. -export_type([http_status/0]).
  30. -type http_version() :: 'HTTP/2' | 'HTTP/1.1' | 'HTTP/1.0'.
  31. -export_type([http_version/0]).
  32. %% @todo We should hide NbAcceptors in a socket variable, even if Ranch
  33. %% doesn't let us do that yet.
  34. -spec start_clear(ranch:ref(), non_neg_integer(), ranch_tcp:opts(), opts())
  35. -> {ok, pid()} | {error, any()}.
  36. start_clear(Ref, NbAcceptors, TransOpts0, ProtoOpts)
  37. when is_integer(NbAcceptors), NbAcceptors > 0 ->
  38. TransOpts = [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())
  41. -> {ok, pid()} | {error, any()}.
  42. start_tls(Ref, NbAcceptors, TransOpts0, ProtoOpts)
  43. when is_integer(NbAcceptors), NbAcceptors > 0 ->
  44. TransOpts = [
  45. connection_type(ProtoOpts),
  46. {next_protocols_advertised, [<<"h2">>, <<"http/1.1">>]},
  47. {alpn_preferred_protocols, [<<"h2">>, <<"http/1.1">>]}
  48. |TransOpts0],
  49. ranch:start_listener(Ref, NbAcceptors, ranch_ssl, TransOpts, cowboy_tls, ProtoOpts).
  50. -spec connection_type(opts()) -> {connection_type, worker | supervisor}.
  51. connection_type(ProtoOpts) ->
  52. {_, Type} = maps:get(stream_handler, ProtoOpts, {cowboy_stream_h, supervisor}),
  53. {connection_type, Type}.
  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).