forked from gin-contrib/gzip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
106 lines (90 loc) · 2.36 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package gzip
import (
"compress/gzip"
"fmt"
"io/ioutil"
"net/http"
"path/filepath"
"strings"
"sync"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
type gzipHandler struct {
*Options
gzPool sync.Pool
}
func newGzipHandler(level int, options ...Option) *gzipHandler {
handler := &gzipHandler{
Options: DefaultOptions,
gzPool: sync.Pool{
New: func() interface{} {
gz, err := gzip.NewWriterLevel(ioutil.Discard, level)
if err != nil {
panic(err)
}
return gz
},
},
}
for _, setter := range options {
setter(handler.Options)
}
return handler
}
func (g *gzipHandler) Handle(c *gin.Context) {
if fn := g.DecompressFn; fn != nil && c.Request.Header.Get("Content-Encoding") == "gzip" {
fn(c)
}
if !g.shouldCompress(c.Request) {
return
}
gz := g.gzPool.Get().(*gzip.Writer)
defer g.gzPool.Put(gz)
gz.Reset(c.Writer)
gzWriter := &gzipWriter{
ResponseWriter: c.Writer,
writer: gz,
minLength: g.Options.MinLength,
}
c.Writer = gzWriter
c.Next()
// nolint:nestif // Complex because lots of error handling.
if gzWriter.compress {
// Just close and flush the gz writer
if err := gz.Close(); err != nil {
if cErr := c.Error(fmt.Errorf("gzip: closing and flushing gzip writer: %w", err)); cErr != nil {
logrus.Error(fmt.Errorf("gzip: attaching gin error: %w", cErr))
}
}
} else {
// Reset to the original writer
gz.Reset(ioutil.Discard)
// Write the buffered data into the original writer
if _, err := gzWriter.ResponseWriter.Write(gzWriter.buffer.Bytes()); err != nil {
if cErr := c.Error(fmt.Errorf("gzip: writing buffer into original writer: %w", err)); cErr != nil {
logrus.Error(fmt.Errorf("gzip: attaching gin error: %w", cErr))
}
}
}
// Set the content length if it's still possible
c.Header("Content-Length", fmt.Sprint(c.Writer.Size()))
}
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") {
return false
}
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) {
return false
}
return true
}