2015-05-15 21:29:14 +02:00
|
|
|
package middleware
|
|
|
|
|
2016-02-05 00:40:08 +02:00
|
|
|
import (
|
2016-09-23 07:53:44 +02:00
|
|
|
"bufio"
|
2023-05-31 07:53:33 +02:00
|
|
|
"bytes"
|
2016-02-05 00:40:08 +02:00
|
|
|
"compress/gzip"
|
|
|
|
"io"
|
2016-09-23 07:53:44 +02:00
|
|
|
"net"
|
2016-02-05 00:40:08 +02:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
2020-11-07 05:52:35 +02:00
|
|
|
"sync"
|
2016-02-05 00:40:08 +02:00
|
|
|
|
2019-01-30 12:56:56 +02:00
|
|
|
"github.com/labstack/echo/v4"
|
2016-02-05 00:40:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2016-07-27 18:34:44 +02:00
|
|
|
// GzipConfig defines the config for Gzip middleware.
|
2016-03-14 22:55:38 +02:00
|
|
|
GzipConfig struct {
|
2016-07-27 18:34:44 +02:00
|
|
|
// Skipper defines a function to skip middleware.
|
|
|
|
Skipper Skipper
|
|
|
|
|
2016-05-10 20:52:04 +02:00
|
|
|
// Gzip compression level.
|
|
|
|
// Optional. Default value -1.
|
2017-12-28 21:24:34 +02:00
|
|
|
Level int `yaml:"level"`
|
2023-05-31 07:53:33 +02:00
|
|
|
|
|
|
|
// Length threshold before gzip compression is applied.
|
|
|
|
// Optional. Default value 0.
|
|
|
|
//
|
|
|
|
// Most of the time you will not need to change the default. Compressing
|
|
|
|
// a short response might increase the transmitted data because of the
|
|
|
|
// gzip format overhead. Compressing the response will also consume CPU
|
|
|
|
// and time on the server and the client (for decompressing). Depending on
|
|
|
|
// your use case such a threshold might be useful.
|
|
|
|
//
|
|
|
|
// See also:
|
|
|
|
// https://webmasters.stackexchange.com/questions/31750/what-is-recommended-minimum-object-size-for-gzip-performance-benefits
|
|
|
|
MinLength int
|
2016-02-18 07:01:47 +02:00
|
|
|
}
|
|
|
|
|
2016-03-10 22:05:33 +02:00
|
|
|
gzipResponseWriter struct {
|
|
|
|
io.Writer
|
2016-09-23 07:53:44 +02:00
|
|
|
http.ResponseWriter
|
2023-05-31 07:53:33 +02:00
|
|
|
wroteHeader bool
|
|
|
|
wroteBody bool
|
|
|
|
minLength int
|
|
|
|
minLengthExceeded bool
|
|
|
|
buffer *bytes.Buffer
|
|
|
|
code int
|
2016-02-05 00:40:08 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-12-20 23:46:25 +02:00
|
|
|
const (
|
|
|
|
gzipScheme = "gzip"
|
|
|
|
)
|
|
|
|
|
2016-03-14 22:55:38 +02:00
|
|
|
var (
|
2016-07-27 18:34:44 +02:00
|
|
|
// DefaultGzipConfig is the default Gzip middleware config.
|
2016-03-20 00:47:20 +02:00
|
|
|
DefaultGzipConfig = GzipConfig{
|
2023-05-31 07:53:33 +02:00
|
|
|
Skipper: DefaultSkipper,
|
|
|
|
Level: -1,
|
|
|
|
MinLength: 0,
|
2016-03-14 22:55:38 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-02-18 07:01:47 +02:00
|
|
|
// Gzip returns a middleware which compresses HTTP response using gzip compression
|
|
|
|
// scheme.
|
2016-03-14 22:55:38 +02:00
|
|
|
func Gzip() echo.MiddlewareFunc {
|
2016-04-08 06:20:50 +02:00
|
|
|
return GzipWithConfig(DefaultGzipConfig)
|
2016-03-14 22:55:38 +02:00
|
|
|
}
|
|
|
|
|
2016-09-01 05:10:14 +02:00
|
|
|
// GzipWithConfig return Gzip middleware with config.
|
2016-05-13 02:45:00 +02:00
|
|
|
// See: `Gzip()`.
|
2016-04-08 06:20:50 +02:00
|
|
|
func GzipWithConfig(config GzipConfig) echo.MiddlewareFunc {
|
2016-04-01 01:30:19 +02:00
|
|
|
// Defaults
|
2016-07-27 18:34:44 +02:00
|
|
|
if config.Skipper == nil {
|
|
|
|
config.Skipper = DefaultGzipConfig.Skipper
|
|
|
|
}
|
2016-04-01 01:30:19 +02:00
|
|
|
if config.Level == 0 {
|
|
|
|
config.Level = DefaultGzipConfig.Level
|
|
|
|
}
|
2023-05-31 07:53:33 +02:00
|
|
|
if config.MinLength < 0 {
|
|
|
|
config.MinLength = DefaultGzipConfig.MinLength
|
|
|
|
}
|
2016-04-01 01:30:19 +02:00
|
|
|
|
2020-11-27 05:01:04 +02:00
|
|
|
pool := gzipCompressPool(config)
|
2023-05-31 07:53:33 +02:00
|
|
|
bpool := bufferPool()
|
2020-11-07 05:52:35 +02:00
|
|
|
|
2016-04-02 23:19:39 +02:00
|
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
2016-07-27 18:34:44 +02:00
|
|
|
if config.Skipper(c) {
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
|
2016-04-24 19:21:23 +02:00
|
|
|
res := c.Response()
|
|
|
|
res.Header().Add(echo.HeaderVary, echo.HeaderAcceptEncoding)
|
2016-12-20 23:46:25 +02:00
|
|
|
if strings.Contains(c.Request().Header.Get(echo.HeaderAcceptEncoding), gzipScheme) {
|
2020-11-07 05:52:35 +02:00
|
|
|
i := pool.Get()
|
|
|
|
w, ok := i.(*gzip.Writer)
|
|
|
|
if !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, i.(error).Error())
|
2016-11-01 01:08:53 +02:00
|
|
|
}
|
2020-11-07 05:52:35 +02:00
|
|
|
rw := res.Writer
|
|
|
|
w.Reset(rw)
|
2023-05-31 07:53:33 +02:00
|
|
|
|
|
|
|
buf := bpool.Get().(*bytes.Buffer)
|
|
|
|
buf.Reset()
|
|
|
|
|
|
|
|
grw := &gzipResponseWriter{Writer: w, ResponseWriter: rw, minLength: config.MinLength, buffer: buf}
|
2016-02-18 07:49:31 +02:00
|
|
|
defer func() {
|
2023-07-16 19:15:24 +02:00
|
|
|
// There are different reasons for cases when we have not yet written response to the client and now need to do so.
|
|
|
|
// a) handler response had only response code and no response body (ala 404 or redirects etc). Response code need to be written now.
|
|
|
|
// b) body is shorter than our minimum length threshold and being buffered currently and needs to be written
|
2021-12-15 10:15:13 +02:00
|
|
|
if !grw.wroteBody {
|
2017-02-14 20:10:04 +02:00
|
|
|
if res.Header().Get(echo.HeaderContentEncoding) == gzipScheme {
|
|
|
|
res.Header().Del(echo.HeaderContentEncoding)
|
|
|
|
}
|
2023-07-16 19:15:24 +02:00
|
|
|
if grw.wroteHeader {
|
|
|
|
rw.WriteHeader(grw.code)
|
|
|
|
}
|
2016-03-20 17:14:55 +02:00
|
|
|
// We have to reset response to it's pristine state when
|
|
|
|
// nothing is written to body or error is returned.
|
|
|
|
// See issue #424, #407.
|
2016-12-22 04:56:35 +02:00
|
|
|
res.Writer = rw
|
2022-11-21 14:29:43 +02:00
|
|
|
w.Reset(io.Discard)
|
2023-05-31 07:53:33 +02:00
|
|
|
} else if !grw.minLengthExceeded {
|
|
|
|
// Write uncompressed response
|
|
|
|
res.Writer = rw
|
|
|
|
if grw.wroteHeader {
|
|
|
|
grw.ResponseWriter.WriteHeader(grw.code)
|
|
|
|
}
|
|
|
|
grw.buffer.WriteTo(rw)
|
|
|
|
w.Reset(io.Discard)
|
2016-03-20 17:14:55 +02:00
|
|
|
}
|
2016-09-23 07:53:44 +02:00
|
|
|
w.Close()
|
2023-05-31 07:53:33 +02:00
|
|
|
bpool.Put(buf)
|
2020-11-07 05:52:35 +02:00
|
|
|
pool.Put(w)
|
2016-02-18 07:49:31 +02:00
|
|
|
}()
|
2016-12-22 04:56:35 +02:00
|
|
|
res.Writer = grw
|
2016-02-18 07:49:31 +02:00
|
|
|
}
|
2016-04-02 23:19:39 +02:00
|
|
|
return next(c)
|
|
|
|
}
|
2016-02-18 07:49:31 +02:00
|
|
|
}
|
2016-02-18 07:01:47 +02:00
|
|
|
}
|
|
|
|
|
2016-12-21 09:00:40 +02:00
|
|
|
func (w *gzipResponseWriter) WriteHeader(code int) {
|
2017-06-28 19:05:09 +02:00
|
|
|
w.Header().Del(echo.HeaderContentLength) // Issue #444
|
2023-05-31 07:53:33 +02:00
|
|
|
|
|
|
|
w.wroteHeader = true
|
|
|
|
|
|
|
|
// Delay writing of the header until we know if we'll actually compress the response
|
|
|
|
w.code = code
|
2016-12-21 09:00:40 +02:00
|
|
|
}
|
|
|
|
|
2016-11-01 01:08:53 +02:00
|
|
|
func (w *gzipResponseWriter) Write(b []byte) (int, error) {
|
2016-09-23 07:53:44 +02:00
|
|
|
if w.Header().Get(echo.HeaderContentType) == "" {
|
|
|
|
w.Header().Set(echo.HeaderContentType, http.DetectContentType(b))
|
2016-02-05 00:40:08 +02:00
|
|
|
}
|
2021-12-15 10:15:13 +02:00
|
|
|
w.wroteBody = true
|
2023-05-31 07:53:33 +02:00
|
|
|
|
|
|
|
if !w.minLengthExceeded {
|
|
|
|
n, err := w.buffer.Write(b)
|
|
|
|
|
|
|
|
if w.buffer.Len() >= w.minLength {
|
|
|
|
w.minLengthExceeded = true
|
|
|
|
|
|
|
|
// The minimum length is exceeded, add Content-Encoding header and write the header
|
|
|
|
w.Header().Set(echo.HeaderContentEncoding, gzipScheme) // Issue #806
|
|
|
|
if w.wroteHeader {
|
|
|
|
w.ResponseWriter.WriteHeader(w.code)
|
|
|
|
}
|
|
|
|
|
|
|
|
return w.Writer.Write(w.buffer.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
return w.Writer.Write(b)
|
|
|
|
}
|
|
|
|
|
2017-03-11 02:10:07 +02:00
|
|
|
func (w *gzipResponseWriter) Flush() {
|
2023-05-31 07:53:33 +02:00
|
|
|
if !w.minLengthExceeded {
|
|
|
|
// Enforce compression because we will not know how much more data will come
|
|
|
|
w.minLengthExceeded = true
|
|
|
|
w.Header().Set(echo.HeaderContentEncoding, gzipScheme) // Issue #806
|
|
|
|
if w.wroteHeader {
|
|
|
|
w.ResponseWriter.WriteHeader(w.code)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Writer.Write(w.buffer.Bytes())
|
|
|
|
}
|
|
|
|
|
2017-03-11 02:10:07 +02:00
|
|
|
w.Writer.(*gzip.Writer).Flush()
|
2024-03-09 10:50:47 +02:00
|
|
|
_ = responseControllerFlush(w.ResponseWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *gzipResponseWriter) Unwrap() http.ResponseWriter {
|
|
|
|
return w.ResponseWriter
|
2016-09-23 07:53:44 +02:00
|
|
|
}
|
|
|
|
|
2016-11-01 01:08:53 +02:00
|
|
|
func (w *gzipResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
2024-03-09 10:50:47 +02:00
|
|
|
return responseControllerHijack(w.ResponseWriter)
|
2016-09-23 07:53:44 +02:00
|
|
|
}
|
2020-08-04 02:58:08 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2020-11-07 05:52:35 +02:00
|
|
|
|
2020-11-27 05:01:04 +02:00
|
|
|
func gzipCompressPool(config GzipConfig) sync.Pool {
|
2020-11-07 05:52:35 +02:00
|
|
|
return sync.Pool{
|
|
|
|
New: func() interface{} {
|
2022-11-21 14:29:43 +02:00
|
|
|
w, err := gzip.NewWriterLevel(io.Discard, config.Level)
|
2020-11-07 05:52:35 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return w
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2023-05-31 07:53:33 +02:00
|
|
|
|
|
|
|
func bufferPool() sync.Pool {
|
|
|
|
return sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
b := &bytes.Buffer{}
|
|
|
|
return b
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|