Skip to content

Commit

Permalink
Change methodHandler element type to methodContext
Browse files Browse the repository at this point in the history
Signed-off-by: ortyomka <iurin.art@gmail.com>
  • Loading branch information
ortyomka committed Jun 29, 2022
1 parent 0644cd6 commit 3e45d2a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 26 deletions.
64 changes: 39 additions & 25 deletions router.go
Expand Up @@ -13,6 +13,9 @@ type (
routes map[string]*Route
echo *Echo
}
methodContext struct {
handler HandlerFunc
}
node struct {
kind kind
label byte
Expand All @@ -32,17 +35,17 @@ type (
kind uint8
children []*node
methodHandler struct {
connect HandlerFunc
delete HandlerFunc
get HandlerFunc
head HandlerFunc
options HandlerFunc
patch HandlerFunc
post HandlerFunc
propfind HandlerFunc
put HandlerFunc
trace HandlerFunc
report HandlerFunc
connect *methodContext
delete *methodContext
get *methodContext
head *methodContext
options *methodContext
patch *methodContext
post *methodContext
propfind *methodContext
put *methodContext
trace *methodContext
report *methodContext
allowHeader string
}
)
Expand Down Expand Up @@ -209,7 +212,9 @@ func (r *Router) insert(method, path string, h HandlerFunc, t kind, ppath string
currentNode.prefix = search
if h != nil {
currentNode.kind = t
currentNode.addHandler(method, h)
currentNode.addHandler(method, &methodContext{
handler: h,
})
currentNode.ppath = ppath
currentNode.pnames = pnames
}
Expand Down Expand Up @@ -257,13 +262,17 @@ func (r *Router) insert(method, path string, h HandlerFunc, t kind, ppath string
if lcpLen == searchLen {
// At parent node
currentNode.kind = t
currentNode.addHandler(method, h)
currentNode.addHandler(method, &methodContext{
handler: h,
})
currentNode.ppath = ppath
currentNode.pnames = pnames
} else {
// Create child node
n = newNode(t, search[lcpLen:], currentNode, nil, new(methodHandler), ppath, pnames, nil, nil)
n.addHandler(method, h)
n.addHandler(method, &methodContext{
handler: h,
})
// Only Static children could reach here
currentNode.addStaticChild(n)
}
Expand All @@ -278,7 +287,9 @@ func (r *Router) insert(method, path string, h HandlerFunc, t kind, ppath string
}
// Create child node
n := newNode(t, search, currentNode, nil, new(methodHandler), ppath, pnames, nil, nil)
n.addHandler(method, h)
n.addHandler(method, &methodContext{
handler: h,
})
switch t {
case staticKind:
currentNode.addStaticChild(n)
Expand All @@ -291,7 +302,9 @@ func (r *Router) insert(method, path string, h HandlerFunc, t kind, ppath string
} else {
// Node already exists
if h != nil {
currentNode.addHandler(method, h)
currentNode.addHandler(method, &methodContext{
handler: h,
})
currentNode.ppath = ppath
if len(currentNode.pnames) == 0 { // Issue #729
currentNode.pnames = pnames
Expand Down Expand Up @@ -345,7 +358,12 @@ func (n *node) findChildWithLabel(l byte) *node {
return nil
}

func (n *node) addHandler(method string, h HandlerFunc) {
func (n *node) addHandler(method string, h *methodContext) {
if h.handler == nil {
n.isHandler = n.methodHandler.isHandler()
return
}

switch method {
case http.MethodConnect:
n.methodHandler.connect = h
Expand All @@ -372,14 +390,10 @@ func (n *node) addHandler(method string, h HandlerFunc) {
}

n.methodHandler.updateAllowHeader()
if h != nil {
n.isHandler = true
} else {
n.isHandler = n.methodHandler.isHandler()
}
n.isHandler = true
}

func (n *node) findHandler(method string) HandlerFunc {
func (n *node) findHandler(method string) *methodContext {
switch method {
case http.MethodConnect:
return n.methodHandler.connect
Expand Down Expand Up @@ -530,7 +544,7 @@ func (r *Router) Find(method, path string, c Context) {
previousBestMatchNode = currentNode
}
if h := currentNode.findHandler(method); h != nil {
matchedHandler = h
matchedHandler = h.handler
break
}
}
Expand Down Expand Up @@ -581,7 +595,7 @@ func (r *Router) Find(method, path string, c Context) {
previousBestMatchNode = currentNode
}
if h := currentNode.findHandler(method); h != nil {
matchedHandler = h
matchedHandler = h.handler
break
}
}
Expand Down
2 changes: 1 addition & 1 deletion router_test.go
Expand Up @@ -2380,7 +2380,7 @@ func TestRouterHandleMethodOptions(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, tc.expectStatus, rec.Code)
}
assert.Equal(t, tc.expectAllowHeader, c.Response().Header().Get("Allow"))
assert.Equal(t, tc.expectAllowHeader, c.Response().Header().Get(HeaderAllow))
})
}
}
Expand Down

0 comments on commit 3e45d2a

Please sign in to comment.