HTTP SD: Allow charset in content type (#8981)

* Added content type regex

Signed-off-by: Levi Harrison <git@leviharrison.dev>
pull/9016/head
Levi Harrison 3 years ago committed by GitHub
parent e2557e4920
commit 78d5a6d083
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -21,7 +21,9 @@ import (
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"time"
"github.com/go-kit/log"
@ -41,7 +43,8 @@ var (
RefreshInterval: model.Duration(60 * time.Second),
HTTPClientConfig: config.DefaultHTTPClientConfig,
}
userAgent = fmt.Sprintf("Prometheus/%s", version.Version)
userAgent = fmt.Sprintf("Prometheus/%s", version.Version)
matchContentType = regexp.MustCompile(`^(?i:application\/json(;\s*charset=("utf-8"|utf-8))?)$`)
)
func init() {
@ -152,7 +155,7 @@ func (d *Discovery) refresh(ctx context.Context) ([]*targetgroup.Group, error) {
return nil, errors.Errorf("server returned HTTP status %s", resp.Status)
}
if resp.Header.Get("Content-Type") != "application/json" {
if !matchContentType.MatchString(strings.TrimSpace(resp.Header.Get("Content-Type"))) {
return nil, errors.Errorf("unsupported content type %q", resp.Header.Get("Content-Type"))
}

@ -104,3 +104,61 @@ func TestHTTPInvalidFormat(t *testing.T) {
_, err = d.refresh(ctx)
require.EqualError(t, err, `unsupported content type "text/plain; charset=utf-8"`)
}
func TestContentTypeRegex(t *testing.T) {
cases := []struct {
header string
match bool
}{
{
header: "application/json;charset=utf-8",
match: true,
},
{
header: "application/json;charset=UTF-8",
match: true,
},
{
header: "Application/JSON;Charset=\"utf-8\"",
match: true,
},
{
header: "application/json; charset=\"utf-8\"",
match: true,
},
{
header: "application/json",
match: true,
},
{
header: "application/jsonl; charset=\"utf-8\"",
match: false,
},
{
header: "application/json;charset=UTF-9",
match: false,
},
{
header: "application /json;charset=UTF-8",
match: false,
},
{
header: "application/ json;charset=UTF-8",
match: false,
},
{
header: "application/json;",
match: false,
},
{
header: "charset=UTF-8",
match: false,
},
}
for _, test := range cases {
t.Run(test.header, func(t *testing.T) {
require.Equal(t, test.match, matchContentType.MatchString(test.header))
})
}
}

Loading…
Cancel
Save