/
gzip.go
63 lines (57 loc) · 1.58 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
// Copyright 2014 struktur AG. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package httputils
import (
"compress/gzip"
"compress/zlib"
"io"
"net/http"
"strings"
)
type gzipResponseWriter struct {
io.Writer
http.ResponseWriter
}
func (w gzipResponseWriter) Write(b []byte) (int, error) {
return w.Writer.Write(b)
}
// MakeGzipHandler wraps handler such that its output will be compressed
// according to what the client supports.
func MakeGzipHandler(handler http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
encodings := strings.Split(r.Header.Get("Accept-Encoding"), ",")
var w_compressed io.WriteCloser
var err error
for _, encoding := range encodings {
encoding = strings.TrimSpace(encoding)
switch encoding {
case "gzip":
w_compressed, err = gzip.NewWriterLevel(w, gzip.BestSpeed)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer w_compressed.Close()
w.Header().Set("Content-Encoding", "gzip")
case "deflate":
w_compressed, err = zlib.NewWriterLevel(w, zlib.BestSpeed)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer w_compressed.Close()
w.Header().Set("Content-Encoding", "deflate")
}
if w_compressed != nil {
break
}
}
if w_compressed == nil {
handler(w, r)
return
}
w.Header().Set("Vary", "Accept-Encoding")
handler(gzipResponseWriter{Writer: w_compressed, ResponseWriter: w}, r)
}
}