Make routes not care about trailing slashes

This commit is contained in:
Maciej Szlosarczyk 2019-05-30 12:22:42 +03:00
parent 4566e14b31
commit 6b4814a60d
No known key found for this signature in database
GPG key ID: 41D62D42D3B0D765
3 changed files with 43 additions and 37 deletions

View file

@ -5,25 +5,6 @@
-define(validCommands, ["hello", "login", "logout", "check", "info", "poll", -define(validCommands, ["hello", "login", "logout", "check", "info", "poll",
"create", "delete", "renew", "update", "transfer"]). "create", "delete", "renew", "update", "transfer"]).
%% By default, return test values. This might cause issues in production.
-define(baseSessionUrl,
case application:get_env(epp_proxy, epp_session_url) of
undefined -> "https://registry.test/epp/session/";
{ok, Value} -> Value
end).
-define(baseCommandUrl,
case application:get_env(epp_proxy, epp_command_url) of
undefined -> "https://registry.test/epp/command/";
{ok, Value} -> Value
end).
-define(baseErrorUrl,
case application:get_env(epp_proxy, epp_error_url) of
undefined -> "https://registry.test/epp/error/";
{ok, Value} -> Value
end).
%% Save yourself some checking beforehand. %% Save yourself some checking beforehand.
is_valid_epp_command(Command) -> is_valid_epp_command(Command) ->
lists:member(Command, ?validCommands). lists:member(Command, ?validCommands).
@ -50,31 +31,56 @@ route_request(Command) when is_list(Command) -> url_map(Command).
url_map(Command) when is_list(Command) -> url_map(Command) when is_list(Command) ->
case Command of case Command of
%% Session commands %% Session commands
"hello" -> string:concat(base_session_url(), Command); "hello" -> unicode:characters_to_list([base_session_url(), Command]);
"login" -> string:concat(base_session_url(), Command); "login" -> unicode:characters_to_list([base_session_url(), Command]);
"logout" -> string:concat(base_session_url(), Command); "logout" -> unicode:characters_to_list([base_session_url(), Command]);
%% Poll commands %% Poll commands
"check" -> string:concat(base_command_url(), Command); "check" -> unicode:characters_to_list([base_command_url(), Command]);
"info" -> string:concat(base_command_url(), Command); "info" -> unicode:characters_to_list([base_command_url(), Command]);
"poll" -> string:concat(base_command_url(), Command); "poll" -> unicode:characters_to_list([base_command_url(), Command]);
%% Transform commands %% Transform commands
"create" -> string:concat(base_command_url(), Command); "create" -> unicode:characters_to_list([base_command_url(), Command]);
"delete" -> string:concat(base_command_url(), Command); "delete" -> unicode:characters_to_list([base_command_url(), Command]);
"renew" -> string:concat(base_command_url(), Command); "renew" -> unicode:characters_to_list([base_command_url(), Command]);
"update" -> string:concat(base_command_url(), Command); "update" -> unicode:characters_to_list([base_command_url(), Command]);
% Transfer is both poll and query % Transfer is both poll and query
"transfer" -> string:concat(base_command_url(), Command); "transfer" -> unicode:characters_to_list([base_command_url(), Command]);
% Error route % Error route
"error" -> base_error_url() "error" -> base_error_url()
% Anything else should fail. % Anything else should fail.
end. end.
%% Just return the macros as defined %% This allows the person who configures proxy to not care about trailing
%% slashes in HTTP.
%% 47 is the equivalent for "/"
appendable_route(Route) ->
case lists:last(Route) of
47 -> Route;
_ -> unicode:characters_to_list([Route, "/"])
end.
parametrizable_route(Route) ->
case lists:last(Route) of
47 -> lists:droplast(Route);
_ -> Route
end.
%% Every time a request is made, this will go to ETS to check what's the route,
%% But that is fast enough to not be noticed by anyone.
base_session_url() -> base_session_url() ->
?baseSessionUrl. case application:get_env(epp_proxy, epp_session_url) of
undefined -> "https://registry.test/epp/session/";
{ok, Value} -> appendable_route(Value)
end.
base_command_url() -> base_command_url() ->
?baseCommandUrl. case application:get_env(epp_proxy, epp_command_url) of
undefined -> "https://registry.test/epp/command/";
{ok, Value} -> appendable_route(Value)
end.
base_error_url() -> base_error_url() ->
?baseErrorUrl. case application:get_env(epp_proxy, epp_error_url) of
undefined -> "https://registry.test/epp/error";
{ok, Value} -> parametrizable_route(Value)
end.

View file

@ -21,7 +21,7 @@ error_request_builder_test() ->
message => <<"Expected better XML">>, message => <<"Expected better XML">>,
headers => [{"User-Agent", <<"EPP proxy">>}]}, headers => [{"User-Agent", <<"EPP proxy">>}]},
Request = epp_http_client:request_builder(RequestMap), Request = epp_http_client:request_builder(RequestMap),
ExpectedTuple = {epp_error_request,get,"https://registry.test/epp/error/", ExpectedTuple = {epp_error_request,get,"https://registry.test/epp/error",
[{<<"code">>,<<"2001">>}, [{<<"code">>,<<"2001">>},
{<<"msg">>,<<"Expected better XML">>}, {<<"msg">>,<<"Expected better XML">>},
{<<"clTRID">>,"EE-123456789"}], {<<"clTRID">>,"EE-123456789"}],

View file

@ -64,5 +64,5 @@ transfer_url_test() ->
?assertEqual("https://registry.test/epp/command/transfer", epp_router:route_request(<<"transfer">>)). ?assertEqual("https://registry.test/epp/command/transfer", epp_router:route_request(<<"transfer">>)).
error_url_test() -> error_url_test() ->
?assertEqual("https://registry.test/epp/error/", epp_router:route_request("error")), ?assertEqual("https://registry.test/epp/error", epp_router:route_request("error")),
?assertEqual("https://registry.test/epp/error/", epp_router:route_request(<<"error">>)). ?assertEqual("https://registry.test/epp/error", epp_router:route_request(<<"error">>)).