mirror of
https://github.com/kataras/iris.git
synced 2025-03-14 10:56:29 +01:00
update host.ProxyHandler to compatiable with different host in target url
This commit is contained in:
parent
f4989bd5aa
commit
b6ebc39b9e
|
@ -12,20 +12,27 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// ProxyHandler returns a new ReverseProxy that rewrites
|
// ProxyHandler returns a new ReverseProxy that rewrites
|
||||||
// URLs to the scheme, host, and base path provided in target. If the
|
// URLs to the scheme, host, and base path provided in target.
|
||||||
// target's path is "/base" and the incoming request was for "/dir",
|
// 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.
|
// 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.
|
// Relative to httputil.NewSingleHostReverseProxy with some additions.
|
||||||
func ProxyHandler(target *url.URL) *httputil.ReverseProxy {
|
func ProxyHandler(target *url.URL) *httputil.ReverseProxy {
|
||||||
targetQuery := target.RawQuery
|
targetQuery := target.RawQuery
|
||||||
director := func(req *http.Request) {
|
director := func(req *http.Request) {
|
||||||
req.URL.Scheme = target.Scheme
|
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.URL.Host = target.Host
|
||||||
req.Host = target.Host
|
req.Host = target.Host
|
||||||
|
|
||||||
req.URL.Path = path.Join(target.Path, req.URL.Path)
|
|
||||||
|
|
||||||
if targetQuery == "" || req.URL.RawQuery == "" {
|
if targetQuery == "" || req.URL.RawQuery == "" {
|
||||||
req.URL.RawQuery = targetQuery + req.URL.RawQuery
|
req.URL.RawQuery = targetQuery + req.URL.RawQuery
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user