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"
|
2016-02-05 00:40:08 +02:00
|
|
|
"compress/gzip"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2016-09-23 07:53:44 +02:00
|
|
|
"net"
|
2016-02-05 00:40:08 +02:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
)
|
|
|
|
|
|
|
|
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.
|
2016-05-19 03:53:54 +02:00
|
|
|
Level int `json:"level"`
|
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
|
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{
|
2017-01-28 21:43:56 +02:00
|
|
|
Skipper: DefaultSkipper,
|
2016-07-27 18:34:44 +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-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
|
|
|
|
}
|
|
|
|
|
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) {
|
2017-02-15 00:11:10 +02:00
|
|
|
res.Header().Add(echo.HeaderContentEncoding, gzipScheme) // Issue #806
|
2016-12-22 04:56:35 +02:00
|
|
|
rw := res.Writer
|
2016-11-01 01:08:53 +02:00
|
|
|
w, err := gzip.NewWriterLevel(rw, config.Level)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-18 07:49:31 +02:00
|
|
|
defer func() {
|
2016-09-23 07:53:44 +02:00
|
|
|
if res.Size == 0 {
|
2017-02-14 20:10:04 +02:00
|
|
|
if res.Header().Get(echo.HeaderContentEncoding) == gzipScheme {
|
|
|
|
res.Header().Del(echo.HeaderContentEncoding)
|
|
|
|
}
|
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
|
2016-09-23 07:53:44 +02:00
|
|
|
w.Reset(ioutil.Discard)
|
2016-03-20 17:14:55 +02:00
|
|
|
}
|
2016-09-23 07:53:44 +02:00
|
|
|
w.Close()
|
2016-02-18 07:49:31 +02:00
|
|
|
}()
|
2016-11-01 01:08:53 +02:00
|
|
|
grw := &gzipResponseWriter{Writer: w, ResponseWriter: rw}
|
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-02-14 05:12:58 +02:00
|
|
|
if code == http.StatusNoContent { // Issue #489
|
|
|
|
w.ResponseWriter.Header().Del(echo.HeaderContentEncoding)
|
2016-12-21 09:00:40 +02:00
|
|
|
}
|
|
|
|
w.ResponseWriter.WriteHeader(code)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
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() {
|
|
|
|
w.Writer.(*gzip.Writer).Flush()
|
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) {
|
2016-09-23 07:53:44 +02:00
|
|
|
return w.ResponseWriter.(http.Hijacker).Hijack()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *gzipResponseWriter) CloseNotify() <-chan bool {
|
|
|
|
return w.ResponseWriter.(http.CloseNotifier).CloseNotify()
|
2016-02-05 00:40:08 +02:00
|
|
|
}
|