From d0842c62ccc4fe48e9b0dfe00a48954db28b2c0d Mon Sep 17 00:00:00 2001 From: Jian Qiu Date: Tue, 1 Sep 2020 10:21:39 +0800 Subject: [PATCH] fix: http error limit request body length (#7699) fix: http error limit request body length Co-authored-by: Qiu Jian --- pkg/util/httputils/httputils.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/pkg/util/httputils/httputils.go b/pkg/util/httputils/httputils.go index cc47684f5a..90a98191e7 100644 --- a/pkg/util/httputils/httputils.go +++ b/pkg/util/httputils/httputils.go @@ -99,16 +99,25 @@ func newJsonClientErrorFromRequest2(method string, urlStr string, hdrs http.Head jce.Request.Method = strings.ToUpper(method) jce.Request.Url = urlStr jce.Request.Headers = make(map[string]string) - excludeHdrs := []string{} + excludeHdrs := []string{ + "Accept", + "Accept-Encoding", + } authHdrs := []string{ http.CanonicalHeaderKey("authorization"), http.CanonicalHeaderKey("x-auth-token"), http.CanonicalHeaderKey("x-subject-token"), } + const ( + MAX_BODY = 128 + FIRST_PART = 100 + ) switch jce.Request.Method { case "PUT", "POST", "PATCH": contType := hdrs.Get(http.CanonicalHeaderKey("content-type")) - if strings.Contains(contType, "json") { + if len(body) > MAX_BODY { + jce.Request.Body = jsonutils.NewString(body[:FIRST_PART] + "..." + body[len(body)-MAX_BODY+FIRST_PART+3:]) + } else if strings.Contains(contType, "json") { jce.Request.Body, _ = jsonutils.ParseString(body) } else if strings.Contains(contType, "xml") || strings.Contains(contType, "x-www-form-urlencoded") {