|
|
@@ -47,7 +47,7 @@
|
|
|
namespace/0
|
|
|
]).
|
|
|
|
|
|
--export([check_ssl_opts/2, validate_method/1]).
|
|
|
+-export([check_ssl_opts/2, validate_method/1, join_paths/2]).
|
|
|
|
|
|
-type connect_timeout() :: emqx_schema:duration() | infinity.
|
|
|
-type pool_type() :: random | hash.
|
|
|
@@ -458,7 +458,7 @@ preprocess_request(
|
|
|
} = Req
|
|
|
) ->
|
|
|
#{
|
|
|
- method => emqx_plugin_libs_rule:preproc_tmpl(bin(Method)),
|
|
|
+ method => emqx_plugin_libs_rule:preproc_tmpl(to_bin(Method)),
|
|
|
path => emqx_plugin_libs_rule:preproc_tmpl(Path),
|
|
|
body => maybe_preproc_tmpl(body, Req),
|
|
|
headers => preproc_headers(Headers),
|
|
|
@@ -471,8 +471,8 @@ preproc_headers(Headers) when is_map(Headers) ->
|
|
|
fun(K, V, Acc) ->
|
|
|
[
|
|
|
{
|
|
|
- emqx_plugin_libs_rule:preproc_tmpl(bin(K)),
|
|
|
- emqx_plugin_libs_rule:preproc_tmpl(bin(V))
|
|
|
+ emqx_plugin_libs_rule:preproc_tmpl(to_bin(K)),
|
|
|
+ emqx_plugin_libs_rule:preproc_tmpl(to_bin(V))
|
|
|
}
|
|
|
| Acc
|
|
|
]
|
|
|
@@ -484,8 +484,8 @@ preproc_headers(Headers) when is_list(Headers) ->
|
|
|
lists:map(
|
|
|
fun({K, V}) ->
|
|
|
{
|
|
|
- emqx_plugin_libs_rule:preproc_tmpl(bin(K)),
|
|
|
- emqx_plugin_libs_rule:preproc_tmpl(bin(V))
|
|
|
+ emqx_plugin_libs_rule:preproc_tmpl(to_bin(K)),
|
|
|
+ emqx_plugin_libs_rule:preproc_tmpl(to_bin(V))
|
|
|
}
|
|
|
end,
|
|
|
Headers
|
|
|
@@ -553,15 +553,41 @@ formalize_request(Method, BasePath, {Path, Headers, _Body}) when
|
|
|
->
|
|
|
formalize_request(Method, BasePath, {Path, Headers});
|
|
|
formalize_request(_Method, BasePath, {Path, Headers, Body}) ->
|
|
|
- {filename:join(BasePath, Path), Headers, Body};
|
|
|
+ {join_paths(BasePath, Path), Headers, Body};
|
|
|
formalize_request(_Method, BasePath, {Path, Headers}) ->
|
|
|
- {filename:join(BasePath, Path), Headers}.
|
|
|
+ {join_paths(BasePath, Path), Headers}.
|
|
|
|
|
|
-bin(Bin) when is_binary(Bin) ->
|
|
|
+%% By default, we cannot treat HTTP paths as "file" or "resource" paths,
|
|
|
+%% because an HTTP server may handle paths like
|
|
|
+%% "/a/b/c/", "/a/b/c" and "/a//b/c" differently.
|
|
|
+%%
|
|
|
+%% So we try to avoid unneccessary path normalization.
|
|
|
+%%
|
|
|
+%% See also: `join_paths_test_/0`
|
|
|
+join_paths(Path1, Path2) ->
|
|
|
+ do_join_paths(lists:reverse(to_list(Path1)), to_list(Path2)).
|
|
|
+
|
|
|
+%% "abc/" + "/cde"
|
|
|
+do_join_paths([$/ | Path1], [$/ | Path2]) ->
|
|
|
+ lists:reverse(Path1) ++ [$/ | Path2];
|
|
|
+%% "abc/" + "cde"
|
|
|
+do_join_paths([$/ | Path1], Path2) ->
|
|
|
+ lists:reverse(Path1) ++ [$/ | Path2];
|
|
|
+%% "abc" + "/cde"
|
|
|
+do_join_paths(Path1, [$/ | Path2]) ->
|
|
|
+ lists:reverse(Path1) ++ [$/ | Path2];
|
|
|
+%% "abc" + "cde"
|
|
|
+do_join_paths(Path1, Path2) ->
|
|
|
+ lists:reverse(Path1) ++ [$/ | Path2].
|
|
|
+
|
|
|
+to_list(List) when is_list(List) -> List;
|
|
|
+to_list(Bin) when is_binary(Bin) -> binary_to_list(Bin).
|
|
|
+
|
|
|
+to_bin(Bin) when is_binary(Bin) ->
|
|
|
Bin;
|
|
|
-bin(Str) when is_list(Str) ->
|
|
|
+to_bin(Str) when is_list(Str) ->
|
|
|
list_to_binary(Str);
|
|
|
-bin(Atom) when is_atom(Atom) ->
|
|
|
+to_bin(Atom) when is_atom(Atom) ->
|
|
|
atom_to_binary(Atom, utf8).
|
|
|
|
|
|
reply_delegator(ReplyFunAndArgs, Result) ->
|
|
|
@@ -642,4 +668,21 @@ redact_test_() ->
|
|
|
?_assertNotEqual(TestData2, redact(TestData2))
|
|
|
].
|
|
|
|
|
|
+join_paths_test_() ->
|
|
|
+ [
|
|
|
+ ?_assertEqual("abc/cde", join_paths("abc", "cde")),
|
|
|
+ ?_assertEqual("abc/cde", join_paths("abc", "/cde")),
|
|
|
+ ?_assertEqual("abc/cde", join_paths("abc/", "cde")),
|
|
|
+ ?_assertEqual("abc/cde", join_paths("abc/", "/cde")),
|
|
|
+
|
|
|
+ ?_assertEqual("/", join_paths("", "")),
|
|
|
+ ?_assertEqual("/cde", join_paths("", "cde")),
|
|
|
+ ?_assertEqual("/cde", join_paths("", "/cde")),
|
|
|
+ ?_assertEqual("/cde", join_paths("/", "cde")),
|
|
|
+ ?_assertEqual("/cde", join_paths("/", "/cde")),
|
|
|
+
|
|
|
+ ?_assertEqual("//cde/", join_paths("/", "//cde/")),
|
|
|
+ ?_assertEqual("abc///cde/", join_paths("abc//", "//cde/"))
|
|
|
+ ].
|
|
|
+
|
|
|
-endif.
|