Reviewers: golang-dev_googlegroups.com,
Message:
Hello [email protected],
I'd like you to review this change to
https://code.google.com/p/go/
Description:
net/url: fix handling of relative paths in ResolveReference.
Fixes issue 3560.
Please review this at https://codereview.appspot.com/6886047/
Affected files:
M src/pkg/net/url/url.go
M src/pkg/net/url/url_test.go
Index: src/pkg/net/url/url.go
===================================================================
--- a/src/pkg/net/url/url.go
+++ b/src/pkg/net/url/url.go
@@ -572,23 +572,32 @@
if len(base) == 0 {
base = []string{""}
}
+
+ ndot := 1
for idx, ref := range refs {
switch {
case ref == ".":
- base[len(base)-1] = ""
+ if ndot == 1 {
+ base[len(base)-1] = ""
+ }
+ ndot = 1
case ref == "..":
newLen := len(base) - 1
if newLen < 1 {
newLen = 1
}
base = base[0:newLen]
- base[len(base)-1] = ""
+ if ndot > 0 {
+ base[len(base)-1] = ""
+ }
+ ndot = 2
default:
if idx == 0 || base[len(base)-1] == "" {
base[len(base)-1] = ref
} else {
base = append(base, ref)
}
+ ndot = 0
}
}
return strings.Join(base, "/")
Index: src/pkg/net/url/url_test.go
===================================================================
--- a/src/pkg/net/url/url_test.go
+++ b/src/pkg/net/url/url_test.go
@@ -536,6 +536,11 @@
{"http://foo.com/bar/baz", "../../../../../quux", "http://foo.com/quux"},
{"http://foo.com/bar", "..", "http://foo.com/"},
{"http://foo.com/bar/baz", "./..", "http://foo.com/"},
+ // ".." in the middle (issue 3560)
+
{"http://foo.com/bar/baz", "quux/dotdot/../tail", "http://foo.com/bar/quux/tail"},
+
{"http://foo.com/bar/baz", "quux/./dotdot/../tail", "http://foo.com/bar/quux/tail"},
+
{"http://foo.com/bar/baz", "quux/./dotdot/.././tail", "http://foo.com/bar/quux/tail"},
+
{"http://foo.com/bar/baz", "quux/./dotdot/../dotdot/../dot/./tail/..", "http://foo.com/bar/quux/dot"},
// "." and ".." in the base aren't special
{"http://foo.com/dot/./dotdot/../foo/bar", "../baz", "http://foo.com/dot/./dotdot/../baz"},