Exemple #1
0
// Gzip returns a middleware which compresses HTTP response using gzip compression scheme.
func Gzip(c *neo.Ctx, next neo.Next) {
	scheme := "gzip"

	c.Res.Header().Set("Vary", "Accept-Encoding")
	// c.Res.Header().Set("Content-Encoding", "gzip")
	if strings.Contains(c.Res.Header().Get("Accept-Encoding"), scheme) {
		w := writerPool.Get().(*gzip.Writer)
		//replace the rep in ctx
		w.Reset(c.Res.Writer())
		defer func() {
			w.Close()
			writerPool.Put(w)
		}()
		gw := gzipWriter{Writer: w, ResponseWriter: c.Res.Writer()}
		c.Res.Header().Set("Content-Encoding", scheme)
		//need a set function for c.Res
		c.Response().SetWriter(gw)
	}
	next()
	return

}