Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions gzhttp/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,12 @@ func NewWrapper(opts ...option) (func(http.Handler) http.HandlerFunc, error) {
return func(h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Add(vary, acceptEncoding)
if c.allowCompressedRequests && contentGzip(r) {
r.Header.Del(contentEncoding)
r.Body = io.NopCloser(&gzipReader{body: r.Body})
defer r.Body.Close()
}

if acceptsGzip(r) {
gw := grwPool.Get().(*GzipResponseWriter)
*gw = GzipResponseWriter{
Expand Down Expand Up @@ -536,17 +542,18 @@ func (pct parsedContentType) equals(mediaType string, params map[string]string)

// Used for functional configuration.
type config struct {
minSize int
level int
writer writer.GzipWriterFactory
contentTypes func(ct string) bool
keepAcceptRanges bool
setContentType bool
suffixETag string
dropETag bool
jitterBuffer int
randomJitter string
sha256Jitter bool
minSize int
level int
writer writer.GzipWriterFactory
contentTypes func(ct string) bool
keepAcceptRanges bool
setContentType bool
suffixETag string
dropETag bool
jitterBuffer int
randomJitter string
sha256Jitter bool
allowCompressedRequests bool
}

func (c *config) validate() error {
Expand Down Expand Up @@ -579,6 +586,15 @@ func MinSize(size int) option {
}
}

// AllowCompressedRequests will enable or disable RFC 7694 compressed requests.
// By default this is Disabled.
// See https://datatracker.ietf.org/doc/html/rfc7694
func AllowCompressedRequests(b bool) option {
return func(c *config) {
c.allowCompressedRequests = b
}
}

// CompressionLevel sets the compression level
func CompressionLevel(level int) option {
return func(c *config) {
Expand Down Expand Up @@ -752,6 +768,12 @@ func RandomJitter(n, buffer int, paranoid bool) option {
}
}

// contentGzip returns true if the given HTTP request indicates that it gzipped.
func contentGzip(r *http.Request) bool {
// See more detail in `acceptsGzip`
return r.Method != http.MethodHead && r.Body != nil && parseEncodingGzip(r.Header.Get(contentEncoding)) > 0
}

// acceptsGzip returns true if the given HTTP request indicates that it will
// accept a gzipped response.
func acceptsGzip(r *http.Request) bool {
Expand Down
19 changes: 19 additions & 0 deletions gzhttp/compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,25 @@ func TestMustNewGzipHandler(t *testing.T) {
handler.ServeHTTP(res3, req3)

assertEqual(t, http.DetectContentType([]byte(testBody)), res3.Header().Get("Content-Type"))

// send compress request body
handlerCompressedRequests := newTestHandlerLevel(testBody, AllowCompressedRequests(true))

var b bytes.Buffer
writerGzip := gzip.NewWriter(&b)
writerGzip.Write(testBody)
writerGzip.Close()

req5, _ := http.NewRequest("POST", "/whatever", &b)
req5.Header.Set("Content-Encoding", "gzip")
resp5 := httptest.NewRecorder()
handlerCompressedRequests.ServeHTTP(resp5, req5)
res5 := resp5.Result()

assertEqual(t, 200, res5.StatusCode)

body, _ := io.ReadAll(res5.Body)
assertEqual(t, len(testBody), len(body))
}

func TestGzipHandlerSmallBodyNoCompression(t *testing.T) {
Expand Down