pki/vendor/github.com/labstack/echo/v4/middleware/compress.go

145 lines
3.3 KiB
Go
Raw Normal View History

2020-11-25 20:36:07 +01:00
package middleware
import (
"bufio"
"compress/gzip"
"io"
"net"
"net/http"
"strings"
2021-04-07 10:58:21 +02:00
"sync"
2020-11-25 20:36:07 +01:00
"github.com/labstack/echo/v4"
)
type (
// GzipConfig defines the config for Gzip middleware.
GzipConfig struct {
// Skipper defines a function to skip middleware.
Skipper Skipper
// Gzip compression level.
// Optional. Default value -1.
Level int `yaml:"level"`
}
gzipResponseWriter struct {
io.Writer
http.ResponseWriter
2022-11-02 17:49:03 +01:00
wroteBody bool
2020-11-25 20:36:07 +01:00
}
)
const (
gzipScheme = "gzip"
)
var (
// DefaultGzipConfig is the default Gzip middleware config.
DefaultGzipConfig = GzipConfig{
Skipper: DefaultSkipper,
Level: -1,
}
)
// Gzip returns a middleware which compresses HTTP response using gzip compression
// scheme.
func Gzip() echo.MiddlewareFunc {
return GzipWithConfig(DefaultGzipConfig)
}
// GzipWithConfig return Gzip middleware with config.
// See: `Gzip()`.
func GzipWithConfig(config GzipConfig) echo.MiddlewareFunc {
// Defaults
if config.Skipper == nil {
config.Skipper = DefaultGzipConfig.Skipper
}
if config.Level == 0 {
config.Level = DefaultGzipConfig.Level
}
2021-04-07 10:58:21 +02:00
pool := gzipCompressPool(config)
2020-11-25 20:36:07 +01:00
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if config.Skipper(c) {
return next(c)
}
res := c.Response()
res.Header().Add(echo.HeaderVary, echo.HeaderAcceptEncoding)
if strings.Contains(c.Request().Header.Get(echo.HeaderAcceptEncoding), gzipScheme) {
res.Header().Set(echo.HeaderContentEncoding, gzipScheme) // Issue #806
2021-04-07 10:58:21 +02:00
i := pool.Get()
w, ok := i.(*gzip.Writer)
if !ok {
return echo.NewHTTPError(http.StatusInternalServerError, i.(error).Error())
2020-11-25 20:36:07 +01:00
}
2021-04-07 10:58:21 +02:00
rw := res.Writer
w.Reset(rw)
2022-11-02 17:49:03 +01:00
grw := &gzipResponseWriter{Writer: w, ResponseWriter: rw}
2020-11-25 20:36:07 +01:00
defer func() {
2022-11-02 17:49:03 +01:00
if !grw.wroteBody {
2020-11-25 20:36:07 +01:00
if res.Header().Get(echo.HeaderContentEncoding) == gzipScheme {
res.Header().Del(echo.HeaderContentEncoding)
}
// We have to reset response to it's pristine state when
// nothing is written to body or error is returned.
// See issue #424, #407.
res.Writer = rw
2023-03-17 13:20:20 +01:00
w.Reset(io.Discard)
2020-11-25 20:36:07 +01:00
}
w.Close()
2021-04-07 10:58:21 +02:00
pool.Put(w)
2020-11-25 20:36:07 +01:00
}()
res.Writer = grw
}
return next(c)
}
}
}
func (w *gzipResponseWriter) WriteHeader(code int) {
w.Header().Del(echo.HeaderContentLength) // Issue #444
w.ResponseWriter.WriteHeader(code)
}
func (w *gzipResponseWriter) Write(b []byte) (int, error) {
if w.Header().Get(echo.HeaderContentType) == "" {
w.Header().Set(echo.HeaderContentType, http.DetectContentType(b))
}
2022-11-02 17:49:03 +01:00
w.wroteBody = true
2020-11-25 20:36:07 +01:00
return w.Writer.Write(b)
}
func (w *gzipResponseWriter) Flush() {
w.Writer.(*gzip.Writer).Flush()
if flusher, ok := w.ResponseWriter.(http.Flusher); ok {
flusher.Flush()
}
}
func (w *gzipResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
return w.ResponseWriter.(http.Hijacker).Hijack()
}
func (w *gzipResponseWriter) Push(target string, opts *http.PushOptions) error {
if p, ok := w.ResponseWriter.(http.Pusher); ok {
return p.Push(target, opts)
}
return http.ErrNotSupported
}
2021-04-07 10:58:21 +02:00
func gzipCompressPool(config GzipConfig) sync.Pool {
return sync.Pool{
New: func() interface{} {
2023-03-17 13:20:20 +01:00
w, err := gzip.NewWriterLevel(io.Discard, config.Level)
2021-04-07 10:58:21 +02:00
if err != nil {
return err
}
return w
},
}
}