-
Notifications
You must be signed in to change notification settings - Fork 0
/
gzip_writer.go
56 lines (46 loc) · 1.21 KB
/
gzip_writer.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
package TeaGo
import (
"bufio"
"compress/gzip"
"errors"
"net"
"net/http"
)
type gzipWriter struct {
responseWriter http.ResponseWriter
gzip *gzip.Writer
}
func newGzipWriter(writer http.ResponseWriter, level int) (*gzipWriter, error) {
w, err := gzip.NewWriterLevel(writer, level)
if err != nil {
return nil, err
}
writer.Header().Set("Content-Encoding", "gzip")
writer.Header().Set("Transfer-Encoding", "chunked")
writer.Header().Set("Vary", "Accept-Encoding")
writer.Header().Set("Accept-encoding", "gzip, deflate, br")
return &gzipWriter{
responseWriter: writer,
gzip: w,
}, nil
}
func (this *gzipWriter) Header() http.Header {
return this.responseWriter.Header()
}
func (this *gzipWriter) WriteHeader(status int) {
this.responseWriter.WriteHeader(status)
}
func (this *gzipWriter) Write(b []byte) (int, error) {
return this.gzip.Write(b)
}
func (this *gzipWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
h, ok := this.responseWriter.(http.Hijacker)
if ok {
return h.Hijack()
}
return nil, nil, errors.New("http.Hijacker not implemented by underlying http.ResponseWriter")
}
func (this *gzipWriter) Close() error {
_ = this.gzip.Flush()
return this.gzip.Close()
}