From: Ian Jackson Date: Mon, 28 Mar 2022 00:40:58 +0000 (+0100) Subject: resource leaf routing: Attempt via impl FromRequest X-Git-Tag: otter-1.0.0~92 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=383fab0156489a705901ba06580c60215dc313c6;p=otter.git 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 --- 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),