2015-01-30 14:52:18 +08:00
|
|
|
// Package gzip provides a simple middleware layer that performs
|
|
|
|
// gzip compression on the response.
|
2015-01-30 13:04:18 +08:00
|
|
|
package gzip
|
|
|
|
|
|
|
|
import (
|
|
|
|
"compress/gzip"
|
2015-04-09 13:24:59 +08:00
|
|
|
"fmt"
|
2015-01-30 13:04:18 +08:00
|
|
|
"io"
|
2015-12-10 01:44:25 +08:00
|
|
|
"io/ioutil"
|
2015-01-30 13:04:18 +08:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/mholt/caddy/middleware"
|
|
|
|
)
|
|
|
|
|
2015-04-09 13:24:59 +08:00
|
|
|
// Gzip is a middleware type which gzips HTTP responses. It is
|
|
|
|
// imperative that any handler which writes to a gzipped response
|
|
|
|
// specifies the Content-Type, otherwise some clients will assume
|
|
|
|
// application/x-gzip and try to download a file.
|
2015-03-21 08:11:54 +08:00
|
|
|
type Gzip struct {
|
2015-06-07 07:58:37 +08:00
|
|
|
Next middleware.Handler
|
|
|
|
Configs []Config
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config holds the configuration for Gzip middleware
|
|
|
|
type Config struct {
|
2015-12-08 06:17:05 +08:00
|
|
|
RequestFilters []RequestFilter
|
|
|
|
ResponseFilters []ResponseFilter
|
|
|
|
Level int // Compression level
|
2015-03-21 08:11:54 +08:00
|
|
|
}
|
|
|
|
|
2015-01-30 13:04:18 +08:00
|
|
|
// ServeHTTP serves a gzipped response if the client supports it.
|
2015-03-29 06:47:41 +08:00
|
|
|
func (g Gzip) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
2015-01-30 13:04:18 +08:00
|
|
|
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
2015-04-03 13:30:54 +08:00
|
|
|
return g.Next.ServeHTTP(w, r)
|
2015-01-30 13:04:18 +08:00
|
|
|
}
|
2015-04-09 13:24:59 +08:00
|
|
|
|
2015-06-07 07:58:37 +08:00
|
|
|
outer:
|
|
|
|
for _, c := range g.Configs {
|
|
|
|
|
2015-12-08 06:17:05 +08:00
|
|
|
// Check request filters to determine if gzipping is permitted for this request
|
|
|
|
for _, filter := range c.RequestFilters {
|
2015-06-07 07:58:37 +08:00
|
|
|
if !filter.ShouldCompress(r) {
|
|
|
|
continue outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete this header so gzipping is not repeated later in the chain
|
|
|
|
r.Header.Del("Accept-Encoding")
|
|
|
|
|
2015-12-08 19:01:24 +08:00
|
|
|
// gzipWriter modifies underlying writer at init,
|
2015-12-10 01:44:25 +08:00
|
|
|
// use a discard writer instead to leave ResponseWriter in
|
2015-12-08 19:01:24 +08:00
|
|
|
// original form.
|
2015-12-10 01:44:25 +08:00
|
|
|
gzipWriter, err := newWriter(c, ioutil.Discard)
|
2015-06-07 07:58:37 +08:00
|
|
|
if err != nil {
|
|
|
|
// should not happen
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
defer gzipWriter.Close()
|
2015-12-19 03:58:23 +08:00
|
|
|
gz := &gzipResponseWriter{Writer: gzipWriter, ResponseWriter: w}
|
2015-06-07 07:58:37 +08:00
|
|
|
|
2015-12-08 06:17:05 +08:00
|
|
|
var rw http.ResponseWriter
|
|
|
|
// if no response filter is used
|
|
|
|
if len(c.ResponseFilters) == 0 {
|
2015-12-10 01:44:25 +08:00
|
|
|
// replace discard writer with ResponseWriter
|
2015-12-08 19:01:24 +08:00
|
|
|
gzipWriter.Reset(w)
|
2015-12-08 06:17:05 +08:00
|
|
|
rw = gz
|
|
|
|
} else {
|
|
|
|
// wrap gzip writer with ResponseFilterWriter
|
|
|
|
rw = NewResponseFilterWriter(c.ResponseFilters, gz)
|
|
|
|
}
|
|
|
|
|
2015-06-07 07:58:37 +08:00
|
|
|
// Any response in forward middleware will now be compressed
|
2015-12-08 06:17:05 +08:00
|
|
|
status, err := g.Next.ServeHTTP(rw, r)
|
2015-06-07 07:58:37 +08:00
|
|
|
|
|
|
|
// If there was an error that remained unhandled, we need
|
|
|
|
// to send something back before gzipWriter gets closed at
|
|
|
|
// the return of this method!
|
|
|
|
if status >= 400 {
|
|
|
|
gz.Header().Set("Content-Type", "text/plain") // very necessary
|
|
|
|
gz.WriteHeader(status)
|
|
|
|
fmt.Fprintf(gz, "%d %s", status, http.StatusText(status))
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return status, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// no matching filter
|
|
|
|
return g.Next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// newWriter create a new Gzip Writer based on the compression level.
|
|
|
|
// If the level is valid (i.e. between 1 and 9), it uses the level.
|
|
|
|
// Otherwise, it uses default compression level.
|
2015-12-08 19:01:24 +08:00
|
|
|
func newWriter(c Config, w io.Writer) (*gzip.Writer, error) {
|
2015-06-07 07:58:37 +08:00
|
|
|
if c.Level >= gzip.BestSpeed && c.Level <= gzip.BestCompression {
|
|
|
|
return gzip.NewWriterLevel(w, c.Level)
|
2015-04-09 13:24:59 +08:00
|
|
|
}
|
2015-06-07 07:58:37 +08:00
|
|
|
return gzip.NewWriter(w), nil
|
2015-01-30 13:04:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// gzipResponeWriter wraps the underlying Write method
|
|
|
|
// with a gzip.Writer to compress the output.
|
|
|
|
type gzipResponseWriter struct {
|
|
|
|
io.Writer
|
|
|
|
http.ResponseWriter
|
2015-12-19 03:58:23 +08:00
|
|
|
statusCodeWritten bool
|
2015-01-30 13:04:18 +08:00
|
|
|
}
|
|
|
|
|
2015-05-02 23:20:39 +08:00
|
|
|
// WriteHeader wraps the underlying WriteHeader method to prevent
|
|
|
|
// problems with conflicting headers from proxied backends. For
|
|
|
|
// example, a backend system that calculates Content-Length would
|
|
|
|
// be wrong because it doesn't know it's being gzipped.
|
2015-12-19 03:58:23 +08:00
|
|
|
func (w *gzipResponseWriter) WriteHeader(code int) {
|
2015-05-02 23:20:39 +08:00
|
|
|
w.Header().Del("Content-Length")
|
2015-12-08 19:01:24 +08:00
|
|
|
w.Header().Set("Content-Encoding", "gzip")
|
2015-12-15 23:56:44 +08:00
|
|
|
w.Header().Add("Vary", "Accept-Encoding")
|
2015-05-02 23:20:39 +08:00
|
|
|
w.ResponseWriter.WriteHeader(code)
|
2015-12-19 03:58:23 +08:00
|
|
|
w.statusCodeWritten = true
|
2015-05-02 23:20:39 +08:00
|
|
|
}
|
|
|
|
|
2015-01-30 13:04:18 +08:00
|
|
|
// Write wraps the underlying Write method to do compression.
|
2015-12-19 03:58:23 +08:00
|
|
|
func (w *gzipResponseWriter) Write(b []byte) (int, error) {
|
2015-01-30 13:04:18 +08:00
|
|
|
if w.Header().Get("Content-Type") == "" {
|
|
|
|
w.Header().Set("Content-Type", http.DetectContentType(b))
|
|
|
|
}
|
2016-01-12 19:26:37 +08:00
|
|
|
if !w.statusCodeWritten {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
2015-01-30 13:04:18 +08:00
|
|
|
n, err := w.Writer.Write(b)
|
|
|
|
return n, err
|
|
|
|
}
|