修改bug

This commit is contained in:
chenjiekun
2023-03-03 12:29:50 +08:00
parent 144bb17ab6
commit a41a7aa848
3 changed files with 28 additions and 18 deletions

View File

@@ -1,6 +1,9 @@
package proxy_mirror
import "github.com/eolinker/apinto/utils"
import (
"github.com/eolinker/apinto/utils"
"strings"
)
type Config struct {
Addr string `json:"Addr" label:"服务地址" description:"镜像服务地址, 需要包含scheme"`
@@ -26,6 +29,9 @@ func (c *Config) doCheck() error {
if !utils.IsMatchSchemeIpPort(c.Addr) && !utils.IsMatchSchemeDomainPort(c.Addr) {
return errAddr
}
//scheme小写
schemeIdx := strings.Index(c.Addr, "://")
c.Addr = strings.ToLower(c.Addr[:schemeIdx]) + c.Addr[schemeIdx:]
//校验采样配置
if c.SampleConf.RandomRange <= 0 {
@@ -53,11 +59,12 @@ func (c *Config) doCheck() error {
}
//校验host
if c.PassHost == modeRewrite && c.Host == "" {
return errHostNull
}
if !utils.IsMatchIpPort(c.Addr) && !utils.IsMatchDomainPort(c.Addr) {
return errAddr
if c.PassHost == modeRewrite {
if c.Host == "" {
return errHostNull
} else if !utils.IsMatchIpPort(c.Host) && !utils.IsMatchDomainPort(c.Host) {
return errAddr
}
}
return nil

View File

@@ -3,7 +3,6 @@ package http_context
import (
"context"
"fmt"
"github.com/valyala/fasthttp"
"net"
"time"
@@ -19,10 +18,10 @@ var _ http_service.IHttpContext = (*cloneContext)(nil)
// HttpContext fasthttpRequestCtx
type cloneContext struct {
org *HttpContext
proxyRequest ProxyRequest
proxyRequests []http_service.IProxy
org *HttpContext
proxyRequest ProxyRequest
response Response
proxyRequests []http_service.IProxy
ctx context.Context
completeHandler eoscContext.CompleteHandler
finishHandler eoscContext.FinishHandler
@@ -115,7 +114,7 @@ func (ctx *cloneContext) Proxies() []http_service.IProxy {
}
func (ctx *cloneContext) Response() http_service.IResponse {
return nil
return &ctx.response
}
func (ctx *cloneContext) SendTo(address string, timeout time.Duration) error {
@@ -131,17 +130,17 @@ func (ctx *cloneContext) SendTo(address string, timeout time.Duration) error {
case eoscContext.ReWriteHost:
request.URI().SetHost(targetHost)
}
response := fasthttp.AcquireResponse()
beginTime := time.Now()
ctx.responseError = fasthttp_client.ProxyTimeout(address, request, response, timeout)
ctx.responseError = fasthttp_client.ProxyTimeout(address, request, ctx.response.Response, timeout)
agent := newRequestAgent(&ctx.proxyRequest, host, scheme, beginTime, time.Now())
if ctx.responseError != nil {
agent.setStatusCode(504)
} else {
agent.setStatusCode(response.StatusCode())
agent.setStatusCode(ctx.response.Response.StatusCode())
}
agent.setResponseLength(response.Header.ContentLength())
agent.setResponseLength(ctx.response.Response.Header.ContentLength())
ctx.proxyRequests = append(ctx.proxyRequests, agent)
return ctx.responseError
@@ -158,7 +157,7 @@ func (ctx *cloneContext) AcceptTime() time.Time {
}
func (ctx *cloneContext) Value(key interface{}) interface{} {
return ctx.org.Value(key)
return ctx.ctx.Value(key)
}
func (ctx *cloneContext) WithValue(key, val interface{}) {

View File

@@ -192,7 +192,11 @@ func (ctx *HttpContext) Clone() (eoscContext.EoContext, error) {
req := fasthttp.AcquireRequest()
ctx.fastHttpRequestCtx.Request.CopyTo(req)
resp := fasthttp.AcquireResponse()
ctx.fastHttpRequestCtx.Response.CopyTo(resp)
copyContext.proxyRequest.reset(req, ctx.requestReader.remoteAddr)
copyContext.response.reset(resp)
copyContext.completeHandler = ctx.completeHandler
copyContext.finishHandler = ctx.finishHandler
@@ -204,7 +208,7 @@ func (ctx *HttpContext) Clone() (eoscContext.EoContext, error) {
copyContext.labels = cloneLabels
//记录请求时间
copyContext.ctx = context.WithValue(ctx.Context(), copyKey, true)
copyContext.ctx = context.WithValue(ctx.Context(), http_service.KeyCloneCtx, true)
copyContext.WithValue(http_service.KeyHttpRetry, 0)
copyContext.WithValue(http_service.KeyHttpTimeout, time.Duration(0))
return copyContext, nil