summaryrefslogtreecommitdiff
path: root/vendor/github.com/gin-contrib/gzip/handler.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/gin-contrib/gzip/handler.go')
-rw-r--r--vendor/github.com/gin-contrib/gzip/handler.go90
1 files changed, 60 insertions, 30 deletions
diff --git a/vendor/github.com/gin-contrib/gzip/handler.go b/vendor/github.com/gin-contrib/gzip/handler.go
index ee9eb9f66..412c8386b 100644
--- a/vendor/github.com/gin-contrib/gzip/handler.go
+++ b/vendor/github.com/gin-contrib/gzip/handler.go
@@ -2,84 +2,114 @@ package gzip
import (
"compress/gzip"
- "fmt"
"io"
"net/http"
"path/filepath"
+ "strconv"
"strings"
"sync"
"github.com/gin-gonic/gin"
)
+const (
+ headerAcceptEncoding = "Accept-Encoding"
+ headerContentEncoding = "Content-Encoding"
+ headerVary = "Vary"
+)
+
type gzipHandler struct {
- *Options
+ *config
gzPool sync.Pool
}
-func newGzipHandler(level int, options ...Option) *gzipHandler {
+func isCompressionLevelValid(level int) bool {
+ return level == gzip.DefaultCompression ||
+ level == gzip.NoCompression ||
+ (level >= gzip.BestSpeed && level <= gzip.BestCompression)
+}
+
+func newGzipHandler(level int, opts ...Option) *gzipHandler {
+ cfg := &config{
+ excludedExtensions: DefaultExcludedExtentions,
+ }
+
+ // Apply each option to the config
+ for _, o := range opts {
+ o.apply(cfg)
+ }
+
+ if !isCompressionLevelValid(level) {
+ // For web content, level 4 seems to be a sweet spot.
+ level = 4
+ }
+
handler := &gzipHandler{
- Options: DefaultOptions,
+ config: cfg,
gzPool: sync.Pool{
New: func() interface{} {
- gz, err := gzip.NewWriterLevel(io.Discard, level)
- if err != nil {
- panic(err)
- }
+ gz, _ := gzip.NewWriterLevel(io.Discard, level)
return gz
},
},
}
- for _, setter := range options {
- setter(handler.Options)
- }
return handler
}
+// Handle is a middleware function for handling gzip compression in HTTP requests and responses.
+// It first checks if the request has a "Content-Encoding" header set to "gzip" and if a decompression
+// function is provided, it will call the decompression function. If the handler is set to decompress only,
+// or if the custom compression decision function indicates not to compress, it will return early.
+// Otherwise, it retrieves a gzip.Writer from the pool, sets the necessary response headers for gzip encoding,
+// and wraps the response writer with a gzipWriter. After the request is processed, it ensures the gzip.Writer
+// is properly closed and the "Content-Length" header is set based on the response size.
func (g *gzipHandler) Handle(c *gin.Context) {
- if fn := g.DecompressFn; fn != nil && c.Request.Header.Get("Content-Encoding") == "gzip" {
+ if fn := g.decompressFn; fn != nil && strings.Contains(c.Request.Header.Get("Content-Encoding"), "gzip") {
fn(c)
}
- if !g.shouldCompress(c.Request) {
+ if g.decompressOnly ||
+ (g.customShouldCompressFn != nil && !g.customShouldCompressFn(c)) ||
+ (g.customShouldCompressFn == nil && !g.shouldCompress(c.Request)) {
return
}
gz := g.gzPool.Get().(*gzip.Writer)
- defer g.gzPool.Put(gz)
- defer gz.Reset(io.Discard)
gz.Reset(c.Writer)
- c.Header("Content-Encoding", "gzip")
- c.Header("Vary", "Accept-Encoding")
+ c.Header(headerContentEncoding, "gzip")
+ c.Writer.Header().Add(headerVary, headerAcceptEncoding)
+ // check ETag Header
+ originalEtag := c.GetHeader("ETag")
+ if originalEtag != "" && !strings.HasPrefix(originalEtag, "W/") {
+ c.Header("ETag", "W/"+originalEtag)
+ }
c.Writer = &gzipWriter{c.Writer, gz}
defer func() {
if c.Writer.Size() < 0 {
// do not write gzip footer when nothing is written to the response body
gz.Reset(io.Discard)
}
- gz.Close()
- c.Header("Content-Length", fmt.Sprint(c.Writer.Size()))
+ _ = gz.Close()
+ if c.Writer.Size() > -1 {
+ c.Header("Content-Length", strconv.Itoa(c.Writer.Size()))
+ }
+ g.gzPool.Put(gz)
}()
c.Next()
}
func (g *gzipHandler) shouldCompress(req *http.Request) bool {
- if !strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") ||
- strings.Contains(req.Header.Get("Connection"), "Upgrade") ||
- strings.Contains(req.Header.Get("Accept"), "text/event-stream") {
+ if !strings.Contains(req.Header.Get(headerAcceptEncoding), "gzip") ||
+ strings.Contains(req.Header.Get("Connection"), "Upgrade") {
return false
}
+ // Check if the request path is excluded from compression
extension := filepath.Ext(req.URL.Path)
- if g.ExcludedExtensions.Contains(extension) {
- return false
- }
-
- if g.ExcludedPaths.Contains(req.URL.Path) {
- return false
- }
- if g.ExcludedPathesRegexs.Contains(req.URL.Path) {
+ if g.excludedExtensions.Contains(extension) ||
+ g.excludedPaths.Contains(req.URL.Path) ||
+ g.excludedPathesRegexs.Contains(req.URL.Path) {
return false
}