mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
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/20128 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
edca4cda88
commit
ddcf8d402a
2 changed files with 27 additions and 0 deletions
|
|
@ -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) }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue