mirror of
https://github.com/eolinker/apinto
synced 2025-10-04 08:26:35 +08:00
修复fasthttp copy请求体截断的问题
This commit is contained in:
@@ -4,11 +4,12 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
hessian "github.com/apache/dubbo-go-hessian2"
|
hessian "github.com/apache/dubbo-go-hessian2"
|
||||||
"github.com/eolinker/eosc/eocontext"
|
"github.com/eolinker/eosc/eocontext"
|
||||||
http_service "github.com/eolinker/eosc/eocontext/http-context"
|
http_service "github.com/eolinker/eosc/eocontext/http-context"
|
||||||
"github.com/eolinker/eosc/log"
|
"github.com/eolinker/eosc/log"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -46,7 +47,7 @@ func (c *Complete) Complete(org eocontext.EoContext) error {
|
|||||||
ctx.Response().SetResponseTime(time.Now().Sub(proxyTime))
|
ctx.Response().SetResponseTime(time.Now().Sub(proxyTime))
|
||||||
ctx.SetLabel("handler", "proxy")
|
ctx.SetLabel("handler", "proxy")
|
||||||
}()
|
}()
|
||||||
body, _ := ctx.Request().Body().RawBody()
|
body, _ := ctx.Proxy().Body().RawBody()
|
||||||
|
|
||||||
var types []string
|
var types []string
|
||||||
var valuesList []hessian.Object
|
var valuesList []hessian.Object
|
||||||
|
@@ -20,11 +20,10 @@ func newMirrorHandler(eoCtx eocontext.EoContext, service *mirrorService) (eocont
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *proxyMirrorCompleteHandler) Complete(ctx eocontext.EoContext) error {
|
func (p *proxyMirrorCompleteHandler) Complete(ctx eocontext.EoContext) error {
|
||||||
cloneCtx, err := ctx.Clone()
|
|
||||||
|
|
||||||
//先执行原始Complete, 再执行镜像请求的Complete
|
//先执行原始Complete, 再执行镜像请求的Complete
|
||||||
orgErr := p.orgComplete.Complete(ctx)
|
orgErr := p.orgComplete.Complete(ctx)
|
||||||
|
cloneCtx, err := ctx.Clone()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn(err)
|
log.Warn(err)
|
||||||
return orgErr
|
return orgErr
|
||||||
|
@@ -67,7 +67,6 @@ func (m *Manager) Delete(id string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Manager) FastHandler(port int, ctx *fasthttp.RequestCtx) {
|
func (m *Manager) FastHandler(port int, ctx *fasthttp.RequestCtx) {
|
||||||
ctx.Request.Body()
|
|
||||||
httpContext := http_context.NewContext(ctx, port)
|
httpContext := http_context.NewContext(ctx, port)
|
||||||
if m.matcher == nil {
|
if m.matcher == nil {
|
||||||
httpContext.SetFinish(notFound)
|
httpContext.SetFinish(notFound)
|
||||||
|
@@ -190,10 +190,12 @@ func (ctx *HttpContext) Clone() (eoscContext.EoContext, error) {
|
|||||||
copyContext.proxyRequests = make([]http_service.IProxy, 0, 2)
|
copyContext.proxyRequests = make([]http_service.IProxy, 0, 2)
|
||||||
|
|
||||||
req := fasthttp.AcquireRequest()
|
req := fasthttp.AcquireRequest()
|
||||||
ctx.fastHttpRequestCtx.Request.CopyTo(req)
|
// 当body未读取,调用Body方法读出stream中当所有body内容,避免请求体被截断
|
||||||
|
ctx.proxyRequest.req.Body()
|
||||||
|
ctx.proxyRequest.req.CopyTo(req)
|
||||||
|
|
||||||
resp := fasthttp.AcquireResponse()
|
resp := fasthttp.AcquireResponse()
|
||||||
ctx.fastHttpRequestCtx.Response.CopyTo(resp)
|
//ctx.fastHttpRequestCtx.Response.CopyTo(resp)
|
||||||
|
|
||||||
copyContext.proxyRequest.reset(req, ctx.requestReader.remoteAddr)
|
copyContext.proxyRequest.reset(req, ctx.requestReader.remoteAddr)
|
||||||
copyContext.response.reset(resp)
|
copyContext.response.reset(resp)
|
||||||
@@ -224,7 +226,12 @@ func NewContext(ctx *fasthttp.RequestCtx, port int) *HttpContext {
|
|||||||
httpContext.fastHttpRequestCtx = ctx
|
httpContext.fastHttpRequestCtx = ctx
|
||||||
httpContext.requestID = uuid.New().String()
|
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.proxyRequest.reset(&ctx.Request, remoteAddr)
|
||||||
httpContext.proxyRequests = httpContext.proxyRequests[:0]
|
httpContext.proxyRequests = httpContext.proxyRequests[:0]
|
||||||
httpContext.response.reset(&ctx.Response)
|
httpContext.response.reset(&ctx.Response)
|
||||||
|
@@ -3,6 +3,7 @@ package http_context
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/eolinker/eosc/log"
|
"github.com/eolinker/eosc/log"
|
||||||
|
|
||||||
http_service "github.com/eolinker/eosc/eocontext/http-context"
|
http_service "github.com/eolinker/eosc/eocontext/http-context"
|
||||||
@@ -44,23 +45,23 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (r *ProxyRequest) reset(request *fasthttp.Request, remoteAddr string) {
|
func (r *ProxyRequest) reset(request *fasthttp.Request, remoteAddr string) {
|
||||||
proxyRequest := fasthttp.AcquireRequest()
|
//proxyRequest := fasthttp.AcquireRequest()
|
||||||
request.CopyTo(proxyRequest)
|
//request.CopyTo(proxyRequest)
|
||||||
|
r.req = request
|
||||||
forwardedFor := proxyRequest.Header.PeekBytes(xforwardedforKey)
|
forwardedFor := r.req.Header.PeekBytes(xforwardedforKey)
|
||||||
if len(forwardedFor) > 0 {
|
if len(forwardedFor) > 0 {
|
||||||
if i := bytes.IndexByte(forwardedFor, ','); i > 0 {
|
if i := bytes.IndexByte(forwardedFor, ','); i > 0 {
|
||||||
r.realIP = string(forwardedFor[:i])
|
r.realIP = string(forwardedFor[:i])
|
||||||
} else {
|
} else {
|
||||||
r.realIP = string(forwardedFor)
|
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 {
|
} else {
|
||||||
proxyRequest.Header.Set("x-forwarded-for", r.remoteAddr)
|
r.req.Header.Set("x-forwarded-for", r.remoteAddr)
|
||||||
r.realIP = 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 {
|
//func NewProxyRequest(request *fasthttp.Request, remoteAddr string) *ProxyRequest {
|
||||||
|
Reference in New Issue
Block a user