Exclude GRPC from compress
This commit is contained in:
parent
2f62ec3632
commit
5c119fe2d6
2 changed files with 28 additions and 1 deletions
|
@ -3,6 +3,7 @@ package middlewares
|
||||||
import (
|
import (
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/NYTimes/gziphandler"
|
"github.com/NYTimes/gziphandler"
|
||||||
"github.com/containous/traefik/log"
|
"github.com/containous/traefik/log"
|
||||||
|
@ -13,7 +14,12 @@ type Compress struct{}
|
||||||
|
|
||||||
// ServerHTTP is a function used by Negroni
|
// ServerHTTP is a function used by Negroni
|
||||||
func (c *Compress) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
func (c *Compress) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
||||||
gzipHandler(next).ServeHTTP(rw, r)
|
contentType := r.Header.Get("Content-Type")
|
||||||
|
if strings.HasPrefix(contentType, "application/grpc") {
|
||||||
|
next.ServeHTTP(rw, r)
|
||||||
|
} else {
|
||||||
|
gzipHandler(next).ServeHTTP(rw, r)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func gzipHandler(h http.Handler) http.Handler {
|
func gzipHandler(h http.Handler) http.Handler {
|
||||||
|
|
|
@ -16,6 +16,7 @@ import (
|
||||||
const (
|
const (
|
||||||
acceptEncodingHeader = "Accept-Encoding"
|
acceptEncodingHeader = "Accept-Encoding"
|
||||||
contentEncodingHeader = "Content-Encoding"
|
contentEncodingHeader = "Content-Encoding"
|
||||||
|
contentTypeHeader = "Content-Type"
|
||||||
varyHeader = "Vary"
|
varyHeader = "Vary"
|
||||||
gzipValue = "gzip"
|
gzipValue = "gzip"
|
||||||
)
|
)
|
||||||
|
@ -81,6 +82,26 @@ func TestShouldNotCompressWhenNoAcceptEncodingHeader(t *testing.T) {
|
||||||
assert.EqualValues(t, rw.Body.Bytes(), fakeBody)
|
assert.EqualValues(t, rw.Body.Bytes(), fakeBody)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestShouldNotCompressWhenGRPC(t *testing.T) {
|
||||||
|
handler := &Compress{}
|
||||||
|
|
||||||
|
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost", nil)
|
||||||
|
req.Header.Add(acceptEncodingHeader, gzipValue)
|
||||||
|
req.Header.Add(contentTypeHeader, "application/grpc")
|
||||||
|
|
||||||
|
baseBody := generateBytes(gziphandler.DefaultMinSize)
|
||||||
|
next := func(rw http.ResponseWriter, r *http.Request) {
|
||||||
|
rw.Write(baseBody)
|
||||||
|
}
|
||||||
|
|
||||||
|
rw := httptest.NewRecorder()
|
||||||
|
handler.ServeHTTP(rw, req, next)
|
||||||
|
|
||||||
|
assert.Empty(t, rw.Header().Get(acceptEncodingHeader))
|
||||||
|
assert.Empty(t, rw.Header().Get(contentEncodingHeader))
|
||||||
|
assert.EqualValues(t, rw.Body.Bytes(), baseBody)
|
||||||
|
}
|
||||||
|
|
||||||
func TestIntegrationShouldNotCompress(t *testing.T) {
|
func TestIntegrationShouldNotCompress(t *testing.T) {
|
||||||
fakeCompressedBody := generateBytes(100000)
|
fakeCompressedBody := generateBytes(100000)
|
||||||
comp := &Compress{}
|
comp := &Compress{}
|
||||||
|
|
Loading…
Reference in a new issue