Skip to content

Commit 56ff2de

Browse files
authored
πŸ› fix: Respect Immutable config for Body() (#3246)
* πŸ› fix: respect Immutable config for Body() * ci: add go 1.22 and 1.23 to test matrix
1 parent 8c84b0f commit 56ff2de

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

β€Ž.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
Build:
1818
strategy:
1919
matrix:
20-
go-version: [1.17.x, 1.18.x, 1.19.x, 1.20.x, 1.21.x]
20+
go-version: [1.17.x, 1.18.x, 1.19.x, 1.20.x, 1.21.x, 1.22.x, 1.23.x]
2121
platform: [ubuntu-latest, windows-latest, macos-latest]
2222
runs-on: ${{ matrix.platform }}
2323
steps:

β€Žctx.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ func (c *Ctx) BaseURL() string {
268268
// Returned value is only valid within the handler. Do not store any references.
269269
// Make copies or use the Immutable setting instead.
270270
func (c *Ctx) BodyRaw() []byte {
271-
return c.fasthttp.Request.Body()
271+
return c.getBody()
272272
}
273273

274274
func (c *Ctx) tryDecodeBodyInOrder(
@@ -340,7 +340,7 @@ func (c *Ctx) Body() []byte {
340340
// rule defined at: https://www.rfc-editor.org/rfc/rfc9110#section-8.4-5
341341
encodingOrder = getSplicedStrList(headerEncoding, encodingOrder)
342342
if len(encodingOrder) == 0 {
343-
return c.fasthttp.Request.Body()
343+
return c.getBody()
344344
}
345345

346346
var decodesRealized uint8
@@ -354,6 +354,9 @@ func (c *Ctx) Body() []byte {
354354
return []byte(err.Error())
355355
}
356356

357+
if c.app.config.Immutable {
358+
return utils.CopyBytes(body)
359+
}
357360
return body
358361
}
359362

@@ -2004,3 +2007,11 @@ func (*Ctx) isLocalHost(address string) bool {
20042007
func (c *Ctx) IsFromLocal() bool {
20052008
return c.isLocalHost(c.fasthttp.RemoteIP().String())
20062009
}
2010+
2011+
func (c *Ctx) getBody() []byte {
2012+
if c.app.config.Immutable {
2013+
return utils.CopyBytes(c.fasthttp.Request.Body())
2014+
}
2015+
2016+
return c.fasthttp.Request.Body()
2017+
}

0 commit comments

Comments
Β (0)