Skip to content

Commit

Permalink
caddyhttp: Wrap the response writer, chain handlers, directive ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
francislavoie committed Apr 27, 2020
1 parent c6051b6 commit 1ab3e02
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
1 change: 1 addition & 0 deletions caddyconfig/httpcaddyfile/directives.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var directiveOrder = []string{
"basicauth",
"request_header",
"encode",
"status_code",
"templates",

// special routing directives
Expand Down
10 changes: 7 additions & 3 deletions modules/caddyhttp/statuscode.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (s *StatusCode) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
return nil
}

func (s StatusCode) ServeHTTP(w http.ResponseWriter, r *http.Request, _ Handler) error {
func (s StatusCode) ServeHTTP(w http.ResponseWriter, r *http.Request, next Handler) error {
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)

// get the status code
Expand All @@ -71,10 +71,14 @@ func (s StatusCode) ServeHTTP(w http.ResponseWriter, r *http.Request, _ Handler)
statusCode = intVal
}

// wrap the response writer so we can modify the status
// without repercursions (i.e. writing it twice)
wrapper := NewResponseRecorder(w, nil, nil)

// write headers
w.WriteHeader(statusCode)
wrapper.WriteHeader(statusCode)

return nil
return next.ServeHTTP(wrapper, r)
}

// Interface guards
Expand Down

0 comments on commit 1ab3e02

Please sign in to comment.