Skip to content

Commit

Permalink
Fixup http write handler changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sparrc committed Oct 13, 2016
1 parent 4c670f7 commit 1fb42ff
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions plugins/inputs/http_listener/http_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"log"
"net"
"net/http"
"strconv"
"sync"
"time"

Expand Down Expand Up @@ -124,6 +123,8 @@ func (t *HttpListener) ServeHTTP(res http.ResponseWriter, req *http.Request) {
res.Header().Set("X-Influxdb-Version", "1.0")
res.WriteHeader(http.StatusBadRequest)
res.Write([]byte(fmt.Sprintf(`{"error":"%s"}`, http400msg.String())))
} else {
res.WriteHeader(http.StatusNoContent)
}
}()

Expand All @@ -138,32 +139,27 @@ func (t *HttpListener) ServeHTTP(res http.ResponseWriter, req *http.Request) {
body = b
}

var bs []byte
if cl := req.Header.Get("Content-length"); cl != "" {
if l, err := strconv.Atoi(cl); err == nil {
bs = make([]byte, 0, l)
}
}

buf := bytes.NewBuffer(bs)
_, err := buf.ReadFrom(body)
buf := bytes.NewBuffer(make([]byte, 0, int(req.ContentLength)))
_, err := buf.ReadFrom(http.MaxBytesReader(res, body, 50*1024*1024))
if err != nil {
log.Printf("E! HttpListener unable to read request body. data: [%s], error: %s\n", string(buf.Bytes()), err.Error())
log.Printf("E! HttpListener unable to read request body. error: %s\n", err.Error())
http400msg.WriteString("HttpHandler unable to read from request body: " + err.Error())
return
}

metrics, err := t.parser.Parse(buf.Bytes())
if err != nil {
log.Printf("E! HttpListener unable to parse metrics. data: [%s], error: %s \n", string(buf.Bytes()), err.Error())
http400msg.WriteString("Error while parsing metrics: " + err.Error())
return
log.Printf("E! HttpListener unable to parse metrics. error: %s \n", err.Error())
if len(metrics) == 0 {
http400msg.WriteString(err.Error())
} else {
http400msg.WriteString("partial write: " + err.Error())
}
}

for _, m := range metrics {
t.acc.AddFields(m.Name(), m.Fields(), m.Tags(), m.Time())
}
res.WriteHeader(http.StatusNoContent)
case "/query":
// Deliver a dummy response to the query endpoint, as some InfluxDB
// clients test endpoint availability with a query
Expand Down

0 comments on commit 1fb42ff

Please sign in to comment.