cowboy.erl 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. %% Copyright (c) 2011-2017, 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/3]).
  16. -export([start_tls/3]).
  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. -spec start_clear(ranch:ref(), ranch_tcp:opts(), opts())
  33. -> {ok, pid()} | {error, any()}.
  34. start_clear(Ref, TransOpts0, ProtoOpts0) ->
  35. {TransOpts, ConnectionType} = ensure_connection_type(TransOpts0),
  36. ProtoOpts = ProtoOpts0#{connection_type => ConnectionType},
  37. ranch:start_listener(Ref, ranch_tcp, TransOpts, cowboy_clear, ProtoOpts).
  38. -spec start_tls(ranch:ref(), ranch_ssl:opts(), opts())
  39. -> {ok, pid()} | {error, any()}.
  40. start_tls(Ref, TransOpts0, ProtoOpts0) ->
  41. {TransOpts1, ConnectionType} = ensure_connection_type(TransOpts0),
  42. TransOpts = [
  43. {next_protocols_advertised, [<<"h2">>, <<"http/1.1">>]},
  44. {alpn_preferred_protocols, [<<"h2">>, <<"http/1.1">>]}
  45. |TransOpts1],
  46. ProtoOpts = ProtoOpts0#{connection_type => ConnectionType},
  47. ranch:start_listener(Ref, ranch_ssl, TransOpts, cowboy_tls, ProtoOpts).
  48. ensure_connection_type(TransOpts) ->
  49. case proplists:get_value(connection_type, TransOpts) of
  50. undefined -> {[{connection_type, supervisor}|TransOpts], supervisor};
  51. ConnectionType -> {TransOpts, ConnectionType}
  52. end.
  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 = maps:get(env, Opts, #{}),
  60. Opts2 = maps:put(env, maps:put(Name, Value, Env), Opts),
  61. ok = ranch:set_protocol_options(Ref, Opts2).