Skip to content

Commit

Permalink
Allow http write handler to decode gzipped body
Browse files Browse the repository at this point in the history
  • Loading branch information
corylanou committed Apr 7, 2015
1 parent 48b3986 commit a646e56
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
35 changes: 25 additions & 10 deletions httpd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,27 +423,41 @@ 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 {
h.Logger.Printf("write body received by handler: %s", string(b))
}
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 {
Expand Down Expand Up @@ -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))
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/read_write_gzip.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)"

0 comments on commit a646e56

Please sign in to comment.