mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-14 14:56:27 +03: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
2 changed files with 5 additions and 2 deletions
|
@ -116,7 +116,7 @@ func (fs FileServer) serveFile(w http.ResponseWriter, r *http.Request) (int, err
|
||||||
}
|
}
|
||||||
urlCopy.Path += "/"
|
urlCopy.Path += "/"
|
||||||
http.Redirect(w, r, urlCopy.String(), http.StatusMovedPermanently)
|
http.Redirect(w, r, urlCopy.String(), http.StatusMovedPermanently)
|
||||||
return http.StatusMovedPermanently, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// ensure no trailing slash
|
// 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, "/")
|
urlCopy.Path = strings.TrimPrefix(urlCopy.Path, "/")
|
||||||
}
|
}
|
||||||
http.Redirect(w, r, urlCopy.String(), http.StatusMovedPermanently)
|
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
|
// perform the test
|
||||||
status, err := fileServer.ServeHTTP(responseRecorder, request)
|
status, err := fileServer.ServeHTTP(responseRecorder, request)
|
||||||
|
if status == 0 {
|
||||||
|
status = responseRecorder.Code
|
||||||
|
}
|
||||||
etag := responseRecorder.Header().Get("Etag")
|
etag := responseRecorder.Header().Get("Etag")
|
||||||
body := responseRecorder.Body.String()
|
body := responseRecorder.Body.String()
|
||||||
vary := responseRecorder.Header().Get("Vary")
|
vary := responseRecorder.Header().Get("Vary")
|
||||||
|
|
Loading…
Reference in a new issue