Unify http_listener error response with influxdb (#4766)

This commit is contained in:
Greg 2018-10-01 17:14:54 -06:00 committed by Daniel Nelson
parent 797fbf7215
commit 11baebd6c9
1 changed files with 25 additions and 14 deletions

View File

@ -5,6 +5,7 @@ import (
"compress/gzip" "compress/gzip"
"crypto/subtle" "crypto/subtle"
"crypto/tls" "crypto/tls"
"fmt"
"io" "io"
"log" "log"
"net" "net"
@ -255,8 +256,8 @@ func (h *HTTPListener) serveWrite(res http.ResponseWriter, req *http.Request) {
body, err = gzip.NewReader(req.Body) body, err = gzip.NewReader(req.Body)
defer body.Close() defer body.Close()
if err != nil { if err != nil {
log.Println("E! " + err.Error()) log.Println("D! " + err.Error())
badRequest(res) badRequest(res, err.Error())
return return
} }
} }
@ -270,16 +271,16 @@ func (h *HTTPListener) serveWrite(res http.ResponseWriter, req *http.Request) {
for { for {
n, err := io.ReadFull(body, buf[bufStart:]) n, err := io.ReadFull(body, buf[bufStart:])
if err != nil && err != io.ErrUnexpectedEOF && err != io.EOF { if err != nil && err != io.ErrUnexpectedEOF && err != io.EOF {
log.Println("E! " + err.Error()) log.Println("D! " + err.Error())
// problem reading the request body // problem reading the request body
badRequest(res) badRequest(res, err.Error())
return return
} }
h.BytesRecv.Incr(int64(n)) h.BytesRecv.Incr(int64(n))
if err == io.EOF { if err == io.EOF {
if return400 { if return400 {
badRequest(res) badRequest(res, "")
} else { } else {
res.WriteHeader(http.StatusNoContent) res.WriteHeader(http.StatusNoContent)
} }
@ -304,12 +305,17 @@ func (h *HTTPListener) serveWrite(res http.ResponseWriter, req *http.Request) {
if err == io.ErrUnexpectedEOF { if err == io.ErrUnexpectedEOF {
// finished reading the request body // finished reading the request body
if err := h.parse(buf[:n+bufStart], now, precision); err != nil { err = h.parse(buf[:n+bufStart], now, precision)
log.Println("E! " + err.Error()) if err != nil {
log.Println("D! "+err.Error(), bufStart+n)
return400 = true return400 = true
} }
if return400 { if return400 {
badRequest(res) if err != nil {
badRequest(res, err.Error())
} else {
badRequest(res, "")
}
} else { } else {
res.WriteHeader(http.StatusNoContent) res.WriteHeader(http.StatusNoContent)
} }
@ -322,7 +328,7 @@ func (h *HTTPListener) serveWrite(res http.ResponseWriter, req *http.Request) {
i := bytes.LastIndexByte(buf, '\n') i := bytes.LastIndexByte(buf, '\n')
if i == -1 { if i == -1 {
// drop any line longer than the max buffer size // 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)) len(buf))
hangingBytes = true hangingBytes = true
return400 = true return400 = true
@ -330,7 +336,7 @@ func (h *HTTPListener) serveWrite(res http.ResponseWriter, req *http.Request) {
continue continue
} }
if err := h.parse(buf[:i+1], now, precision); err != nil { if err := h.parse(buf[:i+1], now, precision); err != nil {
log.Println("E! " + err.Error()) log.Println("D! " + err.Error())
return400 = true return400 = true
} }
// rotate the bit remaining after the last newline to the front of the buffer // rotate the bit remaining after the last newline to the front of the buffer
@ -350,28 +356,33 @@ func (h *HTTPListener) parse(b []byte, t time.Time, precision string) error {
h.handler.SetTimeFunc(func() time.Time { return t }) h.handler.SetTimeFunc(func() time.Time { return t })
metrics, err := h.parser.Parse(b) metrics, err := h.parser.Parse(b)
if err != nil { if err != nil {
return err return fmt.Errorf("unable to parse: %s", err.Error())
} }
for _, m := range metrics { for _, m := range metrics {
h.acc.AddFields(m.Name(), m.Fields(), m.Tags(), m.Time()) h.acc.AddFields(m.Name(), m.Fields(), m.Tags(), m.Time())
} }
return err return nil
} }
func tooLarge(res http.ResponseWriter) { func tooLarge(res http.ResponseWriter) {
res.Header().Set("Content-Type", "application/json") res.Header().Set("Content-Type", "application/json")
res.Header().Set("X-Influxdb-Version", "1.0") res.Header().Set("X-Influxdb-Version", "1.0")
res.Header().Set("X-Influxdb-Error", "http: request body too large")
res.WriteHeader(http.StatusRequestEntityTooLarge) res.WriteHeader(http.StatusRequestEntityTooLarge)
res.Write([]byte(`{"error":"http: request body too large"}`)) 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("Content-Type", "application/json")
res.Header().Set("X-Influxdb-Version", "1.0") 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.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) { func (h *HTTPListener) AuthenticateIfSet(handler http.HandlerFunc, res http.ResponseWriter, req *http.Request) {