Skip to content

Commit 6e5936b

Browse files
committed
Merge branch 'hotfix/0.2.11'
2 parents 33c3753 + 4c26ff3 commit 6e5936b

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

router.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// license that can be found in the LICENSE file.
44

55
/*
6-
Package router 0.2.10 provides fast HTTP request router.
6+
Package router 0.2.11 provides fast HTTP request router.
77
88
The router matches incoming requests by the request method and the path.
99
If a handle is registered for this path and method, the router delegates the
@@ -168,6 +168,11 @@ func (r *Router) HEAD(path string, h Handle) {
168168
r.Handle("HEAD", path, h)
169169
}
170170

171+
// OPTIONS is a shortcut for Router Handle("OPTIONS", path, handle)
172+
func (r *Router) OPTIONS(path string, h Handle) {
173+
r.Handle("OPTIONS", path, h)
174+
}
175+
171176
// Handle registers a new request handle with the given path and method.
172177
func (r *Router) Handle(method, path string, h Handle) {
173178
if r.handlers[method] == nil {
@@ -202,6 +207,18 @@ func (r *Router) Lookup(method, path string) (Handle, []Param, bool) {
202207
return nil, nil, false
203208
}
204209

210+
// AllowedMethods returns list of allowed methods
211+
func (r *Router) AllowedMethods(path string) []string {
212+
var allowed []string
213+
for method, parser := range r.handlers {
214+
if _, _, ok := parser.get(path); ok {
215+
allowed = append(allowed, method)
216+
}
217+
}
218+
219+
return allowed
220+
}
221+
205222
// Listen and serve on requested host and port.
206223
func (r *Router) Listen(hostPort string) {
207224
if err := http.ListenAndServe(hostPort, r); err != nil {
@@ -239,12 +256,7 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
239256
return
240257
}
241258
}
242-
allowed := make([]string, 0, len(r.handlers))
243-
for method, parser := range r.handlers {
244-
if _, _, ok := parser.get(req.URL.Path); ok {
245-
allowed = append(allowed, method)
246-
}
247-
}
259+
allowed := r.AllowedMethods(req.URL.Path)
248260

249261
if len(allowed) == 0 {
250262
if r.NotFound != nil {

0 commit comments

Comments
 (0)