2018-11-14 09:18:03 +00:00
|
|
|
package compress
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
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"
|
2022-11-15 09:56:08 +00:00
|
|
|
"strings"
|
2018-11-14 09:18:03 +00:00
|
|
|
|
2021-07-19 08:22:14 +00:00
|
|
|
"github.com/klauspost/compress/gzhttp"
|
2018-11-14 09:18:03 +00:00
|
|
|
"github.com/opentracing/opentracing-go/ext"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/config/dynamic"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/log"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/middlewares"
|
2022-11-15 09:56:08 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/middlewares/compress/brotli"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/tracing"
|
2018-11-14 09:18:03 +00:00
|
|
|
)
|
|
|
|
|
2022-11-15 09:56:08 +00:00
|
|
|
const typeName = "Compress"
|
|
|
|
|
|
|
|
// DefaultMinSize is the default minimum size (in bytes) required to enable compression.
|
|
|
|
// See https://github.com/klauspost/compress/blob/9559b037e79ad673c71f6ef7c732c00949014cd2/gzhttp/compress.go#L47.
|
|
|
|
const DefaultMinSize = 1024
|
2018-11-14 09:18:03 +00:00
|
|
|
|
|
|
|
// Compress is a middleware that allows to compress the response.
|
|
|
|
type compress struct {
|
2019-10-31 10:36:05 +00:00
|
|
|
next http.Handler
|
|
|
|
name string
|
|
|
|
excludes []string
|
2021-09-20 16:00:08 +00:00
|
|
|
minSize int
|
2022-11-15 09:56:08 +00:00
|
|
|
|
|
|
|
brotliHandler http.Handler
|
|
|
|
gzipHandler 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) {
|
2019-09-13 17:28:04 +00:00
|
|
|
log.FromContext(middlewares.GetLoggerCtx(ctx, name, typeName)).Debug("Creating middleware")
|
2018-11-14 09:18:03 +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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
excludes = append(excludes, mediaType)
|
|
|
|
}
|
|
|
|
|
2022-11-15 09:56:08 +00:00
|
|
|
minSize := DefaultMinSize
|
2021-09-20 16:00:08 +00:00
|
|
|
if conf.MinResponseBodyBytes > 0 {
|
|
|
|
minSize = conf.MinResponseBodyBytes
|
|
|
|
}
|
|
|
|
|
2022-11-15 09:56:08 +00:00
|
|
|
c := &compress{
|
|
|
|
next: next,
|
|
|
|
name: name,
|
|
|
|
excludes: excludes,
|
|
|
|
minSize: minSize,
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
c.brotliHandler, err = c.newBrotliHandler()
|
|
|
|
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-15 09:56:08 +00:00
|
|
|
logger := log.FromContext(middlewares.GetLoggerCtx(req.Context(), c.name, typeName))
|
|
|
|
|
|
|
|
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-15 09:56:08 +00:00
|
|
|
logger.WithError(err).Debug("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
|
2019-10-31 10:36:05 +00:00
|
|
|
if 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client allows us to do whatever we want, so we br compress.
|
|
|
|
// See https://www.rfc-editor.org/rfc/rfc9110.html#section-12.5.3
|
|
|
|
acceptEncoding, ok := req.Header["Accept-Encoding"]
|
|
|
|
if !ok {
|
|
|
|
c.brotliHandler.ServeHTTP(rw, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if encodingAccepts(acceptEncoding, "br") {
|
|
|
|
c.brotliHandler.ServeHTTP(rw, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if encodingAccepts(acceptEncoding, "gzip") {
|
|
|
|
c.gzipHandler.ServeHTTP(rw, req)
|
|
|
|
return
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
2022-11-15 09:56:08 +00:00
|
|
|
|
|
|
|
c.next.ServeHTTP(rw, req)
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *compress) GetTracingInformation() (string, ext.SpanKindEnum) {
|
|
|
|
return c.name, tracing.SpanKindNoneEnum
|
|
|
|
}
|
|
|
|
|
2022-11-15 09:56:08 +00:00
|
|
|
func (c *compress) newGzipHandler() (http.Handler, error) {
|
2021-07-19 08:22:14 +00:00
|
|
|
wrapper, err := gzhttp.NewWrapper(
|
|
|
|
gzhttp.ExceptContentTypes(c.excludes),
|
2022-11-15 09:56:08 +00:00
|
|
|
gzhttp.MinSize(c.minSize),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("new gzip wrapper: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return wrapper(c.next), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *compress) newBrotliHandler() (http.Handler, error) {
|
|
|
|
cfg := brotli.Config{
|
|
|
|
ExcludedContentTypes: c.excludes,
|
|
|
|
MinSize: c.minSize,
|
|
|
|
}
|
|
|
|
|
|
|
|
wrapper, err := brotli.NewWrapper(cfg)
|
2018-11-14 09:18:03 +00:00
|
|
|
if err != nil {
|
2022-11-15 09:56:08 +00:00
|
|
|
return nil, fmt.Errorf("new brotli wrapper: %w", err)
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 09:56:08 +00:00
|
|
|
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
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
2019-10-31 10:36:05 +00:00
|
|
|
|
|
|
|
func contains(values []string, val string) bool {
|
|
|
|
for _, v := range values {
|
|
|
|
if v == val {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2022-11-15 09:56:08 +00:00
|
|
|
|
2019-10-31 10:36:05 +00:00
|
|
|
return false
|
|
|
|
}
|