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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

The resulting type after dereferencing.

Dereferences the value.

Formats the value using the given formatter. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Converts self into T using Into<T>. Read more

Converts self into a target type. Read more

Causes self to use its Binary implementation when Debug-formatted.

Causes self to use its Display implementation when Debug-formatted. Read more

Causes self to use its LowerExp implementation when Debug-formatted. Read more

Causes self to use its LowerHex implementation when Debug-formatted. Read more

Causes self to use its Octal implementation when Debug-formatted.

Causes self to use its Pointer implementation when Debug-formatted. Read more

Causes self to use its UpperExp implementation when Debug-formatted. Read more

Causes self to use its UpperHex implementation when Debug-formatted. Read more

Performs the conversion.

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

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

Performs the conversion.

Converts self into a collection.

Pipes by value. This is generally the method you want to use. Read more

Borrows self and passes that borrow into the pipe function. Read more

Mutably borrows self and passes that borrow into the pipe function. Read more

Borrows self, then passes self.borrow() into the pipe function. Read more

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more

Borrows self, then passes self.as_ref() into the pipe function.

Mutably borrows self, then passes self.as_mut() into the pipe function. Read more

Borrows self, then passes self.deref() into the pipe function.

Mutably borrows self, then passes self.deref_mut() into the pipe function. Read more

Pipes a value into a function that cannot ordinarily be called in suffix position. Read more

Pipes a trait borrow into a function that cannot normally be called in suffix position. Read more

Pipes a trait mutable borrow into a function that cannot normally be called in suffix position. Read more

Pipes a trait borrow into a function that cannot normally be called in suffix position. Read more

Pipes a trait mutable borrow into a function that cannot normally be called in suffix position. Read more

Pipes a dereference into a function that cannot normally be called in suffix position. Read more

Pipes a mutable dereference into a function that cannot normally be called in suffix position. Read more

Pipes a reference into a function that cannot ordinarily be called in suffix position. Read more

Pipes a mutable reference into a function that cannot ordinarily be called in suffix position. Read more

Should always be Self

Immutable access to a value. Read more

Mutable access to a value. Read more

Immutable access to the Borrow<B> of a value. Read more

Mutable access to the BorrowMut<B> of a value. Read more

Immutable access to the AsRef<R> view of a value. Read more

Mutable access to the AsMut<R> view of a value. Read more

Immutable access to the Deref::Target of a value. Read more

Mutable access to the Deref::Target of a value. Read more

Calls .tap() only in debug builds, and is erased in release builds.

Calls .tap_mut() only in debug builds, and is erased in release builds. Read more

Calls .tap_borrow() only in debug builds, and is erased in release builds. Read more

Calls .tap_borrow_mut() only in debug builds, and is erased in release builds. Read more

Calls .tap_ref() only in debug builds, and is erased in release builds. Read more

Calls .tap_ref_mut() only in debug builds, and is erased in release builds. Read more

Calls .tap_deref() only in debug builds, and is erased in release builds. Read more

Calls .tap_deref_mut() only in debug builds, and is erased in release builds. Read more

Provides immutable access for inspection. Read more

Calls tap in debug builds, and does nothing in release builds.

Provides mutable access for modification. Read more

Calls tap_mut in debug builds, and does nothing in release builds.

Provides immutable access to the reference for inspection.

Calls tap_ref in debug builds, and does nothing in release builds.

Provides mutable access to the reference for modification.

Calls tap_ref_mut in debug builds, and does nothing in release builds.

Provides immutable access to the borrow for inspection. Read more

Calls tap_borrow in debug builds, and does nothing in release builds.

Provides mutable access to the borrow for modification.

Calls tap_borrow_mut in debug builds, and does nothing in release builds. Read more

Immutably dereferences self for inspection.

Calls tap_deref in debug builds, and does nothing in release builds.

Mutably dereferences self for modification.

Calls tap_deref_mut in debug builds, and does nothing in release builds. Read more

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

Converts the given value to a String. Read more

Attempts to convert self into T using TryInto<T>. Read more

Attempts to convert self into a target type. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.