Skip to content

Commit

Permalink
Fixed bug where rewrite would persist across requests
Browse files Browse the repository at this point in the history
  • Loading branch information
lonelycode committed Aug 7, 2016
1 parent a08ab75 commit bf920b6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.idea/
.vagrant/
middleware/e1d21f942ec746ed416ab97fe1bf07e8/
middleware/e1d21f942ec746ed416ab97fe1bf07e8_IGNORED/
testapps/
vendor/github.com/TykTechnologies_hotfix/
vendor/github.com/dgrijalva_hotfix/
Expand Down Expand Up @@ -44,4 +45,4 @@ tyk.dashboard.conf
tyk.benchmark.conf
tyk_elasticache.conf
coprocess/
middleware/python/
middleware/python/
11 changes: 7 additions & 4 deletions middleware_url_rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,10 @@ func (m *URLRewriteMiddleware) GetConfig() (interface{}, error) {
return nil, nil
}

func (m *URLRewriteMiddleware) CheckHostRewrite(newTarget string, r *http.Request) {
if strings.HasPrefix(strings.ToLower(newTarget), "https://") || strings.HasPrefix(strings.ToLower(newTarget), "http://") {
func (m *URLRewriteMiddleware) CheckHostRewrite(oldPath, newTarget string, r *http.Request) {
oldAsURL, _ := url.Parse(oldPath)
newAsURL, _ := url.Parse(newTarget)
if oldAsURL.Host != newAsURL.Host {
log.Debug("Detected a host rewrite in pattern!")
context.Set(r, RetainHost, true)
}
Expand All @@ -192,13 +194,14 @@ func (m *URLRewriteMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Req
log.Debug("Rewriter active")
thisMeta := meta.(*tykcommon.URLRewriteMeta)
log.Debug(r.URL)
oldPath := r.URL.String()
p, pErr := m.Rewriter.Rewrite(thisMeta, r.URL.String(), true, r)
if pErr != nil {
return pErr, 500
}

m.CheckHostRewrite(p, r)
m.CheckHostRewrite(oldPath, p, r)

newURL, uErr := url.Parse(p)
if uErr != nil {
log.Error("URL Rewrite failed, could not parse: ", p)
Expand Down
20 changes: 14 additions & 6 deletions tyk_reverse_proxy_clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ func TykNewSingleHostReverseProxy(target *url.URL, spec *APISpec) *ReverseProxy
}
}

var newTarget *url.URL
switchTargets := false
URLRewriteContainsTarget, found := context.GetOk(req, RetainHost)
if found {
if URLRewriteContainsTarget.(bool) {
Expand All @@ -163,17 +165,23 @@ func TykNewSingleHostReverseProxy(target *url.URL, spec *APISpec) *ReverseProxy
if pErr != nil {
log.Error("Failed to parse URL! Err: ", pErr)
} else {
target = tmpTarget
newTarget = tmpTarget
switchTargets = true
}
context.Clear(req)
}
}

// No override, and no load balancing? Use the existing target
req.URL.Scheme = target.Scheme
req.URL.Host = target.Host
req.URL.Path = singleJoiningSlash(target.Path, req.URL.Path)
var targetToUse *url.URL = target
if switchTargets {
targetToUse = newTarget
}
req.URL.Scheme = targetToUse.Scheme
req.URL.Host = targetToUse.Host
req.URL.Path = singleJoiningSlash(targetToUse.Path, req.URL.Path)
if !spec.Proxy.PreserveHostHeader {
req.Host = target.Host
req.Host = targetToUse.Host
}
if targetQuery == "" || req.URL.RawQuery == "" {
req.URL.RawQuery = targetQuery + req.URL.RawQuery
Expand Down Expand Up @@ -418,7 +426,7 @@ func (p *ReverseProxy) WrappedServeHTTP(rw http.ResponseWriter, req *http.Reques
outreq.Close = false

log.Debug("Outbound Request: ", outreq.URL.String())

// Do not modify outbound request headers if they are WS
if !IsWebsocket(outreq) {

Expand Down

0 comments on commit bf920b6

Please sign in to comment.