fluent_uri/component.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
//! URI/IRI components.
use crate::{
encoding::{
encoder::{IRegName, IUserinfo, Port, RegName, Userinfo},
table, EStr, Encoder,
},
internal::{AuthMeta, HostMeta},
};
use core::{marker::PhantomData, num::ParseIntError};
use ref_cast::{ref_cast_custom, RefCastCustom};
#[cfg(feature = "net")]
use crate::net::{Ipv4Addr, Ipv6Addr};
#[cfg(all(feature = "net", feature = "std"))]
use std::{
io,
net::{SocketAddr, ToSocketAddrs},
};
/// An authority component for IRI.
pub type IAuthority<'a> = Authority<'a, IUserinfo, IRegName>;
/// A parsed host component for IRI.
pub type IHost<'a> = Host<'a, IRegName>;
/// A [scheme] component.
///
/// [scheme]: https://datatracker.ietf.org/doc/html/rfc3986#section-3.1
///
/// # Comparison
///
/// `Scheme`s are compared case-insensitively. You should do a case-insensitive
/// comparison if the scheme specification allows both letter cases in the scheme name.
///
/// # Examples
///
/// ```
/// use fluent_uri::{component::Scheme, Uri};
///
/// const SCHEME_HTTP: &Scheme = Scheme::new_or_panic("http");
///
/// let scheme = Uri::parse("HTTP://EXAMPLE.COM/")?.scheme();
///
/// // Case-insensitive comparison.
/// assert_eq!(scheme, SCHEME_HTTP);
/// // Case-sensitive comparison.
/// assert_eq!(scheme.as_str(), "HTTP");
/// # Ok::<_, fluent_uri::error::ParseError>(())
/// ```
#[derive(RefCastCustom)]
#[repr(transparent)]
pub struct Scheme {
inner: str,
}
impl Scheme {
#[ref_cast_custom]
#[inline]
pub(crate) const fn new_validated(scheme: &str) -> &Scheme;
/// Converts a string slice to `&Scheme`.
///
/// # Panics
///
/// Panics if the string is not a valid scheme name according to
/// [Section 3.1 of RFC 3986][scheme]. For a non-panicking variant,
/// use [`new`](Self::new).
///
/// [scheme]: https://datatracker.ietf.org/doc/html/rfc3986#section-3.1
#[inline]
#[must_use]
pub const fn new_or_panic(s: &str) -> &Scheme {
match Self::new(s) {
Some(scheme) => scheme,
None => panic!("invalid scheme"),
}
}
/// Converts a string slice to `&Scheme`, returning `None` if the conversion fails.
#[inline]
#[must_use]
pub const fn new(s: &str) -> Option<&Scheme> {
if matches!(s.as_bytes(), [first, rem @ ..]
if first.is_ascii_alphabetic() && table::SCHEME.validate(rem))
{
Some(Scheme::new_validated(s))
} else {
None
}
}
/// Returns the scheme component as a string slice.
///
/// # Examples
///
/// ```
/// use fluent_uri::Uri;
///
/// let uri = Uri::parse("http://example.com/")?;
/// assert_eq!(uri.scheme().as_str(), "http");
/// let uri = Uri::parse("HTTP://EXAMPLE.COM/")?;
/// assert_eq!(uri.scheme().as_str(), "HTTP");
/// # Ok::<_, fluent_uri::error::ParseError>(())
/// ```
#[inline]
#[must_use]
pub fn as_str(&self) -> &str {
&self.inner
}
}
impl PartialEq for Scheme {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.inner.eq_ignore_ascii_case(&other.inner)
}
}
impl Eq for Scheme {}
#[derive(Clone, Copy)]
struct AuthorityInner<'a> {
val: &'a str,
meta: AuthMeta,
}
impl<'a> AuthorityInner<'a> {
fn userinfo(&self) -> Option<&'a EStr<IUserinfo>> {
let host_start = self.meta.host_bounds.0;
(host_start != 0).then(|| EStr::new_validated(&self.val[..host_start - 1]))
}
fn host(&self) -> &'a str {
let (start, end) = self.meta.host_bounds;
&self.val[start..end]
}
fn port(&self) -> Option<&'a EStr<Port>> {
let host_end = self.meta.host_bounds.1;
(host_end != self.val.len()).then(|| EStr::new_validated(&self.val[host_end + 1..]))
}
fn port_to_u16(&self) -> Result<Option<u16>, ParseIntError> {
self.port()
.filter(|s| !s.is_empty())
.map(|s| s.as_str().parse())
.transpose()
}
#[cfg(all(feature = "net", feature = "std"))]
fn socket_addrs(&self, default_port: u16) -> io::Result<impl Iterator<Item = SocketAddr>> {
use std::vec;
let port = self
.port_to_u16()
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid port value"))?
.unwrap_or(default_port);
match self.meta.host_meta {
HostMeta::Ipv4(addr) => Ok(vec![(addr, port).into()].into_iter()),
HostMeta::Ipv6(addr) => Ok(vec![(addr, port).into()].into_iter()),
HostMeta::IpvFuture => Err(io::Error::new(
io::ErrorKind::InvalidInput,
"address mechanism not supported",
)),
HostMeta::RegName => {
let name = EStr::<IRegName>::new_validated(self.host());
let name = name.decode().into_string().map_err(|_| {
io::Error::new(
io::ErrorKind::InvalidInput,
"registered name does not decode to valid UTF-8",
)
})?;
(&name[..], port).to_socket_addrs()
}
}
}
}
/// An [authority] component.
///
/// [authority]: https://datatracker.ietf.org/doc/html/rfc3986#section-3.2
#[derive(Clone, Copy)]
pub struct Authority<'a, UserinfoE = Userinfo, RegNameE = RegName> {
inner: AuthorityInner<'a>,
_marker: PhantomData<(UserinfoE, RegNameE)>,
}
impl<'a, T, U> Authority<'a, T, U> {
pub(crate) fn cast<V, W>(self) -> Authority<'a, V, W> {
Authority {
inner: self.inner,
_marker: PhantomData,
}
}
}
impl<'a, UserinfoE: Encoder, RegNameE: Encoder> Authority<'a, UserinfoE, RegNameE> {
pub(crate) const fn new(val: &'a str, meta: AuthMeta) -> Self {
Self {
inner: AuthorityInner { val, meta },
_marker: PhantomData,
}
}
/// An empty authority component.
pub const EMPTY: Authority<'static, UserinfoE, RegNameE> = Authority::new("", AuthMeta::EMPTY);
pub(crate) fn meta(&self) -> AuthMeta {
self.inner.meta
}
/// Returns the authority component as a string slice.
///
/// # Examples
///
/// ```
/// use fluent_uri::Uri;
///
/// let uri = Uri::parse("http://user@example.com:8080/")?;
/// let auth = uri.authority().unwrap();
/// assert_eq!(auth.as_str(), "user@example.com:8080");
/// # Ok::<_, fluent_uri::error::ParseError>(())
/// ```
#[inline]
#[must_use]
pub fn as_str(&self) -> &'a str {
self.inner.val
}
/// Returns the optional [userinfo] subcomponent.
///
/// [userinfo]: https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1
///
/// # Examples
///
/// ```
/// use fluent_uri::{encoding::EStr, Uri};
///
/// let uri = Uri::parse("http://user@example.com/")?;
/// let auth = uri.authority().unwrap();
/// assert_eq!(auth.userinfo(), Some(EStr::new_or_panic("user")));
///
/// let uri = Uri::parse("http://example.com/")?;
/// let auth = uri.authority().unwrap();
/// assert_eq!(auth.userinfo(), None);
/// # Ok::<_, fluent_uri::error::ParseError>(())
/// ```
#[must_use]
pub fn userinfo(&self) -> Option<&'a EStr<UserinfoE>> {
self.inner.userinfo().map(EStr::cast)
}
/// Returns the [host] subcomponent as a string slice.
///
/// The host subcomponent is always present, although it may be empty.
///
/// The square brackets enclosing an IPv6 or IPvFuture address are included.
///
/// Note that ASCII characters within a host are *case-insensitive*.
///
/// [host]: https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2
///
/// # Examples
///
/// ```
/// use fluent_uri::Uri;
///
/// let uri = Uri::parse("http://user@example.com:8080/")?;
/// let auth = uri.authority().unwrap();
/// assert_eq!(auth.host(), "example.com");
///
/// let uri = Uri::parse("file:///path/to/file")?;
/// let auth = uri.authority().unwrap();
/// assert_eq!(auth.host(), "");
///
/// let uri = Uri::parse("http://[::1]")?;
/// let auth = uri.authority().unwrap();
/// assert_eq!(auth.host(), "[::1]");
/// # Ok::<_, fluent_uri::error::ParseError>(())
/// ```
#[must_use]
pub fn host(&self) -> &'a str {
self.inner.host()
}
/// Returns the parsed [host] subcomponent.
///
/// Note that ASCII characters within a host are *case-insensitive*.
///
/// [host]: https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2
///
/// # Examples
///
/// ```
/// use fluent_uri::{component::Host, encoding::EStr, Uri};
#[cfg_attr(feature = "net", doc = "use std::net::{Ipv4Addr, Ipv6Addr};")]
///
/// let uri = Uri::parse("foo://127.0.0.1")?;
/// let auth = uri.authority().unwrap();
#[cfg_attr(
feature = "net",
doc = "assert!(matches!(auth.host_parsed(), Host::Ipv4(Ipv4Addr::LOCALHOST)));"
)]
#[cfg_attr(
not(feature = "net"),
doc = "assert!(matches!(auth.host_parsed(), Host::Ipv4 { .. }));"
)]
///
/// let uri = Uri::parse("foo://[::1]")?;
/// let auth = uri.authority().unwrap();
#[cfg_attr(
feature = "net",
doc = "assert!(matches!(auth.host_parsed(), Host::Ipv6(Ipv6Addr::LOCALHOST)));"
)]
#[cfg_attr(
not(feature = "net"),
doc = "assert!(matches!(auth.host_parsed(), Host::Ipv6 { .. }));"
)]
///
/// let uri = Uri::parse("foo://[v1.addr]")?;
/// let auth = uri.authority().unwrap();
/// // The API design for IPvFuture addresses is to be determined.
/// assert!(matches!(auth.host_parsed(), Host::IpvFuture { .. }));
///
/// let uri = Uri::parse("foo://localhost")?;
/// let auth = uri.authority().unwrap();
/// assert!(matches!(auth.host_parsed(), Host::RegName(name) if name == "localhost"));
/// # Ok::<_, fluent_uri::error::ParseError>(())
/// ```
#[must_use]
pub fn host_parsed(&self) -> Host<'a, RegNameE> {
match self.inner.meta.host_meta {
#[cfg(feature = "net")]
HostMeta::Ipv4(addr) => Host::Ipv4(addr),
#[cfg(feature = "net")]
HostMeta::Ipv6(addr) => Host::Ipv6(addr),
#[cfg(not(feature = "net"))]
HostMeta::Ipv4() => Host::Ipv4(),
#[cfg(not(feature = "net"))]
HostMeta::Ipv6() => Host::Ipv6(),
HostMeta::IpvFuture => Host::IpvFuture,
HostMeta::RegName => Host::RegName(EStr::new_validated(self.host())),
}
}
/// Returns the optional [port] subcomponent.
///
/// A scheme may define a default port to use when the port is
/// not present or is empty.
///
/// Note that the port may be empty, with leading zeros, or larger than [`u16::MAX`].
/// It is up to you to decide whether to deny such ports, fallback to the scheme's
/// default if it is empty, ignore the leading zeros, or use a special addressing
/// mechanism that allows ports larger than [`u16::MAX`].
///
/// [port]: https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.3
///
/// # Examples
///
/// ```
/// use fluent_uri::{encoding::EStr, Uri};
///
/// let uri = Uri::parse("foo://localhost:4673/")?;
/// let auth = uri.authority().unwrap();
/// assert_eq!(auth.port(), Some(EStr::new_or_panic("4673")));
///
/// let uri = Uri::parse("foo://localhost:/")?;
/// let auth = uri.authority().unwrap();
/// assert_eq!(auth.port(), Some(EStr::EMPTY));
///
/// let uri = Uri::parse("foo://localhost/")?;
/// let auth = uri.authority().unwrap();
/// assert_eq!(auth.port(), None);
///
/// let uri = Uri::parse("foo://localhost:123456/")?;
/// let auth = uri.authority().unwrap();
/// assert_eq!(auth.port(), Some(EStr::new_or_panic("123456")));
/// # Ok::<_, fluent_uri::error::ParseError>(())
/// ```
#[must_use]
pub fn port(&self) -> Option<&'a EStr<Port>> {
self.inner.port()
}
/// Converts the [port] subcomponent to `u16`, if present and nonempty.
///
/// Returns `Ok(None)` if the port is not present or is empty. Leading zeros are ignored.
///
/// [port]: https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.3
///
/// # Errors
///
/// Returns `Err` if the port cannot be parsed into `u16`.
///
/// # Examples
///
/// ```
/// use fluent_uri::Uri;
///
/// let uri = Uri::parse("foo://localhost:4673/")?;
/// let auth = uri.authority().unwrap();
/// assert_eq!(auth.port_to_u16(), Ok(Some(4673)));
///
/// let uri = Uri::parse("foo://localhost/")?;
/// let auth = uri.authority().unwrap();
/// assert_eq!(auth.port_to_u16(), Ok(None));
///
/// let uri = Uri::parse("foo://localhost:/")?;
/// let auth = uri.authority().unwrap();
/// assert_eq!(auth.port_to_u16(), Ok(None));
///
/// let uri = Uri::parse("foo://localhost:123456/")?;
/// let auth = uri.authority().unwrap();
/// assert!(auth.port_to_u16().is_err());
/// # Ok::<_, fluent_uri::error::ParseError>(())
/// ```
pub fn port_to_u16(&self) -> Result<Option<u16>, ParseIntError> {
self.inner.port_to_u16()
}
/// Converts the host and the port subcomponent to an iterator of resolved [`SocketAddr`]s.
///
/// The default port is used if the port component is not present or is empty.
/// A registered name is first [decoded] and then resolved with [`ToSocketAddrs`].
/// Punycode encoding is **not** performed prior to resolution.
///
/// [decoded]: EStr::decode
///
/// # Errors
///
/// Returns `Err` if any of the following is true.
///
/// - The port cannot be parsed into `u16`.
/// - The host is an IPvFuture address.
/// - A registered name does not decode to valid UTF-8 or fails to resolve.
#[cfg(all(feature = "net", feature = "std"))]
pub fn socket_addrs(&self, default_port: u16) -> io::Result<impl Iterator<Item = SocketAddr>> {
self.inner.socket_addrs(default_port)
}
/// Checks whether a userinfo subcomponent is present.
///
/// # Examples
///
/// ```
/// use fluent_uri::Uri;
///
/// let uri = Uri::parse("http://user@example.com/")?;
/// assert!(uri.authority().unwrap().has_userinfo());
///
/// let uri = Uri::parse("http://example.com/")?;
/// assert!(!uri.authority().unwrap().has_userinfo());
/// # Ok::<_, fluent_uri::error::ParseError>(())
#[inline]
#[must_use]
pub fn has_userinfo(&self) -> bool {
self.inner.meta.host_bounds.0 != 0
}
/// Checks whether a port subcomponent is present.
///
/// # Examples
///
/// ```
/// use fluent_uri::Uri;
///
/// let uri = Uri::parse("foo://localhost:4673/")?;
/// assert!(uri.authority().unwrap().has_port());
///
/// // The port subcomponent can be empty.
/// let uri = Uri::parse("foo://localhost:/")?;
/// assert!(uri.authority().unwrap().has_port());
///
/// let uri = Uri::parse("foo://localhost/")?;
/// let auth = uri.authority().unwrap();
/// assert!(!uri.authority().unwrap().has_port());
/// # Ok::<_, fluent_uri::error::ParseError>(())
/// ```
#[inline]
#[must_use]
pub fn has_port(&self) -> bool {
self.inner.meta.host_bounds.1 != self.inner.val.len()
}
}
/// A parsed [host] component.
///
/// [host]: https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2
#[derive(Clone, Copy)]
#[cfg_attr(fuzzing, derive(PartialEq, Eq))]
pub enum Host<'a, RegNameE: Encoder = RegName> {
/// An IPv4 address.
#[cfg_attr(not(feature = "net"), non_exhaustive)]
Ipv4(
/// The address.
#[cfg(feature = "net")]
Ipv4Addr,
),
/// An IPv6 address.
#[cfg_attr(not(feature = "net"), non_exhaustive)]
Ipv6(
/// The address.
#[cfg(feature = "net")]
Ipv6Addr,
),
/// An IP address of future version.
///
/// This variant is marked as non-exhaustive because the API design
/// for IPvFuture addresses is to be determined.
#[non_exhaustive]
IpvFuture,
/// A registered name.
///
/// Note that ASCII characters within a registered name are *case-insensitive*.
RegName(&'a EStr<RegNameE>),
}