From f7935ed96395523d03e427f0d1a1844ff5f31b1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=BC=9F=E4=B9=90?= Date: Tue, 14 Mar 2023 14:27:59 +0800 Subject: [PATCH] x --- internal/httputil/http.go | 8 +++++++- internal/httputil/http_test.go | 10 ++++++++++ transport/http/codec.go | 2 -- 3 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 internal/httputil/http_test.go diff --git a/internal/httputil/http.go b/internal/httputil/http.go index e84453c..0101fd1 100644 --- a/internal/httputil/http.go +++ b/internal/httputil/http.go @@ -1,6 +1,8 @@ package httputil -import "strings" +import ( + "strings" +) const ( baseContentType = "application" @@ -19,6 +21,10 @@ func ContentSubtype(contentType string) string { if right == -1 { right = len(contentType) } + right1 := strings.Index(contentType, ",") + if right1 > 0 && right1 < right { + right = right1 + } if right < left { return "" } diff --git a/internal/httputil/http_test.go b/internal/httputil/http_test.go new file mode 100644 index 0000000..aa13117 --- /dev/null +++ b/internal/httputil/http_test.go @@ -0,0 +1,10 @@ +package httputil + +import ( + "fmt" + "testing" +) + +func TestContentSubtype(t *testing.T) { + fmt.Println(ContentSubtype("application/json, text/plain, */*")) +} diff --git a/transport/http/codec.go b/transport/http/codec.go index 156448a..fd353d4 100644 --- a/transport/http/codec.go +++ b/transport/http/codec.go @@ -37,7 +37,6 @@ func DefaultRequestQuery(r *http.Request, v any) error { return binding.BindQuery(r.URL.Query(), v) } func DefaultRequestDecoder(r *http.Request, v any) error { - fmt.Println("request decoder") codec, ok := CodeForRequest(r, "Content-type") if !ok { return errors.BadRequest("CODEC", fmt.Sprintf("unregister Content-Type: %s", codec)) @@ -89,7 +88,6 @@ func DefaultErrorEncoder(w http.ResponseWriter, r *http.Request, err error) { func CodeForRequest(r *http.Request, name string) (encoding.Codec, bool) { for _, accept := range r.Header[name] { a := httputil.ContentSubtype(accept) - fmt.Println("aaa", name, a) codec := encoding.GetCodec(a) if codec != nil { return codec, true