summaryrefslogtreecommitdiff
path: root/handler.go
blob: 06bade49f67c0babc3e687204f1c371746c8a439 (plain)
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
package gziphandler

import (
	"compress/gzip"
	"io/ioutil"
	"net/http"
	"strings"
	"sync"
)

type respWriter struct {
	http.ResponseWriter
	w *gzip.Writer
}

func (w *respWriter) Write(d []byte) (int, error) {
	return w.w.Write(d)
}

func (w *respWriter) Close() error {
	return w.w.Close()
}

type handler struct {
	next http.Handler
	pool sync.Pool
}

func acceptsGzip(r *http.Request) bool {
	ae := r.Header.Get("Accept-Encoding")
	for _, e := range strings.Split(ae, ",") {
		vals := strings.Split(e, ";")
		if len(vals) < 1 {
			continue
		}
		if strings.TrimSpace(vals[0]) == "gzip" {
			return true
		}
	}
	return false
}

func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	if !acceptsGzip(r) {
		h.next.ServeHTTP(w, r)
		return
	}
	w.Header().Set("Content-Encoding", "gzip")
	gw := h.pool.Get().(*gzip.Writer)
	gw.Reset(w)
	rw := &respWriter{
		ResponseWriter: w,
		w:              gw,
	}
	h.next.ServeHTTP(rw, r)
	gw.Close()
	h.pool.Put(gw)
}

// Handler returns an http.Handler that compresses the response data written
// by an existing handler h, using the compress/gzip.Writer with default
// compression level.
func Handler(h http.Handler) http.Handler {
	return &handler{
		next: h,
		pool: sync.Pool{
			New: func() interface{} {
				return gzip.NewWriter(ioutil.Discard)
			},
		},
	}
}