From 7a263fed42aa8db28c0f6580d7060a56d4e2d638 Mon Sep 17 00:00:00 2001 From: Umputun Date: Sun, 4 Apr 2021 02:39:42 -0500 Subject: [PATCH] bump go-pkgz/rest --- app/proxy/proxy.go | 2 +- go.mod | 2 +- go.sum | 2 + vendor/github.com/go-pkgz/rest/go.mod | 2 +- vendor/github.com/go-pkgz/rest/go.sum | 2 + vendor/github.com/go-pkgz/rest/gzip.go | 65 +++++++++++++++++++++----- vendor/modules.txt | 2 +- 7 files changed, 61 insertions(+), 16 deletions(-) diff --git a/app/proxy/proxy.go b/app/proxy/proxy.go index 5fd9c16..dd68dc3 100644 --- a/app/proxy/proxy.go +++ b/app/proxy/proxy.go @@ -120,7 +120,7 @@ func (h *Http) toHttp(address string, httpPort int) string { func (h *Http) gzipHandler() func(next http.Handler) http.Handler { if h.GzEnabled { - return R.Gzip + return R.Gzip() } return func(next http.Handler) http.Handler { diff --git a/go.mod b/go.mod index b15a0ce..a374420 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.16 require ( github.com/fsouza/go-dockerclient v1.7.2 github.com/go-pkgz/lgr v0.10.4 - github.com/go-pkgz/rest v1.8.0 + github.com/go-pkgz/rest v1.8.1 github.com/pkg/errors v0.9.1 github.com/stretchr/testify v1.7.0 github.com/umputun/go-flags v1.5.1 diff --git a/go.sum b/go.sum index 08c3da5..a9c95d4 100644 --- a/go.sum +++ b/go.sum @@ -44,6 +44,8 @@ github.com/go-pkgz/lgr v0.10.4 h1:l7qyFjqEZgwRgaQQSEp6tve4A3OU80VrfzpvtEX8ngw= github.com/go-pkgz/lgr v0.10.4/go.mod h1:CD0s1z6EFpIUplV067gitF77tn25JItzwHNKAPqeCF0= github.com/go-pkgz/rest v1.8.0 h1:0AC9NKRsytwZxmvAsO2i/uQzhvMTFVOyu5UNHExpPF0= github.com/go-pkgz/rest v1.8.0/go.mod h1:FKpgK5FgSqREG323OIU/JpIc0xA7dqay9BmK7LZXTQE= +github.com/go-pkgz/rest v1.8.1 h1:M0sMbgcWxHpKjXw7Z8uF6uNcsLynaPoR0CHGczjYSw0= +github.com/go-pkgz/rest v1.8.1/go.mod h1:wZ/dGipZUaF9to0vIQl7PwDHgWQDB0jsrFg1xnAKLDw= github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= diff --git a/vendor/github.com/go-pkgz/rest/go.mod b/vendor/github.com/go-pkgz/rest/go.mod index aab6b51..7898921 100644 --- a/vendor/github.com/go-pkgz/rest/go.mod +++ b/vendor/github.com/go-pkgz/rest/go.mod @@ -4,5 +4,5 @@ go 1.15 require ( github.com/pkg/errors v0.9.1 - github.com/stretchr/testify v1.6.1 + github.com/stretchr/testify v1.7.0 ) diff --git a/vendor/github.com/go-pkgz/rest/go.sum b/vendor/github.com/go-pkgz/rest/go.sum index 7034f5b..0caa82c 100644 --- a/vendor/github.com/go-pkgz/rest/go.sum +++ b/vendor/github.com/go-pkgz/rest/go.sum @@ -8,6 +8,8 @@ github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= diff --git a/vendor/github.com/go-pkgz/rest/gzip.go b/vendor/github.com/go-pkgz/rest/gzip.go index 4e8e005..e1d3d1a 100644 --- a/vendor/github.com/go-pkgz/rest/gzip.go +++ b/vendor/github.com/go-pkgz/rest/gzip.go @@ -9,6 +9,17 @@ import ( "sync" ) +var gzDefaultContentTypes = []string{ + "text/css", + "text/javascript", + "text/xml", + "text/html", + "text/plain", + "application/javascript", + "application/x-javascript", + "application/json", +} + var gzPool = sync.Pool{ New: func() interface{} { return gzip.NewWriter(ioutil.Discard) }, } @@ -28,21 +39,51 @@ func (w *gzipResponseWriter) Write(b []byte) (int, error) { } // Gzip is a middleware compressing response -func Gzip(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") { - next.ServeHTTP(w, r) - return +func Gzip(contentTypes ...string) func(http.Handler) http.Handler { + + gzCts := gzDefaultContentTypes + if len(contentTypes) > 0 { + gzCts = contentTypes + } + + contentType := func(r *http.Request) string { + result := r.Header.Get("Content-type") + if result == "" { + return "application/octet-stream" } + return result + } - w.Header().Set("Content-Encoding", "gzip") + f := func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") { + next.ServeHTTP(w, r) + return + } - gz := gzPool.Get().(*gzip.Writer) - defer gzPool.Put(gz) + var gzOk bool + ctype := contentType(r) + for _, c := range gzCts { + if strings.EqualFold(ctype, c) { + gzOk = true + break + } + } - gz.Reset(w) - defer gz.Close() + if !gzOk { + next.ServeHTTP(w, r) + return + } - next.ServeHTTP(&gzipResponseWriter{ResponseWriter: w, Writer: gz}, r) - }) + w.Header().Set("Content-Encoding", "gzip") + gz := gzPool.Get().(*gzip.Writer) + defer gzPool.Put(gz) + + gz.Reset(w) + defer gz.Close() + + next.ServeHTTP(&gzipResponseWriter{ResponseWriter: w, Writer: gz}, r) + }) + } + return f } diff --git a/vendor/modules.txt b/vendor/modules.txt index 77dd1cf..0df04be 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -68,7 +68,7 @@ github.com/fsouza/go-dockerclient # github.com/go-pkgz/lgr v0.10.4 ## explicit github.com/go-pkgz/lgr -# github.com/go-pkgz/rest v1.8.0 +# github.com/go-pkgz/rest v1.8.1 ## explicit github.com/go-pkgz/rest github.com/go-pkgz/rest/logger