From 5e6181fb849be104edb3e9e69f7cae50e5406450 Mon Sep 17 00:00:00 2001 From: Nikhil Benesch Date: Thu, 6 Sep 2018 23:09:20 -0400 Subject: [PATCH] server: don't break content type sniffing when gzipping Release note: None --- pkg/server/server.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pkg/server/server.go b/pkg/server/server.go index 1b3b60e9ed2b..322475192cd1 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -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 @@ -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) }