Skip to content

Commit 84aa8eb

Browse files
authored
Merge pull request #388 from isgj/method-not-allowed
Add `Method Not Allowed` in the Router
2 parents 4e2857a + 8ddbaf4 commit 84aa8eb

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/router.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ impl<State: 'static> Router<State> {
6161
// if not then fallback to the behavior of HTTP GET else proceed as usual
6262

6363
self.route(path, http::Method::GET)
64+
} else if self
65+
.method_map
66+
.iter()
67+
.filter(|(k, _)| *k != method)
68+
.any(|(_, r)| r.recognize(path).is_ok())
69+
{
70+
// If this `path` can be handled by a callback registered with a different HTTP method
71+
// should return 405 Method Not Allowed
72+
Selection {
73+
endpoint: &method_not_allowed,
74+
params: Params::new(),
75+
}
6476
} else {
6577
Selection {
6678
endpoint: &not_found_endpoint,
@@ -73,3 +85,7 @@ impl<State: 'static> Router<State> {
7385
fn not_found_endpoint<State>(_cx: Request<State>) -> BoxFuture<'static, Response> {
7486
Box::pin(async move { Response::new(http::StatusCode::NOT_FOUND.as_u16()) })
7587
}
88+
89+
fn method_not_allowed<State>(_cx: Request<State>) -> BoxFuture<'static, Response> {
90+
Box::pin(async move { Response::new(http::StatusCode::METHOD_NOT_ALLOWED.as_u16()) })
91+
}

0 commit comments

Comments
 (0)