Browse Source
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.
master
Matthew Holt
5 years ago
No known key found for this signature in database
GPG Key ID: 2A349DD577D586A5
1 changed files with
9 additions and
1 deletions
modules/caddyhttp/staticresp.go
@ -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 {