2015-03-27 23:21:30 +02:00
|
|
|
package echo
|
2015-03-01 19:45:13 +02:00
|
|
|
|
2015-04-01 17:05:54 +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-06-04 00:18:27 +02:00
|
|
|
trees map[string]*node
|
|
|
|
routes []Route
|
|
|
|
echo *Echo
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
node struct {
|
2015-04-24 16:44:30 +02:00
|
|
|
typ ntype
|
2015-04-14 06:57:36 +02:00
|
|
|
label byte
|
|
|
|
prefix string
|
|
|
|
parent *node
|
|
|
|
children children
|
2015-05-04 22:01:02 +02:00
|
|
|
handler HandlerFunc
|
|
|
|
pnames []string
|
|
|
|
echo *Echo
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
2015-04-24 16:44:30 +02:00
|
|
|
ntype uint8
|
2015-04-14 06:57:36 +02:00
|
|
|
children []*node
|
2015-03-01 19:45:13 +02:00
|
|
|
)
|
|
|
|
|
2015-04-24 16:44:30 +02:00
|
|
|
const (
|
|
|
|
stype ntype = iota
|
|
|
|
ptype
|
2015-04-29 08:09:30 +02:00
|
|
|
mtype
|
2015-04-24 16:44:30 +02:00
|
|
|
)
|
|
|
|
|
2015-05-23 05:26:52 +02:00
|
|
|
func NewRouter(e *Echo) (r *Router) {
|
|
|
|
r = &Router{
|
2015-06-01 09:07:53 +02:00
|
|
|
trees: make(map[string]*node),
|
|
|
|
routes: []Route{},
|
|
|
|
echo: e,
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
2015-04-01 17:05:54 +02:00
|
|
|
for _, m := range methods {
|
|
|
|
r.trees[m] = &node{
|
2015-04-14 06:57:36 +02:00
|
|
|
prefix: "",
|
|
|
|
children: children{},
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-01 17:05:54 +02:00
|
|
|
return
|
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-06-04 00:18:27 +02:00
|
|
|
r.insert(method, path[:i], nil, stype, 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-06-04 00:18:27 +02:00
|
|
|
r.insert(method, path[:i], h, ptype, pnames, e)
|
2015-03-01 19:45:13 +02:00
|
|
|
return
|
|
|
|
}
|
2015-06-04 00:18:27 +02:00
|
|
|
r.insert(method, path[:i], nil, ptype, pnames, e)
|
2015-03-07 07:55:51 +02:00
|
|
|
} else if path[i] == '*' {
|
2015-06-04 00:18:27 +02:00
|
|
|
r.insert(method, path[:i], nil, stype, nil, e)
|
2015-04-26 18:48:49 +02:00
|
|
|
pnames = append(pnames, "_name")
|
2015-06-04 00:18:27 +02:00
|
|
|
r.insert(method, path[:i+1], h, mtype, 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
|
|
|
|
|
|
|
r.insert(method, path, h, stype, pnames, e)
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
|
2015-06-04 00:18:27 +02:00
|
|
|
func (r *Router) insert(method, path string, h HandlerFunc, t ntype, pnames []string, e *Echo) {
|
|
|
|
// Adjust max param
|
|
|
|
l := len(pnames)
|
|
|
|
if *e.maxParam < l {
|
|
|
|
*e.maxParam = l
|
|
|
|
}
|
|
|
|
|
2015-04-01 17:05:54 +02:00
|
|
|
cn := r.trees[method] // Current node as root
|
2015-03-01 19:45:13 +02:00
|
|
|
search := path
|
|
|
|
|
|
|
|
for {
|
|
|
|
sl := len(search)
|
|
|
|
pl := len(cn.prefix)
|
|
|
|
l := lcp(search, cn.prefix)
|
|
|
|
|
|
|
|
if l == 0 {
|
|
|
|
// At root node
|
|
|
|
cn.label = search[0]
|
|
|
|
cn.prefix = search
|
|
|
|
if h != nil {
|
2015-04-24 16:44:30 +02:00
|
|
|
cn.typ = t
|
2015-04-01 17:05:54 +02:00
|
|
|
cn.handler = 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-05-01 03:47:13 +02:00
|
|
|
n := newNode(cn.typ, cn.prefix[l:], cn, cn.children, cn.handler, cn.pnames, cn.echo)
|
2015-04-14 06:57:36 +02:00
|
|
|
cn.children = children{n} // Add to parent
|
2015-03-01 19:45:13 +02:00
|
|
|
|
|
|
|
// Reset parent node
|
2015-04-24 16:44:30 +02:00
|
|
|
cn.typ = stype
|
2015-03-01 19:45:13 +02:00
|
|
|
cn.label = cn.prefix[0]
|
|
|
|
cn.prefix = cn.prefix[:l]
|
2015-04-01 17:05:54 +02:00
|
|
|
cn.handler = nil
|
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
|
|
|
|
|
|
|
if l == sl {
|
|
|
|
// At parent node
|
2015-04-24 16:44:30 +02:00
|
|
|
cn.typ = t
|
2015-04-01 17:05:54 +02:00
|
|
|
cn.handler = 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-06-04 00:18:27 +02:00
|
|
|
n = newNode(t, search[l:], cn, nil, h, pnames, e)
|
2015-04-14 06:57:36 +02:00
|
|
|
cn.children = append(cn.children, n)
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
} else if l < sl {
|
|
|
|
search = search[l:]
|
2015-04-14 06:57:36 +02:00
|
|
|
c := cn.findChild(search[0])
|
|
|
|
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-06-04 00:18:27 +02:00
|
|
|
n := newNode(t, search, cn, nil, h, pnames, e)
|
2015-04-14 06:57:36 +02:00
|
|
|
cn.children = append(cn.children, n)
|
2015-03-01 19:45:13 +02:00
|
|
|
} else {
|
|
|
|
// Node already exists
|
|
|
|
if h != nil {
|
2015-04-01 17:05:54 +02:00
|
|
|
cn.handler = 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-06-04 00:18:27 +02:00
|
|
|
func newNode(t ntype, pre string, p *node, c children, h HandlerFunc, pnames []string, e *Echo) *node {
|
2015-04-26 21:44:38 +02:00
|
|
|
return &node{
|
2015-04-24 16:44:30 +02:00
|
|
|
typ: t,
|
2015-05-14 00:20:09 +02:00
|
|
|
label: pre[0],
|
|
|
|
prefix: pre,
|
2015-04-14 06:57:36 +02:00
|
|
|
parent: p,
|
|
|
|
children: c,
|
|
|
|
handler: h,
|
2015-04-24 16:44:30 +02:00
|
|
|
pnames: pnames,
|
2015-06-04 00:18:27 +02:00
|
|
|
echo: e,
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
2015-04-26 21:44:38 +02:00
|
|
|
}
|
|
|
|
|
2015-04-14 06:57:36 +02:00
|
|
|
func (n *node) findChild(l byte) *node {
|
|
|
|
for _, c := range n.children {
|
|
|
|
if c.label == l {
|
|
|
|
return c
|
2015-04-01 17:05:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-24 16:44:30 +02:00
|
|
|
func (n *node) findSchild(l byte) *node {
|
|
|
|
for _, c := range n.children {
|
|
|
|
if c.label == l && c.typ == stype {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *node) findPchild() *node {
|
|
|
|
for _, c := range n.children {
|
|
|
|
if c.typ == ptype {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-01 03:47:13 +02:00
|
|
|
func (n *node) findMchild() *node {
|
2015-04-24 16:44:30 +02:00
|
|
|
for _, c := range n.children {
|
2015-04-29 08:09:30 +02:00
|
|
|
if c.typ == mtype {
|
2015-04-24 16:44:30 +02:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-01 17:05:54 +02:00
|
|
|
// Length of longest common prefix
|
|
|
|
func lcp(a, b string) (i int) {
|
|
|
|
max := len(a)
|
|
|
|
l := len(b)
|
|
|
|
if l < max {
|
|
|
|
max = l
|
|
|
|
}
|
|
|
|
for ; i < max && a[i] == b[i]; i++ {
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-04 00:18:27 +02:00
|
|
|
func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo) {
|
2015-04-01 17:05:54 +02:00
|
|
|
cn := r.trees[method] // Current node as root
|
2015-03-01 19:45:13 +02:00
|
|
|
search := path
|
2015-05-10 07:06:13 +02:00
|
|
|
|
|
|
|
var (
|
|
|
|
c *node // Child node
|
|
|
|
n int // Param counter
|
|
|
|
nt ntype // Next type
|
|
|
|
nn *node // Next node
|
|
|
|
ns string // Next search
|
|
|
|
)
|
2015-03-01 19:45:13 +02:00
|
|
|
|
2015-05-04 22:01:02 +02:00
|
|
|
// TODO: Check empty path???
|
|
|
|
|
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-04-29 08:09:30 +02:00
|
|
|
// Found
|
2015-05-04 22:01:02 +02:00
|
|
|
ctx.pnames = cn.pnames
|
2015-04-29 08:09:30 +02:00
|
|
|
h = cn.handler
|
2015-06-04 00:18:27 +02:00
|
|
|
e = cn.echo
|
2015-04-29 08:09:30 +02:00
|
|
|
return
|
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 != ':' {
|
|
|
|
pl = len(cn.prefix)
|
|
|
|
l = lcp(search, cn.prefix)
|
|
|
|
}
|
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
|
|
|
|
if nt == ptype {
|
|
|
|
goto Param
|
|
|
|
} else if nt == mtype {
|
|
|
|
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 == "" {
|
|
|
|
// TODO: Needs improvement
|
|
|
|
if cn.findMchild() == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Empty value
|
|
|
|
goto MatchAny
|
|
|
|
}
|
|
|
|
|
2015-04-11 19:09:41 +02:00
|
|
|
// Static node
|
2015-04-26 21:44:38 +02:00
|
|
|
c = cn.findSchild(search[0])
|
|
|
|
if c != nil {
|
2015-05-10 07:06:13 +02:00
|
|
|
// Save next
|
|
|
|
if cn.label == '/' {
|
|
|
|
nt = ptype
|
|
|
|
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-04-13 22:12:30 +02:00
|
|
|
Param:
|
2015-04-26 21:44:38 +02:00
|
|
|
c = cn.findPchild()
|
|
|
|
if c != nil {
|
2015-05-10 07:06:13 +02:00
|
|
|
// Save next
|
|
|
|
if cn.label == '/' {
|
|
|
|
nt = mtype
|
|
|
|
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-05-05 06:02:53 +02:00
|
|
|
MatchAny:
|
2015-05-01 03:47:13 +02:00
|
|
|
c = cn.findMchild()
|
|
|
|
if c != nil {
|
|
|
|
cn = c
|
2015-06-04 01:19:03 +02:00
|
|
|
ctx.pvalues[0] = search
|
2015-05-04 22:01:02 +02:00
|
|
|
search = "" // End search
|
2015-05-10 17:22:49 +02:00
|
|
|
continue
|
2015-04-12 22:04:41 +02:00
|
|
|
}
|
2015-05-10 17:22:49 +02:00
|
|
|
// Not found
|
|
|
|
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-05-18 07:54:29 +02:00
|
|
|
if h == nil {
|
2015-05-31 00:38:02 +02:00
|
|
|
c.Error(NewHTTPError(http.StatusNotFound))
|
|
|
|
} else {
|
|
|
|
h(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
|
|
|
}
|