2015-03-27 23:21:30 +02:00
|
|
|
package echo
|
2015-03-01 19:45:13 +02:00
|
|
|
|
2018-10-14 17:16:58 +02:00
|
|
|
import "net/http"
|
|
|
|
|
2015-03-01 19:45:13 +02:00
|
|
|
type (
|
2016-03-20 00:47:20 +02:00
|
|
|
// Router is the registry of all registered routes for an `Echo` instance for
|
|
|
|
// request matching and URL path parameter parsing.
|
2015-05-23 05:26:52 +02:00
|
|
|
Router struct {
|
2015-10-06 15:48:33 +02:00
|
|
|
tree *node
|
2017-05-17 01:29:33 +02:00
|
|
|
routes map[string]*Route
|
2015-10-06 15:48:33 +02:00
|
|
|
echo *Echo
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
node struct {
|
2015-10-08 00:10:40 +02:00
|
|
|
kind kind
|
2015-10-06 15:48:33 +02:00
|
|
|
label byte
|
|
|
|
prefix string
|
|
|
|
parent *node
|
|
|
|
children children
|
2015-11-13 06:23:14 +02:00
|
|
|
ppath string
|
2015-10-06 15:48:33 +02:00
|
|
|
pnames []string
|
2015-11-13 06:23:14 +02:00
|
|
|
methodHandler *methodHandler
|
2015-10-06 15:48:33 +02:00
|
|
|
}
|
2019-07-18 06:18:56 +02:00
|
|
|
kind uint8
|
|
|
|
children []*node
|
2015-10-06 15:48:33 +02:00
|
|
|
methodHandler struct {
|
2018-03-13 21:38:42 +02:00
|
|
|
connect HandlerFunc
|
|
|
|
delete HandlerFunc
|
|
|
|
get HandlerFunc
|
|
|
|
head HandlerFunc
|
|
|
|
options HandlerFunc
|
|
|
|
patch HandlerFunc
|
|
|
|
post HandlerFunc
|
|
|
|
propfind HandlerFunc
|
|
|
|
put HandlerFunc
|
|
|
|
trace HandlerFunc
|
2019-06-09 18:11:18 +02:00
|
|
|
report HandlerFunc
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2015-04-24 16:44:30 +02:00
|
|
|
const (
|
2015-10-08 00:10:40 +02:00
|
|
|
skind kind = iota
|
|
|
|
pkind
|
2016-03-04 08:06:47 +02:00
|
|
|
akind
|
2015-04-24 16:44:30 +02:00
|
|
|
)
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// NewRouter returns a new Router instance.
|
2015-07-27 17:43:11 +02:00
|
|
|
func NewRouter(e *Echo) *Router {
|
|
|
|
return &Router{
|
2015-10-06 15:48:33 +02:00
|
|
|
tree: &node{
|
|
|
|
methodHandler: new(methodHandler),
|
|
|
|
},
|
2017-05-17 01:29:33 +02:00
|
|
|
routes: map[string]*Route{},
|
2015-10-06 15:48:33 +02:00
|
|
|
echo: e,
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Add registers a new route for method and path with matching handler.
|
2016-10-22 05:36:49 +02:00
|
|
|
func (r *Router) Add(method, path string, h HandlerFunc) {
|
2016-04-12 07:53:31 +02:00
|
|
|
// Validate path
|
|
|
|
if path == "" {
|
2019-04-30 07:54:49 +02:00
|
|
|
path = "/"
|
2016-04-12 07:53:31 +02:00
|
|
|
}
|
|
|
|
if path[0] != '/' {
|
|
|
|
path = "/" + path
|
|
|
|
}
|
2018-02-21 21:38:22 +02:00
|
|
|
pnames := []string{} // Param names
|
|
|
|
ppath := path // Pristine path
|
2015-04-24 16:44:30 +02:00
|
|
|
|
2015-04-05 23:21:03 +02:00
|
|
|
for i, l := 0, len(path); i < l; i++ {
|
2015-03-01 19:45:13 +02:00
|
|
|
if path[i] == ':' {
|
2015-04-24 16:44:30 +02:00
|
|
|
j := i + 1
|
|
|
|
|
2016-10-22 05:36:49 +02:00
|
|
|
r.insert(method, path[:i], nil, skind, "", nil)
|
2018-04-12 07:34:01 +02:00
|
|
|
for ; i < l && path[i] != '/'; i++ {
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
2015-04-24 16:44:30 +02:00
|
|
|
|
|
|
|
pnames = append(pnames, path[j:i])
|
|
|
|
path = path[:j] + path[i:]
|
|
|
|
i, l = j, len(path)
|
|
|
|
|
2015-03-01 19:45:13 +02:00
|
|
|
if i == l {
|
2016-10-22 05:36:49 +02:00
|
|
|
r.insert(method, path[:i], h, pkind, ppath, pnames)
|
2019-04-29 07:22:35 +02:00
|
|
|
} else {
|
|
|
|
r.insert(method, path[:i], nil, pkind, "", nil)
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
2015-03-07 07:55:51 +02:00
|
|
|
} else if path[i] == '*' {
|
2016-10-22 05:36:49 +02:00
|
|
|
r.insert(method, path[:i], nil, skind, "", nil)
|
2016-10-11 03:36:48 +02:00
|
|
|
pnames = append(pnames, "*")
|
2016-10-22 05:36:49 +02:00
|
|
|
r.insert(method, path[:i+1], h, akind, ppath, pnames)
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
}
|
2015-06-04 00:18:27 +02:00
|
|
|
|
2016-10-22 05:36:49 +02:00
|
|
|
r.insert(method, path, h, skind, ppath, pnames)
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
|
2016-10-22 05:36:49 +02:00
|
|
|
func (r *Router) insert(method, path string, h HandlerFunc, t kind, ppath string, pnames []string) {
|
2015-06-04 00:18:27 +02:00
|
|
|
// Adjust max param
|
|
|
|
l := len(pnames)
|
2016-10-22 05:36:49 +02:00
|
|
|
if *r.echo.maxParam < l {
|
|
|
|
*r.echo.maxParam = l
|
2015-06-04 00:18:27 +02:00
|
|
|
}
|
|
|
|
|
2015-10-06 15:48:33 +02:00
|
|
|
cn := r.tree // Current node as root
|
2015-07-24 21:03:36 +02:00
|
|
|
if cn == nil {
|
2017-06-03 02:55:59 +02:00
|
|
|
panic("echo: invalid method")
|
2015-07-24 21:03:36 +02:00
|
|
|
}
|
2015-03-01 19:45:13 +02:00
|
|
|
search := path
|
|
|
|
|
|
|
|
for {
|
|
|
|
sl := len(search)
|
|
|
|
pl := len(cn.prefix)
|
2015-06-06 00:08:32 +02:00
|
|
|
l := 0
|
|
|
|
|
|
|
|
// LCP
|
|
|
|
max := pl
|
|
|
|
if sl < max {
|
|
|
|
max = sl
|
|
|
|
}
|
|
|
|
for ; l < max && search[l] == cn.prefix[l]; l++ {
|
|
|
|
}
|
2015-03-01 19:45:13 +02:00
|
|
|
|
|
|
|
if l == 0 {
|
|
|
|
// At root node
|
|
|
|
cn.label = search[0]
|
|
|
|
cn.prefix = search
|
|
|
|
if h != nil {
|
2015-10-08 00:10:40 +02:00
|
|
|
cn.kind = t
|
2015-10-06 15:48:33 +02:00
|
|
|
cn.addHandler(method, h)
|
2015-11-13 06:23:14 +02:00
|
|
|
cn.ppath = ppath
|
2015-04-24 16:44:30 +02:00
|
|
|
cn.pnames = pnames
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
} else if l < pl {
|
2015-04-08 23:40:49 +02:00
|
|
|
// Split node
|
2016-02-15 18:11:29 +02:00
|
|
|
n := newNode(cn.kind, cn.prefix[l:], cn, cn.children, cn.methodHandler, cn.ppath, cn.pnames)
|
2015-03-01 19:45:13 +02:00
|
|
|
|
|
|
|
// Reset parent node
|
2015-10-08 00:10:40 +02:00
|
|
|
cn.kind = skind
|
2015-03-01 19:45:13 +02:00
|
|
|
cn.label = cn.prefix[0]
|
|
|
|
cn.prefix = cn.prefix[:l]
|
2015-06-06 00:08:32 +02:00
|
|
|
cn.children = nil
|
2015-10-06 15:48:33 +02:00
|
|
|
cn.methodHandler = new(methodHandler)
|
2015-11-13 06:23:14 +02:00
|
|
|
cn.ppath = ""
|
2015-04-24 16:44:30 +02:00
|
|
|
cn.pnames = nil
|
2015-03-01 19:45:13 +02:00
|
|
|
|
2015-06-06 00:08:32 +02:00
|
|
|
cn.addChild(n)
|
|
|
|
|
2015-03-01 19:45:13 +02:00
|
|
|
if l == sl {
|
|
|
|
// At parent node
|
2015-10-08 00:10:40 +02:00
|
|
|
cn.kind = t
|
2015-10-06 15:48:33 +02:00
|
|
|
cn.addHandler(method, h)
|
2015-11-13 06:23:14 +02:00
|
|
|
cn.ppath = ppath
|
2015-04-24 16:44:30 +02:00
|
|
|
cn.pnames = pnames
|
2015-03-01 19:45:13 +02:00
|
|
|
} else {
|
2015-04-08 23:40:49 +02:00
|
|
|
// Create child node
|
2016-02-15 18:11:29 +02:00
|
|
|
n = newNode(t, search[l:], cn, nil, new(methodHandler), ppath, pnames)
|
2015-10-06 15:48:33 +02:00
|
|
|
n.addHandler(method, h)
|
2015-06-06 00:08:32 +02:00
|
|
|
cn.addChild(n)
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
} else if l < sl {
|
|
|
|
search = search[l:]
|
2015-06-06 00:08:32 +02:00
|
|
|
c := cn.findChildWithLabel(search[0])
|
2015-04-14 06:57:36 +02:00
|
|
|
if c != nil {
|
2015-04-08 23:40:49 +02:00
|
|
|
// Go deeper
|
2015-04-14 06:57:36 +02:00
|
|
|
cn = c
|
2015-04-08 23:40:49 +02:00
|
|
|
continue
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
2015-04-08 23:40:49 +02:00
|
|
|
// Create child node
|
2016-02-15 18:11:29 +02:00
|
|
|
n := newNode(t, search, cn, nil, new(methodHandler), ppath, pnames)
|
2015-10-06 15:48:33 +02:00
|
|
|
n.addHandler(method, h)
|
2015-06-06 00:08:32 +02:00
|
|
|
cn.addChild(n)
|
2015-03-01 19:45:13 +02:00
|
|
|
} else {
|
|
|
|
// Node already exists
|
|
|
|
if h != nil {
|
2015-10-06 15:48:33 +02:00
|
|
|
cn.addHandler(method, h)
|
2016-03-09 05:40:25 +02:00
|
|
|
cn.ppath = ppath
|
2016-11-16 06:09:52 +02:00
|
|
|
if len(cn.pnames) == 0 { // Issue #729
|
|
|
|
cn.pnames = pnames
|
|
|
|
}
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-08 23:40:49 +02:00
|
|
|
return
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-15 18:11:29 +02:00
|
|
|
func newNode(t kind, pre string, p *node, c children, mh *methodHandler, ppath string, pnames []string) *node {
|
2015-04-26 21:44:38 +02:00
|
|
|
return &node{
|
2015-10-08 00:10:40 +02:00
|
|
|
kind: t,
|
2015-10-06 15:48:33 +02:00
|
|
|
label: pre[0],
|
|
|
|
prefix: pre,
|
|
|
|
parent: p,
|
|
|
|
children: c,
|
2015-11-13 06:23:14 +02:00
|
|
|
ppath: ppath,
|
2015-10-06 15:48:33 +02:00
|
|
|
pnames: pnames,
|
2015-11-13 06:23:14 +02:00
|
|
|
methodHandler: mh,
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
2015-04-26 21:44:38 +02:00
|
|
|
}
|
|
|
|
|
2015-06-06 00:08:32 +02:00
|
|
|
func (n *node) addChild(c *node) {
|
|
|
|
n.children = append(n.children, c)
|
2015-04-01 17:05:54 +02:00
|
|
|
}
|
|
|
|
|
2015-10-08 00:10:40 +02:00
|
|
|
func (n *node) findChild(l byte, t kind) *node {
|
2015-04-24 16:44:30 +02:00
|
|
|
for _, c := range n.children {
|
2015-10-08 00:10:40 +02:00
|
|
|
if c.label == l && c.kind == t {
|
2015-04-24 16:44:30 +02:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-06 00:08:32 +02:00
|
|
|
func (n *node) findChildWithLabel(l byte) *node {
|
2015-04-24 16:44:30 +02:00
|
|
|
for _, c := range n.children {
|
2015-06-06 00:08:32 +02:00
|
|
|
if c.label == l {
|
2015-04-24 16:44:30 +02:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-08 00:10:40 +02:00
|
|
|
func (n *node) findChildByKind(t kind) *node {
|
2015-04-24 16:44:30 +02:00
|
|
|
for _, c := range n.children {
|
2015-10-08 00:10:40 +02:00
|
|
|
if c.kind == t {
|
2015-04-24 16:44:30 +02:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-02 23:19:39 +02:00
|
|
|
func (n *node) addHandler(method string, h HandlerFunc) {
|
2015-10-06 15:48:33 +02:00
|
|
|
switch method {
|
2018-10-14 17:16:58 +02:00
|
|
|
case http.MethodConnect:
|
2018-03-13 21:38:42 +02:00
|
|
|
n.methodHandler.connect = h
|
2018-10-14 17:16:58 +02:00
|
|
|
case http.MethodDelete:
|
2018-03-13 21:38:42 +02:00
|
|
|
n.methodHandler.delete = h
|
2018-10-14 17:16:58 +02:00
|
|
|
case http.MethodGet:
|
2015-10-06 15:48:33 +02:00
|
|
|
n.methodHandler.get = h
|
2018-10-14 17:16:58 +02:00
|
|
|
case http.MethodHead:
|
2018-03-13 21:38:42 +02:00
|
|
|
n.methodHandler.head = h
|
2018-10-14 17:16:58 +02:00
|
|
|
case http.MethodOptions:
|
2018-03-13 21:38:42 +02:00
|
|
|
n.methodHandler.options = h
|
2018-10-14 17:16:58 +02:00
|
|
|
case http.MethodPatch:
|
2018-03-13 21:38:42 +02:00
|
|
|
n.methodHandler.patch = h
|
2018-10-14 17:16:58 +02:00
|
|
|
case http.MethodPost:
|
2015-10-06 15:48:33 +02:00
|
|
|
n.methodHandler.post = h
|
2018-03-13 21:38:42 +02:00
|
|
|
case PROPFIND:
|
|
|
|
n.methodHandler.propfind = h
|
2018-10-14 17:16:58 +02:00
|
|
|
case http.MethodPut:
|
2015-10-06 15:48:33 +02:00
|
|
|
n.methodHandler.put = h
|
2018-10-14 17:16:58 +02:00
|
|
|
case http.MethodTrace:
|
2015-10-06 15:48:33 +02:00
|
|
|
n.methodHandler.trace = h
|
2019-06-09 18:11:18 +02:00
|
|
|
case REPORT:
|
|
|
|
n.methodHandler.report = h
|
2015-10-06 15:48:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-02 23:19:39 +02:00
|
|
|
func (n *node) findHandler(method string) HandlerFunc {
|
2015-10-06 15:48:33 +02:00
|
|
|
switch method {
|
2018-10-14 17:16:58 +02:00
|
|
|
case http.MethodConnect:
|
2018-03-13 21:38:42 +02:00
|
|
|
return n.methodHandler.connect
|
2018-10-14 17:16:58 +02:00
|
|
|
case http.MethodDelete:
|
2018-03-13 21:38:42 +02:00
|
|
|
return n.methodHandler.delete
|
2018-10-14 17:16:58 +02:00
|
|
|
case http.MethodGet:
|
2015-10-06 15:48:33 +02:00
|
|
|
return n.methodHandler.get
|
2018-10-14 17:16:58 +02:00
|
|
|
case http.MethodHead:
|
2018-03-13 21:38:42 +02:00
|
|
|
return n.methodHandler.head
|
2018-10-14 17:16:58 +02:00
|
|
|
case http.MethodOptions:
|
2018-03-13 21:38:42 +02:00
|
|
|
return n.methodHandler.options
|
2018-10-14 17:16:58 +02:00
|
|
|
case http.MethodPatch:
|
2018-03-13 21:38:42 +02:00
|
|
|
return n.methodHandler.patch
|
2018-10-14 17:16:58 +02:00
|
|
|
case http.MethodPost:
|
2015-10-06 15:48:33 +02:00
|
|
|
return n.methodHandler.post
|
2018-03-13 21:38:42 +02:00
|
|
|
case PROPFIND:
|
|
|
|
return n.methodHandler.propfind
|
2018-10-14 17:16:58 +02:00
|
|
|
case http.MethodPut:
|
2015-10-06 15:48:33 +02:00
|
|
|
return n.methodHandler.put
|
2018-10-14 17:16:58 +02:00
|
|
|
case http.MethodTrace:
|
2015-10-06 15:48:33 +02:00
|
|
|
return n.methodHandler.trace
|
2019-06-09 18:11:18 +02:00
|
|
|
case REPORT:
|
|
|
|
return n.methodHandler.report
|
2015-10-06 15:48:33 +02:00
|
|
|
default:
|
|
|
|
return nil
|
2015-07-24 21:03:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-30 23:47:04 +02:00
|
|
|
func (n *node) checkMethodNotAllowed() HandlerFunc {
|
2015-11-22 19:26:11 +02:00
|
|
|
for _, m := range methods {
|
|
|
|
if h := n.findHandler(m); h != nil {
|
2016-07-05 17:43:46 +02:00
|
|
|
return MethodNotAllowedHandler
|
2015-11-22 19:26:11 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-05 17:43:46 +02:00
|
|
|
return NotFoundHandler
|
2015-11-22 19:26:11 +02:00
|
|
|
}
|
|
|
|
|
2017-02-02 21:50:17 +02:00
|
|
|
// Find lookup a handler registered for method and path. It also parses URL for path
|
2016-03-20 00:47:20 +02:00
|
|
|
// parameters and load them into context.
|
|
|
|
//
|
|
|
|
// For performance:
|
|
|
|
//
|
2016-05-04 02:23:31 +02:00
|
|
|
// - Get context from `Echo#AcquireContext()`
|
2016-03-20 00:47:20 +02:00
|
|
|
// - Reset it `Context#Reset()`
|
2016-05-04 02:23:31 +02:00
|
|
|
// - Return it `Echo#ReleaseContext()`.
|
2017-04-10 22:10:31 +02:00
|
|
|
func (r *Router) Find(method, path string, c Context) {
|
|
|
|
ctx := c.(*context)
|
|
|
|
ctx.path = path
|
2015-10-06 15:48:33 +02:00
|
|
|
cn := r.tree // Current node as root
|
2015-09-01 17:03:01 +02:00
|
|
|
|
2015-05-10 07:06:13 +02:00
|
|
|
var (
|
2016-04-17 00:53:27 +02:00
|
|
|
search = path
|
2017-04-10 22:10:31 +02: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
|
2015-05-10 07:06:13 +02:00
|
|
|
)
|
2015-03-01 19:45:13 +02:00
|
|
|
|
2016-03-04 08:06:47 +02:00
|
|
|
// Search order static > param > any
|
2015-03-01 19:45:13 +02:00
|
|
|
for {
|
2015-05-04 22:01:02 +02:00
|
|
|
if search == "" {
|
2018-10-15 05:42:43 +02:00
|
|
|
break
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
|
2015-05-10 07:06:13 +02:00
|
|
|
pl := 0 // Prefix length
|
|
|
|
l := 0 // LCP length
|
|
|
|
|
|
|
|
if cn.label != ':' {
|
2015-06-06 00:08:32 +02:00
|
|
|
sl := len(search)
|
2015-05-10 07:06:13 +02:00
|
|
|
pl = len(cn.prefix)
|
2015-06-06 00:08:32 +02:00
|
|
|
|
|
|
|
// LCP
|
|
|
|
max := pl
|
2015-06-10 05:06:51 +02:00
|
|
|
if sl < max {
|
2015-06-06 00:08:32 +02:00
|
|
|
max = sl
|
|
|
|
}
|
|
|
|
for ; l < max && search[l] == cn.prefix[l]; l++ {
|
|
|
|
}
|
2015-05-10 07:06:13 +02:00
|
|
|
}
|
2015-04-13 22:12:30 +02:00
|
|
|
|
2019-07-18 06:18:56 +02:00
|
|
|
|
2015-04-11 22:49:45 +02:00
|
|
|
if l == pl {
|
2015-05-04 22:01:02 +02:00
|
|
|
// Continue search
|
2015-04-11 22:49:45 +02:00
|
|
|
search = search[l:]
|
2015-05-10 07:06:13 +02:00
|
|
|
} else {
|
2019-07-18 06:18:56 +02:00
|
|
|
if nn == nil { // Issue #1348
|
|
|
|
return // Not found
|
|
|
|
}
|
2015-05-10 07:06:13 +02:00
|
|
|
cn = nn
|
|
|
|
search = ns
|
2015-10-08 00:10:40 +02:00
|
|
|
if nk == pkind {
|
2015-05-10 07:06:13 +02:00
|
|
|
goto Param
|
2016-03-04 08:06:47 +02:00
|
|
|
} else if nk == akind {
|
|
|
|
goto Any
|
2015-05-10 07:06:13 +02:00
|
|
|
}
|
2015-04-11 20:10:19 +02:00
|
|
|
}
|
2015-03-01 19:45:13 +02:00
|
|
|
|
2015-05-04 22:01:02 +02:00
|
|
|
if search == "" {
|
2018-10-15 05:42:43 +02:00
|
|
|
break
|
2015-05-04 22:01:02 +02:00
|
|
|
}
|
|
|
|
|
2015-04-11 19:09:41 +02:00
|
|
|
// Static node
|
2017-04-10 22:10:31 +02:00
|
|
|
if child = cn.findChild(search[0], skind); child != nil {
|
2015-05-10 07:06:13 +02:00
|
|
|
// Save next
|
2016-10-14 23:52:40 +02:00
|
|
|
if cn.prefix[len(cn.prefix)-1] == '/' { // Issue #623
|
2015-10-08 00:10:40 +02:00
|
|
|
nk = pkind
|
2015-05-10 07:06:13 +02:00
|
|
|
nn = cn
|
|
|
|
ns = search
|
|
|
|
}
|
2017-04-10 22:10:31 +02:00
|
|
|
cn = child
|
2015-04-11 19:09:41 +02:00
|
|
|
continue
|
|
|
|
}
|
2015-04-04 19:44:48 +02:00
|
|
|
|
2015-04-11 19:09:41 +02:00
|
|
|
// Param node
|
2015-09-15 22:14:30 +02:00
|
|
|
Param:
|
2017-04-10 22:10:31 +02:00
|
|
|
if child = cn.findChildByKind(pkind); child != nil {
|
2016-03-07 09:24:51 +02:00
|
|
|
// Issue #378
|
2016-04-17 00:53:27 +02:00
|
|
|
if len(pvalues) == n {
|
2016-03-07 09:24:51 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-05-10 07:06:13 +02:00
|
|
|
// Save next
|
2016-10-14 23:52:40 +02:00
|
|
|
if cn.prefix[len(cn.prefix)-1] == '/' { // Issue #623
|
2016-03-04 08:06:47 +02:00
|
|
|
nk = akind
|
2015-05-10 07:06:13 +02:00
|
|
|
nn = cn
|
|
|
|
ns = search
|
|
|
|
}
|
2016-03-07 09:24:51 +02:00
|
|
|
|
2017-04-10 22:10:31 +02:00
|
|
|
cn = child
|
2015-04-11 19:09:41 +02:00
|
|
|
i, l := 0, len(search)
|
2018-04-12 07:34:01 +02:00
|
|
|
for ; i < l && search[i] != '/'; i++ {
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
2016-04-17 00:53:27 +02:00
|
|
|
pvalues[n] = search[:i]
|
2015-04-26 18:48:49 +02:00
|
|
|
n++
|
2015-04-11 19:09:41 +02:00
|
|
|
search = search[i:]
|
|
|
|
continue
|
|
|
|
}
|
2015-04-04 19:44:48 +02:00
|
|
|
|
2016-03-04 08:06:47 +02:00
|
|
|
// Any node
|
|
|
|
Any:
|
|
|
|
if cn = cn.findChildByKind(akind); cn == nil {
|
|
|
|
if nn != nil {
|
|
|
|
cn = nn
|
2017-06-15 01:30:53 +02:00
|
|
|
nn = cn.parent // Next (Issue #954)
|
2016-03-04 08:06:47 +02:00
|
|
|
search = ns
|
|
|
|
if nk == pkind {
|
|
|
|
goto Param
|
|
|
|
} else if nk == akind {
|
|
|
|
goto Any
|
|
|
|
}
|
|
|
|
}
|
2019-07-18 06:18:56 +02:00
|
|
|
return // Not found
|
2015-04-12 22:04:41 +02:00
|
|
|
}
|
2016-04-17 00:53:27 +02:00
|
|
|
pvalues[len(cn.pnames)-1] = search
|
2018-10-15 05:42:43 +02:00
|
|
|
break
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
2015-09-24 23:14:01 +02:00
|
|
|
|
2017-04-10 22:10:31 +02:00
|
|
|
ctx.handler = cn.findHandler(method)
|
|
|
|
ctx.path = cn.ppath
|
|
|
|
ctx.pnames = cn.pnames
|
2015-11-22 19:26:11 +02:00
|
|
|
|
|
|
|
// NOTE: Slow zone...
|
2017-04-10 22:10:31 +02:00
|
|
|
if ctx.handler == nil {
|
|
|
|
ctx.handler = cn.checkMethodNotAllowed()
|
2015-11-22 19:26:11 +02:00
|
|
|
|
2016-03-04 08:06:47 +02:00
|
|
|
// Dig further for any, might have an empty value for *, e.g.
|
2015-10-06 22:56:01 +02:00
|
|
|
// serving a directory. Issue #207.
|
2016-03-04 08:06:47 +02:00
|
|
|
if cn = cn.findChildByKind(akind); cn == nil {
|
2015-10-06 15:48:33 +02:00
|
|
|
return
|
|
|
|
}
|
2016-04-17 00:53:27 +02:00
|
|
|
if h := cn.findHandler(method); h != nil {
|
2017-04-10 22:10:31 +02:00
|
|
|
ctx.handler = h
|
2016-04-17 00:53:27 +02:00
|
|
|
} else {
|
2017-04-10 22:10:31 +02:00
|
|
|
ctx.handler = cn.checkMethodNotAllowed()
|
2015-11-04 02:26:20 +02:00
|
|
|
}
|
2017-04-10 22:10:31 +02:00
|
|
|
ctx.path = cn.ppath
|
|
|
|
ctx.pnames = cn.pnames
|
2016-04-17 00:53:27 +02:00
|
|
|
pvalues[len(cn.pnames)-1] = ""
|
2015-10-06 15:48:33 +02:00
|
|
|
}
|
2016-04-09 23:00:23 +02:00
|
|
|
|
2015-09-24 23:14:01 +02:00
|
|
|
return
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|