Created
August 31, 2018 15:31
-
-
Save ggramaize/9b07a8a4d50255e13ac42a76727a4984 to your computer and use it in GitHub Desktop.
Patched transcoder/zip.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package transcoder | |
import ( | |
"compress/gzip" | |
"github.com/barnacs/compy/proxy" | |
brotlienc "gopkg.in/kothar/brotli-go.v0/enc" | |
brotlidec "gopkg.in/kothar/brotli-go.v0/dec" | |
"net/http" | |
"strings" | |
) | |
type Zip struct { | |
proxy.Transcoder | |
BrotliCompressionLevel int | |
GzipCompressionLevel int | |
SkipGzipped bool | |
} | |
func (t *Zip) Transcode(w *proxy.ResponseWriter, r *proxy.ResponseReader, headers http.Header) error { | |
shouldBrotli := false | |
shouldGzip := false | |
for _, v := range strings.Split(headers.Get("Accept-Encoding"), ", ") { | |
switch strings.SplitN(v, ";", 2)[0] { | |
case "br": | |
shouldBrotli = true | |
case "gzip": | |
shouldGzip = true | |
} | |
} | |
// always gunzip if the client supports Brotli | |
if r.Header().Get("Content-Encoding") == "gzip" && (shouldBrotli || !t.SkipGzipped) { | |
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") | |
} | |
// unbr | |
if r.Header().Get("Content-Encoding") == "br" { | |
brr := brotlidec.NewBrotliReader(r.Reader) | |
defer brr.Close() | |
r.Reader = brr | |
r.Header().Del("Content-Encoding") | |
w.Header().Del("Content-Encoding") | |
} | |
if shouldBrotli && compress(r) { | |
params := brotlienc.NewBrotliParams() | |
params.SetQuality(t.BrotliCompressionLevel) | |
brw := brotlienc.NewBrotliWriter(params, w.Writer) | |
defer brw.Close() | |
w.Writer = brw | |
w.Header().Set("Content-Encoding", "br") | |
} else if shouldGzip && compress(r) { | |
gzw, err := gzip.NewWriterLevel(w.Writer, t.GzipCompressionLevel) | |
if err != nil { | |
return err | |
} | |
defer gzw.Close() | |
w.Writer = gzw | |
w.Header().Set("Content-Encoding", "gzip") | |
} | |
return t.Transcoder.Transcode(w, r, headers) | |
} | |
func compress(r *proxy.ResponseReader) bool { | |
return r.Header().Get("Content-Encoding") == "" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment