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:
parent
167edacb5f
commit
911b8188d2
@ -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
|
||||||
StrDirectories = [{bitstring_to_list(X), bitstring_to_list(Y)} ||
|
{error, Reason} ->
|
||||||
{X,Y} <- Directories],
|
?ERROR_MSG("Problem decoding: ~s", [Body]),
|
||||||
% Find and save the replay nonce
|
{error, Reason};
|
||||||
Nonce = get_nonce(Head),
|
Directories ->
|
||||||
%% Return Map of Directories
|
StrDirectories = [{bitstring_to_list(X), bitstring_to_list(Y)} ||
|
||||||
NewDirs = maps:from_list(StrDirectories),
|
{X,Y} <- Directories],
|
||||||
{ok, NewDirs, Nonce};
|
Nonce = get_nonce(Head),
|
||||||
{ok, {{_, Code, _}, _Head, _Body}} ->
|
%% Return Map of Directories
|
||||||
?ERROR_MSG("Got unexpected status code from <~s>: ~B",
|
NewDirs = maps:from_list(StrDirectories),
|
||||||
[DirURL, Code]),
|
{ok, NewDirs, Nonce}
|
||||||
{error, unexpected_code};
|
end;
|
||||||
{error, Reason} ->
|
Error ->
|
||||||
?ERROR_MSG("Error requesting directory from <~s>: ~p",
|
failed_http_request(Error, Url)
|
||||||
[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),
|
{error, Reason} ->
|
||||||
TOSUrl = get_tos(Head),
|
?ERROR_MSG("Problem decoding: ~s", [Body]),
|
||||||
% Find and save the replay nonce
|
{error, Reason};
|
||||||
NewNonce = get_nonce(Head),
|
Return ->
|
||||||
{ok, {TOSUrl, Return}, NewNonce};
|
TOSUrl = get_tos(Head),
|
||||||
{ok, {{_, Code, _}, _Head, Body}} ->
|
NewNonce = get_nonce(Head),
|
||||||
?ERROR_MSG("Got unexpected status code from <~s>: ~B, Body: ~s",
|
{ok, {TOSUrl, Return}, NewNonce}
|
||||||
[NewAccURl, Code, Body]),
|
end;
|
||||||
{error, unexpected_code};
|
Error ->
|
||||||
{error, Reason} ->
|
failed_http_request(Error, Url)
|
||||||
?ERROR_MSG("Error requesting directory from <~s>: ~p",
|
|
||||||
[NewAccURl, Reason]),
|
|
||||||
{error, Reason}
|
|
||||||
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),
|
{error, Reason} ->
|
||||||
% Find and save the replay nonce
|
?ERROR_MSG("Problem decoding: ~s", [Body]),
|
||||||
NewNonce = get_nonce(Head),
|
{error, Reason};
|
||||||
{ok, Return, NewNonce};
|
Return ->
|
||||||
{ok, {{_, Code, _}, _Head, Body}} ->
|
NewNonce = get_nonce(Head),
|
||||||
?ERROR_MSG("Got unexpected status code from <~s>: ~B, Body: ~s",
|
{ok, Return, NewNonce}
|
||||||
[AccURl, Code, Body]),
|
end;
|
||||||
{error, unexpected_code};
|
Error ->
|
||||||
{error, Reason} ->
|
failed_http_request(Error, Url)
|
||||||
?ERROR_MSG("Error requesting directory from <~s>: ~p",
|
|
||||||
[AccURl, Reason]),
|
|
||||||
{error, Reason}
|
|
||||||
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),
|
{error, Reason} ->
|
||||||
TOSUrl = get_tos(Head),
|
?ERROR_MSG("Problem decoding: ~s", [Body]),
|
||||||
% Find and save the replay nonce
|
{error, Reason};
|
||||||
NewNonce = get_nonce(Head),
|
Return ->
|
||||||
{ok, {TOSUrl, Return}, NewNonce};
|
TOSUrl = get_tos(Head),
|
||||||
{ok, {{_, Code, _}, _Head, Body}} ->
|
NewNonce = get_nonce(Head),
|
||||||
?ERROR_MSG("Got unexpected status code from <~s>: ~B, Head: ~s",
|
{ok, {TOSUrl, Return}, NewNonce}
|
||||||
[AccURl, Code, Body]),
|
end;
|
||||||
{error, unexpected_code};
|
Error ->
|
||||||
{error, Reason} ->
|
failed_http_request(Error, Url)
|
||||||
?ERROR_MSG("Error requesting directory from <~s>: ~p",
|
|
||||||
[AccURl, Reason]),
|
|
||||||
{error, Reason}
|
|
||||||
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").
|
||||||
|
Loading…
Reference in New Issue
Block a user