From 6f720d276ec8785d6f04119291c893bc296c86e2 Mon Sep 17 00:00:00 2001 From: Greg Linton Date: Thu, 27 Sep 2018 15:43:23 -0600 Subject: [PATCH 1/3] Unify http_listener error response with influxdb --- plugins/inputs/http_listener/http_listener.go | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/plugins/inputs/http_listener/http_listener.go b/plugins/inputs/http_listener/http_listener.go index 6415ebc9f29eb..e53f34310e235 100644 --- a/plugins/inputs/http_listener/http_listener.go +++ b/plugins/inputs/http_listener/http_listener.go @@ -5,6 +5,7 @@ import ( "compress/gzip" "crypto/subtle" "crypto/tls" + "fmt" "io" "log" "net" @@ -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 } } @@ -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) } @@ -304,12 +305,13 @@ 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) + badRequest(res, err.Error()) } else { res.WriteHeader(http.StatusNoContent) } @@ -322,7 +324,7 @@ 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 @@ -330,7 +332,7 @@ func (h *HTTPListener) serveWrite(res http.ResponseWriter, req *http.Request) { 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 @@ -363,15 +365,20 @@ func (h *HTTPListener) parse(b []byte, t time.Time, precision string) error { 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) { From 52a5875f07c5401381468bad680bc2fde718e9ec Mon Sep 17 00:00:00 2001 From: Greg Linton Date: Fri, 28 Sep 2018 11:44:18 -0600 Subject: [PATCH 2/3] Use verbage similar to influx when parsing fails --- plugins/inputs/http_listener/http_listener.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/inputs/http_listener/http_listener.go b/plugins/inputs/http_listener/http_listener.go index e53f34310e235..850523836a6c6 100644 --- a/plugins/inputs/http_listener/http_listener.go +++ b/plugins/inputs/http_listener/http_listener.go @@ -352,14 +352,14 @@ 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) { From 6cdb43efe138094ff7e5ffc13e9fbb45377064ce Mon Sep 17 00:00:00 2001 From: Greg Linton Date: Fri, 28 Sep 2018 12:21:03 -0600 Subject: [PATCH 3/3] Fix panic --- plugins/inputs/http_listener/http_listener.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/plugins/inputs/http_listener/http_listener.go b/plugins/inputs/http_listener/http_listener.go index 850523836a6c6..cd82e40c0c963 100644 --- a/plugins/inputs/http_listener/http_listener.go +++ b/plugins/inputs/http_listener/http_listener.go @@ -311,7 +311,11 @@ func (h *HTTPListener) serveWrite(res http.ResponseWriter, req *http.Request) { return400 = true } if return400 { - badRequest(res, err.Error()) + if err != nil { + badRequest(res, err.Error()) + } else { + badRequest(res, "") + } } else { res.WriteHeader(http.StatusNoContent) }