mirror of
https://git.phreedom.club/localhost_frssoft/compy.git
synced 2024-11-06 00:13:20 +00:00
11a40b5051
This avoids sending compressed data to clients which do not support it.
51 lines
1.1 KiB
Go
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") == ""
|
|
}
|