1
0
mirror of https://github.com/golang/go synced 2024-11-26 23:11:24 -07:00

net/url: generate correct Path when hostname empty

Parse("file:///foo") previously returned a URL with Scheme "file"
and Path "///foo". Now it returns a URL with Path "/foo",
such that
        &URL{Scheme: "file", Path: "/foo"}.String() == "file:///foo"

This means that parsing and stringifying the URL "file:/foo"
returns "file:///foo", technically a regression but one that only
affects a corner case.

Fixes #4189.

R=bradfitz, rsc
CC=golang-dev
https://golang.org/cl/7135051
This commit is contained in:
Andrew Gerrand 2013-01-23 11:37:06 +11:00
parent 9413a6f660
commit cdd6ae1288
2 changed files with 16 additions and 5 deletions

View File

@ -386,7 +386,7 @@ func parse(rawurl string, viaRequest bool) (url *URL, err error) {
}
}
if (url.Scheme != "" || !viaRequest) && strings.HasPrefix(rest, "//") && !strings.HasPrefix(rest, "///") {
if (url.Scheme != "" || !viaRequest && !strings.HasPrefix(rest, "///")) && strings.HasPrefix(rest, "//") {
var authority string
authority, rest = split(rest[2:], '/', false)
url.User, url.Host, err = parseAuthority(authority)
@ -442,12 +442,14 @@ func (u *URL) String() string {
if u.Opaque != "" {
result += u.Opaque
} else {
if u.Host != "" || u.User != nil {
if u.Scheme != "" || u.Host != "" || u.User != nil {
result += "//"
if u := u.User; u != nil {
result += u.String() + "@"
}
result += u.Host
if h := u.Host; h != "" {
result += u.Host
}
}
result += escape(u.Path, encodePath)
}

View File

@ -122,14 +122,14 @@ var urltests = []URLTest{
},
"http:%2f%2fwww.google.com/?q=go+language",
},
// non-authority
// non-authority with path
{
"mailto:/webmaster@golang.org",
&URL{
Scheme: "mailto",
Path: "/webmaster@golang.org",
},
"",
"mailto:///webmaster@golang.org", // unfortunate compromise
},
// non-authority
{
@ -242,6 +242,15 @@ var urltests = []URLTest{
},
"http://www.google.com/?q=go+language#foo&bar",
},
{
"file:///home/adg/rabbits",
&URL{
Scheme: "file",
Host: "",
Path: "/home/adg/rabbits",
},
"file:///home/adg/rabbits",
},
}
// more useful string for debugging than fmt's struct printer