2018-11-14 09:18:03 +00:00
|
|
|
package compress
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-02-08 13:15:45 +00:00
|
|
|
"errors"
|
2022-11-15 09:56:08 +00:00
|
|
|
"fmt"
|
2019-10-31 10:36:05 +00:00
|
|
|
"mime"
|
2018-11-14 09:18:03 +00:00
|
|
|
"net/http"
|
2023-08-16 15:50:06 +00:00
|
|
|
"slices"
|
2018-11-14 09:18:03 +00:00
|
|
|
|
2021-07-19 08:22:14 +00:00
|
|
|
"github.com/klauspost/compress/gzhttp"
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/config/dynamic"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/middlewares"
|
2024-01-08 08:10:06 +00:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2018-11-14 09:18:03 +00:00
|
|
|
)
|
|
|
|
|
2022-11-15 09:56:08 +00:00
|
|
|
const typeName = "Compress"
|
|
|
|
|
2024-08-07 14:20:04 +00:00
|
|
|
// defaultMinSize is the default minimum size (in bytes) required to enable compression.
|
2022-11-15 09:56:08 +00:00
|
|
|
// See https://github.com/klauspost/compress/blob/9559b037e79ad673c71f6ef7c732c00949014cd2/gzhttp/compress.go#L47.
|
2024-08-07 14:20:04 +00:00
|
|
|
const defaultMinSize = 1024
|
|
|
|
|
|
|
|
var defaultSupportedEncodings = []string{zstdName, brotliName, gzipName}
|
2018-11-14 09:18:03 +00:00
|
|
|
|
|
|
|
// Compress is a middleware that allows to compress the response.
|
|
|
|
type compress struct {
|
2024-06-06 14:42:04 +00:00
|
|
|
next http.Handler
|
|
|
|
name string
|
|
|
|
excludes []string
|
|
|
|
includes []string
|
|
|
|
minSize int
|
2024-08-07 14:20:04 +00:00
|
|
|
encodings []string
|
2024-06-06 14:42:04 +00:00
|
|
|
defaultEncoding string
|
2022-11-15 09:56:08 +00:00
|
|
|
|
|
|
|
brotliHandler http.Handler
|
|
|
|
gzipHandler http.Handler
|
2024-06-12 09:38:04 +00:00
|
|
|
zstdHandler http.Handler
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new compress middleware.
|
2019-10-31 10:36:05 +00:00
|
|
|
func New(ctx context.Context, next http.Handler, conf dynamic.Compress, name string) (http.Handler, error) {
|
2022-11-21 17:36:05 +00:00
|
|
|
middlewares.GetLogger(ctx, name, typeName).Debug().Msg("Creating middleware")
|
2018-11-14 09:18:03 +00:00
|
|
|
|
2024-01-17 10:32:06 +00:00
|
|
|
if len(conf.ExcludedContentTypes) > 0 && len(conf.IncludedContentTypes) > 0 {
|
2024-02-08 13:15:45 +00:00
|
|
|
return nil, errors.New("excludedContentTypes and includedContentTypes options are mutually exclusive")
|
2024-01-17 10:32:06 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 10:36:05 +00:00
|
|
|
excludes := []string{"application/grpc"}
|
|
|
|
for _, v := range conf.ExcludedContentTypes {
|
|
|
|
mediaType, _, err := mime.ParseMediaType(v)
|
|
|
|
if err != nil {
|
2024-01-17 10:32:06 +00:00
|
|
|
return nil, fmt.Errorf("parsing excluded media type: %w", err)
|
2019-10-31 10:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
excludes = append(excludes, mediaType)
|
|
|
|
}
|
|
|
|
|
2024-01-17 10:32:06 +00:00
|
|
|
var includes []string
|
|
|
|
for _, v := range conf.IncludedContentTypes {
|
|
|
|
mediaType, _, err := mime.ParseMediaType(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("parsing included media type: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
includes = append(includes, mediaType)
|
|
|
|
}
|
|
|
|
|
2024-08-07 14:20:04 +00:00
|
|
|
minSize := defaultMinSize
|
2021-09-20 16:00:08 +00:00
|
|
|
if conf.MinResponseBodyBytes > 0 {
|
|
|
|
minSize = conf.MinResponseBodyBytes
|
|
|
|
}
|
|
|
|
|
2024-08-07 14:20:04 +00:00
|
|
|
if len(conf.Encodings) == 0 {
|
|
|
|
return nil, errors.New("at least one encoding must be specified")
|
|
|
|
}
|
|
|
|
for _, encoding := range conf.Encodings {
|
|
|
|
if !slices.Contains(defaultSupportedEncodings, encoding) {
|
|
|
|
return nil, fmt.Errorf("unsupported encoding: %s", encoding)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if conf.DefaultEncoding != "" && !slices.Contains(conf.Encodings, conf.DefaultEncoding) {
|
|
|
|
return nil, fmt.Errorf("unsupported default encoding: %s", conf.DefaultEncoding)
|
|
|
|
}
|
|
|
|
|
2022-11-15 09:56:08 +00:00
|
|
|
c := &compress{
|
2024-06-06 14:42:04 +00:00
|
|
|
next: next,
|
|
|
|
name: name,
|
|
|
|
excludes: excludes,
|
|
|
|
includes: includes,
|
|
|
|
minSize: minSize,
|
2024-08-07 14:20:04 +00:00
|
|
|
encodings: conf.Encodings,
|
2024-06-06 14:42:04 +00:00
|
|
|
defaultEncoding: conf.DefaultEncoding,
|
2022-11-15 09:56:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2024-06-12 09:38:04 +00:00
|
|
|
|
|
|
|
c.zstdHandler, err = c.newCompressionHandler(zstdName, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.brotliHandler, err = c.newCompressionHandler(brotliName, name)
|
2022-11-15 09:56:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.gzipHandler, err = c.newGzipHandler()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *compress) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger := middlewares.GetLogger(req.Context(), c.name, typeName)
|
2022-11-15 09:56:08 +00:00
|
|
|
|
|
|
|
if req.Method == http.MethodHead {
|
|
|
|
c.next.ServeHTTP(rw, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-31 10:36:05 +00:00
|
|
|
mediaType, _, err := mime.ParseMediaType(req.Header.Get("Content-Type"))
|
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Debug().Err(err).Msg("Unable to parse MIME type")
|
2019-10-31 10:36:05 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 09:56:08 +00:00
|
|
|
// Notably for text/event-stream requests the response should not be compressed.
|
|
|
|
// See https://github.com/traefik/traefik/issues/2576
|
2023-08-16 15:50:06 +00:00
|
|
|
if slices.Contains(c.excludes, mediaType) {
|
2018-11-14 09:18:03 +00:00
|
|
|
c.next.ServeHTTP(rw, req)
|
2022-11-15 09:56:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-06 14:42:04 +00:00
|
|
|
acceptEncoding, ok := req.Header[acceptEncodingHeader]
|
2022-11-15 09:56:08 +00:00
|
|
|
if !ok {
|
2024-06-06 14:42:04 +00:00
|
|
|
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
|
2024-01-16 14:30:06 +00:00
|
|
|
c.next.ServeHTTP(rw, req)
|
2022-11-15 09:56:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-07 14:20:04 +00:00
|
|
|
c.chooseHandler(getCompressionEncoding(acceptEncoding, c.defaultEncoding, c.encodings), rw, req)
|
2024-06-06 14:42:04 +00:00
|
|
|
}
|
2022-11-15 09:56:08 +00:00
|
|
|
|
2024-06-06 14:42:04 +00:00
|
|
|
func (c *compress) chooseHandler(typ string, rw http.ResponseWriter, req *http.Request) {
|
|
|
|
switch typ {
|
2024-06-12 09:38:04 +00:00
|
|
|
case zstdName:
|
|
|
|
c.zstdHandler.ServeHTTP(rw, req)
|
2024-06-06 14:42:04 +00:00
|
|
|
case brotliName:
|
|
|
|
c.brotliHandler.ServeHTTP(rw, req)
|
|
|
|
case gzipName:
|
2022-11-15 09:56:08 +00:00
|
|
|
c.gzipHandler.ServeHTTP(rw, req)
|
2024-06-06 14:42:04 +00:00
|
|
|
default:
|
|
|
|
c.next.ServeHTTP(rw, req)
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-08 08:10:06 +00:00
|
|
|
func (c *compress) GetTracingInformation() (string, string, trace.SpanKind) {
|
|
|
|
return c.name, typeName, trace.SpanKindInternal
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 09:56:08 +00:00
|
|
|
func (c *compress) newGzipHandler() (http.Handler, error) {
|
2024-01-17 10:32:06 +00:00
|
|
|
var wrapper func(http.Handler) http.HandlerFunc
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if len(c.includes) > 0 {
|
|
|
|
wrapper, err = gzhttp.NewWrapper(
|
|
|
|
gzhttp.ContentTypes(c.includes),
|
|
|
|
gzhttp.MinSize(c.minSize),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
wrapper, err = gzhttp.NewWrapper(
|
|
|
|
gzhttp.ExceptContentTypes(c.excludes),
|
|
|
|
gzhttp.MinSize(c.minSize),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-11-15 09:56:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("new gzip wrapper: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return wrapper(c.next), nil
|
|
|
|
}
|
|
|
|
|
2024-06-12 09:38:04 +00:00
|
|
|
func (c *compress) newCompressionHandler(algo string, middlewareName string) (http.Handler, error) {
|
|
|
|
cfg := Config{MinSize: c.minSize, Algorithm: algo, MiddlewareName: middlewareName}
|
2024-01-17 10:32:06 +00:00
|
|
|
if len(c.includes) > 0 {
|
|
|
|
cfg.IncludedContentTypes = c.includes
|
|
|
|
} else {
|
|
|
|
cfg.ExcludedContentTypes = c.excludes
|
2022-11-15 09:56:08 +00:00
|
|
|
}
|
|
|
|
|
2024-06-12 09:38:04 +00:00
|
|
|
return NewCompressionHandler(cfg, c.next)
|
2022-11-15 09:56:08 +00:00
|
|
|
}
|