| package app |
|
|
| import ( |
| "compress/gzip" |
| "io" |
| "net/http" |
| "strings" |
| "sync" |
|
|
| "github.com/felixge/httpsnoop" |
| ) |
|
|
| var gzPool = sync.Pool{New: func() interface{} { return gzip.NewWriter(nil) }} |
|
|
| |
| func wrapGzip(next http.Handler) http.Handler { |
| return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| if !strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") || req.Header.Get("Range") != "" { |
| |
| |
| |
| |
| next.ServeHTTP(w, req) |
| return |
| } |
|
|
| |
| |
| |
| |
|
|
| var output io.Writer |
| var check sync.Once |
| cleanup := func() {} |
| getOutput := func() { |
| if w.Header().Get("Content-Encoding") != "" || w.Header().Get("Content-Type") == "" { |
| |
| output = w |
| return |
| } |
|
|
| gz := gzPool.Get().(*gzip.Writer) |
| gz.Reset(w) |
| w.Header().Set("Content-Encoding", "gzip") |
| w.Header().Set("Vary", "Accept-Encoding") |
| w.Header().Del("Content-Length") |
| cleanup = func() { |
| _ = gz.Close() |
| gzPool.Put(gz) |
| } |
| output = gz |
| } |
|
|
| ww := httpsnoop.Wrap(w, httpsnoop.Hooks{ |
| WriteHeader: func(next httpsnoop.WriteHeaderFunc) httpsnoop.WriteHeaderFunc { check.Do(getOutput); return next }, |
| Write: func(next httpsnoop.WriteFunc) httpsnoop.WriteFunc { |
| return func(b []byte) (int, error) { check.Do(getOutput); return output.Write(b) } |
| }, |
| ReadFrom: func(next httpsnoop.ReadFromFunc) httpsnoop.ReadFromFunc { |
| return func(src io.Reader) (int64, error) { check.Do(getOutput); return io.Copy(output, src) } |
| }, |
| }) |
|
|
| defer func() { cleanup() }() |
| next.ServeHTTP(ww, req) |
| }) |
| } |
|
|