Skip to content

Commit

Permalink
refactor: improve error handling and documentation for content encoding
Browse files Browse the repository at this point in the history
- Rename `UnsupportedContentEncoding` to `ErrUnsupportedContentEncoding` and add a descriptive comment
- Add a comment referencing RFC 7231, Section 3.1.2.2 for content encoding handling

Signed-off-by: appleboy <[email protected]>
  • Loading branch information
appleboy committed Jan 27, 2025
1 parent f3f028a commit 4fc267c
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import (
)

var (
// 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.
// 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.
DefaultExcludedExtentions = NewExcludedExtensions([]string{
".png", ".gif", ".jpeg", ".jpg",
})
UnsupportedContentEncoding = errors.New("Unsupported content encoding")
// ErrUnsupportedContentEncoding is an error that indicates the content encoding
// is not supported by the application.
ErrUnsupportedContentEncoding = errors.New("unsupported content encoding")
)

// Option is an interface that defines a method to apply a configuration
Expand Down Expand Up @@ -241,11 +243,12 @@ func DefaultDecompressHandle(c *gin.Context) {
}

if trimmedValue != "gzip" {
// According to RFC 7231, Section 3.1.2.2:
// 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)
_ = c.AbortWithError(http.StatusUnsupportedMediaType, ErrUnsupportedContentEncoding)
}

r, err := gzip.NewReader(c.Request.Body)
Expand Down

0 comments on commit 4fc267c

Please sign in to comment.