2015-05-15 21:29:14 +02:00
|
|
|
package middleware
|
|
|
|
|
2016-02-05 00:40:08 +02:00
|
|
|
import (
|
|
|
|
"compress/gzip"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
"github.com/labstack/echo/engine"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2016-04-01 01:30:19 +02:00
|
|
|
// GzipConfig defines the config for gzip middleware.
|
2016-03-14 22:55:38 +02:00
|
|
|
GzipConfig struct {
|
2016-05-10 20:52:04 +02:00
|
|
|
// Gzip compression level.
|
|
|
|
// Optional. Default value -1.
|
2016-03-14 22:55:38 +02:00
|
|
|
Level int
|
2016-02-18 07:01:47 +02:00
|
|
|
}
|
|
|
|
|
2016-03-10 22:05:33 +02:00
|
|
|
gzipResponseWriter struct {
|
2016-02-05 00:40:08 +02:00
|
|
|
engine.Response
|
2016-03-10 22:05:33 +02:00
|
|
|
io.Writer
|
2016-02-05 00:40:08 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-03-14 22:55:38 +02:00
|
|
|
var (
|
2016-03-20 00:47:20 +02:00
|
|
|
// DefaultGzipConfig is the default gzip middleware config.
|
|
|
|
DefaultGzipConfig = GzipConfig{
|
2016-03-24 05:56:28 +02:00
|
|
|
Level: -1,
|
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-04-08 06:20:50 +02:00
|
|
|
// GzipWithConfig return gzip middleware from 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
|
|
|
|
if config.Level == 0 {
|
|
|
|
config.Level = DefaultGzipConfig.Level
|
|
|
|
}
|
|
|
|
|
2016-03-14 22:55:38 +02:00
|
|
|
pool := gzipPool(config)
|
|
|
|
scheme := "gzip"
|
|
|
|
|
2016-04-02 23:19:39 +02:00
|
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
2016-04-24 19:21:23 +02:00
|
|
|
res := c.Response()
|
|
|
|
res.Header().Add(echo.HeaderVary, echo.HeaderAcceptEncoding)
|
2016-04-06 16:28:53 +02:00
|
|
|
if strings.Contains(c.Request().Header().Get(echo.HeaderAcceptEncoding), scheme) {
|
2016-04-24 19:21:23 +02:00
|
|
|
rw := res.Writer()
|
2016-03-20 17:14:55 +02:00
|
|
|
gw := pool.Get().(*gzip.Writer)
|
|
|
|
gw.Reset(rw)
|
2016-02-18 07:49:31 +02:00
|
|
|
defer func() {
|
2016-04-24 19:21:23 +02:00
|
|
|
if res.Size() == 0 {
|
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-04-24 19:21:23 +02:00
|
|
|
res.SetWriter(rw)
|
|
|
|
res.Header().Del(echo.HeaderContentEncoding)
|
2016-03-20 17:14:55 +02:00
|
|
|
gw.Reset(ioutil.Discard)
|
|
|
|
}
|
|
|
|
gw.Close()
|
|
|
|
pool.Put(gw)
|
2016-02-18 07:49:31 +02:00
|
|
|
}()
|
2016-04-24 19:21:23 +02:00
|
|
|
g := gzipResponseWriter{Response: res, Writer: gw}
|
|
|
|
res.Header().Set(echo.HeaderContentEncoding, scheme)
|
|
|
|
res.SetWriter(g)
|
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-03-10 22:05:33 +02:00
|
|
|
func (g gzipResponseWriter) Write(b []byte) (int, error) {
|
2016-04-06 16:28:53 +02:00
|
|
|
if g.Header().Get(echo.HeaderContentType) == "" {
|
|
|
|
g.Header().Set(echo.HeaderContentType, http.DetectContentType(b))
|
2016-02-05 00:40:08 +02:00
|
|
|
}
|
2016-03-10 22:05:33 +02:00
|
|
|
return g.Writer.Write(b)
|
2016-02-05 00:40:08 +02:00
|
|
|
}
|
|
|
|
|
2016-03-14 22:55:38 +02:00
|
|
|
func gzipPool(config GzipConfig) sync.Pool {
|
|
|
|
return sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
w, _ := gzip.NewWriterLevel(ioutil.Discard, config.Level)
|
|
|
|
return w
|
|
|
|
},
|
|
|
|
}
|
2016-02-05 00:40:08 +02:00
|
|
|
}
|