pub struct Uri { /* private fields */ }
Expand description
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 http::Uri;
let uri = "/foo/bar?baz".parse::<Uri>().unwrap();
assert_eq!(uri.path(), "/foo/bar");
assert_eq!(uri.query(), Some("baz"));
assert_eq!(uri.host(), None);
let uri = "https://www.rust-lang.org/install.html".parse::<Uri>().unwrap();
assert_eq!(uri.scheme_str(), Some("https"));
assert_eq!(uri.host(), Some("www.rust-lang.org"));
assert_eq!(uri.path(), "/install.html");
Implementations§
§impl Uri
impl Uri
pub fn builder() -> Builder
pub fn builder() -> Builder
Creates a new builder-style object to manufacture a Uri
.
This method returns an instance of Builder
which can be usd to
create a Uri
.
§Examples
use http::Uri;
let uri = Uri::builder()
.scheme("https")
.authority("hyper.rs")
.path_and_query("/")
.build()
.unwrap();
pub fn from_parts(src: Parts) -> Result<Uri, InvalidUriParts>
pub fn from_parts(src: Parts) -> Result<Uri, InvalidUriParts>
Attempt to convert a Parts
into a Uri
.
§Examples
Relative URI
let mut parts = Parts::default();
parts.path_and_query = Some("/foo".parse().unwrap());
let uri = Uri::from_parts(parts).unwrap();
assert_eq!(uri.path(), "/foo");
assert!(uri.scheme().is_none());
assert!(uri.authority().is_none());
Absolute URI
let mut parts = Parts::default();
parts.scheme = Some("http".parse().unwrap());
parts.authority = Some("foo.com".parse().unwrap());
parts.path_and_query = Some("/foo".parse().unwrap());
let uri = Uri::from_parts(parts).unwrap();
assert_eq!(uri.scheme().unwrap().as_str(), "http");
assert_eq!(uri.authority().unwrap(), "foo.com");
assert_eq!(uri.path(), "/foo");
Attempt to convert a Bytes
buffer to a Uri
.
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.
pub fn from_static(src: &'static str) -> Uri
pub fn from_static(src: &'static str) -> Uri
Convert a Uri
from 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
let uri = Uri::from_static("http://example.com/foo");
assert_eq!(uri.host().unwrap(), "example.com");
assert_eq!(uri.path(), "/foo");
pub fn into_parts(self) -> Parts
pub fn into_parts(self) -> Parts
Convert a Uri
into Parts
.
§Note
This is just an inherent method providing the same functionality as
let parts: Parts = uri.into()
§Examples
let uri: Uri = "/foo".parse().unwrap();
let parts = uri.into_parts();
assert_eq!(parts.path_and_query.unwrap(), "/foo");
assert!(parts.scheme.is_none());
assert!(parts.authority.is_none());
pub fn path_and_query(&self) -> Option<&PathAndQuery>
pub fn path_and_query(&self) -> Option<&PathAndQuery>
Returns the path & query components of the Uri
pub fn path(&self) -> &str
pub fn path(&self) -> &str
Get 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
|--------|
|
path
If the URI is *
then the path component is equal to *
.
§Examples
A relative URI
let uri: Uri = "/hello/world".parse().unwrap();
assert_eq!(uri.path(), "/hello/world");
An absolute URI
let uri: Uri = "http://example.org/hello/world".parse().unwrap();
assert_eq!(uri.path(), "/hello/world");
pub fn scheme(&self) -> Option<&Scheme>
pub 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
|-|
|
scheme
§Examples
Absolute URI
use http::uri::{Scheme, Uri};
let uri: Uri = "http://example.org/hello/world".parse().unwrap();
assert_eq!(uri.scheme(), Some(&Scheme::HTTP));
Relative URI
let uri: Uri = "/hello/world".parse().unwrap();
assert!(uri.scheme().is_none());
pub fn scheme_str(&self) -> Option<&str>
pub fn scheme_str(&self) -> Option<&str>
Get the scheme of this Uri
as a &str
.
§Example
let uri: Uri = "http://example.org/hello/world".parse().unwrap();
assert_eq!(uri.scheme_str(), Some("http"));
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:password
component, however
the use of this is deprecated and should be avoided.
abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1
|-------------------------------|
|
authority
§Examples
Absolute URI
let uri: Uri = "http://example.org:80/hello/world".parse().unwrap();
assert_eq!(uri.authority().map(|a| a.as_str()), Some("example.org:80"));
Relative URI
let uri: Uri = "/hello/world".parse().unwrap();
assert!(uri.authority().is_none());
pub fn host(&self) -> Option<&str>
pub 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
|---------|
|
host
§Examples
Absolute URI
let uri: Uri = "http://example.org:80/hello/world".parse().unwrap();
assert_eq!(uri.host(), Some("example.org"));
Relative URI
let uri: Uri = "/hello/world".parse().unwrap();
assert!(uri.host().is_none());
pub fn port(&self) -> Option<Port<&str>>
pub 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_u16
method or as a str
with the as_str
method.
abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1
|-|
|
port
§Examples
Absolute URI with port
let uri: Uri = "http://example.org:80/hello/world".parse().unwrap();
let port = uri.port().unwrap();
assert_eq!(port.as_u16(), 80);
Absolute URI without port
let uri: Uri = "http://example.org/hello/world".parse().unwrap();
assert!(uri.port().is_none());
Relative URI
let uri: Uri = "/hello/world".parse().unwrap();
assert!(uri.port().is_none());
pub fn port_u16(&self) -> Option<u16>
pub fn port_u16(&self) -> Option<u16>
Get the port of this Uri
as a u16
.
§Example
let uri: Uri = "http://example.org:80/hello/world".parse().unwrap();
assert_eq!(uri.port_u16(), Some(80));
pub fn query(&self) -> Option<&str>
pub 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
|-------------------|
|
query
§Examples
Absolute URI
let uri: Uri = "http://example.org/hello/world?key=value".parse().unwrap();
assert_eq!(uri.query(), Some("key=value"));
Relative URI with a query string component
let uri: Uri = "/hello/world?key=value&foo=bar".parse().unwrap();
assert_eq!(uri.query(), Some("key=value&foo=bar"));
Relative URI without a query string component
let uri: Uri = "/hello/world".parse().unwrap();
assert!(uri.query().is_none());
Trait Implementations§
§impl From<PathAndQuery> for Uri
impl From<PathAndQuery> for Uri
Convert a PathAndQuery
into a Uri
.
§fn from(path_and_query: PathAndQuery) -> Uri
fn from(path_and_query: PathAndQuery) -> Uri
impl Eq for Uri
impl IntoUrl for Uri
Auto Trait Implementations§
impl !Freeze for Uri
impl RefUnwindSafe for Uri
impl Send for Uri
impl Sync for Uri
impl Unpin for Uri
impl UnwindSafe for Uri
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)§impl<T> Conv for T
impl<T> Conv for T
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§fn and<S, P, B, E>(self, other: P) -> And<T, P>
fn and<S, P, B, E>(self, other: P) -> And<T, P>
Policy
that returns Action::Follow
only if self
and other
return
Action::Follow
. Read more§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string
, but without panic on OOM.