Merge branch 'v1.3'
This commit is contained in:
commit
2a578748fd
4 changed files with 92 additions and 9 deletions
|
@ -6,12 +6,26 @@ import (
|
||||||
"github.com/NYTimes/gziphandler"
|
"github.com/NYTimes/gziphandler"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Compress is a middleware that allows redirections
|
const (
|
||||||
type Compress struct {
|
contentEncodingHeader = "Content-Encoding"
|
||||||
}
|
)
|
||||||
|
|
||||||
// ServerHTTP is a function used by negroni
|
// Compress is a middleware that allows redirection
|
||||||
|
type Compress struct{}
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
if isEncoded(r.Header) {
|
||||||
|
next.ServeHTTP(rw, r)
|
||||||
|
} else {
|
||||||
newGzipHandler := gziphandler.GzipHandler(next)
|
newGzipHandler := gziphandler.GzipHandler(next)
|
||||||
newGzipHandler.ServeHTTP(rw, r)
|
newGzipHandler.ServeHTTP(rw, r)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func isEncoded(headers http.Header) bool {
|
||||||
|
header := headers.Get(contentEncodingHeader)
|
||||||
|
// According to https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding,
|
||||||
|
// content is not encoded if the header 'Content-Encoding' is empty or equals to 'identity'.
|
||||||
|
return header != "" && header != "identity"
|
||||||
|
}
|
||||||
|
|
69
middlewares/compress_test.go
Normal file
69
middlewares/compress_test.go
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
package middlewares
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/NYTimes/gziphandler"
|
||||||
|
"github.com/containous/traefik/testhelpers"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
acceptEncodingHeader = "Accept-Encoding"
|
||||||
|
varyHeader = "Vary"
|
||||||
|
gzip = "gzip"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestShouldCompressWhenNoContentEncodingHeader(t *testing.T) {
|
||||||
|
handler := &Compress{}
|
||||||
|
|
||||||
|
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost", nil)
|
||||||
|
req.Header.Add(acceptEncodingHeader, gzip)
|
||||||
|
|
||||||
|
baseBody := generateBytes(gziphandler.DefaultMinSize)
|
||||||
|
next := func(rw http.ResponseWriter, r *http.Request) {
|
||||||
|
rw.Write(baseBody)
|
||||||
|
}
|
||||||
|
rw := httptest.NewRecorder()
|
||||||
|
|
||||||
|
handler.ServeHTTP(rw, req, next)
|
||||||
|
|
||||||
|
assert.Equal(t, gzip, rw.Header().Get(contentEncodingHeader))
|
||||||
|
assert.Equal(t, acceptEncodingHeader, rw.Header().Get(varyHeader))
|
||||||
|
|
||||||
|
if assert.ObjectsAreEqualValues(rw.Body.Bytes(), baseBody) {
|
||||||
|
assert.Fail(t, "expected a compressed body", "got %v", rw.Body.Bytes())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestShouldNotCompressWhenContentEncodingHeader(t *testing.T) {
|
||||||
|
handler := &Compress{}
|
||||||
|
|
||||||
|
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost", nil)
|
||||||
|
req.Header.Add(acceptEncodingHeader, gzip)
|
||||||
|
req.Header.Add(contentEncodingHeader, gzip)
|
||||||
|
|
||||||
|
baseBody := generateBytes(gziphandler.DefaultMinSize)
|
||||||
|
|
||||||
|
next := func(rw http.ResponseWriter, r *http.Request) {
|
||||||
|
rw.Write(baseBody)
|
||||||
|
}
|
||||||
|
|
||||||
|
rw := httptest.NewRecorder()
|
||||||
|
handler.ServeHTTP(rw, req, next)
|
||||||
|
|
||||||
|
assert.Equal(t, "", rw.Header().Get(contentEncodingHeader))
|
||||||
|
assert.Equal(t, "", rw.Header().Get(varyHeader))
|
||||||
|
|
||||||
|
assert.EqualValues(t, rw.Body.Bytes(), baseBody)
|
||||||
|
}
|
||||||
|
|
||||||
|
func generateBytes(len int) []byte {
|
||||||
|
var value []byte
|
||||||
|
for i := 0; i < len; i++ {
|
||||||
|
value = append(value, 0x61)
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
}
|
|
@ -8,9 +8,9 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ArthurHlt/go-eureka-client/eureka"
|
"github.com/ArthurHlt/go-eureka-client/eureka"
|
||||||
log "github.com/Sirupsen/logrus"
|
|
||||||
"github.com/cenk/backoff"
|
"github.com/cenk/backoff"
|
||||||
"github.com/containous/traefik/job"
|
"github.com/containous/traefik/job"
|
||||||
|
"github.com/containous/traefik/log"
|
||||||
"github.com/containous/traefik/provider"
|
"github.com/containous/traefik/provider"
|
||||||
"github.com/containous/traefik/safe"
|
"github.com/containous/traefik/safe"
|
||||||
"github.com/containous/traefik/types"
|
"github.com/containous/traefik/types"
|
||||||
|
|
|
@ -167,13 +167,13 @@ func (p *Provider) loadMarathonConfig() *types.Configuration {
|
||||||
|
|
||||||
applications, err := p.marathonClient.Applications(nil)
|
applications, err := p.marathonClient.Applications(nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Failed to create a client for marathon, error: %s", err)
|
log.Errorf("Failed to retrieve applications from Marathon, error: %s", err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks, err := p.marathonClient.AllTasks(&marathon.AllTasksOpts{Status: "running"})
|
tasks, err := p.marathonClient.AllTasks(&marathon.AllTasksOpts{Status: "running"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Failed to create a client for marathon, error: %s", err)
|
log.Errorf("Failed to retrieve task from Marathon, error: %s", err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue