aboutsummaryrefslogtreecommitdiff
path: root/src/quirks.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/quirks.rs')
-rw-r--r--src/quirks.rs59
1 files changed, 49 insertions, 10 deletions
diff --git a/src/quirks.rs b/src/quirks.rs
index 0dbc6eb..0674ebb 100644
--- a/src/quirks.rs
+++ b/src/quirks.rs
@@ -14,6 +14,49 @@
use crate::parser::{default_port, Context, Input, Parser, SchemeType};
use crate::{Host, ParseError, Position, Url};
+/// Internal components / offsets of a URL.
+///
+/// https://user@pass:example.com:1234/foo/bar?baz#quux
+/// | | | | ^^^^| | |
+/// | | | | | | | `----- fragment_start
+/// | | | | | | `--------- query_start
+/// | | | | | `----------------- path_start
+/// | | | | `--------------------- port
+/// | | | `----------------------- host_end
+/// | | `---------------------------------- host_start
+/// | `--------------------------------------- username_end
+/// `---------------------------------------------- scheme_end
+#[derive(Copy, Clone)]
+#[cfg(feature = "expose_internals")]
+pub struct InternalComponents {
+ pub scheme_end: u32,
+ pub username_end: u32,
+ pub host_start: u32,
+ pub host_end: u32,
+ pub port: Option<u16>,
+ pub path_start: u32,
+ pub query_start: Option<u32>,
+ pub fragment_start: Option<u32>,
+}
+
+/// Internal component / parsed offsets of the URL.
+///
+/// This can be useful for implementing efficient serialization
+/// for the URL.
+#[cfg(feature = "expose_internals")]
+pub fn internal_components(url: &Url) -> InternalComponents {
+ InternalComponents {
+ scheme_end: url.scheme_end,
+ username_end: url.username_end,
+ host_start: url.host_start,
+ host_end: url.host_end,
+ port: url.port,
+ path_start: url.path_start,
+ query_start: url.query_start,
+ fragment_start: url.fragment_start,
+ }
+}
+
/// https://url.spec.whatwg.org/#dom-url-domaintoascii
pub fn domain_to_ascii(domain: &str) -> String {
match Host::parse(domain) {
@@ -138,14 +181,10 @@ pub fn set_host(url: &mut Url, new_host: &str) -> Result<(), ()> {
}
}
// Make sure we won't set an empty host to a url with a username or a port
- if host == Host::Domain("".to_string()) {
- if !username(&url).is_empty() {
- return Err(());
- } else if let Some(Some(_)) = opt_port {
- return Err(());
- } else if url.port().is_some() {
- return Err(());
- }
+ if host == Host::Domain("".to_string())
+ && (!username(url).is_empty() || matches!(opt_port, Some(Some(_))) || url.port().is_some())
+ {
+ return Err(());
}
url.set_host_internal(host, opt_port);
Ok(())
@@ -177,10 +216,10 @@ pub fn set_hostname(url: &mut Url, new_hostname: &str) -> Result<(), ()> {
// Empty host on special not file url
if SchemeType::from(url.scheme()) == SchemeType::SpecialNotFile
// Port with an empty host
- ||!port(&url).is_empty()
+ ||!port(url).is_empty()
// Empty host that includes credentials
|| !url.username().is_empty()
- || !url.password().unwrap_or(&"").is_empty()
+ || !url.password().unwrap_or("").is_empty()
{
return Err(());
}