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-01 17:05:54 +02:00
|
|
|
label byte
|
|
|
|
prefix string
|
2015-04-02 14:02:52 +02:00
|
|
|
has ntype // Type of node it contains
|
2015-04-01 17:05:54 +02:00
|
|
|
handler HandlerFunc
|
|
|
|
edges edges
|
2015-04-04 19:44:48 +02:00
|
|
|
echo *Echo
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
edges []*node
|
|
|
|
ntype byte
|
|
|
|
param struct {
|
|
|
|
Name string
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
Params []param
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-04-08 23:40:49 +02:00
|
|
|
snode ntype = 1 + iota // Static node
|
|
|
|
pnode // Param node
|
|
|
|
cnode // Catch-all node
|
2015-03-01 19:45:13 +02:00
|
|
|
)
|
|
|
|
|
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{
|
|
|
|
prefix: "",
|
|
|
|
edges: edges{},
|
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-04 19:44:48 +02:00
|
|
|
r.insert(method, path[:i], nil, pnode, echo)
|
2015-03-01 19:45:13 +02:00
|
|
|
for ; i < l && path[i] != '/'; i++ {
|
|
|
|
}
|
|
|
|
if i == l {
|
2015-04-08 23:40:49 +02:00
|
|
|
r.insert(method, path[:i], h, 0, echo)
|
2015-03-01 19:45:13 +02:00
|
|
|
return
|
|
|
|
}
|
2015-04-08 23:40:49 +02:00
|
|
|
r.insert(method, path[:i], nil, 0, echo)
|
2015-03-07 07:55:51 +02:00
|
|
|
} else if path[i] == '*' {
|
2015-04-08 23:40:49 +02:00
|
|
|
r.insert(method, path[:i], nil, cnode, echo)
|
|
|
|
r.insert(method, path[:l], h, 0, echo)
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-04 19:44:48 +02:00
|
|
|
r.insert(method, path, h, snode, echo)
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
|
2015-04-04 19:44:48 +02:00
|
|
|
func (r *router) insert(method, path string, h HandlerFunc, has ntype, 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
|
|
|
|
cn.has = has
|
|
|
|
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-04 19:44:48 +02:00
|
|
|
n := newNode(cn.prefix[l:], cn.has, cn.handler, cn.edges, cn.echo)
|
2015-03-01 19:45:13 +02:00
|
|
|
cn.edges = edges{n} // Add to parent
|
|
|
|
|
|
|
|
// Reset parent node
|
|
|
|
cn.label = cn.prefix[0]
|
|
|
|
cn.prefix = cn.prefix[:l]
|
2015-04-08 23:40:49 +02:00
|
|
|
cn.has = 0
|
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-04 19:44:48 +02:00
|
|
|
n = newNode(search[l:], has, h, edges{}, echo)
|
2015-03-01 19:45:13 +02:00
|
|
|
cn.edges = append(cn.edges, n)
|
|
|
|
}
|
|
|
|
} else if l < sl {
|
|
|
|
search = search[l:]
|
|
|
|
e := cn.findEdge(search[0])
|
2015-04-08 23:40:49 +02:00
|
|
|
if e != nil {
|
|
|
|
// Go deeper
|
2015-03-01 19:45:13 +02:00
|
|
|
cn = e
|
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
|
|
|
|
n := newNode(search, has, h, edges{}, echo)
|
|
|
|
cn.edges = append(cn.edges, 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-04 19:44:48 +02:00
|
|
|
func newNode(pfx string, has ntype, h HandlerFunc, e edges, echo *Echo) (n *node) {
|
2015-03-01 19:45:13 +02:00
|
|
|
n = &node{
|
2015-04-01 17:05:54 +02:00
|
|
|
label: pfx[0],
|
|
|
|
prefix: pfx,
|
|
|
|
has: has,
|
|
|
|
handler: h,
|
|
|
|
edges: e,
|
2015-04-04 19:44:48 +02:00
|
|
|
echo: echo,
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-01 17:05:54 +02:00
|
|
|
func (n *node) findEdge(l byte) *node {
|
|
|
|
for _, e := range n.edges {
|
|
|
|
if e.label == l {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
n := 0 // Param count
|
|
|
|
|
|
|
|
for {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
pl := len(cn.prefix)
|
|
|
|
l := lcp(search, cn.prefix)
|
|
|
|
|
|
|
|
if l == pl {
|
|
|
|
search = search[l:]
|
2015-04-04 19:44:48 +02:00
|
|
|
if cn.has == pnode {
|
2015-04-05 23:21:03 +02:00
|
|
|
// Param node
|
2015-03-01 19:45:13 +02:00
|
|
|
cn = cn.edges[0]
|
2015-04-08 23:40:49 +02:00
|
|
|
i, l := 0, len(search)
|
2015-03-01 19:45:13 +02:00
|
|
|
for ; i < l && search[i] != '/'; i++ {
|
|
|
|
}
|
2015-04-09 23:59:31 +02:00
|
|
|
p := params[:n+1]
|
2015-03-01 19:45:13 +02:00
|
|
|
p[n].Name = cn.prefix[1:]
|
|
|
|
p[n].Value = search[:i]
|
|
|
|
n++
|
|
|
|
search = search[i:]
|
2015-04-08 23:40:49 +02:00
|
|
|
} else if cn.has == cnode {
|
2015-04-05 23:21:03 +02:00
|
|
|
// Catch-all node
|
2015-04-08 23:40:49 +02:00
|
|
|
cn = cn.edges[0]
|
2015-04-09 23:59:31 +02:00
|
|
|
p := params[:n+1]
|
2015-03-09 08:58:10 +02:00
|
|
|
p[n].Name = "_name"
|
|
|
|
p[n].Value = search
|
|
|
|
search = "" // End search
|
2015-04-04 19:44:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Search complete
|
|
|
|
if len(search) == 0 {
|
2015-03-07 07:55:51 +02:00
|
|
|
continue
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
2015-04-04 19:44:48 +02:00
|
|
|
|
2015-04-08 23:40:49 +02:00
|
|
|
// Go deeper
|
2015-03-01 19:45:13 +02:00
|
|
|
e := cn.findEdge(search[0])
|
|
|
|
if e == nil {
|
|
|
|
// Not found
|
|
|
|
return
|
|
|
|
}
|
2015-03-09 08:58:10 +02:00
|
|
|
cn = e
|
|
|
|
continue
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
2015-04-04 00:40:36 +02:00
|
|
|
return
|
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
|
|
|
}
|