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-03-20 00:47:20 +02:00
|
|
|
// GzipConfig defines config for gzip middleware.
|
2016-03-14 22:55:38 +02:00
|
|
|
GzipConfig struct {
|
2016-03-20 00:47:20 +02:00
|
|
|
// Level is the gzip level.
|
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-14 22:55:38 +02:00
|
|
|
Level: gzip.DefaultCompression,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
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-03-20 00:47:20 +02:00
|
|
|
return GzipFromConfig(DefaultGzipConfig)
|
2016-03-14 22:55:38 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// GzipFromConfig return gzip middleware from config.
|
|
|
|
// See `Gzip()`.
|
2016-03-14 22:55:38 +02:00
|
|
|
func GzipFromConfig(config GzipConfig) echo.MiddlewareFunc {
|
|
|
|
pool := gzipPool(config)
|
|
|
|
scheme := "gzip"
|
|
|
|
|
2016-02-19 08:50:40 +02:00
|
|
|
return func(next echo.Handler) echo.Handler {
|
2016-02-18 07:49:31 +02:00
|
|
|
return echo.HandlerFunc(func(c echo.Context) error {
|
|
|
|
c.Response().Header().Add(echo.Vary, echo.AcceptEncoding)
|
|
|
|
if strings.Contains(c.Request().Header().Get(echo.AcceptEncoding), scheme) {
|
2016-03-10 22:05:33 +02:00
|
|
|
w := pool.Get().(*gzip.Writer)
|
2016-03-11 02:35:20 +02:00
|
|
|
w.Reset(c.Response().Writer())
|
2016-02-18 07:49:31 +02:00
|
|
|
defer func() {
|
|
|
|
w.Close()
|
2016-03-10 22:05:33 +02:00
|
|
|
pool.Put(w)
|
2016-03-20 01:17:43 +02:00
|
|
|
w.Close()
|
2016-02-18 07:49:31 +02:00
|
|
|
}()
|
2016-03-11 02:35:20 +02:00
|
|
|
g := gzipResponseWriter{Response: c.Response(), Writer: w}
|
2016-02-18 07:49:31 +02:00
|
|
|
c.Response().Header().Set(echo.ContentEncoding, scheme)
|
2016-03-11 02:35:20 +02:00
|
|
|
c.Response().SetWriter(g)
|
2016-02-18 07:49:31 +02:00
|
|
|
}
|
2016-03-17 22:24:52 +02:00
|
|
|
return next.Handle(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) {
|
|
|
|
if g.Header().Get(echo.ContentType) == "" {
|
|
|
|
g.Header().Set(echo.ContentType, 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
|
|
|
}
|