Skip to content

Commit 71dcced

Browse files
committed
Move get_url functions from mod_host_meta to ejabberd_http
1 parent b24230f commit 71dcced

File tree

4 files changed

+68
-63
lines changed

4 files changed

+68
-63
lines changed

src/ejabberd_captcha.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ get_url(Str) ->
482482
end.
483483

484484
get_url() ->
485-
case mod_host_meta:get_auto_url(any, ?MODULE) of
485+
case ejabberd_http:get_auto_url(any, ?MODULE) of
486486
undefined ->
487487
undefined;
488488
Url ->

src/ejabberd_http.erl

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
accept/1, receive_headers/1, recv_file/2,
3434
listen_opt_type/1, listen_options/0,
3535
apply_custom_headers/2]).
36-
36+
-export([get_url/4, get_auto_url/2, find_handler_port_path/2]).
3737
-export([init/3]).
3838

3939
-include("logger.hrl").
@@ -868,6 +868,63 @@ apply_custom_headers(Headers, CustomHeaders) ->
868868
maps:from_list(CustomHeaders)),
869869
Doctype ++ maps:to_list(M).
870870

871+
%%%--------------------------------
872+
%%%-export([get_url/4, get_auto_url/2]).
873+
874+
get_url(M, bosh, Tls, Host) ->
875+
get_url(M, Tls, Host, bosh_service_url, mod_bosh);
876+
get_url(M, websocket, Tls, Host) ->
877+
get_url(M, Tls, Host, websocket_url, ejabberd_http_ws);
878+
get_url(M, Option, Tls, Host) ->
879+
get_url(M, Tls, Host, Option, M).
880+
881+
get_url(M, Tls, Host, Option, Handler) ->
882+
case get_url_preliminar(M, Tls, Host, Option, Handler) of
883+
undefined -> undefined;
884+
Url -> misc:expand_keyword(<<"@HOST@">>, Url, Host)
885+
end.
886+
887+
get_url_preliminar(M, Tls, Host, Option, Handler) ->
888+
case gen_mod:get_module_opt(Host, M, Option) of
889+
undefined -> undefined;
890+
auto -> get_auto_url(Tls, Handler);
891+
<<"auto">> -> get_auto_url(Tls, Handler);
892+
U when is_binary(U) -> U
893+
end.
894+
895+
get_auto_url(Tls, Handler) ->
896+
case find_handler_port_path(Tls, Handler) of
897+
[] -> undefined;
898+
[{ThisTls, Port, Path} | _] ->
899+
Protocol = case {ThisTls, Handler} of
900+
{false, ejabberd_http_ws} -> <<"ws">>;
901+
{true, ejabberd_http_ws} -> <<"wss">>;
902+
{false, _} -> <<"http">>;
903+
{true, _} -> <<"https">>
904+
end,
905+
<<Protocol/binary,
906+
"://@HOST@:",
907+
(integer_to_binary(Port))/binary,
908+
"/",
909+
(str:join(Path, <<"/">>))/binary>>
910+
end.
911+
912+
find_handler_port_path(Tls, Handler) ->
913+
lists:filtermap(
914+
fun({{Port, _, _},
915+
ejabberd_http,
916+
#{tls := ThisTls, request_handlers := Handlers}})
917+
when is_integer(Port) and ((Tls == any) or (Tls == ThisTls)) ->
918+
case lists:keyfind(Handler, 2, Handlers) of
919+
false -> false;
920+
{Path, Handler} -> {true, {ThisTls, Port, Path}}
921+
end;
922+
(_) -> false
923+
end, ets:tab2list(ejabberd_listener)).
924+
925+
926+
%%%--------------------------------
927+
871928
% The following code is mostly taken from yaws_ssl.erl
872929

873930
toupper(C) when C >= $a andalso C =< $z -> C - 32;

src/mod_conversejs.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ process([], #request{method = 'GET', host = Host, q = Query, raw_path = RawPath1
7373
{<<"view_mode">>, <<"fullscreen">>}
7474
| ExtraOptions],
7575
Init2 =
76-
case mod_host_meta:get_url(?MODULE, websocket, any, Host) of
76+
case ejabberd_http:get_url(?MODULE, websocket, any, Host) of
7777
undefined -> Init;
7878
WSURL -> [{<<"websocket_url">>, WSURL} | Init]
7979
end,
8080
Init3 =
81-
case mod_host_meta:get_url(?MODULE, bosh, any, Host) of
81+
case ejabberd_http:get_url(?MODULE, bosh, any, Host) of
8282
undefined -> Init2;
8383
BoshURL -> [{<<"bosh_service_url">>, BoshURL} | Init2]
8484
end,
@@ -256,7 +256,7 @@ web_menu_system(Result,
256256
#request{host = Host,
257257
auth = Auth,
258258
tp = Protocol}) ->
259-
AutoUrl = mod_host_meta:get_auto_url(any, ?MODULE),
259+
AutoUrl = ejabberd_http:get_auto_url(any, ?MODULE),
260260
ConverseUrl = misc:expand_keyword(<<"@HOST@">>, AutoUrl, Host),
261261
AutologinQuery =
262262
case {Protocol, Auth} of

src/mod_host_meta.erl

Lines changed: 6 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
-export([start/2, stop/1, reload/3, process/2,
3535
mod_opt_type/1, mod_options/1, depends/2]).
3636
-export([mod_doc/0]).
37-
-export([get_url/4, get_auto_url/2]).
3837

3938
-include("logger.hrl").
4039

@@ -85,15 +84,15 @@ process(_Path, _Request) ->
8584
%% When set to 'auto', it only takes the first valid listener options it finds
8685

8786
file_xml(Host) ->
88-
BoshList = case get_url(?MODULE, bosh, true, Host) of
87+
BoshList = case ejabberd_http:get_url(?MODULE, bosh, true, Host) of
8988
undefined -> [];
9089
BoshUrl ->
9190
[?XA(<<"Link">>,
9291
[{<<"rel">>, <<"urn:xmpp:alt-connections:xbosh">>},
9392
{<<"href">>, BoshUrl}]
9493
)]
9594
end,
96-
WsList = case get_url(?MODULE, websocket, true, Host) of
95+
WsList = case ejabberd_http:get_url(?MODULE, websocket, true, Host) of
9796
undefined -> [];
9897
WsUrl ->
9998
[?XA(<<"Link">>,
@@ -112,12 +111,12 @@ file_xml(Host) ->
112111
)]}.
113112

114113
file_json(Host) ->
115-
BoshList = case get_url(?MODULE, bosh, true, Host) of
114+
BoshList = case ejabberd_http:get_url(?MODULE, bosh, true, Host) of
116115
undefined -> [];
117116
BoshUrl -> [#{rel => <<"urn:xmpp:alt-connections:xbosh">>,
118117
href => BoshUrl}]
119118
end,
120-
WsList = case get_url(?MODULE, websocket, true, Host) of
119+
WsList = case ejabberd_http:get_url(?MODULE, websocket, true, Host) of
121120
undefined -> [];
122121
WsUrl -> [#{rel => <<"urn:xmpp:alt-connections:websocket">>,
123122
href => WsUrl}]
@@ -127,60 +126,9 @@ file_json(Host) ->
127126
{<<"Access-Control-Allow-Origin">>, <<"*">>}],
128127
[misc:json_encode(#{links => BoshList ++ WsList})]}.
129128

130-
get_url(M, bosh, Tls, Host) ->
131-
get_url(M, Tls, Host, bosh_service_url, mod_bosh);
132-
get_url(M, websocket, Tls, Host) ->
133-
get_url(M, Tls, Host, websocket_url, ejabberd_http_ws);
134-
get_url(M, Option, Tls, Host) ->
135-
get_url(M, Tls, Host, Option, M).
136-
137-
get_url(M, Tls, Host, Option, Handler) ->
138-
case get_url_preliminar(M, Tls, Host, Option, Handler) of
139-
undefined -> undefined;
140-
Url -> misc:expand_keyword(<<"@HOST@">>, Url, Host)
141-
end.
142-
143-
get_url_preliminar(M, Tls, Host, Option, Handler) ->
144-
case gen_mod:get_module_opt(Host, M, Option) of
145-
undefined -> undefined;
146-
auto -> get_auto_url(Tls, Handler);
147-
<<"auto">> -> get_auto_url(Tls, Handler);
148-
U when is_binary(U) -> U
149-
end.
150-
151-
get_auto_url(Tls, Handler) ->
152-
case find_handler_port_path(Tls, Handler) of
153-
[] -> undefined;
154-
[{ThisTls, Port, Path} | _] ->
155-
Protocol = case {ThisTls, Handler} of
156-
{false, ejabberd_http_ws} -> <<"ws">>;
157-
{true, ejabberd_http_ws} -> <<"wss">>;
158-
{false, _} -> <<"http">>;
159-
{true, _} -> <<"https">>
160-
end,
161-
<<Protocol/binary,
162-
"://@HOST@:",
163-
(integer_to_binary(Port))/binary,
164-
"/",
165-
(str:join(Path, <<"/">>))/binary>>
166-
end.
167-
168-
find_handler_port_path(Tls, Handler) ->
169-
lists:filtermap(
170-
fun({{Port, _, _},
171-
ejabberd_http,
172-
#{tls := ThisTls, request_handlers := Handlers}})
173-
when is_integer(Port) and ((Tls == any) or (Tls == ThisTls)) ->
174-
case lists:keyfind(Handler, 2, Handlers) of
175-
false -> false;
176-
{Path, Handler} -> {true, {ThisTls, Port, Path}}
177-
end;
178-
(_) -> false
179-
end, ets:tab2list(ejabberd_listener)).
180-
181129
report_hostmeta_listener() ->
182-
case {find_handler_port_path(false, ?MODULE),
183-
find_handler_port_path(true, ?MODULE)} of
130+
case {ejabberd_http:find_handler_port_path(false, ?MODULE),
131+
ejabberd_http:find_handler_port_path(true, ?MODULE)} of
184132
{[], []} ->
185133
?CRITICAL_MSG("It seems you enabled ~p in 'modules' but forgot to "
186134
"add it as a request_handler in an ejabberd_http "

0 commit comments

Comments
 (0)