Skip to content

Commit

Permalink
πŸ› fix: Respect Immutable config for Body() (#3246)
Browse files Browse the repository at this point in the history
* πŸ› fix: respect Immutable config for Body()

* ci: add go 1.22 and 1.23 to test matrix
  • Loading branch information
nickajacks1 authored Dec 13, 2024
1 parent 8c84b0f commit 56ff2de
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
Build:
strategy:
matrix:
go-version: [1.17.x, 1.18.x, 1.19.x, 1.20.x, 1.21.x]
go-version: [1.17.x, 1.18.x, 1.19.x, 1.20.x, 1.21.x, 1.22.x, 1.23.x]
platform: [ubuntu-latest, windows-latest, macos-latest]
runs-on: ${{ matrix.platform }}
steps:
Expand Down
15 changes: 13 additions & 2 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (c *Ctx) 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 *Ctx) BodyRaw() []byte {
return c.fasthttp.Request.Body()
return c.getBody()
}

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

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

if c.app.config.Immutable {
return utils.CopyBytes(body)
}
return body
}

Expand Down Expand Up @@ -2004,3 +2007,11 @@ func (*Ctx) isLocalHost(address string) bool {
func (c *Ctx) IsFromLocal() bool {
return c.isLocalHost(c.fasthttp.RemoteIP().String())
}

func (c *Ctx) getBody() []byte {
if c.app.config.Immutable {
return utils.CopyBytes(c.fasthttp.Request.Body())
}

return c.fasthttp.Request.Body()
}

0 comments on commit 56ff2de

Please sign in to comment.