Coverage Report

Created: 2025-08-11 06:51

/rust/registry/src/index.crates.io-6f17d22bba15001f/url-2.5.4/src/parser.rs
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2013-2016 The rust-url developers.
2
//
3
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6
// option. This file may not be copied, modified, or distributed
7
// except according to those terms.
8
9
use alloc::string::String;
10
use alloc::string::ToString;
11
use core::fmt::{self, Formatter, Write};
12
use core::str;
13
14
use crate::host::{Host, HostInternal};
15
use crate::Url;
16
use form_urlencoded::EncodingOverride;
17
use percent_encoding::{percent_encode, utf8_percent_encode, AsciiSet, CONTROLS};
18
19
/// https://url.spec.whatwg.org/#fragment-percent-encode-set
20
const FRAGMENT: &AsciiSet = &CONTROLS.add(b' ').add(b'"').add(b'<').add(b'>').add(b'`');
21
22
/// https://url.spec.whatwg.org/#path-percent-encode-set
23
const PATH: &AsciiSet = &FRAGMENT.add(b'#').add(b'?').add(b'{').add(b'}');
24
25
/// https://url.spec.whatwg.org/#userinfo-percent-encode-set
26
pub(crate) const USERINFO: &AsciiSet = &PATH
27
    .add(b'/')
28
    .add(b':')
29
    .add(b';')
30
    .add(b'=')
31
    .add(b'@')
32
    .add(b'[')
33
    .add(b'\\')
34
    .add(b']')
35
    .add(b'^')
36
    .add(b'|');
37
38
pub(crate) const PATH_SEGMENT: &AsciiSet = &PATH.add(b'/').add(b'%');
39
40
// The backslash (\) character is treated as a path separator in special URLs
41
// so it needs to be additionally escaped in that case.
42
pub(crate) const SPECIAL_PATH_SEGMENT: &AsciiSet = &PATH_SEGMENT.add(b'\\');
43
44
// https://url.spec.whatwg.org/#query-state
45
const QUERY: &AsciiSet = &CONTROLS.add(b' ').add(b'"').add(b'#').add(b'<').add(b'>');
46
const SPECIAL_QUERY: &AsciiSet = &QUERY.add(b'\'');
47
48
pub type ParseResult<T> = Result<T, ParseError>;
49
50
macro_rules! simple_enum_error {
51
    ($($name: ident => $description: expr,)+) => {
52
        /// Errors that can occur during parsing.
53
        ///
54
        /// This may be extended in the future so exhaustive matching is
55
        /// discouraged with an unused variant.
56
        #[derive(PartialEq, Eq, Clone, Copy, Debug)]
57
        #[non_exhaustive]
58
        pub enum ParseError {
59
            $(
60
                $name,
61
            )+
62
        }
63
64
        impl fmt::Display for ParseError {
65
0
            fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
66
0
                match *self {
67
                    $(
68
0
                        ParseError::$name => fmt.write_str($description),
69
                    )+
70
                }
71
0
            }
72
        }
73
    }
74
}
75
76
#[cfg(feature = "std")]
77
impl std::error::Error for ParseError {}
78
79
#[cfg(not(feature = "std"))]
80
impl core::error::Error for ParseError {}
81
82
simple_enum_error! {
83
    EmptyHost => "empty host",
84
    IdnaError => "invalid international domain name",
85
    InvalidPort => "invalid port number",
86
    InvalidIpv4Address => "invalid IPv4 address",
87
    InvalidIpv6Address => "invalid IPv6 address",
88
    InvalidDomainCharacter => "invalid domain character",
89
    RelativeUrlWithoutBase => "relative URL without a base",
90
    RelativeUrlWithCannotBeABaseBase => "relative URL with a cannot-be-a-base base",
91
    SetHostOnCannotBeABaseUrl => "a cannot-be-a-base URL doesn’t have a host to set",
92
    Overflow => "URLs more than 4 GB are not supported",
93
}
94
95
impl From<::idna::Errors> for ParseError {
96
0
    fn from(_: ::idna::Errors) -> ParseError {
97
0
        ParseError::IdnaError
98
0
    }
99
}
100
101
macro_rules! syntax_violation_enum {
102
    ($($name: ident => $description: literal,)+) => {
103
        /// Non-fatal syntax violations that can occur during parsing.
104
        ///
105
        /// This may be extended in the future so exhaustive matching is
106
        /// forbidden.
107
        #[derive(PartialEq, Eq, Clone, Copy, Debug)]
108
        #[non_exhaustive]
109
        pub enum SyntaxViolation {
110
            $(
111
                /// ```text
112
                #[doc = $description]
113
                /// ```
114
                $name,
115
            )+
116
        }
117
118
        impl SyntaxViolation {
119
0
            pub fn description(&self) -> &'static str {
120
0
                match *self {
121
                    $(
122
                        SyntaxViolation::$name => $description,
123
                    )+
124
                }
125
0
            }
126
        }
127
    }
128
}
129
130
syntax_violation_enum! {
131
    Backslash => "backslash",
132
    C0SpaceIgnored =>
133
        "leading or trailing control or space character are ignored in URLs",
134
    EmbeddedCredentials =>
135
        "embedding authentication information (username or password) \
136
         in an URL is not recommended",
137
    ExpectedDoubleSlash => "expected //",
138
    ExpectedFileDoubleSlash => "expected // after file:",
139
    FileWithHostAndWindowsDrive => "file: with host and Windows drive letter",
140
    NonUrlCodePoint => "non-URL code point",
141
    NullInFragment => "NULL characters are ignored in URL fragment identifiers",
142
    PercentDecode => "expected 2 hex digits after %",
143
    TabOrNewlineIgnored => "tabs or newlines are ignored in URLs",
144
    UnencodedAtSign => "unencoded @ sign in username or password",
145
}
146
147
impl fmt::Display for SyntaxViolation {
148
0
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
149
0
        fmt::Display::fmt(self.description(), f)
150
0
    }
151
}
152
153
#[derive(Copy, Clone, PartialEq, Eq)]
154
pub enum SchemeType {
155
    File,
156
    SpecialNotFile,
157
    NotSpecial,
158
}
159
160
impl SchemeType {
161
0
    pub fn is_special(&self) -> bool {
162
0
        !matches!(*self, SchemeType::NotSpecial)
163
0
    }
164
165
0
    pub fn is_file(&self) -> bool {
166
0
        matches!(*self, SchemeType::File)
167
0
    }
168
}
169
170
impl<T: AsRef<str>> From<T> for SchemeType {
171
0
    fn from(s: T) -> Self {
172
0
        match s.as_ref() {
173
0
            "http" | "https" | "ws" | "wss" | "ftp" => SchemeType::SpecialNotFile,
174
0
            "file" => SchemeType::File,
175
0
            _ => SchemeType::NotSpecial,
176
        }
177
0
    }
Unexecuted instantiation: <url::parser::SchemeType as core::convert::From<&alloc::string::String>>::from
Unexecuted instantiation: <url::parser::SchemeType as core::convert::From<&str>>::from
178
}
179
180
0
pub fn default_port(scheme: &str) -> Option<u16> {
181
0
    match scheme {
182
0
        "http" | "ws" => Some(80),
183
0
        "https" | "wss" => Some(443),
184
0
        "ftp" => Some(21),
185
0
        _ => None,
186
    }
187
0
}
188
189
#[derive(Clone, Debug)]
190
pub struct Input<'i> {
191
    chars: str::Chars<'i>,
192
}
193
194
impl<'i> Input<'i> {
195
0
    pub fn new_no_trim(input: &'i str) -> Self {
196
0
        Input {
197
0
            chars: input.chars(),
198
0
        }
199
0
    }
200
201
0
    pub fn new_trim_tab_and_newlines(
202
0
        original_input: &'i str,
203
0
        vfn: Option<&dyn Fn(SyntaxViolation)>,
204
0
    ) -> Self {
205
0
        let input = original_input.trim_matches(ascii_tab_or_new_line);
206
0
        if let Some(vfn) = vfn {
207
0
            if input.len() < original_input.len() {
208
0
                vfn(SyntaxViolation::C0SpaceIgnored)
209
0
            }
210
0
            if input.chars().any(|c| matches!(c, '\t' | '\n' | '\r')) {
211
0
                vfn(SyntaxViolation::TabOrNewlineIgnored)
212
0
            }
213
0
        }
214
0
        Input {
215
0
            chars: input.chars(),
216
0
        }
217
0
    }
218
219
0
    pub fn new_trim_c0_control_and_space(
220
0
        original_input: &'i str,
221
0
        vfn: Option<&dyn Fn(SyntaxViolation)>,
222
0
    ) -> Self {
223
0
        let input = original_input.trim_matches(c0_control_or_space);
224
0
        if let Some(vfn) = vfn {
225
0
            if input.len() < original_input.len() {
226
0
                vfn(SyntaxViolation::C0SpaceIgnored)
227
0
            }
228
0
            if input.chars().any(|c| matches!(c, '\t' | '\n' | '\r')) {
229
0
                vfn(SyntaxViolation::TabOrNewlineIgnored)
230
0
            }
231
0
        }
232
0
        Input {
233
0
            chars: input.chars(),
234
0
        }
235
0
    }
236
237
    #[inline]
238
0
    pub fn is_empty(&self) -> bool {
239
0
        self.clone().next().is_none()
240
0
    }
241
242
    #[inline]
243
0
    fn starts_with<P: Pattern>(&self, p: P) -> bool {
244
0
        p.split_prefix(&mut self.clone())
245
0
    }
Unexecuted instantiation: <url::parser::Input>::starts_with::<url::parser::ascii_alpha>
Unexecuted instantiation: <url::parser::Input>::starts_with::<&str>
Unexecuted instantiation: <url::parser::Input>::starts_with::<char>
246
247
    #[inline]
248
0
    pub fn split_prefix<P: Pattern>(&self, p: P) -> Option<Self> {
249
0
        let mut remaining = self.clone();
250
0
        if p.split_prefix(&mut remaining) {
251
0
            Some(remaining)
252
        } else {
253
0
            None
254
        }
255
0
    }
Unexecuted instantiation: <url::parser::Input>::split_prefix::<&str>
Unexecuted instantiation: <url::parser::Input>::split_prefix::<char>
256
257
    #[inline]
258
0
    fn split_first(&self) -> (Option<char>, Self) {
259
0
        let mut remaining = self.clone();
260
0
        (remaining.next(), remaining)
261
0
    }
262
263
    #[inline]
264
0
    fn count_matching<F: Fn(char) -> bool>(&self, f: F) -> (u32, Self) {
265
0
        let mut count = 0;
266
0
        let mut remaining = self.clone();
267
        loop {
268
0
            let mut input = remaining.clone();
269
0
            if matches!(input.next(), Some(c) if f(c)) {
270
0
                remaining = input;
271
0
                count += 1;
272
0
            } else {
273
0
                return (count, remaining);
274
0
            }
275
0
        }
276
0
    }
Unexecuted instantiation: <url::parser::Input>::count_matching::<<url::parser::Parser>::parse_relative::{closure#0}>
Unexecuted instantiation: <url::parser::Input>::count_matching::<<url::parser::Parser>::parse_with_scheme::{closure#2}>
277
278
    #[inline]
279
0
    fn next_utf8(&mut self) -> Option<(char, &'i str)> {
280
        loop {
281
0
            let utf8 = self.chars.as_str();
282
0
            match self.chars.next() {
283
0
                Some(c) => {
284
0
                    if !matches!(c, '\t' | '\n' | '\r') {
285
0
                        return Some((c, &utf8[..c.len_utf8()]));
286
0
                    }
287
                }
288
0
                None => return None,
289
            }
290
        }
291
0
    }
292
}
293
294
pub trait Pattern {
295
    fn split_prefix(self, input: &mut Input) -> bool;
296
}
297
298
impl Pattern for char {
299
0
    fn split_prefix(self, input: &mut Input) -> bool {
300
0
        input.next() == Some(self)
301
0
    }
302
}
303
304
impl<'a> Pattern for &'a str {
305
0
    fn split_prefix(self, input: &mut Input) -> bool {
306
0
        for c in self.chars() {
307
0
            if input.next() != Some(c) {
308
0
                return false;
309
0
            }
310
        }
311
0
        true
312
0
    }
313
}
314
315
impl<F: FnMut(char) -> bool> Pattern for F {
316
0
    fn split_prefix(self, input: &mut Input) -> bool {
317
0
        input.next().map_or(false, self)
318
0
    }
319
}
320
321
impl<'i> Iterator for Input<'i> {
322
    type Item = char;
323
0
    fn next(&mut self) -> Option<char> {
324
0
        self.chars
325
0
            .by_ref()
326
0
            .find(|&c| !matches!(c, '\t' | '\n' | '\r'))
327
0
    }
328
}
329
330
pub struct Parser<'a> {
331
    pub serialization: String,
332
    pub base_url: Option<&'a Url>,
333
    pub query_encoding_override: EncodingOverride<'a>,
334
    pub violation_fn: Option<&'a dyn Fn(SyntaxViolation)>,
335
    pub context: Context,
336
}
337
338
#[derive(PartialEq, Eq, Copy, Clone)]
339
pub enum Context {
340
    UrlParser,
341
    Setter,
342
    PathSegmentSetter,
343
}
344
345
impl<'a> Parser<'a> {
346
0
    fn log_violation(&self, v: SyntaxViolation) {
347
0
        if let Some(f) = self.violation_fn {
348
0
            f(v)
349
0
        }
350
0
    }
351
352
0
    fn log_violation_if(&self, v: SyntaxViolation, test: impl FnOnce() -> bool) {
353
0
        if let Some(f) = self.violation_fn {
354
0
            if test() {
355
0
                f(v)
356
0
            }
357
0
        }
358
0
    }
Unexecuted instantiation: <url::parser::Parser>::log_violation_if::<<url::parser::Parser>::parse_file::{closure#0}>
Unexecuted instantiation: <url::parser::Parser>::log_violation_if::<<url::parser::Parser>::parse_file::{closure#1}>
Unexecuted instantiation: <url::parser::Parser>::log_violation_if::<<url::parser::Parser>::parse_relative::{closure#1}>
Unexecuted instantiation: <url::parser::Parser>::log_violation_if::<<url::parser::Parser>::parse_with_scheme::{closure#0}>
Unexecuted instantiation: <url::parser::Parser>::log_violation_if::<<url::parser::Parser>::parse_with_scheme::{closure#3}>
359
360
0
    pub fn for_setter(serialization: String) -> Parser<'a> {
361
0
        Parser {
362
0
            serialization,
363
0
            base_url: None,
364
0
            query_encoding_override: None,
365
0
            violation_fn: None,
366
0
            context: Context::Setter,
367
0
        }
368
0
    }
369
370
    /// https://url.spec.whatwg.org/#concept-basic-url-parser
371
0
    pub fn parse_url(mut self, input: &str) -> ParseResult<Url> {
372
0
        let input = Input::new_trim_c0_control_and_space(input, self.violation_fn);
373
0
        if let Ok(remaining) = self.parse_scheme(input.clone()) {
374
0
            return self.parse_with_scheme(remaining);
375
0
        }
376
377
        // No-scheme state
378
0
        if let Some(base_url) = self.base_url {
379
0
            if input.starts_with('#') {
380
0
                self.fragment_only(base_url, input)
381
0
            } else if base_url.cannot_be_a_base() {
382
0
                Err(ParseError::RelativeUrlWithCannotBeABaseBase)
383
            } else {
384
0
                let scheme_type = SchemeType::from(base_url.scheme());
385
0
                if scheme_type.is_file() {
386
0
                    self.parse_file(input, scheme_type, Some(base_url))
387
                } else {
388
0
                    self.parse_relative(input, scheme_type, base_url)
389
                }
390
            }
391
        } else {
392
0
            Err(ParseError::RelativeUrlWithoutBase)
393
        }
394
0
    }
395
396
0
    pub fn parse_scheme<'i>(&mut self, mut input: Input<'i>) -> Result<Input<'i>, ()> {
397
0
        if input.is_empty() || !input.starts_with(ascii_alpha) {
398
0
            return Err(());
399
0
        }
400
0
        debug_assert!(self.serialization.is_empty());
401
0
        while let Some(c) = input.next() {
402
0
            match c {
403
0
                'a'..='z' | 'A'..='Z' | '0'..='9' | '+' | '-' | '.' => {
404
0
                    self.serialization.push(c.to_ascii_lowercase())
405
                }
406
0
                ':' => return Ok(input),
407
                _ => {
408
0
                    self.serialization.clear();
409
0
                    return Err(());
410
                }
411
            }
412
        }
413
        // EOF before ':'
414
0
        if self.context == Context::Setter {
415
0
            Ok(input)
416
        } else {
417
0
            self.serialization.clear();
418
0
            Err(())
419
        }
420
0
    }
421
422
0
    fn parse_with_scheme(mut self, input: Input<'_>) -> ParseResult<Url> {
423
        use crate::SyntaxViolation::{ExpectedDoubleSlash, ExpectedFileDoubleSlash};
424
0
        let scheme_end = to_u32(self.serialization.len())?;
425
0
        let scheme_type = SchemeType::from(&self.serialization);
426
0
        self.serialization.push(':');
427
0
        match scheme_type {
428
            SchemeType::File => {
429
0
                self.log_violation_if(ExpectedFileDoubleSlash, || !input.starts_with("//"));
430
0
                let base_file_url = self.base_url.and_then(|base| {
431
0
                    if base.scheme() == "file" {
432
0
                        Some(base)
433
                    } else {
434
0
                        None
435
                    }
436
0
                });
437
0
                self.serialization.clear();
438
0
                self.parse_file(input, scheme_type, base_file_url)
439
            }
440
            SchemeType::SpecialNotFile => {
441
                // special relative or authority state
442
0
                let (slashes_count, remaining) = input.count_matching(|c| matches!(c, '/' | '\\'));
443
0
                if let Some(base_url) = self.base_url {
444
0
                    if slashes_count < 2
445
0
                        && base_url.scheme() == &self.serialization[..scheme_end as usize]
446
                    {
447
                        // "Cannot-be-a-base" URLs only happen with "not special" schemes.
448
0
                        debug_assert!(!base_url.cannot_be_a_base());
449
0
                        self.serialization.clear();
450
0
                        return self.parse_relative(input, scheme_type, base_url);
451
0
                    }
452
0
                }
453
                // special authority slashes state
454
0
                self.log_violation_if(ExpectedDoubleSlash, || {
455
0
                    input
456
0
                        .clone()
457
0
                        .take_while(|&c| matches!(c, '/' | '\\'))
458
0
                        .collect::<String>()
459
0
                        != "//"
460
0
                });
461
0
                self.after_double_slash(remaining, scheme_type, scheme_end)
462
            }
463
0
            SchemeType::NotSpecial => self.parse_non_special(input, scheme_type, scheme_end),
464
        }
465
0
    }
466
467
    /// Scheme other than file, http, https, ws, ws, ftp.
468
0
    fn parse_non_special(
469
0
        mut self,
470
0
        input: Input<'_>,
471
0
        scheme_type: SchemeType,
472
0
        scheme_end: u32,
473
0
    ) -> ParseResult<Url> {
474
        // path or authority state (
475
0
        if let Some(input) = input.split_prefix("//") {
476
0
            return self.after_double_slash(input, scheme_type, scheme_end);
477
0
        }
478
        // Anarchist URL (no authority)
479
0
        let path_start = to_u32(self.serialization.len())?;
480
0
        let username_end = path_start;
481
0
        let host_start = path_start;
482
0
        let host_end = path_start;
483
0
        let host = HostInternal::None;
484
0
        let port = None;
485
0
        let remaining = if let Some(input) = input.split_prefix('/') {
486
0
            self.serialization.push('/');
487
0
            self.parse_path(scheme_type, &mut false, path_start as usize, input)
488
        } else {
489
0
            self.parse_cannot_be_a_base_path(input)
490
        };
491
0
        self.with_query_and_fragment(
492
0
            scheme_type,
493
0
            scheme_end,
494
0
            username_end,
495
0
            host_start,
496
0
            host_end,
497
0
            host,
498
0
            port,
499
0
            path_start,
500
0
            remaining,
501
0
        )
502
0
    }
503
504
0
    fn parse_file(
505
0
        mut self,
506
0
        input: Input<'_>,
507
0
        scheme_type: SchemeType,
508
0
        base_file_url: Option<&Url>,
509
0
    ) -> ParseResult<Url> {
510
        use crate::SyntaxViolation::Backslash;
511
        // file state
512
0
        debug_assert!(self.serialization.is_empty());
513
0
        let (first_char, input_after_first_char) = input.split_first();
514
0
        if matches!(first_char, Some('/') | Some('\\')) {
515
0
            self.log_violation_if(SyntaxViolation::Backslash, || first_char == Some('\\'));
516
0
            // file slash state
517
0
            let (next_char, input_after_next_char) = input_after_first_char.split_first();
518
0
            if matches!(next_char, Some('/') | Some('\\')) {
519
0
                self.log_violation_if(Backslash, || next_char == Some('\\'));
520
0
                // file host state
521
0
                self.serialization.push_str("file://");
522
0
                let scheme_end = "file".len() as u32;
523
0
                let host_start = "file://".len() as u32;
524
0
                let (path_start, mut host, remaining) =
525
0
                    self.parse_file_host(input_after_next_char)?;
526
0
                let mut host_end = to_u32(self.serialization.len())?;
527
0
                let mut has_host = !matches!(host, HostInternal::None);
528
0
                let remaining = if path_start {
529
0
                    self.parse_path_start(SchemeType::File, &mut has_host, remaining)
530
                } else {
531
0
                    let path_start = self.serialization.len();
532
0
                    self.serialization.push('/');
533
0
                    self.parse_path(SchemeType::File, &mut has_host, path_start, remaining)
534
                };
535
536
                // For file URLs that have a host and whose path starts
537
                // with the windows drive letter we just remove the host.
538
0
                if !has_host {
539
0
                    self.serialization
540
0
                        .drain(host_start as usize..host_end as usize);
541
0
                    host_end = host_start;
542
0
                    host = HostInternal::None;
543
0
                }
544
0
                let (query_start, fragment_start) =
545
0
                    self.parse_query_and_fragment(scheme_type, scheme_end, remaining)?;
546
0
                return Ok(Url {
547
0
                    serialization: self.serialization,
548
0
                    scheme_end,
549
0
                    username_end: host_start,
550
0
                    host_start,
551
0
                    host_end,
552
0
                    host,
553
0
                    port: None,
554
0
                    path_start: host_end,
555
0
                    query_start,
556
0
                    fragment_start,
557
0
                });
558
            } else {
559
0
                self.serialization.push_str("file://");
560
0
                let scheme_end = "file".len() as u32;
561
0
                let host_start = "file://".len();
562
0
                let mut host_end = host_start;
563
0
                let mut host = HostInternal::None;
564
0
                if !starts_with_windows_drive_letter_segment(&input_after_first_char) {
565
0
                    if let Some(base_url) = base_file_url {
566
0
                        let first_segment = base_url.path_segments().unwrap().next().unwrap();
567
0
                        if is_normalized_windows_drive_letter(first_segment) {
568
0
                            self.serialization.push('/');
569
0
                            self.serialization.push_str(first_segment);
570
0
                        } else if let Some(host_str) = base_url.host_str() {
571
0
                            self.serialization.push_str(host_str);
572
0
                            host_end = self.serialization.len();
573
0
                            host = base_url.host;
574
0
                        }
575
0
                    }
576
0
                }
577
                // If c is the EOF code point, U+002F (/), U+005C (\), U+003F (?), or U+0023 (#), then decrease pointer by one
578
0
                let parse_path_input = if let Some(c) = first_char {
579
0
                    if c == '/' || c == '\\' || c == '?' || c == '#' {
580
0
                        input
581
                    } else {
582
0
                        input_after_first_char
583
                    }
584
                } else {
585
0
                    input_after_first_char
586
                };
587
588
0
                let remaining =
589
0
                    self.parse_path(SchemeType::File, &mut false, host_end, parse_path_input);
590
0
591
0
                let host_start = host_start as u32;
592
593
0
                let (query_start, fragment_start) =
594
0
                    self.parse_query_and_fragment(scheme_type, scheme_end, remaining)?;
595
596
0
                let host_end = host_end as u32;
597
0
                return Ok(Url {
598
0
                    serialization: self.serialization,
599
0
                    scheme_end,
600
0
                    username_end: host_start,
601
0
                    host_start,
602
0
                    host_end,
603
0
                    host,
604
0
                    port: None,
605
0
                    path_start: host_end,
606
0
                    query_start,
607
0
                    fragment_start,
608
0
                });
609
            }
610
0
        }
611
0
        if let Some(base_url) = base_file_url {
612
0
            match first_char {
613
                None => {
614
                    // Copy everything except the fragment
615
0
                    let before_fragment = match base_url.fragment_start {
616
0
                        Some(i) => &base_url.serialization[..i as usize],
617
0
                        None => &*base_url.serialization,
618
                    };
619
0
                    self.serialization.push_str(before_fragment);
620
0
                    Ok(Url {
621
0
                        serialization: self.serialization,
622
0
                        fragment_start: None,
623
0
                        ..*base_url
624
0
                    })
625
                }
626
                Some('?') => {
627
                    // Copy everything up to the query string
628
0
                    let before_query = match (base_url.query_start, base_url.fragment_start) {
629
0
                        (None, None) => &*base_url.serialization,
630
0
                        (Some(i), _) | (None, Some(i)) => base_url.slice(..i),
631
                    };
632
0
                    self.serialization.push_str(before_query);
633
0
                    let (query_start, fragment_start) =
634
0
                        self.parse_query_and_fragment(scheme_type, base_url.scheme_end, input)?;
635
0
                    Ok(Url {
636
0
                        serialization: self.serialization,
637
0
                        query_start,
638
0
                        fragment_start,
639
0
                        ..*base_url
640
0
                    })
641
                }
642
0
                Some('#') => self.fragment_only(base_url, input),
643
                _ => {
644
0
                    if !starts_with_windows_drive_letter_segment(&input) {
645
0
                        let before_query = match (base_url.query_start, base_url.fragment_start) {
646
0
                            (None, None) => &*base_url.serialization,
647
0
                            (Some(i), _) | (None, Some(i)) => base_url.slice(..i),
648
                        };
649
0
                        self.serialization.push_str(before_query);
650
0
                        self.shorten_path(SchemeType::File, base_url.path_start as usize);
651
0
                        let remaining = self.parse_path(
652
0
                            SchemeType::File,
653
0
                            &mut true,
654
0
                            base_url.path_start as usize,
655
0
                            input,
656
0
                        );
657
0
                        self.with_query_and_fragment(
658
0
                            SchemeType::File,
659
0
                            base_url.scheme_end,
660
0
                            base_url.username_end,
661
0
                            base_url.host_start,
662
0
                            base_url.host_end,
663
0
                            base_url.host,
664
0
                            base_url.port,
665
0
                            base_url.path_start,
666
0
                            remaining,
667
0
                        )
668
                    } else {
669
0
                        self.serialization.push_str("file:///");
670
0
                        let scheme_end = "file".len() as u32;
671
0
                        let path_start = "file://".len();
672
0
                        let remaining =
673
0
                            self.parse_path(SchemeType::File, &mut false, path_start, input);
674
0
                        let (query_start, fragment_start) =
675
0
                            self.parse_query_and_fragment(SchemeType::File, scheme_end, remaining)?;
676
0
                        let path_start = path_start as u32;
677
0
                        Ok(Url {
678
0
                            serialization: self.serialization,
679
0
                            scheme_end,
680
0
                            username_end: path_start,
681
0
                            host_start: path_start,
682
0
                            host_end: path_start,
683
0
                            host: HostInternal::None,
684
0
                            port: None,
685
0
                            path_start,
686
0
                            query_start,
687
0
                            fragment_start,
688
0
                        })
689
                    }
690
                }
691
            }
692
        } else {
693
0
            self.serialization.push_str("file:///");
694
0
            let scheme_end = "file".len() as u32;
695
0
            let path_start = "file://".len();
696
0
            let remaining = self.parse_path(SchemeType::File, &mut false, path_start, input);
697
0
            let (query_start, fragment_start) =
698
0
                self.parse_query_and_fragment(SchemeType::File, scheme_end, remaining)?;
699
0
            let path_start = path_start as u32;
700
0
            Ok(Url {
701
0
                serialization: self.serialization,
702
0
                scheme_end,
703
0
                username_end: path_start,
704
0
                host_start: path_start,
705
0
                host_end: path_start,
706
0
                host: HostInternal::None,
707
0
                port: None,
708
0
                path_start,
709
0
                query_start,
710
0
                fragment_start,
711
0
            })
712
        }
713
0
    }
714
715
0
    fn parse_relative(
716
0
        mut self,
717
0
        input: Input<'_>,
718
0
        scheme_type: SchemeType,
719
0
        base_url: &Url,
720
0
    ) -> ParseResult<Url> {
721
0
        // relative state
722
0
        debug_assert!(self.serialization.is_empty());
723
0
        let (first_char, input_after_first_char) = input.split_first();
724
0
        match first_char {
725
            None => {
726
                // Copy everything except the fragment
727
0
                let before_fragment = match base_url.fragment_start {
728
0
                    Some(i) => &base_url.serialization[..i as usize],
729
0
                    None => &*base_url.serialization,
730
                };
731
0
                self.serialization.push_str(before_fragment);
732
0
                Ok(Url {
733
0
                    serialization: self.serialization,
734
0
                    fragment_start: None,
735
0
                    ..*base_url
736
0
                })
737
            }
738
            Some('?') => {
739
                // Copy everything up to the query string
740
0
                let before_query = match (base_url.query_start, base_url.fragment_start) {
741
0
                    (None, None) => &*base_url.serialization,
742
0
                    (Some(i), _) | (None, Some(i)) => base_url.slice(..i),
743
                };
744
0
                self.serialization.push_str(before_query);
745
0
                let (query_start, fragment_start) =
746
0
                    self.parse_query_and_fragment(scheme_type, base_url.scheme_end, input)?;
747
0
                Ok(Url {
748
0
                    serialization: self.serialization,
749
0
                    query_start,
750
0
                    fragment_start,
751
0
                    ..*base_url
752
0
                })
753
            }
754
0
            Some('#') => self.fragment_only(base_url, input),
755
            Some('/') | Some('\\') => {
756
0
                let (slashes_count, remaining) = input.count_matching(|c| matches!(c, '/' | '\\'));
757
0
                if slashes_count >= 2 {
758
0
                    self.log_violation_if(SyntaxViolation::ExpectedDoubleSlash, || {
759
0
                        input
760
0
                            .clone()
761
0
                            .take_while(|&c| matches!(c, '/' | '\\'))
762
0
                            .collect::<String>()
763
0
                            != "//"
764
0
                    });
765
0
                    let scheme_end = base_url.scheme_end;
766
0
                    debug_assert!(base_url.byte_at(scheme_end) == b':');
767
0
                    self.serialization
768
0
                        .push_str(base_url.slice(..scheme_end + 1));
769
0
                    if let Some(after_prefix) = input.split_prefix("//") {
770
0
                        return self.after_double_slash(after_prefix, scheme_type, scheme_end);
771
0
                    }
772
0
                    return self.after_double_slash(remaining, scheme_type, scheme_end);
773
0
                }
774
0
                let path_start = base_url.path_start;
775
0
                self.serialization.push_str(base_url.slice(..path_start));
776
0
                self.serialization.push('/');
777
0
                let remaining = self.parse_path(
778
0
                    scheme_type,
779
0
                    &mut true,
780
0
                    path_start as usize,
781
0
                    input_after_first_char,
782
0
                );
783
0
                self.with_query_and_fragment(
784
0
                    scheme_type,
785
0
                    base_url.scheme_end,
786
0
                    base_url.username_end,
787
0
                    base_url.host_start,
788
0
                    base_url.host_end,
789
0
                    base_url.host,
790
0
                    base_url.port,
791
0
                    base_url.path_start,
792
0
                    remaining,
793
0
                )
794
            }
795
            _ => {
796
0
                let before_query = match (base_url.query_start, base_url.fragment_start) {
797
0
                    (None, None) => &*base_url.serialization,
798
0
                    (Some(i), _) | (None, Some(i)) => base_url.slice(..i),
799
                };
800
0
                self.serialization.push_str(before_query);
801
0
                // FIXME spec says just "remove last entry", not the "pop" algorithm
802
0
                self.pop_path(scheme_type, base_url.path_start as usize);
803
0
                // A special url always has a path.
804
0
                // A path always starts with '/'
805
0
                if self.serialization.len() == base_url.path_start as usize
806
0
                    && (SchemeType::from(base_url.scheme()).is_special() || !input.is_empty())
807
0
                {
808
0
                    self.serialization.push('/');
809
0
                }
810
0
                let remaining = match input.split_first() {
811
0
                    (Some('/'), remaining) => self.parse_path(
812
0
                        scheme_type,
813
0
                        &mut true,
814
0
                        base_url.path_start as usize,
815
0
                        remaining,
816
0
                    ),
817
                    _ => {
818
0
                        self.parse_path(scheme_type, &mut true, base_url.path_start as usize, input)
819
                    }
820
                };
821
0
                self.with_query_and_fragment(
822
0
                    scheme_type,
823
0
                    base_url.scheme_end,
824
0
                    base_url.username_end,
825
0
                    base_url.host_start,
826
0
                    base_url.host_end,
827
0
                    base_url.host,
828
0
                    base_url.port,
829
0
                    base_url.path_start,
830
0
                    remaining,
831
0
                )
832
            }
833
        }
834
0
    }
835
836
0
    fn after_double_slash(
837
0
        mut self,
838
0
        input: Input<'_>,
839
0
        scheme_type: SchemeType,
840
0
        scheme_end: u32,
841
0
    ) -> ParseResult<Url> {
842
0
        self.serialization.push('/');
843
0
        self.serialization.push('/');
844
0
        // authority state
845
0
        let before_authority = self.serialization.len();
846
0
        let (username_end, remaining) = self.parse_userinfo(input, scheme_type)?;
847
0
        let has_authority = before_authority != self.serialization.len();
848
        // host state
849
0
        let host_start = to_u32(self.serialization.len())?;
850
0
        let (host_end, host, port, remaining) =
851
0
            self.parse_host_and_port(remaining, scheme_end, scheme_type)?;
852
0
        if host == HostInternal::None && has_authority {
853
0
            return Err(ParseError::EmptyHost);
854
0
        }
855
        // path state
856
0
        let path_start = to_u32(self.serialization.len())?;
857
0
        let remaining = self.parse_path_start(scheme_type, &mut true, remaining);
858
0
        self.with_query_and_fragment(
859
0
            scheme_type,
860
0
            scheme_end,
861
0
            username_end,
862
0
            host_start,
863
0
            host_end,
864
0
            host,
865
0
            port,
866
0
            path_start,
867
0
            remaining,
868
0
        )
869
0
    }
870
871
    /// Return (username_end, remaining)
872
0
    fn parse_userinfo<'i>(
873
0
        &mut self,
874
0
        mut input: Input<'i>,
875
0
        scheme_type: SchemeType,
876
0
    ) -> ParseResult<(u32, Input<'i>)> {
877
0
        let mut last_at = None;
878
0
        let mut remaining = input.clone();
879
0
        let mut char_count = 0;
880
0
        while let Some(c) = remaining.next() {
881
0
            match c {
882
                '@' => {
883
0
                    if last_at.is_some() {
884
0
                        self.log_violation(SyntaxViolation::UnencodedAtSign)
885
                    } else {
886
0
                        self.log_violation(SyntaxViolation::EmbeddedCredentials)
887
                    }
888
0
                    last_at = Some((char_count, remaining.clone()))
889
                }
890
0
                '/' | '?' | '#' => break,
891
0
                '\\' if scheme_type.is_special() => break,
892
0
                _ => (),
893
            }
894
0
            char_count += 1;
895
        }
896
0
        let (mut userinfo_char_count, remaining) = match last_at {
897
0
            None => return Ok((to_u32(self.serialization.len())?, input)),
898
0
            Some((0, remaining)) => {
899
                // Otherwise, if one of the following is true
900
                // c is the EOF code point, U+002F (/), U+003F (?), or U+0023 (#)
901
                // url is special and c is U+005C (\)
902
                // If @ flag is set and buffer is the empty string, validation error, return failure.
903
0
                if let (Some(c), _) = remaining.split_first() {
904
0
                    if c == '/' || c == '?' || c == '#' || (scheme_type.is_special() && c == '\\') {
905
0
                        return Err(ParseError::EmptyHost);
906
0
                    }
907
0
                }
908
0
                return Ok((to_u32(self.serialization.len())?, remaining));
909
            }
910
0
            Some(x) => x,
911
0
        };
912
0
913
0
        let mut username_end = None;
914
0
        let mut has_password = false;
915
0
        let mut has_username = false;
916
0
        while userinfo_char_count > 0 {
917
0
            let (c, utf8_c) = input.next_utf8().unwrap();
918
0
            userinfo_char_count -= 1;
919
0
            if c == ':' && username_end.is_none() {
920
                // Start parsing password
921
0
                username_end = Some(to_u32(self.serialization.len())?);
922
                // We don't add a colon if the password is empty
923
0
                if userinfo_char_count > 0 {
924
0
                    self.serialization.push(':');
925
0
                    has_password = true;
926
0
                }
927
            } else {
928
0
                if !has_password {
929
0
                    has_username = true;
930
0
                }
931
0
                self.check_url_code_point(c, &input);
932
0
                self.serialization
933
0
                    .extend(utf8_percent_encode(utf8_c, USERINFO));
934
            }
935
        }
936
0
        let username_end = match username_end {
937
0
            Some(i) => i,
938
0
            None => to_u32(self.serialization.len())?,
939
        };
940
0
        if has_username || has_password {
941
0
            self.serialization.push('@');
942
0
        }
943
0
        Ok((username_end, remaining))
944
0
    }
945
946
0
    fn parse_host_and_port<'i>(
947
0
        &mut self,
948
0
        input: Input<'i>,
949
0
        scheme_end: u32,
950
0
        scheme_type: SchemeType,
951
0
    ) -> ParseResult<(u32, HostInternal, Option<u16>, Input<'i>)> {
952
0
        let (host, remaining) = Parser::parse_host(input, scheme_type)?;
953
0
        write!(&mut self.serialization, "{}", host).unwrap();
954
0
        let host_end = to_u32(self.serialization.len())?;
955
0
        if let Host::Domain(h) = &host {
956
0
            if h.is_empty() {
957
                // Port with an empty host
958
0
                if remaining.starts_with(":") {
959
0
                    return Err(ParseError::EmptyHost);
960
0
                }
961
0
                if scheme_type.is_special() {
962
0
                    return Err(ParseError::EmptyHost);
963
0
                }
964
0
            }
965
0
        };
966
967
0
        let (port, remaining) = if let Some(remaining) = remaining.split_prefix(':') {
968
0
            let scheme = || default_port(&self.serialization[..scheme_end as usize]);
969
0
            Parser::parse_port(remaining, scheme, self.context)?
970
        } else {
971
0
            (None, remaining)
972
        };
973
0
        if let Some(port) = port {
974
0
            write!(&mut self.serialization, ":{}", port).unwrap()
975
0
        }
976
0
        Ok((host_end, host.into(), port, remaining))
977
0
    }
978
979
0
    pub fn parse_host(
980
0
        mut input: Input<'_>,
981
0
        scheme_type: SchemeType,
982
0
    ) -> ParseResult<(Host<String>, Input<'_>)> {
983
0
        if scheme_type.is_file() {
984
0
            return Parser::get_file_host(input);
985
0
        }
986
0
        // Undo the Input abstraction here to avoid allocating in the common case
987
0
        // where the host part of the input does not contain any tab or newline
988
0
        let input_str = input.chars.as_str();
989
0
        let mut inside_square_brackets = false;
990
0
        let mut has_ignored_chars = false;
991
0
        let mut non_ignored_chars = 0;
992
0
        let mut bytes = 0;
993
0
        for c in input_str.chars() {
994
0
            match c {
995
0
                ':' if !inside_square_brackets => break,
996
0
                '\\' if scheme_type.is_special() => break,
997
0
                '/' | '?' | '#' => break,
998
0
                '\t' | '\n' | '\r' => {
999
0
                    has_ignored_chars = true;
1000
0
                }
1001
                '[' => {
1002
0
                    inside_square_brackets = true;
1003
0
                    non_ignored_chars += 1
1004
                }
1005
                ']' => {
1006
0
                    inside_square_brackets = false;
1007
0
                    non_ignored_chars += 1
1008
                }
1009
0
                _ => non_ignored_chars += 1,
1010
            }
1011
0
            bytes += c.len_utf8();
1012
        }
1013
        let replaced: String;
1014
        let host_str;
1015
        {
1016
0
            let host_input = input.by_ref().take(non_ignored_chars);
1017
0
            if has_ignored_chars {
1018
0
                replaced = host_input.collect();
1019
0
                host_str = &*replaced
1020
            } else {
1021
0
                for _ in host_input {}
1022
0
                host_str = &input_str[..bytes]
1023
            }
1024
        }
1025
0
        if scheme_type == SchemeType::SpecialNotFile && host_str.is_empty() {
1026
0
            return Err(ParseError::EmptyHost);
1027
0
        }
1028
0
        if !scheme_type.is_special() {
1029
0
            let host = Host::parse_opaque(host_str)?;
1030
0
            return Ok((host, input));
1031
0
        }
1032
0
        let host = Host::parse(host_str)?;
1033
0
        Ok((host, input))
1034
0
    }
1035
1036
0
    fn get_file_host(input: Input<'_>) -> ParseResult<(Host<String>, Input<'_>)> {
1037
0
        let (_, host_str, remaining) = Parser::file_host(input)?;
1038
0
        let host = match Host::parse(&host_str)? {
1039
0
            Host::Domain(ref d) if d == "localhost" => Host::Domain("".to_string()),
1040
0
            host => host,
1041
        };
1042
0
        Ok((host, remaining))
1043
0
    }
1044
1045
0
    fn parse_file_host<'i>(
1046
0
        &mut self,
1047
0
        input: Input<'i>,
1048
0
    ) -> ParseResult<(bool, HostInternal, Input<'i>)> {
1049
        let has_host;
1050
0
        let (_, host_str, remaining) = Parser::file_host(input)?;
1051
0
        let host = if host_str.is_empty() {
1052
0
            has_host = false;
1053
0
            HostInternal::None
1054
        } else {
1055
0
            match Host::parse(&host_str)? {
1056
0
                Host::Domain(ref d) if d == "localhost" => {
1057
0
                    has_host = false;
1058
0
                    HostInternal::None
1059
                }
1060
0
                host => {
1061
0
                    write!(&mut self.serialization, "{}", host).unwrap();
1062
0
                    has_host = true;
1063
0
                    host.into()
1064
                }
1065
            }
1066
        };
1067
0
        Ok((has_host, host, remaining))
1068
0
    }
1069
1070
0
    pub fn file_host(input: Input) -> ParseResult<(bool, String, Input)> {
1071
0
        // Undo the Input abstraction here to avoid allocating in the common case
1072
0
        // where the host part of the input does not contain any tab or newline
1073
0
        let input_str = input.chars.as_str();
1074
0
        let mut has_ignored_chars = false;
1075
0
        let mut non_ignored_chars = 0;
1076
0
        let mut bytes = 0;
1077
0
        for c in input_str.chars() {
1078
0
            match c {
1079
0
                '/' | '\\' | '?' | '#' => break,
1080
0
                '\t' | '\n' | '\r' => has_ignored_chars = true,
1081
0
                _ => non_ignored_chars += 1,
1082
            }
1083
0
            bytes += c.len_utf8();
1084
        }
1085
        let replaced: String;
1086
        let host_str;
1087
0
        let mut remaining = input.clone();
1088
0
        {
1089
0
            let host_input = remaining.by_ref().take(non_ignored_chars);
1090
0
            if has_ignored_chars {
1091
0
                replaced = host_input.collect();
1092
0
                host_str = &*replaced
1093
            } else {
1094
0
                for _ in host_input {}
1095
0
                host_str = &input_str[..bytes]
1096
            }
1097
        }
1098
0
        if is_windows_drive_letter(host_str) {
1099
0
            return Ok((false, "".to_string(), input));
1100
0
        }
1101
0
        Ok((true, host_str.to_string(), remaining))
1102
0
    }
1103
1104
0
    pub fn parse_port<P>(
1105
0
        mut input: Input<'_>,
1106
0
        default_port: P,
1107
0
        context: Context,
1108
0
    ) -> ParseResult<(Option<u16>, Input<'_>)>
1109
0
    where
1110
0
        P: Fn() -> Option<u16>,
1111
0
    {
1112
0
        let mut port: u32 = 0;
1113
0
        let mut has_any_digit = false;
1114
0
        while let (Some(c), remaining) = input.split_first() {
1115
0
            if let Some(digit) = c.to_digit(10) {
1116
0
                port = port * 10 + digit;
1117
0
                if port > u16::MAX as u32 {
1118
0
                    return Err(ParseError::InvalidPort);
1119
0
                }
1120
0
                has_any_digit = true;
1121
0
            } else if context == Context::UrlParser && !matches!(c, '/' | '\\' | '?' | '#') {
1122
0
                return Err(ParseError::InvalidPort);
1123
            } else {
1124
0
                break;
1125
            }
1126
0
            input = remaining;
1127
        }
1128
1129
0
        if !has_any_digit && context == Context::Setter && !input.is_empty() {
1130
0
            return Err(ParseError::InvalidPort);
1131
0
        }
1132
0
1133
0
        let mut opt_port = Some(port as u16);
1134
0
        if !has_any_digit || opt_port == default_port() {
1135
0
            opt_port = None;
1136
0
        }
1137
0
        Ok((opt_port, input))
1138
0
    }
Unexecuted instantiation: <url::parser::Parser>::parse_port::<<url::parser::Parser>::parse_host_and_port::{closure#0}>
Unexecuted instantiation: <url::parser::Parser>::parse_port::<url::quirks::set_host::{closure#0}>
Unexecuted instantiation: <url::parser::Parser>::parse_port::<url::quirks::set_port::{closure#0}>
1139
1140
0
    pub fn parse_path_start<'i>(
1141
0
        &mut self,
1142
0
        scheme_type: SchemeType,
1143
0
        has_host: &mut bool,
1144
0
        input: Input<'i>,
1145
0
    ) -> Input<'i> {
1146
0
        let path_start = self.serialization.len();
1147
0
        let (maybe_c, remaining) = input.split_first();
1148
0
        // If url is special, then:
1149
0
        if scheme_type.is_special() {
1150
0
            if maybe_c == Some('\\') {
1151
0
                // If c is U+005C (\), validation error.
1152
0
                self.log_violation(SyntaxViolation::Backslash);
1153
0
            }
1154
            // A special URL always has a non-empty path.
1155
0
            if !self.serialization.ends_with('/') {
1156
0
                self.serialization.push('/');
1157
0
                // We have already made sure the forward slash is present.
1158
0
                if maybe_c == Some('/') || maybe_c == Some('\\') {
1159
0
                    return self.parse_path(scheme_type, has_host, path_start, remaining);
1160
0
                }
1161
0
            }
1162
0
            return self.parse_path(scheme_type, has_host, path_start, input);
1163
0
        } else if maybe_c == Some('?') || maybe_c == Some('#') {
1164
            // Otherwise, if state override is not given and c is U+003F (?),
1165
            // set url’s query to the empty string and state to query state.
1166
            // Otherwise, if state override is not given and c is U+0023 (#),
1167
            // set url’s fragment to the empty string and state to fragment state.
1168
            // The query and path states will be handled by the caller.
1169
0
            return input;
1170
0
        }
1171
0
1172
0
        if maybe_c.is_some() && maybe_c != Some('/') {
1173
0
            self.serialization.push('/');
1174
0
        }
1175
        // Otherwise, if c is not the EOF code point:
1176
0
        self.parse_path(scheme_type, has_host, path_start, input)
1177
0
    }
1178
1179
0
    pub fn parse_path<'i>(
1180
0
        &mut self,
1181
0
        scheme_type: SchemeType,
1182
0
        has_host: &mut bool,
1183
0
        path_start: usize,
1184
0
        mut input: Input<'i>,
1185
0
    ) -> Input<'i> {
1186
        // Relative path state
1187
        loop {
1188
0
            let mut segment_start = self.serialization.len();
1189
0
            let mut ends_with_slash = false;
1190
            loop {
1191
0
                let input_before_c = input.clone();
1192
0
                let (c, utf8_c) = if let Some(x) = input.next_utf8() {
1193
0
                    x
1194
                } else {
1195
0
                    break;
1196
                };
1197
0
                match c {
1198
0
                    '/' if self.context != Context::PathSegmentSetter => {
1199
0
                        self.serialization.push(c);
1200
0
                        ends_with_slash = true;
1201
0
                        break;
1202
                    }
1203
0
                    '\\' if self.context != Context::PathSegmentSetter
1204
0
                        && scheme_type.is_special() =>
1205
0
                    {
1206
0
                        self.log_violation(SyntaxViolation::Backslash);
1207
0
                        self.serialization.push('/');
1208
0
                        ends_with_slash = true;
1209
0
                        break;
1210
                    }
1211
0
                    '?' | '#' if self.context == Context::UrlParser => {
1212
0
                        input = input_before_c;
1213
0
                        break;
1214
                    }
1215
                    _ => {
1216
0
                        self.check_url_code_point(c, &input);
1217
0
                        if scheme_type.is_file()
1218
0
                            && self.serialization.len() > path_start
1219
0
                            && is_normalized_windows_drive_letter(
1220
0
                                &self.serialization[path_start + 1..],
1221
0
                            )
1222
0
                        {
1223
0
                            self.serialization.push('/');
1224
0
                            segment_start += 1;
1225
0
                        }
1226
0
                        if self.context == Context::PathSegmentSetter {
1227
0
                            if scheme_type.is_special() {
1228
0
                                self.serialization
1229
0
                                    .extend(utf8_percent_encode(utf8_c, SPECIAL_PATH_SEGMENT));
1230
0
                            } else {
1231
0
                                self.serialization
1232
0
                                    .extend(utf8_percent_encode(utf8_c, PATH_SEGMENT));
1233
0
                            }
1234
0
                        } else {
1235
0
                            self.serialization.extend(utf8_percent_encode(utf8_c, PATH));
1236
0
                        }
1237
                    }
1238
                }
1239
            }
1240
0
            let segment_before_slash = if ends_with_slash {
1241
0
                &self.serialization[segment_start..self.serialization.len() - 1]
1242
            } else {
1243
0
                &self.serialization[segment_start..self.serialization.len()]
1244
            };
1245
0
            match segment_before_slash {
1246
0
                // If buffer is a double-dot path segment, shorten url’s path,
1247
0
                ".." | "%2e%2e" | "%2e%2E" | "%2E%2e" | "%2E%2E" | "%2e." | "%2E." | ".%2e"
1248
0
                | ".%2E" => {
1249
0
                    debug_assert!(self.serialization.as_bytes()[segment_start - 1] == b'/');
1250
0
                    self.serialization.truncate(segment_start);
1251
0
                    if self.serialization.ends_with('/')
1252
0
                        && Parser::last_slash_can_be_removed(&self.serialization, path_start)
1253
0
                    {
1254
0
                        self.serialization.pop();
1255
0
                    }
1256
0
                    self.shorten_path(scheme_type, path_start);
1257
0
1258
0
                    // and then if neither c is U+002F (/), nor url is special and c is U+005C (\), append the empty string to url’s path.
1259
0
                    if ends_with_slash && !self.serialization.ends_with('/') {
1260
0
                        self.serialization.push('/');
1261
0
                    }
1262
                }
1263
                // Otherwise, if buffer is a single-dot path segment and if neither c is U+002F (/),
1264
                // nor url is special and c is U+005C (\), append the empty string to url’s path.
1265
0
                "." | "%2e" | "%2E" => {
1266
0
                    self.serialization.truncate(segment_start);
1267
0
                    if !self.serialization.ends_with('/') {
1268
0
                        self.serialization.push('/');
1269
0
                    }
1270
                }
1271
                _ => {
1272
                    // If url’s scheme is "file", url’s path is empty, and buffer is a Windows drive letter, then
1273
0
                    if scheme_type.is_file()
1274
0
                        && segment_start == path_start + 1
1275
0
                        && is_windows_drive_letter(segment_before_slash)
1276
                    {
1277
                        // Replace the second code point in buffer with U+003A (:).
1278
0
                        if let Some(c) = segment_before_slash.chars().next() {
1279
0
                            self.serialization.truncate(segment_start);
1280
0
                            self.serialization.push(c);
1281
0
                            self.serialization.push(':');
1282
0
                            if ends_with_slash {
1283
0
                                self.serialization.push('/');
1284
0
                            }
1285
0
                        }
1286
                        // If url’s host is neither the empty string nor null,
1287
                        // validation error, set url’s host to the empty string.
1288
0
                        if *has_host {
1289
0
                            self.log_violation(SyntaxViolation::FileWithHostAndWindowsDrive);
1290
0
                            *has_host = false; // FIXME account for this in callers
1291
0
                        }
1292
0
                    }
1293
                }
1294
            }
1295
0
            if !ends_with_slash {
1296
0
                break;
1297
0
            }
1298
        }
1299
0
        if scheme_type.is_file() {
1300
0
            // while url’s path’s size is greater than 1
1301
0
            // and url’s path[0] is the empty string,
1302
0
            // validation error, remove the first item from url’s path.
1303
0
            //FIXME: log violation
1304
0
            let path = self.serialization.split_off(path_start);
1305
0
            self.serialization.push('/');
1306
0
            self.serialization.push_str(path.trim_start_matches('/'));
1307
0
        }
1308
1309
0
        input
1310
0
    }
1311
1312
0
    fn last_slash_can_be_removed(serialization: &str, path_start: usize) -> bool {
1313
0
        let url_before_segment = &serialization[..serialization.len() - 1];
1314
0
        if let Some(segment_before_start) = url_before_segment.rfind('/') {
1315
            // Do not remove the root slash
1316
0
            segment_before_start >= path_start
1317
                // Or a windows drive letter slash
1318
0
                && !path_starts_with_windows_drive_letter(&serialization[segment_before_start..])
1319
        } else {
1320
0
            false
1321
        }
1322
0
    }
1323
1324
    /// https://url.spec.whatwg.org/#shorten-a-urls-path
1325
0
    fn shorten_path(&mut self, scheme_type: SchemeType, path_start: usize) {
1326
0
        // If path is empty, then return.
1327
0
        if self.serialization.len() == path_start {
1328
0
            return;
1329
0
        }
1330
0
        // If url’s scheme is "file", path’s size is 1, and path[0] is a normalized Windows drive letter, then return.
1331
0
        if scheme_type.is_file()
1332
0
            && is_normalized_windows_drive_letter(&self.serialization[path_start..])
1333
        {
1334
0
            return;
1335
0
        }
1336
0
        // Remove path’s last item.
1337
0
        self.pop_path(scheme_type, path_start);
1338
0
    }
1339
1340
    /// https://url.spec.whatwg.org/#pop-a-urls-path
1341
0
    fn pop_path(&mut self, scheme_type: SchemeType, path_start: usize) {
1342
0
        if self.serialization.len() > path_start {
1343
0
            let slash_position = self.serialization[path_start..].rfind('/').unwrap();
1344
0
            // + 1 since rfind returns the position before the slash.
1345
0
            let segment_start = path_start + slash_position + 1;
1346
0
            // Don’t pop a Windows drive letter
1347
0
            if !(scheme_type.is_file()
1348
0
                && is_normalized_windows_drive_letter(&self.serialization[segment_start..]))
1349
0
            {
1350
0
                self.serialization.truncate(segment_start);
1351
0
            }
1352
0
        }
1353
0
    }
1354
1355
0
    pub fn parse_cannot_be_a_base_path<'i>(&mut self, mut input: Input<'i>) -> Input<'i> {
1356
        loop {
1357
0
            let input_before_c = input.clone();
1358
0
            match input.next_utf8() {
1359
0
                Some(('?', _)) | Some(('#', _)) if self.context == Context::UrlParser => {
1360
0
                    return input_before_c
1361
                }
1362
0
                Some((c, utf8_c)) => {
1363
0
                    self.check_url_code_point(c, &input);
1364
0
                    self.serialization
1365
0
                        .extend(utf8_percent_encode(utf8_c, CONTROLS));
1366
0
                }
1367
0
                None => return input,
1368
            }
1369
        }
1370
0
    }
1371
1372
    #[allow(clippy::too_many_arguments)]
1373
0
    fn with_query_and_fragment(
1374
0
        mut self,
1375
0
        scheme_type: SchemeType,
1376
0
        scheme_end: u32,
1377
0
        username_end: u32,
1378
0
        host_start: u32,
1379
0
        host_end: u32,
1380
0
        host: HostInternal,
1381
0
        port: Option<u16>,
1382
0
        mut path_start: u32,
1383
0
        remaining: Input<'_>,
1384
0
    ) -> ParseResult<Url> {
1385
0
        // Special case for anarchist URL's with a leading empty path segment
1386
0
        // This prevents web+demo:/.//not-a-host/ or web+demo:/path/..//not-a-host/,
1387
0
        // when parsed and then serialized, from ending up as web+demo://not-a-host/
1388
0
        // (they end up as web+demo:/.//not-a-host/).
1389
0
        //
1390
0
        // If url’s host is null, url does not have an opaque path,
1391
0
        // url’s path’s size is greater than 1, and url’s path[0] is the empty string,
1392
0
        // then append U+002F (/) followed by U+002E (.) to output.
1393
0
        let scheme_end_as_usize = scheme_end as usize;
1394
0
        let path_start_as_usize = path_start as usize;
1395
0
        if path_start_as_usize == scheme_end_as_usize + 1 {
1396
            // Anarchist URL
1397
0
            if self.serialization[path_start_as_usize..].starts_with("//") {
1398
0
                // Case 1: The base URL did not have an empty path segment, but the resulting one does
1399
0
                // Insert the "/." prefix
1400
0
                self.serialization.insert_str(path_start_as_usize, "/.");
1401
0
                path_start += 2;
1402
0
            }
1403
0
            assert!(!self.serialization[scheme_end_as_usize..].starts_with("://"));
1404
0
        } else if path_start_as_usize == scheme_end_as_usize + 3
1405
0
            && &self.serialization[scheme_end_as_usize..path_start_as_usize] == ":/."
1406
        {
1407
            // Anarchist URL with leading empty path segment
1408
            // The base URL has a "/." between the host and the path
1409
0
            assert_eq!(self.serialization.as_bytes()[path_start_as_usize], b'/');
1410
0
            if self
1411
0
                .serialization
1412
0
                .as_bytes()
1413
0
                .get(path_start_as_usize + 1)
1414
0
                .copied()
1415
0
                != Some(b'/')
1416
0
            {
1417
0
                // Case 2: The base URL had an empty path segment, but the resulting one does not
1418
0
                // Remove the "/." prefix
1419
0
                self.serialization
1420
0
                    .replace_range(scheme_end_as_usize..path_start_as_usize, ":");
1421
0
                path_start -= 2;
1422
0
            }
1423
0
            assert!(!self.serialization[scheme_end_as_usize..].starts_with("://"));
1424
0
        }
1425
1426
0
        let (query_start, fragment_start) =
1427
0
            self.parse_query_and_fragment(scheme_type, scheme_end, remaining)?;
1428
0
        Ok(Url {
1429
0
            serialization: self.serialization,
1430
0
            scheme_end,
1431
0
            username_end,
1432
0
            host_start,
1433
0
            host_end,
1434
0
            host,
1435
0
            port,
1436
0
            path_start,
1437
0
            query_start,
1438
0
            fragment_start,
1439
0
        })
1440
0
    }
1441
1442
    /// Return (query_start, fragment_start)
1443
0
    fn parse_query_and_fragment(
1444
0
        &mut self,
1445
0
        scheme_type: SchemeType,
1446
0
        scheme_end: u32,
1447
0
        mut input: Input<'_>,
1448
0
    ) -> ParseResult<(Option<u32>, Option<u32>)> {
1449
0
        let mut query_start = None;
1450
0
        match input.next() {
1451
0
            Some('#') => {}
1452
            Some('?') => {
1453
0
                query_start = Some(to_u32(self.serialization.len())?);
1454
0
                self.serialization.push('?');
1455
0
                let remaining = self.parse_query(scheme_type, scheme_end, input);
1456
0
                if let Some(remaining) = remaining {
1457
0
                    input = remaining
1458
                } else {
1459
0
                    return Ok((query_start, None));
1460
                }
1461
            }
1462
0
            None => return Ok((None, None)),
1463
0
            _ => panic!("Programming error. parse_query_and_fragment() called without ? or #"),
1464
        }
1465
1466
0
        let fragment_start = to_u32(self.serialization.len())?;
1467
0
        self.serialization.push('#');
1468
0
        self.parse_fragment(input);
1469
0
        Ok((query_start, Some(fragment_start)))
1470
0
    }
1471
1472
0
    pub fn parse_query<'i>(
1473
0
        &mut self,
1474
0
        scheme_type: SchemeType,
1475
0
        scheme_end: u32,
1476
0
        mut input: Input<'i>,
1477
0
    ) -> Option<Input<'i>> {
1478
0
        let len = input.chars.as_str().len();
1479
0
        let mut query = String::with_capacity(len); // FIXME: use a streaming decoder instead
1480
0
        let mut remaining = None;
1481
0
        while let Some(c) = input.next() {
1482
0
            if c == '#' && self.context == Context::UrlParser {
1483
0
                remaining = Some(input);
1484
0
                break;
1485
0
            } else {
1486
0
                self.check_url_code_point(c, &input);
1487
0
                query.push(c);
1488
0
            }
1489
        }
1490
1491
0
        let encoding = match &self.serialization[..scheme_end as usize] {
1492
0
            "http" | "https" | "file" | "ftp" => self.query_encoding_override,
1493
0
            _ => None,
1494
        };
1495
0
        let query_bytes = if let Some(o) = encoding {
1496
0
            o(&query)
1497
        } else {
1498
0
            query.as_bytes().into()
1499
        };
1500
0
        let set = if scheme_type.is_special() {
1501
0
            SPECIAL_QUERY
1502
        } else {
1503
0
            QUERY
1504
        };
1505
0
        self.serialization.extend(percent_encode(&query_bytes, set));
1506
0
        remaining
1507
0
    }
1508
1509
0
    fn fragment_only(mut self, base_url: &Url, mut input: Input<'_>) -> ParseResult<Url> {
1510
0
        let before_fragment = match base_url.fragment_start {
1511
0
            Some(i) => base_url.slice(..i),
1512
0
            None => &*base_url.serialization,
1513
        };
1514
0
        debug_assert!(self.serialization.is_empty());
1515
0
        self.serialization
1516
0
            .reserve(before_fragment.len() + input.chars.as_str().len());
1517
0
        self.serialization.push_str(before_fragment);
1518
0
        self.serialization.push('#');
1519
0
        let next = input.next();
1520
0
        debug_assert!(next == Some('#'));
1521
0
        self.parse_fragment(input);
1522
0
        Ok(Url {
1523
0
            serialization: self.serialization,
1524
0
            fragment_start: Some(to_u32(before_fragment.len())?),
1525
            ..*base_url
1526
        })
1527
0
    }
1528
1529
0
    pub fn parse_fragment(&mut self, mut input: Input<'_>) {
1530
0
        while let Some((c, utf8_c)) = input.next_utf8() {
1531
0
            if c == '\0' {
1532
0
                self.log_violation(SyntaxViolation::NullInFragment)
1533
0
            } else {
1534
0
                self.check_url_code_point(c, &input);
1535
0
            }
1536
0
            self.serialization
1537
0
                .extend(utf8_percent_encode(utf8_c, FRAGMENT));
1538
        }
1539
0
    }
1540
1541
0
    fn check_url_code_point(&self, c: char, input: &Input<'_>) {
1542
0
        if let Some(vfn) = self.violation_fn {
1543
0
            if c == '%' {
1544
0
                let mut input = input.clone();
1545
0
                if !matches!((input.next(), input.next()), (Some(a), Some(b))
1546
0
                             if a.is_ascii_hexdigit() && b.is_ascii_hexdigit())
1547
                {
1548
0
                    vfn(SyntaxViolation::PercentDecode)
1549
0
                }
1550
0
            } else if !is_url_code_point(c) {
1551
0
                vfn(SyntaxViolation::NonUrlCodePoint)
1552
0
            }
1553
0
        }
1554
0
    }
1555
}
1556
1557
// Non URL code points:
1558
// U+0000 to U+0020 (space)
1559
// " # % < > [ \ ] ^ ` { | }
1560
// U+007F to U+009F
1561
// surrogates
1562
// U+FDD0 to U+FDEF
1563
// Last two of each plane: U+__FFFE to U+__FFFF for __ in 00 to 10 hex
1564
#[inline]
1565
0
fn is_url_code_point(c: char) -> bool {
1566
0
    matches!(c,
1567
0
        'a'..='z' |
1568
0
        'A'..='Z' |
1569
0
        '0'..='9' |
1570
        '!' | '$' | '&' | '\'' | '(' | ')' | '*' | '+' | ',' | '-' |
1571
        '.' | '/' | ':' | ';' | '=' | '?' | '@' | '_' | '~' |
1572
0
        '\u{A0}'..='\u{D7FF}' | '\u{E000}'..='\u{FDCF}' | '\u{FDF0}'..='\u{FFFD}' |
1573
0
        '\u{10000}'..='\u{1FFFD}' | '\u{20000}'..='\u{2FFFD}' |
1574
0
        '\u{30000}'..='\u{3FFFD}' | '\u{40000}'..='\u{4FFFD}' |
1575
0
        '\u{50000}'..='\u{5FFFD}' | '\u{60000}'..='\u{6FFFD}' |
1576
0
        '\u{70000}'..='\u{7FFFD}' | '\u{80000}'..='\u{8FFFD}' |
1577
0
        '\u{90000}'..='\u{9FFFD}' | '\u{A0000}'..='\u{AFFFD}' |
1578
0
        '\u{B0000}'..='\u{BFFFD}' | '\u{C0000}'..='\u{CFFFD}' |
1579
0
        '\u{D0000}'..='\u{DFFFD}' | '\u{E1000}'..='\u{EFFFD}' |
1580
0
        '\u{F0000}'..='\u{FFFFD}' | '\u{100000}'..='\u{10FFFD}')
1581
0
}
1582
1583
/// https://url.spec.whatwg.org/#c0-controls-and-space
1584
#[inline]
1585
0
fn c0_control_or_space(ch: char) -> bool {
1586
0
    ch <= ' ' // U+0000 to U+0020
1587
0
}
1588
1589
/// https://infra.spec.whatwg.org/#ascii-tab-or-newline
1590
#[inline]
1591
0
fn ascii_tab_or_new_line(ch: char) -> bool {
1592
0
    matches!(ch, '\t' | '\r' | '\n')
1593
0
}
1594
1595
/// https://url.spec.whatwg.org/#ascii-alpha
1596
#[inline]
1597
0
pub fn ascii_alpha(ch: char) -> bool {
1598
0
    ch.is_ascii_alphabetic()
1599
0
}
1600
1601
#[inline]
1602
0
pub fn to_u32(i: usize) -> ParseResult<u32> {
1603
0
    if i <= u32::MAX as usize {
1604
0
        Ok(i as u32)
1605
    } else {
1606
0
        Err(ParseError::Overflow)
1607
    }
1608
0
}
1609
1610
0
fn is_normalized_windows_drive_letter(segment: &str) -> bool {
1611
0
    is_windows_drive_letter(segment) && segment.as_bytes()[1] == b':'
1612
0
}
1613
1614
/// Whether the scheme is file:, the path has a single segment, and that segment
1615
/// is a Windows drive letter
1616
#[inline]
1617
0
pub fn is_windows_drive_letter(segment: &str) -> bool {
1618
0
    segment.len() == 2 && starts_with_windows_drive_letter(segment)
1619
0
}
1620
1621
/// Whether path starts with a root slash
1622
/// and a windows drive letter eg: "/c:" or "/a:/"
1623
0
fn path_starts_with_windows_drive_letter(s: &str) -> bool {
1624
0
    if let Some(c) = s.as_bytes().first() {
1625
0
        matches!(c, b'/' | b'\\' | b'?' | b'#') && starts_with_windows_drive_letter(&s[1..])
1626
    } else {
1627
0
        false
1628
    }
1629
0
}
1630
1631
0
fn starts_with_windows_drive_letter(s: &str) -> bool {
1632
0
    s.len() >= 2
1633
0
        && ascii_alpha(s.as_bytes()[0] as char)
1634
0
        && matches!(s.as_bytes()[1], b':' | b'|')
1635
0
        && (s.len() == 2 || matches!(s.as_bytes()[2], b'/' | b'\\' | b'?' | b'#'))
1636
0
}
1637
1638
/// https://url.spec.whatwg.org/#start-with-a-windows-drive-letter
1639
0
fn starts_with_windows_drive_letter_segment(input: &Input<'_>) -> bool {
1640
0
    let mut input = input.clone();
1641
0
    match (input.next(), input.next(), input.next()) {
1642
        // its first two code points are a Windows drive letter
1643
        // its third code point is U+002F (/), U+005C (\), U+003F (?), or U+0023 (#).
1644
0
        (Some(a), Some(b), Some(c))
1645
0
            if ascii_alpha(a) && matches!(b, ':' | '|') && matches!(c, '/' | '\\' | '?' | '#') =>
1646
0
        {
1647
0
            true
1648
        }
1649
        // its first two code points are a Windows drive letter
1650
        // its length is 2
1651
0
        (Some(a), Some(b), None) if ascii_alpha(a) && matches!(b, ':' | '|') => true,
1652
0
        _ => false,
1653
    }
1654
0
}