mirror of
https://github.com/golang/go
synced 2024-11-05 18:36:08 +00:00
net/url: fix handling of relative paths in ResolveReference.
Fixes #3560. R=golang-dev, rsc CC=golang-dev https://golang.org/cl/6886047
This commit is contained in:
parent
dffdece819
commit
2f45f2801d
2 changed files with 21 additions and 2 deletions
|
@ -572,23 +572,33 @@ func resolvePath(basepath string, refpath string) string {
|
|||
if len(base) == 0 {
|
||||
base = []string{""}
|
||||
}
|
||||
|
||||
rm := true
|
||||
for idx, ref := range refs {
|
||||
switch {
|
||||
case ref == ".":
|
||||
base[len(base)-1] = ""
|
||||
if idx == 0 {
|
||||
base[len(base)-1] = ""
|
||||
rm = true
|
||||
} else {
|
||||
rm = false
|
||||
}
|
||||
case ref == "..":
|
||||
newLen := len(base) - 1
|
||||
if newLen < 1 {
|
||||
newLen = 1
|
||||
}
|
||||
base = base[0:newLen]
|
||||
base[len(base)-1] = ""
|
||||
if rm {
|
||||
base[len(base)-1] = ""
|
||||
}
|
||||
default:
|
||||
if idx == 0 || base[len(base)-1] == "" {
|
||||
base[len(base)-1] = ref
|
||||
} else {
|
||||
base = append(base, ref)
|
||||
}
|
||||
rm = false
|
||||
}
|
||||
}
|
||||
return strings.Join(base, "/")
|
||||
|
|
|
@ -536,6 +536,15 @@ var resolveReferenceTests = []struct {
|
|||
{"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/./../tail", "http://foo.com/bar/quux/tail"},
|
||||
{"http://foo.com/bar/baz", "quux/./dotdot/dotdot/././../../tail", "http://foo.com/bar/quux/tail"},
|
||||
{"http://foo.com/bar/baz", "quux/./dotdot/dotdot/./.././../tail", "http://foo.com/bar/quux/tail"},
|
||||
{"http://foo.com/bar/baz", "quux/./dotdot/dotdot/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"},
|
||||
|
|
Loading…
Reference in a new issue