mirror of
https://github.com/processone/ejabberd.git
synced 2024-12-22 17:28:25 +01:00
Use httpc directly instead of using p1_http wrapper
This commit is contained in:
parent
9ed0357760
commit
5b730cdbf2
@ -56,7 +56,8 @@ init([]) ->
|
|||||||
process_flag(trap_exit, true),
|
process_flag(trap_exit, true),
|
||||||
[code:add_patha(module_ebin_dir(Module))
|
[code:add_patha(module_ebin_dir(Module))
|
||||||
|| {Module, _} <- installed()],
|
|| {Module, _} <- installed()],
|
||||||
p1_http:start(),
|
application:start(inets),
|
||||||
|
inets:start(httpc, [{profile, ext_mod}]),
|
||||||
ejabberd_commands:register_commands(get_commands_spec()),
|
ejabberd_commands:register_commands(get_commands_spec()),
|
||||||
{ok, #state{}}.
|
{ok, #state{}}.
|
||||||
|
|
||||||
@ -313,23 +314,22 @@ check(Package) when is_binary(Package) ->
|
|||||||
%% -- archives and variables functions
|
%% -- archives and variables functions
|
||||||
|
|
||||||
geturl(Url) ->
|
geturl(Url) ->
|
||||||
geturl(Url, []).
|
case getenv("PROXY_SERVER", "", ":") of
|
||||||
geturl(Url, UsrOpts) ->
|
[H, Port] ->
|
||||||
geturl(Url, [], UsrOpts).
|
httpc:set_options([{proxy, {{H, list_to_integer(Port)}, []}}], ext_mod);
|
||||||
geturl(Url, Hdrs, UsrOpts) ->
|
[H] ->
|
||||||
Host = case getenv("PROXY_SERVER", "", ":") of
|
httpc:set_options([{proxy, {{H, 8080}, []}}], ext_mod);
|
||||||
[H, Port] -> [{proxy_host, H}, {proxy_port, list_to_integer(Port)}];
|
_ ->
|
||||||
[H] -> [{proxy_host, H}, {proxy_port, 8080}];
|
ok
|
||||||
_ -> []
|
|
||||||
end,
|
end,
|
||||||
User = case getenv("PROXY_USER", "", [4]) of
|
User = case getenv("PROXY_USER", "", [4]) of
|
||||||
[U, Pass] -> [{proxy_user, U}, {proxy_password, Pass}];
|
[U, Pass] -> [{proxy_auth, {U, Pass}}];
|
||||||
_ -> []
|
_ -> []
|
||||||
end,
|
end,
|
||||||
case p1_http:request(get, Url, Hdrs, [], Host++User++UsrOpts++[{version, "HTTP/1.0"}]) of
|
case httpc:request(get, {Url, []}, User, [{body_format, binary}], ext_mod) of
|
||||||
{ok, 200, Headers, Response} ->
|
{ok, {{_, 200, _}, Headers, Response}} ->
|
||||||
{ok, Headers, Response};
|
{ok, Headers, Response};
|
||||||
{ok, Code, _Headers, Response} ->
|
{ok, {{_, Code, _}, _Headers, Response}} ->
|
||||||
{error, {Code, Response}};
|
{error, {Code, Response}};
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
{error, Reason}
|
{error, Reason}
|
||||||
|
53
src/rest.erl
53
src/rest.erl
@ -36,9 +36,9 @@
|
|||||||
-define(CONNECT_TIMEOUT, 8000).
|
-define(CONNECT_TIMEOUT, 8000).
|
||||||
|
|
||||||
start(Host) ->
|
start(Host) ->
|
||||||
p1_http:start(),
|
application:start(inets),
|
||||||
Pool_size = ejabberd_config:get_option({ext_api_http_pool_size, Host}, 100),
|
Size = ejabberd_config:get_option({ext_api_http_pool_size, Host}, 100),
|
||||||
p1_http:set_pool_size(Pool_size).
|
httpc:set_options([{max_sessions, Size}]).
|
||||||
|
|
||||||
stop(_Host) ->
|
stop(_Host) ->
|
||||||
ok.
|
ok.
|
||||||
@ -58,41 +58,46 @@ with_retry(Method, Args, Retries, MaxRetries, Backoff) ->
|
|||||||
end.
|
end.
|
||||||
|
|
||||||
get(Server, Path) ->
|
get(Server, Path) ->
|
||||||
request(Server, get, Path, [], <<"application/json">>, <<>>).
|
request(Server, get, Path, [], "application/json", <<>>).
|
||||||
get(Server, Path, Params) ->
|
get(Server, Path, Params) ->
|
||||||
request(Server, get, Path, Params, <<"application/json">>, <<>>).
|
request(Server, get, Path, Params, "application/json", <<>>).
|
||||||
|
|
||||||
delete(Server, Path) ->
|
delete(Server, Path) ->
|
||||||
request(Server, delete, Path, [], <<"application/json">>, <<>>).
|
request(Server, delete, Path, [], "application/json", <<>>).
|
||||||
|
|
||||||
post(Server, Path, Params, Content) ->
|
post(Server, Path, Params, Content) ->
|
||||||
Data = encode_json(Content),
|
Data = encode_json(Content),
|
||||||
request(Server, post, Path, Params, <<"application/json">>, Data).
|
request(Server, post, Path, Params, "application/json", Data).
|
||||||
|
|
||||||
put(Server, Path, Params, Content) ->
|
put(Server, Path, Params, Content) ->
|
||||||
Data = encode_json(Content),
|
Data = encode_json(Content),
|
||||||
request(Server, put, Path, Params, <<"application/json">>, Data).
|
request(Server, put, Path, Params, "application/json", Data).
|
||||||
|
|
||||||
patch(Server, Path, Params, Content) ->
|
patch(Server, Path, Params, Content) ->
|
||||||
Data = encode_json(Content),
|
Data = encode_json(Content),
|
||||||
request(Server, patch, Path, Params, <<"application/json">>, Data).
|
request(Server, patch, Path, Params, "application/json", Data).
|
||||||
|
|
||||||
request(Server, Method, Path, Params, Mime, Data) ->
|
request(Server, Method, Path, Params, Mime, Data) ->
|
||||||
URI = url(Server, Path, Params),
|
URI = to_list(url(Server, Path, Params)),
|
||||||
Opts = [{connect_timeout, ?CONNECT_TIMEOUT},
|
Opts = [{connect_timeout, ?CONNECT_TIMEOUT},
|
||||||
{timeout, ?HTTP_TIMEOUT}],
|
{timeout, ?HTTP_TIMEOUT}],
|
||||||
Hdrs = [{<<"connection">>, <<"keep-alive">>},
|
Hdrs = [{"connection", "keep-alive"},
|
||||||
{<<"content-type">>, Mime},
|
{"User-Agent", "ejabberd"}],
|
||||||
{<<"User-Agent">>, <<"ejabberd">>}],
|
Req = if
|
||||||
|
(Method =:= post) orelse (Method =:= patch) orelse (Method =:= put) orelse (Method =:= delete) ->
|
||||||
|
{URI, Hdrs, to_list(Mime), Data};
|
||||||
|
true ->
|
||||||
|
{URI, Hdrs}
|
||||||
|
end,
|
||||||
Begin = os:timestamp(),
|
Begin = os:timestamp(),
|
||||||
Result = case catch p1_http:request(Method, URI, Hdrs, Data, Opts) of
|
Result = try httpc:request(Method, Req, Opts, [{body_format, binary}]) of
|
||||||
{ok, Code, _, <<>>} ->
|
{ok, {{_, Code, _}, _, <<>>}} ->
|
||||||
{ok, Code, []};
|
{ok, Code, []};
|
||||||
{ok, Code, _, <<" ">>} ->
|
{ok, {{_, Code, _}, _, <<" ">>}} ->
|
||||||
{ok, Code, []};
|
{ok, Code, []};
|
||||||
{ok, Code, _, <<"\r\n">>} ->
|
{ok, {{_, Code, _}, _, <<"\r\n">>}} ->
|
||||||
{ok, Code, []};
|
{ok, Code, []};
|
||||||
{ok, Code, _, Body} ->
|
{ok, {{_, Code, _}, _, Body}} ->
|
||||||
try jiffy:decode(Body) of
|
try jiffy:decode(Body) of
|
||||||
JSon ->
|
JSon ->
|
||||||
{ok, Code, JSon}
|
{ok, Code, JSon}
|
||||||
@ -110,8 +115,9 @@ request(Server, Method, Path, Params, Mime, Data) ->
|
|||||||
"** URI = ~s~n"
|
"** URI = ~s~n"
|
||||||
"** Err = ~p",
|
"** Err = ~p",
|
||||||
[URI, Reason]),
|
[URI, Reason]),
|
||||||
{error, {http_error, {error, Reason}}};
|
{error, {http_error, {error, Reason}}}
|
||||||
{'EXIT', Reason} ->
|
catch
|
||||||
|
exit:Reason ->
|
||||||
?ERROR_MSG("HTTP request failed:~n"
|
?ERROR_MSG("HTTP request failed:~n"
|
||||||
"** URI = ~s~n"
|
"** URI = ~s~n"
|
||||||
"** Err = ~p",
|
"** Err = ~p",
|
||||||
@ -141,6 +147,11 @@ request(Server, Method, Path, Params, Mime, Data) ->
|
|||||||
%%% HTTP helpers
|
%%% HTTP helpers
|
||||||
%%%----------------------------------------------------------------------
|
%%%----------------------------------------------------------------------
|
||||||
|
|
||||||
|
to_list(V) when is_binary(V) ->
|
||||||
|
binary_to_list(V);
|
||||||
|
to_list(V) ->
|
||||||
|
V.
|
||||||
|
|
||||||
encode_json(Content) ->
|
encode_json(Content) ->
|
||||||
case catch jiffy:encode(Content) of
|
case catch jiffy:encode(Content) of
|
||||||
{'EXIT', Reason} ->
|
{'EXIT', Reason} ->
|
||||||
@ -164,7 +175,7 @@ base_url(Server, Path) ->
|
|||||||
Base = ejabberd_config:get_option({ext_api_url, Server},
|
Base = ejabberd_config:get_option({ext_api_url, Server},
|
||||||
<<"http://localhost/api">>),
|
<<"http://localhost/api">>),
|
||||||
case binary:last(Base) of
|
case binary:last(Base) of
|
||||||
47 -> <<Base/binary, BPath/binary>>;
|
$/ -> <<Base/binary, BPath/binary>>;
|
||||||
_ -> <<Base/binary, "/", BPath/binary>>
|
_ -> <<Base/binary, "/", BPath/binary>>
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
|
Loading…
Reference in New Issue
Block a user