2015-12-07 17:17:05 -05:00
|
|
|
package gzip
|
|
|
|
|
|
|
|
import (
|
2015-12-08 06:01:24 -05:00
|
|
|
"compress/gzip"
|
2015-12-07 17:17:05 -05:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ResponseFilter determines if the response should be gzipped.
|
|
|
|
type ResponseFilter interface {
|
|
|
|
ShouldCompress(http.ResponseWriter) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// LengthFilter is ResponseFilter for minimum content length.
|
|
|
|
type LengthFilter int64
|
|
|
|
|
|
|
|
// ShouldCompress returns if content length is greater than or
|
|
|
|
// equals to minimum length.
|
|
|
|
func (l LengthFilter) ShouldCompress(w http.ResponseWriter) bool {
|
2015-12-07 17:27:57 -05:00
|
|
|
contentLength := w.Header().Get("Content-Length")
|
2015-12-07 17:17:05 -05:00
|
|
|
length, err := strconv.ParseInt(contentLength, 10, 64)
|
|
|
|
if err != nil || length == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return l != 0 && int64(l) <= length
|
|
|
|
}
|
|
|
|
|
2016-12-19 11:51:09 -05:00
|
|
|
// SkipCompressedFilter is ResponseFilter that will discard already compressed responses
|
|
|
|
type SkipCompressedFilter struct{}
|
|
|
|
|
|
|
|
// ShouldCompress returns true if served file is not already compressed
|
|
|
|
// encodings via https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding
|
|
|
|
func (n SkipCompressedFilter) ShouldCompress(w http.ResponseWriter) bool {
|
|
|
|
switch w.Header().Get("Content-Encoding") {
|
|
|
|
case "gzip", "compress", "deflate", "br":
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-07 17:17:05 -05:00
|
|
|
// ResponseFilterWriter validates ResponseFilters. It writes
|
|
|
|
// gzip compressed data if ResponseFilters are satisfied or
|
|
|
|
// uncompressed data otherwise.
|
|
|
|
type ResponseFilterWriter struct {
|
2015-12-18 15:25:06 -05:00
|
|
|
filters []ResponseFilter
|
|
|
|
shouldCompress bool
|
|
|
|
statusCodeWritten bool
|
2015-12-18 14:58:23 -05:00
|
|
|
*gzipResponseWriter
|
2015-12-07 17:17:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewResponseFilterWriter creates and initializes a new ResponseFilterWriter.
|
2015-12-18 14:58:23 -05:00
|
|
|
func NewResponseFilterWriter(filters []ResponseFilter, gz *gzipResponseWriter) *ResponseFilterWriter {
|
2015-12-07 17:17:05 -05:00
|
|
|
return &ResponseFilterWriter{filters: filters, gzipResponseWriter: gz}
|
|
|
|
}
|
|
|
|
|
2016-01-06 18:04:33 -05:00
|
|
|
// WriteHeader wraps underlying WriteHeader method and
|
|
|
|
// compresses if filters are satisfied.
|
2015-12-08 06:01:24 -05:00
|
|
|
func (r *ResponseFilterWriter) WriteHeader(code int) {
|
|
|
|
// Determine if compression should be used or not.
|
|
|
|
r.shouldCompress = true
|
|
|
|
for _, filter := range r.filters {
|
|
|
|
if !filter.ShouldCompress(r) {
|
|
|
|
r.shouldCompress = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.shouldCompress {
|
2015-12-09 12:44:25 -05:00
|
|
|
// replace discard writer with ResponseWriter
|
2015-12-08 06:01:24 -05:00
|
|
|
if gzWriter, ok := r.gzipResponseWriter.Writer.(*gzip.Writer); ok {
|
|
|
|
gzWriter.Reset(r.ResponseWriter)
|
2015-12-07 17:17:05 -05:00
|
|
|
}
|
2015-12-08 06:01:24 -05:00
|
|
|
// use gzip WriteHeader to include and delete
|
|
|
|
// necessary headers
|
|
|
|
r.gzipResponseWriter.WriteHeader(code)
|
|
|
|
} else {
|
|
|
|
r.ResponseWriter.WriteHeader(code)
|
2015-12-07 17:17:05 -05:00
|
|
|
}
|
2015-12-18 15:25:06 -05:00
|
|
|
r.statusCodeWritten = true
|
2015-12-08 06:01:24 -05:00
|
|
|
}
|
2015-12-07 17:17:05 -05:00
|
|
|
|
2015-12-08 06:01:24 -05:00
|
|
|
// Write wraps underlying Write method and compresses if filters
|
|
|
|
// are satisfied
|
|
|
|
func (r *ResponseFilterWriter) Write(b []byte) (int, error) {
|
2015-12-18 15:25:06 -05:00
|
|
|
if !r.statusCodeWritten {
|
|
|
|
r.WriteHeader(http.StatusOK)
|
|
|
|
}
|
2015-12-07 17:17:05 -05:00
|
|
|
if r.shouldCompress {
|
|
|
|
return r.gzipResponseWriter.Write(b)
|
|
|
|
}
|
|
|
|
return r.ResponseWriter.Write(b)
|
|
|
|
}
|