Struct rocket::route::RouteUri [−][src]
pub struct RouteUri<'a> { pub base: Origin<'a>, pub unmounted_origin: Origin<'a>, pub origin: Origin<'a>, // some fields omitted }
Expand description
A route URI which is matched against requests.
A route URI is composed of two components:
-
base
Otherwise known as the route’s “mount point”, the
base
is a staticOrigin
that prefixes the route URI. All route URIs have abase
. When routes are created manually withRoute::new()
, the base defaults to/
. When mounted viaRocket::mount()
, the base is explicitly specified as the first argument.use rocket::Route; use rocket::http::Method; let route = Route::new(Method::Get, "/foo/<bar>", handler); assert_eq!(route.uri.base(), "/"); let rocket = rocket::build().mount("/base", vec![route]); let routes: Vec<_> = rocket.routes().collect(); assert_eq!(routes[0].uri.base(), "/base");
-
origin
Otherwise known as the “route URI”, the
origin
is anOrigin
with potentially dynamic (<dyn>
or<dyn..>
) segments. It is prefixed with thebase
. This is the URI which is matched against incoming requests for routing.use rocket::Route; use rocket::http::Method; let route = Route::new(Method::Get, "/foo/<bar>", handler); assert_eq!(route.uri, "/foo/<bar>"); let rocket = rocket::build().mount("/base", vec![route]); let routes: Vec<_> = rocket.routes().collect(); assert_eq!(routes[0].uri, "/base/foo/<bar>");
Fields
base: Origin<'a>
The mount point.
unmounted_origin: Origin<'a>
The URI without the base
mount point.
origin: Origin<'a>
The URI with the base mount point. This is the canoncical route URI.
Implementations
The path of the base mount point of this route URI as an &str
.
Example
use rocket::Route; use rocket::http::Method; let index = Route::new(Method::Get, "/foo/bar?a=1", handler); assert_eq!(index.uri.base(), "/"); let index = index.map_base(|base| format!("{}{}", "/boo", base)).unwrap(); assert_eq!(index.uri.base(), "/boo");
The path part of this route URI as an &str
.
Example
use rocket::Route; use rocket::http::Method; let index = Route::new(Method::Get, "/foo/bar?a=1", handler); assert_eq!(index.uri.path(), "/foo/bar"); let index = index.map_base(|base| format!("{}{}", "/boo", base)).unwrap(); assert_eq!(index.uri.path(), "/boo/foo/bar");
The query part of this route URI, if there is one.
Example
use rocket::Route; use rocket::http::Method; let index = Route::new(Method::Get, "/foo/bar", handler); assert!(index.uri.query().is_none()); // Normalization clears the empty '?'. let index = Route::new(Method::Get, "/foo/bar?", handler); assert!(index.uri.query().is_none()); let index = Route::new(Method::Get, "/foo/bar?a=1", handler); assert_eq!(index.uri.query().unwrap(), "a=1"); let index = index.map_base(|base| format!("{}{}", "/boo", base)).unwrap(); assert_eq!(index.uri.query().unwrap(), "a=1");
The full URI as an &str
.
Example
use rocket::Route; use rocket::http::Method; let index = Route::new(Method::Get, "/foo/bar?a=1", handler); assert_eq!(index.uri.as_str(), "/foo/bar?a=1"); let index = index.map_base(|base| format!("{}{}", "/boo", base)).unwrap(); assert_eq!(index.uri.as_str(), "/boo/foo/bar?a=1");
Methods from Deref<Target = Origin<'a>>
Returns the path part of this URI.
Example
let uri = uri!("/a/b/c"); assert_eq!(uri.path(), "/a/b/c"); let uri = uri!("/a/b/c?name=bob"); assert_eq!(uri.path(), "/a/b/c");
Returns the query part of this URI without the question mark, if there is any.
Example
let uri = uri!("/a/b/c?alphabet=true"); assert_eq!(uri.query().unwrap(), "alphabet=true"); let uri = uri!("/a/b/c"); assert!(uri.query().is_none());
Applies the function f
to the internal path
and returns a new
Origin
with the new path. If the path returned from f
is invalid,
returns None
. Otherwise, returns Some
, even if the new path is
abnormal.
Examples
Affix a trailing slash if one isn’t present.
let uri = uri!("/a/b/c"); let expected_uri = uri!("/a/b/c/d"); assert_eq!(uri.map_path(|p| format!("{}/d", p)), Some(expected_uri)); let uri = uri!("/a/b/c"); let abnormal_map = uri.map_path(|p| format!("{}///d", p)); assert_eq!(abnormal_map.unwrap(), "/a/b/c///d"); let uri = uri!("/a/b/c"); let expected = uri!("/b/c"); let mapped = uri.map_path(|p| p.strip_prefix("/a").unwrap_or(p)); assert_eq!(mapped, Some(expected)); let uri = uri!("/a"); assert_eq!(uri.map_path(|p| p.strip_prefix("/a").unwrap_or(p)), None); let uri = uri!("/a/b/c"); assert_eq!(uri.map_path(|p| format!("hi/{}", p)), None);
Returns true
if self
is normalized. Otherwise, returns false
.
See Normalization for more information on what it
means for an origin URI to be normalized. Note that uri!()
always
normalizes static input.
Example
use rocket::http::uri::Origin; assert!(Origin::parse("/").unwrap().is_normalized()); assert!(Origin::parse("/a/b/c").unwrap().is_normalized()); assert!(Origin::parse("/a/b/c?a=b&c").unwrap().is_normalized()); assert!(!Origin::parse("/a/b/c//d").unwrap().is_normalized()); assert!(!Origin::parse("/a?q&&b").unwrap().is_normalized()); assert!(uri!("/a/b/c//d").is_normalized()); assert!(uri!("/a?q&&b").is_normalized());
Trait Implementations
Auto Trait Implementations
impl<'a> !RefUnwindSafe for RouteUri<'a>
impl<'a> UnwindSafe for RouteUri<'a>
Blanket Implementations
Mutably borrows from an owned value. Read more
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
Converts self
into a collection.
type Output = T
type Output = T
Should always be Self
pub fn vzip(self) -> V