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 (
|
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
|
|
|
|
methodHandler *methodHandler
|
|
|
|
pnames []string
|
|
|
|
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
|
|
|
)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-04 00:18:27 +02:00
|
|
|
func (r *Router) Add(method, path string, h HandlerFunc, e *Echo) {
|
|
|
|
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-10-08 00:10:40 +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-10-08 00:10:40 +02:00
|
|
|
r.insert(method, path[:i], h, pkind, pnames, e)
|
2015-03-01 19:45:13 +02:00
|
|
|
return
|
|
|
|
}
|
2015-10-08 00:10:40 +02:00
|
|
|
r.insert(method, path[:i], nil, pkind, pnames, e)
|
2015-03-07 07:55:51 +02:00
|
|
|
} else if path[i] == '*' {
|
2015-10-08 00:10:40 +02:00
|
|
|
r.insert(method, path[:i], nil, skind, nil, e)
|
2015-09-17 17:11:35 +02:00
|
|
|
pnames = append(pnames, "_*")
|
2015-10-08 00:10:40 +02:00
|
|
|
r.insert(method, path[:i+1], h, mkind, 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-10-08 00:10:40 +02:00
|
|
|
r.insert(method, path, h, skind, pnames, e)
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
|
2015-10-08 00:10:40 +02:00
|
|
|
func (r *Router) insert(method, path string, h HandlerFunc, t kind, 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-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-10-08 00:10:40 +02:00
|
|
|
n := newNode(cn.kind, cn.prefix[l:], cn, cn.children, cn.methodHandler, 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-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-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-10-06 15:48:33 +02:00
|
|
|
n = newNode(t, search[l:], cn, nil, new(methodHandler), pnames, e)
|
|
|
|
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-10-06 15:48:33 +02:00
|
|
|
n := newNode(t, search, cn, nil, new(methodHandler), pnames, e)
|
|
|
|
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-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-10-08 00:10:40 +02:00
|
|
|
func newNode(t kind, pre string, p *node, c children, mh *methodHandler, 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,
|
|
|
|
methodHandler: mh,
|
|
|
|
pnames: pnames,
|
|
|
|
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:
|
|
|
|
n.methodHandler.delete = h
|
|
|
|
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:
|
|
|
|
return n.methodHandler.delete
|
|
|
|
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-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
|
|
|
|
|
|
|
// Strip trailing slash
|
|
|
|
if r.echo.stripTrailingSlash {
|
2015-09-25 00:15:52 +02:00
|
|
|
l := len(path) - 1
|
2015-09-24 23:14:01 +02:00
|
|
|
if path != "/" && path[l] == '/' { // Issue #218
|
|
|
|
path = path[:l]
|
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-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-10-06 15:48:33 +02:00
|
|
|
if h == nil {
|
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 22:56:01 +02:00
|
|
|
h = notFoundHandler
|
2015-10-06 15:48:33 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
h = cn.findHandler(method)
|
|
|
|
ctx.pvalues[len(cn.pnames)-1] = ""
|
|
|
|
}
|
2015-09-24 23:14:01 +02:00
|
|
|
return
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
|
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-04-26 07:32:20 +02:00
|
|
|
h, _ := r.Find(req.Method, req.URL.Path, c)
|
2015-05-30 02:20:13 +02:00
|
|
|
c.reset(req, w, r.echo)
|
2015-07-24 21:03:36 +02:00
|
|
|
if err := h(c); err != nil {
|
|
|
|
r.echo.httpErrorHandler(err, c)
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
2015-04-09 07:23:47 +02:00
|
|
|
r.echo.pool.Put(c)
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|