-
Notifications
You must be signed in to change notification settings - Fork 3
/
gzip.go
executable file
·39 lines (32 loc) · 883 Bytes
/
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
package gow
import "compress/gzip"
const (
BestCompression = gzip.BestCompression
BestSpeed = gzip.BestSpeed
DefaultCompression = gzip.DefaultCompression
NoCompression = gzip.NoCompression
)
// Gzip middleware
// 1~9 Compress level
func Gzip(level int, options ...Option) HandlerFunc {
return newGzipHandler(level, options...).Handle
}
type gzipWriter struct {
ResponseWriter
writer *gzip.Writer
}
// WriteString response string
func (g *gzipWriter) WriteString(s string) (int, error) {
g.Header().Del("Content-Length")
return g.writer.Write([]byte(s))
}
// Write response []byte
func (g *gzipWriter) Write(data []byte) (int, error) {
g.Header().Del("Content-Length")
return g.writer.Write(data)
}
// WriteHeader write gzip header
func (g *gzipWriter) WriteHeader(code int) {
g.Header().Del("Content-Length")
g.ResponseWriter.WriteHeader(code)
}