parser.rs 59 KB

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