From a646e56adfdd45bb5b453f258b06ac9641010773 Mon Sep 17 00:00:00 2001 From: Cory LaNou Date: Tue, 7 Apr 2015 10:13:02 -0500 Subject: [PATCH] Allow http write handler to decode gzipped body --- httpd/handler.go | 35 +++++++++++++++++++++++++---------- tests/read_write_gzip.sh | 6 +++--- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/httpd/handler.go b/httpd/handler.go index debc16f259e..c7f20a1a4be 100644 --- a/httpd/handler.go +++ b/httpd/handler.go @@ -423,11 +423,32 @@ func (h *Handler) serveDump(w http.ResponseWriter, r *http.Request, user *influx // serveWrite receives incoming series data and writes it to the database. func (h *Handler) serveWrite(w http.ResponseWriter, r *http.Request, user *influxdb.User) { + var writeError = func(result influxdb.Result, statusCode int) { + w.Header().Add("content-type", "application/json") + w.WriteHeader(statusCode) + _ = json.NewEncoder(w).Encode(&result) + return + } + + // Check to see if we have a gzip'd post + var body io.ReadCloser + if r.Header.Get("Content-encoding") == "gzip" { + b, err := gzip.NewReader(r.Body) + if err != nil { + writeError(influxdb.Result{Err: err}, http.StatusBadRequest) + return + } + body = b + defer r.Body.Close() + } else { + body = r.Body + } + var bp client.BatchPoints var dec *json.Decoder if h.WriteTrace { - b, err := ioutil.ReadAll(r.Body) + b, err := ioutil.ReadAll(body) if err != nil { h.Logger.Print("write handler failed to read bytes from request body") } else { @@ -435,15 +456,8 @@ func (h *Handler) serveWrite(w http.ResponseWriter, r *http.Request, user *influ } dec = json.NewDecoder(strings.NewReader(string(b))) } else { - dec = json.NewDecoder(r.Body) - defer r.Body.Close() - } - - var writeError = func(result influxdb.Result, statusCode int) { - w.Header().Add("content-type", "application/json") - w.WriteHeader(statusCode) - _ = json.NewEncoder(w).Encode(&result) - return + dec = json.NewDecoder(body) + defer body.Close() } if err := dec.Decode(&bp); err != nil { @@ -485,6 +499,7 @@ func (h *Handler) serveWrite(w http.ResponseWriter, r *http.Request, user *influ writeError(influxdb.Result{Err: err}, http.StatusInternalServerError) return } else { + w.WriteHeader(http.StatusOK) w.Header().Add("X-InfluxDB-Index", fmt.Sprintf("%d", index)) } } diff --git a/tests/read_write_gzip.sh b/tests/read_write_gzip.sh index 63b53c21c7c..94ceaf762b6 100755 --- a/tests/read_write_gzip.sh +++ b/tests/read_write_gzip.sh @@ -4,12 +4,12 @@ curl -G http://localhost:8086/query --data-urlencode "q=CREATE DATABASE foo" echo "creating retention policy" curl -G http://localhost:8086/query --data-urlencode "q=CREATE RETENTION POLICY bar ON foo DURATION 1h REPLICATION 3 DEFAULT" -echo '{"database" : "foo", "retentionPolicy" : "bar", "points": [{"name": "cpu", "tags": {"host": "server01"},"timestamp": "2015-01-26T22:01:11.703Z","fields": {"value": 123}}]}' | gzip > ./foo.zip +echo '{"database" : "foo", "retentionPolicy" : "bar", "points": [{"name": "cpu", "tags": {"host": "server01"},"timestamp": "2015-01-26T22:01:11.703Z","fields": {"value": 123}}]}' | gzip > foo.json.gz echo "inserting data" -curl -v -i --compressed -H "Content-encoding: gzip" -H "Content-Type: application/json" -X POST --data-binary ./foo.zip http://localhost:8086/write +curl -v -i -H "Content-encoding: gzip" -H "Content-Type: application/json" -X POST -T foo.json.gz http://localhost:8086/write -rm ./foo.zip +rm foo.json.gz echo "querying data with gzip encoding" curl -v -G --compressed http://localhost:8086/query --data-urlencode "db=foo" --data-urlencode "q=SELECT sum(value) FROM \"foo\".\"bar\".cpu GROUP BY time(1h)"