From 9435b12f96651533cd89bf4eed527feb758dec17 Mon Sep 17 00:00:00 2001 From: luominggang Date: Wed, 9 Jan 2019 10:56:36 +0800 Subject: [PATCH] fix rewrite --- http/httpProxy.go | 6 +++++- http/httpProxy_test.go | 9 ++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/http/httpProxy.go b/http/httpProxy.go index 4f836316..37e1795a 100644 --- a/http/httpProxy.go +++ b/http/httpProxy.go @@ -162,7 +162,11 @@ func (r *rewriteRule) rewrite(uri string) (string, bool) { ruleMatched = !ruleMatched } if ruleMatched { - return string(r.pattern.ExpandString(nil, r.replace, uri, r.pattern.FindStringSubmatchIndex(uri))), true + matchedIndex := r.pattern.FindStringSubmatchIndex(uri) + if matchedIndex == nil { + return uri, false + } + return string(r.pattern.ExpandString(nil, r.replace, uri, matchedIndex)), true } return uri, false } diff --git a/http/httpProxy_test.go b/http/httpProxy_test.go index 04a05a95..d59c74b5 100644 --- a/http/httpProxy_test.go +++ b/http/httpProxy_test.go @@ -1,8 +1,10 @@ package http import ( - "github.com/stretchr/testify/assert" + "fmt" "testing" + + "github.com/stretchr/testify/assert" ) func TestNewLocationMatcher(t *testing.T) { @@ -10,6 +12,7 @@ func TestNewLocationMatcher(t *testing.T) { {Upstream: "test1", Match: "/", Type: "start", RewriteRules: []string{"exact /Test2/1 /(.*) /test"}}, {Upstream: "test2", Match: "/test2/.*", Type: "regexp", RewriteRules: []string{"!iregexp ^/Test2/1/.* ^/test2/(.*) /test/$1"}}, {Upstream: "test3", Match: "/test3/.*", Type: "iregexp", RewriteRules: []string{"start / ^/(.*) /test/$1"}}, + {Upstream: "test4", Match: "^(/|/2/)(p1|p2).*", Type: "regexp", RewriteRules: []string{"start / ^/(p1|p2)/(.*) /2/$1/$2"}}, }) service := "" @@ -28,4 +31,8 @@ func TestNewLocationMatcher(t *testing.T) { service, rewritePath, _ = matcher.Pick("/Test2/1", true) assert.Equal(t, "test1", service) assert.Equal(t, "/test", rewritePath) + fmt.Println(matcher.Pick("/p1/test", true)) + fmt.Println(matcher.Pick("/p2/test", true)) + fmt.Println(matcher.Pick("/2/p1/test", true)) + fmt.Println(matcher.Pick("/2/p2/test", true)) }