-
Notifications
You must be signed in to change notification settings - Fork 1
/
gzip.go
103 lines (85 loc) · 2.69 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2021 Flamego. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gzip
import (
"bufio"
"compress/gzip"
"net"
"net/http"
"strings"
"github.com/flamego/flamego"
"github.com/pkg/errors"
)
const (
headerAcceptEncoding = "Accept-Encoding"
headerContentEncoding = "Content-Encoding"
headerVary = "Vary"
)
// Options represents a struct for specifying configuration options for the gzip
// middleware.
type Options struct {
// CompressionLevel indicates the compression level, possible values are between
// 0 to 9 (from no compression to best compression). Default is 4.
CompressionLevel int
}
func isCompressionLevelValid(level int) bool {
return level == gzip.DefaultCompression ||
level == gzip.NoCompression ||
(level >= gzip.BestSpeed && level <= gzip.BestCompression)
}
func prepareOptions(options []Options) Options {
var opt Options
if len(options) > 0 {
opt = options[0]
}
if !isCompressionLevelValid(opt.CompressionLevel) {
// For web content, level 4 seems to be a sweet spot.
opt.CompressionLevel = 4
}
return opt
}
// Gzip returns a Handler that adds gzip compression to all requests. Make sure
// to include the gzip middleware above other middleware that alter the response
// body (like the render middleware).
func Gzip(options ...Options) flamego.Handler {
opt := prepareOptions(options)
return flamego.ContextInvoker(func(ctx flamego.Context) {
if !strings.Contains(ctx.Request().Header.Get(headerAcceptEncoding), "gzip") {
return
}
headers := ctx.ResponseWriter().Header()
headers.Set(headerContentEncoding, "gzip")
headers.Set(headerVary, headerAcceptEncoding)
// We've made sure compression level is valid in prepareOptions,
// no need to check same error again.
gz, err := gzip.NewWriterLevel(ctx.ResponseWriter(), opt.CompressionLevel)
if err != nil {
panic("gzip: " + err.Error())
}
defer func() { _ = gz.Close() }()
w := &responseWriter{
writer: gz,
ResponseWriter: ctx.ResponseWriter(),
}
ctx.MapTo(w, (*http.ResponseWriter)(nil))
ctx.Next()
// Delete content length after we know we have been written to
ctx.ResponseWriter().Header().Del("Content-Length")
})
}
type responseWriter struct {
writer *gzip.Writer
flamego.ResponseWriter
}
func (w *responseWriter) Write(p []byte) (int, error) {
return w.writer.Write(p)
}
var _ http.Hijacker = (*responseWriter)(nil)
func (w *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
hijacker, ok := w.ResponseWriter.(http.Hijacker)
if !ok {
return nil, nil, errors.New("the ResponseWriter doesn't support the Hijacker interface")
}
return hijacker.Hijack()
}