25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-22 16:20:52 +01:00

Add flexibility on rest url config

This commit is contained in:
Christophe Romain 2018-04-18 13:16:08 +02:00
parent 332567693c
commit 265c7b62c7

View File

@ -154,28 +154,43 @@ encode_json(Content) ->
end.
base_url(Server, Path) ->
Tail = case iolist_to_binary(Path) of
BPath = case iolist_to_binary(Path) of
<<$/, Ok/binary>> -> Ok;
Ok -> Ok
end,
case Tail of
<<"http", _Url/binary>> -> Tail;
Url = case BPath of
<<"http", _/binary>> -> BPath;
_ ->
Base = ejabberd_config:get_option({ext_api_url, Server},
fun(X) -> iolist_to_binary(X) end,
<<"http://localhost/api">>),
<<Base/binary, "/", Tail/binary>>
case binary:last(Base) of
47 -> <<Base/binary, BPath/binary>>;
_ -> <<Base/binary, "/", BPath/binary>>
end
end,
case binary:last(Url) of
47 -> binary_part(Url, 0, size(Url)-1);
_ -> Url
end.
url(Server, Path, []) ->
binary_to_list(base_url(Server, Path));
url(Server, Path, Params) ->
Base = base_url(Server, Path),
[<<$&, ParHead/binary>> | ParTail] =
[<<"&", (iolist_to_binary(Key))/binary, "=",
url(Url, []) ->
Url;
url(Url, Params) ->
L = [<<"&", (iolist_to_binary(Key))/binary, "=",
(misc:url_encode(Value))/binary>>
|| {Key, Value} <- Params],
Tail = iolist_to_binary([ParHead | ParTail]),
binary_to_list(<<Base/binary, $?, Tail/binary>>).
<<$&, Encoded/binary>> = iolist_to_binary(L),
<<Url/binary, $?, Encoded/binary>>.
url(Server, Path, Params) ->
case binary:split(base_url(Server, Path), <<"?">>) of
[Url] ->
url(Url, Params);
[Url, Extra] ->
Custom = [list_to_tuple(binary:split(P, <<"=">>))
|| P <- binary:split(Extra, <<"&">>, [global])],
url(Url, Custom++Params)
end.
-spec opt_type(ext_api_http_pool_size) -> fun((pos_integer()) -> pos_integer());
(ext_api_url) -> fun((binary()) -> binary());