iri_string/resolve.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
//! URI and IRI resolvers.
//!
//! # IRI resolution can fail without WHATWG URL Standard serialization
//!
//! ## Pure RFC 3986 algorithm
//!
//! Though this is not explicitly stated in RFC 3986, IRI resolution can fail.
//! Below are examples:
//!
//! * base=`scheme:`, ref=`.///bar`.
//! + Resulting IRI should have scheme `scheme` and path `//bar`, but does not have authority.
//! * base=`scheme:foo`, ref=`.///bar`.
//! + Resulting IRI should have scheme `scheme` and path `//bar`, but does not have authority.
//! * base=`scheme:`, ref=`/..//baz`.
//! + Resulting IRI should have scheme `scheme` and path `//bar`, but does not have authority.
//! * base=`scheme:foo/bar`, ref=`..//baz`.
//! + Resulting IRI should have scheme `scheme` and path `//bar`, but does not have authority.
//!
//! IRI without authority (note that this is different from "with empty authority")
//! cannot have a path starting with `//`, since it is ambiguous and can be
//! interpreted as an IRI with authority. For the above examples, `scheme://bar`
//! is not valid output, as `bar` in `scheme://bar` will be interpreted as an
//! authority, not a path.
//!
//! Thus, IRI resolution by pure RFC 3986 algorithm can fail for some abnormal
//! cases.
//!
//! Note that this kind of failure can happen only when the base IRI has no
//! authority and empty path. This would be rare in the wild, since many people
//! would use an IRI with authority part, such as `http://`.
//!
//! If you are handling `scheme://`-style URIs and IRIs, don't worry about the
//! failure. Currently no cases are known to fail when at least one of the base
//! IRI or the relative IRI contains authorities.
//!
//! If you want this kind of abnormal IRI resolution to succeed and to be
//! idempotent, check the resolution result using
//! [`Normalized::ensure_rfc3986_normalizable`] (see the section below).
//!
//! ## WHATWG serialization
//!
//! To handle IRI resolution failure, WHATWG URL Standard defines serialization
//! algorithm for this kind of result, and it makes IRI resolution (and even
//! normalization) infallible and idempotent.
//!
//! IRI resolution and normalization provided by this crate automatically
//! applies this special rule if necessary, so they are infallible. If you want
//! to detect resolution/normalization failure, use
//! [`Normalized::ensure_rfc3986_normalizable`] method.
//!
//! ## Examples
//!
//! ```
//! # #[cfg(feature = "alloc")] {
//! use iri_string::format::ToDedicatedString;
//! use iri_string::types::{IriAbsoluteStr, IriReferenceStr};
//!
//! let base = IriAbsoluteStr::new("scheme:")?;
//! {
//! let reference = IriReferenceStr::new(".///not-a-host")?;
//! let result = reference.resolve_against(base);
//! assert!(result.ensure_rfc3986_normalizable().is_err());
//! assert_eq!(result.to_dedicated_string(), "scheme:/.//not-a-host");
//! }
//!
//! {
//! let reference2 = IriReferenceStr::new("/..//not-a-host")?;
//! // Resulting string will be `scheme://not-a-host`, but `not-a-host`
//! // should be a path segment, not a host. So, the semantically correct
//! // target IRI cannot be represented by RFC 3986 IRI resolution.
//! let result2 = reference2.resolve_against(base);
//! assert!(result2.ensure_rfc3986_normalizable().is_err());
//!
//! // Algorithm defined in WHATWG URL Standard addresses this case.
//! assert_eq!(result2.to_dedicated_string(), "scheme:/.//not-a-host");
//! }
//! # }
//! # Ok::<_, iri_string::validate::Error>(())
//! ```
use crate::components::RiReferenceComponents;
use crate::normalize::{NormalizationInput, Normalized};
use crate::spec::Spec;
use crate::types::{RiAbsoluteStr, RiQueryStr, RiReferenceStr, RiStr};
/// A resolver against the fixed base.
#[derive(Debug, Clone, Copy)]
pub struct FixedBaseResolver<'a, S: Spec> {
/// Components of the base IRI.
base_components: RiReferenceComponents<'a, S>,
}
impl<'a, S: Spec> FixedBaseResolver<'a, S> {
/// Creates a new resolver with the given base.
///
/// # Examples
///
/// ```
/// # use iri_string::validate::Error;
/// # // `ToDedicatedString` is available only when
/// # // `alloc` feature is enabled.
/// #[cfg(feature = "alloc")] {
/// use iri_string::format::ToDedicatedString;
/// use iri_string::resolve::FixedBaseResolver;
/// use iri_string::types::{IriAbsoluteStr, IriReferenceStr};
///
/// let base = IriAbsoluteStr::new("http://example.com/base/")?;
/// let resolver = FixedBaseResolver::new(base);
///
/// let reference = IriReferenceStr::new("../there")?;
/// let resolved = resolver.resolve(reference);
///
/// assert_eq!(resolved.to_dedicated_string(), "http://example.com/there");
/// # }
/// # Ok::<_, Error>(())
/// ```
#[must_use]
pub fn new(base: &'a RiAbsoluteStr<S>) -> Self {
Self {
base_components: RiReferenceComponents::from(base.as_ref()),
}
}
/// Returns the base.
///
/// # Examples
///
/// ```
/// use iri_string::resolve::FixedBaseResolver;
/// use iri_string::types::{IriAbsoluteStr, IriReferenceStr};
///
/// let base = IriAbsoluteStr::new("http://example.com/base/")?;
/// let resolver = FixedBaseResolver::new(base);
///
/// assert_eq!(resolver.base(), base);
/// # Ok::<_, iri_string::validate::Error>(())
/// ```
#[must_use]
pub fn base(&self) -> &'a RiAbsoluteStr<S> {
// SAFETY: `base_components` can only be created from `&RiAbsoluteStr<S>`,
// and the type of `base_components` does not allow modification of the
// content after it is created.
unsafe { RiAbsoluteStr::new_maybe_unchecked(self.base_components.iri().as_str()) }
}
}
/// Components getters.
///
/// These getters are more efficient than calling through the result of `.base()`.
impl<'a, S: Spec> FixedBaseResolver<'a, S> {
/// Returns the scheme.
///
/// The following colon is truncated.
///
/// # Examples
///
/// ```
/// # use iri_string::validate::Error;
/// use iri_string::resolve::FixedBaseResolver;
/// use iri_string::types::IriAbsoluteStr;
///
/// let base = IriAbsoluteStr::new("http://example.com/base/?query")?;
/// let resolver = FixedBaseResolver::new(base);
///
/// assert_eq!(resolver.scheme_str(), "http");
/// assert_eq!(base.scheme_str(), "http");
/// # Ok::<_, Error>(())
/// ```
#[inline]
#[must_use]
pub fn scheme_str(&self) -> &str {
self.base_components
.scheme_str()
.expect("[validity] absolute IRI should have the scheme part")
}
/// Returns the authority.
///
/// The leading `//` is truncated.
///
/// # Examples
///
/// ```
/// # use iri_string::validate::Error;
/// use iri_string::resolve::FixedBaseResolver;
/// use iri_string::types::IriAbsoluteStr;
///
/// let base = IriAbsoluteStr::new("http://user:pass@example.com/base/?query")?;
/// let resolver = FixedBaseResolver::new(base);
///
/// assert_eq!(resolver.authority_str(), Some("user:pass@example.com"));
/// assert_eq!(base.authority_str(), Some("user:pass@example.com"));
/// # Ok::<_, Error>(())
/// ```
#[inline]
#[must_use]
pub fn authority_str(&self) -> Option<&str> {
self.base_components.authority_str()
}
/// Returns the path.
///
/// # Examples
///
/// ```
/// # use iri_string::validate::Error;
/// use iri_string::resolve::FixedBaseResolver;
/// use iri_string::types::IriAbsoluteStr;
///
/// let base = IriAbsoluteStr::new("http://user:pass@example.com/base/?query")?;
/// let resolver = FixedBaseResolver::new(base);
///
/// assert_eq!(resolver.path_str(), "/base/");
/// assert_eq!(base.path_str(), "/base/");
/// # Ok::<_, Error>(())
/// ```
#[inline]
#[must_use]
pub fn path_str(&self) -> &str {
self.base_components.path_str()
}
/// Returns the query.
///
/// The leading question mark (`?`) is truncated.
///
/// # Examples
///
/// ```
/// # use iri_string::validate::Error;
/// use iri_string::resolve::FixedBaseResolver;
/// use iri_string::types::{IriAbsoluteStr, IriQueryStr};
///
/// let base = IriAbsoluteStr::new("http://user:pass@example.com/base/?query")?;
/// let resolver = FixedBaseResolver::new(base);
/// let query = IriQueryStr::new("query")?;
///
/// assert_eq!(resolver.query(), Some(query));
/// assert_eq!(base.query(), Some(query));
/// # Ok::<_, Error>(())
/// ```
#[inline]
#[must_use]
pub fn query(&self) -> Option<&RiQueryStr<S>> {
let query_raw = self.query_str()?;
let query = RiQueryStr::new(query_raw)
.expect("[validity] must be valid query if present in an absolute-IRI");
Some(query)
}
/// Returns the query in a raw string slice.
///
/// The leading question mark (`?`) is truncated.
///
/// # Examples
///
/// ```
/// # use iri_string::validate::Error;
/// use iri_string::resolve::FixedBaseResolver;
/// use iri_string::types::IriAbsoluteStr;
///
/// let base = IriAbsoluteStr::new("http://user:pass@example.com/base/?query")?;
/// let resolver = FixedBaseResolver::new(base);
///
/// assert_eq!(resolver.query_str(), Some("query"));
/// assert_eq!(base.query_str(), Some("query"));
/// # Ok::<_, Error>(())
/// ```
#[inline]
#[must_use]
pub fn query_str(&self) -> Option<&str> {
self.base_components.query_str()
}
}
impl<'a, S: Spec> FixedBaseResolver<'a, S> {
/// Resolves the given reference against the fixed base.
///
/// The task returned by this method does **not** normalize the resolution
/// result. However, `..` and `.` are recognized even when they are
/// percent-encoded.
///
/// # Failures
///
/// This function itself does not fail, but resolution algorithm defined by
/// RFC 3986 can fail. In that case, serialization algorithm defined by
/// WHATWG URL Standard would be automatically applied.
///
/// See the documentation of [`Normalized`].
///
/// # Examples
///
/// ```
/// # use iri_string::validate::Error;
/// # // `ToDedicatedString` is available only when
/// # // `alloc` feature is enabled.
/// # #[cfg(feature = "alloc")] {
/// use iri_string::format::ToDedicatedString;
/// use iri_string::resolve::FixedBaseResolver;
/// use iri_string::types::{IriAbsoluteStr, IriReferenceStr};
///
/// let base = IriAbsoluteStr::new("http://example.com/base/")?;
/// let resolver = FixedBaseResolver::new(base);
///
/// let reference = IriReferenceStr::new("../there")?;
/// let resolved = resolver.resolve(reference);
///
/// assert_eq!(resolved.to_dedicated_string(), "http://example.com/there");
/// # }
/// # Ok::<_, Error>(())
/// ```
///
/// Note that `..` and `.` path segments are recognized even when they are
/// percent-encoded.
///
/// ```
/// # use iri_string::validate::Error;
/// # // `ToDedicatedString` is available only when
/// # // `alloc` feature is enabled.
/// # #[cfg(feature = "alloc")] {
/// use iri_string::format::ToDedicatedString;
/// use iri_string::resolve::FixedBaseResolver;
/// use iri_string::types::{IriAbsoluteStr, IriReferenceStr};
///
/// let base = IriAbsoluteStr::new("HTTP://example.COM/base/base2/")?;
/// let resolver = FixedBaseResolver::new(base);
///
/// // `%2e%2e` is recognized as `..`.
/// // However, `dot%2edot` is NOT normalized into `dot.dot`.
/// let reference = IriReferenceStr::new("%2e%2e/../dot%2edot")?;
/// let resolved = resolver.resolve(reference);
///
/// // Resolved but not normalized.
/// assert_eq!(resolved.to_dedicated_string(), "HTTP://example.COM/dot%2edot");
/// # }
/// # Ok::<_, Error>(())
/// ```
#[inline]
#[must_use]
pub fn resolve(&self, reference: &'a RiReferenceStr<S>) -> Normalized<'a, RiStr<S>> {
let input = NormalizationInput::with_resolution_params(&self.base_components, reference);
Normalized::from_input(input)
}
}