2015-03-27 23:21:30 +02:00
|
|
|
package echo
|
2015-03-01 19:45:13 +02:00
|
|
|
|
2015-07-30 23:43:22 +02:00
|
|
|
import "net/http"
|
2015-03-01 19:45:13 +02:00
|
|
|
|
|
|
|
type (
|
2016-01-29 14:06:20 +02:00
|
|
|
|
|
|
|
// Router is the registry of all registered routes for an Echo instance for
|
|
|
|
// request matching and handler dispatching.
|
|
|
|
//
|
|
|
|
// Router implements the http.Handler specification and can be registered
|
|
|
|
// to serve requests.
|
2015-05-23 05:26:52 +02:00
|
|
|
Router struct {
|
2015-10-06 15:48:33 +02:00
|
|
|
tree *node
|
|
|
|
routes []Route
|
|
|
|
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
|
|
|
echo *Echo
|
|
|
|
}
|
2015-10-08 00:10:40 +02:00
|
|
|
kind uint8
|
2015-10-06 15:48:33 +02:00
|
|
|
children []*node
|
|
|
|
methodHandler struct {
|
|
|
|
connect HandlerFunc
|
|
|
|
delete HandlerFunc
|
|
|
|
get HandlerFunc
|
|
|
|
head HandlerFunc
|
|
|
|
options HandlerFunc
|
|
|
|
patch HandlerFunc
|
|
|
|
post HandlerFunc
|
|
|
|
put HandlerFunc
|
|
|
|
trace 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
|
|
|
|
mkind
|
2015-04-24 16:44:30 +02:00
|
|
|
)
|
|
|
|
|
2016-01-29 14:06: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),
|
|
|
|
},
|
|
|
|
routes: []Route{},
|
|
|
|
echo: e,
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-29 14:06:20 +02:00
|
|
|
// Add registers a new route with a matcher for the URL path.
|
2015-06-04 00:18:27 +02:00
|
|
|
func (r *Router) Add(method, path string, h HandlerFunc, e *Echo) {
|
2015-11-13 06:23:14 +02:00
|
|
|
ppath := path // Pristine path
|
2015-06-04 00:18:27 +02:00
|
|
|
pnames := []string{} // Param names
|
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
|
|
|
|
|
2015-11-13 06:23:14 +02:00
|
|
|
r.insert(method, path[:i], nil, skind, "", nil, e)
|
2015-03-01 19:45:13 +02:00
|
|
|
for ; i < l && path[i] != '/'; i++ {
|
|
|
|
}
|
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 {
|
2015-11-13 06:23:14 +02:00
|
|
|
r.insert(method, path[:i], h, pkind, ppath, pnames, e)
|
2015-03-01 19:45:13 +02:00
|
|
|
return
|
|
|
|
}
|
2015-11-13 06:23:14 +02:00
|
|
|
r.insert(method, path[:i], nil, pkind, ppath, pnames, e)
|
2015-03-07 07:55:51 +02:00
|
|
|
} else if path[i] == '*' {
|
2015-11-13 06:23:14 +02:00
|
|
|
r.insert(method, path[:i], nil, skind, "", nil, e)
|
2015-09-17 17:11:35 +02:00
|
|
|
pnames = append(pnames, "_*")
|
2015-11-13 06:23:14 +02:00
|
|
|
r.insert(method, path[:i+1], h, mkind, ppath, pnames, e)
|
2015-04-26 18:48:49 +02:00
|
|
|
return
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
}
|
2015-06-04 00:18:27 +02:00
|
|
|
|
2015-11-13 06:23:14 +02:00
|
|
|
r.insert(method, path, h, skind, ppath, pnames, e)
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
|
2015-11-13 06:23:14 +02:00
|
|
|
func (r *Router) insert(method, path string, h HandlerFunc, t kind, ppath string, pnames []string, e *Echo) {
|
2015-06-04 00:18:27 +02:00
|
|
|
// Adjust max param
|
|
|
|
l := len(pnames)
|
|
|
|
if *e.maxParam < l {
|
|
|
|
*e.maxParam = l
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
panic("echo => invalid method")
|
|
|
|
}
|
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-06-04 00:18:27 +02:00
|
|
|
cn.echo = e
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
} else if l < pl {
|
2015-04-08 23:40:49 +02:00
|
|
|
// Split node
|
2015-11-13 06:23:14 +02:00
|
|
|
n := newNode(cn.kind, cn.prefix[l:], cn, cn.children, cn.methodHandler, cn.ppath, cn.pnames, cn.echo)
|
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-04-02 23:41:36 +02:00
|
|
|
cn.echo = 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-06-04 00:18:27 +02:00
|
|
|
cn.echo = e
|
2015-03-01 19:45:13 +02:00
|
|
|
} else {
|
2015-04-08 23:40:49 +02:00
|
|
|
// Create child node
|
2015-11-13 06:23:14 +02:00
|
|
|
n = newNode(t, search[l:], cn, nil, new(methodHandler), ppath, pnames, e)
|
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
|
2015-11-13 06:23:14 +02:00
|
|
|
n := newNode(t, search, cn, nil, new(methodHandler), ppath, pnames, e)
|
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)
|
2015-11-13 06:23:14 +02:00
|
|
|
cn.ppath = path
|
2015-04-24 16:44:30 +02:00
|
|
|
cn.pnames = pnames
|
2015-06-04 00:18:27 +02:00
|
|
|
cn.echo = e
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-13 06:23:14 +02:00
|
|
|
func newNode(t kind, pre string, p *node, c children, mh *methodHandler, ppath string, pnames []string, e *Echo) *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-10-06 15:48:33 +02:00
|
|
|
echo: e,
|
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
|
|
|
|
}
|
|
|
|
|
2015-10-06 15:48:33 +02:00
|
|
|
func (n *node) addHandler(method string, h HandlerFunc) {
|
|
|
|
switch method {
|
|
|
|
case GET:
|
|
|
|
n.methodHandler.get = h
|
|
|
|
case POST:
|
|
|
|
n.methodHandler.post = h
|
|
|
|
case PUT:
|
|
|
|
n.methodHandler.put = h
|
|
|
|
case DELETE:
|
|
|
|
n.methodHandler.delete = h
|
|
|
|
case PATCH:
|
|
|
|
n.methodHandler.patch = h
|
|
|
|
case OPTIONS:
|
2015-10-24 02:02:28 +02:00
|
|
|
n.methodHandler.options = h
|
2015-10-06 15:48:33 +02:00
|
|
|
case HEAD:
|
|
|
|
n.methodHandler.head = h
|
|
|
|
case CONNECT:
|
|
|
|
n.methodHandler.connect = h
|
|
|
|
case TRACE:
|
|
|
|
n.methodHandler.trace = h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *node) findHandler(method string) HandlerFunc {
|
|
|
|
switch method {
|
|
|
|
case GET:
|
|
|
|
return n.methodHandler.get
|
|
|
|
case POST:
|
|
|
|
return n.methodHandler.post
|
|
|
|
case PUT:
|
|
|
|
return n.methodHandler.put
|
|
|
|
case DELETE:
|
|
|
|
return n.methodHandler.delete
|
|
|
|
case PATCH:
|
|
|
|
return n.methodHandler.patch
|
|
|
|
case OPTIONS:
|
2015-10-24 02:02:28 +02:00
|
|
|
return n.methodHandler.options
|
2015-10-06 15:48:33 +02:00
|
|
|
case HEAD:
|
|
|
|
return n.methodHandler.head
|
|
|
|
case CONNECT:
|
|
|
|
return n.methodHandler.connect
|
|
|
|
case TRACE:
|
|
|
|
return n.methodHandler.trace
|
|
|
|
default:
|
|
|
|
return nil
|
2015-07-24 21:03:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-22 19:26:11 +02:00
|
|
|
func (n *node) check405() HandlerFunc {
|
|
|
|
for _, m := range methods {
|
|
|
|
if h := n.findHandler(m); h != nil {
|
|
|
|
return methodNotAllowedHandler
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return notFoundHandler
|
|
|
|
}
|
|
|
|
|
2016-01-29 14:06:20 +02:00
|
|
|
// Find dispatches the request to the handler whos route is matched with the
|
|
|
|
// specified request path.
|
2015-06-04 00:18:27 +02:00
|
|
|
func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo) {
|
2015-07-24 21:03:36 +02:00
|
|
|
h = notFoundHandler
|
2015-10-06 20:04:36 +02:00
|
|
|
e = r.echo
|
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 (
|
2015-09-01 17:03:01 +02:00
|
|
|
search = path
|
2015-09-15 22:14:30 +02:00
|
|
|
c *node // Child node
|
|
|
|
n int // Param counter
|
2015-10-08 00:10:40 +02:00
|
|
|
nk kind // Next kind
|
2015-09-15 22:14:30 +02:00
|
|
|
nn *node // Next node
|
|
|
|
ns string // Next search
|
2015-05-10 07:06:13 +02:00
|
|
|
)
|
2015-03-01 19:45:13 +02:00
|
|
|
|
2015-04-29 08:09:30 +02:00
|
|
|
// Search order static > param > match-any
|
2015-03-01 19:45:13 +02:00
|
|
|
for {
|
2015-05-04 22:01:02 +02:00
|
|
|
if search == "" {
|
2015-10-06 22:56:01 +02:00
|
|
|
goto End
|
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
|
|
|
|
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 {
|
|
|
|
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
|
2015-10-08 00:10:40 +02:00
|
|
|
} else if nk == mkind {
|
2015-05-10 07:06:13 +02:00
|
|
|
goto MatchAny
|
|
|
|
} else {
|
|
|
|
// Not found
|
|
|
|
return
|
|
|
|
}
|
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 == "" {
|
2015-10-06 22:56:01 +02:00
|
|
|
goto End
|
2015-05-04 22:01:02 +02:00
|
|
|
}
|
|
|
|
|
2015-04-11 19:09:41 +02:00
|
|
|
// Static node
|
2015-10-08 00:10:40 +02:00
|
|
|
c = cn.findChild(search[0], skind)
|
2015-04-26 21:44:38 +02:00
|
|
|
if c != nil {
|
2015-05-10 07:06:13 +02:00
|
|
|
// Save next
|
|
|
|
if cn.label == '/' {
|
2015-10-08 00:10:40 +02:00
|
|
|
nk = pkind
|
2015-05-10 07:06:13 +02:00
|
|
|
nn = cn
|
|
|
|
ns = search
|
|
|
|
}
|
2015-04-26 21:44:38 +02:00
|
|
|
cn = c
|
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:
|
2015-10-08 00:10:40 +02:00
|
|
|
c = cn.findChildByKind(pkind)
|
2015-04-26 21:44:38 +02:00
|
|
|
if c != nil {
|
2015-05-10 07:06:13 +02:00
|
|
|
// Save next
|
|
|
|
if cn.label == '/' {
|
2015-10-08 00:10:40 +02:00
|
|
|
nk = mkind
|
2015-05-10 07:06:13 +02:00
|
|
|
nn = cn
|
|
|
|
ns = search
|
|
|
|
}
|
2015-04-26 21:44:38 +02:00
|
|
|
cn = c
|
2015-04-11 19:09:41 +02:00
|
|
|
i, l := 0, len(search)
|
|
|
|
for ; i < l && search[i] != '/'; i++ {
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
2015-04-26 21:44:38 +02:00
|
|
|
ctx.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
|
|
|
|
2015-05-04 22:01:02 +02:00
|
|
|
// Match-any node
|
2015-09-15 22:14:30 +02:00
|
|
|
MatchAny:
|
2015-09-17 04:28:09 +02:00
|
|
|
// c = cn.getChild()
|
2015-10-08 00:10:40 +02:00
|
|
|
if cn = cn.findChildByKind(mkind); cn == nil {
|
2015-09-25 00:15:52 +02:00
|
|
|
// Not found
|
|
|
|
return
|
2015-04-12 22:04:41 +02:00
|
|
|
}
|
2015-09-25 00:15:52 +02:00
|
|
|
ctx.pvalues[len(cn.pnames)-1] = search
|
2015-10-06 22:56:01 +02:00
|
|
|
goto End
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
2015-09-24 23:14:01 +02:00
|
|
|
|
2015-10-06 22:56:01 +02:00
|
|
|
End:
|
2015-11-13 06:23:14 +02:00
|
|
|
ctx.path = cn.ppath
|
2015-09-24 23:14:01 +02:00
|
|
|
ctx.pnames = cn.pnames
|
2015-10-06 15:48:33 +02:00
|
|
|
h = cn.findHandler(method)
|
2015-10-06 20:04:36 +02:00
|
|
|
if cn.echo != nil {
|
|
|
|
e = cn.echo
|
|
|
|
}
|
2015-11-22 19:26:11 +02:00
|
|
|
|
|
|
|
// NOTE: Slow zone...
|
2015-10-06 15:48:33 +02:00
|
|
|
if h == nil {
|
2015-11-22 19:26:11 +02:00
|
|
|
h = cn.check405()
|
|
|
|
|
2015-10-06 20:04:36 +02:00
|
|
|
// Dig further for match-any, might have an empty value for *, e.g.
|
2015-10-06 22:56:01 +02:00
|
|
|
// serving a directory. Issue #207.
|
2015-10-08 00:10:40 +02:00
|
|
|
if cn = cn.findChildByKind(mkind); cn == nil {
|
2015-10-06 15:48:33 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.pvalues[len(cn.pnames)-1] = ""
|
2015-11-04 02:26:20 +02:00
|
|
|
if h = cn.findHandler(method); h == nil {
|
2015-11-22 19:26:11 +02:00
|
|
|
h = cn.check405()
|
2015-11-04 02:26:20 +02:00
|
|
|
}
|
2015-10-06 15:48:33 +02:00
|
|
|
}
|
2015-09-24 23:14:01 +02:00
|
|
|
return
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
|
2016-01-29 14:06:20 +02:00
|
|
|
// ServeHTTP implements the Handler interface and can be registered to serve a
|
|
|
|
// particular path or subtree in an HTTP server.
|
|
|
|
// See Router.Find()
|
2015-05-23 05:26:52 +02:00
|
|
|
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
2015-04-09 23:59:31 +02:00
|
|
|
c := r.echo.pool.Get().(*Context)
|
2015-11-16 17:57:40 +02:00
|
|
|
h, _ := r.Find(req.Method, req.URL.Path, c)
|
|
|
|
c.reset(req, w, r.echo)
|
2015-07-24 21:03:36 +02:00
|
|
|
if err := h(c); err != nil {
|
2015-11-16 17:57:40 +02:00
|
|
|
r.echo.httpErrorHandler(err, c)
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
2015-11-16 17:57:40 +02:00
|
|
|
r.echo.pool.Put(c)
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|