From 40895ba8d9eded4d27a16cac663ac13142f9db53 Mon Sep 17 00:00:00 2001 From: kevin Date: Mon, 10 Aug 2020 18:27:08 +0800 Subject: [PATCH] optimize reading http request body --- rest/handler/cryptionhandler.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/rest/handler/cryptionhandler.go b/rest/handler/cryptionhandler.go index 93bbd889..e83132c1 100644 --- a/rest/handler/cryptionhandler.go +++ b/rest/handler/cryptionhandler.go @@ -42,7 +42,14 @@ func decryptBody(key []byte, r *http.Request) error { return errContentLengthExceeded } - content, err := ioutil.ReadAll(io.LimitReader(r.Body, maxBytes)) + var content []byte + var err error + if r.ContentLength > 0 { + content = make([]byte, r.ContentLength, r.ContentLength) + _, err = io.ReadFull(r.Body, content) + } else { + content, err = ioutil.ReadAll(io.LimitReader(r.Body, maxBytes)) + } if err != nil { return err }