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:
parent
7f14826564
commit
0789a145fc
44
src/rest.erl
44
src/rest.erl
@ -80,9 +80,13 @@ patch(Server, Path, Params, Content) ->
|
|||||||
request(Server, patch, Path, Params, ?CONTENT_TYPE, Data).
|
request(Server, patch, Path, Params, ?CONTENT_TYPE, Data).
|
||||||
|
|
||||||
request(Server, Method, Path, Params, Mime, Data) ->
|
request(Server, Method, Path, Params, Mime, Data) ->
|
||||||
URI = to_list(url(Server, Path, Params)),
|
{Query, Opts} = case Params of
|
||||||
Opts = [{connect_timeout, ?CONNECT_TIMEOUT},
|
{_, _} -> Params;
|
||||||
{timeout, ?HTTP_TIMEOUT}],
|
_ -> {Params, []}
|
||||||
|
end,
|
||||||
|
URI = to_list(url(Server, Path, Query)),
|
||||||
|
HttpOpts = [{connect_timeout, ?CONNECT_TIMEOUT},
|
||||||
|
{timeout, ?HTTP_TIMEOUT}],
|
||||||
Hdrs = [{"connection", "keep-alive"},
|
Hdrs = [{"connection", "keep-alive"},
|
||||||
{"Accept", "application/json"},
|
{"Accept", "application/json"},
|
||||||
{"User-Agent", "ejabberd"}]
|
{"User-Agent", "ejabberd"}]
|
||||||
@ -94,17 +98,14 @@ request(Server, Method, Path, Params, Mime, Data) ->
|
|||||||
{URI, Hdrs}
|
{URI, Hdrs}
|
||||||
end,
|
end,
|
||||||
Begin = os:timestamp(),
|
Begin = os:timestamp(),
|
||||||
Result = try httpc:request(Method, Req, Opts, [{body_format, binary}]) of
|
Result = try httpc:request(Method, Req, HttpOpts, [{body_format, binary}]) of
|
||||||
{ok, {{_, Code, _}, _, <<>>}} ->
|
{ok, {{_, Code, _}, RetHdrs, Body}} ->
|
||||||
{ok, Code, []};
|
try decode_json(Body) of
|
||||||
{ok, {{_, Code, _}, _, <<" ">>}} ->
|
|
||||||
{ok, Code, []};
|
|
||||||
{ok, {{_, Code, _}, _, <<"\r\n">>}} ->
|
|
||||||
{ok, Code, []};
|
|
||||||
{ok, {{_, Code, _}, _, Body}} ->
|
|
||||||
try jiffy:decode(Body) of
|
|
||||||
JSon ->
|
JSon ->
|
||||||
{ok, Code, JSon}
|
case proplists:get_bool(return_headers, Opts) of
|
||||||
|
true -> {ok, Code, RetHdrs, JSon};
|
||||||
|
false -> {ok, Code, JSon}
|
||||||
|
end
|
||||||
catch
|
catch
|
||||||
_:Error ->
|
_:Error ->
|
||||||
?ERROR_MSG("HTTP response decode failed:~n"
|
?ERROR_MSG("HTTP response decode failed:~n"
|
||||||
@ -130,11 +131,6 @@ request(Server, Method, Path, Params, Mime, Data) ->
|
|||||||
end,
|
end,
|
||||||
ejabberd_hooks:run(backend_api_call, Server, [Server, Method, Path]),
|
ejabberd_hooks:run(backend_api_call, Server, [Server, Method, Path]),
|
||||||
case Result of
|
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}}} ->
|
{error, {http_error,{error,timeout}}} ->
|
||||||
ejabberd_hooks:run(backend_api_timeout, Server,
|
ejabberd_hooks:run(backend_api_timeout, Server,
|
||||||
[Server, Method, Path]);
|
[Server, Method, Path]);
|
||||||
@ -143,7 +139,12 @@ request(Server, Method, Path, Params, Mime, Data) ->
|
|||||||
[Server, Method, Path]);
|
[Server, Method, Path]);
|
||||||
{error, _} ->
|
{error, _} ->
|
||||||
ejabberd_hooks:run(backend_api_error, Server,
|
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,
|
end,
|
||||||
Result.
|
Result.
|
||||||
|
|
||||||
@ -168,6 +169,11 @@ encode_json(Content) ->
|
|||||||
Encoded
|
Encoded
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
decode_json(<<>>) -> [];
|
||||||
|
decode_json(<<" ">>) -> [];
|
||||||
|
decode_json(<<"\r\n">>) -> [];
|
||||||
|
decode_json(Data) -> jiffy:decode(Data).
|
||||||
|
|
||||||
custom_headers(Server) ->
|
custom_headers(Server) ->
|
||||||
case ejabberd_config:get_option({ext_api_headers, Server},
|
case ejabberd_config:get_option({ext_api_headers, Server},
|
||||||
<<>>) of
|
<<>>) of
|
||||||
|
Loading…
Reference in New Issue
Block a user