mirror of
https://github.com/caddyserver/caddy.git
synced 2024-11-22 12:08:28 +08:00
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:
parent
1de4a99ec3
commit
11eee95222
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue
Block a user