From e43763311d894a2de777c4d116bfd97a1bf00a17 Mon Sep 17 00:00:00 2001 From: Muhamad Surya Iksanudin Date: Sat, 27 Jul 2024 12:42:54 +0700 Subject: [PATCH] v3: Consolidate Logic of Handling the Request Body (#3093) reduce redundant call --- ctx.go | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/ctx.go b/ctx.go index b961508b52..84378d7690 100644 --- a/ctx.go +++ b/ctx.go @@ -273,10 +273,7 @@ func (c *DefaultCtx) BaseURL() string { // Returned value is only valid within the handler. Do not store any references. // Make copies or use the Immutable setting instead. func (c *DefaultCtx) BodyRaw() []byte { - if c.app.config.Immutable { - return utils.CopyBytes(c.fasthttp.Request.Body()) - } - return c.fasthttp.Request.Body() + return c.getBody() } func (c *DefaultCtx) tryDecodeBodyInOrder( @@ -344,20 +341,14 @@ func (c *DefaultCtx) Body() []byte { // If no encoding is provided, return the original body if len(headerEncoding) == 0 { - if c.app.config.Immutable { - return utils.CopyBytes(c.fasthttp.Request.Body()) - } - return c.fasthttp.Request.Body() + return c.getBody() } // Split and get the encodings list, in order to attend the // rule defined at: https://www.rfc-editor.org/rfc/rfc9110#section-8.4-5 encodingOrder = getSplicedStrList(headerEncoding, encodingOrder) if len(encodingOrder) == 0 { - if c.app.config.Immutable { - return utils.CopyBytes(c.fasthttp.Request.Body()) - } - return c.fasthttp.Request.Body() + return c.getBody() } var decodesRealized uint8 @@ -1913,6 +1904,14 @@ func (c *DefaultCtx) release() { } } +func (c *DefaultCtx) getBody() []byte { + if c.app.config.Immutable { + return utils.CopyBytes(c.fasthttp.Request.Body()) + } + + return c.fasthttp.Request.Body() +} + // Methods to use with next stack. func (c *DefaultCtx) getMethodINT() int { return c.methodINT