2021-09-27 23:30:43 +03:00
|
|
|
package http
|
|
|
|
|
2021-11-09 16:34:05 +03:00
|
|
|
// Radix tree implementation below is a based on the original work by
|
|
|
|
// Armon Dadgar in https://github.com/armon/go-radix/blob/master/radix.go
|
|
|
|
// (MIT licensed). It's been heavily modified for use as a HTTP routing tree.
|
|
|
|
// Copied from chi mux tree.go https://raw.githubusercontent.com/go-chi/chi/master/tree.go
|
2021-11-09 17:07:52 +03:00
|
|
|
// Modified by Unistack LLC to support interface{} type handler and parameters in map[string]string
|
2021-11-09 16:34:05 +03:00
|
|
|
|
2021-09-27 23:30:43 +03:00
|
|
|
import (
|
2022-12-27 23:47:11 +03:00
|
|
|
"errors"
|
2021-11-09 16:34:05 +03:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2021-09-27 23:30:43 +03:00
|
|
|
"regexp"
|
2021-11-09 16:34:05 +03:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
2021-09-27 23:30:43 +03:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2022-12-27 23:47:11 +03:00
|
|
|
var (
|
|
|
|
ErrNotFound = errors.New("route not found")
|
|
|
|
ErrMethodNotAllowed = errors.New("method not allowed")
|
|
|
|
)
|
|
|
|
|
2021-11-09 16:34:05 +03:00
|
|
|
type methodTyp uint
|
2021-09-29 13:41:47 +03:00
|
|
|
|
2021-11-09 16:34:05 +03:00
|
|
|
const (
|
|
|
|
mSTUB methodTyp = 1 << iota
|
|
|
|
mCONNECT
|
|
|
|
mDELETE
|
|
|
|
mGET
|
|
|
|
mHEAD
|
|
|
|
mOPTIONS
|
|
|
|
mPATCH
|
|
|
|
mPOST
|
|
|
|
mPUT
|
|
|
|
mTRACE
|
|
|
|
)
|
2021-09-29 13:41:47 +03:00
|
|
|
|
2021-11-09 16:34:05 +03:00
|
|
|
var mALL = mCONNECT | mDELETE | mGET | mHEAD |
|
|
|
|
mOPTIONS | mPATCH | mPOST | mPUT | mTRACE
|
2021-09-29 13:41:47 +03:00
|
|
|
|
2021-11-09 16:34:05 +03:00
|
|
|
var methodMap = map[string]methodTyp{
|
|
|
|
http.MethodConnect: mCONNECT,
|
|
|
|
http.MethodDelete: mDELETE,
|
|
|
|
http.MethodGet: mGET,
|
|
|
|
http.MethodHead: mHEAD,
|
|
|
|
http.MethodOptions: mOPTIONS,
|
|
|
|
http.MethodPatch: mPATCH,
|
|
|
|
http.MethodPost: mPOST,
|
|
|
|
http.MethodPut: mPUT,
|
|
|
|
http.MethodTrace: mTRACE,
|
2021-09-27 23:30:43 +03:00
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// RegisterMethod adds support for custom HTTP method handlers
|
2021-11-09 16:34:05 +03:00
|
|
|
func RegisterMethod(method string) error {
|
|
|
|
if method == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
method = strings.ToUpper(method)
|
|
|
|
if _, ok := methodMap[method]; ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
n := len(methodMap)
|
|
|
|
if n > strconv.IntSize-2 {
|
|
|
|
return fmt.Errorf("max number of methods reached (%d)", strconv.IntSize)
|
|
|
|
}
|
|
|
|
mt := methodTyp(2 << n)
|
|
|
|
methodMap[method] = mt
|
|
|
|
mALL |= mt
|
|
|
|
|
|
|
|
return nil
|
2021-09-27 23:30:43 +03:00
|
|
|
}
|
|
|
|
|
2021-11-09 16:34:05 +03:00
|
|
|
type nodeTyp uint8
|
|
|
|
|
2021-09-27 23:30:43 +03:00
|
|
|
const (
|
2021-11-09 16:34:05 +03:00
|
|
|
ntStatic nodeTyp = iota // /home
|
|
|
|
ntRegexp // /{id:[0-9]+}
|
|
|
|
ntParam // /{user}
|
|
|
|
ntCatchAll // /api/v1/*
|
2021-09-27 23:30:43 +03:00
|
|
|
)
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// NewTrie create new tree
|
|
|
|
func NewTrie() *Trie {
|
|
|
|
return &Trie{}
|
2021-11-09 16:34:05 +03:00
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// Trie holds nodes for path based tree search
|
|
|
|
type Trie struct {
|
2021-11-09 16:34:05 +03:00
|
|
|
// regexp matcher for regexp nodes
|
|
|
|
rex *regexp.Regexp
|
|
|
|
|
|
|
|
// HTTP handler endpoints on the leaf node
|
|
|
|
endpoints endpoints
|
|
|
|
|
|
|
|
// prefix is the common prefix we ignore
|
|
|
|
prefix string
|
|
|
|
|
|
|
|
// child nodes should be stored in-order for iteration,
|
|
|
|
// in groups of the node type.
|
|
|
|
children [ntCatchAll + 1]nodes
|
|
|
|
|
|
|
|
// first byte of the child prefix
|
|
|
|
tail byte
|
|
|
|
|
|
|
|
// node type: static, regexp, param, catchAll
|
|
|
|
typ nodeTyp
|
|
|
|
|
|
|
|
// first byte of the prefix
|
|
|
|
label byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// endpoints is a mapping of http method constants to handlers
|
|
|
|
// for a given route.
|
|
|
|
type endpoints map[methodTyp]*endpoint
|
|
|
|
|
|
|
|
type endpoint struct {
|
|
|
|
// endpoint handler
|
|
|
|
handler interface{}
|
|
|
|
// pattern is the routing pattern for handler nodes
|
|
|
|
pattern string
|
2021-11-09 17:07:52 +03:00
|
|
|
// parameters keys recorded on handler nodes
|
|
|
|
paramKeys []string
|
2021-11-09 16:34:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s endpoints) Value(method methodTyp) *endpoint {
|
|
|
|
mh, ok := s[method]
|
|
|
|
if !ok {
|
|
|
|
mh = &endpoint{}
|
|
|
|
s[method] = mh
|
|
|
|
}
|
|
|
|
|
|
|
|
return mh
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// Insert add elemenent to tree
|
|
|
|
func (n *Trie) Insert(methods []string, pattern string, handler interface{}) error {
|
2021-11-09 16:34:05 +03:00
|
|
|
var err error
|
|
|
|
for _, method := range methods {
|
|
|
|
if err = n.insert(methodMap[method], pattern, handler); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
func (n *Trie) insert(method methodTyp, pattern string, handler interface{}) error {
|
|
|
|
var parent *Trie
|
2021-11-09 16:34:05 +03:00
|
|
|
search := pattern
|
|
|
|
|
|
|
|
for {
|
|
|
|
// Handle key exhaustion
|
|
|
|
if len(search) == 0 {
|
|
|
|
// Insert or update the node's leaf handler
|
|
|
|
return n.setEndpoint(method, handler, pattern)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're going to be searching for a wild node next,
|
|
|
|
// in this case, we need to get the tail
|
|
|
|
label := search[0]
|
|
|
|
var segTail byte
|
|
|
|
var segEndIdx int
|
|
|
|
var segTyp nodeTyp
|
|
|
|
var segRexpat string
|
|
|
|
var err error
|
|
|
|
if label == '{' || label == '*' {
|
|
|
|
segTyp, _, segRexpat, segTail, _, segEndIdx, err = patNextSegment(search)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var prefix string
|
|
|
|
if segTyp == ntRegexp {
|
|
|
|
prefix = segRexpat
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for the edge to attach to
|
|
|
|
parent = n
|
|
|
|
n = n.getEdge(segTyp, label, segTail, prefix)
|
|
|
|
|
|
|
|
// No edge, create one
|
|
|
|
if n == nil {
|
2022-01-10 16:47:37 +03:00
|
|
|
child := &Trie{typ: ntStatic, label: label, tail: segTail, prefix: search}
|
|
|
|
var hn *Trie
|
2021-11-09 16:34:05 +03:00
|
|
|
hn, err = parent.addChild(child, search)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-09-27 23:30:43 +03:00
|
|
|
}
|
2021-11-09 16:34:05 +03:00
|
|
|
return hn.setEndpoint(method, handler, pattern)
|
2021-09-27 23:30:43 +03:00
|
|
|
}
|
2021-11-09 16:34:05 +03:00
|
|
|
|
|
|
|
// Found an edge to match the pattern
|
|
|
|
|
|
|
|
if n.typ > ntStatic {
|
|
|
|
// We found a param node, trim the param from the search path and continue.
|
|
|
|
// This param/wild pattern segment would already be on the tree from a previous
|
|
|
|
// call to addChild when creating a new node.
|
|
|
|
search = search[segEndIdx:]
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Static nodes fall below here.
|
|
|
|
// Determine longest prefix of the search key on match.
|
|
|
|
commonPrefix := longestPrefix(search, n.prefix)
|
|
|
|
if commonPrefix == len(n.prefix) {
|
|
|
|
// the common prefix is as long as the current node's prefix we're attempting to insert.
|
|
|
|
// keep the search going.
|
|
|
|
search = search[commonPrefix:]
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split the node
|
2022-01-10 16:47:37 +03:00
|
|
|
child := &Trie{
|
2021-11-09 16:34:05 +03:00
|
|
|
typ: ntStatic,
|
|
|
|
prefix: search[:commonPrefix],
|
|
|
|
}
|
|
|
|
if err = parent.replaceChild(search[0], segTail, child); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore the existing node
|
|
|
|
n.label = n.prefix[commonPrefix]
|
|
|
|
n.prefix = n.prefix[commonPrefix:]
|
|
|
|
if _, err = child.addChild(n, n.prefix); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the new key is a subset, set the method/handler on this node and finish.
|
|
|
|
search = search[commonPrefix:]
|
|
|
|
if len(search) == 0 {
|
|
|
|
return child.setEndpoint(method, handler, pattern)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new edge for the node
|
2022-01-10 16:47:37 +03:00
|
|
|
subchild := &Trie{
|
2021-11-09 16:34:05 +03:00
|
|
|
typ: ntStatic,
|
|
|
|
label: search[0],
|
|
|
|
prefix: search,
|
|
|
|
}
|
2022-01-10 16:47:37 +03:00
|
|
|
var hn *Trie
|
2021-11-09 16:34:05 +03:00
|
|
|
hn, err = child.addChild(subchild, search)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return hn.setEndpoint(method, handler, pattern)
|
2021-09-27 23:30:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-09 16:34:05 +03:00
|
|
|
// addChild appends the new `child` node to the tree using the `pattern` as the trie key.
|
|
|
|
// For a URL router like chi's, we split the static, param, regexp and wildcard segments
|
|
|
|
// into different nodes. In addition, addChild will recursively call itself until every
|
|
|
|
// pattern segment is added to the url pattern tree as individual nodes, depending on type.
|
2022-01-10 16:47:37 +03:00
|
|
|
func (n *Trie) addChild(child *Trie, prefix string) (*Trie, error) {
|
2021-11-09 16:34:05 +03:00
|
|
|
search := prefix
|
|
|
|
|
|
|
|
// handler leaf node added to the tree is the child.
|
|
|
|
// this may be overridden later down the flow
|
|
|
|
hn := child
|
2021-09-27 23:30:43 +03:00
|
|
|
|
2021-11-09 16:34:05 +03:00
|
|
|
// Parse next segment
|
|
|
|
segTyp, _, segRexpat, segTail, segStartIdx, segEndIdx, err := patNextSegment(search)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-09-29 13:41:47 +03:00
|
|
|
}
|
2021-11-09 16:34:05 +03:00
|
|
|
// Add child depending on next up segment
|
|
|
|
switch segTyp {
|
2021-09-29 13:41:47 +03:00
|
|
|
|
2021-11-09 16:34:05 +03:00
|
|
|
case ntStatic:
|
|
|
|
// Search prefix is all static (that is, has no params in path)
|
|
|
|
// noop
|
2021-09-29 13:41:47 +03:00
|
|
|
|
2021-11-09 16:34:05 +03:00
|
|
|
default:
|
|
|
|
// Search prefix contains a param, regexp or wildcard
|
|
|
|
|
|
|
|
if segTyp == ntRegexp {
|
|
|
|
rex, err := regexp.Compile(segRexpat)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid regexp pattern '%s' in route param", segRexpat)
|
|
|
|
}
|
|
|
|
child.prefix = segRexpat
|
|
|
|
child.rex = rex
|
2021-09-29 13:41:47 +03:00
|
|
|
}
|
2021-11-09 16:34:05 +03:00
|
|
|
|
2021-11-09 17:07:52 +03:00
|
|
|
switch {
|
|
|
|
case segStartIdx == 0:
|
2021-11-09 16:34:05 +03:00
|
|
|
// Route starts with a param
|
|
|
|
child.typ = segTyp
|
|
|
|
|
|
|
|
if segTyp == ntCatchAll {
|
|
|
|
segStartIdx = -1
|
|
|
|
} else {
|
|
|
|
segStartIdx = segEndIdx
|
|
|
|
}
|
|
|
|
if segStartIdx < 0 {
|
|
|
|
segStartIdx = len(search)
|
|
|
|
}
|
|
|
|
child.tail = segTail // for params, we set the tail
|
|
|
|
|
|
|
|
if segStartIdx != len(search) {
|
|
|
|
// add static edge for the remaining part, split the end.
|
|
|
|
// its not possible to have adjacent param nodes, so its certainly
|
|
|
|
// going to be a static node next.
|
|
|
|
|
|
|
|
search = search[segStartIdx:] // advance search position
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
nn := &Trie{
|
2021-11-09 16:34:05 +03:00
|
|
|
typ: ntStatic,
|
|
|
|
label: search[0],
|
|
|
|
prefix: search,
|
2021-09-29 13:41:47 +03:00
|
|
|
}
|
2021-11-09 16:34:05 +03:00
|
|
|
var err error
|
|
|
|
hn, err = child.addChild(nn, search)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-09 17:07:52 +03:00
|
|
|
case segStartIdx > 0:
|
2021-11-09 16:34:05 +03:00
|
|
|
// Route has some param
|
|
|
|
|
|
|
|
// starts with a static segment
|
|
|
|
child.typ = ntStatic
|
|
|
|
child.prefix = search[:segStartIdx]
|
|
|
|
child.rex = nil
|
|
|
|
|
|
|
|
// add the param edge node
|
|
|
|
search = search[segStartIdx:]
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
nn := &Trie{
|
2021-11-09 16:34:05 +03:00
|
|
|
typ: segTyp,
|
|
|
|
label: search[0],
|
|
|
|
tail: segTail,
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
hn, err = child.addChild(nn, search)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-09-29 13:41:47 +03:00
|
|
|
}
|
2021-09-27 23:30:43 +03:00
|
|
|
}
|
2021-11-09 16:34:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
n.children[child.typ] = append(n.children[child.typ], child)
|
|
|
|
n.children[child.typ].Sort()
|
|
|
|
return hn, nil
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
func (n *Trie) replaceChild(label, tail byte, child *Trie) error {
|
2021-11-09 16:34:05 +03:00
|
|
|
for i := 0; i < len(n.children[child.typ]); i++ {
|
|
|
|
if n.children[child.typ][i].label == label && n.children[child.typ][i].tail == tail {
|
|
|
|
n.children[child.typ][i] = child
|
|
|
|
n.children[child.typ][i].label = label
|
|
|
|
n.children[child.typ][i].tail = tail
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("replacing missing child")
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
func (n *Trie) getEdge(ntyp nodeTyp, label, tail byte, prefix string) *Trie {
|
2021-11-09 16:34:05 +03:00
|
|
|
nds := n.children[ntyp]
|
|
|
|
for i := 0; i < len(nds); i++ {
|
|
|
|
if nds[i].label == label && nds[i].tail == tail {
|
|
|
|
if ntyp == ntRegexp && nds[i].prefix != prefix {
|
|
|
|
continue
|
2021-09-27 23:30:43 +03:00
|
|
|
}
|
2021-11-09 16:34:05 +03:00
|
|
|
return nds[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
func (n *Trie) setEndpoint(method methodTyp, handler interface{}, pattern string) error {
|
2021-11-09 16:34:05 +03:00
|
|
|
// Set the handler for the method type on the node
|
|
|
|
if n.endpoints == nil {
|
|
|
|
n.endpoints = make(endpoints)
|
|
|
|
}
|
|
|
|
|
|
|
|
paramKeys, err := patParamKeys(pattern)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if method&mSTUB == mSTUB {
|
|
|
|
n.endpoints.Value(mSTUB).handler = handler
|
|
|
|
}
|
|
|
|
if method&mALL == mALL {
|
|
|
|
h := n.endpoints.Value(mALL)
|
|
|
|
h.handler = handler
|
|
|
|
h.pattern = pattern
|
|
|
|
h.paramKeys = paramKeys
|
|
|
|
for _, m := range methodMap {
|
|
|
|
h := n.endpoints.Value(m)
|
|
|
|
h.handler = handler
|
|
|
|
h.pattern = pattern
|
|
|
|
h.paramKeys = paramKeys
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
h := n.endpoints.Value(method)
|
|
|
|
h.handler = handler
|
|
|
|
h.pattern = pattern
|
|
|
|
h.paramKeys = paramKeys
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// Search try to find element in tree with path and method
|
2022-12-27 23:47:11 +03:00
|
|
|
func (n *Trie) Search(method string, path string) (interface{}, map[string]string, error) {
|
2021-11-09 16:34:05 +03:00
|
|
|
params := &routeParams{}
|
|
|
|
// Find the routing handlers for the path
|
|
|
|
rn := n.findRoute(params, methodMap[method], path)
|
2022-12-27 23:47:11 +03:00
|
|
|
if rn == nil && !params.methodNotAllowed {
|
|
|
|
return nil, nil, ErrNotFound
|
|
|
|
}
|
|
|
|
if params.methodNotAllowed {
|
|
|
|
return nil, nil, ErrMethodNotAllowed
|
2021-11-09 16:34:05 +03:00
|
|
|
}
|
|
|
|
ep, ok := rn.endpoints[methodMap[method]]
|
|
|
|
if !ok {
|
2022-12-27 23:47:11 +03:00
|
|
|
return nil, nil, ErrMethodNotAllowed
|
2021-11-09 16:34:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
eparams := make(map[string]string, len(params.keys))
|
|
|
|
for idx, key := range params.keys {
|
|
|
|
eparams[key] = params.vals[idx]
|
|
|
|
}
|
|
|
|
|
2022-12-27 23:47:11 +03:00
|
|
|
return ep.handler, eparams, nil
|
2021-11-09 16:34:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type routeParams struct {
|
2022-12-27 23:47:11 +03:00
|
|
|
keys []string
|
|
|
|
vals []string
|
|
|
|
methodNotAllowed bool
|
2021-11-09 16:34:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Recursive edge traversal by checking all nodeTyp groups along the way.
|
|
|
|
// It's like searching through a multi-dimensional radix trie.
|
2022-01-10 16:47:37 +03:00
|
|
|
func (n *Trie) findRoute(params *routeParams, method methodTyp, path string) *Trie {
|
2021-11-09 16:34:05 +03:00
|
|
|
nn := n
|
|
|
|
search := path
|
|
|
|
|
|
|
|
for t, nds := range nn.children {
|
|
|
|
ntyp := nodeTyp(t)
|
|
|
|
if len(nds) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
var xn *Trie
|
2021-11-09 16:34:05 +03:00
|
|
|
xsearch := search
|
|
|
|
|
|
|
|
var label byte
|
|
|
|
if search != "" {
|
|
|
|
label = search[0]
|
2021-09-27 23:30:43 +03:00
|
|
|
}
|
2021-11-09 16:34:05 +03:00
|
|
|
|
|
|
|
switch ntyp {
|
|
|
|
case ntStatic:
|
|
|
|
xn = nds.findEdge(label)
|
|
|
|
if xn == nil || !strings.HasPrefix(xsearch, xn.prefix) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
xsearch = xsearch[len(xn.prefix):]
|
|
|
|
|
|
|
|
case ntParam, ntRegexp:
|
|
|
|
// short-circuit and return no matching route for empty param values
|
|
|
|
if xsearch == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// serially loop through each node grouped by the tail delimiter
|
|
|
|
for idx := 0; idx < len(nds); idx++ {
|
|
|
|
xn = nds[idx]
|
|
|
|
|
|
|
|
// label for param nodes is the delimiter byte
|
|
|
|
p := strings.IndexByte(xsearch, xn.tail)
|
|
|
|
|
|
|
|
if p < 0 {
|
|
|
|
if xn.tail == '/' {
|
|
|
|
p = len(xsearch)
|
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else if ntyp == ntRegexp && p == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if ntyp == ntRegexp && xn.rex != nil {
|
|
|
|
if !xn.rex.MatchString(xsearch[:p]) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else if strings.IndexByte(xsearch[:p], '/') != -1 {
|
|
|
|
// avoid a match across path segments
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
prevlen := len(params.vals)
|
|
|
|
params.vals = append(params.vals, xsearch[:p])
|
|
|
|
xsearch = xsearch[p:]
|
|
|
|
|
|
|
|
if len(xsearch) == 0 {
|
|
|
|
if xn.isLeaf() {
|
|
|
|
h := xn.endpoints[method]
|
|
|
|
if h != nil && h.handler != nil {
|
|
|
|
params.keys = append(params.keys, h.paramKeys...)
|
|
|
|
return xn
|
|
|
|
}
|
2022-12-27 23:47:11 +03:00
|
|
|
params.methodNotAllowed = true
|
2021-09-27 23:30:43 +03:00
|
|
|
}
|
|
|
|
}
|
2021-11-09 16:34:05 +03:00
|
|
|
|
|
|
|
// recursively find the next node on this branch
|
|
|
|
fin := xn.findRoute(params, method, xsearch)
|
|
|
|
if fin != nil {
|
|
|
|
return fin
|
2021-09-27 23:30:43 +03:00
|
|
|
}
|
2021-11-09 16:34:05 +03:00
|
|
|
|
|
|
|
// not found on this branch, reset vars
|
|
|
|
params.vals = params.vals[:prevlen]
|
|
|
|
xsearch = search
|
2021-09-27 23:30:43 +03:00
|
|
|
}
|
2021-11-09 16:34:05 +03:00
|
|
|
|
|
|
|
params.vals = append(params.vals, "")
|
|
|
|
|
|
|
|
default:
|
|
|
|
// catch-all nodes
|
|
|
|
params.vals = append(params.vals, search)
|
|
|
|
xn = nds[0]
|
|
|
|
xsearch = ""
|
2021-09-27 23:30:43 +03:00
|
|
|
}
|
2021-11-09 16:34:05 +03:00
|
|
|
|
|
|
|
if xn == nil {
|
|
|
|
continue
|
2021-09-27 23:30:43 +03:00
|
|
|
}
|
2021-11-09 16:34:05 +03:00
|
|
|
|
|
|
|
// did we find it yet?
|
|
|
|
if len(xsearch) == 0 {
|
|
|
|
if xn.isLeaf() {
|
|
|
|
h := xn.endpoints[method]
|
|
|
|
if h != nil && h.handler != nil {
|
|
|
|
params.keys = append(params.keys, h.paramKeys...)
|
|
|
|
return xn
|
|
|
|
}
|
2022-12-27 23:47:11 +03:00
|
|
|
params.methodNotAllowed = true
|
2021-11-09 16:34:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// recursively find the next node..
|
|
|
|
fin := xn.findRoute(params, method, xsearch)
|
|
|
|
if fin != nil {
|
|
|
|
return fin
|
|
|
|
}
|
|
|
|
|
|
|
|
// Did not find final handler, let's remove the param here if it was set
|
|
|
|
if xn.typ > ntStatic {
|
|
|
|
if len(params.vals) > 0 {
|
|
|
|
params.vals = params.vals[:len(params.vals)-1]
|
|
|
|
}
|
2021-09-27 23:30:43 +03:00
|
|
|
}
|
2021-11-09 16:34:05 +03:00
|
|
|
|
2021-09-27 23:30:43 +03:00
|
|
|
}
|
|
|
|
|
2021-11-09 16:34:05 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
func (n *Trie) isLeaf() bool {
|
2021-11-09 16:34:05 +03:00
|
|
|
return n.endpoints != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// patNextSegment returns the next segment details from a pattern:
|
|
|
|
// node type, param key, regexp string, param tail byte, param starting index, param ending index
|
|
|
|
func patNextSegment(pattern string) (nodeTyp, string, string, byte, int, int, error) {
|
|
|
|
ps := strings.Index(pattern, "{")
|
|
|
|
ws := strings.Index(pattern, "*")
|
|
|
|
|
|
|
|
if ps < 0 && ws < 0 {
|
|
|
|
return ntStatic, "", "", 0, 0, len(pattern), nil // we return the entire thing
|
2021-09-27 23:30:43 +03:00
|
|
|
}
|
|
|
|
|
2021-11-09 16:34:05 +03:00
|
|
|
// Sanity check
|
|
|
|
if ps >= 0 && ws >= 0 && ws < ps {
|
|
|
|
return ntStatic, "", "", 0, 0, 0, fmt.Errorf("wildcard '*' must be the last pattern in a route, otherwise use a '{param}'")
|
|
|
|
}
|
|
|
|
|
|
|
|
var tail byte = '/' // Default endpoint tail to / byte
|
|
|
|
|
|
|
|
if ps < 0 {
|
|
|
|
// Wildcard pattern as finale
|
|
|
|
if ws < len(pattern)-1 {
|
|
|
|
return ntStatic, "", "", 0, 0, 0, fmt.Errorf("wildcard '*' must be the last value in a route. trim trailing text or use a '{param}' instead")
|
|
|
|
}
|
|
|
|
|
|
|
|
return ntCatchAll, "*", "", 0, ws, len(pattern), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Param/Regexp pattern is next
|
|
|
|
nt := ntParam
|
|
|
|
|
|
|
|
// Read to closing } taking into account opens and closes in curl count (cc)
|
|
|
|
cc := 0
|
|
|
|
pe := ps
|
|
|
|
for i, c := range pattern[ps:] {
|
|
|
|
if c == '{' {
|
|
|
|
cc++
|
|
|
|
} else if c == '}' {
|
|
|
|
cc--
|
|
|
|
if cc == 0 {
|
|
|
|
pe = ps + i
|
|
|
|
break
|
2021-09-27 23:30:43 +03:00
|
|
|
}
|
|
|
|
}
|
2021-11-09 16:34:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if pe == ps {
|
|
|
|
return ntStatic, "", "", 0, 0, 0, fmt.Errorf("route param closing delimiter '}' is missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
key := pattern[ps+1 : pe]
|
|
|
|
pe++ // set end to next position
|
|
|
|
|
|
|
|
if pe < len(pattern) {
|
|
|
|
tail = pattern[pe]
|
|
|
|
}
|
|
|
|
|
|
|
|
var rexpat string
|
|
|
|
if idx := strings.Index(key, ":"); idx >= 0 {
|
|
|
|
nt = ntRegexp
|
|
|
|
rexpat = key[idx+1:]
|
|
|
|
key = key[:idx]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rexpat) > 0 {
|
|
|
|
if rexpat[0] != '^' {
|
|
|
|
rexpat = "^" + rexpat
|
|
|
|
}
|
|
|
|
if rexpat[len(rexpat)-1] != '$' {
|
|
|
|
rexpat += "$"
|
|
|
|
}
|
|
|
|
}
|
2021-09-27 23:30:43 +03:00
|
|
|
|
2021-11-09 16:34:05 +03:00
|
|
|
return nt, key, rexpat, tail, ps, pe, nil
|
|
|
|
}
|
2021-09-27 23:30:43 +03:00
|
|
|
|
2021-11-09 16:34:05 +03:00
|
|
|
func patParamKeys(pattern string) ([]string, error) {
|
|
|
|
pat := pattern
|
|
|
|
paramKeys := []string{}
|
|
|
|
for {
|
|
|
|
ptyp, paramKey, _, _, _, e, err := patNextSegment(pat)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if ptyp == ntStatic {
|
|
|
|
return paramKeys, nil
|
|
|
|
}
|
|
|
|
for i := 0; i < len(paramKeys); i++ {
|
|
|
|
if paramKeys[i] == paramKey {
|
|
|
|
return nil, fmt.Errorf("routing pattern '%s' contains duplicate param key, '%s'", pattern, paramKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
paramKeys = append(paramKeys, paramKey)
|
|
|
|
pat = pat[e:]
|
|
|
|
}
|
2021-09-27 23:30:43 +03:00
|
|
|
}
|
|
|
|
|
2021-11-09 16:34:05 +03:00
|
|
|
// longestPrefix finds the length of the shared prefix
|
|
|
|
// of two strings
|
|
|
|
func longestPrefix(k1, k2 string) int {
|
|
|
|
max := len(k1)
|
|
|
|
if l := len(k2); l < max {
|
|
|
|
max = l
|
|
|
|
}
|
|
|
|
var i int
|
|
|
|
for i = 0; i < max; i++ {
|
|
|
|
if k1[i] != k2[i] {
|
|
|
|
break
|
2021-09-27 23:30:43 +03:00
|
|
|
}
|
|
|
|
}
|
2021-11-09 16:34:05 +03:00
|
|
|
return i
|
2021-09-27 23:30:43 +03:00
|
|
|
}
|
2021-09-29 13:41:47 +03:00
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
type nodes []*Trie
|
2021-11-09 16:34:05 +03:00
|
|
|
|
|
|
|
// Sort the list of nodes by label
|
|
|
|
func (ns nodes) Sort() { sort.Sort(ns); ns.tailSort() }
|
|
|
|
func (ns nodes) Len() int { return len(ns) }
|
|
|
|
func (ns nodes) Swap(i, j int) { ns[i], ns[j] = ns[j], ns[i] }
|
|
|
|
func (ns nodes) Less(i, j int) bool { return ns[i].label < ns[j].label }
|
|
|
|
|
|
|
|
// tailSort pushes nodes with '/' as the tail to the end of the list for param nodes.
|
|
|
|
// The list order determines the traversal order.
|
|
|
|
func (ns nodes) tailSort() {
|
|
|
|
for i := len(ns) - 1; i >= 0; i-- {
|
|
|
|
if ns[i].typ > ntStatic && ns[i].tail == '/' {
|
|
|
|
ns.Swap(i, len(ns)-1)
|
|
|
|
return
|
|
|
|
}
|
2021-09-29 13:41:47 +03:00
|
|
|
}
|
2021-11-09 16:34:05 +03:00
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
func (ns nodes) findEdge(label byte) *Trie {
|
2021-11-09 16:34:05 +03:00
|
|
|
num := len(ns)
|
|
|
|
idx := 0
|
|
|
|
i, j := 0, num-1
|
|
|
|
for i <= j {
|
|
|
|
idx = i + (j-i)/2
|
|
|
|
if label > ns[idx].label {
|
|
|
|
i = idx + 1
|
|
|
|
} else if label < ns[idx].label {
|
|
|
|
j = idx - 1
|
|
|
|
} else {
|
|
|
|
i = num // breaks cond
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ns[idx].label != label {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ns[idx]
|
2021-09-29 13:41:47 +03:00
|
|
|
}
|