Skip to content

Commit

Permalink
net/url: generate correct Path when hostname empty
Browse files Browse the repository at this point in the history
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
  • Loading branch information
adg committed Jan 23, 2013
1 parent 9413a6f commit cdd6ae1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/pkg/net/url/url.go
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
13 changes: 11 additions & 2 deletions src/pkg/net/url/url_test.go
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit cdd6ae1

Please sign in to comment.