25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-12-22 17:28:25 +01:00

Allow returning HTTP headers in REST responses

This commit is contained in:
Evgeny Khramtsov 2019-04-19 15:16:47 +03:00
parent 7f14826564
commit 0789a145fc

View File

@ -80,9 +80,13 @@ patch(Server, Path, Params, Content) ->
request(Server, patch, Path, Params, ?CONTENT_TYPE, Data).
request(Server, Method, Path, Params, Mime, Data) ->
URI = to_list(url(Server, Path, Params)),
Opts = [{connect_timeout, ?CONNECT_TIMEOUT},
{timeout, ?HTTP_TIMEOUT}],
{Query, Opts} = case Params of
{_, _} -> Params;
_ -> {Params, []}
end,
URI = to_list(url(Server, Path, Query)),
HttpOpts = [{connect_timeout, ?CONNECT_TIMEOUT},
{timeout, ?HTTP_TIMEOUT}],
Hdrs = [{"connection", "keep-alive"},
{"Accept", "application/json"},
{"User-Agent", "ejabberd"}]
@ -94,17 +98,14 @@ request(Server, Method, Path, Params, Mime, Data) ->
{URI, Hdrs}
end,
Begin = os:timestamp(),
Result = try httpc:request(Method, Req, Opts, [{body_format, binary}]) of
{ok, {{_, Code, _}, _, <<>>}} ->
{ok, Code, []};
{ok, {{_, Code, _}, _, <<" ">>}} ->
{ok, Code, []};
{ok, {{_, Code, _}, _, <<"\r\n">>}} ->
{ok, Code, []};
{ok, {{_, Code, _}, _, Body}} ->
try jiffy:decode(Body) of
Result = try httpc:request(Method, Req, HttpOpts, [{body_format, binary}]) of
{ok, {{_, Code, _}, RetHdrs, Body}} ->
try decode_json(Body) of
JSon ->
{ok, Code, JSon}
case proplists:get_bool(return_headers, Opts) of
true -> {ok, Code, RetHdrs, JSon};
false -> {ok, Code, JSon}
end
catch
_:Error ->
?ERROR_MSG("HTTP response decode failed:~n"
@ -130,11 +131,6 @@ request(Server, Method, Path, Params, Mime, Data) ->
end,
ejabberd_hooks:run(backend_api_call, Server, [Server, Method, Path]),
case Result of
{ok, _, _} ->
End = os:timestamp(),
Elapsed = timer:now_diff(End, Begin) div 1000, %% time in ms
ejabberd_hooks:run(backend_api_response_time, Server,
[Server, Method, Path, Elapsed]);
{error, {http_error,{error,timeout}}} ->
ejabberd_hooks:run(backend_api_timeout, Server,
[Server, Method, Path]);
@ -143,7 +139,12 @@ request(Server, Method, Path, Params, Mime, Data) ->
[Server, Method, Path]);
{error, _} ->
ejabberd_hooks:run(backend_api_error, Server,
[Server, Method, Path])
[Server, Method, Path]);
_ ->
End = os:timestamp(),
Elapsed = timer:now_diff(End, Begin) div 1000, %% time in ms
ejabberd_hooks:run(backend_api_response_time, Server,
[Server, Method, Path, Elapsed])
end,
Result.
@ -168,6 +169,11 @@ encode_json(Content) ->
Encoded
end.
decode_json(<<>>) -> [];
decode_json(<<" ">>) -> [];
decode_json(<<"\r\n">>) -> [];
decode_json(Data) -> jiffy:decode(Data).
custom_headers(Server) ->
case ejabberd_config:get_option({ext_api_headers, Server},
<<>>) of