fix: Double GZIP.
This commit is contained in:
parent
ccc429e36c
commit
d87c4d89e9
2 changed files with 89 additions and 6 deletions
|
@ -6,12 +6,26 @@ import (
|
|||
"github.com/NYTimes/gziphandler"
|
||||
)
|
||||
|
||||
// Compress is a middleware that allows redirections
|
||||
type Compress struct {
|
||||
const (
|
||||
contentEncodingHeader = "Content-Encoding"
|
||||
)
|
||||
|
||||
// Compress is a middleware that allows redirection
|
||||
type Compress struct{}
|
||||
|
||||
// ServerHTTP is a function used by Negroni
|
||||
func (c *Compress) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
||||
if isEncoded(r.Header) {
|
||||
next.ServeHTTP(rw, r)
|
||||
} else {
|
||||
newGzipHandler := gziphandler.GzipHandler(next)
|
||||
newGzipHandler.ServeHTTP(rw, r)
|
||||
}
|
||||
}
|
||||
|
||||
// ServerHTTP is a function used by negroni
|
||||
func (c *Compress) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
||||
newGzipHandler := gziphandler.GzipHandler(next)
|
||||
newGzipHandler.ServeHTTP(rw, r)
|
||||
func isEncoded(headers http.Header) bool {
|
||||
header := headers.Get(contentEncodingHeader)
|
||||
// According to https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding,
|
||||
// content is not encoded if the header 'Content-Encoding' is empty or equals to 'identity'.
|
||||
return header != "" && header != "identity"
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue