Skip to content

Commit 86b200f

Browse files
committed
Merge branch 'bugfix/1412-route-matchany-multilevel' of github.com:neotel-at/echo into bugfix/1412-route-matchany-multilevel
2 parents 7738dd3 + ccf6f1a commit 86b200f

File tree

6 files changed

+163
-64
lines changed

6 files changed

+163
-64
lines changed

echo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ const (
227227

228228
const (
229229
// Version of Echo
230-
Version = "4.1.10"
230+
Version = "4.1.11"
231231
website = "https://echo.labstack.com"
232232
// http://patorjk.com/software/taag/#p=display&f=Small%20Slant&t=Echo
233233
banner = `

middleware/proxy.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -92,24 +92,22 @@ func proxyRaw(t *ProxyTarget, c echo.Context) http.Handler {
9292
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
9393
in, _, err := c.Response().Hijack()
9494
if err != nil {
95-
c.Error(fmt.Errorf("proxy raw, hijack error=%v, url=%s", t.URL, err))
95+
c.Set("_error", fmt.Sprintf("proxy raw, hijack error=%v, url=%s", t.URL, err))
9696
return
9797
}
9898
defer in.Close()
9999

100100
out, err := net.Dial("tcp", t.URL.Host)
101101
if err != nil {
102-
he := echo.NewHTTPError(http.StatusBadGateway, fmt.Sprintf("proxy raw, dial error=%v, url=%s", t.URL, err))
103-
c.Error(he)
102+
c.Set("_error", echo.NewHTTPError(http.StatusBadGateway, fmt.Sprintf("proxy raw, dial error=%v, url=%s", t.URL, err)))
104103
return
105104
}
106105
defer out.Close()
107106

108107
// Write header
109108
err = r.Write(out)
110109
if err != nil {
111-
he := echo.NewHTTPError(http.StatusBadGateway, fmt.Sprintf("proxy raw, request header copy error=%v, url=%s", t.URL, err))
112-
c.Error(he)
110+
c.Set("_error", echo.NewHTTPError(http.StatusBadGateway, fmt.Sprintf("proxy raw, request header copy error=%v, url=%s", t.URL, err)))
113111
return
114112
}
115113

@@ -123,7 +121,7 @@ func proxyRaw(t *ProxyTarget, c echo.Context) http.Handler {
123121
go cp(in, out)
124122
err = <-errCh
125123
if err != nil && err != io.EOF {
126-
c.Logger().Errorf("proxy raw, copy body error=%v, url=%s", t.URL, err)
124+
c.Set("_error", fmt.Errorf("proxy raw, copy body error=%v, url=%s", t.URL, err))
127125
}
128126
})
129127
}
@@ -251,6 +249,9 @@ func ProxyWithConfig(config ProxyConfig) echo.MiddlewareFunc {
251249
default:
252250
proxyHTTP(tgt, c, config).ServeHTTP(res, req)
253251
}
252+
if e, ok := c.Get("_error").(error); ok {
253+
err = e
254+
}
254255

255256
return
256257
}

middleware/proxy_1_11.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ func proxyHTTP(tgt *ProxyTarget, c echo.Context, config ProxyConfig) http.Handle
1717
if tgt.Name != "" {
1818
desc = fmt.Sprintf("%s(%s)", tgt.Name, tgt.URL.String())
1919
}
20-
c.Logger().Errorf("remote %s unreachable, could not forward: %v", desc, err)
21-
c.Error(echo.NewHTTPError(http.StatusServiceUnavailable))
20+
c.Set("_error", echo.NewHTTPError(http.StatusBadGateway, fmt.Sprintf("remote %s unreachable, could not forward: %v", desc, err)))
2221
}
2322
proxy.Transport = config.Transport
2423
return proxy

middleware/proxy_1_11_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,5 @@ func TestProxy_1_11(t *testing.T) {
4949
req.URL.Path = "/api/users"
5050
e.ServeHTTP(rec, req)
5151
assert.Equal(t, "/api/users", req.URL.Path)
52-
assert.Equal(t, http.StatusServiceUnavailable, rec.Code)
52+
assert.Equal(t, http.StatusBadGateway, rec.Code)
5353
}

router.go

+9
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ func (r *Router) insert(method, path string, h HandlerFunc, t kind, ppath string
136136
// Split node
137137
n := newNode(cn.kind, cn.prefix[l:], cn, cn.children, cn.methodHandler, cn.ppath, cn.pnames)
138138

139+
// Update parent path for all children to new node
140+
for _, child := range cn.children {
141+
child.parent = n
142+
}
143+
139144
// Reset parent node
140145
cn.kind = skind
141146
cn.label = cn.prefix[0]
@@ -417,6 +422,10 @@ func (r *Router) Find(method, path string, c Context) {
417422
if np == nil {
418423
break // no further parent nodes in tree, abort
419424
}
425+
var str strings.Builder
426+
str.WriteString(nn.prefix)
427+
str.WriteString(search)
428+
search = str.String()
420429
nn = np
421430
}
422431
if cn != nil { // use the found "any" route and update path

0 commit comments

Comments
 (0)