Skip to content

Commit

Permalink
server: don't break content type sniffing when gzipping
Browse files Browse the repository at this point in the history
Release note: None
  • Loading branch information
benesch committed Sep 7, 2018
1 parent dd90644 commit 5e6181f
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1894,6 +1894,8 @@ func (s *Server) PGServer() *pgwire.Server {
return s.pgServer
}

// TODO(benesch): Use https://github.com/NYTimes/gziphandler instead.
// gzipResponseWriter reinvents the wheel and is not as robust.
type gzipResponseWriter struct {
gz gzip.Writer
http.ResponseWriter
Expand All @@ -1916,6 +1918,11 @@ func (w *gzipResponseWriter) Reset(rw http.ResponseWriter) {
}

func (w *gzipResponseWriter) Write(b []byte) (int, error) {
// The underlying http.ResponseWriter can't sniff gzipped data properly, so we
// do our own sniffing on the uncompressed data.
if w.Header().Get("Content-Type") == "" {
w.Header().Set("Content-Type", http.DetectContentType(b))
}
return w.gz.Write(b)
}

Expand Down

0 comments on commit 5e6181f

Please sign in to comment.