]> Cypherpunks repositories - gostls13.git/commitdiff
net/url: generate correct Path when hostname empty
authorAndrew Gerrand <adg@golang.org>
Wed, 23 Jan 2013 00:37:06 +0000 (11:37 +1100)
committerAndrew Gerrand <adg@golang.org>
Wed, 23 Jan 2013 00:37:06 +0000 (11:37 +1100)
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

src/pkg/net/url/url.go
src/pkg/net/url/url_test.go

index 71758fe49e00997be3458ea2c0dfc20b704f379b..22425b33681f39ebbc0259932b0af6b4d8affe20 100644 (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)
        }
index 4d3545dadb74f7a3e11945a99472fb7ffbfa66ba..9eddf730e09e242c770f0d5bc3b16d6cd1df51cf 100644 (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