pub struct URI<'a> { /* fields omitted */ }
Borrowed string type for absolute URIs.
Constructs a new URI from a given string. The URI is assumed to be an
absolute, well formed URI.
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.
A valid URI with only non-empty segments:
use rocket::http::uri::URI;
let uri = URI::new("/a/b/c");
assert_eq!(uri.segment_count(), 3);
A URI with empty segments:
use rocket::http::uri::URI;
let uri = URI::new("/a/b//c/d///e");
assert_eq!(uri.segment_count(), 5);
Returns an iterator over the segments of the path in this URI. Skips
empty segments.
A valid URI with only non-empty segments:
use rocket::http::uri::URI;
let uri = URI::new("/a/b/c?a=true#done");
for (i, segment) in uri.segments().enumerate() {
match i {
0 => assert_eq!(segment, "a"),
1 => assert_eq!(segment, "b"),
2 => assert_eq!(segment, "c"),
_ => panic!("only three segments")
}
}
A URI with empty segments:
use rocket::http::uri::URI;
let uri = URI::new("///a//b///c////d?#");
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"),
_ => panic!("only four segments")
}
}
Returns the path part of this URI.
A URI with only a path:
use rocket::http::uri::URI;
let uri = URI::new("/a/b/c");
assert_eq!(uri.path(), "/a/b/c");
A URI with other components:
use rocket::http::uri::URI;
let uri = URI::new("/a/b/c?name=bob#done");
assert_eq!(uri.path(), "/a/b/c");
Returns the query part of this URI without the question mark, if there is
any.
A URI with a query part:
use rocket::http::uri::URI;
let uri = URI::new("/a/b/c?alphabet=true");
assert_eq!(uri.query(), Some("alphabet=true"));
A URI without the query part:
use rocket::http::uri::URI;
let uri = URI::new("/a/b/c");
assert_eq!(uri.query(), None);
Returns the fargment part of this URI without the hash mark, if there is
any.
A URI with a fragment part:
use rocket::http::uri::URI;
let uri = URI::new("/a?alphabet=true#end");
assert_eq!(uri.fragment(), Some("end"));
A URI without the fragment part:
use rocket::http::uri::URI;
let uri = URI::new("/a?query=true");
assert_eq!(uri.fragment(), None);
Returns a URL-decoded version of the string. If the percent encoded
values are not valid UTF-8, an Err
is returned.
use rocket::http::uri::URI;
let uri = URI::new("/Hello%2C%20world%21");
let decoded_path = URI::percent_decode(uri.path().as_bytes()).expect("decoded");
assert_eq!(decoded_path, "/Hello, world!");
Returns a URL-decoded version of the path. Any invalid UTF-8
percent-encoded byte sequences will be replaced � U+FFFD, the
replacement character.
use rocket::http::uri::URI;
let uri = URI::new("/Hello%2C%20world%21");
let decoded_path = URI::percent_decode_lossy(uri.path().as_bytes());
assert_eq!(decoded_path, "/Hello, world!");
Returns a URL-encoded version of the string. Any characters outside of
visible ASCII-range are encoded as well as ' ', '"', '#', '<', '>', '`',
'?', '{', '}', '%', and '/'.
use rocket::http::uri::URI;
let encoded = URI::percent_encode("hello?a=<b>hi</b>");
assert_eq!(encoded, "hello%3Fa=%3Cb%3Ehi%3C%2Fb%3E");
Returns the inner string of this URI.
The returned string is in raw form. It contains empty segments. If you'd
like a string without empty segments, use to_string
instead.
use rocket::http::uri::URI;
let uri = URI::new("/a/b///c/d/e//f?name=Mike#end");
assert_eq!(uri.as_str(), "/a/b///c/d/e//f?name=Mike#end");
type Error = ()
The associated error to be returned if derivation fails.
Derives an instance of Self
from the incoming request metadata. Read more
Performs copy-assignment from source
. Read more
This method tests for self
and other
values to be equal, and is used by ==
. Read more
This method tests for !=
.
Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more
Converts the given value to a String
. Read more
type Owned = T
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
type Error = !
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static
type Error = <U as TryFrom<T>>::Error
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Mutably borrows from an owned value. Read more
impl<T> Typeable for T where T: Any, | |
Get the TypeId
of this object.