Struct rocket::http::uri::Origin[]

pub struct Origin<'a> { /* fields omitted */ }

A URI with an absolute path and optional query: /path?query.

Origin URIs are the primary type of URI encountered in Rocket applications. They are also the simplest type of URIs, made up of only a path and an optional query.

Structure

The following diagram illustrates the syntactic structure of an origin URI:

/first_segment/second_segment/third?optional=query
|---------------------------------| |------------|
                path                    query

The URI must begin with a /, can be followed by any number of segments, and an optional ? query separator and query string.

Normalization

Rocket prefers, and will sometimes require, origin URIs to be normalized. A normalized origin URI is a valid origin URI that contains zero empty segments except when there are no segments.

As an example, the following URIs are all valid, normalized URIs:

"/",
"/a/b/c",
"/a/b/c?q",
"/hello?lang=en",
"/some%20thing?q=foo&lang=fr",

By contrast, the following are valid but abnormal URIs:

"//",               // one empty segment
"/a/b/",            // trailing empty segment
"/a/ab//c//d",      // two empty segments
"/?a&&b",           // empty query segment
"/?foo&",           // trailing empty query segment

The Origin::into_normalized() method can be used to normalize any Origin:

// abnormal versions
"//", "/a/b/", "/a/ab//c//d", "/a?a&&b&",

// normalized versions
"/",  "/a/b",  "/a/ab/c/d", "/a?a&b",

Implementations

impl<'a> Origin<'a>

pub fn parse(string: &'a str) -> Result<Origin<'a>, Error<'a>>

Parses the string string into an Origin. Parsing will never allocate. Returns an Error if string is not a valid origin URI.

Example

use rocket::http::uri::Origin;

// Parse a valid origin URI.
let uri = Origin::parse("/a/b/c?query").expect("valid URI");
assert_eq!(uri.path(), "/a/b/c");
assert_eq!(uri.query().unwrap(), "query");

// Invalid URIs fail to parse.
Origin::parse("foo bar").expect_err("invalid URI");

pub fn parse_owned(string: String) -> Result<Origin<'static>, Error<'static>>

Parses the string string into an Origin. Parsing will never allocate. This method should be used instead of Origin::parse() when the source URI is already a String. Returns an Error if string is not a valid origin URI.

Example

use rocket::http::uri::Origin;

let source = format!("/foo/{}/three", 2);
let uri = Origin::parse_owned(source).expect("valid URI");
assert_eq!(uri.path(), "/foo/2/three");
assert_eq!(uri.query(), None);

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.

Example

use rocket::http::uri::Origin;

let normal = Origin::parse("/").unwrap();
assert!(normal.is_normalized());

let normal = Origin::parse("/a/b/c").unwrap();
assert!(normal.is_normalized());

let normal = Origin::parse("/a/b/c?a=b&c").unwrap();
assert!(normal.is_normalized());

let abnormal = Origin::parse("/a/b/c//d").unwrap();
assert!(!abnormal.is_normalized());

let abnormal = Origin::parse("/a?q&&b").unwrap();
assert!(!abnormal.is_normalized());

pub fn into_normalized(self) -> Origin<'a>

Normalizes self.

See Normalization for more information on what it means for an origin URI to be normalized.

Example

use rocket::http::uri::Origin;

let abnormal = Origin::parse("/a/b/c//d").unwrap();
assert!(!abnormal.is_normalized());

let normalized = abnormal.into_normalized();
assert!(normalized.is_normalized());
assert_eq!(normalized, Origin::parse("/a/b/c/d").unwrap());

pub fn path(&self) -> &RawStr

Returns the path part of this URI.

Examples

A URI with only a path:

use rocket::http::uri::Origin;

let uri = Origin::parse("/a/b/c").unwrap();
assert_eq!(uri.path(), "/a/b/c");

A URI with a query:

use rocket::http::uri::Origin;

let uri = Origin::parse("/a/b/c?name=bob").unwrap();
assert_eq!(uri.path(), "/a/b/c");

pub fn map_path<F>(&self, f: F) -> Option<Origin<'a>> where
    F: FnOnce(&RawStr) -> String

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.

use rocket::http::uri::Origin;

let old_uri = Origin::parse("/a/b/c").unwrap();
let expected_uri = Origin::parse("/a/b/c/").unwrap();
assert_eq!(old_uri.map_path(|p| format!("{}/", p)), Some(expected_uri));

let old_uri = Origin::parse("/a/b/c/").unwrap();
let expected_uri = Origin::parse("/a/b/c//").unwrap();
assert_eq!(old_uri.map_path(|p| format!("{}/", p)), Some(expected_uri));

let old_uri = Origin::parse("/a/b/c/").unwrap();
assert_eq!(old_uri.map_path(|p| format!("hi/{}", p)), None);

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

Returns the query part of this URI without the question mark, if there is any.

Examples

A URI with a query part:

use rocket::http::uri::Origin;

let uri = Origin::parse("/a/b/c?alphabet=true").unwrap();
assert_eq!(uri.query().unwrap(), "alphabet=true");

A URI without the query part:

use rocket::http::uri::Origin;

let uri = Origin::parse("/a/b/c").unwrap();
assert_eq!(uri.query(), None);

pub fn clear_query(&mut self)

Removes the query part of this URI, if there is any.

Example

use rocket::http::uri::Origin;

let mut uri = Origin::parse("/a/b/c?query=some").unwrap();
assert_eq!(uri.query().unwrap(), "query=some");

uri.clear_query();
assert_eq!(uri.query(), None);

pub fn path_segments(&self) -> Segments<'_>

Notable traits for Segments<'o>

impl<'o> Iterator for Segments<'o> type Item = &'o str;

Returns a (smart) iterator over the non-empty, percent-decoded segments of the path of this URI.

Example

use rocket::http::uri::Origin;

let uri = Origin::parse("/a%20b/b%2Fc/d//e?query=some").unwrap();
let path_segs: Vec<&str> = uri.path_segments().collect();
assert_eq!(path_segs, &["a b", "b/c", "d", "e"]);

pub fn query_segments(&self) -> QuerySegments<'_>

Notable traits for QuerySegments<'o>

impl<'o> Iterator for QuerySegments<'o> type Item = (&'o str, &'o str);

Returns a (smart) iterator over the non-empty, url-decoded (name, value) pairs of the query of this URI. If there is no query, the iterator is empty.

Example

use rocket::http::uri::Origin;

let uri = Origin::parse("/").unwrap();
let query_segs: Vec<_> = uri.query_segments().collect();
assert!(query_segs.is_empty());

let uri = Origin::parse("/foo/bar?a+b%2F=some+one%40gmail.com&&%26%3D2").unwrap();
let query_segs: Vec<_> = uri.query_segments().collect();
assert_eq!(query_segs, &[("a b/", "some one@gmail.com"), ("&=2", "")]);

pub fn raw_path_segments(&self) -> impl Iterator<Item = &RawStr>

Returns an iterator over the raw, undecoded segments of the path in this URI. Segments may be empty.

Example

use rocket::http::uri::Origin;

let uri = Origin::parse("/").unwrap();
let segments: Vec<_> = uri.raw_path_segments().collect();
assert!(segments.is_empty());

let uri = Origin::parse("//").unwrap();
let segments: Vec<_> = uri.raw_path_segments().collect();
assert_eq!(segments, &["", ""]);

let uri = Origin::parse("/a").unwrap();
let segments: Vec<_> = uri.raw_path_segments().collect();
assert_eq!(segments, &["a"]);

let uri = Origin::parse("/a//b///c/d?query&param").unwrap();
let segments: Vec<_> = uri.raw_path_segments().collect();
assert_eq!(segments, &["a", "", "b", "", "", "c", "d"]);

pub fn raw_query_segments(&self) -> impl Iterator<Item = &RawStr>

Returns an iterator over the non-empty, non-url-decoded (name, value) pairs of the query of this URI. If there is no query, the iterator is empty. Segments may be empty.

Example

use rocket::http::uri::Origin;

let uri = Origin::parse("/").unwrap();
assert!(uri.raw_query_segments().next().is_none());

let uri = Origin::parse("/?a=b&dog").unwrap();
let query_segs: Vec<_> = uri.raw_query_segments().collect();
assert_eq!(query_segs, &["a=b", "dog"]);

let uri = Origin::parse("/?&").unwrap();
let query_segs: Vec<_> = uri.raw_query_segments().collect();
assert_eq!(query_segs, &["", ""]);

let uri = Origin::parse("/foo/bar?a+b%2F=some+one%40gmail.com&&%26%3D2").unwrap();
let query_segs: Vec<_> = uri.raw_query_segments().collect();
assert_eq!(query_segs, &["a+b%2F=some+one%40gmail.com", "", "%26%3D2"]);

Trait Implementations

impl<'a> Clone for Origin<'a>

impl<'a> Debug for Origin<'a>

impl<'_> Display for Origin<'_>

impl<'a> From<Origin<'a>> for Uri<'a>

impl<'a, 'r> FromRequest<'a, 'r> for &'a Origin<'a>[src]

type Error = Infallible

The associated error to be returned if derivation fails.

impl<'_> IntoOwned for Origin<'_>

type Owned = Origin<'static>

The owned version of the type.

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

Auto Trait Implementations

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

impl<'a> Send for Origin<'a>

impl<'a> Sync for Origin<'a>

impl<'a> Unpin for Origin<'a>

impl<'a> UnwindSafe for Origin<'a>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> IntoCollection<T> for T

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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