25
1
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:
Paweł Chmielowski 2018-04-23 17:40:44 +02:00
parent 9ed0357760
commit 5b730cdbf2
2 changed files with 45 additions and 34 deletions

View File

@ -56,7 +56,8 @@ init([]) ->
process_flag(trap_exit, true),
[code:add_patha(module_ebin_dir(Module))
|| {Module, _} <- installed()],
p1_http:start(),
application:start(inets),
inets:start(httpc, [{profile, ext_mod}]),
ejabberd_commands:register_commands(get_commands_spec()),
{ok, #state{}}.
@ -313,23 +314,22 @@ check(Package) when is_binary(Package) ->
%% -- archives and variables functions
geturl(Url) ->
geturl(Url, []).
geturl(Url, UsrOpts) ->
geturl(Url, [], UsrOpts).
geturl(Url, Hdrs, UsrOpts) ->
Host = case getenv("PROXY_SERVER", "", ":") of
[H, Port] -> [{proxy_host, H}, {proxy_port, list_to_integer(Port)}];
[H] -> [{proxy_host, H}, {proxy_port, 8080}];
_ -> []
case getenv("PROXY_SERVER", "", ":") of
[H, Port] ->
httpc:set_options([{proxy, {{H, list_to_integer(Port)}, []}}], ext_mod);
[H] ->
httpc:set_options([{proxy, {{H, 8080}, []}}], ext_mod);
_ ->
ok
end,
User = case getenv("PROXY_USER", "", [4]) of
[U, Pass] -> [{proxy_user, U}, {proxy_password, Pass}];
[U, Pass] -> [{proxy_auth, {U, Pass}}];
_ -> []
end,
case p1_http:request(get, Url, Hdrs, [], Host++User++UsrOpts++[{version, "HTTP/1.0"}]) of
{ok, 200, Headers, Response} ->
case httpc:request(get, {Url, []}, User, [{body_format, binary}], ext_mod) of
{ok, {{_, 200, _}, Headers, Response}} ->
{ok, Headers, Response};
{ok, Code, _Headers, Response} ->
{ok, {{_, Code, _}, _Headers, Response}} ->
{error, {Code, Response}};
{error, Reason} ->
{error, Reason}

View File

@ -36,9 +36,9 @@
-define(CONNECT_TIMEOUT, 8000).
start(Host) ->
p1_http:start(),
Pool_size = ejabberd_config:get_option({ext_api_http_pool_size, Host}, 100),
p1_http:set_pool_size(Pool_size).
application:start(inets),
Size = ejabberd_config:get_option({ext_api_http_pool_size, Host}, 100),
httpc:set_options([{max_sessions, Size}]).
stop(_Host) ->
ok.
@ -58,41 +58,46 @@ with_retry(Method, Args, Retries, MaxRetries, Backoff) ->
end.
get(Server, Path) ->
request(Server, get, Path, [], <<"application/json">>, <<>>).
request(Server, get, Path, [], "application/json", <<>>).
get(Server, Path, Params) ->
request(Server, get, Path, Params, <<"application/json">>, <<>>).
request(Server, get, Path, Params, "application/json", <<>>).
delete(Server, Path) ->
request(Server, delete, Path, [], <<"application/json">>, <<>>).
request(Server, delete, Path, [], "application/json", <<>>).
post(Server, Path, Params, 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) ->
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) ->
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) ->
URI = url(Server, Path, Params),
URI = to_list(url(Server, Path, Params)),
Opts = [{connect_timeout, ?CONNECT_TIMEOUT},
{timeout, ?HTTP_TIMEOUT}],
Hdrs = [{<<"connection">>, <<"keep-alive">>},
{<<"content-type">>, Mime},
{<<"User-Agent">>, <<"ejabberd">>}],
Hdrs = [{"connection", "keep-alive"},
{"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(),
Result = case catch p1_http:request(Method, URI, Hdrs, Data, Opts) of
{ok, Code, _, <<>>} ->
Result = try httpc:request(Method, Req, Opts, [{body_format, binary}]) of
{ok, {{_, Code, _}, _, <<>>}} ->
{ok, Code, []};
{ok, Code, _, <<" ">>} ->
{ok, {{_, Code, _}, _, <<" ">>}} ->
{ok, Code, []};
{ok, Code, _, <<"\r\n">>} ->
{ok, {{_, Code, _}, _, <<"\r\n">>}} ->
{ok, Code, []};
{ok, Code, _, Body} ->
{ok, {{_, Code, _}, _, Body}} ->
try jiffy:decode(Body) of
JSon ->
{ok, Code, JSon}
@ -110,8 +115,9 @@ request(Server, Method, Path, Params, Mime, Data) ->
"** URI = ~s~n"
"** Err = ~p",
[URI, Reason]),
{error, {http_error, {error, Reason}}};
{'EXIT', Reason} ->
{error, {http_error, {error, Reason}}}
catch
exit:Reason ->
?ERROR_MSG("HTTP request failed:~n"
"** URI = ~s~n"
"** Err = ~p",
@ -141,6 +147,11 @@ request(Server, Method, Path, Params, Mime, Data) ->
%%% HTTP helpers
%%%----------------------------------------------------------------------
to_list(V) when is_binary(V) ->
binary_to_list(V);
to_list(V) ->
V.
encode_json(Content) ->
case catch jiffy:encode(Content) of
{'EXIT', Reason} ->
@ -164,7 +175,7 @@ base_url(Server, Path) ->
Base = ejabberd_config:get_option({ext_api_url, Server},
<<"http://localhost/api">>),
case binary:last(Base) of
47 -> <<Base/binary, BPath/binary>>;
$/ -> <<Base/binary, BPath/binary>>;
_ -> <<Base/binary, "/", BPath/binary>>
end
end,