url/
parser.rs

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