urly 1.4.1

Yet another URL library
Documentation
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
//! Lenient parsing with yarl-style normalization, and strict RFC 3986 validation.

use std::borrow::Cow;

use ntex_bytes::{ByteString, Bytes};

use crate::authority::{self, offset, parse_port, validate_authority};
use crate::chars::{self, PATH, QUERY, lowercase, scheme_len, split_at_char};
use crate::error::{ErrorKind, InvalidUrl};
use crate::host::normalize_host;
use crate::quoting::{Component, requote};
use crate::scheme::Scheme;
use crate::url::{Components, Url, assemble, assemble_target, origin_form};

/// Maximum URL length in bytes.
pub(crate) const MAX_LEN: usize = u16::MAX as usize;

/// Splits a URL reference into its raw components.
fn split(s: &str) -> Components<'_> {
    let (scheme, rest) = match scheme_len(s) {
        Some(n) => (Some(&s[..n]), &s[n + 1..]),
        None => (None, s),
    };
    let (rest, fragment) = split_at_char(rest, b'#');
    let (rest, query) = split_at_char(rest, b'?');
    let (authority, path) = match rest.strip_prefix("//") {
        Some(r) => {
            let (authority, path) = r.split_at(r.find('/').unwrap_or(r.len()));
            (Some(authority), path)
        }
        None => (None, rest),
    };
    Components {
        scheme,
        authority,
        path,
        query,
        fragment,
    }
}

/// Parses and normalizes a URL reference.
///
/// `orig` is the buffer `src` came from; it is reused if no normalization is needed.
pub(crate) fn parse(src: &str, orig: Option<&ByteString>) -> Result<Url, InvalidUrl> {
    let (cleaned, lead) = clean(src)?;
    parse_clean(&cleaned, orig).map_err(|e| e.offset(lead))
}

/// Parses and normalizes an authority-form `[userinfo@]host[:port]`.
pub(crate) fn parse_authority(src: &str) -> Result<Url, InvalidUrl> {
    let (cleaned, lead) = clean(src)?;
    parse_authority_clean(&cleaned).map_err(|e| e.offset(lead))
}

fn parse_authority_clean(s: &str) -> Result<Url, InvalidUrl> {
    if let Some(i) = s.find(['/', '?', '#']) {
        return Err(InvalidUrl::at(
            ErrorKind::InvalidChar(char::from(s.as_bytes()[i])),
            i,
        ));
    }
    let host = authority::split(s).1;
    if host.is_empty() {
        return Err(InvalidUrl::at(ErrorKind::InvalidHost, offset(s, host)));
    }
    let authority = normalize_authority(s)?;
    assemble(
        &Components {
            authority: Some(&authority),
            ..Components::default()
        },
        None,
    )
}

/// Trims whitespace and removes tabs and newlines, returns the cleaned input
/// and the number of leading bytes removed.
fn clean(src: &str) -> Result<(Cow<'_, str>, usize), InvalidUrl> {
    let trimmed = src.trim_start_matches(|c: char| c <= ' ');
    let lead = src.len() - trimmed.len();
    let trimmed = trimmed.trim_end_matches(|c: char| c <= ' ');
    if trimmed.is_empty() {
        return Err(InvalidUrl::new(ErrorKind::Empty));
    }
    if trimmed.len() > MAX_LEN {
        return Err(InvalidUrl::new(ErrorKind::TooLong));
    }
    // branchless prefilter vectorizes, `\t`, `\n` and `\r` are below 0x0e
    let cleaned = if trimmed.bytes().fold(false, |a, b| a | (b < 0x0e))
        && trimmed.contains(['\t', '\n', '\r'])
    {
        Cow::Owned(trimmed.replace(['\t', '\n', '\r'], ""))
    } else {
        Cow::Borrowed(trimmed)
    };
    Ok((cleaned, lead))
}

fn parse_clean(s: &str, orig: Option<&ByteString>) -> Result<Url, InvalidUrl> {
    let c = split(s);
    normalize(&c, orig).map_err(|e| {
        // Authority errors are relative to that component.
        e.offset(c.authority.map_or(0, |a| offset(s, a)))
    })
}

/// Normalizes parsed components, preserving their interpretation.
pub(crate) fn normalize(c: &Components<'_>, orig: Option<&ByteString>) -> Result<Url, InvalidUrl> {
    let scheme = c.scheme.map(|s| lowercase(s.into()));
    let authority = c.authority.map(normalize_authority).transpose()?;
    let path = normalize_path(c.path);
    let query = c.query.map(|q| requote(q, Component::Query));
    let fragment = c.fragment.map(|f| requote(f, Component::Fragment));

    assemble(
        &Components {
            scheme: scheme.as_deref(),
            authority: authority.as_deref(),
            path: &path,
            query: query.as_deref(),
            fragment: fragment.as_deref(),
        },
        orig,
    )
}

/// Parses HTTP request-target forms without validating or normalizing their components.
pub(crate) fn parse_target(src: Bytes) -> Result<Url, InvalidUrl> {
    // checked before the UTF-8 scan, so oversized input is not scanned
    if src.len() > MAX_LEN {
        return Err(InvalidUrl::new(ErrorKind::TooLong));
    }
    simdutf8::compat::from_utf8(&src).map_err(crate::url::utf8_error)?;
    // SAFETY: UTF-8 is checked above; target syntax is checked separately.
    parse_target_str(unsafe { ByteString::from_bytes_unchecked(src) }, None)
}

/// Parses an HTTP request-target that is already known to be UTF-8.
///
/// `query` is the position of the first `?`, if known.
pub(crate) fn parse_target_str(
    src: ByteString,
    query: Option<Option<usize>>,
) -> Result<Url, InvalidUrl> {
    if src.is_empty() {
        return Err(InvalidUrl::new(ErrorKind::Empty));
    }
    if src.len() > MAX_LEN {
        return Err(InvalidUrl::new(ErrorKind::TooLong));
    }
    // the common origin-form needs no reassembly
    if src.starts_with('/') && !src.starts_with("//") {
        return Ok(origin_form(src, query));
    }
    let c = if src.starts_with('/') || src == "*" {
        let (path, query) = split_at_char(&src, b'?');
        Components {
            path,
            query,
            ..Components::default()
        }
    } else if scheme_len(&src).is_some_and(|n| src[n + 1..].starts_with("//")) {
        split(&src)
    } else {
        Components {
            authority: Some(&src),
            ..Components::default()
        }
    };
    assemble_target(&c, Some(&src))
}

pub(crate) fn validate_target(url: &Url) -> Result<(), InvalidUrl> {
    let src = url.as_str();
    validate_target_chars(src.as_bytes())?;
    if let Some(scheme) = url.scheme_str() {
        Scheme::new(scheme)?;
    }
    if let Some(a) = url.authority() {
        let a = a.as_str();
        validate_authority(a).map_err(|e| e.offset(offset(src, a)))?;
        let (userinfo, host, _) = authority::split(a);
        if userinfo.is_some() || host.is_empty() {
            return Err(InvalidUrl::at(ErrorKind::InvalidAuthority, offset(src, a)));
        }
        // Encoded hosts must also be valid after decoding, as in normalized parsing.
        if host.contains('%') {
            normalize_host(host).map_err(|e| e.offset(offset(src, host)))?;
        }
    }
    Ok(())
}

fn validate_target_chars(target: &[u8]) -> Result<(), InvalidUrl> {
    let mut path = true;
    let mut i = 0;
    while let Some(&b) = target.get(i) {
        match b {
            b'%' => {
                let (Some(&hi), Some(&lo)) = (target.get(i + 1), target.get(i + 2)) else {
                    return Err(InvalidUrl::at(ErrorKind::InvalidPercentEncoding, i));
                };
                if !hi.is_ascii_hexdigit() || !lo.is_ascii_hexdigit() {
                    return Err(InvalidUrl::at(ErrorKind::InvalidPercentEncoding, i));
                }
                if path && hi == b'0' && lo == b'0' {
                    return Err(InvalidUrl::at(ErrorKind::InvalidChar('\0'), i));
                }
                i += 3;
                continue;
            }
            b'?' => path = false,
            b'#' | b'"' | b'<' | b'>' | b'\\' | 0..=b' ' | 0x7f.. => {
                return Err(InvalidUrl::at(ErrorKind::InvalidChar(char::from(b)), i));
            }
            _ => (),
        }
        i += 1;
    }
    Ok(())
}

/// Requotes a path and removes dot segments from absolute paths.
pub(crate) fn normalize_path(path: &str) -> Cow<'_, str> {
    let path = requote(path, Component::Path);
    if path.starts_with('/') && has_dot_segments(&path) {
        Cow::Owned(remove_dot_segments(&path))
    } else {
        path
    }
}

/// Normalizes an authority: requotes userinfo, normalizes the host and port.
pub(crate) fn normalize_authority(a: &str) -> Result<Cow<'_, str>, InvalidUrl> {
    let (userinfo, host, port) = authority::split(a);
    let host_pos = offset(a, host);
    let new_host = normalize_host(host).map_err(|e| e.offset(host_pos))?;

    let mut changed = matches!(new_host, Cow::Owned(_));
    let new_port = match port {
        None => None,
        Some("") => {
            changed = true;
            None
        }
        Some(p) => {
            let port = parse_port(p).map_err(|e| e.offset(offset(a, p)))?;
            changed |= p.len() > 1 && p.starts_with('0');
            Some(port)
        }
    };
    if new_host.is_empty() && (userinfo.is_some() || new_port.is_some()) {
        return Err(InvalidUrl::at(ErrorKind::InvalidHost, host_pos));
    }
    let new_userinfo = userinfo.map(|u| match u.split_once(':') {
        Some((user, password)) => {
            let user = requote(user, Component::UserInfo);
            let password = requote(password, Component::UserInfo);
            if matches!((&user, &password), (Cow::Borrowed(_), Cow::Borrowed(_))) {
                Cow::Borrowed(u)
            } else {
                Cow::Owned(format!("{user}:{password}"))
            }
        }
        None => requote(u, Component::UserInfo),
    });
    changed |= matches!(new_userinfo, Some(Cow::Owned(_)));

    if !changed {
        return Ok(Cow::Borrowed(a));
    }
    Ok(Cow::Owned(join_authority(
        new_userinfo.as_deref(),
        &new_host,
        new_port,
    )))
}

pub(crate) fn join_authority(userinfo: Option<&str>, host: &str, port: Option<u16>) -> String {
    let mut out = String::with_capacity(host.len() + 16);
    if let Some(userinfo) = userinfo {
        out.push_str(userinfo);
        out.push('@');
    }
    out.push_str(host);
    if let Some(port) = port {
        out.push(':');
        out.push_str(&port.to_string());
    }
    out
}

pub(crate) fn has_dot_segments(path: &str) -> bool {
    let b = path.as_bytes();
    b.iter().enumerate().any(|(i, &c)| {
        c == b'.' && (i == 0 || b[i - 1] == b'/') && {
            let end = if b.get(i + 1) == Some(&b'.') { i + 2 } else { i + 1 };
            end == b.len() || b[end] == b'/'
        }
    })
}

/// RFC 3986 section 5.2.4.
pub(crate) fn remove_dot_segments(path: &str) -> String {
    let mut result = String::with_capacity(path.len());
    let rest = match path.strip_prefix('/') {
        Some(rest) => {
            result.push('/');
            rest
        }
        None => path,
    };
    // output segments are joined with `/` after `base`
    let base = result.len();
    let mut count = 0usize;
    let mut trailing_slash = false;
    for segment in rest.split('/') {
        match segment {
            "." => trailing_slash = true,
            ".." => {
                if count > 0 {
                    let pos = result[base..].rfind('/').map_or(base, |i| base + i);
                    result.truncate(pos);
                    count -= 1;
                }
                trailing_slash = true;
            }
            s => {
                if count > 0 {
                    result.push('/');
                }
                result.push_str(s);
                count += 1;
                trailing_slash = false;
            }
        }
    }
    if trailing_slash && count > 0 {
        result.push('/');
    }
    result
}

/// Strictly validates a URI reference.
pub(crate) fn validate(src: &str) -> Result<(), InvalidUrl> {
    if src.is_empty() {
        return Err(InvalidUrl::new(ErrorKind::Empty));
    }
    if src.len() > MAX_LEN {
        return Err(InvalidUrl::new(ErrorKind::TooLong));
    }
    // a colon in the first segment can only be a scheme delimiter
    if scheme_len(src).is_none()
        && let Some(i) = src.find([':', '/', '?', '#'])
        && src.as_bytes()[i] == b':'
    {
        return Err(match Scheme::new(&src[..i]) {
            Err(e) if e.kind() == ErrorKind::InvalidScheme => e,
            _ => InvalidUrl::at(ErrorKind::InvalidScheme, 0),
        });
    }
    let c = split(src);
    if let Some(a) = c.authority {
        validate_authority(a).map_err(|e| e.offset(offset(src, a)))?;
    }
    for (part, allowed) in [
        (Some(c.path), &PATH),
        (c.query, &QUERY),
        (c.fragment, &QUERY),
    ] {
        if let Some(part) = part {
            chars::check(part, allowed).map_err(|e| e.offset(offset(src, part)))?;
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    /// STATEMENT: `parse_target` builds path targets, including the origin-form fast
    /// path, the same as general target assembly.
    ///
    /// HOW:
    /// - Parse origin-form targets with empty, repeated, encoded, non-ASCII and
    ///   fragment-like parts, and queries containing `?`, `/` and `:`, plus `//`
    ///   paths that must not take the fast path.
    /// - Assemble the same split components with `assemble_target`.
    /// - Compare the string and every component accessor of both URLs, and of
    ///   `parse_target_str` given the position of the first `?`.
    #[test]
    fn origin_form_matches_assembly() {
        for target in [
            "/",
            "/?",
            "/??",
            "/a",
            "/a/",
            "/a:b/c",
            "/./a/../b",
            "/a%2Fb%zz",
            "/ü b#f",
            "/a?",
            "/a?b=1&c",
            "/a?b?c/d:e",
            "/a#f?q",
            "/a?q#f",
            "//",
            "//a/b?c",
        ] {
            let src = ByteString::from(target);
            let fast = parse_target(src.clone().into_bytes()).unwrap();
            let (path, query) = split_at_char(&src, b'?');
            let slow = assemble_target(
                &Components {
                    path,
                    query,
                    ..Components::default()
                },
                Some(&src),
            )
            .unwrap();
            assert_eq!(fast.as_str(), slow.as_str(), "{target}");
            assert_eq!(fast.scheme_str(), slow.scheme_str(), "{target}");
            assert_eq!(fast.authority(), slow.authority(), "{target}");
            assert_eq!(fast.host(), slow.host(), "{target}");
            assert_eq!(fast.path(), slow.path(), "{target}");
            assert_eq!(fast.query(), slow.query(), "{target}");
            assert_eq!(fast.fragment(), slow.fragment(), "{target}");
            assert_eq!(fast.path_and_query(), slow.path_and_query(), "{target}");

            let hinted = parse_target_str(src.clone(), Some(src.find('?'))).unwrap();
            assert_eq!(hinted.as_str(), slow.as_str(), "{target}");
            assert_eq!(hinted.path(), slow.path(), "{target}");
            assert_eq!(hinted.query(), slow.query(), "{target}");
        }
    }

    /// STATEMENT: `parse_target_str` ignores a query position that does not
    /// point at a `?`, so a wrong position cannot split inside a character.
    ///
    /// HOW:
    /// - Pass positions inside a multi-byte character, on a non-`?` byte and
    ///   past the end of the target.
    /// - Check the path and query are those of the first `?`.
    #[test]
    fn origin_form_ignores_bad_query_position() {
        let src = ByteString::from("/\u{fc}b?q");
        for pos in [2, 3, 100] {
            let url = parse_target_str(src.clone(), Some(Some(pos))).unwrap();
            assert_eq!(url.path(), "/\u{fc}b", "{pos}");
            assert_eq!(url.query().unwrap(), "q", "{pos}");
        }
    }

    #[test]
    fn dot_segments() {
        let cases = [
            ("/a/b/c/./../../g", "/a/g"),
            ("/a/b/..", "/a/"),
            ("/..", "/"),
            ("/.", "/"),
            ("/a/./", "/a/"),
            ("/a/../../b", "/b"),
            ("../a", "a"),
            (".", ""),
            ("/a//../b", "/a/b"),
            ("//.", "//"),
            ("//..", "/"),
            ("a/b/../../c", "c"),
            ("/a/b/../..", "/"),
            ("/a/.b/../c", "/a/c"),
        ];
        for (input, expected) in cases {
            assert_eq!(remove_dot_segments(input), expected, "{input}");
        }
        assert!(!has_dot_segments("/a/.b/c./..b/"));
        for path in [
            ".", "..", "/.", "/..", "./a", "../a", "/a/./b", "/a/../b", "a/.",
        ] {
            assert!(has_dot_segments(path), "{path}");
        }
    }

    #[test]
    fn authority() {
        assert!(matches!(
            normalize_authority("u:p@a.com:80"),
            Ok(Cow::Borrowed(_))
        ));
        assert_eq!(normalize_authority("U@A.com:").unwrap(), "U@a.com");
        assert_eq!(normalize_authority("a b:c@h:080").unwrap(), "a%20b:c@h:80");
        assert_eq!(normalize_authority("a@b@h").unwrap(), "a%40b@h");
        let err = normalize_authority("h:8a").unwrap_err();
        assert_eq!(
            (err.kind(), err.position()),
            (ErrorKind::InvalidPort, Some(3))
        );
        let err = normalize_authority("h:65536").unwrap_err();
        assert_eq!(err.kind(), ErrorKind::PortOutOfRange);
        let err = normalize_authority("u@:80").unwrap_err();
        assert_eq!(err.kind(), ErrorKind::InvalidHost);
    }

    #[test]
    fn authority_form() {
        for (src, expected) in [
            ("custom.domain", "//custom.domain"),
            (" Custom.Domain:080 ", "//custom.domain:80"),
            ("u:p@h:1", "//u:p@h:1"),
            ("[::1]:8080", "//[::1]:8080"),
            ("h:", "//h"),
        ] {
            assert_eq!(parse_authority(src).unwrap(), expected, "{src}");
        }
        let cases = [
            ("", ErrorKind::Empty, None),
            ("h/p", ErrorKind::InvalidChar('/'), Some(1)),
            (" h?q", ErrorKind::InvalidChar('?'), Some(2)),
            ("h#f", ErrorKind::InvalidChar('#'), Some(1)),
            (":", ErrorKind::InvalidHost, Some(0)),
            ("u@:80", ErrorKind::InvalidHost, Some(2)),
            ("h:8a", ErrorKind::InvalidPort, Some(3)),
        ];
        for (input, kind, pos) in cases {
            let err = parse_authority(input).unwrap_err();
            assert_eq!((err.kind(), err.position()), (kind, pos), "{input}");
        }
    }

    #[test]
    fn strict() {
        for valid in [
            "http://u:p@example.com:8080/a/b?c=d&e#f",
            "//example.com",
            "/a:b",
            "a/b:c",
            "?q",
            "#f",
            "mailto:a@b.c",
            "urn:isbn:0451450523",
            "http://[::1]/",
            "file:///etc/hosts",
        ] {
            assert_eq!(validate(valid), Ok(()), "{valid}");
        }
        let cases = [
            ("", ErrorKind::Empty, None),
            ("http://ex ample.com", ErrorKind::InvalidChar(' '), Some(9)),
            ("1http://a", ErrorKind::InvalidScheme, Some(0)),
            ("ht_tp://a", ErrorKind::InvalidScheme, Some(2)),
            (":a", ErrorKind::InvalidScheme, Some(0)),
            ("http://a/%zz", ErrorKind::InvalidPercentEncoding, Some(9)),
            ("http://a/?x#y#", ErrorKind::InvalidChar('#'), Some(13)),
            ("http://a:1x/", ErrorKind::InvalidPort, Some(10)),
            ("http://a:70000", ErrorKind::PortOutOfRange, Some(9)),
            ("http://[::1/", ErrorKind::InvalidIpv6, Some(7)),
            ("/ü", ErrorKind::InvalidChar('ü'), Some(1)),
        ];
        for (input, kind, pos) in cases {
            let err = validate(input).unwrap_err();
            assert_eq!((err.kind(), err.position()), (kind, pos), "{input}");
        }
    }
}