将request和bodyHandler放在要使用时初始化

This commit is contained in:
Liujian
2021-08-09 15:46:28 +08:00
parent abd6902271
commit 3fffbddc6d
7 changed files with 23 additions and 16 deletions

View File

@@ -22,7 +22,7 @@ type Context struct {
LogFields *access_field.Fields
request IRequest
labels map[string]string
BodyHandler *BodyRequestHandler
bodyHandler *BodyRequestHandler
}
//NewContext 创建Context
@@ -30,15 +30,12 @@ func NewContext(ctx *fasthttp.RequestCtx) *Context {
id := uuid.NewV4()
requestID := id.String()
newRequest := &ctx.Request
req := NewRequest(ctx.Request)
newCtx := &Context{
context: ctx,
requestOrg: fasthttp.AcquireRequest(),
proxyRequest: fasthttp.AcquireRequest(),
request: req,
requestID: requestID,
LogFields: access_field.NewFields(),
BodyHandler: NewBodyRequestHandler(req.contentType, ctx.Request.Body()),
}
newRequest.CopyTo(newCtx.requestOrg)
newRequest.CopyTo(newCtx.proxyRequest)
@@ -75,6 +72,9 @@ func (ctx *Context) RequestId() string {
}
func (ctx *Context) Request() IRequest {
if ctx.request == nil {
ctx.request = newRequest(ctx.requestOrg)
}
return ctx.request
}
@@ -90,6 +90,14 @@ func (ctx *Context) ProxyResponse() *fasthttp.Response {
return ctx.proxyResponse
}
func (ctx *Context) BodyHandler() *BodyRequestHandler {
if ctx.bodyHandler == nil {
r := ctx.Request()
ctx.bodyHandler = newBodyRequestHandler(r.ContentType(), r.RawBody())
}
return ctx.bodyHandler
}
func (ctx *Context) SetBody(body []byte) {
ctx.context.SetBody(body)
}