You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tree.go 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. package chi
  2. // Radix tree implementation below is a based on the original work by
  3. // Armon Dadgar in https://github.com/armon/go-radix/blob/master/radix.go
  4. // (MIT licensed). It's been heavily modified for use as a HTTP routing tree.
  5. import (
  6. "fmt"
  7. "net/http"
  8. "regexp"
  9. "sort"
  10. "strconv"
  11. "strings"
  12. )
  13. type methodTyp uint
  14. const (
  15. mSTUB methodTyp = 1 << iota
  16. mCONNECT
  17. mDELETE
  18. mGET
  19. mHEAD
  20. mOPTIONS
  21. mPATCH
  22. mPOST
  23. mPUT
  24. mTRACE
  25. )
  26. var mALL = mCONNECT | mDELETE | mGET | mHEAD |
  27. mOPTIONS | mPATCH | mPOST | mPUT | mTRACE
  28. var methodMap = map[string]methodTyp{
  29. http.MethodConnect: mCONNECT,
  30. http.MethodDelete: mDELETE,
  31. http.MethodGet: mGET,
  32. http.MethodHead: mHEAD,
  33. http.MethodOptions: mOPTIONS,
  34. http.MethodPatch: mPATCH,
  35. http.MethodPost: mPOST,
  36. http.MethodPut: mPUT,
  37. http.MethodTrace: mTRACE,
  38. }
  39. // RegisterMethod adds support for custom HTTP method handlers, available
  40. // via Router#Method and Router#MethodFunc
  41. func RegisterMethod(method string) {
  42. if method == "" {
  43. return
  44. }
  45. method = strings.ToUpper(method)
  46. if _, ok := methodMap[method]; ok {
  47. return
  48. }
  49. n := len(methodMap)
  50. if n > strconv.IntSize-2 {
  51. panic(fmt.Sprintf("chi: max number of methods reached (%d)", strconv.IntSize))
  52. }
  53. mt := methodTyp(2 << n)
  54. methodMap[method] = mt
  55. mALL |= mt
  56. }
  57. type nodeTyp uint8
  58. const (
  59. ntStatic nodeTyp = iota // /home
  60. ntRegexp // /{id:[0-9]+}
  61. ntParam // /{user}
  62. ntCatchAll // /api/v1/*
  63. )
  64. type node struct {
  65. // subroutes on the leaf node
  66. subroutes Routes
  67. // regexp matcher for regexp nodes
  68. rex *regexp.Regexp
  69. // HTTP handler endpoints on the leaf node
  70. endpoints endpoints
  71. // prefix is the common prefix we ignore
  72. prefix string
  73. // child nodes should be stored in-order for iteration,
  74. // in groups of the node type.
  75. children [ntCatchAll + 1]nodes
  76. // first byte of the child prefix
  77. tail byte
  78. // node type: static, regexp, param, catchAll
  79. typ nodeTyp
  80. // first byte of the prefix
  81. label byte
  82. }
  83. // endpoints is a mapping of http method constants to handlers
  84. // for a given route.
  85. type endpoints map[methodTyp]*endpoint
  86. type endpoint struct {
  87. // endpoint handler
  88. handler http.Handler
  89. // pattern is the routing pattern for handler nodes
  90. pattern string
  91. // parameter keys recorded on handler nodes
  92. paramKeys []string
  93. }
  94. func (s endpoints) Value(method methodTyp) *endpoint {
  95. mh, ok := s[method]
  96. if !ok {
  97. mh = &endpoint{}
  98. s[method] = mh
  99. }
  100. return mh
  101. }
  102. func (n *node) InsertRoute(method methodTyp, pattern string, handler http.Handler) *node {
  103. var parent *node
  104. search := pattern
  105. for {
  106. // Handle key exhaustion
  107. if len(search) == 0 {
  108. // Insert or update the node's leaf handler
  109. n.setEndpoint(method, handler, pattern)
  110. return n
  111. }
  112. // We're going to be searching for a wild node next,
  113. // in this case, we need to get the tail
  114. var label = search[0]
  115. var segTail byte
  116. var segEndIdx int
  117. var segTyp nodeTyp
  118. var segRexpat string
  119. if label == '{' || label == '*' {
  120. segTyp, _, segRexpat, segTail, _, segEndIdx = patNextSegment(search)
  121. }
  122. var prefix string
  123. if segTyp == ntRegexp {
  124. prefix = segRexpat
  125. }
  126. // Look for the edge to attach to
  127. parent = n
  128. n = n.getEdge(segTyp, label, segTail, prefix)
  129. // No edge, create one
  130. if n == nil {
  131. child := &node{label: label, tail: segTail, prefix: search}
  132. hn := parent.addChild(child, search)
  133. hn.setEndpoint(method, handler, pattern)
  134. return hn
  135. }
  136. // Found an edge to match the pattern
  137. if n.typ > ntStatic {
  138. // We found a param node, trim the param from the search path and continue.
  139. // This param/wild pattern segment would already be on the tree from a previous
  140. // call to addChild when creating a new node.
  141. search = search[segEndIdx:]
  142. continue
  143. }
  144. // Static nodes fall below here.
  145. // Determine longest prefix of the search key on match.
  146. commonPrefix := longestPrefix(search, n.prefix)
  147. if commonPrefix == len(n.prefix) {
  148. // the common prefix is as long as the current node's prefix we're attempting to insert.
  149. // keep the search going.
  150. search = search[commonPrefix:]
  151. continue
  152. }
  153. // Split the node
  154. child := &node{
  155. typ: ntStatic,
  156. prefix: search[:commonPrefix],
  157. }
  158. parent.replaceChild(search[0], segTail, child)
  159. // Restore the existing node
  160. n.label = n.prefix[commonPrefix]
  161. n.prefix = n.prefix[commonPrefix:]
  162. child.addChild(n, n.prefix)
  163. // If the new key is a subset, set the method/handler on this node and finish.
  164. search = search[commonPrefix:]
  165. if len(search) == 0 {
  166. child.setEndpoint(method, handler, pattern)
  167. return child
  168. }
  169. // Create a new edge for the node
  170. subchild := &node{
  171. typ: ntStatic,
  172. label: search[0],
  173. prefix: search,
  174. }
  175. hn := child.addChild(subchild, search)
  176. hn.setEndpoint(method, handler, pattern)
  177. return hn
  178. }
  179. }
  180. // addChild appends the new `child` node to the tree using the `pattern` as the trie key.
  181. // For a URL router like chi's, we split the static, param, regexp and wildcard segments
  182. // into different nodes. In addition, addChild will recursively call itself until every
  183. // pattern segment is added to the url pattern tree as individual nodes, depending on type.
  184. func (n *node) addChild(child *node, prefix string) *node {
  185. search := prefix
  186. // handler leaf node added to the tree is the child.
  187. // this may be overridden later down the flow
  188. hn := child
  189. // Parse next segment
  190. segTyp, _, segRexpat, segTail, segStartIdx, segEndIdx := patNextSegment(search)
  191. // Add child depending on next up segment
  192. switch segTyp {
  193. case ntStatic:
  194. // Search prefix is all static (that is, has no params in path)
  195. // noop
  196. default:
  197. // Search prefix contains a param, regexp or wildcard
  198. if segTyp == ntRegexp {
  199. rex, err := regexp.Compile(segRexpat)
  200. if err != nil {
  201. panic(fmt.Sprintf("chi: invalid regexp pattern '%s' in route param", segRexpat))
  202. }
  203. child.prefix = segRexpat
  204. child.rex = rex
  205. }
  206. if segStartIdx == 0 {
  207. // Route starts with a param
  208. child.typ = segTyp
  209. if segTyp == ntCatchAll {
  210. segStartIdx = -1
  211. } else {
  212. segStartIdx = segEndIdx
  213. }
  214. if segStartIdx < 0 {
  215. segStartIdx = len(search)
  216. }
  217. child.tail = segTail // for params, we set the tail
  218. if segStartIdx != len(search) {
  219. // add static edge for the remaining part, split the end.
  220. // its not possible to have adjacent param nodes, so its certainly
  221. // going to be a static node next.
  222. search = search[segStartIdx:] // advance search position
  223. nn := &node{
  224. typ: ntStatic,
  225. label: search[0],
  226. prefix: search,
  227. }
  228. hn = child.addChild(nn, search)
  229. }
  230. } else if segStartIdx > 0 {
  231. // Route has some param
  232. // starts with a static segment
  233. child.typ = ntStatic
  234. child.prefix = search[:segStartIdx]
  235. child.rex = nil
  236. // add the param edge node
  237. search = search[segStartIdx:]
  238. nn := &node{
  239. typ: segTyp,
  240. label: search[0],
  241. tail: segTail,
  242. }
  243. hn = child.addChild(nn, search)
  244. }
  245. }
  246. n.children[child.typ] = append(n.children[child.typ], child)
  247. n.children[child.typ].Sort()
  248. return hn
  249. }
  250. func (n *node) replaceChild(label, tail byte, child *node) {
  251. for i := 0; i < len(n.children[child.typ]); i++ {
  252. if n.children[child.typ][i].label == label && n.children[child.typ][i].tail == tail {
  253. n.children[child.typ][i] = child
  254. n.children[child.typ][i].label = label
  255. n.children[child.typ][i].tail = tail
  256. return
  257. }
  258. }
  259. panic("chi: replacing missing child")
  260. }
  261. func (n *node) getEdge(ntyp nodeTyp, label, tail byte, prefix string) *node {
  262. nds := n.children[ntyp]
  263. for i := 0; i < len(nds); i++ {
  264. if nds[i].label == label && nds[i].tail == tail {
  265. if ntyp == ntRegexp && nds[i].prefix != prefix {
  266. continue
  267. }
  268. return nds[i]
  269. }
  270. }
  271. return nil
  272. }
  273. func (n *node) setEndpoint(method methodTyp, handler http.Handler, pattern string) {
  274. // Set the handler for the method type on the node
  275. if n.endpoints == nil {
  276. n.endpoints = make(endpoints)
  277. }
  278. paramKeys := patParamKeys(pattern)
  279. if method&mSTUB == mSTUB {
  280. n.endpoints.Value(mSTUB).handler = handler
  281. }
  282. if method&mALL == mALL {
  283. h := n.endpoints.Value(mALL)
  284. h.handler = handler
  285. h.pattern = pattern
  286. h.paramKeys = paramKeys
  287. for _, m := range methodMap {
  288. h := n.endpoints.Value(m)
  289. h.handler = handler
  290. h.pattern = pattern
  291. h.paramKeys = paramKeys
  292. }
  293. } else {
  294. h := n.endpoints.Value(method)
  295. h.handler = handler
  296. h.pattern = pattern
  297. h.paramKeys = paramKeys
  298. }
  299. }
  300. func (n *node) FindRoute(rctx *Context, method methodTyp, path string) (*node, endpoints, http.Handler) {
  301. // Reset the context routing pattern and params
  302. rctx.routePattern = ""
  303. rctx.routeParams.Keys = rctx.routeParams.Keys[:0]
  304. rctx.routeParams.Values = rctx.routeParams.Values[:0]
  305. // Find the routing handlers for the path
  306. rn := n.findRoute(rctx, method, path)
  307. if rn == nil {
  308. return nil, nil, nil
  309. }
  310. // Record the routing params in the request lifecycle
  311. rctx.URLParams.Keys = append(rctx.URLParams.Keys, rctx.routeParams.Keys...)
  312. rctx.URLParams.Values = append(rctx.URLParams.Values, rctx.routeParams.Values...)
  313. // Record the routing pattern in the request lifecycle
  314. if rn.endpoints[method].pattern != "" {
  315. rctx.routePattern = rn.endpoints[method].pattern
  316. rctx.RoutePatterns = append(rctx.RoutePatterns, rctx.routePattern)
  317. }
  318. return rn, rn.endpoints, rn.endpoints[method].handler
  319. }
  320. // Recursive edge traversal by checking all nodeTyp groups along the way.
  321. // It's like searching through a multi-dimensional radix trie.
  322. func (n *node) findRoute(rctx *Context, method methodTyp, path string) *node {
  323. nn := n
  324. search := path
  325. for t, nds := range nn.children {
  326. ntyp := nodeTyp(t)
  327. if len(nds) == 0 {
  328. continue
  329. }
  330. var xn *node
  331. xsearch := search
  332. var label byte
  333. if search != "" {
  334. label = search[0]
  335. }
  336. switch ntyp {
  337. case ntStatic:
  338. xn = nds.findEdge(label)
  339. if xn == nil || !strings.HasPrefix(xsearch, xn.prefix) {
  340. continue
  341. }
  342. xsearch = xsearch[len(xn.prefix):]
  343. case ntParam, ntRegexp:
  344. // short-circuit and return no matching route for empty param values
  345. if xsearch == "" {
  346. continue
  347. }
  348. // serially loop through each node grouped by the tail delimiter
  349. for idx := 0; idx < len(nds); idx++ {
  350. xn = nds[idx]
  351. // label for param nodes is the delimiter byte
  352. p := strings.IndexByte(xsearch, xn.tail)
  353. if p < 0 {
  354. if xn.tail == '/' {
  355. p = len(xsearch)
  356. } else {
  357. continue
  358. }
  359. } else if ntyp == ntRegexp && p == 0 {
  360. continue
  361. }
  362. if ntyp == ntRegexp && xn.rex != nil {
  363. if !xn.rex.MatchString(xsearch[:p]) {
  364. continue
  365. }
  366. } else if strings.IndexByte(xsearch[:p], '/') != -1 {
  367. // avoid a match across path segments
  368. continue
  369. }
  370. prevlen := len(rctx.routeParams.Values)
  371. rctx.routeParams.Values = append(rctx.routeParams.Values, xsearch[:p])
  372. xsearch = xsearch[p:]
  373. if len(xsearch) == 0 {
  374. if xn.isLeaf() {
  375. h := xn.endpoints[method]
  376. if h != nil && h.handler != nil {
  377. rctx.routeParams.Keys = append(rctx.routeParams.Keys, h.paramKeys...)
  378. return xn
  379. }
  380. // flag that the routing context found a route, but not a corresponding
  381. // supported method
  382. rctx.methodNotAllowed = true
  383. }
  384. }
  385. // recursively find the next node on this branch
  386. fin := xn.findRoute(rctx, method, xsearch)
  387. if fin != nil {
  388. return fin
  389. }
  390. // not found on this branch, reset vars
  391. rctx.routeParams.Values = rctx.routeParams.Values[:prevlen]
  392. xsearch = search
  393. }
  394. rctx.routeParams.Values = append(rctx.routeParams.Values, "")
  395. default:
  396. // catch-all nodes
  397. rctx.routeParams.Values = append(rctx.routeParams.Values, search)
  398. xn = nds[0]
  399. xsearch = ""
  400. }
  401. if xn == nil {
  402. continue
  403. }
  404. // did we find it yet?
  405. if len(xsearch) == 0 {
  406. if xn.isLeaf() {
  407. h := xn.endpoints[method]
  408. if h != nil && h.handler != nil {
  409. rctx.routeParams.Keys = append(rctx.routeParams.Keys, h.paramKeys...)
  410. return xn
  411. }
  412. // flag that the routing context found a route, but not a corresponding
  413. // supported method
  414. rctx.methodNotAllowed = true
  415. }
  416. }
  417. // recursively find the next node..
  418. fin := xn.findRoute(rctx, method, xsearch)
  419. if fin != nil {
  420. return fin
  421. }
  422. // Did not find final handler, let's remove the param here if it was set
  423. if xn.typ > ntStatic {
  424. if len(rctx.routeParams.Values) > 0 {
  425. rctx.routeParams.Values = rctx.routeParams.Values[:len(rctx.routeParams.Values)-1]
  426. }
  427. }
  428. }
  429. return nil
  430. }
  431. func (n *node) findEdge(ntyp nodeTyp, label byte) *node {
  432. nds := n.children[ntyp]
  433. num := len(nds)
  434. idx := 0
  435. switch ntyp {
  436. case ntStatic, ntParam, ntRegexp:
  437. i, j := 0, num-1
  438. for i <= j {
  439. idx = i + (j-i)/2
  440. if label > nds[idx].label {
  441. i = idx + 1
  442. } else if label < nds[idx].label {
  443. j = idx - 1
  444. } else {
  445. i = num // breaks cond
  446. }
  447. }
  448. if nds[idx].label != label {
  449. return nil
  450. }
  451. return nds[idx]
  452. default: // catch all
  453. return nds[idx]
  454. }
  455. }
  456. func (n *node) isLeaf() bool {
  457. return n.endpoints != nil
  458. }
  459. func (n *node) findPattern(pattern string) bool {
  460. nn := n
  461. for _, nds := range nn.children {
  462. if len(nds) == 0 {
  463. continue
  464. }
  465. n = nn.findEdge(nds[0].typ, pattern[0])
  466. if n == nil {
  467. continue
  468. }
  469. var idx int
  470. var xpattern string
  471. switch n.typ {
  472. case ntStatic:
  473. idx = longestPrefix(pattern, n.prefix)
  474. if idx < len(n.prefix) {
  475. continue
  476. }
  477. case ntParam, ntRegexp:
  478. idx = strings.IndexByte(pattern, '}') + 1
  479. case ntCatchAll:
  480. idx = longestPrefix(pattern, "*")
  481. default:
  482. panic("chi: unknown node type")
  483. }
  484. xpattern = pattern[idx:]
  485. if len(xpattern) == 0 {
  486. return true
  487. }
  488. return n.findPattern(xpattern)
  489. }
  490. return false
  491. }
  492. func (n *node) routes() []Route {
  493. rts := []Route{}
  494. n.walk(func(eps endpoints, subroutes Routes) bool {
  495. if eps[mSTUB] != nil && eps[mSTUB].handler != nil && subroutes == nil {
  496. return false
  497. }
  498. // Group methodHandlers by unique patterns
  499. pats := make(map[string]endpoints)
  500. for mt, h := range eps {
  501. if h.pattern == "" {
  502. continue
  503. }
  504. p, ok := pats[h.pattern]
  505. if !ok {
  506. p = endpoints{}
  507. pats[h.pattern] = p
  508. }
  509. p[mt] = h
  510. }
  511. for p, mh := range pats {
  512. hs := make(map[string]http.Handler)
  513. if mh[mALL] != nil && mh[mALL].handler != nil {
  514. hs["*"] = mh[mALL].handler
  515. }
  516. for mt, h := range mh {
  517. if h.handler == nil {
  518. continue
  519. }
  520. m := methodTypString(mt)
  521. if m == "" {
  522. continue
  523. }
  524. hs[m] = h.handler
  525. }
  526. rt := Route{subroutes, hs, p}
  527. rts = append(rts, rt)
  528. }
  529. return false
  530. })
  531. return rts
  532. }
  533. func (n *node) walk(fn func(eps endpoints, subroutes Routes) bool) bool {
  534. // Visit the leaf values if any
  535. if (n.endpoints != nil || n.subroutes != nil) && fn(n.endpoints, n.subroutes) {
  536. return true
  537. }
  538. // Recurse on the children
  539. for _, ns := range n.children {
  540. for _, cn := range ns {
  541. if cn.walk(fn) {
  542. return true
  543. }
  544. }
  545. }
  546. return false
  547. }
  548. // patNextSegment returns the next segment details from a pattern:
  549. // node type, param key, regexp string, param tail byte, param starting index, param ending index
  550. func patNextSegment(pattern string) (nodeTyp, string, string, byte, int, int) {
  551. ps := strings.Index(pattern, "{")
  552. ws := strings.Index(pattern, "*")
  553. if ps < 0 && ws < 0 {
  554. return ntStatic, "", "", 0, 0, len(pattern) // we return the entire thing
  555. }
  556. // Sanity check
  557. if ps >= 0 && ws >= 0 && ws < ps {
  558. panic("chi: wildcard '*' must be the last pattern in a route, otherwise use a '{param}'")
  559. }
  560. var tail byte = '/' // Default endpoint tail to / byte
  561. if ps >= 0 {
  562. // Param/Regexp pattern is next
  563. nt := ntParam
  564. // Read to closing } taking into account opens and closes in curl count (cc)
  565. cc := 0
  566. pe := ps
  567. for i, c := range pattern[ps:] {
  568. if c == '{' {
  569. cc++
  570. } else if c == '}' {
  571. cc--
  572. if cc == 0 {
  573. pe = ps + i
  574. break
  575. }
  576. }
  577. }
  578. if pe == ps {
  579. panic("chi: route param closing delimiter '}' is missing")
  580. }
  581. key := pattern[ps+1 : pe]
  582. pe++ // set end to next position
  583. if pe < len(pattern) {
  584. tail = pattern[pe]
  585. }
  586. var rexpat string
  587. if idx := strings.Index(key, ":"); idx >= 0 {
  588. nt = ntRegexp
  589. rexpat = key[idx+1:]
  590. key = key[:idx]
  591. }
  592. if len(rexpat) > 0 {
  593. if rexpat[0] != '^' {
  594. rexpat = "^" + rexpat
  595. }
  596. if rexpat[len(rexpat)-1] != '$' {
  597. rexpat += "$"
  598. }
  599. }
  600. return nt, key, rexpat, tail, ps, pe
  601. }
  602. // Wildcard pattern as finale
  603. if ws < len(pattern)-1 {
  604. panic("chi: wildcard '*' must be the last value in a route. trim trailing text or use a '{param}' instead")
  605. }
  606. return ntCatchAll, "*", "", 0, ws, len(pattern)
  607. }
  608. func patParamKeys(pattern string) []string {
  609. pat := pattern
  610. paramKeys := []string{}
  611. for {
  612. ptyp, paramKey, _, _, _, e := patNextSegment(pat)
  613. if ptyp == ntStatic {
  614. return paramKeys
  615. }
  616. for i := 0; i < len(paramKeys); i++ {
  617. if paramKeys[i] == paramKey {
  618. panic(fmt.Sprintf("chi: routing pattern '%s' contains duplicate param key, '%s'", pattern, paramKey))
  619. }
  620. }
  621. paramKeys = append(paramKeys, paramKey)
  622. pat = pat[e:]
  623. }
  624. }
  625. // longestPrefix finds the length of the shared prefix
  626. // of two strings
  627. func longestPrefix(k1, k2 string) int {
  628. max := len(k1)
  629. if l := len(k2); l < max {
  630. max = l
  631. }
  632. var i int
  633. for i = 0; i < max; i++ {
  634. if k1[i] != k2[i] {
  635. break
  636. }
  637. }
  638. return i
  639. }
  640. func methodTypString(method methodTyp) string {
  641. for s, t := range methodMap {
  642. if method == t {
  643. return s
  644. }
  645. }
  646. return ""
  647. }
  648. type nodes []*node
  649. // Sort the list of nodes by label
  650. func (ns nodes) Sort() { sort.Sort(ns); ns.tailSort() }
  651. func (ns nodes) Len() int { return len(ns) }
  652. func (ns nodes) Swap(i, j int) { ns[i], ns[j] = ns[j], ns[i] }
  653. func (ns nodes) Less(i, j int) bool { return ns[i].label < ns[j].label }
  654. // tailSort pushes nodes with '/' as the tail to the end of the list for param nodes.
  655. // The list order determines the traversal order.
  656. func (ns nodes) tailSort() {
  657. for i := len(ns) - 1; i >= 0; i-- {
  658. if ns[i].typ > ntStatic && ns[i].tail == '/' {
  659. ns.Swap(i, len(ns)-1)
  660. return
  661. }
  662. }
  663. }
  664. func (ns nodes) findEdge(label byte) *node {
  665. num := len(ns)
  666. idx := 0
  667. i, j := 0, num-1
  668. for i <= j {
  669. idx = i + (j-i)/2
  670. if label > ns[idx].label {
  671. i = idx + 1
  672. } else if label < ns[idx].label {
  673. j = idx - 1
  674. } else {
  675. i = num // breaks cond
  676. }
  677. }
  678. if ns[idx].label != label {
  679. return nil
  680. }
  681. return ns[idx]
  682. }
  683. // Route describes the details of a routing handler.
  684. // Handlers map key is an HTTP method
  685. type Route struct {
  686. SubRoutes Routes
  687. Handlers map[string]http.Handler
  688. Pattern string
  689. }
  690. // WalkFunc is the type of the function called for each method and route visited by Walk.
  691. type WalkFunc func(method string, route string, handler http.Handler, middlewares ...func(http.Handler) http.Handler) error
  692. // Walk walks any router tree that implements Routes interface.
  693. func Walk(r Routes, walkFn WalkFunc) error {
  694. return walk(r, walkFn, "")
  695. }
  696. func walk(r Routes, walkFn WalkFunc, parentRoute string, parentMw ...func(http.Handler) http.Handler) error {
  697. for _, route := range r.Routes() {
  698. mws := make([]func(http.Handler) http.Handler, len(parentMw))
  699. copy(mws, parentMw)
  700. mws = append(mws, r.Middlewares()...)
  701. if route.SubRoutes != nil {
  702. if err := walk(route.SubRoutes, walkFn, parentRoute+route.Pattern, mws...); err != nil {
  703. return err
  704. }
  705. continue
  706. }
  707. for method, handler := range route.Handlers {
  708. if method == "*" {
  709. // Ignore a "catchAll" method, since we pass down all the specific methods for each route.
  710. continue
  711. }
  712. fullRoute := parentRoute + route.Pattern
  713. fullRoute = strings.Replace(fullRoute, "/*/", "/", -1)
  714. if chain, ok := handler.(*ChainHandler); ok {
  715. if err := walkFn(method, fullRoute, chain.Endpoint, append(mws, chain.Middlewares...)...); err != nil {
  716. return err
  717. }
  718. } else {
  719. if err := walkFn(method, fullRoute, handler, mws...); err != nil {
  720. return err
  721. }
  722. }
  723. }
  724. }
  725. return nil
  726. }