2015-01-30 01:52:18 -05:00
|
|
|
// Package gzip provides a simple middleware layer that performs
|
|
|
|
// gzip compression on the response.
|
2015-01-30 00:04:18 -05:00
|
|
|
package gzip
|
|
|
|
|
|
|
|
import (
|
|
|
|
"compress/gzip"
|
2015-04-09 00:24:59 -05:00
|
|
|
"fmt"
|
2015-01-30 00:04:18 -05:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/mholt/caddy/middleware"
|
|
|
|
)
|
|
|
|
|
2015-04-09 00:24:59 -05: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-20 19:11:54 -05:00
|
|
|
type Gzip struct {
|
2015-04-03 00:30:54 -05:00
|
|
|
Next middleware.Handler
|
2015-03-20 19:11:54 -05:00
|
|
|
}
|
|
|
|
|
2015-01-30 00:04:18 -05:00
|
|
|
// New creates a new gzip middleware instance.
|
|
|
|
func New(c middleware.Controller) (middleware.Middleware, error) {
|
2015-04-03 00:30:54 -05:00
|
|
|
return func(next middleware.Handler) middleware.Handler {
|
|
|
|
return Gzip{Next: next}
|
2015-01-30 00:04:18 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTP serves a gzipped response if the client supports it.
|
2015-03-28 17:47:41 -05:00
|
|
|
func (g Gzip) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
2015-01-30 00:04:18 -05:00
|
|
|
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
2015-04-03 00:30:54 -05:00
|
|
|
return g.Next.ServeHTTP(w, r)
|
2015-01-30 00:04:18 -05:00
|
|
|
}
|
2015-04-09 00:24:59 -05:00
|
|
|
|
2015-04-26 23:53:47 -05:00
|
|
|
// Delete this header so gzipping isn't repeated later in the chain
|
|
|
|
r.Header.Del("Accept-Encoding")
|
|
|
|
|
2015-01-30 00:04:18 -05:00
|
|
|
w.Header().Set("Content-Encoding", "gzip")
|
|
|
|
gzipWriter := gzip.NewWriter(w)
|
|
|
|
defer gzipWriter.Close()
|
|
|
|
gz := gzipResponseWriter{Writer: gzipWriter, ResponseWriter: w}
|
2015-04-09 00:24:59 -05:00
|
|
|
|
|
|
|
// Any response in forward middleware will now be compressed
|
|
|
|
status, err := g.Next.ServeHTTP(gz, r)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
} else {
|
|
|
|
return status, err
|
|
|
|
}
|
2015-01-30 00:04:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// gzipResponeWriter wraps the underlying Write method
|
|
|
|
// with a gzip.Writer to compress the output.
|
|
|
|
type gzipResponseWriter struct {
|
|
|
|
io.Writer
|
|
|
|
http.ResponseWriter
|
|
|
|
}
|
|
|
|
|
2015-05-02 10:20:39 -05: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.
|
|
|
|
func (w gzipResponseWriter) WriteHeader(code int) {
|
|
|
|
w.Header().Del("Content-Length")
|
|
|
|
w.ResponseWriter.WriteHeader(code)
|
|
|
|
}
|
|
|
|
|
2015-01-30 00:04:18 -05:00
|
|
|
// Write wraps the underlying Write method to do compression.
|
|
|
|
func (w gzipResponseWriter) Write(b []byte) (int, error) {
|
|
|
|
if w.Header().Get("Content-Type") == "" {
|
|
|
|
w.Header().Set("Content-Type", http.DetectContentType(b))
|
|
|
|
}
|
|
|
|
n, err := w.Writer.Write(b)
|
|
|
|
return n, err
|
|
|
|
}
|