|
@@ -7,7 +7,6 @@ import (
|
|
|
"net"
|
|
|
"net/http"
|
|
|
"net/url"
|
|
|
- "path/filepath"
|
|
|
"strings"
|
|
|
"sync"
|
|
|
"time"
|
|
@@ -61,6 +60,10 @@ type ReverseProxy struct {
|
|
|
Verbal bool
|
|
|
}
|
|
|
|
|
|
+type ResponseRewriteRuleSet struct {
|
|
|
+ ProxyDomain string
|
|
|
+}
|
|
|
+
|
|
|
type requestCanceler interface {
|
|
|
CancelRequest(req *http.Request)
|
|
|
}
|
|
@@ -229,7 +232,7 @@ func addXForwardedForHeader(req *http.Request) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func (p *ReverseProxy) ProxyHTTP(rw http.ResponseWriter, req *http.Request) error {
|
|
|
+func (p *ReverseProxy) ProxyHTTP(rw http.ResponseWriter, req *http.Request, rrr *ResponseRewriteRuleSet) error {
|
|
|
transport := p.Transport
|
|
|
if transport == nil {
|
|
|
transport = http.DefaultTransport
|
|
@@ -296,8 +299,20 @@ func (p *ReverseProxy) ProxyHTTP(rw http.ResponseWriter, req *http.Request) erro
|
|
|
|
|
|
//Custom header rewriter functions
|
|
|
if res.Header.Get("Location") != "" {
|
|
|
+ locationRewrite := res.Header.Get("Location")
|
|
|
+
|
|
|
+ domainWithoutPort := rrr.ProxyDomain
|
|
|
+ if strings.Contains(rrr.ProxyDomain, ":") {
|
|
|
+ //Split the port away
|
|
|
+ domainWithoutPort = strings.Split(rrr.ProxyDomain, ":")[0]
|
|
|
+ }
|
|
|
+
|
|
|
+ //Trim away the source headers if exists
|
|
|
+ locationRewrite = strings.TrimPrefix(locationRewrite, "http://"+rrr.ProxyDomain)
|
|
|
+ locationRewrite = strings.TrimPrefix(locationRewrite, "http://"+domainWithoutPort)
|
|
|
+
|
|
|
//Custom redirection to this rproxy relative path
|
|
|
- res.Header.Set("Location", filepath.ToSlash(filepath.Join(p.Prepender, res.Header.Get("Location"))))
|
|
|
+ res.Header.Set("Location", locationRewrite)
|
|
|
}
|
|
|
// Copy header from response to client.
|
|
|
copyHeader(rw.Header(), res.Header)
|
|
@@ -403,12 +418,12 @@ func (p *ReverseProxy) ProxyHTTPS(rw http.ResponseWriter, req *http.Request) err
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) error {
|
|
|
+func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request, rrr *ResponseRewriteRuleSet) error {
|
|
|
if req.Method == "CONNECT" {
|
|
|
err := p.ProxyHTTPS(rw, req)
|
|
|
return err
|
|
|
} else {
|
|
|
- err := p.ProxyHTTP(rw, req)
|
|
|
+ err := p.ProxyHTTP(rw, req, rrr)
|
|
|
return err
|
|
|
}
|
|
|
}
|