From b6ebc39b9e234bcd1c53ad48024d6523f8481bd8 Mon Sep 17 00:00:00 2001 From: tuhao Date: Mon, 11 Jan 2021 15:25:32 +0800 Subject: [PATCH] update host.ProxyHandler to compatiable with different host in target url --- core/host/proxy.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/core/host/proxy.go b/core/host/proxy.go index 0ddb353c..241f0436 100644 --- a/core/host/proxy.go +++ b/core/host/proxy.go @@ -12,20 +12,27 @@ import ( ) // ProxyHandler returns a new ReverseProxy that rewrites -// URLs to the scheme, host, and base path provided in target. If the -// target's path is "/base" and the incoming request was for "/dir", +// URLs to the scheme, host, and base path provided in target. +// Case 1: req.Host == target.Host +// If the target's path is "/base" and the incoming request was for "/dir", // the target request will be for /base/dir. -// +// Case 2: req.Host != target.Host +// the target request will be forwarded to the target's url // Relative to httputil.NewSingleHostReverseProxy with some additions. func ProxyHandler(target *url.URL) *httputil.ReverseProxy { targetQuery := target.RawQuery director := func(req *http.Request) { req.URL.Scheme = target.Scheme + + if req.Host != target.Host { + req.URL.Path = target.Path + } else { + req.URL.Path = path.Join(target.Path, req.URL.Path) + } + req.URL.Host = target.Host req.Host = target.Host - req.URL.Path = path.Join(target.Path, req.URL.Path) - if targetQuery == "" || req.URL.RawQuery == "" { req.URL.RawQuery = targetQuery + req.URL.RawQuery } else {