Skip to content

Commit 3a05be0

Browse files
committed
Fix #1493 to avoid router loop for param nodes
1 parent beb24f8 commit 3a05be0

File tree

1 file changed

+42
-32
lines changed

1 file changed

+42
-32
lines changed

router.go

+42-32
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,8 @@ func (r *Router) Find(method, path string, c Context) {
376376
continue
377377
}
378378

379-
// Param node
380379
Param:
380+
// Param node
381381
if child = cn.findChildByKind(pkind); child != nil {
382382
// Issue #378
383383
if len(pvalues) == n {
@@ -401,47 +401,57 @@ func (r *Router) Find(method, path string, c Context) {
401401
continue
402402
}
403403

404-
// Any node
405404
Any:
406-
if cn = cn.findChildByKind(akind); cn == nil {
407-
if nn != nil {
408-
// No next node to go down in routing (issue #954)
409-
// Find nearest "any" route going up the routing tree
410-
search = ns
411-
np := nn.parent
412-
// Consider param route one level up only
413-
// if no slash is remaining in search string
414-
if cn = nn.findChildByKind(pkind); cn != nil && strings.IndexByte(ns, '/') == -1 {
405+
// Any node
406+
if cn = cn.findChildByKind(akind); cn != nil {
407+
// If any node is found, use remaining path for pvalues
408+
pvalues[len(cn.pnames)-1] = search
409+
break
410+
}
411+
412+
// No node found, continue at stored next node
413+
// or find nearest "any" route
414+
if nn != nil {
415+
// No next node to go down in routing (issue #954)
416+
// Find nearest "any" route going up the routing tree
417+
search = ns
418+
np := nn.parent
419+
// Consider param route one level up only
420+
if cn = nn.findChildByKind(pkind); cn != nil {
421+
if strings.IndexByte(ns, '/') == -1 {
422+
// If no slash is remaining in search string set param value
415423
pvalues[len(cn.pnames)-1] = search
416424
break
417425
} else if cn != nil && strings.IndexByte(ns, '/') != 1 {
418-
// If slash is present, it means that this is a parameterized route.
419-
cn = cn.parent
426+
// Otherwise continue route processing with restored next node
427+
cn = nn
428+
nn = nil
429+
ns = ""
420430
goto Param
421431
}
422-
for {
423-
np = nn.parent
424-
if cn = nn.findChildByKind(akind); cn != nil {
425-
break
426-
}
427-
if np == nil {
428-
break // no further parent nodes in tree, abort
429-
}
430-
var str strings.Builder
431-
str.WriteString(nn.prefix)
432-
str.WriteString(search)
433-
search = str.String()
434-
nn = np
435-
}
436-
if cn != nil { // use the found "any" route and update path
437-
pvalues[len(cn.pnames)-1] = search
432+
}
433+
// No param route found, try to resolve nearest any route
434+
for {
435+
np = nn.parent
436+
if cn = nn.findChildByKind(akind); cn != nil {
438437
break
439438
}
439+
if np == nil {
440+
break // no further parent nodes in tree, abort
441+
}
442+
var str strings.Builder
443+
str.WriteString(nn.prefix)
444+
str.WriteString(search)
445+
search = str.String()
446+
nn = np
447+
}
448+
if cn != nil { // use the found "any" route and update path
449+
pvalues[len(cn.pnames)-1] = search
450+
break
440451
}
441-
return // Not found
442452
}
443-
pvalues[len(cn.pnames)-1] = search
444-
break
453+
return // Not found
454+
445455
}
446456

447457
ctx.handler = cn.findHandler(method)

0 commit comments

Comments
 (0)