|
3 | 3 | // license that can be found in the LICENSE file. |
4 | 4 |
|
5 | 5 | /* |
6 | | -Package router 0.2.10 provides fast HTTP request router. |
| 6 | +Package router 0.2.11 provides fast HTTP request router. |
7 | 7 |
|
8 | 8 | The router matches incoming requests by the request method and the path. |
9 | 9 | 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) { |
168 | 168 | r.Handle("HEAD", path, h) |
169 | 169 | } |
170 | 170 |
|
| 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 | + |
171 | 176 | // Handle registers a new request handle with the given path and method. |
172 | 177 | func (r *Router) Handle(method, path string, h Handle) { |
173 | 178 | if r.handlers[method] == nil { |
@@ -202,6 +207,18 @@ func (r *Router) Lookup(method, path string) (Handle, []Param, bool) { |
202 | 207 | return nil, nil, false |
203 | 208 | } |
204 | 209 |
|
| 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 | + |
205 | 222 | // Listen and serve on requested host and port. |
206 | 223 | func (r *Router) Listen(hostPort string) { |
207 | 224 | if err := http.ListenAndServe(hostPort, r); err != nil { |
@@ -239,12 +256,7 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) { |
239 | 256 | return |
240 | 257 | } |
241 | 258 | } |
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) |
248 | 260 |
|
249 | 261 | if len(allowed) == 0 { |
250 | 262 | if r.NotFound != nil { |
|
0 commit comments