mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
HTTP SD: Allow charset in content type (#8981)
* Added content type regex Signed-off-by: Levi Harrison <git@leviharrison.dev>
This commit is contained in:
parent
e2557e4920
commit
78d5a6d083
|
@ -21,7 +21,9 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-kit/log"
|
"github.com/go-kit/log"
|
||||||
|
@ -41,7 +43,8 @@ var (
|
||||||
RefreshInterval: model.Duration(60 * time.Second),
|
RefreshInterval: model.Duration(60 * time.Second),
|
||||||
HTTPClientConfig: config.DefaultHTTPClientConfig,
|
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() {
|
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)
|
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"))
|
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)
|
_, err = d.refresh(ctx)
|
||||||
require.EqualError(t, err, `unsupported content type "text/plain; charset=utf-8"`)
|
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…
Reference in a new issue