Skip to content

Commit

Permalink
Merge branch 'lego12239-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
etrepum committed May 9, 2016
2 parents 0ca0375 + 507f019 commit 5ed5ee1
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
@@ -1,3 +1,9 @@
Version 2.15.0 released 2016-05-08

* mochiweb_request now normalizes paths such that duplicate slashes are
discarded (and thus all path segments except the last are non-empty).
https://github.com/mochi/mochiweb/pull/173

Version 2.14.0 released 2016-04-11

* mochiweb_html now requires a letter to begin a HTML tag
Expand Down
2 changes: 1 addition & 1 deletion src/mochiweb.app.src
@@ -1,7 +1,7 @@
%% This is generated from src/mochiweb.app.src
{application, mochiweb,
[{description, "MochiMedia Web Server"},
{vsn, "2.14.0"},
{vsn, "2.15.0"},
{modules, []},
{registered, []},
{env, []},
Expand Down
2 changes: 1 addition & 1 deletion src/mochiweb_request.erl
Expand Up @@ -135,7 +135,7 @@ get(path, {?MODULE, [_Socket, _Opts, _Method, RawPath, _Version, _Headers]}) ->
case erlang:get(?SAVE_PATH) of
undefined ->
{Path0, _, _} = mochiweb_util:urlsplit_path(RawPath),
Path = mochiweb_util:unquote(Path0),
Path = mochiweb_util:normalize_path(mochiweb_util:unquote(Path0)),
put(?SAVE_PATH, Path),
Path;
Cached ->
Expand Down
46 changes: 46 additions & 0 deletions src/mochiweb_util.erl
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,20 @@ 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/").
%% Per RFC 3986, all but the last path segment must be non-empty.
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 +1005,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
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

0 comments on commit 5ed5ee1

Please sign in to comment.