parser.rs 59 KB

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