Returns the handle registered with the given path (key). The values of wildcards are saved to a map. If no handle can be found, a TSR (trailing slash redirect) recommendation is made if a handle exists with an extra (without the) trailing slash for the given path.
(path string)
| 461 | // made if a handle exists with an extra (without the) trailing slash for the |
| 462 | // given path. |
| 463 | func (n *Node) getValue(path string) (handle RouterHandle, p Params, outnode *Node, tsr bool) { |
| 464 | walk: // outer loop for walking the tree |
| 465 | for { |
| 466 | if len(path) > len(n.path) { |
| 467 | if path[:len(n.path)] == n.path { |
| 468 | path = path[len(n.path):] |
| 469 | // If this node does not have a wildcard (param or catchAll) |
| 470 | // child, we can just look up the next child node and continue |
| 471 | // to walk down the tree |
| 472 | if !n.wildChild { |
| 473 | c := path[0] |
| 474 | for i := 0; i < len(n.indices); i++ { |
| 475 | if c == n.indices[i] { |
| 476 | n = n.children[i] |
| 477 | continue walk |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | // Nothing found. |
| 482 | // We can recommend to redirect to the same URL without a |
| 483 | // trailing slash if a leaf exists for that path. |
| 484 | tsr = (path == "/" && n.handle != nil) |
| 485 | return |
| 486 | |
| 487 | } |
| 488 | |
| 489 | // handle wildcard child |
| 490 | outnode = n.children[0] |
| 491 | n = outnode |
| 492 | switch n.nType { |
| 493 | case param: |
| 494 | // find param end (either '/' or path end) |
| 495 | end := 0 |
| 496 | for end < len(path) && path[end] != '/' { |
| 497 | end++ |
| 498 | } |
| 499 | |
| 500 | // save param value |
| 501 | if p == nil { |
| 502 | // lazy allocation |
| 503 | p = make(Params, 0, n.maxParams) |
| 504 | } |
| 505 | i := len(p) |
| 506 | p = p[:i+1] // expand slice within preallocated capacity |
| 507 | p[i].Key = n.path[1:] |
| 508 | p[i].Value = path[:end] |
| 509 | |
| 510 | // we need to go deeper! |
| 511 | if end < len(path) { |
| 512 | if len(n.children) > 0 { |
| 513 | path = path[end:] |
| 514 | n = n.children[0] |
| 515 | continue walk |
| 516 | } |
| 517 | |
| 518 | // ... but we can't |
| 519 | tsr = (len(path) == end+1) |
| 520 | return |