2015-01-12 21:33:08 +00:00
|
|
|
// Copyright 2013 The Gorilla Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"compress/flate"
|
|
|
|
"compress/gzip"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type compressResponseWriter struct {
|
|
|
|
io.Writer
|
|
|
|
http.ResponseWriter
|
2015-07-27 17:07:11 +00:00
|
|
|
http.Hijacker
|
2015-01-12 21:33:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *compressResponseWriter) Header() http.Header {
|
|
|
|
return w.ResponseWriter.Header()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *compressResponseWriter) Write(b []byte) (int, error) {
|
|
|
|
h := w.ResponseWriter.Header()
|
|
|
|
if h.Get("Content-Type") == "" {
|
|
|
|
h.Set("Content-Type", http.DetectContentType(b))
|
|
|
|
}
|
|
|
|
|
|
|
|
return w.Writer.Write(b)
|
|
|
|
}
|
|
|
|
|
2015-07-27 17:07:11 +00:00
|
|
|
// CompressHandler gzip compresses HTTP responses for clients that support it
|
|
|
|
// via the 'Accept-Encoding' header.
|
2015-01-12 21:33:08 +00:00
|
|
|
func CompressHandler(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
L:
|
|
|
|
for _, enc := range strings.Split(r.Header.Get("Accept-Encoding"), ",") {
|
|
|
|
switch strings.TrimSpace(enc) {
|
|
|
|
case "gzip":
|
|
|
|
w.Header().Set("Content-Encoding", "gzip")
|
|
|
|
w.Header().Add("Vary", "Accept-Encoding")
|
|
|
|
|
|
|
|
gw := gzip.NewWriter(w)
|
|
|
|
defer gw.Close()
|
|
|
|
|
2015-07-27 17:07:11 +00:00
|
|
|
h, hok := w.(http.Hijacker)
|
|
|
|
if !hok { /* w is not Hijacker... oh well... */
|
|
|
|
h = nil
|
|
|
|
}
|
|
|
|
|
2015-01-12 21:33:08 +00:00
|
|
|
w = &compressResponseWriter{
|
|
|
|
Writer: gw,
|
|
|
|
ResponseWriter: w,
|
2015-07-27 17:07:11 +00:00
|
|
|
Hijacker: h,
|
2015-01-12 21:33:08 +00:00
|
|
|
}
|
2015-07-27 17:07:11 +00:00
|
|
|
|
2015-01-12 21:33:08 +00:00
|
|
|
break L
|
|
|
|
case "deflate":
|
|
|
|
w.Header().Set("Content-Encoding", "deflate")
|
|
|
|
w.Header().Add("Vary", "Accept-Encoding")
|
|
|
|
|
|
|
|
fw, _ := flate.NewWriter(w, flate.DefaultCompression)
|
|
|
|
defer fw.Close()
|
|
|
|
|
2015-07-27 17:07:11 +00:00
|
|
|
h, hok := w.(http.Hijacker)
|
|
|
|
if !hok { /* w is not Hijacker... oh well... */
|
|
|
|
h = nil
|
|
|
|
}
|
|
|
|
|
2015-01-12 21:33:08 +00:00
|
|
|
w = &compressResponseWriter{
|
|
|
|
Writer: fw,
|
|
|
|
ResponseWriter: w,
|
2015-07-27 17:07:11 +00:00
|
|
|
Hijacker: h,
|
2015-01-12 21:33:08 +00:00
|
|
|
}
|
2015-07-27 17:07:11 +00:00
|
|
|
|
2015-01-12 21:33:08 +00:00
|
|
|
break L
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|