修复fasthttp copy请求体截断的问题

This commit is contained in:
Liujian
2023-03-09 16:40:55 +08:00
parent 9d9e99bcab
commit 102f14daf9
5 changed files with 22 additions and 15 deletions

View File

@@ -4,11 +4,12 @@ import (
"encoding/json"
"errors"
"fmt"
"time"
hessian "github.com/apache/dubbo-go-hessian2"
"github.com/eolinker/eosc/eocontext"
http_service "github.com/eolinker/eosc/eocontext/http-context"
"github.com/eolinker/eosc/log"
"time"
)
var (
@@ -46,7 +47,7 @@ func (c *Complete) Complete(org eocontext.EoContext) error {
ctx.Response().SetResponseTime(time.Now().Sub(proxyTime))
ctx.SetLabel("handler", "proxy")
}()
body, _ := ctx.Request().Body().RawBody()
body, _ := ctx.Proxy().Body().RawBody()
var types []string
var valuesList []hessian.Object

View File

@@ -20,11 +20,10 @@ func newMirrorHandler(eoCtx eocontext.EoContext, service *mirrorService) (eocont
}
func (p *proxyMirrorCompleteHandler) Complete(ctx eocontext.EoContext) error {
cloneCtx, err := ctx.Clone()
//先执行原始Complete, 再执行镜像请求的Complete
orgErr := p.orgComplete.Complete(ctx)
cloneCtx, err := ctx.Clone()
if err != nil {
log.Warn(err)
return orgErr

View File

@@ -67,7 +67,6 @@ func (m *Manager) Delete(id string) {
}
func (m *Manager) FastHandler(port int, ctx *fasthttp.RequestCtx) {
ctx.Request.Body()
httpContext := http_context.NewContext(ctx, port)
if m.matcher == nil {
httpContext.SetFinish(notFound)

View File

@@ -190,10 +190,12 @@ func (ctx *HttpContext) Clone() (eoscContext.EoContext, error) {
copyContext.proxyRequests = make([]http_service.IProxy, 0, 2)
req := fasthttp.AcquireRequest()
ctx.fastHttpRequestCtx.Request.CopyTo(req)
// 当body未读取调用Body方法读出stream中当所有body内容避免请求体被截断
ctx.proxyRequest.req.Body()
ctx.proxyRequest.req.CopyTo(req)
resp := fasthttp.AcquireResponse()
ctx.fastHttpRequestCtx.Response.CopyTo(resp)
//ctx.fastHttpRequestCtx.Response.CopyTo(resp)
copyContext.proxyRequest.reset(req, ctx.requestReader.remoteAddr)
copyContext.response.reset(resp)
@@ -224,7 +226,12 @@ func NewContext(ctx *fasthttp.RequestCtx, port int) *HttpContext {
httpContext.fastHttpRequestCtx = ctx
httpContext.requestID = uuid.New().String()
httpContext.requestReader.reset(&ctx.Request, remoteAddr)
// 原始请求最大读取body为8k使用clone request
request := fasthttp.AcquireRequest()
ctx.Request.CopyTo(request)
httpContext.requestReader.reset(request, remoteAddr)
// proxyRequest保留原始请求
httpContext.proxyRequest.reset(&ctx.Request, remoteAddr)
httpContext.proxyRequests = httpContext.proxyRequests[:0]
httpContext.response.reset(&ctx.Response)

View File

@@ -3,6 +3,7 @@ package http_context
import (
"bytes"
"fmt"
"github.com/eolinker/eosc/log"
http_service "github.com/eolinker/eosc/eocontext/http-context"
@@ -44,23 +45,23 @@ var (
)
func (r *ProxyRequest) reset(request *fasthttp.Request, remoteAddr string) {
proxyRequest := fasthttp.AcquireRequest()
request.CopyTo(proxyRequest)
forwardedFor := proxyRequest.Header.PeekBytes(xforwardedforKey)
//proxyRequest := fasthttp.AcquireRequest()
//request.CopyTo(proxyRequest)
r.req = request
forwardedFor := r.req.Header.PeekBytes(xforwardedforKey)
if len(forwardedFor) > 0 {
if i := bytes.IndexByte(forwardedFor, ','); i > 0 {
r.realIP = string(forwardedFor[:i])
} else {
r.realIP = string(forwardedFor)
}
proxyRequest.Header.Set("x-forwarded-for", fmt.Sprint(string(forwardedFor), ",", r.remoteAddr))
r.req.Header.Set("x-forwarded-for", fmt.Sprint(string(forwardedFor), ",", r.remoteAddr))
} else {
proxyRequest.Header.Set("x-forwarded-for", r.remoteAddr)
r.req.Header.Set("x-forwarded-for", r.remoteAddr)
r.realIP = r.remoteAddr
}
r.RequestReader.reset(proxyRequest, remoteAddr)
r.RequestReader.reset(r.req, remoteAddr)
}
//func NewProxyRequest(request *fasthttp.Request, remoteAddr string) *ProxyRequest {