如果Header中Location字段含有跟源站一样的地址,则自动修改为当前域名

This commit is contained in:
刘祥超
2022-04-09 20:37:05 +08:00
parent 3f621c5af0
commit 8c4e7129f3
2 changed files with 19 additions and 2 deletions

View File

@@ -279,6 +279,22 @@ func (this *HTTPRequest) doReverseProxy() {
}
}
// 替换Location中的源站地址
var locationHeader = resp.Header.Get("Location")
if len(locationHeader) > 0 {
locationURL, err := url.Parse(locationHeader)
if err == nil && (locationURL.Host == originAddr || strings.HasPrefix(originAddr, locationURL.Host+":")) {
locationURL.Host = this.ReqHost
if this.IsHTTP {
locationURL.Scheme = "http"
} else if this.IsHTTPS {
locationURL.Scheme = "https"
}
resp.Header.Set("Location", locationURL.String())
}
}
// 响应Header
this.writer.AddHeaders(resp.Header)
this.processResponseHeaders(resp.StatusCode)