25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-26 16:26:24 +01:00

Refactor the http response handlers.

Encapsulate some dangerous calls with try catch.
This commit is contained in:
Konstantinos Kallas 2017-06-09 19:47:50 +03:00
parent 167edacb5f
commit 911b8188d2

View File

@ -23,33 +23,31 @@
-spec directory(url()) -> -spec directory(url()) ->
{ok, map(), nonce()} | {error, _}. {ok, map(), nonce()} | {error, _}.
directory(DirURL) -> directory(Url) ->
Options = [], Options = [],
HttpOptions = [{timeout, ?REQUEST_TIMEOUT}], HttpOptions = [{timeout, ?REQUEST_TIMEOUT}],
case httpc:request(get, {DirURL, []}, HttpOptions, Options) of case httpc:request(get, {Url, []}, HttpOptions, Options) of
{ok, {{_, Code, _}, Head, Body}} when Code >= 200, Code =< 299 -> {ok, {{_, Code, _}, Head, Body}} when Code >= 200, Code =< 299 ->
%% Decode the json string %% Decode the json string
{Directories} = jiffy:decode(Body), case decode(Body) of
{error, Reason} ->
?ERROR_MSG("Problem decoding: ~s", [Body]),
{error, Reason};
Directories ->
StrDirectories = [{bitstring_to_list(X), bitstring_to_list(Y)} || StrDirectories = [{bitstring_to_list(X), bitstring_to_list(Y)} ||
{X,Y} <- Directories], {X,Y} <- Directories],
% Find and save the replay nonce
Nonce = get_nonce(Head), Nonce = get_nonce(Head),
%% Return Map of Directories %% Return Map of Directories
NewDirs = maps:from_list(StrDirectories), NewDirs = maps:from_list(StrDirectories),
{ok, NewDirs, Nonce}; {ok, NewDirs, Nonce}
{ok, {{_, Code, _}, _Head, _Body}} -> end;
?ERROR_MSG("Got unexpected status code from <~s>: ~B", Error ->
[DirURL, Code]), failed_http_request(Error, Url)
{error, unexpected_code};
{error, Reason} ->
?ERROR_MSG("Error requesting directory from <~s>: ~p",
[DirURL, Reason]),
{error, Reason}
end. end.
-spec new_account(url(), jose_jwk:key(), proplist(), nonce()) -> -spec new_account(url(), jose_jwk:key(), proplist(), nonce()) ->
{ok, {url(), proplist()}, nonce()} | {error, _}. {ok, {url(), proplist()}, nonce()} | {error, _}.
new_account(NewAccURl, PrivateKey, Req, Nonce) -> new_account(Url, PrivateKey, Req, Nonce) ->
%% Make the request body %% Make the request body
ReqBody = jiffy:encode({[{ <<"resource">>, <<"new-reg">>}] ++ Req}), ReqBody = jiffy:encode({[{ <<"resource">>, <<"new-reg">>}] ++ Req}),
{_, SignedBody} = sign_json_jose(PrivateKey, ReqBody, Nonce), {_, SignedBody} = sign_json_jose(PrivateKey, ReqBody, Nonce),
@ -58,27 +56,24 @@ new_account(NewAccURl, PrivateKey, Req, Nonce) ->
Options = [], Options = [],
HttpOptions = [{timeout, ?REQUEST_TIMEOUT}], HttpOptions = [{timeout, ?REQUEST_TIMEOUT}],
case httpc:request(post, case httpc:request(post,
{NewAccURl, [], "application/jose+json", FinalBody}, HttpOptions, Options) of {Url, [], "application/jose+json", FinalBody}, HttpOptions, Options) of
{ok, {{_, Code, _}, Head, Body}} when Code >= 200, Code =< 299 -> {ok, {{_, Code, _}, Head, Body}} when Code >= 200, Code =< 299 ->
%% Decode the json string case decode(Body) of
{Return} = jiffy:decode(Body),
TOSUrl = get_tos(Head),
% Find and save the replay nonce
NewNonce = get_nonce(Head),
{ok, {TOSUrl, Return}, NewNonce};
{ok, {{_, Code, _}, _Head, Body}} ->
?ERROR_MSG("Got unexpected status code from <~s>: ~B, Body: ~s",
[NewAccURl, Code, Body]),
{error, unexpected_code};
{error, Reason} -> {error, Reason} ->
?ERROR_MSG("Error requesting directory from <~s>: ~p", ?ERROR_MSG("Problem decoding: ~s", [Body]),
[NewAccURl, Reason]), {error, Reason};
{error, Reason} Return ->
TOSUrl = get_tos(Head),
NewNonce = get_nonce(Head),
{ok, {TOSUrl, Return}, NewNonce}
end;
Error ->
failed_http_request(Error, Url)
end. end.
-spec update_account(url(), jose_jwk:key(), proplist(), nonce()) -> -spec update_account(url(), jose_jwk:key(), proplist(), nonce()) ->
{ok, proplist(), nonce()} | {error, _}. {ok, proplist(), nonce()} | {error, _}.
update_account(AccURl, PrivateKey, Req, Nonce) -> update_account(Url, PrivateKey, Req, Nonce) ->
%% Make the request body %% Make the request body
ReqBody = jiffy:encode({[{ <<"resource">>, <<"reg">>}] ++ Req}), ReqBody = jiffy:encode({[{ <<"resource">>, <<"reg">>}] ++ Req}),
{_, SignedBody} = sign_json_jose(PrivateKey, ReqBody, Nonce), {_, SignedBody} = sign_json_jose(PrivateKey, ReqBody, Nonce),
@ -87,26 +82,23 @@ update_account(AccURl, PrivateKey, Req, Nonce) ->
Options = [], Options = [],
HttpOptions = [{timeout, ?REQUEST_TIMEOUT}], HttpOptions = [{timeout, ?REQUEST_TIMEOUT}],
case httpc:request(post, case httpc:request(post,
{AccURl, [], "application/jose+json", FinalBody}, HttpOptions, Options) of {Url, [], "application/jose+json", FinalBody}, HttpOptions, Options) of
{ok, {{_, Code, _}, Head, Body}} when Code >= 200, Code =< 299 -> {ok, {{_, Code, _}, Head, Body}} when Code >= 200, Code =< 299 ->
%% Decode the json string case decode(Body) of
{Return} = jiffy:decode(Body),
% Find and save the replay nonce
NewNonce = get_nonce(Head),
{ok, Return, NewNonce};
{ok, {{_, Code, _}, _Head, Body}} ->
?ERROR_MSG("Got unexpected status code from <~s>: ~B, Body: ~s",
[AccURl, Code, Body]),
{error, unexpected_code};
{error, Reason} -> {error, Reason} ->
?ERROR_MSG("Error requesting directory from <~s>: ~p", ?ERROR_MSG("Problem decoding: ~s", [Body]),
[AccURl, Reason]), {error, Reason};
{error, Reason} Return ->
NewNonce = get_nonce(Head),
{ok, Return, NewNonce}
end;
Error ->
failed_http_request(Error, Url)
end. end.
-spec get_account(url(), jose_jwk:key(), nonce()) -> -spec get_account(url(), jose_jwk:key(), nonce()) ->
{ok, {url(), proplist()}, nonce()} | {error, _}. {ok, {url(), proplist()}, nonce()} | {error, _}.
get_account(AccURl, PrivateKey, Nonce) -> get_account(Url, PrivateKey, Nonce) ->
%% Make the request body %% Make the request body
ReqBody = jiffy:encode({[{<<"resource">>, <<"reg">>}]}), ReqBody = jiffy:encode({[{<<"resource">>, <<"reg">>}]}),
%% Jose Sign %% Jose Sign
@ -116,22 +108,19 @@ get_account(AccURl, PrivateKey, Nonce) ->
Options = [], Options = [],
HttpOptions = [{timeout, ?REQUEST_TIMEOUT}], HttpOptions = [{timeout, ?REQUEST_TIMEOUT}],
case httpc:request(post, case httpc:request(post,
{AccURl, [], "application/jose+json", FinalBody}, HttpOptions, Options) of {Url, [], "application/jose+json", FinalBody}, HttpOptions, Options) of
{ok, {{_, Code, _}, Head, Body}} when Code >= 200, Code =< 299 -> {ok, {{_, Code, _}, Head, Body}} when Code >= 200, Code =< 299 ->
%% Decode the json string case decode(Body) of
{Return} = jiffy:decode(Body),
TOSUrl = get_tos(Head),
% Find and save the replay nonce
NewNonce = get_nonce(Head),
{ok, {TOSUrl, Return}, NewNonce};
{ok, {{_, Code, _}, _Head, Body}} ->
?ERROR_MSG("Got unexpected status code from <~s>: ~B, Head: ~s",
[AccURl, Code, Body]),
{error, unexpected_code};
{error, Reason} -> {error, Reason} ->
?ERROR_MSG("Error requesting directory from <~s>: ~p", ?ERROR_MSG("Problem decoding: ~s", [Body]),
[AccURl, Reason]), {error, Reason};
{error, Reason} Return ->
TOSUrl = get_tos(Head),
NewNonce = get_nonce(Head),
{ok, {TOSUrl, Return}, NewNonce}
end;
Error ->
failed_http_request(Error, Url)
end. end.
@ -178,6 +167,26 @@ sign_json_jose(Key, Json, Nonce) ->
%% Signed Message %% Signed Message
jose_jws:sign(Key, Json, JwsObj). jose_jws:sign(Key, Json, JwsObj).
decode(Json) ->
try
{Result} = jiffy:decode(Json),
Result
catch
_:Reason ->
{error, Reason}
end.
-spec failed_http_request({ok, _} | {error, _}, url()) -> {error, _}.
failed_http_request({ok, {{_, Code, _}, _Head, Body}}, Url) ->
?ERROR_MSG("Got unexpected status code from <~s>: ~B, Body: ~s",
[Url, Code, Body]),
{error, unexpected_code};
failed_http_request({error, Reason}, Url) ->
?ERROR_MSG("Error making a request to <~s>: ~p",
[Url, Reason]),
{error, Reason}.
%% %%
%% Debugging Funcs -- They are only used for the development phase %% Debugging Funcs -- They are only used for the development phase
%% %%
@ -213,5 +222,5 @@ generate_key() ->
%% Just a test %% Just a test
scenario0(KeyFile) -> scenario0(KeyFile) ->
PrivateKey = jose_jwk:from_file(KeyFile), PrivateKey = jose_jwk:from_file(KeyFile),
% scenario("http://localhost:4000", "2", PrivateKey). scenario("http://localhost:4000", "2", PrivateKey).
new_user_scenario("http://localhost:4000"). % new_user_scenario("http://localhost:4000").