Multiple Segments
You can also match against multiple segments by using <param..>
in a route path. The type of such parameters, known as segments parameters, must implement FromSegments. Segments parameters must be the final component of a path: any text after a segments parameter will result in a compile-time error.
As an example, the following route matches against all paths that begin with /page/
:
#[get("/page/<path..>")]
fn get_page(path: PathBuf) -> T { ... }
The path after /page/
will be available in the path
parameter. The FromSegments
implementation for PathBuf
ensures that path
cannot lead to path traversal attacks. With this, a safe and secure static file server can be implemented in 4 lines:
#[get("/<file..>")]
fn files(file: PathBuf) -> Option<NamedFile> {
NamedFile::open(Path::new("static/").join(file)).ok()
}