@@ -376,8 +376,8 @@ func (r *Router) Find(method, path string, c Context) {
376
376
continue
377
377
}
378
378
379
- // Param node
380
379
Param:
380
+ // Param node
381
381
if child = cn .findChildByKind (pkind ); child != nil {
382
382
// Issue #378
383
383
if len (pvalues ) == n {
@@ -401,47 +401,57 @@ func (r *Router) Find(method, path string, c Context) {
401
401
continue
402
402
}
403
403
404
- // Any node
405
404
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
415
423
pvalues [len (cn .pnames )- 1 ] = search
416
424
break
417
425
} 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 = ""
420
430
goto Param
421
431
}
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 {
438
437
break
439
438
}
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
440
451
}
441
- return // Not found
442
452
}
443
- pvalues [ len ( cn . pnames ) - 1 ] = search
444
- break
453
+ return // Not found
454
+
445
455
}
446
456
447
457
ctx .handler = cn .findHandler (method )
0 commit comments