Skip to content

Commit

Permalink
v3: Consolidate Logic of Handling the Request Body (#3093)
Browse files Browse the repository at this point in the history
reduce redundant call
  • Loading branch information
ad3n committed Jul 27, 2024
1 parent 99173cc commit e437633
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

1 comment on commit e437633

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: e437633 Previous: 87bb93e Ratio
Benchmark_Middleware_BasicAuth - B/op 80 B/op 48 B/op 1.67
Benchmark_Middleware_BasicAuth - allocs/op 5 allocs/op 3 allocs/op 1.67
Benchmark_Middleware_BasicAuth_Upper - B/op 80 B/op 48 B/op 1.67
Benchmark_Middleware_BasicAuth_Upper - allocs/op 5 allocs/op 3 allocs/op 1.67
Benchmark_CORS_NewHandler - B/op 16 B/op 0 B/op +∞
Benchmark_CORS_NewHandler - allocs/op 1 allocs/op 0 allocs/op +∞
Benchmark_CORS_NewHandlerSingleOrigin - B/op 16 B/op 0 B/op +∞
Benchmark_CORS_NewHandlerSingleOrigin - allocs/op 1 allocs/op 0 allocs/op +∞
Benchmark_CORS_NewHandlerPreflight - B/op 104 B/op 0 B/op +∞
Benchmark_CORS_NewHandlerPreflight - allocs/op 5 allocs/op 0 allocs/op +∞
Benchmark_CORS_NewHandlerPreflightSingleOrigin - B/op 104 B/op 0 B/op +∞
Benchmark_CORS_NewHandlerPreflightSingleOrigin - allocs/op 5 allocs/op 0 allocs/op +∞
Benchmark_CORS_NewHandlerPreflightWildcard 1049 ns/op 104 B/op 5 allocs/op 691 ns/op 0 B/op 0 allocs/op 1.52
Benchmark_CORS_NewHandlerPreflightWildcard - ns/op 1049 ns/op 691 ns/op 1.52
Benchmark_CORS_NewHandlerPreflightWildcard - B/op 104 B/op 0 B/op +∞
Benchmark_CORS_NewHandlerPreflightWildcard - allocs/op 5 allocs/op 0 allocs/op +∞
Benchmark_Middleware_CSRF_Check - allocs/op 11 allocs/op 7 allocs/op 1.57
Benchmark_Middleware_CSRF_GenerateToken - B/op 510 B/op 326 B/op 1.56
Benchmark_Middleware_CSRF_GenerateToken - allocs/op 10 allocs/op 6 allocs/op 1.67

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.