[]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",
"/some%20thing"

By contrast, the following are valid but abnormal URIs:

"//",               // one empty segment
"/a/b/",            // trailing empty segment
"/a/ab//c//d"       // two empty segments

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

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

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

Methods

impl<'a> Origin<'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(), Some("query"));

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

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

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 abnormal = Origin::parse("/a/b/c//d").unwrap();
assert!(!abnormal.is_normalized());

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.to_normalized();
assert!(normalized.is_normalized());
assert_eq!(normalized, Origin::parse("/a/b/c/d").unwrap());

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

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(), Some("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);

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(), Some("query=some"));

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

Important traits for Segments<'a>

Returns an iterator over the segments of the path in this URI. Skips empty segments.

Examples

A valid URI with only non-empty segments:

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

let uri = Origin::parse("/a/b/c?a=true").unwrap();
for (i, segment) in uri.segments().enumerate() {
    match i {
        0 => assert_eq!(segment, "a"),
        1 => assert_eq!(segment, "b"),
        2 => assert_eq!(segment, "c"),
        _ => unreachable!("only three segments")
    }
}

A URI with empty segments:

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

let uri = Origin::parse("///a//b///c////d?query&param").unwrap();
for (i, segment) in uri.segments().enumerate() {
    match i {
        0 => assert_eq!(segment, "a"),
        1 => assert_eq!(segment, "b"),
        2 => assert_eq!(segment, "c"),
        3 => assert_eq!(segment, "d"),
        _ => unreachable!("only four segments")
    }
}

Returns the number of segments in the URI. Empty segments, which are invalid according to RFC#3986, are not counted.

The segment count is cached after the first invocation. As a result, this function is O(1) after the first invocation, and O(n) before.

Examples

A valid URI with only non-empty segments:

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

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

A URI with empty segments:

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

let uri = Origin::parse("/a/b//c/d///e").unwrap();
assert_eq!(uri.segment_count(), 5);

Trait Implementations

impl<'a> IntoOwned for Origin<'a>

The owned version of the type.

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

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

This method tests for !=.

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

Performs copy-assignment from source. Read more

impl<'a> Display for Origin<'a>

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

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

The associated error to be returned if derivation fails.

Auto Trait Implementations

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

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

Blanket Implementations

impl<T> From for T
[src]

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

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

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

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

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

The type returned in the event of a conversion error.

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

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

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

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

The type returned in the event of a conversion error.

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

impl<T> Typeable for T where
    T: Any

Get the TypeId of this object.

impl<T> IntoCollection for T

impl<T, I> AsResult for T where
    I: Input,