From 1539c5f0aa0c18fdced7cd5a8916685c74a1e748 Mon Sep 17 00:00:00 2001 From: xiaoqing Date: Sun, 7 Nov 2021 22:09:35 +0800 Subject: [PATCH 1/3] 1. Handle prometheus post query cache correctly 2. Support request content type is application JSON with charset Signed-off-by: xiaoqing --- pkg/proxy/engines/key.go | 4 ++-- pkg/proxy/params/params.go | 37 ++++++++++++++++++++++++------------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/pkg/proxy/engines/key.go b/pkg/proxy/engines/key.go index 3292e5ac9..ae5dc97db 100644 --- a/pkg/proxy/engines/key.go +++ b/pkg/proxy/engines/key.go @@ -97,10 +97,10 @@ 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) 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 dc821862b..8b04a7cb3 100644 --- a/pkg/proxy/params/params.go +++ b/pkg/proxy/params/params.go @@ -19,9 +19,13 @@ package params import ( "bytes" + "fmt" + "strconv" + "io" "net/http" "net/url" + "strings" "github.com/trickstercache/trickster/v2/pkg/proxy/headers" "github.com/trickstercache/trickster/v2/pkg/proxy/methods" @@ -59,20 +63,27 @@ func GetRequestValues(r *http.Request) (url.Values, string, bool) { if !methods.HasBody(r.Method) { v = r.URL.Query() s = r.URL.RawQuery - } else if r.Header.Get(headers.NameContentType) == headers.ValueApplicationJSON { - v = url.Values{} - b, _ := io.ReadAll(r.Body) - r.Body.Close() - r.Body = io.NopCloser(bytes.NewReader(b)) - s = string(b) - isBody = true } else { - r.ParseForm() - v = r.PostForm - s = v.Encode() - isBody = true - r.ContentLength = int64(len(s)) - r.Body = io.NopCloser(bytes.NewReader([]byte(s))) + if strings.HasPrefix(r.Header.Get(headers.NameContentType), headers.ValueApplicationJSON) { + v = r.URL.Query() + b, _ := io.ReadAll(r.Body) + r.Body.Close() + r.Body = io.NopCloser(bytes.NewReader(b)) + s = string(b) + isBody = true + } else { + r.ParseForm() + v = r.PostForm + s = v.Encode() + isBody = true + r.ContentLength = int64(len(s)) + r.Body = io.NopCloser(bytes.NewReader([]byte(s))) + } + if len(s) == 0 { + v = r.URL.Query() + s = r.URL.RawQuery + isBody = false + } } return v, s, isBody } From 744e825978fc9e3ae134c07bec224c0f6a13a3f7 Mon Sep 17 00:00:00 2001 From: James Ranson Date: Sat, 2 Dec 2023 17:30:58 -0700 Subject: [PATCH 2/3] fix compile issues Signed-off-by: James Ranson --- pkg/proxy/engines/key.go | 4 ++-- pkg/proxy/params/params.go | 5 +---- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/pkg/proxy/engines/key.go b/pkg/proxy/engines/key.go index ae5dc97db..dbac3fb07 100644 --- a/pkg/proxy/engines/key.go +++ b/pkg/proxy/engines/key.go @@ -24,12 +24,12 @@ import ( "strconv" "strings" + "github.com/trickstercache/trickster/v2/pkg/checksum/md5" "github.com/trickstercache/trickster/v2/pkg/proxy/errors" "github.com/trickstercache/trickster/v2/pkg/proxy/headers" "github.com/trickstercache/trickster/v2/pkg/proxy/methods" "github.com/trickstercache/trickster/v2/pkg/proxy/params" "github.com/trickstercache/trickster/v2/pkg/proxy/request" - "github.com/trickstercache/trickster/v2/pkg/checksum/md5" ) // DeriveCacheKey calculates a query-specific keyname based on the user request @@ -95,7 +95,7 @@ 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) || strings.HasPrefix(ct, headers.ValueApplicationJSON) { if strings.HasPrefix(ct, headers.ValueMultipartFormData) { diff --git a/pkg/proxy/params/params.go b/pkg/proxy/params/params.go index 8b04a7cb3..a1e00a3a3 100644 --- a/pkg/proxy/params/params.go +++ b/pkg/proxy/params/params.go @@ -19,9 +19,6 @@ package params import ( "bytes" - "fmt" - "strconv" - "io" "net/http" "net/url" @@ -64,7 +61,7 @@ func GetRequestValues(r *http.Request) (url.Values, string, bool) { v = r.URL.Query() s = r.URL.RawQuery } else { - if strings.HasPrefix(r.Header.Get(headers.NameContentType), headers.ValueApplicationJSON) { + if strings.HasPrefix(strings.ToLower(r.Header.Get(headers.NameContentType)), headers.ValueApplicationJSON) { v = r.URL.Query() b, _ := io.ReadAll(r.Body) r.Body.Close() From d2b8cd4816e51fdc359e76a730830ec1b285331f Mon Sep 17 00:00:00 2001 From: James Ranson Date: Sat, 2 Dec 2023 17:44:14 -0700 Subject: [PATCH 3/3] fix typo Signed-off-by: James Ranson --- pkg/proxy/params/params.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/proxy/params/params.go b/pkg/proxy/params/params.go index b41b81b27..dcdb1f122 100644 --- a/pkg/proxy/params/params.go +++ b/pkg/proxy/params/params.go @@ -97,10 +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, is body - } + 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] {