Skip to content

Commit

Permalink
Unify http_listener error response with influxdb (influxdata#4766)
Browse files Browse the repository at this point in the history
  • Loading branch information
glinton authored and otherpirate committed Mar 15, 2019
1 parent 4ea0493 commit 9bb4626
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions plugins/inputs/http_listener/http_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"compress/gzip"
"crypto/subtle"
"crypto/tls"
"fmt"
"io"
"log"
"net"
Expand Down Expand Up @@ -255,8 +256,8 @@ func (h *HTTPListener) serveWrite(res http.ResponseWriter, req *http.Request) {
body, err = gzip.NewReader(req.Body)
defer body.Close()
if err != nil {
log.Println("E! " + err.Error())
badRequest(res)
log.Println("D! " + err.Error())
badRequest(res, err.Error())
return
}
}
Expand All @@ -270,16 +271,16 @@ func (h *HTTPListener) serveWrite(res http.ResponseWriter, req *http.Request) {
for {
n, err := io.ReadFull(body, buf[bufStart:])
if err != nil && err != io.ErrUnexpectedEOF && err != io.EOF {
log.Println("E! " + err.Error())
log.Println("D! " + err.Error())
// problem reading the request body
badRequest(res)
badRequest(res, err.Error())
return
}
h.BytesRecv.Incr(int64(n))

if err == io.EOF {
if return400 {
badRequest(res)
badRequest(res, "")
} else {
res.WriteHeader(http.StatusNoContent)
}
Expand All @@ -304,12 +305,17 @@ func (h *HTTPListener) serveWrite(res http.ResponseWriter, req *http.Request) {

if err == io.ErrUnexpectedEOF {
// finished reading the request body
if err := h.parse(buf[:n+bufStart], now, precision); err != nil {
log.Println("E! " + err.Error())
err = h.parse(buf[:n+bufStart], now, precision)
if err != nil {
log.Println("D! "+err.Error(), bufStart+n)
return400 = true
}
if return400 {
badRequest(res)
if err != nil {
badRequest(res, err.Error())
} else {
badRequest(res, "")
}
} else {
res.WriteHeader(http.StatusNoContent)
}
Expand All @@ -322,15 +328,15 @@ func (h *HTTPListener) serveWrite(res http.ResponseWriter, req *http.Request) {
i := bytes.LastIndexByte(buf, '\n')
if i == -1 {
// drop any line longer than the max buffer size
log.Printf("E! http_listener received a single line longer than the maximum of %d bytes",
log.Printf("D! http_listener received a single line longer than the maximum of %d bytes",
len(buf))
hangingBytes = true
return400 = true
bufStart = 0
continue
}
if err := h.parse(buf[:i+1], now, precision); err != nil {
log.Println("E! " + err.Error())
log.Println("D! " + err.Error())
return400 = true
}
// rotate the bit remaining after the last newline to the front of the buffer
Expand All @@ -350,28 +356,33 @@ func (h *HTTPListener) parse(b []byte, t time.Time, precision string) error {
h.handler.SetTimeFunc(func() time.Time { return t })
metrics, err := h.parser.Parse(b)
if err != nil {
return err
return fmt.Errorf("unable to parse: %s", err.Error())
}

for _, m := range metrics {
h.acc.AddFields(m.Name(), m.Fields(), m.Tags(), m.Time())
}

return err
return nil
}

func tooLarge(res http.ResponseWriter) {
res.Header().Set("Content-Type", "application/json")
res.Header().Set("X-Influxdb-Version", "1.0")
res.Header().Set("X-Influxdb-Error", "http: request body too large")
res.WriteHeader(http.StatusRequestEntityTooLarge)
res.Write([]byte(`{"error":"http: request body too large"}`))
}

func badRequest(res http.ResponseWriter) {
func badRequest(res http.ResponseWriter, errString string) {
res.Header().Set("Content-Type", "application/json")
res.Header().Set("X-Influxdb-Version", "1.0")
if errString == "" {
errString = "http: bad request"
}
res.Header().Set("X-Influxdb-Error", errString)
res.WriteHeader(http.StatusBadRequest)
res.Write([]byte(`{"error":"http: bad request"}`))
res.Write([]byte(fmt.Sprintf(`{"error":%q}`, errString)))
}

func (h *HTTPListener) AuthenticateIfSet(handler http.HandlerFunc, res http.ResponseWriter, req *http.Request) {
Expand Down

0 comments on commit 9bb4626

Please sign in to comment.