Skip to content

Commit

Permalink
gzhttp: Make content-type optional (#510)
Browse files Browse the repository at this point in the history
  • Loading branch information
klauspost committed Mar 1, 2022
1 parent 84e0164 commit 67a6288
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions gzhttp/gzip.go → gzhttp/compress.go
Expand Up @@ -63,6 +63,7 @@ type GzipResponseWriter struct {
buf []byte // Holds the first part of the write before reaching the minSize or the end of the write.
ignore bool // If true, then we immediately passthru writes to the underlying ResponseWriter.
keepAcceptRanges bool // Keep "Accept-Ranges" header.
setContentType bool // Add content type, if missing and detected.

contentTypeFilter func(ct string) bool // Only compress if the response is one of these content-types. All are accepted if empty.
}
Expand Down Expand Up @@ -120,7 +121,7 @@ func (w *GzipResponseWriter) Write(b []byte) (int, error) {

// Handles the intended case of setting a nil Content-Type (as for http/server or http/fs)
// Set the header only if the key does not exist
if _, ok := w.Header()[contentType]; !ok {
if _, ok := w.Header()[contentType]; w.setContentType && !ok {
w.Header().Set(contentType, ct)
}

Expand Down Expand Up @@ -278,7 +279,7 @@ func (w *GzipResponseWriter) Flush() {

// Handles the intended case of setting a nil Content-Type (as for http/server or http/fs)
// Set the header only if the key does not exist
if _, ok := w.Header()[contentType]; !ok {
if _, ok := w.Header()[contentType]; w.setContentType && !ok {
w.Header().Set(contentType, ct)
}
}
Expand Down Expand Up @@ -343,7 +344,8 @@ func NewWrapper(opts ...option) (func(http.Handler) http.HandlerFunc, error) {
Levels: gzkp.Levels,
New: gzkp.NewWriter,
},
contentTypes: DefaultContentTypeFilter,
contentTypes: DefaultContentTypeFilter,
setContentType: true,
}

for _, o := range opts {
Expand All @@ -367,6 +369,7 @@ func NewWrapper(opts ...option) (func(http.Handler) http.HandlerFunc, error) {
contentTypeFilter: c.contentTypes,
keepAcceptRanges: c.keepAcceptRanges,
buf: gw.buf,
setContentType: c.setContentType,
}
if len(gw.buf) > 0 {
gw.buf = gw.buf[:0]
Expand Down Expand Up @@ -426,6 +429,7 @@ type config struct {
writer writer.GzipWriterFactory
contentTypes func(ct string) bool
keepAcceptRanges bool
setContentType bool
}

func (c *config) validate() error {
Expand Down Expand Up @@ -456,6 +460,15 @@ func CompressionLevel(level int) option {
}
}

// SetContentType sets the content type before returning
// requests, if unset before returning, and it was detected.
// Default: true.
func SetContentType(b bool) option {
return func(c *config) {
c.setContentType = b
}
}

// Implementation changes the implementation of GzipWriter
//
// The default implementation is writer/stdlib/NewWriter
Expand Down
File renamed without changes.

0 comments on commit 67a6288

Please sign in to comment.