mirror of
https://github.com/labstack/echo.git
synced 2025-01-26 03:20:08 +02:00
Fixes the uses of caret(^) in rewrite regex
This commit is contained in:
parent
d3245067e0
commit
84da507a2e
@ -59,7 +59,11 @@ func RewriteWithConfig(config RewriteConfig) echo.MiddlewareFunc {
|
|||||||
for k, v := range config.Rules {
|
for k, v := range config.Rules {
|
||||||
k = regexp.QuoteMeta(k)
|
k = regexp.QuoteMeta(k)
|
||||||
k = strings.Replace(k, `\*`, "(.*)", -1)
|
k = strings.Replace(k, `\*`, "(.*)", -1)
|
||||||
|
k = strings.Replace(k, `\^`, "^", -1)
|
||||||
k = k + "$"
|
k = k + "$"
|
||||||
|
if strings.HasPrefix(k, "/") {
|
||||||
|
k = "^" + k
|
||||||
|
}
|
||||||
config.rulesRegex[regexp.MustCompile(k)] = v
|
config.rulesRegex[regexp.MustCompile(k)] = v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,6 +18,10 @@ func TestRewrite(t *testing.T) {
|
|||||||
"/api/*": "/$1",
|
"/api/*": "/$1",
|
||||||
"/js/*": "/public/javascripts/$1",
|
"/js/*": "/public/javascripts/$1",
|
||||||
"/users/*/orders/*": "/user/$1/order/$2",
|
"/users/*/orders/*": "/user/$1/order/$2",
|
||||||
|
"/foo/*": "/v1/foo/$1",
|
||||||
|
"/v1/foo/*": "/v1/foo/$1",
|
||||||
|
"/v2/foo/*": "/v2/foo/$1",
|
||||||
|
"^/bar/*": "/foobar/$1",
|
||||||
},
|
},
|
||||||
}))
|
}))
|
||||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||||
@ -37,6 +41,18 @@ func TestRewrite(t *testing.T) {
|
|||||||
req.URL.Path = "/api/new users"
|
req.URL.Path = "/api/new users"
|
||||||
e.ServeHTTP(rec, req)
|
e.ServeHTTP(rec, req)
|
||||||
assert.Equal(t, "/new users", req.URL.Path)
|
assert.Equal(t, "/new users", req.URL.Path)
|
||||||
|
req.URL.Path = "/foo/bar"
|
||||||
|
e.ServeHTTP(rec, req)
|
||||||
|
assert.Equal(t, "/v1/foo/bar", req.URL.Path)
|
||||||
|
req.URL.Path = "/v1/foo/bar"
|
||||||
|
e.ServeHTTP(rec, req)
|
||||||
|
assert.Equal(t, "/v1/foo/bar", req.URL.Path)
|
||||||
|
req.URL.Path = "/v2/foo/bar"
|
||||||
|
e.ServeHTTP(rec, req)
|
||||||
|
assert.Equal(t, "/v2/foo/bar", req.URL.Path)
|
||||||
|
req.URL.Path = "/bar/baz"
|
||||||
|
e.ServeHTTP(rec, req)
|
||||||
|
assert.Equal(t, "/foobar/baz", req.URL.Path)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Issue #1086
|
// Issue #1086
|
||||||
|
Loading…
x
Reference in New Issue
Block a user