Skip to content

Commit 2207c37

Browse files
arun0009Arun Gopalpuri
andauthored
use echo.GetPath for rewrite in proxy (#1548)
Co-authored-by: Arun Gopalpuri <[email protected]>
1 parent 6e7c7ce commit 2207c37

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

echo.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -606,12 +606,12 @@ func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
606606
h := NotFoundHandler
607607

608608
if e.premiddleware == nil {
609-
e.findRouter(r.Host).Find(r.Method, getPath(r), c)
609+
e.findRouter(r.Host).Find(r.Method, GetPath(r), c)
610610
h = c.Handler()
611611
h = applyMiddleware(h, e.middleware...)
612612
} else {
613613
h = func(c Context) error {
614-
e.findRouter(r.Host).Find(r.Method, getPath(r), c)
614+
e.findRouter(r.Host).Find(r.Method, GetPath(r), c)
615615
h := c.Handler()
616616
h = applyMiddleware(h, e.middleware...)
617617
return h(c)
@@ -817,7 +817,8 @@ func WrapMiddleware(m func(http.Handler) http.Handler) MiddlewareFunc {
817817
}
818818
}
819819

820-
func getPath(r *http.Request) string {
820+
// GetPath returns RawPath, if it's empty returns Path from URL
821+
func GetPath(r *http.Request) string {
821822
path := r.URL.RawPath
822823
if path == "" {
823824
path = r.URL.Path

middleware/proxy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func ProxyWithConfig(config ProxyConfig) echo.MiddlewareFunc {
224224

225225
// Rewrite
226226
for k, v := range config.rewriteRegex {
227-
replacer := captureTokens(k, req.URL.Path)
227+
replacer := captureTokens(k, echo.GetPath(req))
228228
if replacer != nil {
229229
req.URL.Path = replacer.Replace(v)
230230
}

middleware/proxy_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ func TestProxy(t *testing.T) {
104104
e.ServeHTTP(rec, req)
105105
assert.Equal(t, "/user/jack/order/1", req.URL.Path)
106106
assert.Equal(t, http.StatusOK, rec.Code)
107+
req.URL.Path = "/users/jill/orders/T%2FcO4lW%2Ft%2FVp%2F"
108+
e.ServeHTTP(rec, req)
109+
assert.Equal(t, "/user/jill/order/T%2FcO4lW%2Ft%2FVp%2F", req.URL.Path)
110+
assert.Equal(t, http.StatusOK, rec.Code)
107111

108112
// ProxyTarget is set in context
109113
contextObserver := func(next echo.HandlerFunc) echo.HandlerFunc {

0 commit comments

Comments
 (0)