Browse Source

auto update script executed

Toby Chui 1 year ago
parent
commit
37aa111c9a
1 changed files with 44 additions and 8 deletions
  1. 44 8
      mod/dynamicproxy/dpcore/dpcore.go

+ 44 - 8
mod/dynamicproxy/dpcore/dpcore.go

@@ -71,23 +71,23 @@ type requestCanceler interface {
 }
 
 func NewDynamicProxyCore(target *url.URL, prepender string) *ReverseProxy {
-	targetQuery := target.RawQuery
 	director := func(req *http.Request) {
-		fmt.Println(target.Host, target.Path, req.URL.Path)
+		targetQuery := target.RawQuery
 
 		req.URL.Scheme = target.Scheme
-		//req.URL.Host = target.Host
-		req.URL.Path = singleJoiningSlash(target.Path, req.URL.Path)
 
-		// If Host is empty, the Request.Write method uses
-		// the value of URL.Host.
-		// force use URL.Host
-		//req.Host = req.URL.Host
+		req.URL.Host = target.Host
+
+		req.URL.Path, req.URL.RawPath = joinURLPath(target, req.URL)
 
 		if targetQuery == "" || req.URL.RawQuery == "" {
+
 			req.URL.RawQuery = targetQuery + req.URL.RawQuery
+
 		} else {
+
 			req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
+
 		}
 
 		if _, ok := req.Header["User-Agent"]; !ok {
@@ -115,6 +115,42 @@ func singleJoiningSlash(a, b string) string {
 	return a + b
 }
 
+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 copyHeader(dst, src http.Header) {
 	for k, vv := range src {
 		for _, v := range vv {