Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of JSON with a UTF8 BOM in httpjson #3267

Merged
merged 1 commit into from
Sep 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
}