hyper-static - a static file handler for Rust/Hyper with minimal logic
Crate: https://crates.io/crates/hyper-static
Repository: https://github.com/alttch/hyper-static
The idea is to have a static file handler with no overhead. Make any handler
function for Hyper, with own logic, if static file needs to be returned - give
the crate a path and that is it.
- serves large files with zero-copy buffers
- correctly processes partial requests (Content-Range)
- crate errors can be transformed directly into Hyper results
Example:
async fn handler(req: Request<Body>) -> Result<Response<Body>, http::Error> {
let path = std::path::Path::new("/path/to/file");
return match serve::file(
&path,
Some("text/html"), &req.headers(), 65536 )
.await
{
Ok(v) => v, Err(e) => e.into(), };
return match static_file(
&path,
Some("text/html"),
&parts.headers,
65536
)
.await
{
Ok(v) => {
debug!(
r#""GET {}" {}"#,
uri,
v.as_ref().map_or(0, |res| res.status().as_u16())
);
v
}
Err(e) => {
let resp: Result<Response<Body>, http::Error> = e.into();
warn!(
r#""GET {}" {}"#,
uri,
resp.as_ref().map_or(0, |res| res.status().as_u16())
);
resp
}
};
}