Skip to content

Commit

Permalink
Properly handle HTTP error response when requested gzip encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
vkuznet committed Jul 12, 2024
1 parent 28e02bd commit 8effd5a
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion web/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,27 @@ func responseMsg(w http.ResponseWriter, r *http.Request, err error, code int) in
// otherwise we'll use list of JSON records
var out []ServerError
out = append(out, rec)
data, _ := json.Marshal(out)
data, err := json.Marshal(out)
if err != nil {
log.Println("ERROR: unable to json.Marshal", err)
}

// properly define HTTP writer headers
w.Header().Del("Content-Encoding")
w.Header().Del("Accept-Encoding")
w.Header().Del("Content-Type")
w.Header().Add("Content-Type", "application/json")

// define HTTP writer as gzip one if it is requested in request
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
w.Header().Add("Content-Encoding", "gzip")
gw := gzip.NewWriter(w)
defer gw.Close()
w = utils.GzipWriter{GzipWriter: gw, Writer: w}
}
// write status code first (but after all headers)
w.WriteHeader(code)
// write body after status code
w.Write(data)
return int64(len(data))
}
Expand Down

0 comments on commit 8effd5a

Please sign in to comment.