mirror of
https://github.com/labstack/echo.git
synced 2024-12-24 20:14:31 +02:00
50fb7e0050
Signed-off-by: Vishal Rana <vr@labstack.com>
62 lines
1.3 KiB
Go
62 lines
1.3 KiB
Go
package middleware
|
|
|
|
import (
|
|
"bufio"
|
|
"compress/gzip"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/labstack/echo"
|
|
)
|
|
|
|
type (
|
|
gzipWriter struct {
|
|
io.Writer
|
|
http.ResponseWriter
|
|
}
|
|
)
|
|
|
|
func (w gzipWriter) Write(b []byte) (int, error) {
|
|
if w.Header().Get(echo.ContentType) == "" {
|
|
w.Header().Set(echo.ContentType, http.DetectContentType(b))
|
|
}
|
|
return w.Writer.Write(b)
|
|
}
|
|
|
|
func (w gzipWriter) Flush() error {
|
|
return w.Writer.(*gzip.Writer).Flush()
|
|
}
|
|
|
|
func (w gzipWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
|
return w.ResponseWriter.(http.Hijacker).Hijack()
|
|
}
|
|
|
|
func (w *gzipWriter) CloseNotify() <-chan bool {
|
|
return w.ResponseWriter.(http.CloseNotifier).CloseNotify()
|
|
}
|
|
|
|
// Gzip returns a middleware which compresses HTTP response using gzip compression
|
|
// scheme.
|
|
func Gzip() echo.MiddlewareFunc {
|
|
scheme := "gzip"
|
|
|
|
return func(h echo.HandlerFunc) echo.HandlerFunc {
|
|
return func(c *echo.Context) error {
|
|
c.Response().Header().Add(echo.Vary, echo.AcceptEncoding)
|
|
if strings.Contains(c.Request().Header.Get(echo.AcceptEncoding), scheme) {
|
|
w := gzip.NewWriter(c.Response().Writer())
|
|
defer w.Close()
|
|
gw := gzipWriter{Writer: w, ResponseWriter: c.Response().Writer()}
|
|
c.Response().Header().Set(echo.ContentEncoding, scheme)
|
|
c.Response().SetWriter(gw)
|
|
}
|
|
if err := h(c); err != nil {
|
|
c.Error(err)
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
}
|