25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-12-22 17:28:25 +01:00

Improve request_handlers validator

This commit is contained in:
Evgeny Khramtsov 2019-04-23 16:19:26 +03:00
parent e623678747
commit cc892ddc01

View File

@ -397,10 +397,11 @@ process(Handlers, Request) ->
%% requested path is "/test/foo/bar", the local path is %% requested path is "/test/foo/bar", the local path is
%% ["foo", "bar"] %% ["foo", "bar"]
LocalPath = lists:nthtail(length(HandlerPathPrefix), Request#request.path), LocalPath = lists:nthtail(length(HandlerPathPrefix), Request#request.path),
R = try R = case erlang:function_exported(HandlerModule, socket_handoff, 3) of
HandlerModule:socket_handoff( true ->
LocalPath, Request, HandlerOpts) HandlerModule:socket_handoff(
catch error:undef -> LocalPath, Request, HandlerOpts);
false ->
HandlerModule:process(LocalPath, Request) HandlerModule:process(LocalPath, Request)
end, end,
ejabberd_hooks:run(http_request_debug, [{LocalPath, Request}]), ejabberd_hooks:run(http_request_debug, [{LocalPath, Request}]),
@ -962,6 +963,23 @@ transform_listen_option({request_handlers, Hs}, Opts) ->
transform_listen_option(Opt, Opts) -> transform_listen_option(Opt, Opts) ->
[Opt|Opts]. [Opt|Opts].
prepare_request_module(mod_http_bind) ->
mod_bosh;
prepare_request_module(Mod) when is_atom(Mod) ->
case code:ensure_loaded(Mod) of
{module, Mod} ->
Mod;
Err ->
?ERROR_MSG(
"Failed to load request handler ~s: "
"make sure there is no typo and file ~s.beam "
"exists inside either ~s or ~s directory",
[Mod, Mod,
filename:dirname(code:which(?MODULE)),
ext_mod:modules_dir()]),
erlang:error(Err)
end.
-spec opt_type(atom()) -> fun((any()) -> any()) | [atom()]. -spec opt_type(atom()) -> fun((any()) -> any()) | [atom()].
opt_type(trusted_proxies) -> opt_type(trusted_proxies) ->
fun (all) -> all; fun (all) -> all;
@ -1003,11 +1021,7 @@ listen_opt_type(request_handlers) ->
Hs2 = [{str:tokens( Hs2 = [{str:tokens(
iolist_to_binary(Path), <<"/">>), iolist_to_binary(Path), <<"/">>),
Mod} || {Path, Mod} <- Hs1], Mod} || {Path, Mod} <- Hs1],
[{Path, [{Path, prepare_request_module(Mod)} || {Path, Mod} <- Hs2]
case Mod of
mod_http_bind -> mod_bosh;
_ -> Mod
end} || {Path, Mod} <- Hs2]
end; end;
listen_opt_type(default_host) -> listen_opt_type(default_host) ->
fun iolist_to_binary/1; fun iolist_to_binary/1;