]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: prepend ./ to directory list hrefs in FileServer
authorSalman Aljammaz <s@0x65.net>
Sun, 21 Aug 2016 15:59:56 +0000 (16:59 +0100)
committerBrad Fitzpatrick <bradfitz@golang.org>
Sun, 21 Aug 2016 18:14:05 +0000 (18:14 +0000)
Certain browsers (Chrome 53, Safari 9.1.2, Firefox 46) won't correctly
follow a directory listing's links if the file name begins with a run
of characters then a colon, e.g. "foo:bar". Probably mistaking it for
a URI. However, they are happy to follow "./foo:bar", so this change
prepends "./" to all link hrefs in the directory listing of
FileServer.

Change-Id: I60ee8e1ebac73cbd3a3ac0f23e80fdf52e3dc352
Reviewed-on: https://go-review.googlesource.com/27440
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/net/http/fs.go
src/net/http/fs_test.go

index c7a58a61dff9fcffa941b8324c328b8a7dea95b3..9ebc5582141e0a1513fb7a5950e8982a5dbcd2cf 100644 (file)
@@ -90,7 +90,7 @@ func dirList(w ResponseWriter, f File) {
                // part of the URL path, and not indicate the start of a query
                // string or fragment.
                url := url.URL{Path: name}
-               fmt.Fprintf(w, "<a href=\"%s\">%s</a>\n", url.String(), htmlReplacer.Replace(name))
+               fmt.Fprintf(w, "<a href=\"./%s\">%s</a>\n", url.String(), htmlReplacer.Replace(name))
        }
        fmt.Fprintf(w, "</pre>\n")
 }
index c811891e871958f862d9bdcb15d49a825c3c0849..aa3323dd23b76563f1437245eabe597a9ff9f0bd 100644 (file)
@@ -270,10 +270,10 @@ func TestFileServerEscapesNames(t *testing.T) {
        tests := []struct {
                name, escaped string
        }{
-               {`simple_name`, `<a href="simple_name">simple_name</a>`},
-               {`"'<>&`, `<a href="%22%27%3C%3E&">&#34;&#39;&lt;&gt;&amp;</a>`},
-               {`?foo=bar#baz`, `<a href="%3Ffoo=bar%23baz">?foo=bar#baz</a>`},
-               {`<combo>?foo`, `<a href="%3Ccombo%3E%3Ffoo">&lt;combo&gt;?foo</a>`},
+               {`simple_name`, `<a href="./simple_name">simple_name</a>`},
+               {`"'<>&`, `<a href="./%22%27%3C%3E&">&#34;&#39;&lt;&gt;&amp;</a>`},
+               {`?foo=bar#baz`, `<a href="./%3Ffoo=bar%23baz">?foo=bar#baz</a>`},
+               {`<combo>?foo`, `<a href="./%3Ccombo%3E%3Ffoo">&lt;combo&gt;?foo</a>`},
        }
 
        // We put each test file in its own directory in the fakeFS so we can look at it in isolation.
@@ -349,7 +349,7 @@ func TestFileServerSortsNames(t *testing.T) {
                t.Fatalf("read Body: %v", err)
        }
        s := string(b)
-       if !strings.Contains(s, "<a href=\"a\">a</a>\n<a href=\"b\">b</a>") {
+       if !strings.Contains(s, "<a href=\"./a\">a</a>\n<a href=\"./b\">b</a>") {
                t.Errorf("output appears to be unsorted:\n%s", s)
        }
 }