5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-10 11:00:27 +00:00
matterbridge/vendor/github.com/labstack/echo/v4/router.go

468 lines
9.5 KiB
Go
Raw Normal View History

2017-02-18 22:00:46 +00:00
package echo
2020-01-09 20:02:56 +00:00
import (
"net/http"
"strings"
)
2017-02-18 22:00:46 +00:00
type (
// Router is the registry of all registered routes for an `Echo` instance for
// request matching and URL path parameter parsing.
Router struct {
tree *node
2017-06-05 22:01:05 +00:00
routes map[string]*Route
2017-02-18 22:00:46 +00:00
echo *Echo
}
node struct {
kind kind
label byte
prefix string
parent *node
children children
ppath string
pnames []string
methodHandler *methodHandler
}
2020-01-09 20:02:56 +00:00
kind uint8
children []*node
2017-02-18 22:00:46 +00:00
methodHandler struct {
connect HandlerFunc
delete HandlerFunc
get HandlerFunc
head HandlerFunc
options HandlerFunc
patch HandlerFunc
post HandlerFunc
propfind HandlerFunc
put HandlerFunc
trace HandlerFunc
2019-06-16 21:33:25 +00:00
report HandlerFunc
2017-02-18 22:00:46 +00:00
}
)
const (
skind kind = iota
pkind
akind
)
// NewRouter returns a new Router instance.
func NewRouter(e *Echo) *Router {
return &Router{
tree: &node{
methodHandler: new(methodHandler),
},
2017-06-05 22:01:05 +00:00
routes: map[string]*Route{},
2017-02-18 22:00:46 +00:00
echo: e,
}
}
// Add registers a new route for method and path with matching handler.
func (r *Router) Add(method, path string, h HandlerFunc) {
// Validate path
if path == "" {
2019-06-16 21:33:25 +00:00
path = "/"
2017-02-18 22:00:46 +00:00
}
if path[0] != '/' {
path = "/" + path
}
pnames := []string{} // Param names
ppath := path // Pristine path
2017-02-18 22:00:46 +00:00
for i, l := 0, len(path); i < l; i++ {
if path[i] == ':' {
j := i + 1
r.insert(method, path[:i], nil, skind, "", nil)
for ; i < l && path[i] != '/'; i++ {
}
pnames = append(pnames, path[j:i])
path = path[:j] + path[i:]
i, l = j, len(path)
if i == l {
r.insert(method, path[:i], h, pkind, ppath, pnames)
2019-06-16 21:33:25 +00:00
} else {
r.insert(method, path[:i], nil, pkind, "", nil)
2017-02-18 22:00:46 +00:00
}
} else if path[i] == '*' {
r.insert(method, path[:i], nil, skind, "", nil)
pnames = append(pnames, "*")
r.insert(method, path[:i+1], h, akind, ppath, pnames)
}
}
r.insert(method, path, h, skind, ppath, pnames)
}
func (r *Router) insert(method, path string, h HandlerFunc, t kind, ppath string, pnames []string) {
// Adjust max param
l := len(pnames)
if *r.echo.maxParam < l {
*r.echo.maxParam = l
}
cn := r.tree // Current node as root
if cn == nil {
2017-06-05 22:01:05 +00:00
panic("echo: invalid method")
2017-02-18 22:00:46 +00:00
}
search := path
for {
sl := len(search)
pl := len(cn.prefix)
l := 0
// LCP
max := pl
if sl < max {
max = sl
}
for ; l < max && search[l] == cn.prefix[l]; l++ {
}
if l == 0 {
// At root node
cn.label = search[0]
cn.prefix = search
if h != nil {
cn.kind = t
cn.addHandler(method, h)
cn.ppath = ppath
cn.pnames = pnames
}
} else if l < pl {
// Split node
n := newNode(cn.kind, cn.prefix[l:], cn, cn.children, cn.methodHandler, cn.ppath, cn.pnames)
2020-01-09 20:02:56 +00:00
// Update parent path for all children to new node
for _, child := range cn.children {
child.parent = n
}
2017-02-18 22:00:46 +00:00
// Reset parent node
cn.kind = skind
cn.label = cn.prefix[0]
cn.prefix = cn.prefix[:l]
cn.children = nil
cn.methodHandler = new(methodHandler)
cn.ppath = ""
cn.pnames = nil
cn.addChild(n)
if l == sl {
// At parent node
cn.kind = t
cn.addHandler(method, h)
cn.ppath = ppath
cn.pnames = pnames
} else {
// Create child node
n = newNode(t, search[l:], cn, nil, new(methodHandler), ppath, pnames)
n.addHandler(method, h)
cn.addChild(n)
}
} else if l < sl {
search = search[l:]
c := cn.findChildWithLabel(search[0])
if c != nil {
// Go deeper
cn = c
continue
}
// Create child node
n := newNode(t, search, cn, nil, new(methodHandler), ppath, pnames)
n.addHandler(method, h)
cn.addChild(n)
} else {
// Node already exists
if h != nil {
cn.addHandler(method, h)
cn.ppath = ppath
if len(cn.pnames) == 0 { // Issue #729
cn.pnames = pnames
}
}
}
return
}
}
func newNode(t kind, pre string, p *node, c children, mh *methodHandler, ppath string, pnames []string) *node {
return &node{
kind: t,
label: pre[0],
prefix: pre,
parent: p,
children: c,
ppath: ppath,
pnames: pnames,
methodHandler: mh,
}
}
func (n *node) addChild(c *node) {
n.children = append(n.children, c)
}
func (n *node) findChild(l byte, t kind) *node {
for _, c := range n.children {
if c.label == l && c.kind == t {
return c
}
}
return nil
}
func (n *node) findChildWithLabel(l byte) *node {
for _, c := range n.children {
if c.label == l {
return c
}
}
return nil
}
func (n *node) findChildByKind(t kind) *node {
for _, c := range n.children {
if c.kind == t {
return c
}
}
return nil
}
func (n *node) addHandler(method string, h HandlerFunc) {
switch method {
case http.MethodConnect:
n.methodHandler.connect = h
case http.MethodDelete:
n.methodHandler.delete = h
case http.MethodGet:
2017-02-18 22:00:46 +00:00
n.methodHandler.get = h
case http.MethodHead:
n.methodHandler.head = h
case http.MethodOptions:
n.methodHandler.options = h
case http.MethodPatch:
n.methodHandler.patch = h
case http.MethodPost:
2017-02-18 22:00:46 +00:00
n.methodHandler.post = h
case PROPFIND:
n.methodHandler.propfind = h
case http.MethodPut:
2017-02-18 22:00:46 +00:00
n.methodHandler.put = h
case http.MethodTrace:
2017-02-18 22:00:46 +00:00
n.methodHandler.trace = h
2019-06-16 21:33:25 +00:00
case REPORT:
n.methodHandler.report = h
2017-02-18 22:00:46 +00:00
}
}
func (n *node) findHandler(method string) HandlerFunc {
switch method {
case http.MethodConnect:
return n.methodHandler.connect
case http.MethodDelete:
return n.methodHandler.delete
case http.MethodGet:
2017-02-18 22:00:46 +00:00
return n.methodHandler.get
case http.MethodHead:
return n.methodHandler.head
case http.MethodOptions:
return n.methodHandler.options
case http.MethodPatch:
return n.methodHandler.patch
case http.MethodPost:
2017-02-18 22:00:46 +00:00
return n.methodHandler.post
case PROPFIND:
return n.methodHandler.propfind
case http.MethodPut:
2017-02-18 22:00:46 +00:00
return n.methodHandler.put
case http.MethodTrace:
2017-02-18 22:00:46 +00:00
return n.methodHandler.trace
2019-06-16 21:33:25 +00:00
case REPORT:
return n.methodHandler.report
2017-02-18 22:00:46 +00:00
default:
return nil
}
}
func (n *node) checkMethodNotAllowed() HandlerFunc {
for _, m := range methods {
if h := n.findHandler(m); h != nil {
return MethodNotAllowedHandler
}
}
return NotFoundHandler
}
// Find lookup a handler registered for method and path. It also parses URL for path
// parameters and load them into context.
//
// For performance:
//
// - Get context from `Echo#AcquireContext()`
// - Reset it `Context#Reset()`
// - Return it `Echo#ReleaseContext()`.
2017-06-05 22:01:05 +00:00
func (r *Router) Find(method, path string, c Context) {
ctx := c.(*context)
ctx.path = path
2017-02-18 22:00:46 +00:00
cn := r.tree // Current node as root
var (
search = path
2017-06-05 22:01:05 +00:00
child *node // Child node
n int // Param counter
nk kind // Next kind
nn *node // Next node
ns string // Next search
pvalues = ctx.pvalues // Use the internal slice so the interface can keep the illusion of a dynamic slice
2017-02-18 22:00:46 +00:00
)
// Search order static > param > any
for {
if search == "" {
break
2017-02-18 22:00:46 +00:00
}
pl := 0 // Prefix length
l := 0 // LCP length
if cn.label != ':' {
sl := len(search)
pl = len(cn.prefix)
// LCP
max := pl
if sl < max {
max = sl
}
for ; l < max && search[l] == cn.prefix[l]; l++ {
}
}
if l == pl {
// Continue search
search = search[l:]
} else {
2019-09-07 20:46:58 +00:00
if nn == nil { // Issue #1348
return // Not found
}
2017-02-18 22:00:46 +00:00
cn = nn
search = ns
if nk == pkind {
goto Param
} else if nk == akind {
goto Any
}
}
if search == "" {
break
2017-02-18 22:00:46 +00:00
}
// Static node
2017-06-05 22:01:05 +00:00
if child = cn.findChild(search[0], skind); child != nil {
2017-02-18 22:00:46 +00:00
// Save next
if cn.prefix[len(cn.prefix)-1] == '/' { // Issue #623
nk = pkind
nn = cn
ns = search
}
2017-06-05 22:01:05 +00:00
cn = child
2017-02-18 22:00:46 +00:00
continue
}
// Param node
Param:
2017-06-05 22:01:05 +00:00
if child = cn.findChildByKind(pkind); child != nil {
2017-02-18 22:00:46 +00:00
// Issue #378
if len(pvalues) == n {
continue
}
// Save next
if cn.prefix[len(cn.prefix)-1] == '/' { // Issue #623
nk = akind
nn = cn
ns = search
}
2017-06-05 22:01:05 +00:00
cn = child
2017-02-18 22:00:46 +00:00
i, l := 0, len(search)
for ; i < l && search[i] != '/'; i++ {
}
pvalues[n] = search[:i]
n++
search = search[i:]
continue
}
// Any node
Any:
if cn = cn.findChildByKind(akind); cn == nil {
if nn != nil {
2020-01-09 20:02:56 +00:00
// No next node to go down in routing (issue #954)
// Find nearest "any" route going up the routing tree
2017-02-18 22:00:46 +00:00
search = ns
2020-01-09 20:02:56 +00:00
np := nn.parent
// Consider param route one level up only
// if no slash is remaining in search string
if cn = nn.findChildByKind(pkind); cn != nil && strings.IndexByte(ns, '/') == -1 {
pvalues[len(cn.pnames)-1] = search
break
}
for {
np = nn.parent
if cn = nn.findChildByKind(akind); cn != nil {
break
}
if np == nil {
break // no further parent nodes in tree, abort
}
var str strings.Builder
str.WriteString(nn.prefix)
str.WriteString(search)
search = str.String()
nn = np
}
if cn != nil { // use the found "any" route and update path
pvalues[len(cn.pnames)-1] = search
break
2017-02-18 22:00:46 +00:00
}
}
2019-09-07 20:46:58 +00:00
return // Not found
2017-02-18 22:00:46 +00:00
}
pvalues[len(cn.pnames)-1] = search
break
2017-02-18 22:00:46 +00:00
}
2017-06-05 22:01:05 +00:00
ctx.handler = cn.findHandler(method)
ctx.path = cn.ppath
ctx.pnames = cn.pnames
2017-02-18 22:00:46 +00:00
// NOTE: Slow zone...
2017-06-05 22:01:05 +00:00
if ctx.handler == nil {
ctx.handler = cn.checkMethodNotAllowed()
2017-02-18 22:00:46 +00:00
// Dig further for any, might have an empty value for *, e.g.
// serving a directory. Issue #207.
if cn = cn.findChildByKind(akind); cn == nil {
return
}
if h := cn.findHandler(method); h != nil {
2017-06-05 22:01:05 +00:00
ctx.handler = h
2017-02-18 22:00:46 +00:00
} else {
2017-06-05 22:01:05 +00:00
ctx.handler = cn.checkMethodNotAllowed()
2017-02-18 22:00:46 +00:00
}
2017-06-05 22:01:05 +00:00
ctx.path = cn.ppath
ctx.pnames = cn.pnames
2017-02-18 22:00:46 +00:00
pvalues[len(cn.pnames)-1] = ""
}
return
}