Skip to main content

HttpUrl

Struct HttpUrl 

Source
pub struct HttpUrl(/* private fields */);
Expand description

An immutable HTTP or HTTPS URL.

HttpUrl is a thin newtype around url::Url that enforces the “scheme is http or https” invariant at construction time. Parsing, percent-encoding, normalization and all component access are provided by the url crate — reach them through HttpUrl::as_url or HttpUrl::into_url. This crate focuses on the HttpUrlBuilder API for constructing URLs programmatically, plus a typed Scheme accessor that reflects the http/https invariant.

§Example

use http_url::HttpUrl;

let url = HttpUrl::parse("https://example.com/path?a=1#frag").unwrap();
assert_eq!(url.scheme(), "https");
assert_eq!(url.as_url().host_str(), Some("example.com"));
assert_eq!(url.as_url().path(), "/path");
assert_eq!(url.as_url().query(), Some("a=1"));
assert_eq!(url.as_url().fragment(), Some("frag"));

Implementations§

Source§

impl HttpUrl

Source

pub fn parse(url: &str) -> Result<Self>

Parse a URL string into an HttpUrl.

The URL must have an http:// or https:// scheme; all other parsing behavior follows the WHATWG URL Standard as implemented by the url crate.

Source

pub fn from_url(url: Url) -> Result<Self>

Wrap an existing url::Url, returning an error if its scheme is not http or https.

Source

pub fn as_url(&self) -> &Url

Borrow the underlying url::Url, through which all URL components (host, path, query, fragment, …) are accessible.

Source

pub fn into_url(self) -> Url

Consume this HttpUrl and return the underlying url::Url.

Source

pub fn builder() -> HttpUrlBuilder

Create a new HttpUrlBuilder.

Source

pub fn new_builder(&self) -> HttpUrlBuilder

Create a builder pre-populated with this URL’s components, so it can be modified and rebuilt.

§Example
use http_url::HttpUrl;

let url = HttpUrl::parse("https://example.com/a/b?q=1").unwrap();
let url2 = url.new_builder()
    .add_path_segment("c")
    .build()
    .unwrap();
assert_eq!(url2.as_url().path(), "/a/b/c");
Source

pub fn scheme(&self) -> Scheme

Returns the URL scheme as a typed Scheme.

This is the typed mirror of url::Url::scheme; for a valid HttpUrl it is always http or https.

Source

pub fn top_private_domain(&self) -> Option<String>

Compute the top private domain (eTLD+1) of this URL’s host.

Returns None if the host is an IP address or does not have enough domain labels. This is a simplified implementation that assumes the last two labels form the private domain; a full implementation would consult the Public Suffix List.

Trait Implementations§

Source§

impl Clone for HttpUrl

Source§

fn clone(&self) -> HttpUrl

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 HttpUrl

Source§

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

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

impl Display for HttpUrl

Source§

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

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

impl Eq for HttpUrl

Source§

impl From<HttpUrl> for Url

Source§

fn from(http: HttpUrl) -> Self

Converts to this type from the input type.
Source§

impl FromStr for HttpUrl

Source§

type Err = HttpUrlError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for HttpUrl

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for HttpUrl

Source§

fn eq(&self, other: &HttpUrl) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for HttpUrl

Source§

impl TryFrom<Url> for HttpUrl

Source§

type Error = HttpUrlError

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

fn try_from(url: Url) -> Result<Self>

Performs the conversion.

Auto Trait Implementations§

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> 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.