Commit 8f4e7f7f authored by Matthew Holt's avatar Matthew Holt

Refactored gzip middleware to return errors

parent a6744501
...@@ -13,28 +13,27 @@ import ( ...@@ -13,28 +13,27 @@ import (
// Gzip is a http.Handler middleware type which gzips HTTP responses. // Gzip is a http.Handler middleware type which gzips HTTP responses.
type Gzip struct { type Gzip struct {
Next http.HandlerFunc Next middleware.HandlerFunc
} }
// New creates a new gzip middleware instance. // New creates a new gzip middleware instance.
func New(c middleware.Controller) (middleware.Middleware, error) { func New(c middleware.Controller) (middleware.Middleware, error) {
return func(next http.HandlerFunc) http.HandlerFunc { return func(next middleware.HandlerFunc) middleware.HandlerFunc {
gz := Gzip{Next: next} gz := Gzip{Next: next}
return gz.ServeHTTP return gz.ServeHTTP
}, nil }, nil
} }
// ServeHTTP serves a gzipped response if the client supports it. // ServeHTTP serves a gzipped response if the client supports it.
func (g Gzip) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (g Gzip) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") { if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
g.Next(w, r) return g.Next(w, r)
return
} }
w.Header().Set("Content-Encoding", "gzip") w.Header().Set("Content-Encoding", "gzip")
gzipWriter := gzip.NewWriter(w) gzipWriter := gzip.NewWriter(w)
defer gzipWriter.Close() defer gzipWriter.Close()
gz := gzipResponseWriter{Writer: gzipWriter, ResponseWriter: w} gz := gzipResponseWriter{Writer: gzipWriter, ResponseWriter: w}
g.Next(gz, r) return g.Next(gz, r)
} }
// gzipResponeWriter wraps the underlying Write method // gzipResponeWriter wraps the underlying Write method
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment