From fef86de0453bd3bd07a15a815622d7c4c3bf2e38 Mon Sep 17 00:00:00 2001 From: wowqing Date: Sun, 3 Dec 2023 08:48:07 +0800 Subject: [PATCH] Handle prometheus post query cache correctly (#590) * 1. Handle prometheus post query cache correctly 2. Support request content type is application JSON with charset Signed-off-by: xiaoqing * fix compile issues Signed-off-by: James Ranson * fix typo Signed-off-by: James Ranson --------- Signed-off-by: xiaoqing Signed-off-by: James Ranson Co-authored-by: James Ranson --- pkg/proxy/engines/key.go | 6 +++--- pkg/proxy/params/params.go | 5 ++++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/pkg/proxy/engines/key.go b/pkg/proxy/engines/key.go index e627f7c0d..dbac3fb07 100644 --- a/pkg/proxy/engines/key.go +++ b/pkg/proxy/engines/key.go @@ -95,12 +95,12 @@ func (pr *proxyRequest) DeriveCacheKey(extra string) string { } if methods.HasBody(r.Method) && pc.CacheKeyFormFields != nil && len(pc.CacheKeyFormFields) > 0 { - ct := r.Header.Get(headers.NameContentType) + ct := strings.ToLower(r.Header.Get(headers.NameContentType)) if ct == headers.ValueXFormURLEncoded || - strings.HasPrefix(ct, headers.ValueMultipartFormData) || ct == headers.ValueApplicationJSON { + strings.HasPrefix(ct, headers.ValueMultipartFormData) || strings.HasPrefix(ct, headers.ValueApplicationJSON) { if strings.HasPrefix(ct, headers.ValueMultipartFormData) { pr.ParseMultipartForm(1024 * 1024) - } else if ct == headers.ValueApplicationJSON { + } else if strings.HasPrefix(ct, headers.ValueApplicationJSON) { var document map[string]interface{} err := json.Unmarshal(b, &document) if err == nil { diff --git a/pkg/proxy/params/params.go b/pkg/proxy/params/params.go index 7f831da55..dcdb1f122 100644 --- a/pkg/proxy/params/params.go +++ b/pkg/proxy/params/params.go @@ -97,6 +97,10 @@ func GetRequestValues(r *http.Request) (url.Values, string, bool) { r.Body.Close() r.Body = io.NopCloser(bytes.NewReader(b)) s = string(b) + isBody = true + if strings.HasPrefix(strings.ToLower(r.Header.Get(headers.NameContentType)), headers.ValueApplicationJSON) { + return v, s, isBody + } if vs, err := url.ParseQuery(s); err == nil && isQueryBody(r) { for vsk := range vs { for _, vsv := range vs[vsk] { @@ -108,7 +112,6 @@ func GetRequestValues(r *http.Request) (url.Values, string, bool) { } } } - isBody = true } return v, s, isBody }