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