2018-11-14 09:18:03 +00:00
|
|
|
package compress
|
|
|
|
|
|
|
|
import (
|
|
|
|
"compress/gzip"
|
|
|
|
"context"
|
2019-10-31 10:36:05 +00:00
|
|
|
"mime"
|
2018-11-14 09:18:03 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/NYTimes/gziphandler"
|
|
|
|
"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"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/tracing"
|
2018-11-14 09:18:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
typeName = "Compress"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &compress{next: next, name: name, excludes: excludes}, nil
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *compress) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
2019-10-31 10:36:05 +00:00
|
|
|
mediaType, _, err := mime.ParseMediaType(req.Header.Get("Content-Type"))
|
|
|
|
if err != nil {
|
|
|
|
log.FromContext(middlewares.GetLoggerCtx(context.Background(), c.name, typeName)).Debug(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if contains(c.excludes, mediaType) {
|
2018-11-14 09:18:03 +00:00
|
|
|
c.next.ServeHTTP(rw, req)
|
|
|
|
} else {
|
2019-09-13 17:28:04 +00:00
|
|
|
ctx := middlewares.GetLoggerCtx(req.Context(), c.name, typeName)
|
|
|
|
gzipHandler(ctx, 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
|
|
|
|
}
|
|
|
|
|
2019-09-13 17:28:04 +00:00
|
|
|
func gzipHandler(ctx context.Context, h http.Handler) http.Handler {
|
2018-11-14 09:18:03 +00:00
|
|
|
wrapper, err := gziphandler.GzipHandlerWithOpts(
|
|
|
|
gziphandler.CompressionLevel(gzip.DefaultCompression),
|
|
|
|
gziphandler.MinSize(gziphandler.DefaultMinSize))
|
|
|
|
if err != nil {
|
2019-09-13 17:28:04 +00:00
|
|
|
log.FromContext(ctx).Error(err)
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return wrapper(h)
|
|
|
|
}
|
2019-10-31 10:36:05 +00:00
|
|
|
|
|
|
|
func contains(values []string, val string) bool {
|
|
|
|
for _, v := range values {
|
|
|
|
if v == val {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|