Move get_url functions from mod_host_meta to ejabberd_http

This commit is contained in:
Badlop
2025-11-14 15:00:29 +01:00
parent b24230fe0a
commit 71dccedfc6
4 changed files with 68 additions and 63 deletions
+1 -1
View File
@@ -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 ->
+58 -1
View File
@@ -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,
<<Protocol/binary,
"://@HOST@:",
(integer_to_binary(Port))/binary,
"/",
(str:join(Path, <<"/">>))/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;
+3 -3
View File
@@ -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
+6 -58
View File
@@ -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,
<<Protocol/binary,
"://@HOST@:",
(integer_to_binary(Port))/binary,
"/",
(str:join(Path, <<"/">>))/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 "