1
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-16 21:56:40 -05:00

caddyhttp: Default to error status if found in context

This is just a convenience if using a static_response handler in an
error route, by setting the default status code to the same one as
the error status.
This commit is contained in:
Matthew Holt 2020-06-04 10:32:01 -06:00
parent 2d1f7b9da8
commit 7b0962ba4d
No known key found for this signature in database
GPG key ID: 2A349DD577D586A5

View file

@ -121,8 +121,16 @@ func (s StaticResponse) ServeHTTP(w http.ResponseWriter, r *http.Request, _ Hand
w.Header()["Content-Type"] = nil w.Header()["Content-Type"] = nil
} }
// get the status code // get the status code; if this handler exists in an error route,
// use the recommended status code as the default; otherwise 200
statusCode := http.StatusOK statusCode := http.StatusOK
if reqErr, ok := r.Context().Value(ErrorCtxKey).(error); ok {
if handlerErr, ok := reqErr.(HandlerError); ok {
if handlerErr.StatusCode > 0 {
statusCode = handlerErr.StatusCode
}
}
}
if codeStr := s.StatusCode.String(); codeStr != "" { if codeStr := s.StatusCode.String(); codeStr != "" {
intVal, err := strconv.Atoi(repl.ReplaceAll(codeStr, "")) intVal, err := strconv.Atoi(repl.ReplaceAll(codeStr, ""))
if err != nil { if err != nil {