add gzip response capability to httpd endpoints. fixes #1449

pull/1514/head
Cory LaNou 2015-02-04 16:16:37 -07:00
parent ba5275a7fa
commit 2b5565a2e7
1 changed files with 27 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
@ -12,6 +13,8 @@ import (
"strings"
"time"
"compress/gzip"
"code.google.com/p/go-uuid/uuid"
"github.com/bmizerany/pat"
@ -92,6 +95,7 @@ func NewHandler(s *influxdb.Server, requireAuthentication bool, version string)
handler = http.HandlerFunc(hf)
}
handler = gzipFilter(handler)
handler = versionHeader(handler, version)
handler = cors(handler)
handler = requestID(handler)
@ -376,6 +380,29 @@ func authenticate(inner func(http.ResponseWriter, *http.Request, *influxdb.User)
})
}
type gzipResponseWriter struct {
io.Writer
http.ResponseWriter
}
func (w gzipResponseWriter) Write(b []byte) (int, error) {
return w.Writer.Write(b)
}
// determines if the client can accept compressed responses, and encodes accordingly
func gzipFilter(inner http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
inner.ServeHTTP(w, r)
}
w.Header().Set("Content-Encoding", "gzip")
gz := gzip.NewWriter(w)
defer gz.Close()
gzw := gzipResponseWriter{Writer: gz, ResponseWriter: w}
inner.ServeHTTP(gzw, r)
})
}
// versionHeader taks a HTTP handler and returns a HTTP handler
// and adds the X-INFLUXBD-VERSION header to outgoing responses.
func versionHeader(inner http.Handler, version string) http.Handler {