Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove duplicate slashes from an uri path #173

Merged
merged 2 commits into from
May 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/mochiweb_request.erl
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ get(path, {?MODULE, [_Socket, _Opts, _Method, RawPath, _Version, _Headers]}) ->
undefined ->
{Path0, _, _} = mochiweb_util:urlsplit_path(RawPath),
Path = mochiweb_util:unquote(Path0),
put(?SAVE_PATH, Path),
Path;
Path_n = mochiweb_util:normalize_path(Path),
put(?SAVE_PATH, Path_n),
Path_n;
Cached ->
Cached
end;
Expand Down
45 changes: 45 additions & 0 deletions src/mochiweb_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
-export([safe_relative_path/1, partition/2]).
-export([parse_qvalues/1, pick_accepted_encodings/3]).
-export([make_io/1]).
-export([normalize_path/1]).

-define(PERCENT, 37). % $\%
-define(FULLSTOP, 46). % $\.
Expand Down Expand Up @@ -586,6 +587,19 @@ make_io(Integer) when is_integer(Integer) ->
make_io(Io) when is_list(Io); is_binary(Io) ->
Io.

%% @spec normalize_path(string()) -> string()
%% @doc Remove duplicate slashes from an uri path ("//foo///bar////" becomes
%% "/foo/bar/").
normalize_path(Path) ->
normalize_path(Path, []).

normalize_path([], Acc) ->
lists:reverse(Acc);
normalize_path("/" ++ Path, "/" ++ _ = Acc) ->
normalize_path(Path, Acc);
normalize_path([C|Path], Acc) ->
normalize_path(Path, [C|Acc]).

%%
%% Tests
%%
Expand Down Expand Up @@ -990,4 +1004,35 @@ pick_accepted_encodings_test() ->
),
ok.

normalize_path_test() ->
"" = normalize_path(""),
"/" = normalize_path("/"),
"/" = normalize_path("//"),
"/" = normalize_path("///"),
"foo" = normalize_path("foo"),
"/foo" = normalize_path("/foo"),
"/foo" = normalize_path("//foo"),
"/foo" = normalize_path("///foo"),
"foo/" = normalize_path("foo/"),
"foo/" = normalize_path("foo//"),
"foo/" = normalize_path("foo///"),
"foo/bar" = normalize_path("foo/bar"),
"foo/bar" = normalize_path("foo//bar"),
"foo/bar" = normalize_path("foo///bar"),
"foo/bar" = normalize_path("foo////bar"),
"/foo/bar" = normalize_path("/foo/bar"),
"/foo/bar" = normalize_path("/foo////bar"),
"/foo/bar" = normalize_path("////foo/bar"),
"/foo/bar" = normalize_path("////foo///bar"),
"/foo/bar" = normalize_path("////foo////bar"),
"/foo/bar/" = normalize_path("/foo/bar/"),
"/foo/bar/" = normalize_path("////foo/bar/"),
"/foo/bar/" = normalize_path("/foo////bar/"),
"/foo/bar/" = normalize_path("/foo/bar////"),
"/foo/bar/" = normalize_path("///foo////bar/"),
"/foo/bar/" = normalize_path("////foo/bar////"),
"/foo/bar/" = normalize_path("/foo///bar////"),
"/foo/bar/" = normalize_path("////foo///bar////"),
ok.

-endif.
2 changes: 1 addition & 1 deletion test/mochiweb_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ with_server(Transport, ServerFun, ClientFun) ->
mochiweb_test_util:with_server(Transport, ServerFun, ClientFun).

request_test() ->
R = mochiweb_request:new(z, z, "/foo/bar/baz%20wibble+quux?qs=2", z, []),
R = mochiweb_request:new(z, z, "//foo///bar/baz%20wibble+quux?qs=2", z, []),
"/foo/bar/baz wibble quux" = R:get(path),
ok.

Expand Down