Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 05:15 UTC
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.
Matthew Holt committed
Jun 4, 2020 at 10:32 UTC
7b0962ba4df7e1dd7ab3f193fba394a4b899c431
1 file changed
+9
-1
modules/caddyhttp/staticresp.go
+9
-1
@@ -121,8 +121,16 @@ func (s StaticResponse) ServeHTTP(w http.ResponseWriter, r *http.Request, _ Hand
121
w.Header()["Content-Type"] = nil
122
}
123
124
- // get the status code
124
+ // get the status code; if this handler exists in an error route,
125
+ // use the recommended status code as the default; otherwise 200
126
statusCode := http.StatusOK
127
+ if reqErr, ok := r.Context().Value(ErrorCtxKey).(error); ok {
128
+ if handlerErr, ok := reqErr.(HandlerError); ok {
129
+ if handlerErr.StatusCode > 0 {
130
+ statusCode = handlerErr.StatusCode
131
+ }
132
+ }
133
+ }
134
if codeStr := s.StatusCode.String(); codeStr != "" {
135
intVal, err := strconv.Atoi(repl.ReplaceAll(codeStr, ""))
136
if err != nil {