use std::convert::Infallible;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use http::header::{ALLOW, CONTENT_LENGTH, HeaderValue};
use http::{Method, StatusCode};
use tower::Service;
mod path;
mod pattern;
mod tree;
pub use path::{ParamProblem, PathParams, PathProblem, check_path, decode_param, percent_decode};
pub use pattern::{MAX_PARAMS, PathPattern, PatternError, PatternErrorKind, Segment};
pub use tree::{InsertError, Matched, PathTree};
use crate::AppError;
use crate::body::{Body, HttpBody};
use crate::extract::Request;
use crate::handler::{BoxedService, Endpoint, Handler};
use crate::response::{IntoResponse, Response};
#[derive(Clone, Default)]
pub struct Router {
tree: Arc<PathTree<Methods>>,
}
#[derive(Clone, Default)]
struct Methods {
endpoints: Vec<(Method, BoxedService)>,
any: Option<BoxedService>,
allow: Option<HeaderValue>,
}
impl Methods {
fn get(&self, method: &Method) -> Option<&BoxedService> {
self.endpoints
.iter()
.find(|(candidate, _)| candidate == method)
.map(|(_, endpoint)| endpoint)
.or(self.any.as_ref())
}
fn has(&self, method: &Method) -> bool {
self.endpoints
.iter()
.any(|(candidate, _)| candidate == method)
}
fn compute_allow(&mut self) {
const ORDER: [Method; 9] = [
Method::GET,
Method::HEAD,
Method::POST,
Method::PUT,
Method::PATCH,
Method::DELETE,
Method::OPTIONS,
Method::TRACE,
Method::CONNECT,
];
let listed = |method: &Method| match *method {
Method::HEAD => self.has(&Method::HEAD) || self.has(&Method::GET),
Method::OPTIONS => true,
_ => self.has(method),
};
let mut names: Vec<&str> = ORDER
.iter()
.filter(|method| listed(method))
.map(Method::as_str)
.collect();
names.extend(
self.endpoints
.iter()
.map(|(method, _)| method)
.filter(|method| !ORDER.contains(method))
.map(Method::as_str),
);
self.allow = HeaderValue::from_str(&names.join(", ")).ok();
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum RouteError {
Pattern(PatternError),
Conflict {
method: Method,
pattern: String,
existing: String,
},
}
impl std::fmt::Display for RouteError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
RouteError::Pattern(error) => error.fmt(f),
RouteError::Conflict {
method,
pattern,
existing,
} if pattern == existing => write!(f, "`{method} {pattern}` is routed twice"),
RouteError::Conflict {
method,
pattern,
existing,
} => write!(
f,
"`{method} {pattern}` matches the same paths as `{method} {existing}` (only parameter names differ)"
),
}
}
}
impl std::error::Error for RouteError {}
impl Router {
pub fn new() -> Self {
Self::default()
}
pub fn route<H, T>(mut self, method: Method, pattern: &str, handler: H) -> Self
where
H: Handler<T>,
T: 'static,
{
if let Err(error) = self.try_route(method, pattern, Endpoint::new(handler)) {
panic!("{error}");
}
self
}
pub fn try_route(
&mut self,
method: Method,
pattern: &str,
endpoint: Endpoint,
) -> Result<(), RouteError> {
self.try_route_service(method, pattern, || endpoint.into_service())
}
pub(crate) fn try_route_service(
&mut self,
method: Method,
pattern: &str,
mount: impl FnOnce() -> BoxedService,
) -> Result<(), RouteError> {
let parsed = PathPattern::parse(pattern).map_err(RouteError::Pattern)?;
let tree = Arc::make_mut(&mut self.tree);
if let Some((existing, methods)) = tree.same_shape_mut(&parsed) {
if methods.any.is_some() || methods.has(&method) {
return Err(RouteError::Conflict {
method,
pattern: parsed.as_str().to_owned(),
existing: existing.as_str().to_owned(),
});
}
methods.endpoints.push((method, mount()));
methods.compute_allow();
return Ok(());
}
let mut methods = Methods {
endpoints: vec![(method, mount())],
any: None,
allow: None,
};
methods.compute_allow();
tree.insert_parsed(parsed, methods)
.map_err(|error| match error {
InsertError::Pattern(error) => RouteError::Pattern(error),
InsertError::Conflict { .. } => unreachable!("checked by shape above"),
})
}
pub(crate) fn try_route_any(
&mut self,
pattern: &str,
mount: impl FnOnce() -> BoxedService,
) -> Result<(), RouteError> {
let parsed = PathPattern::parse(pattern).map_err(RouteError::Pattern)?;
let tree = Arc::make_mut(&mut self.tree);
if let Some((existing, methods)) = tree.same_shape_mut(&parsed) {
let method = methods
.endpoints
.first()
.map_or(Method::GET, |(method, _)| method.clone());
return Err(RouteError::Conflict {
method,
pattern: parsed.as_str().to_owned(),
existing: existing.as_str().to_owned(),
});
}
let mut methods = Methods {
endpoints: Vec::new(),
any: Some(mount()),
allow: None,
};
methods.compute_allow();
tree.insert_parsed(parsed, methods)
.map_err(|error| match error {
InsertError::Pattern(error) => RouteError::Pattern(error),
InsertError::Conflict { .. } => unreachable!("checked by shape above"),
})
}
pub fn route_layer<L>(mut self, layer: L) -> Self
where
L: tower::Layer<BoxedService>,
L::Service: Service<Request, Response = Response, Error = Infallible>
+ Clone
+ Send
+ Sync
+ 'static,
<L::Service as Service<Request>>::Future: Send + 'static,
{
let tree = Arc::make_mut(&mut self.tree);
for methods in tree.values_mut() {
for (_, endpoint) in &mut methods.endpoints {
*endpoint = BoxedService::new(layer.layer(endpoint.clone()));
}
if let Some(endpoint) = &mut methods.any {
*endpoint = BoxedService::new(layer.layer(endpoint.clone()));
}
}
self
}
pub fn is_empty(&self) -> bool {
self.tree.is_empty()
}
fn dispatch(&self, mut request: Request) -> Dispatch {
let path = request.uri().path();
if let Err(problem) = check_path(path) {
return Dispatch::Ready(AppError::bad_request(problem.to_string()).into_response());
}
let Some(found) = self.tree.at(path) else {
return Dispatch::Ready(AppError::not_found().into_response());
};
let methods = found.value;
let method = request.method();
let (endpoint, head) = match methods.get(method) {
Some(endpoint) => (endpoint, false),
None if *method == Method::HEAD => match methods.get(&Method::GET) {
Some(endpoint) => (endpoint, true),
None => return Dispatch::Ready(method_not_allowed(methods)),
},
None if *method == Method::OPTIONS => {
let mut response = StatusCode::NO_CONTENT.into_response();
if let Some(allow) = &methods.allow {
response.headers_mut().insert(ALLOW, allow.clone());
}
return Dispatch::Ready(response);
}
None => return Dispatch::Ready(method_not_allowed(methods)),
};
let endpoint = endpoint.clone();
if !found.raw_values().is_empty() {
let decoded = PathParams::decode(
found.pattern.shared_names(),
found.raw_values(),
found.ends_in_catch_all(),
);
match decoded {
Ok(params) => {
request.extensions_mut().insert(params);
}
Err((name, problem)) => {
return Dispatch::Ready(
AppError::bad_request(problem.message(&name)).into_response(),
);
}
}
}
Dispatch::Endpoint {
future: Box::pin(crate::handler::call(endpoint, request)),
head,
}
}
}
fn method_not_allowed(methods: &Methods) -> Response {
let mut error = AppError::from_status(StatusCode::METHOD_NOT_ALLOWED);
if let Some(allow) = &methods.allow {
error = error.with_header(ALLOW, allow.clone());
}
error.into_response()
}
enum Dispatch {
Ready(Response),
Endpoint {
future: Pin<Box<dyn Future<Output = Response> + Send>>,
head: bool,
},
}
impl Service<Request> for Router {
type Response = Response;
type Error = Infallible;
type Future = RouteFuture;
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Infallible>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, request: Request) -> Self::Future {
RouteFuture(match self.dispatch(request) {
Dispatch::Ready(response) => State::Ready(Some(response)),
Dispatch::Endpoint { future, head } => State::Endpoint { future, head },
})
}
}
pub struct RouteFuture(State);
enum State {
Ready(Option<Response>),
Endpoint {
future: Pin<Box<dyn Future<Output = Response> + Send>>,
head: bool,
},
}
impl Future for RouteFuture {
type Output = Result<Response, Infallible>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match &mut self.0 {
State::Ready(response) => Poll::Ready(Ok(response
.take()
.expect("a route future is polled after it completed"))),
State::Endpoint { future, head } => {
let head = *head;
future.as_mut().poll(cx).map(|response| {
Ok(if head {
without_body(response)
} else {
response
})
})
}
}
}
}
fn without_body(mut response: Response) -> Response {
if !response.headers().contains_key(CONTENT_LENGTH)
&& let Some(length) = response.body().size_hint().exact()
{
response
.headers_mut()
.insert(CONTENT_LENGTH, HeaderValue::from(length));
}
*response.body_mut() = Body::empty();
response
}
impl std::fmt::Debug for Router {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_list()
.entries(self.tree.iter().flat_map(|(pattern, methods)| {
methods
.endpoints
.iter()
.map(move |(method, _)| format!("{method} {pattern}"))
.chain(methods.any.iter().map(move |_| format!("* {pattern}")))
}))
.finish()
}
}
#[cfg(test)]
mod tests {
use http_body_util::BodyExt;
use tower::ServiceExt;
use super::*;
use crate::Path;
async fn send(
router: &Router,
method: Method,
uri: &str,
) -> (StatusCode, http::HeaderMap, String) {
let request = http::Request::builder()
.method(method)
.uri(uri)
.body(Body::empty())
.unwrap();
let response = router.clone().oneshot(request).await.unwrap();
let status = response.status();
let headers = response.headers().clone();
let body = response.into_body().collect().await.unwrap().to_bytes();
(status, headers, String::from_utf8_lossy(&body).into_owned())
}
fn deals() -> Router {
async fn show(Path(id): Path<String>) -> String {
format!("deal {id}")
}
async fn file(Path((id, path)): Path<(String, String)>) -> String {
format!("{id}:{path}")
}
Router::new()
.route(Method::GET, "/deals", || async { "index" })
.route(Method::POST, "/deals", || async { "store" })
.route(Method::GET, "/deals/{id}", show)
.route(Method::DELETE, "/deals/{deal}", || async { "destroy" })
.route(Method::GET, "/deals/{id}/files/{*path}", file)
}
#[tokio::test]
async fn requests_reach_their_route_with_decoded_parameters() {
let router = deals();
assert_eq!(send(&router, Method::GET, "/deals").await.2, "index");
assert_eq!(send(&router, Method::POST, "/deals").await.2, "store");
assert_eq!(
send(&router, Method::GET, "/deals/caf%C3%A9").await.2,
"deal café"
);
assert_eq!(send(&router, Method::DELETE, "/deals/7").await.2, "destroy");
assert_eq!(
send(&router, Method::GET, "/deals/7/files/a%2Fb/c.txt?x=1")
.await
.2,
"7:a/b/c.txt"
);
}
#[tokio::test]
async fn unmatched_paths_are_404_and_other_methods_405() {
let router = deals();
let (status, headers, body) = send(&router, Method::GET, "/nope").await;
assert_eq!(status, StatusCode::NOT_FOUND);
assert_eq!(headers["content-type"], "application/json");
assert!(body.contains("\"code\":\"not_found\""), "{body}");
for path in ["/deals/", "//deals", "/deals/7/files", "/deals/7/files/"] {
assert_eq!(
send(&router, Method::GET, path).await.0,
StatusCode::NOT_FOUND,
"{path}"
);
}
let (status, headers, body) = send(&router, Method::PUT, "/deals").await;
assert_eq!(status, StatusCode::METHOD_NOT_ALLOWED);
assert_eq!(headers["allow"], "GET, HEAD, POST, OPTIONS");
assert!(body.contains("\"code\":\"method_not_allowed\""), "{body}");
let (_, headers, _) = send(&router, Method::PATCH, "/deals/7").await;
assert_eq!(headers["allow"], "GET, HEAD, DELETE, OPTIONS");
}
#[tokio::test]
async fn head_runs_get_without_the_body_and_options_lists_the_methods() {
let router = deals();
let (status, headers, body) = send(&router, Method::HEAD, "/deals/7").await;
assert_eq!(status, StatusCode::OK);
assert_eq!(body, "");
assert_eq!(headers["content-length"], "6");
let (status, headers, body) = send(&router, Method::OPTIONS, "/deals").await;
assert_eq!(status, StatusCode::NO_CONTENT);
assert_eq!(headers["allow"], "GET, HEAD, POST, OPTIONS");
assert_eq!(body, "");
assert_eq!(
send(&router, Method::OPTIONS, "/nope").await.0,
StatusCode::NOT_FOUND
);
let explicit = Router::new()
.route(Method::POST, "/x", || async { "post" })
.route(Method::OPTIONS, "/x", || async { "mine" });
assert_eq!(send(&explicit, Method::OPTIONS, "/x").await.2, "mine");
assert_eq!(
send(&explicit, Method::HEAD, "/x").await.0,
StatusCode::METHOD_NOT_ALLOWED,
"HEAD needs a GET route"
);
}
#[tokio::test]
async fn ambiguous_paths_are_400_before_matching() {
let router = deals();
for path in [
"/deals/..",
"/deals/./7",
"/deals/%2e%2E",
"/deals/7/files/a/../b",
"/deals/7/files/a/%2E%2E/b",
"/deals\\7",
] {
let (status, _, body) = send(&router, Method::GET, path).await;
assert_eq!(status, StatusCode::BAD_REQUEST, "{path}");
assert!(body.contains("Invalid URL"), "{path}: {body}");
}
for (path, message) in [
("/deals/a%2Fb", "Invalid URL: `id` holds an encoded slash"),
("/deals/%FF", "Invalid URL: Invalid UTF-8 in `id`"),
("/deals/a%00", "Invalid URL: `id` holds a NUL character"),
("/deals/a%5Cb", "Invalid URL: `id` holds a backslash"),
(
"/deals/7/files/a%2F..%2Fb",
"Invalid URL: `path` is or holds a `.` or `..` segment",
),
] {
let (status, _, body) = send(&router, Method::GET, path).await;
assert_eq!(status, StatusCode::BAD_REQUEST, "{path}");
assert!(body.contains(message), "{path}: {body}");
}
}
#[test]
fn conflicts_and_bad_patterns_are_refused() {
let mut router = deals();
let error = router
.try_route(Method::GET, "/deals/{deal}", Endpoint::new(|| async {}))
.unwrap_err();
assert_eq!(
error.to_string(),
"`GET /deals/{deal}` matches the same paths as `GET /deals/{id}` (only parameter names differ)"
);
let error = router
.try_route(Method::GET, "/deals", Endpoint::new(|| async {}))
.unwrap_err();
assert_eq!(error.to_string(), "`GET /deals` is routed twice");
assert!(matches!(
router.try_route(Method::GET, "/deals/:id", Endpoint::new(|| async {})),
Err(RouteError::Pattern(_))
));
assert!(
router
.try_route(Method::PUT, "/deals/{deal}", Endpoint::new(|| async {}))
.is_ok()
);
assert_eq!(
format!("{router:?}"),
r#"["GET /deals", "POST /deals", "GET /deals/{id}", "DELETE /deals/{id}", "PUT /deals/{id}", "GET /deals/{id}/files/{*path}"]"#
);
}
#[tokio::test]
async fn extension_methods_route_and_are_allowed() {
let report = Method::from_bytes(b"REPORT").unwrap();
let router = Router::new()
.route(report.clone(), "/cal", || async { "report" })
.route(Method::GET, "/cal", || async { "get" });
assert_eq!(send(&router, report, "/cal").await.2, "report");
let (_, headers, _) = send(&router, Method::DELETE, "/cal").await;
assert_eq!(headers["allow"], "GET, HEAD, OPTIONS, REPORT");
}
#[tokio::test]
async fn a_path_can_take_every_method_at_one_endpoint() {
let echo = || {
Endpoint::new(|request: Request| async move {
format!("{} {}", request.method(), request.uri().path())
})
.into_service()
};
let mut router = Router::new();
router.try_route_any("/khata", echo).unwrap();
router.try_route_any("/khata/{*rest}", echo).unwrap();
router
.try_route(Method::GET, "/deals", Endpoint::new(|| async { "deals" }))
.unwrap();
let propfind = Method::from_bytes(b"PROPFIND").unwrap();
for method in [
Method::GET,
Method::POST,
Method::DELETE,
Method::PATCH,
Method::OPTIONS,
Method::HEAD,
propfind,
] {
assert_eq!(
send(&router, method.clone(), "/khata/a").await.2,
format!("{method} /khata/a")
);
}
assert_eq!(send(&router, Method::PUT, "/khata").await.2, "PUT /khata");
let (status, headers, _) = send(&router, Method::POST, "/deals").await;
assert_eq!(status, StatusCode::METHOD_NOT_ALLOWED);
assert_eq!(headers["allow"], "GET, HEAD, OPTIONS");
assert_eq!(
format!("{router:?}"),
r#"["* /khata", "* /khata/{*rest}", "GET /deals"]"#
);
}
#[test]
fn every_method_at_one_endpoint_and_a_method_on_the_same_shape_conflict() {
let any = || Endpoint::new(|| async {}).into_service();
let mut router = Router::new();
router
.try_route(Method::GET, "/khata/{*rest}", Endpoint::new(|| async {}))
.unwrap();
assert_eq!(
router
.try_route_any("/khata/{*path}", any)
.unwrap_err()
.to_string(),
"`GET /khata/{*path}` matches the same paths as `GET /khata/{*rest}` (only parameter names differ)"
);
let mut router = Router::new();
router.try_route_any("/khata", any).unwrap();
assert_eq!(
router
.try_route(Method::POST, "/khata", Endpoint::new(|| async {}))
.unwrap_err()
.to_string(),
"`POST /khata` is routed twice"
);
}
}