作者 tangxuhui

调整优化

@@ -32,55 +32,12 @@ func (controller *ReverseProxyController) SuplusSaleApp() { @@ -32,55 +32,12 @@ func (controller *ReverseProxyController) SuplusSaleApp() {
32 panic(err) 32 panic(err)
33 } 33 }
34 controller.Ctx.Request.URL.Path = strings.Replace(controller.Ctx.Request.URL.Path, "/suplus-sale-app", "", 1) 34 controller.Ctx.Request.URL.Path = strings.Replace(controller.Ctx.Request.URL.Path, "/suplus-sale-app", "", 1)
35 - targetQuery := target.RawQuery  
36 - directorFunc := func(req *http.Request) {  
37 - req.Host = target.Host  
38 - req.URL.Scheme = target.Scheme  
39 - req.URL.Host = target.Host  
40 - req.URL.Path, req.URL.RawPath = joinURLPath(target, controller.Ctx.Request.URL)  
41 - if targetQuery == "" || req.URL.RawQuery == "" {  
42 - req.URL.RawQuery = targetQuery + req.URL.RawQuery  
43 - } else {  
44 - req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery  
45 - }  
46 - if _, ok := req.Header["User-Agent"]; !ok {  
47 - // explicitly disable User-Agent so it's not set to default value  
48 - req.Header.Set("User-Agent", "")  
49 - } 35 + newProxy := httputil.NewSingleHostReverseProxy(target)
  36 + directorFunc := newProxy.Director
  37 + newDirectorFunc := func(r *http.Request) {
  38 + directorFunc(r)
  39 + r.Host = target.Host
50 } 40 }
51 - newProxy := &httputil.ReverseProxy{Director: directorFunc} 41 + newProxy.Director = newDirectorFunc
52 newProxy.ServeHTTP(controller.Ctx.ResponseWriter, controller.Ctx.Request) 42 newProxy.ServeHTTP(controller.Ctx.ResponseWriter, controller.Ctx.Request)
53 } 43 }
54 -  
55 -func joinURLPath(a, b *url.URL) (path, rawpath string) {  
56 - if a.RawPath == "" && b.RawPath == "" {  
57 - return singleJoiningSlash(a.Path, b.Path), ""  
58 - }  
59 - // Same as singleJoiningSlash, but uses EscapedPath to determine  
60 - // whether a slash should be added  
61 - apath := a.EscapedPath()  
62 - bpath := b.EscapedPath()  
63 -  
64 - aslash := strings.HasSuffix(apath, "/")  
65 - bslash := strings.HasPrefix(bpath, "/")  
66 -  
67 - switch {  
68 - case aslash && bslash:  
69 - return a.Path + b.Path[1:], apath + bpath[1:]  
70 - case !aslash && !bslash:  
71 - return a.Path + "/" + b.Path, apath + "/" + bpath  
72 - }  
73 - return a.Path + b.Path, apath + bpath  
74 -}  
75 -  
76 -func singleJoiningSlash(a, b string) string {  
77 - aslash := strings.HasSuffix(a, "/")  
78 - bslash := strings.HasPrefix(b, "/")  
79 - switch {  
80 - case aslash && bslash:  
81 - return a + b[1:]  
82 - case !aslash && !bslash:  
83 - return a + "/" + b  
84 - }  
85 - return a + b  
86 -}