mirror of
https://git.phreedom.club/localhost_frssoft/compy.git
synced 2024-11-30 12:01:29 +00:00
Honor Accept-Encoding header in gzip transcoder
This avoids sending compressed data to clients which do not support it.
This commit is contained in:
parent
cac3b84a5d
commit
11a40b5051
|
@ -16,7 +16,7 @@ type Proxy struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Transcoder interface {
|
type Transcoder interface {
|
||||||
Transcode(*ResponseWriter, *ResponseReader) error
|
Transcode(*ResponseWriter, *ResponseReader, http.Header) error
|
||||||
}
|
}
|
||||||
|
|
||||||
func New() *Proxy {
|
func New() *Proxy {
|
||||||
|
@ -68,7 +68,7 @@ func (p *Proxy) handle(w http.ResponseWriter, r *http.Request) error {
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
rw := newResponseWriter(w)
|
rw := newResponseWriter(w)
|
||||||
rr := newResponseReader(resp)
|
rr := newResponseReader(resp)
|
||||||
err = p.proxyResponse(rw, rr)
|
err = p.proxyResponse(rw, rr, r.Header)
|
||||||
read := rr.counter.Count()
|
read := rr.counter.Count()
|
||||||
written := rw.rw.Count()
|
written := rw.rw.Count()
|
||||||
log.Printf("transcoded: %d -> %d (%3.1f%%)", read, written, float64(written)/float64(read)*100)
|
log.Printf("transcoded: %d -> %d (%3.1f%%)", read, written, float64(written)/float64(read)*100)
|
||||||
|
@ -92,14 +92,14 @@ func forward(r *http.Request) (*http.Response, error) {
|
||||||
return http.DefaultTransport.RoundTrip(r)
|
return http.DefaultTransport.RoundTrip(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Proxy) proxyResponse(w *ResponseWriter, r *ResponseReader) error {
|
func (p *Proxy) proxyResponse(w *ResponseWriter, r *ResponseReader, headers http.Header) error {
|
||||||
w.takeHeaders(r)
|
w.takeHeaders(r)
|
||||||
transcoder, found := p.transcoders[r.ContentType()]
|
transcoder, found := p.transcoders[r.ContentType()]
|
||||||
if !found {
|
if !found {
|
||||||
return w.ReadFrom(r)
|
return w.ReadFrom(r)
|
||||||
}
|
}
|
||||||
w.setChunked()
|
w.setChunked()
|
||||||
if err := transcoder.Transcode(w, r); err != nil {
|
if err := transcoder.Transcode(w, r, headers); err != nil {
|
||||||
return fmt.Errorf("transcoding error: %s", err)
|
return fmt.Errorf("transcoding error: %s", err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -3,11 +3,12 @@ package transcoder
|
||||||
import (
|
import (
|
||||||
"github.com/barnacs/compy/proxy"
|
"github.com/barnacs/compy/proxy"
|
||||||
"image/gif"
|
"image/gif"
|
||||||
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Gif struct{}
|
type Gif struct{}
|
||||||
|
|
||||||
func (t *Gif) Transcode(w *proxy.ResponseWriter, r *proxy.ResponseReader) error {
|
func (t *Gif) Transcode(w *proxy.ResponseWriter, r *proxy.ResponseReader, headers http.Header) error {
|
||||||
img, err := gif.Decode(r)
|
img, err := gif.Decode(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -3,6 +3,8 @@ package transcoder
|
||||||
import (
|
import (
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
"github.com/barnacs/compy/proxy"
|
"github.com/barnacs/compy/proxy"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Gzip struct {
|
type Gzip struct {
|
||||||
|
@ -10,7 +12,7 @@ type Gzip struct {
|
||||||
SkipGzipped bool
|
SkipGzipped bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Gzip) Transcode(w *proxy.ResponseWriter, r *proxy.ResponseReader) error {
|
func (t *Gzip) Transcode(w *proxy.ResponseWriter, r *proxy.ResponseReader, headers http.Header) error {
|
||||||
if t.decompress(r) {
|
if t.decompress(r) {
|
||||||
gzr, err := gzip.NewReader(r.Reader)
|
gzr, err := gzip.NewReader(r.Reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -21,13 +23,22 @@ func (t *Gzip) Transcode(w *proxy.ResponseWriter, r *proxy.ResponseReader) error
|
||||||
r.Header().Del("Content-Encoding")
|
r.Header().Del("Content-Encoding")
|
||||||
w.Header().Del("Content-Encoding")
|
w.Header().Del("Content-Encoding")
|
||||||
}
|
}
|
||||||
if compress(r) {
|
|
||||||
|
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)
|
gzw := gzip.NewWriter(w.Writer)
|
||||||
defer gzw.Flush()
|
defer gzw.Flush()
|
||||||
w.Writer = gzw
|
w.Writer = gzw
|
||||||
w.Header().Set("Content-Encoding", "gzip")
|
w.Header().Set("Content-Encoding", "gzip")
|
||||||
}
|
}
|
||||||
return t.Transcoder.Transcode(w, r)
|
return t.Transcoder.Transcode(w, r, headers)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Gzip) decompress(r *proxy.ResponseReader) bool {
|
func (t *Gzip) decompress(r *proxy.ResponseReader) bool {
|
||||||
|
|
|
@ -2,10 +2,11 @@ package transcoder
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/barnacs/compy/proxy"
|
"github.com/barnacs/compy/proxy"
|
||||||
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Identity struct{}
|
type Identity struct{}
|
||||||
|
|
||||||
func (i *Identity) Transcode(w *proxy.ResponseWriter, r *proxy.ResponseReader) error {
|
func (i *Identity) Transcode(w *proxy.ResponseWriter, r *proxy.ResponseReader, headers http.Header) error {
|
||||||
return w.ReadFrom(r)
|
return w.ReadFrom(r)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package transcoder
|
||||||
import (
|
import (
|
||||||
"github.com/barnacs/compy/proxy"
|
"github.com/barnacs/compy/proxy"
|
||||||
"github.com/pixiv/go-libjpeg/jpeg"
|
"github.com/pixiv/go-libjpeg/jpeg"
|
||||||
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Jpeg struct {
|
type Jpeg struct {
|
||||||
|
@ -20,7 +21,7 @@ func NewJpeg(quality int) *Jpeg {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Jpeg) Transcode(w *proxy.ResponseWriter, r *proxy.ResponseReader) error {
|
func (t *Jpeg) Transcode(w *proxy.ResponseWriter, r *proxy.ResponseReader, headers http.Header) error {
|
||||||
img, err := jpeg.Decode(r, t.decOptions)
|
img, err := jpeg.Decode(r, t.decOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"github.com/tdewolff/minify/css"
|
"github.com/tdewolff/minify/css"
|
||||||
"github.com/tdewolff/minify/html"
|
"github.com/tdewolff/minify/html"
|
||||||
"github.com/tdewolff/minify/js"
|
"github.com/tdewolff/minify/js"
|
||||||
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Minifier struct {
|
type Minifier struct {
|
||||||
|
@ -24,6 +25,6 @@ func NewMinifier() *Minifier {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Minifier) Transcode(w *proxy.ResponseWriter, r *proxy.ResponseReader) error {
|
func (t *Minifier) Transcode(w *proxy.ResponseWriter, r *proxy.ResponseReader, headers http.Header) error {
|
||||||
return t.m.Minify(r.ContentType(), w, r)
|
return t.m.Minify(r.ContentType(), w, r)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,11 +3,12 @@ package transcoder
|
||||||
import (
|
import (
|
||||||
"github.com/barnacs/compy/proxy"
|
"github.com/barnacs/compy/proxy"
|
||||||
"image/png"
|
"image/png"
|
||||||
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Png struct{}
|
type Png struct{}
|
||||||
|
|
||||||
func (t *Png) Transcode(w *proxy.ResponseWriter, r *proxy.ResponseReader) error {
|
func (t *Png) Transcode(w *proxy.ResponseWriter, r *proxy.ResponseReader, headers http.Header) error {
|
||||||
img, err := png.Decode(r)
|
img, err := png.Decode(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
Loading…
Reference in a new issue