Merge branch v3.0 into master
This commit is contained in:
commit
67f0700377
43 changed files with 1528 additions and 1327 deletions
|
@ -162,7 +162,7 @@ func findTypedField(rType reflect.Type, node *parser.Node) (reflect.StructField,
|
|||
return reflect.StructField{}, false
|
||||
}
|
||||
|
||||
for i := 0; i < rType.NumField(); i++ {
|
||||
for i := range rType.NumField() {
|
||||
cField := rType.Field(i)
|
||||
|
||||
// ignore unexported fields.
|
||||
|
|
|
@ -175,6 +175,8 @@ type Compress struct {
|
|||
// MinResponseBodyBytes defines the minimum amount of bytes a response body must have to be compressed.
|
||||
// Default: 1024.
|
||||
MinResponseBodyBytes int `json:"minResponseBodyBytes,omitempty" toml:"minResponseBodyBytes,omitempty" yaml:"minResponseBodyBytes,omitempty" export:"true"`
|
||||
// DefaultEncoding specifies the default encoding if the `Accept-Encoding` header is not in the request or contains a wildcard (`*`).
|
||||
DefaultEncoding string `json:"defaultEncoding,omitempty" toml:"defaultEncoding,omitempty" yaml:"defaultEncoding,omitempty" export:"true"`
|
||||
}
|
||||
|
||||
// +k8s:deepcopy-gen=true
|
||||
|
|
|
@ -334,8 +334,8 @@ func TestOpenTelemetry(t *testing.T) {
|
|||
// TODO: the len of startUnixNano is no supposed to be 20, it should be 19
|
||||
expectedConfig := []string{
|
||||
`({"name":"traefik_config_reloads_total","description":"Config reloads","unit":"1","sum":{"dataPoints":\[{"startTimeUnixNano":"[\d]{19}","timeUnixNano":"[\d]{19}","asDouble":1}\],"aggregationTemporality":2,"isMonotonic":true}})`,
|
||||
`({"name":"traefik_config_last_reload_success","description":"Last config reload success","unit":"ms","gauge":{"dataPoints":\[{"timeUnixNano":"[\d]{19}","asDouble":1}\]}})`,
|
||||
`({"name":"traefik_open_connections","description":"How many open connections exist, by entryPoint and protocol","unit":"1","gauge":{"dataPoints":\[{"attributes":\[{"key":"entrypoint","value":{"stringValue":"test"}},{"key":"protocol","value":{"stringValue":"TCP"}}\],"timeUnixNano":"[\d]{19}","asDouble":1}\]}})`,
|
||||
`({"name":"traefik_config_last_reload_success","description":"Last config reload success","unit":"ms","gauge":{"dataPoints":\[{"startTimeUnixNano":"[\d]{19}","timeUnixNano":"[\d]{19}","asDouble":1}\]}})`,
|
||||
`({"name":"traefik_open_connections","description":"How many open connections exist, by entryPoint and protocol","unit":"1","gauge":{"dataPoints":\[{"attributes":\[{"key":"entrypoint","value":{"stringValue":"test"}},{"key":"protocol","value":{"stringValue":"TCP"}}\],"startTimeUnixNano":"[\d]{19}","timeUnixNano":"[\d]{19}","asDouble":1}\]}})`,
|
||||
}
|
||||
|
||||
registry.ConfigReloadsCounter().Add(1)
|
||||
|
@ -345,7 +345,7 @@ func TestOpenTelemetry(t *testing.T) {
|
|||
tryAssertMessage(t, c, expectedConfig)
|
||||
|
||||
expectedTLSCerts := []string{
|
||||
`({"name":"traefik_tls_certs_not_after","description":"Certificate expiration timestamp","unit":"ms","gauge":{"dataPoints":\[{"attributes":\[{"key":"key","value":{"stringValue":"value"}}\],"timeUnixNano":"[\d]{19}","asDouble":1}\]}})`,
|
||||
`({"name":"traefik_tls_certs_not_after","description":"Certificate expiration timestamp","unit":"ms","gauge":{"dataPoints":\[{"attributes":\[{"key":"key","value":{"stringValue":"value"}}\],"startTimeUnixNano":"[\d]{19}","timeUnixNano":"[\d]{19}","asDouble":1}\]}})`,
|
||||
}
|
||||
|
||||
registry.TLSCertsNotAfterTimestampGauge().With("key", "value").Set(1)
|
||||
|
@ -389,7 +389,7 @@ func TestOpenTelemetry(t *testing.T) {
|
|||
`({"name":"traefik_service_requests_total","description":"How many HTTP requests processed on a service, partitioned by status code, protocol, and method.","unit":"1","sum":{"dataPoints":\[{"attributes":\[{"key":"code","value":{"stringValue":"(?:200|404)"}},{"key":"method","value":{"stringValue":"GET"}},{"key":"service","value":{"stringValue":"ServiceReqsCounter"}}\],"startTimeUnixNano":"[\d]{19}","timeUnixNano":"[\d]{19}","asDouble":1},{"attributes":\[{"key":"code","value":{"stringValue":"(?:200|404)"}},{"key":"method","value":{"stringValue":"GET"}},{"key":"service","value":{"stringValue":"ServiceReqsCounter"}}\],"startTimeUnixNano":"[\d]{19}","timeUnixNano":"[\d]{19}","asDouble":1}\],"aggregationTemporality":2,"isMonotonic":true}})`,
|
||||
`({"name":"traefik_service_requests_tls_total","description":"How many HTTP requests with TLS processed on a service, partitioned by TLS version and TLS cipher.","unit":"1","sum":{"dataPoints":\[{"attributes":\[{"key":"service","value":{"stringValue":"test"}},{"key":"tls_cipher","value":{"stringValue":"bar"}},{"key":"tls_version","value":{"stringValue":"foo"}}\],"startTimeUnixNano":"[\d]{19}","timeUnixNano":"[\d]{19}","asDouble":1}\],"aggregationTemporality":2,"isMonotonic":true}})`,
|
||||
`({"name":"traefik_service_request_duration_seconds","description":"How long it took to process the request on a service, partitioned by status code, protocol, and method.","unit":"ms","histogram":{"dataPoints":\[{"attributes":\[{"key":"code","value":{"stringValue":"200"}},{"key":"service","value":{"stringValue":"test"}}\],"startTimeUnixNano":"[\d]{19}","timeUnixNano":"[\d]{19}","count":"1","sum":10000,"bucketCounts":\["0","0","0","0","0","0","0","0","0","0","0","0","0","0","1"\],"explicitBounds":\[0.005,0.01,0.025,0.05,0.075,0.1,0.25,0.5,0.75,1,2.5,5,7.5,10\],"min":10000,"max":10000}\],"aggregationTemporality":2}})`,
|
||||
`({"name":"traefik_service_server_up","description":"service server is up, described by gauge value of 0 or 1.","unit":"1","gauge":{"dataPoints":\[{"attributes":\[{"key":"service","value":{"stringValue":"test"}},{"key":"url","value":{"stringValue":"http://127.0.0.1"}}\],"timeUnixNano":"[\d]{19}","asDouble":1}\]}})`,
|
||||
`({"name":"traefik_service_server_up","description":"service server is up, described by gauge value of 0 or 1.","unit":"1","gauge":{"dataPoints":\[{"attributes":\[{"key":"service","value":{"stringValue":"test"}},{"key":"url","value":{"stringValue":"http://127.0.0.1"}}\],"startTimeUnixNano":"[\d]{19}","timeUnixNano":"[\d]{19}","asDouble":1}\]}})`,
|
||||
`({"name":"traefik_service_requests_bytes_total","description":"The total size of requests in bytes received by a service, partitioned by status code, protocol, and method.","unit":"1","sum":{"dataPoints":\[{"attributes":\[{"key":"code","value":{"stringValue":"404"}},{"key":"method","value":{"stringValue":"GET"}},{"key":"service","value":{"stringValue":"ServiceReqsCounter"}}\],"startTimeUnixNano":"[\d]{19}","timeUnixNano":"[\d]{19}","asDouble":1}\],"aggregationTemporality":2,"isMonotonic":true}})`,
|
||||
`({"name":"traefik_service_responses_bytes_total","description":"The total size of responses in bytes returned by a service, partitioned by status code, protocol, and method.","unit":"1","sum":{"dataPoints":\[{"attributes":\[{"key":"code","value":{"stringValue":"404"}},{"key":"method","value":{"stringValue":"GET"}},{"key":"service","value":{"stringValue":"ServiceReqsCounter"}}\],"startTimeUnixNano":"[\d]{19}","timeUnixNano":"[\d]{19}","asDouble":1}\],"aggregationTemporality":2,"isMonotonic":true}})`,
|
||||
}
|
||||
|
@ -463,7 +463,7 @@ func verifyMessage(msg string, expected []string) []error {
|
|||
re := regexp.MustCompile(pattern)
|
||||
match := re.FindStringSubmatch(msg)
|
||||
if len(match) != 2 {
|
||||
errs = append(errs, fmt.Errorf("Got %q %v, want %q", msg, match, pattern))
|
||||
errs = append(errs, fmt.Errorf("got %q %v, want %q", msg, match, pattern))
|
||||
}
|
||||
}
|
||||
return errs
|
||||
|
|
|
@ -601,6 +601,7 @@ func (s *mockSpan) SetAttributes(kv ...attribute.KeyValue) {
|
|||
func (s *mockSpan) End(...trace.SpanEndOption) {}
|
||||
func (s *mockSpan) RecordError(_ error, _ ...trace.EventOption) {}
|
||||
func (s *mockSpan) AddEvent(_ string, _ ...trace.EventOption) {}
|
||||
func (s *mockSpan) AddLink(_ trace.Link) {}
|
||||
|
||||
func (s *mockSpan) SetName(name string) { s.name = name }
|
||||
|
||||
|
|
137
pkg/middlewares/compress/acceptencoding.go
Normal file
137
pkg/middlewares/compress/acceptencoding.go
Normal file
|
@ -0,0 +1,137 @@
|
|||
package compress
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const acceptEncodingHeader = "Accept-Encoding"
|
||||
|
||||
const (
|
||||
brotliName = "br"
|
||||
gzipName = "gzip"
|
||||
identityName = "identity"
|
||||
wildcardName = "*"
|
||||
notAcceptable = "not_acceptable"
|
||||
)
|
||||
|
||||
type Encoding struct {
|
||||
Type string
|
||||
Weight *float64
|
||||
}
|
||||
|
||||
func getCompressionType(acceptEncoding []string, defaultType string) string {
|
||||
if defaultType == "" {
|
||||
// Keeps the pre-existing default inside Traefik.
|
||||
defaultType = brotliName
|
||||
}
|
||||
|
||||
encodings, hasWeight := parseAcceptEncoding(acceptEncoding)
|
||||
|
||||
if hasWeight {
|
||||
if len(encodings) == 0 {
|
||||
return identityName
|
||||
}
|
||||
|
||||
encoding := encodings[0]
|
||||
|
||||
if encoding.Type == identityName && encoding.Weight != nil && *encoding.Weight == 0 {
|
||||
return notAcceptable
|
||||
}
|
||||
|
||||
if encoding.Type == wildcardName && encoding.Weight != nil && *encoding.Weight == 0 {
|
||||
return notAcceptable
|
||||
}
|
||||
|
||||
if encoding.Type == wildcardName {
|
||||
return defaultType
|
||||
}
|
||||
|
||||
return encoding.Type
|
||||
}
|
||||
|
||||
for _, dt := range []string{brotliName, gzipName} {
|
||||
if slices.ContainsFunc(encodings, func(e Encoding) bool { return e.Type == dt }) {
|
||||
return dt
|
||||
}
|
||||
}
|
||||
|
||||
if slices.ContainsFunc(encodings, func(e Encoding) bool { return e.Type == wildcardName }) {
|
||||
return defaultType
|
||||
}
|
||||
|
||||
return identityName
|
||||
}
|
||||
|
||||
func parseAcceptEncoding(acceptEncoding []string) ([]Encoding, bool) {
|
||||
var encodings []Encoding
|
||||
var hasWeight bool
|
||||
|
||||
for _, line := range acceptEncoding {
|
||||
for _, item := range strings.Split(strings.ReplaceAll(line, " ", ""), ",") {
|
||||
parsed := strings.SplitN(item, ";", 2)
|
||||
if len(parsed) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
switch parsed[0] {
|
||||
case brotliName, gzipName, identityName, wildcardName:
|
||||
// supported encoding
|
||||
default:
|
||||
continue
|
||||
}
|
||||
|
||||
var weight *float64
|
||||
if len(parsed) > 1 && strings.HasPrefix(parsed[1], "q=") {
|
||||
w, _ := strconv.ParseFloat(strings.TrimPrefix(parsed[1], "q="), 64)
|
||||
|
||||
weight = &w
|
||||
hasWeight = true
|
||||
}
|
||||
|
||||
encodings = append(encodings, Encoding{
|
||||
Type: parsed[0],
|
||||
Weight: weight,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
slices.SortFunc(encodings, compareEncoding)
|
||||
|
||||
return encodings, hasWeight
|
||||
}
|
||||
|
||||
func compareEncoding(a, b Encoding) int {
|
||||
lhs, rhs := a.Weight, b.Weight
|
||||
|
||||
if lhs == nil && rhs == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
if lhs == nil && *rhs == 0 {
|
||||
return -1
|
||||
}
|
||||
|
||||
if lhs == nil {
|
||||
return 1
|
||||
}
|
||||
|
||||
if rhs == nil && *lhs == 0 {
|
||||
return 1
|
||||
}
|
||||
|
||||
if rhs == nil {
|
||||
return -1
|
||||
}
|
||||
|
||||
if *lhs < *rhs {
|
||||
return 1
|
||||
}
|
||||
|
||||
if *lhs > *rhs {
|
||||
return -1
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
143
pkg/middlewares/compress/acceptencoding_test.go
Normal file
143
pkg/middlewares/compress/acceptencoding_test.go
Normal file
|
@ -0,0 +1,143 @@
|
|||
package compress
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_getCompressionType(t *testing.T) {
|
||||
testCases := []struct {
|
||||
desc string
|
||||
values []string
|
||||
defaultType string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
desc: "br > gzip (no weight)",
|
||||
values: []string{"gzip, br"},
|
||||
expected: brotliName,
|
||||
},
|
||||
{
|
||||
desc: "known compression type (no weight)",
|
||||
values: []string{"compress, gzip"},
|
||||
expected: gzipName,
|
||||
},
|
||||
{
|
||||
desc: "unknown compression type (no weight), no encoding",
|
||||
values: []string{"compress, rar"},
|
||||
expected: identityName,
|
||||
},
|
||||
{
|
||||
desc: "wildcard return the default compression type",
|
||||
values: []string{"*"},
|
||||
expected: brotliName,
|
||||
},
|
||||
{
|
||||
desc: "wildcard return the custom default compression type",
|
||||
values: []string{"*"},
|
||||
defaultType: "foo",
|
||||
expected: "foo",
|
||||
},
|
||||
{
|
||||
desc: "follows weight",
|
||||
values: []string{"br;q=0.8, gzip;q=1.0, *;q=0.1"},
|
||||
expected: gzipName,
|
||||
},
|
||||
{
|
||||
desc: "ignore unknown compression type",
|
||||
values: []string{"compress;q=1.0, gzip;q=0.5"},
|
||||
expected: gzipName,
|
||||
},
|
||||
{
|
||||
desc: "not acceptable (identity)",
|
||||
values: []string{"compress;q=1.0, identity;q=0"},
|
||||
expected: notAcceptable,
|
||||
},
|
||||
{
|
||||
desc: "not acceptable (wildcard)",
|
||||
values: []string{"compress;q=1.0, *;q=0"},
|
||||
expected: notAcceptable,
|
||||
},
|
||||
{
|
||||
desc: "non-zero is higher than 0",
|
||||
values: []string{"gzip, *;q=0"},
|
||||
expected: gzipName,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
encodingType := getCompressionType(test.values, test.defaultType)
|
||||
|
||||
assert.Equal(t, test.expected, encodingType)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_parseAcceptEncoding(t *testing.T) {
|
||||
testCases := []struct {
|
||||
desc string
|
||||
values []string
|
||||
expected []Encoding
|
||||
assertWeight assert.BoolAssertionFunc
|
||||
}{
|
||||
{
|
||||
desc: "weight",
|
||||
values: []string{"br;q=1.0, gzip;q=0.8, *;q=0.1"},
|
||||
expected: []Encoding{
|
||||
{Type: brotliName, Weight: ptr[float64](1)},
|
||||
{Type: gzipName, Weight: ptr(0.8)},
|
||||
{Type: wildcardName, Weight: ptr(0.1)},
|
||||
},
|
||||
assertWeight: assert.True,
|
||||
},
|
||||
{
|
||||
desc: "mixed",
|
||||
values: []string{"gzip, br;q=1.0, *;q=0"},
|
||||
expected: []Encoding{
|
||||
{Type: brotliName, Weight: ptr[float64](1)},
|
||||
{Type: gzipName},
|
||||
{Type: wildcardName, Weight: ptr[float64](0)},
|
||||
},
|
||||
assertWeight: assert.True,
|
||||
},
|
||||
{
|
||||
desc: "no weight",
|
||||
values: []string{"gzip, br, *"},
|
||||
expected: []Encoding{
|
||||
{Type: gzipName},
|
||||
{Type: brotliName},
|
||||
{Type: wildcardName},
|
||||
},
|
||||
assertWeight: assert.False,
|
||||
},
|
||||
{
|
||||
desc: "weight and identity",
|
||||
values: []string{"gzip;q=1.0, identity; q=0.5, *;q=0"},
|
||||
expected: []Encoding{
|
||||
{Type: gzipName, Weight: ptr[float64](1)},
|
||||
{Type: identityName, Weight: ptr(0.5)},
|
||||
{Type: wildcardName, Weight: ptr[float64](0)},
|
||||
},
|
||||
assertWeight: assert.True,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
aes, hasWeight := parseAcceptEncoding(test.values)
|
||||
|
||||
assert.Equal(t, test.expected, aes)
|
||||
test.assertWeight(t, hasWeight)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func ptr[T any](t T) *T {
|
||||
return &t
|
||||
}
|
|
@ -160,6 +160,7 @@ func (r *responseWriter) Write(p []byte) (int, error) {
|
|||
}
|
||||
if !found {
|
||||
r.compressionDisabled = true
|
||||
r.rw.WriteHeader(r.statusCode)
|
||||
return r.rw.Write(p)
|
||||
}
|
||||
}
|
||||
|
@ -167,6 +168,7 @@ func (r *responseWriter) Write(p []byte) (int, error) {
|
|||
for _, excludedContentType := range r.excludedContentTypes {
|
||||
if excludedContentType.equals(mediaType, params) {
|
||||
r.compressionDisabled = true
|
||||
r.rw.WriteHeader(r.statusCode)
|
||||
return r.rw.Write(p)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -121,7 +121,7 @@ func Test_MinSize(t *testing.T) {
|
|||
var bodySize int
|
||||
h := mustNewWrapper(t, cfg)(http.HandlerFunc(
|
||||
func(rw http.ResponseWriter, req *http.Request) {
|
||||
for i := 0; i < bodySize; i++ {
|
||||
for range bodySize {
|
||||
// We make sure to Write at least once less than minSize so that both
|
||||
// cases below go through the same algo: i.e. they start buffering
|
||||
// because they haven't reached minSize.
|
||||
|
@ -356,7 +356,7 @@ func Test_ExcludedContentTypes(t *testing.T) {
|
|||
h := mustNewWrapper(t, cfg)(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||||
rw.Header().Set(contentType, test.contentType)
|
||||
|
||||
rw.WriteHeader(http.StatusOK)
|
||||
rw.WriteHeader(http.StatusAccepted)
|
||||
|
||||
_, err := rw.Write(bigTestBody)
|
||||
require.NoError(t, err)
|
||||
|
@ -368,7 +368,7 @@ func Test_ExcludedContentTypes(t *testing.T) {
|
|||
rw := httptest.NewRecorder()
|
||||
h.ServeHTTP(rw, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, rw.Code)
|
||||
assert.Equal(t, http.StatusAccepted, rw.Code)
|
||||
|
||||
if test.expCompression {
|
||||
assert.Equal(t, "br", rw.Header().Get(contentEncoding))
|
||||
|
@ -460,7 +460,7 @@ func Test_IncludedContentTypes(t *testing.T) {
|
|||
h := mustNewWrapper(t, cfg)(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||||
rw.Header().Set(contentType, test.contentType)
|
||||
|
||||
rw.WriteHeader(http.StatusOK)
|
||||
rw.WriteHeader(http.StatusAccepted)
|
||||
|
||||
_, err := rw.Write(bigTestBody)
|
||||
require.NoError(t, err)
|
||||
|
@ -472,7 +472,7 @@ func Test_IncludedContentTypes(t *testing.T) {
|
|||
rw := httptest.NewRecorder()
|
||||
h.ServeHTTP(rw, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, rw.Code)
|
||||
assert.Equal(t, http.StatusAccepted, rw.Code)
|
||||
|
||||
if test.expCompression {
|
||||
assert.Equal(t, "br", rw.Header().Get(contentEncoding))
|
||||
|
|
|
@ -7,7 +7,6 @@ import (
|
|||
"mime"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/klauspost/compress/gzhttp"
|
||||
"github.com/traefik/traefik/v3/pkg/config/dynamic"
|
||||
|
@ -24,11 +23,12 @@ const DefaultMinSize = 1024
|
|||
|
||||
// Compress is a middleware that allows to compress the response.
|
||||
type compress struct {
|
||||
next http.Handler
|
||||
name string
|
||||
excludes []string
|
||||
includes []string
|
||||
minSize int
|
||||
next http.Handler
|
||||
name string
|
||||
excludes []string
|
||||
includes []string
|
||||
minSize int
|
||||
defaultEncoding string
|
||||
|
||||
brotliHandler http.Handler
|
||||
gzipHandler http.Handler
|
||||
|
@ -68,11 +68,12 @@ func New(ctx context.Context, next http.Handler, conf dynamic.Compress, name str
|
|||
}
|
||||
|
||||
c := &compress{
|
||||
next: next,
|
||||
name: name,
|
||||
excludes: excludes,
|
||||
includes: includes,
|
||||
minSize: minSize,
|
||||
next: next,
|
||||
name: name,
|
||||
excludes: excludes,
|
||||
includes: includes,
|
||||
minSize: minSize,
|
||||
defaultEncoding: conf.DefaultEncoding,
|
||||
}
|
||||
|
||||
var err error
|
||||
|
@ -109,25 +110,33 @@ func (c *compress) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
// Client doesn't specify a preferred encoding, for compatibility don't encode the request
|
||||
// See https://github.com/traefik/traefik/issues/9734
|
||||
acceptEncoding, ok := req.Header["Accept-Encoding"]
|
||||
acceptEncoding, ok := req.Header[acceptEncodingHeader]
|
||||
if !ok {
|
||||
if c.defaultEncoding != "" {
|
||||
// RFC says: "If no Accept-Encoding header field is in the request, any content coding is considered acceptable by the user agent."
|
||||
// https://www.rfc-editor.org/rfc/rfc9110#field.accept-encoding
|
||||
c.chooseHandler(c.defaultEncoding, rw, req)
|
||||
return
|
||||
}
|
||||
|
||||
// Client doesn't specify a preferred encoding, for compatibility don't encode the request
|
||||
// See https://github.com/traefik/traefik/issues/9734
|
||||
c.next.ServeHTTP(rw, req)
|
||||
return
|
||||
}
|
||||
|
||||
if encodingAccepts(acceptEncoding, "br") {
|
||||
c.chooseHandler(getCompressionType(acceptEncoding, c.defaultEncoding), rw, req)
|
||||
}
|
||||
|
||||
func (c *compress) chooseHandler(typ string, rw http.ResponseWriter, req *http.Request) {
|
||||
switch typ {
|
||||
case brotliName:
|
||||
c.brotliHandler.ServeHTTP(rw, req)
|
||||
return
|
||||
}
|
||||
|
||||
if encodingAccepts(acceptEncoding, "gzip") {
|
||||
case gzipName:
|
||||
c.gzipHandler.ServeHTTP(rw, req)
|
||||
return
|
||||
default:
|
||||
c.next.ServeHTTP(rw, req)
|
||||
}
|
||||
|
||||
c.next.ServeHTTP(rw, req)
|
||||
}
|
||||
|
||||
func (c *compress) GetTracingInformation() (string, string, trace.SpanKind) {
|
||||
|
@ -172,19 +181,3 @@ func (c *compress) newBrotliHandler() (http.Handler, error) {
|
|||
|
||||
return wrapper(c.next), nil
|
||||
}
|
||||
|
||||
func encodingAccepts(acceptEncoding []string, typ string) bool {
|
||||
for _, ae := range acceptEncoding {
|
||||
for _, e := range strings.Split(ae, ",") {
|
||||
parsed := strings.Split(strings.TrimSpace(e), ";")
|
||||
if len(parsed) == 0 {
|
||||
continue
|
||||
}
|
||||
if parsed[0] == typ || parsed[0] == "*" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
|
|
@ -18,12 +18,9 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
acceptEncodingHeader = "Accept-Encoding"
|
||||
contentEncodingHeader = "Content-Encoding"
|
||||
contentTypeHeader = "Content-Type"
|
||||
varyHeader = "Vary"
|
||||
gzipValue = "gzip"
|
||||
brotliValue = "br"
|
||||
)
|
||||
|
||||
func TestNegotiation(t *testing.T) {
|
||||
|
@ -62,9 +59,9 @@ func TestNegotiation(t *testing.T) {
|
|||
expEncoding: "br",
|
||||
},
|
||||
{
|
||||
desc: "multi accept header, prefer br",
|
||||
desc: "multi accept header, prefer gzip",
|
||||
acceptEncHeader: "gzip;q=1.0, br;q=0.8",
|
||||
expEncoding: "br",
|
||||
expEncoding: "gzip",
|
||||
},
|
||||
{
|
||||
desc: "multi accept header list, prefer br",
|
||||
|
@ -98,7 +95,7 @@ func TestNegotiation(t *testing.T) {
|
|||
|
||||
func TestShouldCompressWhenNoContentEncodingHeader(t *testing.T) {
|
||||
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req.Header.Add(acceptEncodingHeader, gzipValue)
|
||||
req.Header.Add(acceptEncodingHeader, gzipName)
|
||||
|
||||
baseBody := generateBytes(gzhttp.DefaultMinSize)
|
||||
|
||||
|
@ -112,7 +109,7 @@ func TestShouldCompressWhenNoContentEncodingHeader(t *testing.T) {
|
|||
rw := httptest.NewRecorder()
|
||||
handler.ServeHTTP(rw, req)
|
||||
|
||||
assert.Equal(t, gzipValue, rw.Header().Get(contentEncodingHeader))
|
||||
assert.Equal(t, gzipName, rw.Header().Get(contentEncodingHeader))
|
||||
assert.Equal(t, acceptEncodingHeader, rw.Header().Get(varyHeader))
|
||||
|
||||
gr, err := gzip.NewReader(rw.Body)
|
||||
|
@ -125,11 +122,11 @@ func TestShouldCompressWhenNoContentEncodingHeader(t *testing.T) {
|
|||
|
||||
func TestShouldNotCompressWhenContentEncodingHeader(t *testing.T) {
|
||||
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req.Header.Add(acceptEncodingHeader, gzipValue)
|
||||
req.Header.Add(acceptEncodingHeader, gzipName)
|
||||
|
||||
fakeCompressedBody := generateBytes(gzhttp.DefaultMinSize)
|
||||
next := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
rw.Header().Add(contentEncodingHeader, gzipValue)
|
||||
rw.Header().Add(contentEncodingHeader, gzipName)
|
||||
rw.Header().Add(varyHeader, acceptEncodingHeader)
|
||||
_, err := rw.Write(fakeCompressedBody)
|
||||
if err != nil {
|
||||
|
@ -142,7 +139,7 @@ func TestShouldNotCompressWhenContentEncodingHeader(t *testing.T) {
|
|||
rw := httptest.NewRecorder()
|
||||
handler.ServeHTTP(rw, req)
|
||||
|
||||
assert.Equal(t, gzipValue, rw.Header().Get(contentEncodingHeader))
|
||||
assert.Equal(t, gzipName, rw.Header().Get(contentEncodingHeader))
|
||||
assert.Equal(t, acceptEncodingHeader, rw.Header().Get(varyHeader))
|
||||
|
||||
assert.EqualValues(t, rw.Body.Bytes(), fakeCompressedBody)
|
||||
|
@ -225,7 +222,7 @@ func TestShouldNotCompressWhenEmptyAcceptEncodingHeader(t *testing.T) {
|
|||
|
||||
func TestShouldNotCompressHeadRequest(t *testing.T) {
|
||||
req := testhelpers.MustNewRequest(http.MethodHead, "http://localhost", nil)
|
||||
req.Header.Add(acceptEncodingHeader, gzipValue)
|
||||
req.Header.Add(acceptEncodingHeader, gzipName)
|
||||
|
||||
fakeBody := generateBytes(gzhttp.DefaultMinSize)
|
||||
next := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
|
@ -301,7 +298,7 @@ func TestShouldNotCompressWhenSpecificContentType(t *testing.T) {
|
|||
t.Parallel()
|
||||
|
||||
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req.Header.Add(acceptEncodingHeader, gzipValue)
|
||||
req.Header.Add(acceptEncodingHeader, gzipName)
|
||||
if test.reqContentType != "" {
|
||||
req.Header.Add(contentTypeHeader, test.reqContentType)
|
||||
}
|
||||
|
@ -352,7 +349,7 @@ func TestShouldCompressWhenSpecificContentType(t *testing.T) {
|
|||
t.Parallel()
|
||||
|
||||
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req.Header.Add(acceptEncodingHeader, gzipValue)
|
||||
req.Header.Add(acceptEncodingHeader, gzipName)
|
||||
|
||||
next := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
rw.Header().Set(contentTypeHeader, test.respContentType)
|
||||
|
@ -368,7 +365,7 @@ func TestShouldCompressWhenSpecificContentType(t *testing.T) {
|
|||
rw := httptest.NewRecorder()
|
||||
handler.ServeHTTP(rw, req)
|
||||
|
||||
assert.Equal(t, gzipValue, rw.Header().Get(contentEncodingHeader))
|
||||
assert.Equal(t, gzipName, rw.Header().Get(contentEncodingHeader))
|
||||
assert.Equal(t, acceptEncodingHeader, rw.Header().Get(varyHeader))
|
||||
assert.NotEqualValues(t, rw.Body.Bytes(), baseBody)
|
||||
})
|
||||
|
@ -386,7 +383,7 @@ func TestIntegrationShouldNotCompress(t *testing.T) {
|
|||
{
|
||||
name: "when content already compressed",
|
||||
handler: http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
rw.Header().Add(contentEncodingHeader, gzipValue)
|
||||
rw.Header().Add(contentEncodingHeader, gzipName)
|
||||
rw.Header().Add(varyHeader, acceptEncodingHeader)
|
||||
_, err := rw.Write(fakeCompressedBody)
|
||||
if err != nil {
|
||||
|
@ -398,7 +395,7 @@ func TestIntegrationShouldNotCompress(t *testing.T) {
|
|||
{
|
||||
name: "when content already compressed and status code Created",
|
||||
handler: http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
rw.Header().Add(contentEncodingHeader, gzipValue)
|
||||
rw.Header().Add(contentEncodingHeader, gzipName)
|
||||
rw.Header().Add(varyHeader, acceptEncodingHeader)
|
||||
rw.WriteHeader(http.StatusCreated)
|
||||
_, err := rw.Write(fakeCompressedBody)
|
||||
|
@ -419,14 +416,14 @@ func TestIntegrationShouldNotCompress(t *testing.T) {
|
|||
defer ts.Close()
|
||||
|
||||
req := testhelpers.MustNewRequest(http.MethodGet, ts.URL, nil)
|
||||
req.Header.Add(acceptEncodingHeader, gzipValue)
|
||||
req.Header.Add(acceptEncodingHeader, gzipName)
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, test.expectedStatusCode, resp.StatusCode)
|
||||
|
||||
assert.Equal(t, gzipValue, resp.Header.Get(contentEncodingHeader))
|
||||
assert.Equal(t, gzipName, resp.Header.Get(contentEncodingHeader))
|
||||
assert.Equal(t, acceptEncodingHeader, resp.Header.Get(varyHeader))
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
|
@ -438,7 +435,7 @@ func TestIntegrationShouldNotCompress(t *testing.T) {
|
|||
|
||||
func TestShouldWriteHeaderWhenFlush(t *testing.T) {
|
||||
next := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
rw.Header().Add(contentEncodingHeader, gzipValue)
|
||||
rw.Header().Add(contentEncodingHeader, gzipName)
|
||||
rw.Header().Add(varyHeader, acceptEncodingHeader)
|
||||
rw.WriteHeader(http.StatusUnauthorized)
|
||||
rw.(http.Flusher).Flush()
|
||||
|
@ -454,14 +451,14 @@ func TestShouldWriteHeaderWhenFlush(t *testing.T) {
|
|||
defer ts.Close()
|
||||
|
||||
req := testhelpers.MustNewRequest(http.MethodGet, ts.URL, nil)
|
||||
req.Header.Add(acceptEncodingHeader, gzipValue)
|
||||
req.Header.Add(acceptEncodingHeader, gzipName)
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
|
||||
|
||||
assert.Equal(t, gzipValue, resp.Header.Get(contentEncodingHeader))
|
||||
assert.Equal(t, gzipName, resp.Header.Get(contentEncodingHeader))
|
||||
assert.Equal(t, acceptEncodingHeader, resp.Header.Get(varyHeader))
|
||||
}
|
||||
|
||||
|
@ -505,14 +502,14 @@ func TestIntegrationShouldCompress(t *testing.T) {
|
|||
defer ts.Close()
|
||||
|
||||
req := testhelpers.MustNewRequest(http.MethodGet, ts.URL, nil)
|
||||
req.Header.Add(acceptEncodingHeader, gzipValue)
|
||||
req.Header.Add(acceptEncodingHeader, gzipName)
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, test.expectedStatusCode, resp.StatusCode)
|
||||
|
||||
assert.Equal(t, gzipValue, resp.Header.Get(contentEncodingHeader))
|
||||
assert.Equal(t, gzipName, resp.Header.Get(contentEncodingHeader))
|
||||
assert.Equal(t, acceptEncodingHeader, resp.Header.Get(varyHeader))
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
|
@ -547,7 +544,7 @@ func TestMinResponseBodyBytes(t *testing.T) {
|
|||
t.Parallel()
|
||||
|
||||
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req.Header.Add(acceptEncodingHeader, gzipValue)
|
||||
req.Header.Add(acceptEncodingHeader, gzipName)
|
||||
|
||||
next := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
if _, err := rw.Write(fakeBody); err != nil {
|
||||
|
@ -562,7 +559,7 @@ func TestMinResponseBodyBytes(t *testing.T) {
|
|||
handler.ServeHTTP(rw, req)
|
||||
|
||||
if test.expectedCompression {
|
||||
assert.Equal(t, gzipValue, rw.Header().Get(contentEncodingHeader))
|
||||
assert.Equal(t, gzipName, rw.Header().Get(contentEncodingHeader))
|
||||
assert.NotEqualValues(t, rw.Body.Bytes(), fakeBody)
|
||||
return
|
||||
}
|
||||
|
@ -636,7 +633,7 @@ func Test1xxResponses(t *testing.T) {
|
|||
},
|
||||
}
|
||||
req, _ := http.NewRequestWithContext(httptrace.WithClientTrace(context.Background(), trace), http.MethodGet, server.URL, nil)
|
||||
req.Header.Add(acceptEncodingHeader, gzipValue)
|
||||
req.Header.Add(acceptEncodingHeader, gzipName)
|
||||
|
||||
res, err := frontendClient.Do(req)
|
||||
assert.NoError(t, err)
|
||||
|
@ -648,7 +645,7 @@ func Test1xxResponses(t *testing.T) {
|
|||
}
|
||||
checkLinkHeaders(t, []string{"</style.css>; rel=preload; as=style", "</script.js>; rel=preload; as=script", "</foo.js>; rel=preload; as=script"}, res.Header["Link"])
|
||||
|
||||
assert.Equal(t, gzipValue, res.Header.Get(contentEncodingHeader))
|
||||
assert.Equal(t, gzipName, res.Header.Get(contentEncodingHeader))
|
||||
body, _ := io.ReadAll(res.Body)
|
||||
assert.NotEqualValues(t, body, fakeBody)
|
||||
}
|
||||
|
@ -730,7 +727,7 @@ func runBenchmark(b *testing.B, req *http.Request, handler http.Handler) {
|
|||
b.Fatalf("Expected 200 but got %d", code)
|
||||
}
|
||||
|
||||
assert.Equal(b, gzipValue, res.Header().Get(contentEncodingHeader))
|
||||
assert.Equal(b, gzipName, res.Header().Get(contentEncodingHeader))
|
||||
}
|
||||
|
||||
func generateBytes(length int) []byte {
|
||||
|
|
|
@ -58,6 +58,7 @@ func (s *mockSpan) SetAttributes(kv ...attribute.KeyValue) {
|
|||
func (s *mockSpan) End(...trace.SpanEndOption) {}
|
||||
func (s *mockSpan) RecordError(_ error, _ ...trace.EventOption) {}
|
||||
func (s *mockSpan) AddEvent(_ string, _ ...trace.EventOption) {}
|
||||
func (s *mockSpan) AddLink(_ trace.Link) {}
|
||||
|
||||
func (s *mockSpan) SetName(name string) { s.name = name }
|
||||
|
||||
|
|
|
@ -264,7 +264,7 @@ func queryRegexp(tree *matchersTree, queries ...string) error {
|
|||
|
||||
// IsASCII checks if the given string contains only ASCII characters.
|
||||
func IsASCII(s string) bool {
|
||||
for i := 0; i < len(s); i++ {
|
||||
for i := range len(s) {
|
||||
if s[i] >= utf8.RuneSelf {
|
||||
return false
|
||||
}
|
||||
|
|
|
@ -124,7 +124,7 @@ func hostSNIRegexp(tree *matchersTree, templates ...string) error {
|
|||
|
||||
// isASCII checks if the given string contains only ASCII characters.
|
||||
func isASCII(s string) bool {
|
||||
for i := 0; i < len(s); i++ {
|
||||
for i := range len(s) {
|
||||
if s[i] >= utf8.RuneSelf {
|
||||
return false
|
||||
}
|
||||
|
|
|
@ -219,7 +219,7 @@ func varGroupName(idx int) string {
|
|||
func braceIndices(s string) ([]int, error) {
|
||||
var level, idx int
|
||||
var idxs []int
|
||||
for i := 0; i < len(s); i++ {
|
||||
for i := range len(s) {
|
||||
switch s[i] {
|
||||
case '{':
|
||||
if level++; level == 1 {
|
||||
|
|
|
@ -3878,7 +3878,7 @@ func TestFilterHealthStatuses(t *testing.T) {
|
|||
err := p.Init()
|
||||
require.NoError(t, err)
|
||||
|
||||
for i := 0; i < len(test.items); i++ {
|
||||
for i := range len(test.items) {
|
||||
var err error
|
||||
test.items[i].ExtraConf, err = p.getExtraConf(test.items[i].Labels)
|
||||
require.NoError(t, err)
|
||||
|
|
|
@ -235,7 +235,7 @@ func (p *Provider) loadConfigurationFromIngresses(ctx context.Context, client Cl
|
|||
certConfigs := make(map[string]*tls.CertAndStores)
|
||||
for _, ingress := range ingresses {
|
||||
logger := log.Ctx(ctx).With().Str("ingress", ingress.Name).Str("namespace", ingress.Namespace).Logger()
|
||||
ctx = logger.WithContext(ctx)
|
||||
ctxIngress := logger.WithContext(ctx)
|
||||
|
||||
if !p.shouldProcessIngress(ingress, ingressClasses) {
|
||||
continue
|
||||
|
@ -247,7 +247,7 @@ func (p *Provider) loadConfigurationFromIngresses(ctx context.Context, client Cl
|
|||
continue
|
||||
}
|
||||
|
||||
err = getCertificates(ctx, ingress, client, certConfigs)
|
||||
err = getCertificates(ctxIngress, ingress, client, certConfigs)
|
||||
if err != nil {
|
||||
logger.Error().Err(err).Msg("Error configuring TLS")
|
||||
}
|
||||
|
@ -289,7 +289,7 @@ func (p *Provider) loadConfigurationFromIngresses(ctx context.Context, client Cl
|
|||
rt.TLS = rtConfig.Router.TLS
|
||||
}
|
||||
|
||||
p.applyRouterTransform(ctx, rt, ingress)
|
||||
p.applyRouterTransform(ctxIngress, rt, ingress)
|
||||
|
||||
conf.HTTP.Routers["default-router"] = rt
|
||||
conf.HTTP.Services["default-backend"] = service
|
||||
|
@ -336,7 +336,7 @@ func (p *Provider) loadConfigurationFromIngresses(ctx context.Context, client Cl
|
|||
|
||||
rt := loadRouter(rule, pa, rtConfig, serviceName)
|
||||
|
||||
p.applyRouterTransform(ctx, rt, ingress)
|
||||
p.applyRouterTransform(ctxIngress, rt, ingress)
|
||||
|
||||
routerKey := strings.TrimPrefix(provider.Normalize(ingress.Namespace+"-"+ingress.Name+"-"+rule.Host+pa.Path), "-")
|
||||
|
||||
|
|
|
@ -345,7 +345,7 @@ func TestListenProvidersThrottleProviderConfigReload(t *testing.T) {
|
|||
// To load 5 new configs it would require 150ms (5 configs * 30ms).
|
||||
// In 100ms, we should only have time to load 3 configs.
|
||||
assert.LessOrEqual(t, publishedConfigCount, 3, "config was applied too many times")
|
||||
assert.Greater(t, publishedConfigCount, 0, "config was not applied at least once")
|
||||
assert.Positive(t, publishedConfigCount, "config was not applied at least once")
|
||||
}
|
||||
|
||||
func TestListenProvidersSkipsEmptyConfigs(t *testing.T) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue