compy/transcoder/gzip.go
Andrew Gaul 11a40b5051 Honor Accept-Encoding header in gzip transcoder
This avoids sending compressed data to clients which do not support
it.
2017-01-14 15:33:35 -08:00

51 lines
1.1 KiB
Go

package transcoder
import (
"compress/gzip"
"github.com/barnacs/compy/proxy"
"net/http"
"strings"
)
type Gzip struct {
proxy.Transcoder
SkipGzipped bool
}
func (t *Gzip) Transcode(w *proxy.ResponseWriter, r *proxy.ResponseReader, headers http.Header) error {
if t.decompress(r) {
gzr, err := gzip.NewReader(r.Reader)
if err != nil {
return err
}
defer gzr.Close()
r.Reader = gzr
r.Header().Del("Content-Encoding")
w.Header().Del("Content-Encoding")
}
shouldGzip := false
for _, v := range strings.Split(headers.Get("Accept-Encoding"), ", ") {
if strings.SplitN(v, ";", 2)[0] == "gzip" {
shouldGzip = true
break
}
}
if shouldGzip && compress(r) {
gzw := gzip.NewWriter(w.Writer)
defer gzw.Flush()
w.Writer = gzw
w.Header().Set("Content-Encoding", "gzip")
}
return t.Transcoder.Transcode(w, r, headers)
}
func (t *Gzip) decompress(r *proxy.ResponseReader) bool {
return !t.SkipGzipped && r.Header().Get("Content-Encoding") == "gzip"
}
func compress(r *proxy.ResponseReader) bool {
return r.Header().Get("Content-Encoding") == ""
}