mirror of
https://git.phreedom.club/localhost_frssoft/compy.git
synced 2024-11-27 10:31:31 +00:00
Transcode via Brotli
Brotli offers 20% better compression than gzip.
This commit is contained in:
parent
ecfa095a14
commit
389077e0fa
|
@ -11,7 +11,7 @@ Features:
|
||||||
- HTTPS proxy (encrypted connection between client and proxy)
|
- HTTPS proxy (encrypted connection between client and proxy)
|
||||||
- man in the middle support (compress HTTPS traffic)
|
- man in the middle support (compress HTTPS traffic)
|
||||||
- HTTP2 support (over TLS)
|
- HTTP2 support (over TLS)
|
||||||
- gzip compression
|
- Brotli and gzip compression
|
||||||
- transcode animated GIFs to static images
|
- transcode animated GIFs to static images
|
||||||
- transcode JPEG images to desired quality using libjpeg
|
- transcode JPEG images to desired quality using libjpeg
|
||||||
- transcode PNG and JPEG images to WebP
|
- transcode PNG and JPEG images to WebP
|
||||||
|
|
5
compy.go
5
compy.go
|
@ -18,6 +18,7 @@ var (
|
||||||
ca = flag.String("ca", "", "CA path")
|
ca = flag.String("ca", "", "CA path")
|
||||||
caKey = flag.String("cakey", "", "CA key path")
|
caKey = flag.String("cakey", "", "CA key path")
|
||||||
|
|
||||||
|
brotli = flag.Int("brotli", -1, "Brotli compression level (0-11, default 6)")
|
||||||
jpeg = flag.Int("jpeg", 50, "jpeg quality (1-100, 0 to disable)")
|
jpeg = flag.Int("jpeg", 50, "jpeg quality (1-100, 0 to disable)")
|
||||||
gif = flag.Bool("gif", true, "transcode gifs into static images")
|
gif = flag.Bool("gif", true, "transcode gifs into static images")
|
||||||
gzip = flag.Int("gzip", -1, "gzip compression level (0-9, default 6)")
|
gzip = flag.Int("gzip", -1, "gzip compression level (0-9, default 6)")
|
||||||
|
@ -56,9 +57,9 @@ func main() {
|
||||||
|
|
||||||
var ttc proxy.Transcoder
|
var ttc proxy.Transcoder
|
||||||
if *minify {
|
if *minify {
|
||||||
ttc = &tc.Gzip{tc.NewMinifier(), *gzip, false}
|
ttc = &tc.Zip{tc.NewMinifier(), *brotli, *gzip, false}
|
||||||
} else {
|
} else {
|
||||||
ttc = &tc.Gzip{&tc.Identity{}, *gzip, true}
|
ttc = &tc.Zip{&tc.Identity{}, *brotli, *gzip, true}
|
||||||
}
|
}
|
||||||
|
|
||||||
p.AddTranscoder("text/css", ttc)
|
p.AddTranscoder("text/css", ttc)
|
||||||
|
|
|
@ -15,6 +15,7 @@ import (
|
||||||
"github.com/barnacs/compy/proxy"
|
"github.com/barnacs/compy/proxy"
|
||||||
tc "github.com/barnacs/compy/transcoder"
|
tc "github.com/barnacs/compy/transcoder"
|
||||||
"github.com/chai2010/webp"
|
"github.com/chai2010/webp"
|
||||||
|
brotlidec "gopkg.in/kothar/brotli-go.v0/dec"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test(t *testing.T) {
|
func Test(t *testing.T) {
|
||||||
|
@ -34,7 +35,7 @@ func (s *CompyTest) SetUpSuite(c *C) {
|
||||||
|
|
||||||
s.proxy = proxy.New()
|
s.proxy = proxy.New()
|
||||||
s.proxy.AddTranscoder("image/jpeg", tc.NewJpeg(50))
|
s.proxy.AddTranscoder("image/jpeg", tc.NewJpeg(50))
|
||||||
s.proxy.AddTranscoder("text/html", &tc.Gzip{&tc.Identity{}, *gzip, true})
|
s.proxy.AddTranscoder("text/html", &tc.Zip{&tc.Identity{}, *brotli, *gzip, true})
|
||||||
go func() {
|
go func() {
|
||||||
err := s.proxy.Start(*host)
|
err := s.proxy.Start(*host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -91,6 +92,23 @@ func (s *CompyTest) TestGzip(c *C) {
|
||||||
c.Assert(err, IsNil)
|
c.Assert(err, IsNil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *CompyTest) TestBrotli(c *C) {
|
||||||
|
req, err := http.NewRequest("GET", s.server.URL+"/html", nil)
|
||||||
|
c.Assert(err, IsNil)
|
||||||
|
req.Header.Add("Accept-Encoding", "br, gzip")
|
||||||
|
|
||||||
|
resp, err := s.client.Do(req)
|
||||||
|
c.Assert(err, IsNil)
|
||||||
|
defer resp.Body.Close()
|
||||||
|
c.Assert(resp.StatusCode, Equals, 200)
|
||||||
|
c.Assert(resp.Header.Get("Content-Encoding"), Equals, "br")
|
||||||
|
|
||||||
|
brr := brotlidec.NewBrotliReader(resp.Body)
|
||||||
|
defer brr.Close()
|
||||||
|
_, err = ioutil.ReadAll(brr)
|
||||||
|
c.Assert(err, IsNil)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *CompyTest) TestJpeg(c *C) {
|
func (s *CompyTest) TestJpeg(c *C) {
|
||||||
req, err := http.NewRequest("GET", s.server.URL+"/image/jpeg", nil)
|
req, err := http.NewRequest("GET", s.server.URL+"/image/jpeg", nil)
|
||||||
c.Assert(err, IsNil)
|
c.Assert(err, IsNil)
|
||||||
|
|
|
@ -1,54 +0,0 @@
|
||||||
package transcoder
|
|
||||||
|
|
||||||
import (
|
|
||||||
"compress/gzip"
|
|
||||||
"github.com/barnacs/compy/proxy"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Gzip struct {
|
|
||||||
proxy.Transcoder
|
|
||||||
CompressionLevel int
|
|
||||||
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, err := gzip.NewWriterLevel(w.Writer, t.CompressionLevel)
|
|
||||||
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 (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") == ""
|
|
||||||
}
|
|
63
transcoder/zip.go
Normal file
63
transcoder/zip.go
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
package transcoder
|
||||||
|
|
||||||
|
import (
|
||||||
|
"compress/gzip"
|
||||||
|
"github.com/barnacs/compy/proxy"
|
||||||
|
brotlienc "gopkg.in/kothar/brotli-go.v0/enc"
|
||||||
|
"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")
|
||||||
|
}
|
||||||
|
|
||||||
|
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") == ""
|
||||||
|
}
|
Loading…
Reference in a new issue