diff --git a/src/ejabberd_captcha.erl b/src/ejabberd_captcha.erl index 15a485c55..ffca217e7 100644 --- a/src/ejabberd_captcha.erl +++ b/src/ejabberd_captcha.erl @@ -482,7 +482,7 @@ get_url(Str) -> end. get_url() -> - case mod_host_meta:get_auto_url(any, ?MODULE) of + case ejabberd_http:get_auto_url(any, ?MODULE) of undefined -> undefined; Url -> diff --git a/src/ejabberd_http.erl b/src/ejabberd_http.erl index 3664e091f..7b4b4bff9 100644 --- a/src/ejabberd_http.erl +++ b/src/ejabberd_http.erl @@ -33,7 +33,7 @@ accept/1, receive_headers/1, recv_file/2, listen_opt_type/1, listen_options/0, apply_custom_headers/2]). - +-export([get_url/4, get_auto_url/2, find_handler_port_path/2]). -export([init/3]). -include("logger.hrl"). @@ -868,6 +868,63 @@ apply_custom_headers(Headers, CustomHeaders) -> maps:from_list(CustomHeaders)), Doctype ++ maps:to_list(M). +%%%-------------------------------- +%%%-export([get_url/4, get_auto_url/2]). + +get_url(M, bosh, Tls, Host) -> + get_url(M, Tls, Host, bosh_service_url, mod_bosh); +get_url(M, websocket, Tls, Host) -> + get_url(M, Tls, Host, websocket_url, ejabberd_http_ws); +get_url(M, Option, Tls, Host) -> + get_url(M, Tls, Host, Option, M). + +get_url(M, Tls, Host, Option, Handler) -> + case get_url_preliminar(M, Tls, Host, Option, Handler) of + undefined -> undefined; + Url -> misc:expand_keyword(<<"@HOST@">>, Url, Host) + end. + +get_url_preliminar(M, Tls, Host, Option, Handler) -> + case gen_mod:get_module_opt(Host, M, Option) of + undefined -> undefined; + auto -> get_auto_url(Tls, Handler); + <<"auto">> -> get_auto_url(Tls, Handler); + U when is_binary(U) -> U + end. + +get_auto_url(Tls, Handler) -> + case find_handler_port_path(Tls, Handler) of + [] -> undefined; + [{ThisTls, Port, Path} | _] -> + Protocol = case {ThisTls, Handler} of + {false, ejabberd_http_ws} -> <<"ws">>; + {true, ejabberd_http_ws} -> <<"wss">>; + {false, _} -> <<"http">>; + {true, _} -> <<"https">> + end, + <>))/binary>> + end. + +find_handler_port_path(Tls, Handler) -> + lists:filtermap( + fun({{Port, _, _}, + ejabberd_http, + #{tls := ThisTls, request_handlers := Handlers}}) + when is_integer(Port) and ((Tls == any) or (Tls == ThisTls)) -> + case lists:keyfind(Handler, 2, Handlers) of + false -> false; + {Path, Handler} -> {true, {ThisTls, Port, Path}} + end; + (_) -> false + end, ets:tab2list(ejabberd_listener)). + + +%%%-------------------------------- + % The following code is mostly taken from yaws_ssl.erl toupper(C) when C >= $a andalso C =< $z -> C - 32; diff --git a/src/mod_conversejs.erl b/src/mod_conversejs.erl index c28151076..8ea151b88 100644 --- a/src/mod_conversejs.erl +++ b/src/mod_conversejs.erl @@ -73,12 +73,12 @@ process([], #request{method = 'GET', host = Host, q = Query, raw_path = RawPath1 {<<"view_mode">>, <<"fullscreen">>} | ExtraOptions], Init2 = - case mod_host_meta:get_url(?MODULE, websocket, any, Host) of + case ejabberd_http:get_url(?MODULE, websocket, any, Host) of undefined -> Init; WSURL -> [{<<"websocket_url">>, WSURL} | Init] end, Init3 = - case mod_host_meta:get_url(?MODULE, bosh, any, Host) of + case ejabberd_http:get_url(?MODULE, bosh, any, Host) of undefined -> Init2; BoshURL -> [{<<"bosh_service_url">>, BoshURL} | Init2] end, @@ -256,7 +256,7 @@ web_menu_system(Result, #request{host = Host, auth = Auth, tp = Protocol}) -> - AutoUrl = mod_host_meta:get_auto_url(any, ?MODULE), + AutoUrl = ejabberd_http:get_auto_url(any, ?MODULE), ConverseUrl = misc:expand_keyword(<<"@HOST@">>, AutoUrl, Host), AutologinQuery = case {Protocol, Auth} of diff --git a/src/mod_host_meta.erl b/src/mod_host_meta.erl index bcb83657a..2c487528c 100644 --- a/src/mod_host_meta.erl +++ b/src/mod_host_meta.erl @@ -34,7 +34,6 @@ -export([start/2, stop/1, reload/3, process/2, mod_opt_type/1, mod_options/1, depends/2]). -export([mod_doc/0]). --export([get_url/4, get_auto_url/2]). -include("logger.hrl"). @@ -85,7 +84,7 @@ process(_Path, _Request) -> %% When set to 'auto', it only takes the first valid listener options it finds file_xml(Host) -> - BoshList = case get_url(?MODULE, bosh, true, Host) of + BoshList = case ejabberd_http:get_url(?MODULE, bosh, true, Host) of undefined -> []; BoshUrl -> [?XA(<<"Link">>, @@ -93,7 +92,7 @@ file_xml(Host) -> {<<"href">>, BoshUrl}] )] end, - WsList = case get_url(?MODULE, websocket, true, Host) of + WsList = case ejabberd_http:get_url(?MODULE, websocket, true, Host) of undefined -> []; WsUrl -> [?XA(<<"Link">>, @@ -112,12 +111,12 @@ file_xml(Host) -> )]}. file_json(Host) -> - BoshList = case get_url(?MODULE, bosh, true, Host) of + BoshList = case ejabberd_http:get_url(?MODULE, bosh, true, Host) of undefined -> []; BoshUrl -> [#{rel => <<"urn:xmpp:alt-connections:xbosh">>, href => BoshUrl}] end, - WsList = case get_url(?MODULE, websocket, true, Host) of + WsList = case ejabberd_http:get_url(?MODULE, websocket, true, Host) of undefined -> []; WsUrl -> [#{rel => <<"urn:xmpp:alt-connections:websocket">>, href => WsUrl}] @@ -127,60 +126,9 @@ file_json(Host) -> {<<"Access-Control-Allow-Origin">>, <<"*">>}], [misc:json_encode(#{links => BoshList ++ WsList})]}. -get_url(M, bosh, Tls, Host) -> - get_url(M, Tls, Host, bosh_service_url, mod_bosh); -get_url(M, websocket, Tls, Host) -> - get_url(M, Tls, Host, websocket_url, ejabberd_http_ws); -get_url(M, Option, Tls, Host) -> - get_url(M, Tls, Host, Option, M). - -get_url(M, Tls, Host, Option, Handler) -> - case get_url_preliminar(M, Tls, Host, Option, Handler) of - undefined -> undefined; - Url -> misc:expand_keyword(<<"@HOST@">>, Url, Host) - end. - -get_url_preliminar(M, Tls, Host, Option, Handler) -> - case gen_mod:get_module_opt(Host, M, Option) of - undefined -> undefined; - auto -> get_auto_url(Tls, Handler); - <<"auto">> -> get_auto_url(Tls, Handler); - U when is_binary(U) -> U - end. - -get_auto_url(Tls, Handler) -> - case find_handler_port_path(Tls, Handler) of - [] -> undefined; - [{ThisTls, Port, Path} | _] -> - Protocol = case {ThisTls, Handler} of - {false, ejabberd_http_ws} -> <<"ws">>; - {true, ejabberd_http_ws} -> <<"wss">>; - {false, _} -> <<"http">>; - {true, _} -> <<"https">> - end, - <>))/binary>> - end. - -find_handler_port_path(Tls, Handler) -> - lists:filtermap( - fun({{Port, _, _}, - ejabberd_http, - #{tls := ThisTls, request_handlers := Handlers}}) - when is_integer(Port) and ((Tls == any) or (Tls == ThisTls)) -> - case lists:keyfind(Handler, 2, Handlers) of - false -> false; - {Path, Handler} -> {true, {ThisTls, Port, Path}} - end; - (_) -> false - end, ets:tab2list(ejabberd_listener)). - report_hostmeta_listener() -> - case {find_handler_port_path(false, ?MODULE), - find_handler_port_path(true, ?MODULE)} of + case {ejabberd_http:find_handler_port_path(false, ?MODULE), + ejabberd_http:find_handler_port_path(true, ?MODULE)} of {[], []} -> ?CRITICAL_MSG("It seems you enabled ~p in 'modules' but forgot to " "add it as a request_handler in an ejabberd_http "