2024-05-23 14:44:31 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// SPDX-FileCopyrightText: © 2015 LabStack LLC and Echo contributors
|
|
|
|
|
2021-03-20 14:40:23 -07:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"compress/gzip"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
2024-05-23 14:44:31 -07:00
|
|
|
// DecompressConfig defines the config for Decompress middleware.
|
|
|
|
type DecompressConfig struct {
|
|
|
|
// Skipper defines a function to skip middleware.
|
|
|
|
Skipper Skipper
|
2021-03-20 14:40:23 -07:00
|
|
|
|
2024-05-23 14:44:31 -07:00
|
|
|
// GzipDecompressPool defines an interface to provide the sync.Pool used to create/store Gzip readers
|
|
|
|
GzipDecompressPool Decompressor
|
|
|
|
}
|
2021-03-20 14:40:23 -07:00
|
|
|
|
2023-08-05 11:43:19 -07:00
|
|
|
// GZIPEncoding content-encoding header if set to "gzip", decompress body contents.
|
2021-03-20 14:40:23 -07:00
|
|
|
const GZIPEncoding string = "gzip"
|
|
|
|
|
|
|
|
// Decompressor is used to get the sync.Pool used by the middleware to get Gzip readers
|
|
|
|
type Decompressor interface {
|
|
|
|
gzipDecompressPool() sync.Pool
|
|
|
|
}
|
|
|
|
|
2024-05-23 14:44:31 -07:00
|
|
|
// DefaultDecompressConfig defines the config for decompress middleware
|
|
|
|
var DefaultDecompressConfig = DecompressConfig{
|
|
|
|
Skipper: DefaultSkipper,
|
|
|
|
GzipDecompressPool: &DefaultGzipDecompressPool{},
|
|
|
|
}
|
2021-03-20 14:40:23 -07:00
|
|
|
|
|
|
|
// DefaultGzipDecompressPool is the default implementation of Decompressor interface
|
|
|
|
type DefaultGzipDecompressPool struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DefaultGzipDecompressPool) gzipDecompressPool() sync.Pool {
|
2022-01-18 11:42:25 -08:00
|
|
|
return sync.Pool{New: func() interface{} { return new(gzip.Reader) }}
|
2021-03-20 14:40:23 -07:00
|
|
|
}
|
|
|
|
|
2023-08-05 11:43:19 -07:00
|
|
|
// Decompress decompresses request body based if content encoding type is set to "gzip" with default config
|
2021-03-20 14:40:23 -07:00
|
|
|
func Decompress() echo.MiddlewareFunc {
|
|
|
|
return DecompressWithConfig(DefaultDecompressConfig)
|
|
|
|
}
|
|
|
|
|
2023-08-05 11:43:19 -07:00
|
|
|
// DecompressWithConfig decompresses request body based if content encoding type is set to "gzip" with config
|
2021-03-20 14:40:23 -07:00
|
|
|
func DecompressWithConfig(config DecompressConfig) echo.MiddlewareFunc {
|
|
|
|
// Defaults
|
|
|
|
if config.Skipper == nil {
|
|
|
|
config.Skipper = DefaultGzipConfig.Skipper
|
|
|
|
}
|
|
|
|
if config.GzipDecompressPool == nil {
|
|
|
|
config.GzipDecompressPool = DefaultDecompressConfig.GzipDecompressPool
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
pool := config.GzipDecompressPool.gzipDecompressPool()
|
2022-01-18 11:42:25 -08:00
|
|
|
|
2021-03-20 14:40:23 -07:00
|
|
|
return func(c echo.Context) error {
|
|
|
|
if config.Skipper(c) {
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
|
2022-01-18 11:42:25 -08:00
|
|
|
if c.Request().Header.Get(echo.HeaderContentEncoding) != GZIPEncoding {
|
|
|
|
return next(c)
|
|
|
|
}
|
2021-03-20 14:40:23 -07:00
|
|
|
|
2022-01-18 11:42:25 -08:00
|
|
|
i := pool.Get()
|
|
|
|
gr, ok := i.(*gzip.Reader)
|
|
|
|
if !ok || gr == nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, i.(error).Error())
|
|
|
|
}
|
|
|
|
defer pool.Put(gr)
|
2021-03-20 14:40:23 -07:00
|
|
|
|
2022-01-18 11:42:25 -08:00
|
|
|
b := c.Request().Body
|
|
|
|
defer b.Close()
|
2021-03-20 14:40:23 -07:00
|
|
|
|
2022-01-18 11:42:25 -08:00
|
|
|
if err := gr.Reset(b); err != nil {
|
|
|
|
if err == io.EOF { //ignore if body is empty
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
return err
|
2021-03-20 14:40:23 -07:00
|
|
|
}
|
2022-01-18 11:42:25 -08:00
|
|
|
|
|
|
|
// only Close gzip reader if it was set to a proper gzip source otherwise it will panic on close.
|
|
|
|
defer gr.Close()
|
|
|
|
|
|
|
|
c.Request().Body = gr
|
|
|
|
|
2021-03-20 14:40:23 -07:00
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|