staticfiles: Signal that redirection headers have been written (#2792)

The Handler interface expects a first return value of 0 if headers have
already been written.
(cf. https://godoc.org/github.com/caddyserver/caddy/caddyhttp/httpserver#Handler)

When using http.Redirect, this is the case as http.Redirect does write
headers. When using Caddy with nested handlers, returning
http.StatusMovedPermanently could cause a wrong (200) response on a
non-slashy request on a directory name. Returning 0 ensures the
redirection will always take place.
This commit is contained in:
Jannis Andrija Schnitzer 2019-10-07 18:22:53 +02:00 committed by Matt Holt
parent 1de4a99ec3
commit 11eee95222
2 changed files with 5 additions and 2 deletions

View File

@ -116,7 +116,7 @@ func (fs FileServer) serveFile(w http.ResponseWriter, r *http.Request) (int, err
}
urlCopy.Path += "/"
http.Redirect(w, r, urlCopy.String(), http.StatusMovedPermanently)
return http.StatusMovedPermanently, nil
return 0, nil
}
} else {
// ensure no trailing slash
@ -143,7 +143,7 @@ func (fs FileServer) serveFile(w http.ResponseWriter, r *http.Request) (int, err
urlCopy.Path = strings.TrimPrefix(urlCopy.Path, "/")
}
http.Redirect(w, r, urlCopy.String(), http.StatusMovedPermanently)
return http.StatusMovedPermanently, nil
return 0, nil
}
}

View File

@ -301,6 +301,9 @@ func TestServeHTTP(t *testing.T) {
// perform the test
status, err := fileServer.ServeHTTP(responseRecorder, request)
if status == 0 {
status = responseRecorder.Code
}
etag := responseRecorder.Header().Get("Etag")
body := responseRecorder.Body.String()
vary := responseRecorder.Header().Get("Vary")