0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00

Bug fix for default error handling with gzip

This commit is contained in:
Matthew Holt 2015-04-08 23:24:59 -06:00
parent d33256f1dc
commit 2a0cfb608d

View file

@ -4,6 +4,7 @@ package gzip
import (
"compress/gzip"
"fmt"
"io"
"net/http"
"strings"
@ -11,7 +12,10 @@ import (
"github.com/mholt/caddy/middleware"
)
// Gzip is a middleware type which gzips HTTP responses.
// 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.
type Gzip struct {
Next middleware.Handler
}
@ -28,11 +32,26 @@ func (g Gzip) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
return g.Next.ServeHTTP(w, r)
}
w.Header().Set("Content-Encoding", "gzip")
gzipWriter := gzip.NewWriter(w)
defer gzipWriter.Close()
gz := gzipResponseWriter{Writer: gzipWriter, ResponseWriter: w}
return g.Next.ServeHTTP(gz, r)
// 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
}
}
// gzipResponeWriter wraps the underlying Write method