]> granicus.if.org Git - ejabberd/commitdiff
Improve request_handlers validator
authorEvgeny Khramtsov <ekhramtsov@process-one.net>
Tue, 23 Apr 2019 13:19:26 +0000 (16:19 +0300)
committerEvgeny Khramtsov <ekhramtsov@process-one.net>
Tue, 23 Apr 2019 13:19:26 +0000 (16:19 +0300)
src/ejabberd_http.erl

index 9588e6cd1b2791457008c2c1b983d7d8b7f5ccc6..e37269659a47c9c95b97fa06b60f6cd80a9c2c56 100644 (file)
@@ -397,10 +397,11 @@ process(Handlers, Request) ->
             %% requested path is "/test/foo/bar", the local path is
             %% ["foo", "bar"]
             LocalPath = lists:nthtail(length(HandlerPathPrefix), Request#request.path),
-           R = try
-                   HandlerModule:socket_handoff(
-                     LocalPath, Request, HandlerOpts)
-               catch error:undef ->
+           R = case erlang:function_exported(HandlerModule, socket_handoff, 3) of
+                   true ->
+                       HandlerModule:socket_handoff(
+                         LocalPath, Request, HandlerOpts);
+                   false ->
                        HandlerModule:process(LocalPath, Request)
                end,
             ejabberd_hooks:run(http_request_debug, [{LocalPath, Request}]),
@@ -962,6 +963,23 @@ transform_listen_option({request_handlers, Hs}, Opts) ->
 transform_listen_option(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()].
 opt_type(trusted_proxies) ->
     fun (all) -> all;
@@ -1003,11 +1021,7 @@ listen_opt_type(request_handlers) ->
            Hs2 = [{str:tokens(
                      iolist_to_binary(Path), <<"/">>),
                    Mod} || {Path, Mod} <- Hs1],
-           [{Path,
-             case Mod of
-                 mod_http_bind -> mod_bosh;
-                 _ -> Mod
-             end} || {Path, Mod} <- Hs2]
+           [{Path, prepare_request_module(Mod)} || {Path, Mod} <- Hs2]
     end;
 listen_opt_type(default_host) ->
     fun iolist_to_binary/1;