1
0

tree.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. // Copyright 2013 Julien Schmidt. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be found
  3. // in the LICENSE file.
  4. package httprouter
  5. import (
  6. "strings"
  7. "unicode"
  8. "unicode/utf8"
  9. )
  10. func min(a, b int) int {
  11. if a <= b {
  12. return a
  13. }
  14. return b
  15. }
  16. func countParams(path string) uint8 {
  17. var n uint
  18. for i := 0; i < len(path); i++ {
  19. if path[i] != ':' && path[i] != '*' {
  20. continue
  21. }
  22. n++
  23. }
  24. if n >= 255 {
  25. return 255
  26. }
  27. return uint8(n)
  28. }
  29. type nodeType uint8
  30. const (
  31. static nodeType = iota // default
  32. root
  33. param
  34. catchAll
  35. )
  36. type node struct {
  37. path string
  38. wildChild bool
  39. nType nodeType
  40. maxParams uint8
  41. indices string
  42. children []*node
  43. handle Handle
  44. priority uint32
  45. }
  46. // increments priority of the given child and reorders if necessary
  47. func (n *node) incrementChildPrio(pos int) int {
  48. n.children[pos].priority++
  49. prio := n.children[pos].priority
  50. // adjust position (move to front)
  51. newPos := pos
  52. for newPos > 0 && n.children[newPos-1].priority < prio {
  53. // swap node positions
  54. n.children[newPos-1], n.children[newPos] = n.children[newPos], n.children[newPos-1]
  55. newPos--
  56. }
  57. // build new index char string
  58. if newPos != pos {
  59. n.indices = n.indices[:newPos] + // unchanged prefix, might be empty
  60. n.indices[pos:pos+1] + // the index char we move
  61. n.indices[newPos:pos] + n.indices[pos+1:] // rest without char at 'pos'
  62. }
  63. return newPos
  64. }
  65. // addRoute adds a node with the given handle to the path.
  66. // Not concurrency-safe!
  67. func (n *node) addRoute(path string, handle Handle) {
  68. fullPath := path
  69. n.priority++
  70. numParams := countParams(path)
  71. // non-empty tree
  72. if len(n.path) > 0 || len(n.children) > 0 {
  73. walk:
  74. for {
  75. // Update maxParams of the current node
  76. if numParams > n.maxParams {
  77. n.maxParams = numParams
  78. }
  79. // Find the longest common prefix.
  80. // This also implies that the common prefix contains no ':' or '*'
  81. // since the existing key can't contain those chars.
  82. i := 0
  83. max := min(len(path), len(n.path))
  84. for i < max && path[i] == n.path[i] {
  85. i++
  86. }
  87. // Split edge
  88. if i < len(n.path) {
  89. child := node{
  90. path: n.path[i:],
  91. wildChild: n.wildChild,
  92. nType: static,
  93. indices: n.indices,
  94. children: n.children,
  95. handle: n.handle,
  96. priority: n.priority - 1,
  97. }
  98. // Update maxParams (max of all children)
  99. for i := range child.children {
  100. if child.children[i].maxParams > child.maxParams {
  101. child.maxParams = child.children[i].maxParams
  102. }
  103. }
  104. n.children = []*node{&child}
  105. // []byte for proper unicode char conversion, see #65
  106. n.indices = string([]byte{n.path[i]})
  107. n.path = path[:i]
  108. n.handle = nil
  109. n.wildChild = false
  110. }
  111. // Make new node a child of this node
  112. if i < len(path) {
  113. path = path[i:]
  114. if n.wildChild {
  115. n = n.children[0]
  116. n.priority++
  117. // Update maxParams of the child node
  118. if numParams > n.maxParams {
  119. n.maxParams = numParams
  120. }
  121. numParams--
  122. // Check if the wildcard matches
  123. if len(path) >= len(n.path) && n.path == path[:len(n.path)] &&
  124. // Check for longer wildcard, e.g. :name and :names
  125. (len(n.path) >= len(path) || path[len(n.path)] == '/') {
  126. continue walk
  127. } else {
  128. // Wildcard conflict
  129. pathSeg := strings.SplitN(path, "/", 2)[0]
  130. prefix := fullPath[:strings.Index(fullPath, pathSeg)] + n.path
  131. panic("'" + pathSeg +
  132. "' in new path '" + fullPath +
  133. "' conflicts with existing wildcard '" + n.path +
  134. "' in existing prefix '" + prefix +
  135. "'")
  136. }
  137. }
  138. c := path[0]
  139. // slash after param
  140. if n.nType == param && c == '/' && len(n.children) == 1 {
  141. n = n.children[0]
  142. n.priority++
  143. continue walk
  144. }
  145. // Check if a child with the next path byte exists
  146. for i := 0; i < len(n.indices); i++ {
  147. if c == n.indices[i] {
  148. i = n.incrementChildPrio(i)
  149. n = n.children[i]
  150. continue walk
  151. }
  152. }
  153. // Otherwise insert it
  154. if c != ':' && c != '*' {
  155. // []byte for proper unicode char conversion, see #65
  156. n.indices += string([]byte{c})
  157. child := &node{
  158. maxParams: numParams,
  159. }
  160. n.children = append(n.children, child)
  161. n.incrementChildPrio(len(n.indices) - 1)
  162. n = child
  163. }
  164. n.insertChild(numParams, path, fullPath, handle)
  165. return
  166. } else if i == len(path) { // Make node a (in-path) leaf
  167. if n.handle != nil {
  168. panic("a handle is already registered for path '" + fullPath + "'")
  169. }
  170. n.handle = handle
  171. }
  172. return
  173. }
  174. } else { // Empty tree
  175. n.insertChild(numParams, path, fullPath, handle)
  176. n.nType = root
  177. }
  178. }
  179. func (n *node) insertChild(numParams uint8, path, fullPath string, handle Handle) {
  180. var offset int // already handled bytes of the path
  181. // find prefix until first wildcard (beginning with ':'' or '*'')
  182. for i, max := 0, len(path); numParams > 0; i++ {
  183. c := path[i]
  184. if c != ':' && c != '*' {
  185. continue
  186. }
  187. // find wildcard end (either '/' or path end)
  188. end := i + 1
  189. for end < max && path[end] != '/' {
  190. switch path[end] {
  191. // the wildcard name must not contain ':' and '*'
  192. case ':', '*':
  193. panic("only one wildcard per path segment is allowed, has: '" +
  194. path[i:] + "' in path '" + fullPath + "'")
  195. default:
  196. end++
  197. }
  198. }
  199. // check if this Node existing children which would be
  200. // unreachable if we insert the wildcard here
  201. if len(n.children) > 0 {
  202. panic("wildcard route '" + path[i:end] +
  203. "' conflicts with existing children in path '" + fullPath + "'")
  204. }
  205. // check if the wildcard has a name
  206. if end-i < 2 {
  207. panic("wildcards must be named with a non-empty name in path '" + fullPath + "'")
  208. }
  209. if c == ':' { // param
  210. // split path at the beginning of the wildcard
  211. if i > 0 {
  212. n.path = path[offset:i]
  213. offset = i
  214. }
  215. child := &node{
  216. nType: param,
  217. maxParams: numParams,
  218. }
  219. n.children = []*node{child}
  220. n.wildChild = true
  221. n = child
  222. n.priority++
  223. numParams--
  224. // if the path doesn't end with the wildcard, then there
  225. // will be another non-wildcard subpath starting with '/'
  226. if end < max {
  227. n.path = path[offset:end]
  228. offset = end
  229. child := &node{
  230. maxParams: numParams,
  231. priority: 1,
  232. }
  233. n.children = []*node{child}
  234. n = child
  235. }
  236. } else { // catchAll
  237. if end != max || numParams > 1 {
  238. panic("catch-all routes are only allowed at the end of the path in path '" + fullPath + "'")
  239. }
  240. if len(n.path) > 0 && n.path[len(n.path)-1] == '/' {
  241. panic("catch-all conflicts with existing handle for the path segment root in path '" + fullPath + "'")
  242. }
  243. // currently fixed width 1 for '/'
  244. i--
  245. if path[i] != '/' {
  246. panic("no / before catch-all in path '" + fullPath + "'")
  247. }
  248. n.path = path[offset:i]
  249. // first node: catchAll node with empty path
  250. child := &node{
  251. wildChild: true,
  252. nType: catchAll,
  253. maxParams: 1,
  254. }
  255. n.children = []*node{child}
  256. n.indices = string(path[i])
  257. n = child
  258. n.priority++
  259. // second node: node holding the variable
  260. child = &node{
  261. path: path[i:],
  262. nType: catchAll,
  263. maxParams: 1,
  264. handle: handle,
  265. priority: 1,
  266. }
  267. n.children = []*node{child}
  268. return
  269. }
  270. }
  271. // insert remaining path part and handle to the leaf
  272. n.path = path[offset:]
  273. n.handle = handle
  274. }
  275. // Returns the handle registered with the given path (key). The values of
  276. // wildcards are saved to a map.
  277. // If no handle can be found, a TSR (trailing slash redirect) recommendation is
  278. // made if a handle exists with an extra (without the) trailing slash for the
  279. // given path.
  280. func (n *node) getValue(path string) (handle Handle, p Params, tsr bool) {
  281. walk: // outer loop for walking the tree
  282. for {
  283. if len(path) > len(n.path) {
  284. if path[:len(n.path)] == n.path {
  285. path = path[len(n.path):]
  286. // If this node does not have a wildcard (param or catchAll)
  287. // child, we can just look up the next child node and continue
  288. // to walk down the tree
  289. if !n.wildChild {
  290. c := path[0]
  291. for i := 0; i < len(n.indices); i++ {
  292. if c == n.indices[i] {
  293. n = n.children[i]
  294. continue walk
  295. }
  296. }
  297. // Nothing found.
  298. // We can recommend to redirect to the same URL without a
  299. // trailing slash if a leaf exists for that path.
  300. tsr = (path == "/" && n.handle != nil)
  301. return
  302. }
  303. // handle wildcard child
  304. n = n.children[0]
  305. switch n.nType {
  306. case param:
  307. // find param end (either '/' or path end)
  308. end := 0
  309. for end < len(path) && path[end] != '/' {
  310. end++
  311. }
  312. // save param value
  313. if p == nil {
  314. // lazy allocation
  315. p = make(Params, 0, n.maxParams)
  316. }
  317. i := len(p)
  318. p = p[:i+1] // expand slice within preallocated capacity
  319. p[i].Key = n.path[1:]
  320. p[i].Value = path[:end]
  321. // we need to go deeper!
  322. if end < len(path) {
  323. if len(n.children) > 0 {
  324. path = path[end:]
  325. n = n.children[0]
  326. continue walk
  327. }
  328. // ... but we can't
  329. tsr = (len(path) == end+1)
  330. return
  331. }
  332. if handle = n.handle; handle != nil {
  333. return
  334. } else if len(n.children) == 1 {
  335. // No handle found. Check if a handle for this path + a
  336. // trailing slash exists for TSR recommendation
  337. n = n.children[0]
  338. tsr = (n.path == "/" && n.handle != nil)
  339. }
  340. return
  341. case catchAll:
  342. // save param value
  343. if p == nil {
  344. // lazy allocation
  345. p = make(Params, 0, n.maxParams)
  346. }
  347. i := len(p)
  348. p = p[:i+1] // expand slice within preallocated capacity
  349. p[i].Key = n.path[2:]
  350. p[i].Value = path
  351. handle = n.handle
  352. return
  353. default:
  354. panic("invalid node type")
  355. }
  356. }
  357. } else if path == n.path {
  358. // We should have reached the node containing the handle.
  359. // Check if this node has a handle registered.
  360. if handle = n.handle; handle != nil {
  361. return
  362. }
  363. if path == "/" && n.wildChild && n.nType != root {
  364. tsr = true
  365. return
  366. }
  367. // No handle found. Check if a handle for this path + a
  368. // trailing slash exists for trailing slash recommendation
  369. for i := 0; i < len(n.indices); i++ {
  370. if n.indices[i] == '/' {
  371. n = n.children[i]
  372. tsr = (len(n.path) == 1 && n.handle != nil) ||
  373. (n.nType == catchAll && n.children[0].handle != nil)
  374. return
  375. }
  376. }
  377. return
  378. }
  379. // Nothing found. We can recommend to redirect to the same URL with an
  380. // extra trailing slash if a leaf exists for that path
  381. tsr = (path == "/") ||
  382. (len(n.path) == len(path)+1 && n.path[len(path)] == '/' &&
  383. path == n.path[:len(n.path)-1] && n.handle != nil)
  384. return
  385. }
  386. }
  387. // Makes a case-insensitive lookup of the given path and tries to find a handler.
  388. // It can optionally also fix trailing slashes.
  389. // It returns the case-corrected path and a bool indicating whether the lookup
  390. // was successful.
  391. func (n *node) findCaseInsensitivePath(path string, fixTrailingSlash bool) (ciPath []byte, found bool) {
  392. return n.findCaseInsensitivePathRec(
  393. path,
  394. strings.ToLower(path),
  395. make([]byte, 0, len(path)+1), // preallocate enough memory for new path
  396. [4]byte{}, // empty rune buffer
  397. fixTrailingSlash,
  398. )
  399. }
  400. // shift bytes in array by n bytes left
  401. func shiftNRuneBytes(rb [4]byte, n int) [4]byte {
  402. switch n {
  403. case 0:
  404. return rb
  405. case 1:
  406. return [4]byte{rb[1], rb[2], rb[3], 0}
  407. case 2:
  408. return [4]byte{rb[2], rb[3]}
  409. case 3:
  410. return [4]byte{rb[3]}
  411. default:
  412. return [4]byte{}
  413. }
  414. }
  415. // recursive case-insensitive lookup function used by n.findCaseInsensitivePath
  416. func (n *node) findCaseInsensitivePathRec(path, loPath string, ciPath []byte, rb [4]byte, fixTrailingSlash bool) ([]byte, bool) {
  417. loNPath := strings.ToLower(n.path)
  418. walk: // outer loop for walking the tree
  419. for len(loPath) >= len(loNPath) && (len(loNPath) == 0 || loPath[1:len(loNPath)] == loNPath[1:]) {
  420. // add common path to result
  421. ciPath = append(ciPath, n.path...)
  422. if path = path[len(n.path):]; len(path) > 0 {
  423. loOld := loPath
  424. loPath = loPath[len(loNPath):]
  425. // If this node does not have a wildcard (param or catchAll) child,
  426. // we can just look up the next child node and continue to walk down
  427. // the tree
  428. if !n.wildChild {
  429. // skip rune bytes already processed
  430. rb = shiftNRuneBytes(rb, len(loNPath))
  431. if rb[0] != 0 {
  432. // old rune not finished
  433. for i := 0; i < len(n.indices); i++ {
  434. if n.indices[i] == rb[0] {
  435. // continue with child node
  436. n = n.children[i]
  437. loNPath = strings.ToLower(n.path)
  438. continue walk
  439. }
  440. }
  441. } else {
  442. // process a new rune
  443. var rv rune
  444. // find rune start
  445. // runes are up to 4 byte long,
  446. // -4 would definitely be another rune
  447. var off int
  448. for max := min(len(loNPath), 3); off < max; off++ {
  449. if i := len(loNPath) - off; utf8.RuneStart(loOld[i]) {
  450. // read rune from cached lowercase path
  451. rv, _ = utf8.DecodeRuneInString(loOld[i:])
  452. break
  453. }
  454. }
  455. // calculate lowercase bytes of current rune
  456. utf8.EncodeRune(rb[:], rv)
  457. // skipp already processed bytes
  458. rb = shiftNRuneBytes(rb, off)
  459. for i := 0; i < len(n.indices); i++ {
  460. // lowercase matches
  461. if n.indices[i] == rb[0] {
  462. // must use a recursive approach since both the
  463. // uppercase byte and the lowercase byte might exist
  464. // as an index
  465. if out, found := n.children[i].findCaseInsensitivePathRec(
  466. path, loPath, ciPath, rb, fixTrailingSlash,
  467. ); found {
  468. return out, true
  469. }
  470. break
  471. }
  472. }
  473. // same for uppercase rune, if it differs
  474. if up := unicode.ToUpper(rv); up != rv {
  475. utf8.EncodeRune(rb[:], up)
  476. rb = shiftNRuneBytes(rb, off)
  477. for i := 0; i < len(n.indices); i++ {
  478. // uppercase matches
  479. if n.indices[i] == rb[0] {
  480. // continue with child node
  481. n = n.children[i]
  482. loNPath = strings.ToLower(n.path)
  483. continue walk
  484. }
  485. }
  486. }
  487. }
  488. // Nothing found. We can recommend to redirect to the same URL
  489. // without a trailing slash if a leaf exists for that path
  490. return ciPath, (fixTrailingSlash && path == "/" && n.handle != nil)
  491. }
  492. n = n.children[0]
  493. switch n.nType {
  494. case param:
  495. // find param end (either '/' or path end)
  496. k := 0
  497. for k < len(path) && path[k] != '/' {
  498. k++
  499. }
  500. // add param value to case insensitive path
  501. ciPath = append(ciPath, path[:k]...)
  502. // we need to go deeper!
  503. if k < len(path) {
  504. if len(n.children) > 0 {
  505. // continue with child node
  506. n = n.children[0]
  507. loNPath = strings.ToLower(n.path)
  508. loPath = loPath[k:]
  509. path = path[k:]
  510. continue
  511. }
  512. // ... but we can't
  513. if fixTrailingSlash && len(path) == k+1 {
  514. return ciPath, true
  515. }
  516. return ciPath, false
  517. }
  518. if n.handle != nil {
  519. return ciPath, true
  520. } else if fixTrailingSlash && len(n.children) == 1 {
  521. // No handle found. Check if a handle for this path + a
  522. // trailing slash exists
  523. n = n.children[0]
  524. if n.path == "/" && n.handle != nil {
  525. return append(ciPath, '/'), true
  526. }
  527. }
  528. return ciPath, false
  529. case catchAll:
  530. return append(ciPath, path...), true
  531. default:
  532. panic("invalid node type")
  533. }
  534. } else {
  535. // We should have reached the node containing the handle.
  536. // Check if this node has a handle registered.
  537. if n.handle != nil {
  538. return ciPath, true
  539. }
  540. // No handle found.
  541. // Try to fix the path by adding a trailing slash
  542. if fixTrailingSlash {
  543. for i := 0; i < len(n.indices); i++ {
  544. if n.indices[i] == '/' {
  545. n = n.children[i]
  546. if (len(n.path) == 1 && n.handle != nil) ||
  547. (n.nType == catchAll && n.children[0].handle != nil) {
  548. return append(ciPath, '/'), true
  549. }
  550. return ciPath, false
  551. }
  552. }
  553. }
  554. return ciPath, false
  555. }
  556. }
  557. // Nothing found.
  558. // Try to fix the path by adding / removing a trailing slash
  559. if fixTrailingSlash {
  560. if path == "/" {
  561. return ciPath, true
  562. }
  563. if len(loPath)+1 == len(loNPath) && loNPath[len(loPath)] == '/' &&
  564. loPath[1:] == loNPath[1:len(loPath)] && n.handle != nil {
  565. return append(ciPath, n.path...), true
  566. }
  567. }
  568. return ciPath, false
  569. }