From 383fab0156489a705901ba06580c60215dc313c6 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 28 Mar 2022 01:40:58 +0100 Subject: [PATCH] resource leaf routing: Attempt via impl FromRequest I wrote this thinking that implementing FromRequest, so that we can return Status::NOT_FOUND would work to pass on to the next route. But, no. Signed-off-by: Ian Jackson --- daemon/main.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/daemon/main.rs b/daemon/main.rs index d19f0a28..9e59add4 100644 --- a/daemon/main.rs +++ b/daemon/main.rs @@ -129,6 +129,23 @@ struct CheckedResourceLeaf { #[error("not a valid resource path")] struct UnknownResource; +impl ResponseError for UnknownResource { + fn status_code(&self) -> StatusCode { StatusCode::NOT_FOUND } + fn error_response(&self) -> HttpResponse { error_response(self) } +} + +// Magically looks for "{leaf}" +impl FromRequest for CheckedResourceLeaf { + type Error = UnknownResource; + type Future = future::Ready>; + fn from_request(req: &HttpRequest, _: &mut actix_web::dev::Payload) + -> Self::Future { + future::ready( + req.match_info().query("leaf").parse() + ) + } +} + impl FromStr for CheckedResourceLeaf { type Err = UnknownResource; fn from_str(s: &str) -> Result { @@ -298,8 +315,7 @@ async fn updates_route(query: Query) -> impl Responder { #[route("/_/{leaf}", method="GET", method="HEAD")] #[throws(io::Error)] -async fn resource(leaf: Path>) -> impl Responder { - let leaf = leaf.into_inner().0; +async fn resource(leaf: CheckedResourceLeaf) -> impl Responder { let path = match leaf.locn { RL::Main => format!("{}/{}", config().template_dir, leaf.safe_leaf), RL::Wasm(s) => format!("{}/{}", config().wasm_dir, s), -- 2.30.2