13
13
routes map [string ]* Route
14
14
echo * Echo
15
15
}
16
+ methodContext struct {
17
+ handler HandlerFunc
18
+ }
16
19
node struct {
17
20
kind kind
18
21
label byte
@@ -32,17 +35,17 @@ type (
32
35
kind uint8
33
36
children []* node
34
37
methodHandler struct {
35
- connect HandlerFunc
36
- delete HandlerFunc
37
- get HandlerFunc
38
- head HandlerFunc
39
- options HandlerFunc
40
- patch HandlerFunc
41
- post HandlerFunc
42
- propfind HandlerFunc
43
- put HandlerFunc
44
- trace HandlerFunc
45
- report HandlerFunc
38
+ connect * methodContext
39
+ delete * methodContext
40
+ get * methodContext
41
+ head * methodContext
42
+ options * methodContext
43
+ patch * methodContext
44
+ post * methodContext
45
+ propfind * methodContext
46
+ put * methodContext
47
+ trace * methodContext
48
+ report * methodContext
46
49
allowHeader string
47
50
}
48
51
)
@@ -209,7 +212,9 @@ func (r *Router) insert(method, path string, h HandlerFunc, t kind, ppath string
209
212
currentNode .prefix = search
210
213
if h != nil {
211
214
currentNode .kind = t
212
- currentNode .addHandler (method , h )
215
+ currentNode .addHandler (method , & methodContext {
216
+ handler : h ,
217
+ })
213
218
currentNode .ppath = ppath
214
219
currentNode .pnames = pnames
215
220
}
@@ -257,13 +262,17 @@ func (r *Router) insert(method, path string, h HandlerFunc, t kind, ppath string
257
262
if lcpLen == searchLen {
258
263
// At parent node
259
264
currentNode .kind = t
260
- currentNode .addHandler (method , h )
265
+ currentNode .addHandler (method , & methodContext {
266
+ handler : h ,
267
+ })
261
268
currentNode .ppath = ppath
262
269
currentNode .pnames = pnames
263
270
} else {
264
271
// Create child node
265
272
n = newNode (t , search [lcpLen :], currentNode , nil , new (methodHandler ), ppath , pnames , nil , nil )
266
- n .addHandler (method , h )
273
+ n .addHandler (method , & methodContext {
274
+ handler : h ,
275
+ })
267
276
// Only Static children could reach here
268
277
currentNode .addStaticChild (n )
269
278
}
@@ -278,7 +287,9 @@ func (r *Router) insert(method, path string, h HandlerFunc, t kind, ppath string
278
287
}
279
288
// Create child node
280
289
n := newNode (t , search , currentNode , nil , new (methodHandler ), ppath , pnames , nil , nil )
281
- n .addHandler (method , h )
290
+ n .addHandler (method , & methodContext {
291
+ handler : h ,
292
+ })
282
293
switch t {
283
294
case staticKind :
284
295
currentNode .addStaticChild (n )
@@ -291,7 +302,9 @@ func (r *Router) insert(method, path string, h HandlerFunc, t kind, ppath string
291
302
} else {
292
303
// Node already exists
293
304
if h != nil {
294
- currentNode .addHandler (method , h )
305
+ currentNode .addHandler (method , & methodContext {
306
+ handler : h ,
307
+ })
295
308
currentNode .ppath = ppath
296
309
if len (currentNode .pnames ) == 0 { // Issue #729
297
310
currentNode .pnames = pnames
@@ -345,7 +358,12 @@ func (n *node) findChildWithLabel(l byte) *node {
345
358
return nil
346
359
}
347
360
348
- func (n * node ) addHandler (method string , h HandlerFunc ) {
361
+ func (n * node ) addHandler (method string , h * methodContext ) {
362
+ if h .handler == nil {
363
+ n .isHandler = n .methodHandler .isHandler ()
364
+ return
365
+ }
366
+
349
367
switch method {
350
368
case http .MethodConnect :
351
369
n .methodHandler .connect = h
@@ -372,14 +390,10 @@ func (n *node) addHandler(method string, h HandlerFunc) {
372
390
}
373
391
374
392
n .methodHandler .updateAllowHeader ()
375
- if h != nil {
376
- n .isHandler = true
377
- } else {
378
- n .isHandler = n .methodHandler .isHandler ()
379
- }
393
+ n .isHandler = true
380
394
}
381
395
382
- func (n * node ) findHandler (method string ) HandlerFunc {
396
+ func (n * node ) findHandler (method string ) * methodContext {
383
397
switch method {
384
398
case http .MethodConnect :
385
399
return n .methodHandler .connect
@@ -530,7 +544,7 @@ func (r *Router) Find(method, path string, c Context) {
530
544
previousBestMatchNode = currentNode
531
545
}
532
546
if h := currentNode .findHandler (method ); h != nil {
533
- matchedHandler = h
547
+ matchedHandler = h . handler
534
548
break
535
549
}
536
550
}
@@ -581,7 +595,7 @@ func (r *Router) Find(method, path string, c Context) {
581
595
previousBestMatchNode = currentNode
582
596
}
583
597
if h := currentNode .findHandler (method ); h != nil {
584
- matchedHandler = h
598
+ matchedHandler = h . handler
585
599
break
586
600
}
587
601
}
0 commit comments