Commit cdd6ae12 authored by Andrew Gerrand's avatar Andrew Gerrand

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
parent 9413a6f6
......@@ -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)
}
......
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment