mirror of
https://github.com/labstack/echo.git
synced 2025-01-12 01:22:21 +02:00
use echo.GetPath for rewrite in proxy (#1548)
Co-authored-by: Arun Gopalpuri <arun@gopalpuri.com>
This commit is contained in:
parent
6e7c7cea03
commit
2207c37bf8
7
echo.go
7
echo.go
@ -606,12 +606,12 @@ func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
h := NotFoundHandler
|
h := NotFoundHandler
|
||||||
|
|
||||||
if e.premiddleware == nil {
|
if e.premiddleware == nil {
|
||||||
e.findRouter(r.Host).Find(r.Method, getPath(r), c)
|
e.findRouter(r.Host).Find(r.Method, GetPath(r), c)
|
||||||
h = c.Handler()
|
h = c.Handler()
|
||||||
h = applyMiddleware(h, e.middleware...)
|
h = applyMiddleware(h, e.middleware...)
|
||||||
} else {
|
} else {
|
||||||
h = func(c Context) error {
|
h = func(c Context) error {
|
||||||
e.findRouter(r.Host).Find(r.Method, getPath(r), c)
|
e.findRouter(r.Host).Find(r.Method, GetPath(r), c)
|
||||||
h := c.Handler()
|
h := c.Handler()
|
||||||
h = applyMiddleware(h, e.middleware...)
|
h = applyMiddleware(h, e.middleware...)
|
||||||
return h(c)
|
return h(c)
|
||||||
@ -817,7 +817,8 @@ func WrapMiddleware(m func(http.Handler) http.Handler) MiddlewareFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getPath(r *http.Request) string {
|
// GetPath returns RawPath, if it's empty returns Path from URL
|
||||||
|
func GetPath(r *http.Request) string {
|
||||||
path := r.URL.RawPath
|
path := r.URL.RawPath
|
||||||
if path == "" {
|
if path == "" {
|
||||||
path = r.URL.Path
|
path = r.URL.Path
|
||||||
|
@ -224,7 +224,7 @@ func ProxyWithConfig(config ProxyConfig) echo.MiddlewareFunc {
|
|||||||
|
|
||||||
// Rewrite
|
// Rewrite
|
||||||
for k, v := range config.rewriteRegex {
|
for k, v := range config.rewriteRegex {
|
||||||
replacer := captureTokens(k, req.URL.Path)
|
replacer := captureTokens(k, echo.GetPath(req))
|
||||||
if replacer != nil {
|
if replacer != nil {
|
||||||
req.URL.Path = replacer.Replace(v)
|
req.URL.Path = replacer.Replace(v)
|
||||||
}
|
}
|
||||||
|
@ -104,6 +104,10 @@ func TestProxy(t *testing.T) {
|
|||||||
e.ServeHTTP(rec, req)
|
e.ServeHTTP(rec, req)
|
||||||
assert.Equal(t, "/user/jack/order/1", req.URL.Path)
|
assert.Equal(t, "/user/jack/order/1", req.URL.Path)
|
||||||
assert.Equal(t, http.StatusOK, rec.Code)
|
assert.Equal(t, http.StatusOK, rec.Code)
|
||||||
|
req.URL.Path = "/users/jill/orders/T%2FcO4lW%2Ft%2FVp%2F"
|
||||||
|
e.ServeHTTP(rec, req)
|
||||||
|
assert.Equal(t, "/user/jill/order/T%2FcO4lW%2Ft%2FVp%2F", req.URL.Path)
|
||||||
|
assert.Equal(t, http.StatusOK, rec.Code)
|
||||||
|
|
||||||
// ProxyTarget is set in context
|
// ProxyTarget is set in context
|
||||||
contextObserver := func(next echo.HandlerFunc) echo.HandlerFunc {
|
contextObserver := func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||||
|
Loading…
Reference in New Issue
Block a user