Skip to content

Commit

Permalink
Fix parsing of JSON with a UTF8 BOM in httpjson (influxdata#3267)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnelson authored Sep 26, 2017
1 parent f23d1eb commit 8614445
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
7 changes: 6 additions & 1 deletion plugins/inputs/httpjson/httpjson.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package httpjson

import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
Expand All @@ -15,6 +16,10 @@ import (
"github.com/influxdata/telegraf/plugins/parsers"
)

var (
utf8BOM = []byte("\xef\xbb\xbf")
)

// HttpJson struct
type HttpJson struct {
Name string
Expand Down Expand Up @@ -170,7 +175,6 @@ func (h *HttpJson) gatherServer(
serverURL string,
) error {
resp, responseTime, err := h.sendRequest(serverURL)

if err != nil {
return err
}
Expand Down Expand Up @@ -266,6 +270,7 @@ func (h *HttpJson) sendRequest(serverURL string) (string, float64, error) {
if err != nil {
return string(body), responseTime, err
}
body = bytes.TrimPrefix(body, utf8BOM)

// Process response
if resp.StatusCode != http.StatusOK {
Expand Down
15 changes: 15 additions & 0 deletions plugins/inputs/httpjson/httpjson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,3 +560,18 @@ func TestHttpJsonArray200Tags(t *testing.T) {
}
}
}

var jsonBOM = []byte("\xef\xbb\xbf[{\"value\":17}]")

// TestHttpJsonBOM tests that UTF-8 JSON with a BOM can be parsed
func TestHttpJsonBOM(t *testing.T) {
httpjson := genMockHttpJson(string(jsonBOM), 200)

for _, service := range httpjson {
if service.Name == "other_webapp" {
var acc testutil.Accumulator
err := acc.GatherError(service.Gather)
require.NoError(t, err)
}
}
}

0 comments on commit 8614445

Please sign in to comment.