emqx_authn_http_test_server.erl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2024 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_authn_http_test_server).
  17. -behaviour(supervisor).
  18. -behaviour(cowboy_handler).
  19. % cowboy_server callbacks
  20. -export([init/2]).
  21. % supervisor callbacks
  22. -export([init/1]).
  23. % API
  24. -export([
  25. start_link/2,
  26. start_link/3,
  27. stop/0,
  28. set_handler/1
  29. ]).
  30. %%------------------------------------------------------------------------------
  31. %% API
  32. %%------------------------------------------------------------------------------
  33. start_link(Port, Path) ->
  34. start_link(Port, Path, false).
  35. start_link(Port, Path, SSLOpts) ->
  36. supervisor:start_link({local, ?MODULE}, ?MODULE, [Port, Path, SSLOpts]).
  37. stop() ->
  38. gen_server:stop(?MODULE).
  39. set_handler(F) when is_function(F, 2) ->
  40. true = ets:insert(?MODULE, {handler, F}),
  41. ok.
  42. %%------------------------------------------------------------------------------
  43. %% supervisor API
  44. %%------------------------------------------------------------------------------
  45. init([Port, Path, SSLOpts]) ->
  46. Dispatch = cowboy_router:compile(
  47. [
  48. {'_', [{Path, ?MODULE, []}]}
  49. ]
  50. ),
  51. ProtoOpts = #{env => #{dispatch => Dispatch}},
  52. Tab = ets:new(?MODULE, [set, named_table, public]),
  53. ets:insert(Tab, {handler, fun default_handler/2}),
  54. {Transport, TransOpts, CowboyModule} = transport_settings(Port, SSLOpts),
  55. ChildSpec = ranch:child_spec(?MODULE, Transport, TransOpts, CowboyModule, ProtoOpts),
  56. {ok, {#{}, [ChildSpec]}}.
  57. %%------------------------------------------------------------------------------
  58. %% cowboy_server API
  59. %%------------------------------------------------------------------------------
  60. init(Req, State) ->
  61. [{handler, Handler}] = ets:lookup(?MODULE, handler),
  62. Handler(Req, State).
  63. %%------------------------------------------------------------------------------
  64. %% Internal functions
  65. %%------------------------------------------------------------------------------
  66. transport_settings(Port, false) ->
  67. TransOpts = #{
  68. socket_opts => [{port, Port}],
  69. connection_type => supervisor
  70. },
  71. {ranch_tcp, TransOpts, cowboy_clear};
  72. transport_settings(Port, SSLOpts) ->
  73. TransOpts = #{
  74. socket_opts => [
  75. {port, Port},
  76. {next_protocols_advertised, [<<"h2">>, <<"http/1.1">>]},
  77. {alpn_preferred_protocols, [<<"h2">>, <<"http/1.1">>]}
  78. | SSLOpts
  79. ],
  80. connection_type => supervisor
  81. },
  82. {ranch_ssl, TransOpts, cowboy_tls}.
  83. default_handler(Req0, State) ->
  84. Req = cowboy_req:reply(
  85. 400,
  86. #{<<"content-type">> => <<"text/plain">>},
  87. <<"">>,
  88. Req0
  89. ),
  90. {ok, Req, State}.