Skip to content

Commit e437633

Browse files
authored
v3: Consolidate Logic of Handling the Request Body (#3093)
reduce redundant call
1 parent 99173cc commit e437633

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

ctx.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,7 @@ func (c *DefaultCtx) BaseURL() string {
273273
// Returned value is only valid within the handler. Do not store any references.
274274
// Make copies or use the Immutable setting instead.
275275
func (c *DefaultCtx) BodyRaw() []byte {
276-
if c.app.config.Immutable {
277-
return utils.CopyBytes(c.fasthttp.Request.Body())
278-
}
279-
return c.fasthttp.Request.Body()
276+
return c.getBody()
280277
}
281278

282279
func (c *DefaultCtx) tryDecodeBodyInOrder(
@@ -344,20 +341,14 @@ func (c *DefaultCtx) Body() []byte {
344341

345342
// If no encoding is provided, return the original body
346343
if len(headerEncoding) == 0 {
347-
if c.app.config.Immutable {
348-
return utils.CopyBytes(c.fasthttp.Request.Body())
349-
}
350-
return c.fasthttp.Request.Body()
344+
return c.getBody()
351345
}
352346

353347
// Split and get the encodings list, in order to attend the
354348
// rule defined at: https://www.rfc-editor.org/rfc/rfc9110#section-8.4-5
355349
encodingOrder = getSplicedStrList(headerEncoding, encodingOrder)
356350
if len(encodingOrder) == 0 {
357-
if c.app.config.Immutable {
358-
return utils.CopyBytes(c.fasthttp.Request.Body())
359-
}
360-
return c.fasthttp.Request.Body()
351+
return c.getBody()
361352
}
362353

363354
var decodesRealized uint8
@@ -1913,6 +1904,14 @@ func (c *DefaultCtx) release() {
19131904
}
19141905
}
19151906

1907+
func (c *DefaultCtx) getBody() []byte {
1908+
if c.app.config.Immutable {
1909+
return utils.CopyBytes(c.fasthttp.Request.Body())
1910+
}
1911+
1912+
return c.fasthttp.Request.Body()
1913+
}
1914+
19161915
// Methods to use with next stack.
19171916
func (c *DefaultCtx) getMethodINT() int {
19181917
return c.methodINT

0 commit comments

Comments
 (0)