diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a186908aa0..00566dd9c0 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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: diff --git a/ctx.go b/ctx.go index 6f414ed56a..c7caada702 100644 --- a/ctx.go +++ b/ctx.go @@ -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( @@ -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 @@ -354,6 +354,9 @@ func (c *Ctx) Body() []byte { return []byte(err.Error()) } + if c.app.config.Immutable { + return utils.CopyBytes(body) + } return body } @@ -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() +}