123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657 |
- package myth
- import (
- "strings"
- "unicode"
- "unicode/utf8"
- )
- func min(a, b int) int {
- if a <= b {
- return a
- }
- return b
- }
- func countParams(path string) uint8 {
- var n uint
- for i := 0; i < len(path); i++ {
- if path[i] != ':' && path[i] != '*' {
- continue
- }
- n++
- }
- if n >= 255 {
- return 255
- }
- return uint8(n)
- }
- type nodeType uint8
- const (
- static nodeType = iota
- root
- param
- catchAll
- )
- type node struct {
- path string
- wildChild bool
- nType nodeType
- maxParams uint8
- indices string
- children []*node
- handle Handle
- priority uint32
- }
- func (n *node) incrementChildPrio(pos int) int {
- n.children[pos].priority++
- prio := n.children[pos].priority
-
- newPos := pos
- for newPos > 0 && n.children[newPos-1].priority < prio {
-
- n.children[newPos-1], n.children[newPos] = n.children[newPos], n.children[newPos-1]
- newPos--
- }
-
- if newPos != pos {
- n.indices = n.indices[:newPos] +
- n.indices[pos:pos+1] +
- n.indices[newPos:pos] + n.indices[pos+1:]
- }
- return newPos
- }
- func (n *node) addRoute(path string, handle Handle) {
- fullPath := path
- n.priority++
- numParams := countParams(path)
-
- if len(n.path) > 0 || len(n.children) > 0 {
- walk:
- for {
-
- if numParams > n.maxParams {
- n.maxParams = numParams
- }
-
-
-
- i := 0
- max := min(len(path), len(n.path))
- for i < max && path[i] == n.path[i] {
- i++
- }
-
- if i < len(n.path) {
- child := node{
- path: n.path[i:],
- wildChild: n.wildChild,
- nType: static,
- indices: n.indices,
- children: n.children,
- handle: n.handle,
- priority: n.priority - 1,
- }
-
- for i := range child.children {
- if child.children[i].maxParams > child.maxParams {
- child.maxParams = child.children[i].maxParams
- }
- }
- n.children = []*node{&child}
-
- n.indices = string([]byte{n.path[i]})
- n.path = path[:i]
- n.handle = nil
- n.wildChild = false
- }
-
- if i < len(path) {
- path = path[i:]
- if n.wildChild {
- n = n.children[0]
- n.priority++
-
- if numParams > n.maxParams {
- n.maxParams = numParams
- }
- numParams--
-
- if len(path) >= len(n.path) && n.path == path[:len(n.path)] &&
-
- (len(n.path) >= len(path) || path[len(n.path)] == '/') {
- continue walk
- } else {
-
- var pathSeg string
- if n.nType == catchAll {
- pathSeg = path
- } else {
- pathSeg = strings.SplitN(path, "/", 2)[0]
- }
- prefix := fullPath[:strings.Index(fullPath, pathSeg)] + n.path
- panic("'" + pathSeg +
- "' in new path '" + fullPath +
- "' conflicts with existing wildcard '" + n.path +
- "' in existing prefix '" + prefix +
- "'")
- }
- }
- c := path[0]
-
- if n.nType == param && c == '/' && len(n.children) == 1 {
- n = n.children[0]
- n.priority++
- continue walk
- }
-
- for i := 0; i < len(n.indices); i++ {
- if c == n.indices[i] {
- i = n.incrementChildPrio(i)
- n = n.children[i]
- continue walk
- }
- }
-
- if c != ':' && c != '*' {
-
- n.indices += string([]byte{c})
- child := &node{
- maxParams: numParams,
- }
- n.children = append(n.children, child)
- n.incrementChildPrio(len(n.indices) - 1)
- n = child
- }
- n.insertChild(numParams, path, fullPath, handle)
- return
- } else if i == len(path) {
- if n.handle != nil {
- panic("a handle is already registered for path '" + fullPath + "'")
- }
- n.handle = handle
- }
- return
- }
- } else {
- n.insertChild(numParams, path, fullPath, handle)
- n.nType = root
- }
- }
- func (n *node) insertChild(numParams uint8, path, fullPath string, handle Handle) {
- var offset int
-
- for i, max := 0, len(path); numParams > 0; i++ {
- c := path[i]
- if c != ':' && c != '*' {
- continue
- }
-
- end := i + 1
- for end < max && path[end] != '/' {
- switch path[end] {
-
- case ':', '*':
- panic("only one wildcard per path segment is allowed, has: '" +
- path[i:] + "' in path '" + fullPath + "'")
- default:
- end++
- }
- }
-
-
- if len(n.children) > 0 {
- panic("wildcard route '" + path[i:end] +
- "' conflicts with existing children in path '" + fullPath + "'")
- }
-
- if end-i < 2 {
- panic("wildcards must be named with a non-empty name in path '" + fullPath + "'")
- }
- if c == ':' {
-
- if i > 0 {
- n.path = path[offset:i]
- offset = i
- }
- child := &node{
- nType: param,
- maxParams: numParams,
- }
- n.children = []*node{child}
- n.wildChild = true
- n = child
- n.priority++
- numParams--
-
-
- if end < max {
- n.path = path[offset:end]
- offset = end
- child := &node{
- maxParams: numParams,
- priority: 1,
- }
- n.children = []*node{child}
- n = child
- }
- } else {
- if end != max || numParams > 1 {
- panic("catch-all routes are only allowed at the end of the path in path '" + fullPath + "'")
- }
- if len(n.path) > 0 && n.path[len(n.path)-1] == '/' {
- panic("catch-all conflicts with existing handle for the path segment root in path '" + fullPath + "'")
- }
-
- i--
- if path[i] != '/' {
- panic("no / before catch-all in path '" + fullPath + "'")
- }
- n.path = path[offset:i]
-
- child := &node{
- wildChild: true,
- nType: catchAll,
- maxParams: 1,
- }
- n.children = []*node{child}
- n.indices = string(path[i])
- n = child
- n.priority++
-
- child = &node{
- path: path[i:],
- nType: catchAll,
- maxParams: 1,
- handle: handle,
- priority: 1,
- }
- n.children = []*node{child}
- return
- }
- }
-
- n.path = path[offset:]
- n.handle = handle
- }
- func (n *node) getValue(path string) (handle Handle, p map[string]string, tsr bool) {
- walk:
- for {
- if len(path) > len(n.path) {
- if path[:len(n.path)] == n.path {
- path = path[len(n.path):]
-
-
-
- if !n.wildChild {
- c := path[0]
- for i := 0; i < len(n.indices); i++ {
- if c == n.indices[i] {
- n = n.children[i]
- continue walk
- }
- }
-
-
-
- tsr = (path == "/" && n.handle != nil)
- return
- }
-
- n = n.children[0]
- switch n.nType {
- case param:
-
- end := 0
- for end < len(path) && path[end] != '/' {
- end++
- }
-
- if p == nil {
-
- p = make(map[string]string)
- }
-
-
-
-
- p[n.path[1:]] = path[:end]
-
- if end < len(path) {
- if len(n.children) > 0 {
- path = path[end:]
- n = n.children[0]
- continue walk
- }
-
- tsr = (len(path) == end+1)
- return
- }
- if handle = n.handle; handle != nil {
- return
- } else if len(n.children) == 1 {
-
-
- n = n.children[0]
- tsr = (n.path == "/" && n.handle != nil)
- }
- return
- case catchAll:
-
- if p == nil {
-
- p = make(map[string]string)
- }
-
-
-
-
- p[n.path[2:]] = path
- handle = n.handle
- return
- default:
- panic("invalid node type")
- }
- }
- } else if path == n.path {
-
-
- if handle = n.handle; handle != nil {
- return
- }
- if path == "/" && n.wildChild && n.nType != root {
- tsr = true
- return
- }
-
-
- for i := 0; i < len(n.indices); i++ {
- if n.indices[i] == '/' {
- n = n.children[i]
- tsr = (len(n.path) == 1 && n.handle != nil) ||
- (n.nType == catchAll && n.children[0].handle != nil)
- return
- }
- }
- return
- }
-
-
- tsr = (path == "/") ||
- (len(n.path) == len(path)+1 && n.path[len(path)] == '/' &&
- path == n.path[:len(n.path)-1] && n.handle != nil)
- return
- }
- }
- func (n *node) findCaseInsensitivePath(path string, fixTrailingSlash bool) (ciPath []byte, found bool) {
- return n.findCaseInsensitivePathRec(
- path,
- strings.ToLower(path),
- make([]byte, 0, len(path)+1),
- [4]byte{},
- fixTrailingSlash,
- )
- }
- func shiftNRuneBytes(rb [4]byte, n int) [4]byte {
- switch n {
- case 0:
- return rb
- case 1:
- return [4]byte{rb[1], rb[2], rb[3], 0}
- case 2:
- return [4]byte{rb[2], rb[3]}
- case 3:
- return [4]byte{rb[3]}
- default:
- return [4]byte{}
- }
- }
- func (n *node) findCaseInsensitivePathRec(path, loPath string, ciPath []byte, rb [4]byte, fixTrailingSlash bool) ([]byte, bool) {
- loNPath := strings.ToLower(n.path)
- walk:
- for len(loPath) >= len(loNPath) && (len(loNPath) == 0 || loPath[1:len(loNPath)] == loNPath[1:]) {
-
- ciPath = append(ciPath, n.path...)
- if path = path[len(n.path):]; len(path) > 0 {
- loOld := loPath
- loPath = loPath[len(loNPath):]
-
-
-
- if !n.wildChild {
-
- rb = shiftNRuneBytes(rb, len(loNPath))
- if rb[0] != 0 {
-
- for i := 0; i < len(n.indices); i++ {
- if n.indices[i] == rb[0] {
-
- n = n.children[i]
- loNPath = strings.ToLower(n.path)
- continue walk
- }
- }
- } else {
-
- var rv rune
-
-
-
- var off int
- for max := min(len(loNPath), 3); off < max; off++ {
- if i := len(loNPath) - off; utf8.RuneStart(loOld[i]) {
-
- rv, _ = utf8.DecodeRuneInString(loOld[i:])
- break
- }
- }
-
- utf8.EncodeRune(rb[:], rv)
-
- rb = shiftNRuneBytes(rb, off)
- for i := 0; i < len(n.indices); i++ {
-
- if n.indices[i] == rb[0] {
-
-
-
- if out, found := n.children[i].findCaseInsensitivePathRec(
- path, loPath, ciPath, rb, fixTrailingSlash,
- ); found {
- return out, true
- }
- break
- }
- }
-
- if up := unicode.ToUpper(rv); up != rv {
- utf8.EncodeRune(rb[:], up)
- rb = shiftNRuneBytes(rb, off)
- for i := 0; i < len(n.indices); i++ {
-
- if n.indices[i] == rb[0] {
-
- n = n.children[i]
- loNPath = strings.ToLower(n.path)
- continue walk
- }
- }
- }
- }
-
-
- return ciPath, (fixTrailingSlash && path == "/" && n.handle != nil)
- }
- n = n.children[0]
- switch n.nType {
- case param:
-
- k := 0
- for k < len(path) && path[k] != '/' {
- k++
- }
-
- ciPath = append(ciPath, path[:k]...)
-
- if k < len(path) {
- if len(n.children) > 0 {
-
- n = n.children[0]
- loNPath = strings.ToLower(n.path)
- loPath = loPath[k:]
- path = path[k:]
- continue
- }
-
- if fixTrailingSlash && len(path) == k+1 {
- return ciPath, true
- }
- return ciPath, false
- }
- if n.handle != nil {
- return ciPath, true
- } else if fixTrailingSlash && len(n.children) == 1 {
-
-
- n = n.children[0]
- if n.path == "/" && n.handle != nil {
- return append(ciPath, '/'), true
- }
- }
- return ciPath, false
- case catchAll:
- return append(ciPath, path...), true
- default:
- panic("invalid node type")
- }
- } else {
-
-
- if n.handle != nil {
- return ciPath, true
- }
-
-
- if fixTrailingSlash {
- for i := 0; i < len(n.indices); i++ {
- if n.indices[i] == '/' {
- n = n.children[i]
- if (len(n.path) == 1 && n.handle != nil) ||
- (n.nType == catchAll && n.children[0].handle != nil) {
- return append(ciPath, '/'), true
- }
- return ciPath, false
- }
- }
- }
- return ciPath, false
- }
- }
-
-
- if fixTrailingSlash {
- if path == "/" {
- return ciPath, true
- }
- if len(loPath)+1 == len(loNPath) && loNPath[len(loPath)] == '/' &&
- loPath[1:] == loNPath[1:len(loPath)] && n.handle != nil {
- return append(ciPath, n.path...), true
- }
- }
- return ciPath, false
- }
|