Skip to content

Commit

Permalink
fix: default auto decompressor on uncompressed or double compressed b…
Browse files Browse the repository at this point in the history
…odies (#91)

* fix: default auto decompressor on uncompressed or double compressed bodies

* Update options.go

---------

Co-authored-by: Alex Dyukov <alex.dyukov.93@gmail.com>
Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
  • Loading branch information
3 people authored Jan 27, 2025
1 parent 3529136 commit 438926d
Showing 1 changed file with 56 additions and 10 deletions.
66 changes: 56 additions & 10 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@ package gzip

import (
"compress/gzip"
"errors"
"io"
"net/http"
"regexp"
"strings"

"github.com/gin-gonic/gin"
)

// DefaultExcludedExtentions is a predefined list of file extensions that should be excluded from gzip compression.
// These extensions typically represent image files that are already compressed
// and do not benefit from additional compression.
var DefaultExcludedExtentions = NewExcludedExtensions([]string{
".png", ".gif", ".jpeg", ".jpg",
})
var (
// DefaultExcludedExtentions is a predefined list of file extensions that should be excluded from gzip compression.

Check failure on line 15 in options.go

View workflow job for this annotation

GitHub Actions / lint

File is not properly formatted (gofmt)
// These extensions typically represent image files that are already compressed
// and do not benefit from additional compression.
DefaultExcludedExtentions = NewExcludedExtensions([]string{
".png", ".gif", ".jpeg", ".jpg",
})
UnsupportedContentEncoding = errors.New("Unsupported content encoding")

Check failure on line 21 in options.go

View workflow job for this annotation

GitHub Actions / lint

ST1005: error strings should not be capitalized (stylecheck)
)

// Option is an interface that defines a method to apply a configuration
// to a given config instance. Implementations of this interface can be
Expand Down Expand Up @@ -210,12 +215,53 @@ func DefaultDecompressHandle(c *gin.Context) {
if c.Request.Body == nil {
return
}
r, err := gzip.NewReader(c.Request.Body)
if err != nil {
_ = c.AbortWithError(http.StatusBadRequest, err)

contentEncodingField := strings.Split(strings.ToLower(c.GetHeader("Content-Encoding")), ",")
if len(contentEncodingField) == 0 { // nothing to decompress
c.Next()

return
}

toClose := make([]io.Closer, 0, len(contentEncodingField))
defer func() {
for i := len(toClose); i > 0; i-- {
toClose[i-1].Close()
}
}()

// parses multiply gzips like
// Content-Encoding: gzip, gzip, gzip
// allowed by RFC
for i := 0; i < len(contentEncodingField); i++ {
trimmedValue := strings.TrimSpace(contentEncodingField[i])

if trimmedValue == "" {
continue
}

if trimmedValue != "gzip" {
// https://www.rfc-editor.org/rfc/rfc7231#section-3.1.2.2
// An origin server MAY respond with a status code of 415 (Unsupported
// Media Type) if a representation in the request message has a content
// coding that is not acceptable.
_ = c.AbortWithError(http.StatusUnsupportedMediaType, UnsupportedContentEncoding)
}

r, err := gzip.NewReader(c.Request.Body)
if err != nil {
_ = c.AbortWithError(http.StatusBadRequest, err)

return
}

toClose = append(toClose, c.Request.Body)

c.Request.Body = r
}

c.Request.Header.Del("Content-Encoding")
c.Request.Header.Del("Content-Length")
c.Request.Body = r

c.Next()
}

0 comments on commit 438926d

Please sign in to comment.