-
Notifications
You must be signed in to change notification settings - Fork 92
/
gzip.go
36 lines (31 loc) · 900 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
package terminal
import (
"compress/gzip"
"io"
"net/http"
"strings"
)
type gzipResponseWriter struct {
io.Writer
http.ResponseWriter
}
// Use the Writer part of gzipResponseWriter to write the output.
func (w gzipResponseWriter) Write(b []byte) (int, error) {
return w.Writer.Write(b)
}
func makeGzipHandler(handler http.HandlerFunc) http.HandlerFunc {
return func(resp http.ResponseWriter, req *http.Request) {
// Check if the client can accept the gzip encoding.
if !strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") {
// The client cannot accept it, so return the output
// uncompressed.
handler(resp, req)
return
}
// Set the HTTP header indicating encoding.
resp.Header().Set("Content-Encoding", "gzip")
gzipWriter := gzip.NewWriter(resp)
defer gzipWriter.Close()
handler(gzipResponseWriter{Writer: gzipWriter, ResponseWriter: resp}, req)
}
}