aboutsummaryrefslogtreecommitdiff
path: root/src/rest.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/rest.erl')
-rw-r--r--src/rest.erl53
1 files changed, 32 insertions, 21 deletions
diff --git a/src/rest.erl b/src/rest.erl
index 99501ae56..0165d9fa7 100644
--- a/src/rest.erl
+++ b/src/rest.erl
@@ -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,