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 (
|
|
|
|
router struct {
|
2015-04-01 17:05:54 +02:00
|
|
|
trees map[string]*node
|
|
|
|
echo *Echo
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
node struct {
|
2015-04-14 06:57:36 +02:00
|
|
|
label byte
|
|
|
|
prefix string
|
|
|
|
parent *node
|
|
|
|
children children
|
|
|
|
handler HandlerFunc
|
|
|
|
echo *Echo
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
2015-04-14 06:57:36 +02:00
|
|
|
children []*node
|
|
|
|
param struct {
|
2015-03-01 19:45:13 +02:00
|
|
|
Name string
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
Params []param
|
|
|
|
)
|
|
|
|
|
2015-04-01 17:05:54 +02:00
|
|
|
func NewRouter(e *Echo) (r *router) {
|
2015-03-01 19:45:13 +02:00
|
|
|
r = &router{
|
2015-04-01 17:05:54 +02:00
|
|
|
trees: make(map[string]*node),
|
|
|
|
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-04-02 23:41:36 +02:00
|
|
|
func (r *router) Add(method, path string, h HandlerFunc, echo *Echo) {
|
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-12 17:05:29 +02:00
|
|
|
r.insert(method, path[:i], nil, echo)
|
2015-03-01 19:45:13 +02:00
|
|
|
for ; i < l && path[i] != '/'; i++ {
|
|
|
|
}
|
|
|
|
if i == l {
|
2015-04-12 17:05:29 +02:00
|
|
|
r.insert(method, path[:i], h, echo)
|
2015-03-01 19:45:13 +02:00
|
|
|
return
|
|
|
|
}
|
2015-04-12 17:05:29 +02:00
|
|
|
r.insert(method, path[:i], nil, echo)
|
2015-03-07 07:55:51 +02:00
|
|
|
} else if path[i] == '*' {
|
2015-04-12 17:05:29 +02:00
|
|
|
r.insert(method, path[:i], nil, echo)
|
|
|
|
r.insert(method, path[:l], h, echo)
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-12 17:05:29 +02:00
|
|
|
r.insert(method, path, h, echo)
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
|
2015-04-12 17:05:29 +02:00
|
|
|
func (r *router) insert(method, path string, h HandlerFunc, echo *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
|
|
|
|
|
|
|
|
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-01 17:05:54 +02:00
|
|
|
cn.handler = h
|
2015-04-02 23:41:36 +02:00
|
|
|
cn.echo = echo
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
} else if l < pl {
|
2015-04-08 23:40:49 +02:00
|
|
|
// Split node
|
2015-04-14 06:57:36 +02:00
|
|
|
n := newNode(cn.prefix[l:], cn, cn.children, cn.handler, cn.echo)
|
|
|
|
cn.children = children{n} // Add to parent
|
2015-03-01 19:45:13 +02:00
|
|
|
|
|
|
|
// Reset parent node
|
|
|
|
cn.label = cn.prefix[0]
|
|
|
|
cn.prefix = cn.prefix[:l]
|
2015-04-01 17:05:54 +02:00
|
|
|
cn.handler = 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-01 17:05:54 +02:00
|
|
|
cn.handler = h
|
2015-04-02 23:41:36 +02:00
|
|
|
cn.echo = echo
|
2015-03-01 19:45:13 +02:00
|
|
|
} else {
|
2015-04-08 23:40:49 +02:00
|
|
|
// Create child node
|
2015-04-14 06:57:36 +02:00
|
|
|
n = newNode(search[l:], cn, children{}, h, echo)
|
|
|
|
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-04-14 06:57:36 +02:00
|
|
|
n := newNode(search, cn, children{}, h, echo)
|
|
|
|
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-02 23:41:36 +02:00
|
|
|
cn.echo = echo
|
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-04-14 06:57:36 +02:00
|
|
|
func newNode(pfx string, p *node, c children, h HandlerFunc, echo *Echo) (n *node) {
|
2015-03-01 19:45:13 +02:00
|
|
|
n = &node{
|
2015-04-14 06:57:36 +02:00
|
|
|
label: pfx[0],
|
|
|
|
prefix: pfx,
|
|
|
|
parent: p,
|
|
|
|
children: c,
|
|
|
|
handler: h,
|
|
|
|
echo: echo,
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-04-09 23:59:31 +02:00
|
|
|
func (r *router) Find(method, path string, params Params) (h HandlerFunc, echo *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-04-14 06:57:36 +02:00
|
|
|
n := 0 // Param count
|
|
|
|
c := new(node) // Child node
|
2015-03-01 19:45:13 +02:00
|
|
|
|
2015-04-11 19:09:41 +02:00
|
|
|
// Search order static > param > catch-all
|
2015-03-01 19:45:13 +02:00
|
|
|
for {
|
2015-04-13 22:12:30 +02:00
|
|
|
if search == "" || search == cn.prefix {
|
2015-04-04 00:40:36 +02:00
|
|
|
// Found
|
2015-04-01 17:05:54 +02:00
|
|
|
h = cn.handler
|
2015-04-02 23:41:36 +02:00
|
|
|
echo = cn.echo
|
2015-03-01 19:45:13 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-11 20:10:19 +02:00
|
|
|
pl := len(cn.prefix)
|
2015-03-01 19:45:13 +02:00
|
|
|
l := lcp(search, cn.prefix)
|
2015-04-13 22:12:30 +02:00
|
|
|
|
2015-04-11 22:49:45 +02:00
|
|
|
if l == pl {
|
|
|
|
search = search[l:]
|
2015-04-14 06:57:36 +02:00
|
|
|
} else if l < pl && cn.label != ':' {
|
|
|
|
goto Up
|
2015-04-11 20:10:19 +02:00
|
|
|
}
|
2015-03-01 19:45:13 +02:00
|
|
|
|
2015-04-11 19:09:41 +02:00
|
|
|
// Static node
|
2015-04-14 06:57:36 +02:00
|
|
|
c = cn.findChild(search[0])
|
|
|
|
if c != nil {
|
|
|
|
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-14 06:57:36 +02:00
|
|
|
c = cn.findChild(':')
|
|
|
|
if c != nil {
|
|
|
|
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-12 22:04:41 +02:00
|
|
|
params[n].Name = cn.prefix[1:]
|
|
|
|
params[n].Value = search[:i]
|
2015-04-11 19:09:41 +02:00
|
|
|
n++
|
|
|
|
search = search[i:]
|
|
|
|
continue
|
|
|
|
}
|
2015-04-04 19:44:48 +02:00
|
|
|
|
2015-04-11 19:09:41 +02:00
|
|
|
// Catch-all node
|
2015-04-14 06:57:36 +02:00
|
|
|
c = cn.findChild('*')
|
|
|
|
if c != nil {
|
|
|
|
cn = c
|
2015-04-11 19:09:41 +02:00
|
|
|
p := params[:n+1]
|
|
|
|
p[n].Name = "_name"
|
|
|
|
p[n].Value = search
|
|
|
|
search = "" // End search
|
2015-03-09 08:58:10 +02:00
|
|
|
continue
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
2015-04-11 19:09:41 +02:00
|
|
|
|
2015-04-13 22:12:30 +02:00
|
|
|
Up:
|
|
|
|
tn := cn // Save current node
|
2015-04-12 22:04:41 +02:00
|
|
|
cn = cn.parent
|
|
|
|
if cn == nil {
|
|
|
|
// Not found
|
|
|
|
return
|
|
|
|
}
|
2015-04-13 22:12:30 +02:00
|
|
|
// Search upwards
|
|
|
|
if l == pl {
|
|
|
|
// Reset search
|
|
|
|
search = tn.prefix + search
|
|
|
|
}
|
|
|
|
goto Param
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-07 22:02:23 +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)
|
|
|
|
h, _ := r.Find(req.Method, req.URL.Path, c.params)
|
|
|
|
c.Response.Writer = w
|
2015-04-01 17:05:54 +02:00
|
|
|
if h != nil {
|
|
|
|
h(c)
|
|
|
|
} else {
|
|
|
|
r.echo.notFoundHandler(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
|
|
|
}
|