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

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.

Should always be Self

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

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.