Remove '/./' parts from path

This commit is contained in:
Aliaksandr Valialkin
2015-12-03 19:00:24 +02:00
parent fab86ba9b4
commit ed1d21e27d
3 changed files with 17 additions and 0 deletions
+1
View File
@@ -10,6 +10,7 @@ var (
strSlash = []byte("/")
strSlashSlash = []byte("//")
strSlashDotDot = []byte("/..")
strSlashDotSlash = []byte("/./")
strSlashDotDotSlash = []byte("/../")
strCRLF = []byte("\r\n")
strHTTP = []byte("http")
+11
View File
@@ -263,6 +263,17 @@ func normalizePath(dst, src []byte) []byte {
b = b[:len(b)-n+nn]
}
// remove /./ parts
for {
n := bytes.Index(b, strSlashDotSlash)
if n < 0 {
break
}
nn := n + len(strSlashDotSlash) - 1
copy(b[n:], b[nn:])
b = b[:len(b)-nn+n]
}
// remove trailing /foo/..
n := bytes.LastIndex(b, strSlashDotDot)
if n >= 0 && n+len(strSlashDotDot) == len(b) {
+5
View File
@@ -71,6 +71,11 @@ func TestURIPathNormalize(t *testing.T) {
// fake dotdot
testURIPathNormalize(t, &u, "/aaa/..bbb/ccc/..", "/aaa/..bbb/")
// single dot
testURIPathNormalize(t, &u, "/a/./b/././c/./d.html", "/a/b/c/d.html")
testURIPathNormalize(t, &u, "./foo/", "/foo/")
testURIPathNormalize(t, &u, "./../.././../../aaa/bbb/../../../././../", "/")
}
func testURIPathNormalize(t *testing.T, u *URI, requestURI, expectedPath string) {