@@ -13,6 +13,10 @@ import (
13
13
"strings"
14
14
)
15
15
16
+ var (
17
+ ErrMethodMismatch = errors .New ("Method is Not Allowed" )
18
+ )
19
+
16
20
// NewRouter returns a new router instance.
17
21
func NewRouter () * Router {
18
22
return & Router {namedRoutes : make (map [string ]* Route ), KeepContext : false }
@@ -106,8 +110,8 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
106
110
req = setCurrentRoute (req , match .Route )
107
111
}
108
112
109
- if match .MethodMismatch {
110
- handler = MethodNotAllowedHandler ()
113
+ if handler == nil && match .MatchErr == ErrMethodMismatch {
114
+ handler = methodNotAllowedHandler ()
111
115
}
112
116
113
117
if handler == nil {
@@ -351,9 +355,10 @@ type RouteMatch struct {
351
355
Handler http.Handler
352
356
Vars map [string ]string
353
357
354
- // MethodMismatch flag is set to true if a route is matched but there
355
- // is a mismatch in the request method and route method
356
- MethodMismatch bool
358
+ // MatchErr is set to appropriate matching error
359
+ // It is set to ErrMethodMismatch if there is a mismatch in
360
+ // the request method and route method
361
+ MatchErr error
357
362
}
358
363
359
364
type contextKey int
@@ -556,11 +561,11 @@ func matchMapWithRegex(toCheck map[string]*regexp.Regexp, toMatch map[string][]s
556
561
return true
557
562
}
558
563
559
- // MethodNotAllowed replies to the request with an HTTP status code 405.
560
- func MethodNotAllowed (w http.ResponseWriter , r * http.Request ) {
564
+ // methodNotAllowed replies to the request with an HTTP status code 405.
565
+ func methodNotAllowed (w http.ResponseWriter , r * http.Request ) {
561
566
w .WriteHeader (http .StatusMethodNotAllowed )
562
567
}
563
568
564
- // MethodNotAllowedHandler returns a simple request handler
569
+ // methodNotAllowedHandler returns a simple request handler
565
570
// that replies to each request with a status code 405.
566
- func MethodNotAllowedHandler () http.Handler { return http .HandlerFunc (MethodNotAllowed ) }
571
+ func methodNotAllowedHandler () http.Handler { return http .HandlerFunc (methodNotAllowed ) }
0 commit comments