Add support for Brotli
Co-authored-by: Mathieu Lonjaret <mathieu.lonjaret@gmail.com> Co-authored-by: Tom Moulard <tom.moulard@traefik.io> Co-authored-by: Romain <rtribotte@users.noreply.github.com> Co-authored-by: Kevin Pollet <pollet.kevin@gmail.com>
This commit is contained in:
parent
1a1cfd1adc
commit
67d9c8da0b
11 changed files with 1201 additions and 39 deletions
|
@ -1,22 +1,26 @@
|
|||
package compress
|
||||
|
||||
import (
|
||||
"compress/gzip"
|
||||
"context"
|
||||
"fmt"
|
||||
"mime"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/klauspost/compress/gzhttp"
|
||||
"github.com/opentracing/opentracing-go/ext"
|
||||
"github.com/traefik/traefik/v2/pkg/config/dynamic"
|
||||
"github.com/traefik/traefik/v2/pkg/log"
|
||||
"github.com/traefik/traefik/v2/pkg/middlewares"
|
||||
"github.com/traefik/traefik/v2/pkg/middlewares/compress/brotli"
|
||||
"github.com/traefik/traefik/v2/pkg/tracing"
|
||||
)
|
||||
|
||||
const (
|
||||
typeName = "Compress"
|
||||
)
|
||||
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
|
||||
|
||||
// Compress is a middleware that allows to compress the response.
|
||||
type compress struct {
|
||||
|
@ -24,6 +28,9 @@ type compress struct {
|
|||
name string
|
||||
excludes []string
|
||||
minSize int
|
||||
|
||||
brotliHandler http.Handler
|
||||
gzipHandler http.Handler
|
||||
}
|
||||
|
||||
// New creates a new compress middleware.
|
||||
|
@ -40,42 +47,117 @@ func New(ctx context.Context, next http.Handler, conf dynamic.Compress, name str
|
|||
excludes = append(excludes, mediaType)
|
||||
}
|
||||
|
||||
minSize := gzhttp.DefaultMinSize
|
||||
minSize := DefaultMinSize
|
||||
if conf.MinResponseBodyBytes > 0 {
|
||||
minSize = conf.MinResponseBodyBytes
|
||||
}
|
||||
|
||||
return &compress{next: next, name: name, excludes: excludes, minSize: minSize}, nil
|
||||
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
|
||||
}
|
||||
|
||||
func (c *compress) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
mediaType, _, err := mime.ParseMediaType(req.Header.Get("Content-Type"))
|
||||
if err != nil {
|
||||
log.FromContext(middlewares.GetLoggerCtx(context.Background(), c.name, typeName)).Debug(err)
|
||||
logger := log.FromContext(middlewares.GetLoggerCtx(req.Context(), c.name, typeName))
|
||||
|
||||
if req.Method == http.MethodHead {
|
||||
c.next.ServeHTTP(rw, req)
|
||||
return
|
||||
}
|
||||
|
||||
mediaType, _, err := mime.ParseMediaType(req.Header.Get("Content-Type"))
|
||||
if err != nil {
|
||||
logger.WithError(err).Debug("Unable to parse MIME type")
|
||||
}
|
||||
|
||||
// Notably for text/event-stream requests the response should not be compressed.
|
||||
// See https://github.com/traefik/traefik/issues/2576
|
||||
if contains(c.excludes, mediaType) {
|
||||
c.next.ServeHTTP(rw, req)
|
||||
} else {
|
||||
ctx := middlewares.GetLoggerCtx(req.Context(), c.name, typeName)
|
||||
c.gzipHandler(ctx).ServeHTTP(rw, req)
|
||||
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
|
||||
}
|
||||
|
||||
c.next.ServeHTTP(rw, req)
|
||||
}
|
||||
|
||||
func (c *compress) GetTracingInformation() (string, ext.SpanKindEnum) {
|
||||
return c.name, tracing.SpanKindNoneEnum
|
||||
}
|
||||
|
||||
func (c *compress) gzipHandler(ctx context.Context) http.Handler {
|
||||
func (c *compress) newGzipHandler() (http.Handler, error) {
|
||||
wrapper, err := gzhttp.NewWrapper(
|
||||
gzhttp.ExceptContentTypes(c.excludes),
|
||||
gzhttp.CompressionLevel(gzip.DefaultCompression),
|
||||
gzhttp.MinSize(c.minSize))
|
||||
gzhttp.MinSize(c.minSize),
|
||||
)
|
||||
if err != nil {
|
||||
log.FromContext(ctx).Error(err)
|
||||
return nil, fmt.Errorf("new gzip wrapper: %w", err)
|
||||
}
|
||||
|
||||
return wrapper(c.next)
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("new brotli wrapper: %w", err)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func contains(values []string, val string) bool {
|
||||
|
@ -84,5 +166,6 @@ func contains(values []string, val string) bool {
|
|||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue