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
impl HttpUrl
Sourcepub fn parse(url: &str) -> Result<Self>
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.
Sourcepub fn from_url(url: Url) -> Result<Self>
pub fn from_url(url: Url) -> Result<Self>
Wrap an existing url::Url, returning an error if its scheme is not
http or https.
Sourcepub fn as_url(&self) -> &Url
pub fn as_url(&self) -> &Url
Borrow the underlying url::Url, through which all URL components
(host, path, query, fragment, …) are accessible.
Sourcepub fn builder() -> HttpUrlBuilder
pub fn builder() -> HttpUrlBuilder
Create a new HttpUrlBuilder.
Sourcepub fn new_builder(&self) -> HttpUrlBuilder
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");Sourcepub fn scheme(&self) -> Scheme
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.
Sourcepub fn top_private_domain(&self) -> Option<String>
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.