作者 tangxuhui

调整优化

... ... @@ -32,55 +32,12 @@ func (controller *ReverseProxyController) SuplusSaleApp() {
panic(err)
}
controller.Ctx.Request.URL.Path = strings.Replace(controller.Ctx.Request.URL.Path, "/suplus-sale-app", "", 1)
targetQuery := target.RawQuery
directorFunc := func(req *http.Request) {
req.Host = target.Host
req.URL.Scheme = target.Scheme
req.URL.Host = target.Host
req.URL.Path, req.URL.RawPath = joinURLPath(target, controller.Ctx.Request.URL)
if targetQuery == "" || req.URL.RawQuery == "" {
req.URL.RawQuery = targetQuery + req.URL.RawQuery
} else {
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
newProxy := httputil.NewSingleHostReverseProxy(target)
directorFunc := newProxy.Director
newDirectorFunc := func(r *http.Request) {
directorFunc(r)
r.Host = target.Host
}
if _, ok := req.Header["User-Agent"]; !ok {
// explicitly disable User-Agent so it's not set to default value
req.Header.Set("User-Agent", "")
}
}
newProxy := &httputil.ReverseProxy{Director: directorFunc}
newProxy.Director = newDirectorFunc
newProxy.ServeHTTP(controller.Ctx.ResponseWriter, controller.Ctx.Request)
}
func joinURLPath(a, b *url.URL) (path, rawpath string) {
if a.RawPath == "" && b.RawPath == "" {
return singleJoiningSlash(a.Path, b.Path), ""
}
// Same as singleJoiningSlash, but uses EscapedPath to determine
// whether a slash should be added
apath := a.EscapedPath()
bpath := b.EscapedPath()
aslash := strings.HasSuffix(apath, "/")
bslash := strings.HasPrefix(bpath, "/")
switch {
case aslash && bslash:
return a.Path + b.Path[1:], apath + bpath[1:]
case !aslash && !bslash:
return a.Path + "/" + b.Path, apath + "/" + bpath
}
return a.Path + b.Path, apath + bpath
}
func singleJoiningSlash(a, b string) string {
aslash := strings.HasSuffix(a, "/")
bslash := strings.HasPrefix(b, "/")
switch {
case aslash && bslash:
return a + b[1:]
case !aslash && !bslash:
return a + "/" + b
}
return a + b
}
... ...