Skip to content

Commit

Permalink
bugfix if handler wrote it's own response
Browse files Browse the repository at this point in the history
  • Loading branch information
ninlil committed Mar 27, 2023
1 parent d75f4b4 commit 85e2ef1
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion router/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strconv"
"strings"

"github.com/ninlil/butler/bufferedresponse"
"github.com/ninlil/butler/log"
)

Expand Down Expand Up @@ -104,6 +105,11 @@ func createResponse(accept string, data interface{}) (buf []byte, ct string, ind

func (rt *Route) writeResponse(w http.ResponseWriter, r *http.Request, status int, data interface{}) {

var w2 *bufferedresponse.ResponseWriter = nil
if data == nil {
w2, _ = bufferedresponse.Get(w)
}

buf, ct, indent, err := createResponse(r.Header.Get("Accept"), data)

if err != nil {
Expand All @@ -118,7 +124,12 @@ func (rt *Route) writeResponse(w http.ResponseWriter, r *http.Request, status in
if ct != "" {
w.Header().Set("Content-Type", ct)
}

size := len(buf)
if w2 != nil {
size += w2.Size()
}

if size > 0 {
w.Header().Set("Content-Length", fmt.Sprint(size))
}
Expand All @@ -132,7 +143,7 @@ func (rt *Route) writeResponse(w http.ResponseWriter, r *http.Request, status in
}

w.WriteHeader(status)
if size > 0 {
if len(buf) > 0 {
_, _ = w.Write(buf)
}
}

0 comments on commit 85e2ef1

Please sign in to comment.