Skip to content

Commit

Permalink
GzipResponse must implement CloseNotifier if ResponseWriter implement it
Browse files Browse the repository at this point in the history
  • Loading branch information
juliens authored and jprobinson committed Jan 4, 2018
1 parent d6f4660 commit 063b218
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ type GzipResponseWriter struct {
contentTypes []string // Only compress if the response is one of these content-types. All are accepted if empty.
}

type GzipResponseWriterWithCloseNotify struct {
*GzipResponseWriter
}

func (w *GzipResponseWriterWithCloseNotify) CloseNotify() <-chan bool {
return w.ResponseWriter.(http.CloseNotifier).CloseNotify()
}

// Write appends data to the gzip writer.
func (w *GzipResponseWriter) Write(b []byte) (int, error) {
// If content type is not set.
Expand Down Expand Up @@ -264,7 +272,6 @@ func GzipHandlerWithOpts(opts ...option) (func(http.Handler) http.Handler, error

return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add(vary, acceptEncoding)

if acceptsGzip(r) {
gw := &GzipResponseWriter{
ResponseWriter: w,
Expand All @@ -274,7 +281,13 @@ func GzipHandlerWithOpts(opts ...option) (func(http.Handler) http.Handler, error
}
defer gw.Close()

h.ServeHTTP(gw, r)
if _, ok := w.(http.CloseNotifier); ok {
gwcn := GzipResponseWriterWithCloseNotify{gw}
h.ServeHTTP(gwcn, r)
} else {
h.ServeHTTP(gw, r)
}

} else {
h.ServeHTTP(w, r)
}
Expand Down

0 comments on commit 063b218

Please sign in to comment.