-
Notifications
You must be signed in to change notification settings - Fork 0
/
gzip.go
89 lines (76 loc) · 1.74 KB
/
gzip.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
package gzip
import (
"bufio"
"compress/gzip"
"io"
"io/ioutil"
"net"
"net/http"
"strings"
"sync"
"github.com/go-kira/kira"
)
var gzPool sync.Pool
// New ...
func New() Gzip {
return Gzip{}
}
// Gzip ...
type Gzip struct{}
// Middleware ...
func (g Gzip) Middleware(ctx *kira.Context, next kira.HandlerFunc) {
gzPool.New = func() interface{} {
gz, err := gzip.NewWriterLevel(ioutil.Discard, ctx.Config().GetInt("gzip.level", gzip.DefaultCompression))
if err != nil {
panic(err)
}
return gz
}
if !strings.Contains(ctx.Request().Header.Get("Accept-Encoding"), "gzip") {
next(ctx)
return
}
// GZip
gz := gzPool.Get().(*gzip.Writer)
defer gzPool.Put(gz)
defer gz.Reset(ioutil.Discard)
gz.Reset(ctx.Response())
ctx.Response().Header().Set("Content-Encoding", "gzip")
ctx.Response().Header().Set("Vary", "Accept-Encoding")
ctx.SetResponse(&gzipWriter{
Writer: gz,
ResponseWriter: ctx.Response(),
})
defer func() {
gz.Close()
}()
// Next to the next handler.
next(ctx)
}
// Custom ResponseWriter for gzip
type gzipWriter struct {
io.Writer
http.ResponseWriter
}
func (w *gzipWriter) WriteHeader(code int) {
if code == http.StatusNoContent {
w.ResponseWriter.Header().Del("Content-Encoding")
}
w.Header().Del("Content-Length")
w.ResponseWriter.WriteHeader(code)
}
func (w *gzipWriter) Write(b []byte) (int, error) {
if w.Header().Get("Content-Type") == "" {
w.Header().Set("Content-Type", http.DetectContentType(b))
}
return w.Writer.Write(b)
}
func (w *gzipWriter) Flush() {
w.Writer.(*gzip.Writer).Flush()
if flusher, ok := w.ResponseWriter.(http.Flusher); ok {
flusher.Flush()
}
}
func (w *gzipWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
return w.ResponseWriter.(http.Hijacker).Hijack()
}