You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by rn...@apache.org on 2017/08/14 15:38:21 UTC

[11/31] mochiweb commit: updated refs/heads/upstream to 23dc119

Add normalize_path/1 function to mochiweb_util.

Function removes duplicate slashes from a string supplied as a first function
argument.

Signed-off-by: Oleg Nemanov <le...@yandex.ru>


Project: http://git-wip-us.apache.org/repos/asf/couchdb-mochiweb/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-mochiweb/commit/3c9b72a8
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-mochiweb/tree/3c9b72a8
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-mochiweb/diff/3c9b72a8

Branch: refs/heads/upstream
Commit: 3c9b72a82ae80f6c13246c829b00a1c9d4dc4858
Parents: 0ca0375
Author: Oleg Nemanov <le...@yandex.ru>
Authored: Wed Apr 27 13:15:43 2016 +0300
Committer: Oleg Nemanov <le...@yandex.ru>
Committed: Wed Apr 27 13:15:43 2016 +0300

----------------------------------------------------------------------
 src/mochiweb_util.erl | 45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-mochiweb/blob/3c9b72a8/src/mochiweb_util.erl
----------------------------------------------------------------------
diff --git a/src/mochiweb_util.erl b/src/mochiweb_util.erl
index c606767..22ae8f4 100644
--- a/src/mochiweb_util.erl
+++ b/src/mochiweb_util.erl
@@ -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). % $\.
@@ -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
 %%
@@ -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.