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)
| 337 | // made if a handle exists with an extra (without the) trailing slash for the |
| 338 | // given path. |
| 339 | func (n *node) getValue(path string) (handle Handle, p Params, tsr bool) { |
| 340 | walk: // outer loop for walking the tree |
| 341 | for { |
| 342 | if len(path) > len(n.path) { |
| 343 | if path[:len(n.path)] == n.path { |
| 344 | path = path[len(n.path):] |
| 345 | // If this node does not have a wildcard (param or catchAll) |
| 346 | // child, we can just look up the next child node and continue |
| 347 | // to walk down the tree |
| 348 | if !n.wildChild { |
| 349 | c := path[0] |
| 350 | for i := 0; i < len(n.indices); i++ { |
| 351 | if c == n.indices[i] { |
| 352 | n = n.children[i] |
| 353 | continue walk |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | // Nothing found. |
| 358 | // We can recommend to redirect to the same URL without a |
| 359 | // trailing slash if a leaf exists for that path. |
| 360 | tsr = (path == "/" && n.handle != nil) |
| 361 | return |
| 362 | |
| 363 | } |
| 364 | |
| 365 | // handle wildcard child |
| 366 | n = n.children[0] |
| 367 | switch n.nType { |
| 368 | case param: |
| 369 | // find param end (either '/' or path end) |
| 370 | end := 0 |
| 371 | for end < len(path) && path[end] != '/' { |
| 372 | end++ |
| 373 | } |
| 374 | |
| 375 | // save param value |
| 376 | if p == nil { |
| 377 | // lazy allocation |
| 378 | p = make(Params, 0, n.maxParams) |
| 379 | } |
| 380 | i := len(p) |
| 381 | p = p[:i+1] // expand slice within preallocated capacity |
| 382 | p[i].Key = n.path[1:] |
| 383 | p[i].Value = path[:end] |
| 384 | |
| 385 | // we need to go deeper! |
| 386 | if end < len(path) { |
| 387 | if len(n.children) > 0 { |
| 388 | path = path[end:] |
| 389 | n = n.children[0] |
| 390 | continue walk |
| 391 | } |
| 392 | |
| 393 | // ... but we can't |
| 394 | tsr = (len(path) == end+1) |
| 395 | return |
| 396 | } |
no outgoing calls