另客网go项目公用的代码库

tree.go 15KB

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