Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export NoGzipResponseWriter for custom ResponseWriter wrappers #722

Merged
merged 1 commit into from Jan 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 20 additions & 20 deletions gzhttp/compress.go
Expand Up @@ -389,7 +389,7 @@ func NewWrapper(opts ...option) (func(http.Handler) http.HandlerFunc, error) {
h.ServeHTTP(gw, r)
}
} else {
h.ServeHTTP(newNoCompressResponseWriter(w), r)
h.ServeHTTP(newNoGzipResponseWriter(w), r)
w.Header().Del(HeaderNoCompression)
}
}
Expand Down Expand Up @@ -746,11 +746,11 @@ func atoi(s string) (int, bool) {
return int(i64), err == nil
}

// newNoCompressResponseWriter will return a response writer that
// newNoGzipResponseWriter will return a response writer that
// cleans up compression artifacts.
// Depending on whether http.Hijacker is supported the returned will as well.
func newNoCompressResponseWriter(w http.ResponseWriter) http.ResponseWriter {
n := &noCompressResponseWriter{hw: w}
func newNoGzipResponseWriter(w http.ResponseWriter) http.ResponseWriter {
n := &NoGzipResponseWriter{ResponseWriter: w}
if hj, ok := w.(http.Hijacker); ok {
x := struct {
http.ResponseWriter
Expand All @@ -767,45 +767,45 @@ func newNoCompressResponseWriter(w http.ResponseWriter) http.ResponseWriter {
return n
}

// noCompressResponseWriter filters out HeaderNoCompression.
type noCompressResponseWriter struct {
hw http.ResponseWriter
// NoGzipResponseWriter filters out HeaderNoCompression.
type NoGzipResponseWriter struct {
http.ResponseWriter
hdrCleaned bool
}

func (n *noCompressResponseWriter) CloseNotify() <-chan bool {
if cn, ok := n.hw.(http.CloseNotifier); ok {
func (n *NoGzipResponseWriter) CloseNotify() <-chan bool {
if cn, ok := n.ResponseWriter.(http.CloseNotifier); ok {
return cn.CloseNotify()
}
return nil
}

func (n *noCompressResponseWriter) Flush() {
func (n *NoGzipResponseWriter) Flush() {
if !n.hdrCleaned {
n.hw.Header().Del(HeaderNoCompression)
n.ResponseWriter.Header().Del(HeaderNoCompression)
n.hdrCleaned = true
}
if f, ok := n.hw.(http.Flusher); ok {
if f, ok := n.ResponseWriter.(http.Flusher); ok {
f.Flush()
}
}

func (n *noCompressResponseWriter) Header() http.Header {
return n.hw.Header()
func (n *NoGzipResponseWriter) Header() http.Header {
return n.ResponseWriter.Header()
}

func (n *noCompressResponseWriter) Write(bytes []byte) (int, error) {
func (n *NoGzipResponseWriter) Write(bytes []byte) (int, error) {
if !n.hdrCleaned {
n.hw.Header().Del(HeaderNoCompression)
n.ResponseWriter.Header().Del(HeaderNoCompression)
n.hdrCleaned = true
}
return n.hw.Write(bytes)
return n.ResponseWriter.Write(bytes)
}

func (n *noCompressResponseWriter) WriteHeader(statusCode int) {
func (n *NoGzipResponseWriter) WriteHeader(statusCode int) {
if !n.hdrCleaned {
n.hw.Header().Del(HeaderNoCompression)
n.ResponseWriter.Header().Del(HeaderNoCompression)
n.hdrCleaned = true
}
n.hw.WriteHeader(statusCode)
n.ResponseWriter.WriteHeader(statusCode)
}