Struct Uri
pub struct Uri { /* private fields */ }
The URI component of a request.
For HTTP 1, this is included as part of the request line. From Section 5.3, Request Target:
Once an inbound connection is obtained, the client sends an HTTP request message (Section 3) with a request-target derived from the target URI. There are four distinct formats for the request-target, depending on both the method being requested and whether the request is to a proxy.
request-target = origin-form / absolute-form / authority-form / asterisk-form
The URI is structured as follows:
abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1
|-| |-------------------------------||--------| |-------------------| |-----|
| | | | |
scheme authority path query fragment
For HTTP 2.0, the URI is encoded using pseudoheaders.
Examples
use Uri;
let uri = "/foo/bar?baz"..unwrap;
assert_eq!;
assert_eq!;
assert_eq!;
let uri = "https://www.rust-lang.org/install.html"..unwrap;
assert_eq!;
assert_eq!;
assert_eq!;
Implementations
impl Uri
fn builder() -> BuilderCreates a new builder-style object to manufacture a
Uri.This method returns an instance of
Builderwhich can be usd to create aUri.Examples
use Uri; let uri = builder .scheme .authority .path_and_query .build .unwrap;fn from_parts(src: Parts) -> Result<Uri, InvalidUriParts>Attempt to convert a
Partsinto aUri.Examples
Relative URI
# use *; let mut parts = default; parts.path_and_query = Some; let uri = from_parts.unwrap; assert_eq!; assert!; assert!;Absolute URI
# use *; let mut parts = default; parts.scheme = Some; parts.authority = Some; parts.path_and_query = Some; let uri = from_parts.unwrap; assert_eq!; assert_eq!; assert_eq!;Attempt to convert a
Bytesbuffer to aUri.This will try to prevent a copy if the type passed is the type used internally, and will copy the data if it is not.
fn from_static(src: &'static str) -> SelfConvert a
Urifrom a static string.This function will not perform any copying, however the string is checked to ensure that it is valid.
Panics
This function panics if the argument is an invalid URI.
Examples
# use Uri; let uri = from_static; assert_eq!; assert_eq!;fn into_parts(self) -> PartsConvert a
UriintoParts.Note
This is just an inherent method providing the same functionality as
let parts: Parts = uri.into()Examples
# use *; let uri: Uri = "/foo".parse.unwrap; let parts = uri.into_parts; assert_eq!; assert!; assert!;fn path_and_query(&self) -> Option<&PathAndQuery>Returns the path & query components of the Uri
fn path(&self) -> &strGet the path of this
Uri.Both relative and absolute URIs contain a path component, though it might be the empty string. The path component is case sensitive.
abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1 |--------| | pathIf the URI is
*then the path component is equal to*.Examples
A relative URI
# use Uri; let uri: Uri = "/hello/world".parse.unwrap; assert_eq!;An absolute URI
# use Uri; let uri: Uri = "http://example.org/hello/world".parse.unwrap; assert_eq!;fn scheme(&self) -> Option<&Scheme>Get the scheme of this
Uri.The URI scheme refers to a specification for assigning identifiers within that scheme. Only absolute URIs contain a scheme component, but not all absolute URIs will contain a scheme component. Although scheme names are case-insensitive, the canonical form is lowercase.
abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1 |-| | schemeExamples
Absolute URI
use ; let uri: Uri = "http://example.org/hello/world".parse.unwrap; assert_eq!;Relative URI
# use Uri; let uri: Uri = "/hello/world".parse.unwrap; assert!;fn scheme_str(&self) -> Option<&str>Get the scheme of this
Urias a&str.Example
# use Uri; let uri: Uri = "http://example.org/hello/world".parse.unwrap; assert_eq!;Get the authority of this
Uri.The authority is a hierarchical element for naming authority such that the remainder of the URI is delegated to that authority. For HTTP, the authority consists of the host and port. The host portion of the authority is case-insensitive.
The authority also includes a
username:passwordcomponent, however the use of this is deprecated and should be avoided.abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1 |-------------------------------| | authorityExamples
Absolute URI
# use Uri; let uri: Uri = "http://example.org:80/hello/world".parse.unwrap; assert_eq!;Relative URI
# use Uri; let uri: Uri = "/hello/world".parse.unwrap; assert!;fn host(&self) -> Option<&str>Get the host of this
Uri.The host subcomponent of authority is identified by an IP literal encapsulated within square brackets, an IPv4 address in dotted- decimal form, or a registered name. The host subcomponent is case-insensitive.
abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1 |---------| | hostExamples
Absolute URI
# use Uri; let uri: Uri = "http://example.org:80/hello/world".parse.unwrap; assert_eq!;Relative URI
# use Uri; let uri: Uri = "/hello/world".parse.unwrap; assert!;fn port(&self) -> Option<Port<&str>>Get the port part of this
Uri.The port subcomponent of authority is designated by an optional port number following the host and delimited from it by a single colon (":") character. It can be turned into a decimal port number with the
as_u16method or as astrwith theas_strmethod.abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1 |-| | portExamples
Absolute URI with port
# use Uri; let uri: Uri = "http://example.org:80/hello/world".parse.unwrap; let port = uri.port.unwrap; assert_eq!;Absolute URI without port
# use Uri; let uri: Uri = "http://example.org/hello/world".parse.unwrap; assert!;Relative URI
# use Uri; let uri: Uri = "/hello/world".parse.unwrap; assert!;fn port_u16(&self) -> Option<u16>Get the port of this
Urias au16.Example
# use ; let uri: Uri = "http://example.org:80/hello/world".parse.unwrap; assert_eq!;fn query(&self) -> Option<&str>Get the query string of this
Uri, starting after the?.The query component contains non-hierarchical data that, along with data in the path component, serves to identify a resource within the scope of the URI's scheme and naming authority (if any). The query component is indicated by the first question mark ("?") character and terminated by a number sign ("#") character or by the end of the URI.
abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1 |-------------------| | queryExamples
Absolute URI
# use Uri; let uri: Uri = "http://example.org/hello/world?key=value".parse.unwrap; assert_eq!;Relative URI with a query string component
# use Uri; let uri: Uri = "/hello/world?key=value&foo=bar".parse.unwrap; assert_eq!;Relative URI without a query string component
# use Uri; let uri: Uri = "/hello/world".parse.unwrap; assert!;
Trait Implementations
impl Clone for Uri
fn clone(&self) -> Self
impl Debug for Uri
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl Default for Uri
fn default() -> Uri
impl Display for Uri
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl Eq for Uri
impl From<Authority> for Uri
fn from(authority: Authority) -> Self
impl From<PathAndQuery> for Uri
fn from(path_and_query: PathAndQuery) -> Self
impl FromStr for Uri
type Err = InvalidUri;fn from_str(s: &str) -> Result<Uri, InvalidUri>
impl Hash for Uri
fn hash<H>(&self, state: &mut H) where H: Hasher,
impl PartialEq for Uri
fn eq(&self, other: &Uri) -> bool
impl PartialEq<&str> for Uri
fn eq(&self, other: &&str) -> bool
impl PartialEq<str> for Uri
fn eq(&self, other: &str) -> bool
impl TryFrom<&String> for Uri
type Error = InvalidUri;fn try_from(t: &String) -> Result<Self, Self::Error>
impl TryFrom<&Uri> for Uri
type Error = Error;fn try_from(src: &Uri) -> Result<Self, Self::Error>
impl TryFrom<&[u8]> for Uri
type Error = InvalidUri;fn try_from(t: &[u8]) -> Result<Self, Self::Error>
impl TryFrom<&str> for Uri
type Error = InvalidUri;fn try_from(t: &str) -> Result<Self, Self::Error>
impl TryFrom<Parts> for Uri
type Error = InvalidUriParts;fn try_from(src: Parts) -> Result<Self, Self::Error>
impl TryFrom<String> for Uri
type Error = InvalidUri;fn try_from(t: String) -> Result<Self, Self::Error>
impl TryFrom<Vec<u8>> for Uri
type Error = InvalidUri;fn try_from(vec: Vec<u8>) -> Result<Self, Self::Error>
Auto Trait Implementations
impl !Freeze for Uri
impl RefUnwindSafe for Uri
impl Send for Uri
impl Sync for Uri
impl Unpin for Uri
impl UnsafeUnpin for Uri
impl UnwindSafe for Uri
Blanket Implementations
impl<T> Any for Uri
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for Uri
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for Uri
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> CloneToUninit for Uri
where
T: Clone,
unsafe fn clone_to_uninit(&self, dest: *mut u8)
impl<T> From<T> for Uri
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for Uri
where
T: Clone,
type Owned = T;fn to_owned(&self) -> Tfn clone_into(&self, target: &mut T)
impl<T> ToString for Uri
where
T: Display + ?Sized,
fn to_string(&self) -> String
impl<T, U> Into<U> for Uri
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for Uri
where
U: Into<T>,
type Error = never;fn try_from(value: U) -> Result<T, never>
impl<T, U> TryInto<U> for Uri
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>