Commit ddcf8d40 authored by Mohit Agarwal's avatar Mohit Agarwal Committed by Brad Fitzpatrick

net/http: redirect if the URL path is a dir & doesn't end in a slash

Fixes #13996

Change-Id: I9b2c7fba0705900aca9a70bc6a2687667a9a976c
Reviewed-on: https://go-review.googlesource.com/20128Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent edca4cda
......@@ -393,6 +393,15 @@ func serveFile(w ResponseWriter, r *Request, fs FileSystem, name string, redirec
}
}
// redirect if the directory name doesn't end in a slash
if d.IsDir() {
url := r.URL.Path
if url[len(url)-1] != '/' {
localRedirect(w, r, path.Base(url)+"/")
return
}
}
// use contents of index.html for directory, if present
if d.IsDir() {
index := strings.TrimSuffix(name, "/") + indexPage
......
......@@ -505,6 +505,24 @@ func TestServeFileFromCWD(t *testing.T) {
}
}
// Issue 13996
func TestServeDirWithoutTrailingSlash(t *testing.T) {
e := "/testdata/"
defer afterTest(t)
ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
ServeFile(w, r, ".")
}))
defer ts.Close()
r, err := Get(ts.URL + "/testdata")
if err != nil {
t.Fatal(err)
}
r.Body.Close()
if g := r.Request.URL.Path; g != e {
t.Errorf("got %s, want %s", g, e)
}
}
// Tests that ServeFile doesn't add a Content-Length if a Content-Encoding is
// specified.
func TestServeFileWithContentEncoding_h1(t *testing.T) { testServeFileWithContentEncoding(t, h1Mode) }
......
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