Skip to main content

Uri

Struct Uri 

Source
pub struct Uri { /* private fields */ }
Expand description

A URI.

§Equality

Uri deliberately does not implement PartialEq as RFC 3261 equivalence. That relation is not transitive — the RFC says so in §19.1.4, and gives the example that sip:carol@chicago.com is equivalent to both sip:carol@chicago.com;security=on and sip:carol@chicago.com;security=off, which are not equivalent to each other. A non-transitive PartialEq breaks HashMap, sorting, and every reader’s assumptions.

So: Uri::equivalent implements the RFC relation and is what protocol logic must use.

Implementations§

Source§

impl Uri

Source

pub fn parse(raw: Bytes) -> Result<Self, UriError>

Parse a URI.

The input must be the URI alone: any enclosing <> and surrounding whitespace belong to the header grammar and must be stripped by the caller.

Source

pub fn sip(host: Host) -> Self

Build a sip: or sips: URI.

Source

pub fn scheme(&self) -> &Scheme

The scheme.

Source

pub fn user(&self) -> Option<&[u8]>

The user part, still percent-encoded, or None for a URI with no userinfo or a scheme sipx does not model.

Source

pub fn password(&self) -> Option<&[u8]>

The password, still percent-encoded.

Present in the grammar and therefore parsed; RFC 3261 §19.1.1 advises against using it, and sipx never puts one in a URI it builds.

Source

pub fn decoded_user(&self) -> Option<Vec<u8>>

The user part with its percent escapes decoded.

Yields bytes, not a string, and that is not an oversight: RFC 4475 §3.1.1.4 has a registration whose user part is null-%00-null, and sip:%C3%A9@host decodes to non-ASCII. Either would have to panic or be lossily replaced to become a str.

Returns None if there is no user part or an escape is malformed.

Source

pub fn replace_user(&mut self, user: impl Into<Bytes>) -> Result<bool, UriError>

Replace an existing, already percent-encoded user part of a SIP or SIPS URI.

Returns Ok(false) without touching the URI when its scheme is not SIP or SIPS or it has no user part. For a parsed URI, a valid replacement changes only the retained user span: scheme spelling, password, host spelling, delimiters, port, parameters and URI headers stay byte-identical. The old verbatim form is invalidated rather than replayed stale. A URI whose verbatim form was already discarded serializes canonically from its structured parts.

§Errors

UriError::PercentEscape reports a malformed % HEX HEX sequence. UriError::User reports an empty value or a byte outside RFC 3261 §25.1’s user production. Either error leaves the URI unchanged.

Source

pub fn replace_tel_subscriber( &mut self, subscriber: impl Into<Bytes>, ) -> Result<bool, UriError>

Replace the telephone-subscriber of a parsed RFC 3966 tel: URI.

Returns Ok(false) without touching the URI for every other scheme. A successful replacement splices only the parser-retained subscriber span, so mixed-case scheme spelling and the complete optional parameter tail stay byte-identical. This validates the global/local subscriber production but deliberately does not interpret parameters such as phone-context.

§Errors

UriError::TelephoneSubscriber reports an empty value or one outside RFC 3966’s global-number-digits and local-number-digits productions. The error is atomic.

Source

pub fn host(&self) -> Option<&Host>

The host.

Source

pub fn port(&self) -> Option<u16>

The port, if the URI states one.

A URI without a port is not the same as one naming the default port; see Uri::equivalent.

Source

pub fn params(&self) -> Option<&Params>

The URI parameters — the ;name=value list.

Source

pub fn headers(&self) -> Option<&Params>

The URI headers — the ?name=value&… list.

Source

pub fn opaque(&self) -> Option<&[u8]>

Everything after the scheme, for a scheme sipx does not model.

Source

pub fn tel_parts(&self) -> Option<TelUriParts<'_>>

Split an RFC 3966 tel: URI into exact subscriber and parameter-tail spans.

Returns None for every other scheme. This is a syntax view only: it preserves visual separators, parameter spelling and order and performs no normalization or validation.

Source

pub fn transport(&self) -> Option<&[u8]>

The value of the transport parameter.

Source

pub fn selected_transport(&self) -> Result<UriTransport, UriTransportError>

Select the effective transport without resolving the URI’s host.

Source

pub fn push_param(&mut self, param: Param)

Add a URI parameter.

Appended, not replaced: RFC 3261 §19.1.1 forbids a repeated uri-parameter, so a caller re-setting one of its own parameters wants Uri::remove_param first — see Params.

Source

pub fn push_header(&mut self, header: Param) -> bool

Add a URI header component and report whether this URI can carry one.

SIP and SIPS URIs have a ?name=value component. Opaque schemes, including tel, do not; returning false lets History-Info follow RFC 7044 §10.2 without pretending a reason was embedded in a URI whose grammar has nowhere to put it.

Source

pub fn remove_header(&mut self, name: &str) -> bool

Remove every URI header component with this name.

Source

pub fn remove_param(&mut self, name: &str) -> bool

Remove a URI parameter, and say whether one was there.

Names match the way §19.1.4 compares them, so %74ransport is transport.

Source

pub fn has_headers(&self) -> bool

Whether this URI carries any header components.

A Request-URI must not (RFC 3261 §19.1.1); validation uses this.

Source

pub fn write_to(&self, out: &mut Vec<u8>)

Serialize.

A parsed, unmodified URI is written back exactly as it arrived.

Source

pub fn to_bytes(&self) -> Bytes

Serialize to bytes.

Source

pub fn equivalent(&self, other: &Self) -> bool

Whether two URIs are equivalent under RFC 3261 §19.1.4.

Note that this relation is not transitive; see the type-level documentation.

Trait Implementations§

Source§

impl Clone for Uri

Source§

fn clone(&self) -> Uri

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Uri

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Uri

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.