Struct rocket::route::RouteUri

source ·
pub struct RouteUri<'a> {
    pub base: Origin<'a>,
    pub unmounted_origin: Origin<'a>,
    pub origin: Origin<'a>,
    /* private fields */
}
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 static Origin that prefixes the route URI. All route URIs have a base. When routes are created manually with Route::new(), the base defaults to /. When mounted via Rocket::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 an Origin with potentially dynamic (<dyn> or <dyn..>) segments. It is prefixed with the base. 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 canonical route URI.

Implementations§

source§

impl<'a> RouteUri<'a>

source

pub fn base(&self) -> &str

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");
source

pub fn path(&self) -> &str

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");
source

pub fn query(&self) -> Option<&str>

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");
source

pub fn as_str(&self) -> &str

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>>§

source

pub fn path(&self) -> Path<'_>

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");
source

pub fn query(&self) -> Option<Query<'_>>

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());
source

pub fn map_path<'s, F, P>(&'s self, f: F) -> Option<Origin<'a>>where F: FnOnce(&'s RawStr) -> P, P: Into<RawStrBuf> + 's,

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);
source

pub fn is_normalized(&self) -> bool

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§

source§

impl<'a> Clone for RouteUri<'a>

source§

fn clone(&self) -> RouteUri<'a>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for RouteUri<'_>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a> Deref for RouteUri<'a>

§

type Target = Origin<'a>

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl Display for RouteUri<'_>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl PartialEq<&str> for RouteUri<'_>

source§

fn eq(&self, other: &&str) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a, 'b> PartialEq<Origin<'b>> for RouteUri<'a>

source§

fn eq(&self, other: &Origin<'b>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<str> for RouteUri<'_>

source§

fn eq(&self, other: &str) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for RouteUri<'a>

§

impl<'a> Send for RouteUri<'a>

§

impl<'a> Sync for RouteUri<'a>

§

impl<'a> Unpin for RouteUri<'a>

§

impl<'a> UnwindSafe for RouteUri<'a>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<'a, T> AsTaggedExplicit<'a> for Twhere T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self>

§

impl<'a, T> AsTaggedImplicit<'a> for Twhere T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32 ) -> TaggedParser<'a, Implicit, Self>

source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoCollection<T> for T

source§

fn into_collection<A>(self) -> SmallVec<A>where A: Array<Item = T>,

Converts self into a collection.
source§

fn mapped<U, F, A>(self, f: F) -> SmallVec<A>where F: FnMut(T) -> U, A: Array<Item = U>,

source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more