cowboy.erl 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. %% Internal.
  20. -export([log/2]).
  21. -export([log/4]).
  22. -type opts() :: cowboy_http:opts() | cowboy_http2:opts().
  23. -export_type([opts/0]).
  24. -type fields() :: [atom()
  25. | {atom(), cowboy_constraints:constraint() | [cowboy_constraints:constraint()]}
  26. | {atom(), cowboy_constraints:constraint() | [cowboy_constraints:constraint()], any()}].
  27. -export_type([fields/0]).
  28. -type http_headers() :: #{binary() => iodata()}.
  29. -export_type([http_headers/0]).
  30. -type http_status() :: non_neg_integer() | binary().
  31. -export_type([http_status/0]).
  32. -type http_version() :: 'HTTP/2' | 'HTTP/1.1' | 'HTTP/1.0'.
  33. -export_type([http_version/0]).
  34. -spec start_clear(ranch:ref(), ranch:opts(), opts())
  35. -> {ok, pid()} | {error, any()}.
  36. start_clear(Ref, TransOpts0, ProtoOpts0) ->
  37. TransOpts1 = ranch:normalize_opts(TransOpts0),
  38. {TransOpts, ConnectionType} = ensure_connection_type(TransOpts1),
  39. ProtoOpts = ProtoOpts0#{connection_type => ConnectionType},
  40. ranch:start_listener(Ref, ranch_tcp, TransOpts, cowboy_clear, ProtoOpts).
  41. -spec start_tls(ranch:ref(), ranch:opts(), opts())
  42. -> {ok, pid()} | {error, any()}.
  43. start_tls(Ref, TransOpts0, ProtoOpts0) ->
  44. TransOpts1 = ranch:normalize_opts(TransOpts0),
  45. SocketOpts = maps:get(socket_opts, TransOpts1, []),
  46. TransOpts2 = TransOpts1#{socket_opts => [
  47. {next_protocols_advertised, [<<"h2">>, <<"http/1.1">>]},
  48. {alpn_preferred_protocols, [<<"h2">>, <<"http/1.1">>]}
  49. |SocketOpts]},
  50. {TransOpts, ConnectionType} = ensure_connection_type(TransOpts2),
  51. ProtoOpts = ProtoOpts0#{connection_type => ConnectionType},
  52. ranch:start_listener(Ref, ranch_ssl, TransOpts, cowboy_tls, ProtoOpts).
  53. ensure_connection_type(TransOpts=#{connection_type := ConnectionType}) ->
  54. {TransOpts, ConnectionType};
  55. ensure_connection_type(TransOpts) ->
  56. {TransOpts#{connection_type => supervisor}, supervisor}.
  57. -spec stop_listener(ranch:ref()) -> ok | {error, not_found}.
  58. stop_listener(Ref) ->
  59. ranch:stop_listener(Ref).
  60. -spec set_env(ranch:ref(), atom(), any()) -> ok.
  61. set_env(Ref, Name, Value) ->
  62. Opts = ranch:get_protocol_options(Ref),
  63. Env = maps:get(env, Opts, #{}),
  64. Opts2 = maps:put(env, maps:put(Name, Value, Env), Opts),
  65. ok = ranch:set_protocol_options(Ref, Opts2).
  66. %% Internal.
  67. -spec log({log, logger:level(), io:format(), list()}, opts()) -> ok.
  68. log({log, Level, Format, Args}, Opts) ->
  69. log(Level, Format, Args, Opts).
  70. -spec log(logger:level(), io:format(), list(), opts()) -> ok.
  71. log(Level, Format, Args, #{logger := Logger})
  72. when Logger =/= error_logger ->
  73. _ = Logger:Level(Format, Args),
  74. ok;
  75. %% We use error_logger by default. Because error_logger does
  76. %% not have all the levels we accept we have to do some
  77. %% mapping to error_logger functions.
  78. log(Level, Format, Args, _) ->
  79. Function = case Level of
  80. emergency -> error_msg;
  81. alert -> error_msg;
  82. critical -> error_msg;
  83. error -> error_msg;
  84. warning -> warning_msg;
  85. notice -> warning_msg;
  86. info -> info_msg;
  87. debug -> info_msg
  88. end,
  89. error_logger:Function(Format, Args).