Skip to content

Commit e2b30e9

Browse files
saljambradfitz
authored andcommitted
net/http: prepend ./ to directory list hrefs in FileServer
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 <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 6a393dc commit e2b30e9

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

src/net/http/fs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func dirList(w ResponseWriter, f File) {
9090
// part of the URL path, and not indicate the start of a query
9191
// string or fragment.
9292
url := url.URL{Path: name}
93-
fmt.Fprintf(w, "<a href=\"%s\">%s</a>\n", url.String(), htmlReplacer.Replace(name))
93+
fmt.Fprintf(w, "<a href=\"./%s\">%s</a>\n", url.String(), htmlReplacer.Replace(name))
9494
}
9595
fmt.Fprintf(w, "</pre>\n")
9696
}

src/net/http/fs_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,10 @@ func TestFileServerEscapesNames(t *testing.T) {
270270
tests := []struct {
271271
name, escaped string
272272
}{
273-
{`simple_name`, `<a href="simple_name">simple_name</a>`},
274-
{`"'<>&`, `<a href="%22%27%3C%3E&">&#34;&#39;&lt;&gt;&amp;</a>`},
275-
{`?foo=bar#baz`, `<a href="%3Ffoo=bar%23baz">?foo=bar#baz</a>`},
276-
{`<combo>?foo`, `<a href="%3Ccombo%3E%3Ffoo">&lt;combo&gt;?foo</a>`},
273+
{`simple_name`, `<a href="./simple_name">simple_name</a>`},
274+
{`"'<>&`, `<a href="./%22%27%3C%3E&">&#34;&#39;&lt;&gt;&amp;</a>`},
275+
{`?foo=bar#baz`, `<a href="./%3Ffoo=bar%23baz">?foo=bar#baz</a>`},
276+
{`<combo>?foo`, `<a href="./%3Ccombo%3E%3Ffoo">&lt;combo&gt;?foo</a>`},
277277
}
278278

279279
// 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) {
349349
t.Fatalf("read Body: %v", err)
350350
}
351351
s := string(b)
352-
if !strings.Contains(s, "<a href=\"a\">a</a>\n<a href=\"b\">b</a>") {
352+
if !strings.Contains(s, "<a href=\"./a\">a</a>\n<a href=\"./b\">b</a>") {
353353
t.Errorf("output appears to be unsorted:\n%s", s)
354354
}
355355
}

0 commit comments

Comments
 (0)