From 7de5c8b5445216f397af5fc4cc6f5cfda465c4e4 Mon Sep 17 00:00:00 2001 From: "leitao.zhang" Date: Tue, 17 Nov 2020 11:15:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=97=A0=E6=B3=95=E4=BC=A0?= =?UTF-8?q?=E9=80=92url=E5=8F=82=E6=95=B0=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/handler/proxyhandler.go | 53 ++++++++++++++++++++----------------- src/main.go | 2 +- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/handler/proxyhandler.go b/src/handler/proxyhandler.go index a50b008..a9c9d21 100644 --- a/src/handler/proxyhandler.go +++ b/src/handler/proxyhandler.go @@ -77,6 +77,15 @@ func Proxy(c *gin.Context) { //如果是静态文件 if CheckStaticFile(c.Request.URL.Path) { host := getHost(proxyData.ReqUrl) + //var prefix string + //if apiId == "" { + // prefix= fmt.Sprintf("%s/%s/service/%s/%s/", config.Prefix, config.MeshId,applyId,apiId) + //}else { + // prefix= fmt.Sprintf("%s/%s/service/%s/", config.Prefix, config.MeshId,applyId) + //} + // + //staticPath:= strings.ReplaceAll(c.Request.URL.Path,prefix,"") + fwd, _ := forward.New(forward.PassHostHeader(false)) c.Request.URL = testutils.ParseURI(fmt.Sprintf("%s%s", host, c.Request.URL.Path)) c.Request.RequestURI = getRequestURI(c.Request) @@ -86,7 +95,7 @@ func Proxy(c *gin.Context) { roundTripper := http.DefaultTransport.(*http.Transport) //roundTripper.ForceAttemptHTTP2 = false roundTripper.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} - f, err := forward.New(forward.PassHostHeader(false), forward.RoundTripper(roundTripper), forward.ResponseModifier(func(resp *http.Response) error { + forwarder, err := forward.New(forward.PassHostHeader(false), forward.RoundTripper(roundTripper), forward.ResponseModifier(func(resp *http.Response) error { respbody, err1 := ioutil.ReadAll(resp.Body) if err1 != nil { log.Println(err1) @@ -148,15 +157,25 @@ func Proxy(c *gin.Context) { return nil })) if err != nil { - + c.JSON(500, errors.New("请求失败")) + return + } + reqURL := testutils.ParseURI(proxyData.ReqUrl) + if reqURL.RawQuery == "" { + reqURL.RawQuery = c.Request.URL.RawQuery + } else { + if c.Request.URL.RawQuery != "" { + reqURL.RawQuery = "&" + c.Request.URL.RawQuery + } } - c.Request.URL = testutils.ParseURI(proxyData.ReqUrl) + + c.Request.URL = reqURL fmt.Println("c.Request.URL-------", c.Request.URL) - c.Request.RequestURI = getRequestURI(c.Request) + c.Request.RequestURI = reqURL.RequestURI() fmt.Println("c.Request-------", c.Request) c.Request.Host = getHost(proxyData.ReqUrl) fmt.Println("proxyData.RealUrl-------", proxyData.ReqUrl) - f.ServeHTTP(c.Writer, c.Request) + forwarder.ServeHTTP(c.Writer, c.Request) } } @@ -169,26 +188,12 @@ func CheckStaticFile(path string) bool { return false } -func getProxyURL(proxyData model.ProxyData, req *http.Request) string { - realPath := proxyData.ReqUrl - rawQuery := req.URL.RawQuery - var result = "" - if rawQuery == "" { - if proxyData.DataServiceType1 == 6 { - result = realPath + "?f=json" - } else { - result = realPath - } - } else { - result = realPath + "?" + rawQuery - if proxyData.DataServiceType1 == 6 && !strings.Contains(rawQuery, "f=json") { - result = result + "&f=json" - } - } - return result -} - func getRequestURI(req *http.Request) string { + + //requestURI, _ := url.ParseRequestURI(req.RequestURI) + // + //requestURI.RawQuery + path := req.URL.Path rawQuery := req.URL.RawQuery var result = "" diff --git a/src/main.go b/src/main.go index 75927c1..e178e30 100644 --- a/src/main.go +++ b/src/main.go @@ -18,7 +18,7 @@ import ( var ( argPort = pflag.Int("port", 8011, "") argPrefix = pflag.String("prefix", "/bgmesh/fiddler", "") - meshId = pflag.String("meshId", "133", "") + meshId = pflag.String("meshId", "461", "") redisUrl = pflag.String("redisUrl", "redis://apaas-redis:6379", "") redisTag = pflag.String("redisTag", "apaas-mesh-proxy", "") confPath = pflag.String("confPath", "/app/config/proxy.json", "") -- 2.26.0