You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1203 lines
29 KiB

  1. "use strict";
  2. const punycode = require("punycode");
  3. const tr46 = require("tr46");
  4. const infra = require("./infra");
  5. const { utf8DecodeWithoutBOM } = require("./encoding");
  6. const { percentDecodeString, utf8PercentEncodeCodePoint, utf8PercentEncodeString, isC0ControlPercentEncode,
  7. isFragmentPercentEncode, isQueryPercentEncode, isSpecialQueryPercentEncode, isPathPercentEncode,
  8. isUserinfoPercentEncode } = require("./percent-encoding");
  9. const specialSchemes = {
  10. ftp: 21,
  11. file: null,
  12. http: 80,
  13. https: 443,
  14. ws: 80,
  15. wss: 443
  16. };
  17. const failure = Symbol("failure");
  18. function countSymbols(str) {
  19. return [...str].length;
  20. }
  21. function at(input, idx) {
  22. const c = input[idx];
  23. return isNaN(c) ? undefined : String.fromCodePoint(c);
  24. }
  25. function isSingleDot(buffer) {
  26. return buffer === "." || buffer.toLowerCase() === "%2e";
  27. }
  28. function isDoubleDot(buffer) {
  29. buffer = buffer.toLowerCase();
  30. return buffer === ".." || buffer === "%2e." || buffer === ".%2e" || buffer === "%2e%2e";
  31. }
  32. function isWindowsDriveLetterCodePoints(cp1, cp2) {
  33. return infra.isASCIIAlpha(cp1) && (cp2 === 58 || cp2 === 124);
  34. }
  35. function isWindowsDriveLetterString(string) {
  36. return string.length === 2 && infra.isASCIIAlpha(string.codePointAt(0)) && (string[1] === ":" || string[1] === "|");
  37. }
  38. function isNormalizedWindowsDriveLetterString(string) {
  39. return string.length === 2 && infra.isASCIIAlpha(string.codePointAt(0)) && string[1] === ":";
  40. }
  41. function containsForbiddenHostCodePoint(string) {
  42. return string.search(/\u0000|\u0009|\u000A|\u000D|\u0020|#|%|\/|:|<|>|\?|@|\[|\\|\]|\^/) !== -1;
  43. }
  44. function containsForbiddenHostCodePointExcludingPercent(string) {
  45. return string.search(/\u0000|\u0009|\u000A|\u000D|\u0020|#|\/|:|<|>|\?|@|\[|\\|\]|\^/) !== -1;
  46. }
  47. function isSpecialScheme(scheme) {
  48. return specialSchemes[scheme] !== undefined;
  49. }
  50. function isSpecial(url) {
  51. return isSpecialScheme(url.scheme);
  52. }
  53. function isNotSpecial(url) {
  54. return !isSpecialScheme(url.scheme);
  55. }
  56. function defaultPort(scheme) {
  57. return specialSchemes[scheme];
  58. }
  59. function parseIPv4Number(input) {
  60. let R = 10;
  61. if (input.length >= 2 && input.charAt(0) === "0" && input.charAt(1).toLowerCase() === "x") {
  62. input = input.substring(2);
  63. R = 16;
  64. } else if (input.length >= 2 && input.charAt(0) === "0") {
  65. input = input.substring(1);
  66. R = 8;
  67. }
  68. if (input === "") {
  69. return 0;
  70. }
  71. let regex = /[^0-7]/;
  72. if (R === 10) {
  73. regex = /[^0-9]/;
  74. }
  75. if (R === 16) {
  76. regex = /[^0-9A-Fa-f]/;
  77. }
  78. if (regex.test(input)) {
  79. return failure;
  80. }
  81. return parseInt(input, R);
  82. }
  83. function parseIPv4(input) {
  84. const parts = input.split(".");
  85. if (parts[parts.length - 1] === "") {
  86. if (parts.length > 1) {
  87. parts.pop();
  88. }
  89. }
  90. if (parts.length > 4) {
  91. return input;
  92. }
  93. const numbers = [];
  94. for (const part of parts) {
  95. if (part === "") {
  96. return input;
  97. }
  98. const n = parseIPv4Number(part);
  99. if (n === failure) {
  100. return input;
  101. }
  102. numbers.push(n);
  103. }
  104. for (let i = 0; i < numbers.length - 1; ++i) {
  105. if (numbers[i] > 255) {
  106. return failure;
  107. }
  108. }
  109. if (numbers[numbers.length - 1] >= Math.pow(256, 5 - numbers.length)) {
  110. return failure;
  111. }
  112. let ipv4 = numbers.pop();
  113. let counter = 0;
  114. for (const n of numbers) {
  115. ipv4 += n * Math.pow(256, 3 - counter);
  116. ++counter;
  117. }
  118. return ipv4;
  119. }
  120. function serializeIPv4(address) {
  121. let output = "";
  122. let n = address;
  123. for (let i = 1; i <= 4; ++i) {
  124. output = String(n % 256) + output;
  125. if (i !== 4) {
  126. output = "." + output;
  127. }
  128. n = Math.floor(n / 256);
  129. }
  130. return output;
  131. }
  132. function parseIPv6(input) {
  133. const address = [0, 0, 0, 0, 0, 0, 0, 0];
  134. let pieceIndex = 0;
  135. let compress = null;
  136. let pointer = 0;
  137. input = punycode.ucs2.decode(input);
  138. if (input[pointer] === 58) {
  139. if (input[pointer + 1] !== 58) {
  140. return failure;
  141. }
  142. pointer += 2;
  143. ++pieceIndex;
  144. compress = pieceIndex;
  145. }
  146. while (pointer < input.length) {
  147. if (pieceIndex === 8) {
  148. return failure;
  149. }
  150. if (input[pointer] === 58) {
  151. if (compress !== null) {
  152. return failure;
  153. }
  154. ++pointer;
  155. ++pieceIndex;
  156. compress = pieceIndex;
  157. continue;
  158. }
  159. let value = 0;
  160. let length = 0;
  161. while (length < 4 && infra.isASCIIHex(input[pointer])) {
  162. value = value * 0x10 + parseInt(at(input, pointer), 16);
  163. ++pointer;
  164. ++length;
  165. }
  166. if (input[pointer] === 46) {
  167. if (length === 0) {
  168. return failure;
  169. }
  170. pointer -= length;
  171. if (pieceIndex > 6) {
  172. return failure;
  173. }
  174. let numbersSeen = 0;
  175. while (input[pointer] !== undefined) {
  176. let ipv4Piece = null;
  177. if (numbersSeen > 0) {
  178. if (input[pointer] === 46 && numbersSeen < 4) {
  179. ++pointer;
  180. } else {
  181. return failure;
  182. }
  183. }
  184. if (!infra.isASCIIDigit(input[pointer])) {
  185. return failure;
  186. }
  187. while (infra.isASCIIDigit(input[pointer])) {
  188. const number = parseInt(at(input, pointer));
  189. if (ipv4Piece === null) {
  190. ipv4Piece = number;
  191. } else if (ipv4Piece === 0) {
  192. return failure;
  193. } else {
  194. ipv4Piece = ipv4Piece * 10 + number;
  195. }
  196. if (ipv4Piece > 255) {
  197. return failure;
  198. }
  199. ++pointer;
  200. }
  201. address[pieceIndex] = address[pieceIndex] * 0x100 + ipv4Piece;
  202. ++numbersSeen;
  203. if (numbersSeen === 2 || numbersSeen === 4) {
  204. ++pieceIndex;
  205. }
  206. }
  207. if (numbersSeen !== 4) {
  208. return failure;
  209. }
  210. break;
  211. } else if (input[pointer] === 58) {
  212. ++pointer;
  213. if (input[pointer] === undefined) {
  214. return failure;
  215. }
  216. } else if (input[pointer] !== undefined) {
  217. return failure;
  218. }
  219. address[pieceIndex] = value;
  220. ++pieceIndex;
  221. }
  222. if (compress !== null) {
  223. let swaps = pieceIndex - compress;
  224. pieceIndex = 7;
  225. while (pieceIndex !== 0 && swaps > 0) {
  226. const temp = address[compress + swaps - 1];
  227. address[compress + swaps - 1] = address[pieceIndex];
  228. address[pieceIndex] = temp;
  229. --pieceIndex;
  230. --swaps;
  231. }
  232. } else if (compress === null && pieceIndex !== 8) {
  233. return failure;
  234. }
  235. return address;
  236. }
  237. function serializeIPv6(address) {
  238. let output = "";
  239. const compress = findLongestZeroSequence(address);
  240. let ignore0 = false;
  241. for (let pieceIndex = 0; pieceIndex <= 7; ++pieceIndex) {
  242. if (ignore0 && address[pieceIndex] === 0) {
  243. continue;
  244. } else if (ignore0) {
  245. ignore0 = false;
  246. }
  247. if (compress === pieceIndex) {
  248. const separator = pieceIndex === 0 ? "::" : ":";
  249. output += separator;
  250. ignore0 = true;
  251. continue;
  252. }
  253. output += address[pieceIndex].toString(16);
  254. if (pieceIndex !== 7) {
  255. output += ":";
  256. }
  257. }
  258. return output;
  259. }
  260. function parseHost(input, isNotSpecialArg = false) {
  261. if (input[0] === "[") {
  262. if (input[input.length - 1] !== "]") {
  263. return failure;
  264. }
  265. return parseIPv6(input.substring(1, input.length - 1));
  266. }
  267. if (isNotSpecialArg) {
  268. return parseOpaqueHost(input);
  269. }
  270. const domain = utf8DecodeWithoutBOM(percentDecodeString(input));
  271. const asciiDomain = domainToASCII(domain);
  272. if (asciiDomain === failure) {
  273. return failure;
  274. }
  275. if (containsForbiddenHostCodePoint(asciiDomain)) {
  276. return failure;
  277. }
  278. const ipv4Host = parseIPv4(asciiDomain);
  279. if (typeof ipv4Host === "number" || ipv4Host === failure) {
  280. return ipv4Host;
  281. }
  282. return asciiDomain;
  283. }
  284. function parseOpaqueHost(input) {
  285. if (containsForbiddenHostCodePointExcludingPercent(input)) {
  286. return failure;
  287. }
  288. return utf8PercentEncodeString(input, isC0ControlPercentEncode);
  289. }
  290. function findLongestZeroSequence(arr) {
  291. let maxIdx = null;
  292. let maxLen = 1; // only find elements > 1
  293. let currStart = null;
  294. let currLen = 0;
  295. for (let i = 0; i < arr.length; ++i) {
  296. if (arr[i] !== 0) {
  297. if (currLen > maxLen) {
  298. maxIdx = currStart;
  299. maxLen = currLen;
  300. }
  301. currStart = null;
  302. currLen = 0;
  303. } else {
  304. if (currStart === null) {
  305. currStart = i;
  306. }
  307. ++currLen;
  308. }
  309. }
  310. // if trailing zeros
  311. if (currLen > maxLen) {
  312. return currStart;
  313. }
  314. return maxIdx;
  315. }
  316. function serializeHost(host) {
  317. if (typeof host === "number") {
  318. return serializeIPv4(host);
  319. }
  320. // IPv6 serializer
  321. if (host instanceof Array) {
  322. return "[" + serializeIPv6(host) + "]";
  323. }
  324. return host;
  325. }
  326. function domainToASCII(domain, beStrict = false) {
  327. const result = tr46.toASCII(domain, {
  328. checkBidi: true,
  329. checkHyphens: false,
  330. checkJoiners: true,
  331. useSTD3ASCIIRules: beStrict,
  332. verifyDNSLength: beStrict
  333. });
  334. if (result === null || result === "") {
  335. return failure;
  336. }
  337. return result;
  338. }
  339. function trimControlChars(url) {
  340. return url.replace(/^[\u0000-\u001F\u0020]+|[\u0000-\u001F\u0020]+$/g, "");
  341. }
  342. function trimTabAndNewline(url) {
  343. return url.replace(/\u0009|\u000A|\u000D/g, "");
  344. }
  345. function shortenPath(url) {
  346. const { path } = url;
  347. if (path.length === 0) {
  348. return;
  349. }
  350. if (url.scheme === "file" && path.length === 1 && isNormalizedWindowsDriveLetter(path[0])) {
  351. return;
  352. }
  353. path.pop();
  354. }
  355. function includesCredentials(url) {
  356. return url.username !== "" || url.password !== "";
  357. }
  358. function cannotHaveAUsernamePasswordPort(url) {
  359. return url.host === null || url.host === "" || url.cannotBeABaseURL || url.scheme === "file";
  360. }
  361. function isNormalizedWindowsDriveLetter(string) {
  362. return /^[A-Za-z]:$/.test(string);
  363. }
  364. function URLStateMachine(input, base, encodingOverride, url, stateOverride) {
  365. this.pointer = 0;
  366. this.input = input;
  367. this.base = base || null;
  368. this.encodingOverride = encodingOverride || "utf-8";
  369. this.stateOverride = stateOverride;
  370. this.url = url;
  371. this.failure = false;
  372. this.parseError = false;
  373. if (!this.url) {
  374. this.url = {
  375. scheme: "",
  376. username: "",
  377. password: "",
  378. host: null,
  379. port: null,
  380. path: [],
  381. query: null,
  382. fragment: null,
  383. cannotBeABaseURL: false
  384. };
  385. const res = trimControlChars(this.input);
  386. if (res !== this.input) {
  387. this.parseError = true;
  388. }
  389. this.input = res;
  390. }
  391. const res = trimTabAndNewline(this.input);
  392. if (res !== this.input) {
  393. this.parseError = true;
  394. }
  395. this.input = res;
  396. this.state = stateOverride || "scheme start";
  397. this.buffer = "";
  398. this.atFlag = false;
  399. this.arrFlag = false;
  400. this.passwordTokenSeenFlag = false;
  401. this.input = punycode.ucs2.decode(this.input);
  402. for (; this.pointer <= this.input.length; ++this.pointer) {
  403. const c = this.input[this.pointer];
  404. const cStr = isNaN(c) ? undefined : String.fromCodePoint(c);
  405. // exec state machine
  406. const ret = this["parse " + this.state](c, cStr);
  407. if (!ret) {
  408. break; // terminate algorithm
  409. } else if (ret === failure) {
  410. this.failure = true;
  411. break;
  412. }
  413. }
  414. }
  415. URLStateMachine.prototype["parse scheme start"] = function parseSchemeStart(c, cStr) {
  416. if (infra.isASCIIAlpha(c)) {
  417. this.buffer += cStr.toLowerCase();
  418. this.state = "scheme";
  419. } else if (!this.stateOverride) {
  420. this.state = "no scheme";
  421. --this.pointer;
  422. } else {
  423. this.parseError = true;
  424. return failure;
  425. }
  426. return true;
  427. };
  428. URLStateMachine.prototype["parse scheme"] = function parseScheme(c, cStr) {
  429. if (infra.isASCIIAlphanumeric(c) || c === 43 || c === 45 || c === 46) {
  430. this.buffer += cStr.toLowerCase();
  431. } else if (c === 58) {
  432. if (this.stateOverride) {
  433. if (isSpecial(this.url) && !isSpecialScheme(this.buffer)) {
  434. return false;
  435. }
  436. if (!isSpecial(this.url) && isSpecialScheme(this.buffer)) {
  437. return false;
  438. }
  439. if ((includesCredentials(this.url) || this.url.port !== null) && this.buffer === "file") {
  440. return false;
  441. }
  442. if (this.url.scheme === "file" && (this.url.host === "" || this.url.host === null)) {
  443. return false;
  444. }
  445. }
  446. this.url.scheme = this.buffer;
  447. if (this.stateOverride) {
  448. if (this.url.port === defaultPort(this.url.scheme)) {
  449. this.url.port = null;
  450. }
  451. return false;
  452. }
  453. this.buffer = "";
  454. if (this.url.scheme === "file") {
  455. if (this.input[this.pointer + 1] !== 47 || this.input[this.pointer + 2] !== 47) {
  456. this.parseError = true;
  457. }
  458. this.state = "file";
  459. } else if (isSpecial(this.url) && this.base !== null && this.base.scheme === this.url.scheme) {
  460. this.state = "special relative or authority";
  461. } else if (isSpecial(this.url)) {
  462. this.state = "special authority slashes";
  463. } else if (this.input[this.pointer + 1] === 47) {
  464. this.state = "path or authority";
  465. ++this.pointer;
  466. } else {
  467. this.url.cannotBeABaseURL = true;
  468. this.url.path.push("");
  469. this.state = "cannot-be-a-base-URL path";
  470. }
  471. } else if (!this.stateOverride) {
  472. this.buffer = "";
  473. this.state = "no scheme";
  474. this.pointer = -1;
  475. } else {
  476. this.parseError = true;
  477. return failure;
  478. }
  479. return true;
  480. };
  481. URLStateMachine.prototype["parse no scheme"] = function parseNoScheme(c) {
  482. if (this.base === null || (this.base.cannotBeABaseURL && c !== 35)) {
  483. return failure;
  484. } else if (this.base.cannotBeABaseURL && c === 35) {
  485. this.url.scheme = this.base.scheme;
  486. this.url.path = this.base.path.slice();
  487. this.url.query = this.base.query;
  488. this.url.fragment = "";
  489. this.url.cannotBeABaseURL = true;
  490. this.state = "fragment";
  491. } else if (this.base.scheme === "file") {
  492. this.state = "file";
  493. --this.pointer;
  494. } else {
  495. this.state = "relative";
  496. --this.pointer;
  497. }
  498. return true;
  499. };
  500. URLStateMachine.prototype["parse special relative or authority"] = function parseSpecialRelativeOrAuthority(c) {
  501. if (c === 47 && this.input[this.pointer + 1] === 47) {
  502. this.state = "special authority ignore slashes";
  503. ++this.pointer;
  504. } else {
  505. this.parseError = true;
  506. this.state = "relative";
  507. --this.pointer;
  508. }
  509. return true;
  510. };
  511. URLStateMachine.prototype["parse path or authority"] = function parsePathOrAuthority(c) {
  512. if (c === 47) {
  513. this.state = "authority";
  514. } else {
  515. this.state = "path";
  516. --this.pointer;
  517. }
  518. return true;
  519. };
  520. URLStateMachine.prototype["parse relative"] = function parseRelative(c) {
  521. this.url.scheme = this.base.scheme;
  522. if (c === 47) {
  523. this.state = "relative slash";
  524. } else if (isSpecial(this.url) && c === 92) {
  525. this.parseError = true;
  526. this.state = "relative slash";
  527. } else {
  528. this.url.username = this.base.username;
  529. this.url.password = this.base.password;
  530. this.url.host = this.base.host;
  531. this.url.port = this.base.port;
  532. this.url.path = this.base.path.slice();
  533. this.url.query = this.base.query;
  534. if (c === 63) {
  535. this.url.query = "";
  536. this.state = "query";
  537. } else if (c === 35) {
  538. this.url.fragment = "";
  539. this.state = "fragment";
  540. } else if (!isNaN(c)) {
  541. this.url.query = null;
  542. this.url.path.pop();
  543. this.state = "path";
  544. --this.pointer;
  545. }
  546. }
  547. return true;
  548. };
  549. URLStateMachine.prototype["parse relative slash"] = function parseRelativeSlash(c) {
  550. if (isSpecial(this.url) && (c === 47 || c === 92)) {
  551. if (c === 92) {
  552. this.parseError = true;
  553. }
  554. this.state = "special authority ignore slashes";
  555. } else if (c === 47) {
  556. this.state = "authority";
  557. } else {
  558. this.url.username = this.base.username;
  559. this.url.password = this.base.password;
  560. this.url.host = this.base.host;
  561. this.url.port = this.base.port;
  562. this.state = "path";
  563. --this.pointer;
  564. }
  565. return true;
  566. };
  567. URLStateMachine.prototype["parse special authority slashes"] = function parseSpecialAuthoritySlashes(c) {
  568. if (c === 47 && this.input[this.pointer + 1] === 47) {
  569. this.state = "special authority ignore slashes";
  570. ++this.pointer;
  571. } else {
  572. this.parseError = true;
  573. this.state = "special authority ignore slashes";
  574. --this.pointer;
  575. }
  576. return true;
  577. };
  578. URLStateMachine.prototype["parse special authority ignore slashes"] = function parseSpecialAuthorityIgnoreSlashes(c) {
  579. if (c !== 47 && c !== 92) {
  580. this.state = "authority";
  581. --this.pointer;
  582. } else {
  583. this.parseError = true;
  584. }
  585. return true;
  586. };
  587. URLStateMachine.prototype["parse authority"] = function parseAuthority(c, cStr) {
  588. if (c === 64) {
  589. this.parseError = true;
  590. if (this.atFlag) {
  591. this.buffer = "%40" + this.buffer;
  592. }
  593. this.atFlag = true;
  594. // careful, this is based on buffer and has its own pointer (this.pointer != pointer) and inner chars
  595. const len = countSymbols(this.buffer);
  596. for (let pointer = 0; pointer < len; ++pointer) {
  597. const codePoint = this.buffer.codePointAt(pointer);
  598. if (codePoint === 58 && !this.passwordTokenSeenFlag) {
  599. this.passwordTokenSeenFlag = true;
  600. continue;
  601. }
  602. const encodedCodePoints = utf8PercentEncodeCodePoint(codePoint, isUserinfoPercentEncode);
  603. if (this.passwordTokenSeenFlag) {
  604. this.url.password += encodedCodePoints;
  605. } else {
  606. this.url.username += encodedCodePoints;
  607. }
  608. }
  609. this.buffer = "";
  610. } else if (isNaN(c) || c === 47 || c === 63 || c === 35 ||
  611. (isSpecial(this.url) && c === 92)) {
  612. if (this.atFlag && this.buffer === "") {
  613. this.parseError = true;
  614. return failure;
  615. }
  616. this.pointer -= countSymbols(this.buffer) + 1;
  617. this.buffer = "";
  618. this.state = "host";
  619. } else {
  620. this.buffer += cStr;
  621. }
  622. return true;
  623. };
  624. URLStateMachine.prototype["parse hostname"] =
  625. URLStateMachine.prototype["parse host"] = function parseHostName(c, cStr) {
  626. if (this.stateOverride && this.url.scheme === "file") {
  627. --this.pointer;
  628. this.state = "file host";
  629. } else if (c === 58 && !this.arrFlag) {
  630. if (this.buffer === "") {
  631. this.parseError = true;
  632. return failure;
  633. }
  634. const host = parseHost(this.buffer, isNotSpecial(this.url));
  635. if (host === failure) {
  636. return failure;
  637. }
  638. this.url.host = host;
  639. this.buffer = "";
  640. this.state = "port";
  641. if (this.stateOverride === "hostname") {
  642. return false;
  643. }
  644. } else if (isNaN(c) || c === 47 || c === 63 || c === 35 ||
  645. (isSpecial(this.url) && c === 92)) {
  646. --this.pointer;
  647. if (isSpecial(this.url) && this.buffer === "") {
  648. this.parseError = true;
  649. return failure;
  650. } else if (this.stateOverride && this.buffer === "" &&
  651. (includesCredentials(this.url) || this.url.port !== null)) {
  652. this.parseError = true;
  653. return false;
  654. }
  655. const host = parseHost(this.buffer, isNotSpecial(this.url));
  656. if (host === failure) {
  657. return failure;
  658. }
  659. this.url.host = host;
  660. this.buffer = "";
  661. this.state = "path start";
  662. if (this.stateOverride) {
  663. return false;
  664. }
  665. } else {
  666. if (c === 91) {
  667. this.arrFlag = true;
  668. } else if (c === 93) {
  669. this.arrFlag = false;
  670. }
  671. this.buffer += cStr;
  672. }
  673. return true;
  674. };
  675. URLStateMachine.prototype["parse port"] = function parsePort(c, cStr) {
  676. if (infra.isASCIIDigit(c)) {
  677. this.buffer += cStr;
  678. } else if (isNaN(c) || c === 47 || c === 63 || c === 35 ||
  679. (isSpecial(this.url) && c === 92) ||
  680. this.stateOverride) {
  681. if (this.buffer !== "") {
  682. const port = parseInt(this.buffer);
  683. if (port > Math.pow(2, 16) - 1) {
  684. this.parseError = true;
  685. return failure;
  686. }
  687. this.url.port = port === defaultPort(this.url.scheme) ? null : port;
  688. this.buffer = "";
  689. }
  690. if (this.stateOverride) {
  691. return false;
  692. }
  693. this.state = "path start";
  694. --this.pointer;
  695. } else {
  696. this.parseError = true;
  697. return failure;
  698. }
  699. return true;
  700. };
  701. const fileOtherwiseCodePoints = new Set([47, 92, 63, 35]);
  702. function startsWithWindowsDriveLetter(input, pointer) {
  703. const length = input.length - pointer;
  704. return length >= 2 &&
  705. isWindowsDriveLetterCodePoints(input[pointer], input[pointer + 1]) &&
  706. (length === 2 || fileOtherwiseCodePoints.has(input[pointer + 2]));
  707. }
  708. URLStateMachine.prototype["parse file"] = function parseFile(c) {
  709. this.url.scheme = "file";
  710. if (c === 47 || c === 92) {
  711. if (c === 92) {
  712. this.parseError = true;
  713. }
  714. this.state = "file slash";
  715. } else if (this.base !== null && this.base.scheme === "file") {
  716. this.url.host = this.base.host;
  717. this.url.path = this.base.path.slice();
  718. this.url.query = this.base.query;
  719. if (c === 63) {
  720. this.url.query = "";
  721. this.state = "query";
  722. } else if (c === 35) {
  723. this.url.fragment = "";
  724. this.state = "fragment";
  725. } else if (!isNaN(c)) {
  726. this.url.query = null;
  727. if (!startsWithWindowsDriveLetter(this.input, this.pointer)) {
  728. shortenPath(this.url);
  729. } else {
  730. this.parseError = true;
  731. this.url.path = [];
  732. }
  733. this.state = "path";
  734. --this.pointer;
  735. }
  736. } else {
  737. this.state = "path";
  738. --this.pointer;
  739. }
  740. return true;
  741. };
  742. URLStateMachine.prototype["parse file slash"] = function parseFileSlash(c) {
  743. if (c === 47 || c === 92) {
  744. if (c === 92) {
  745. this.parseError = true;
  746. }
  747. this.state = "file host";
  748. } else {
  749. if (this.base !== null && this.base.scheme === "file") {
  750. if (!startsWithWindowsDriveLetter(this.input, this.pointer) &&
  751. isNormalizedWindowsDriveLetterString(this.base.path[0])) {
  752. this.url.path.push(this.base.path[0]);
  753. }
  754. this.url.host = this.base.host;
  755. }
  756. this.state = "path";
  757. --this.pointer;
  758. }
  759. return true;
  760. };
  761. URLStateMachine.prototype["parse file host"] = function parseFileHost(c, cStr) {
  762. if (isNaN(c) || c === 47 || c === 92 || c === 63 || c === 35) {
  763. --this.pointer;
  764. if (!this.stateOverride && isWindowsDriveLetterString(this.buffer)) {
  765. this.parseError = true;
  766. this.state = "path";
  767. } else if (this.buffer === "") {
  768. this.url.host = "";
  769. if (this.stateOverride) {
  770. return false;
  771. }
  772. this.state = "path start";
  773. } else {
  774. let host = parseHost(this.buffer, isNotSpecial(this.url));
  775. if (host === failure) {
  776. return failure;
  777. }
  778. if (host === "localhost") {
  779. host = "";
  780. }
  781. this.url.host = host;
  782. if (this.stateOverride) {
  783. return false;
  784. }
  785. this.buffer = "";
  786. this.state = "path start";
  787. }
  788. } else {
  789. this.buffer += cStr;
  790. }
  791. return true;
  792. };
  793. URLStateMachine.prototype["parse path start"] = function parsePathStart(c) {
  794. if (isSpecial(this.url)) {
  795. if (c === 92) {
  796. this.parseError = true;
  797. }
  798. this.state = "path";
  799. if (c !== 47 && c !== 92) {
  800. --this.pointer;
  801. }
  802. } else if (!this.stateOverride && c === 63) {
  803. this.url.query = "";
  804. this.state = "query";
  805. } else if (!this.stateOverride && c === 35) {
  806. this.url.fragment = "";
  807. this.state = "fragment";
  808. } else if (c !== undefined) {
  809. this.state = "path";
  810. if (c !== 47) {
  811. --this.pointer;
  812. }
  813. }
  814. return true;
  815. };
  816. URLStateMachine.prototype["parse path"] = function parsePath(c) {
  817. if (isNaN(c) || c === 47 || (isSpecial(this.url) && c === 92) ||
  818. (!this.stateOverride && (c === 63 || c === 35))) {
  819. if (isSpecial(this.url) && c === 92) {
  820. this.parseError = true;
  821. }
  822. if (isDoubleDot(this.buffer)) {
  823. shortenPath(this.url);
  824. if (c !== 47 && !(isSpecial(this.url) && c === 92)) {
  825. this.url.path.push("");
  826. }
  827. } else if (isSingleDot(this.buffer) && c !== 47 &&
  828. !(isSpecial(this.url) && c === 92)) {
  829. this.url.path.push("");
  830. } else if (!isSingleDot(this.buffer)) {
  831. if (this.url.scheme === "file" && this.url.path.length === 0 && isWindowsDriveLetterString(this.buffer)) {
  832. this.buffer = this.buffer[0] + ":";
  833. }
  834. this.url.path.push(this.buffer);
  835. }
  836. this.buffer = "";
  837. if (c === 63) {
  838. this.url.query = "";
  839. this.state = "query";
  840. }
  841. if (c === 35) {
  842. this.url.fragment = "";
  843. this.state = "fragment";
  844. }
  845. } else {
  846. // TODO: If c is not a URL code point and not "%", parse error.
  847. if (c === 37 &&
  848. (!infra.isASCIIHex(this.input[this.pointer + 1]) ||
  849. !infra.isASCIIHex(this.input[this.pointer + 2]))) {
  850. this.parseError = true;
  851. }
  852. this.buffer += utf8PercentEncodeCodePoint(c, isPathPercentEncode);
  853. }
  854. return true;
  855. };
  856. URLStateMachine.prototype["parse cannot-be-a-base-URL path"] = function parseCannotBeABaseURLPath(c) {
  857. if (c === 63) {
  858. this.url.query = "";
  859. this.state = "query";
  860. } else if (c === 35) {
  861. this.url.fragment = "";
  862. this.state = "fragment";
  863. } else {
  864. // TODO: Add: not a URL code point
  865. if (!isNaN(c) && c !== 37) {
  866. this.parseError = true;
  867. }
  868. if (c === 37 &&
  869. (!infra.isASCIIHex(this.input[this.pointer + 1]) ||
  870. !infra.isASCIIHex(this.input[this.pointer + 2]))) {
  871. this.parseError = true;
  872. }
  873. if (!isNaN(c)) {
  874. this.url.path[0] += utf8PercentEncodeCodePoint(c, isC0ControlPercentEncode);
  875. }
  876. }
  877. return true;
  878. };
  879. URLStateMachine.prototype["parse query"] = function parseQuery(c) {
  880. if (!isSpecial(this.url) || this.url.scheme === "ws" || this.url.scheme === "wss") {
  881. this.encodingOverride = "utf-8";
  882. }
  883. if (!this.stateOverride & c === 35) {
  884. this.url.fragment = "";
  885. this.state = "fragment";
  886. } else if (!isNaN(c)) {
  887. // TODO: If c is not a URL code point and not "%", parse error.
  888. if (c === 37 &&
  889. (!infra.isASCIIHex(this.input[this.pointer + 1]) ||
  890. !infra.isASCIIHex(this.input[this.pointer + 2]))) {
  891. this.parseError = true;
  892. }
  893. const queryPercentEncodePredicate = isSpecial(this.url) ? isSpecialQueryPercentEncode : isQueryPercentEncode;
  894. // TODO: use "percent-encode after encoding" passing in this.encodingOverride
  895. this.url.query += utf8PercentEncodeCodePoint(c, queryPercentEncodePredicate);
  896. }
  897. return true;
  898. };
  899. URLStateMachine.prototype["parse fragment"] = function parseFragment(c) {
  900. if (!isNaN(c)) {
  901. // TODO: If c is not a URL code point and not "%", parse error.
  902. if (c === 37 &&
  903. (!infra.isASCIIHex(this.input[this.pointer + 1]) ||
  904. !infra.isASCIIHex(this.input[this.pointer + 2]))) {
  905. this.parseError = true;
  906. }
  907. this.url.fragment += utf8PercentEncodeCodePoint(c, isFragmentPercentEncode);
  908. }
  909. return true;
  910. };
  911. function serializeURL(url, excludeFragment) {
  912. let output = url.scheme + ":";
  913. if (url.host !== null) {
  914. output += "//";
  915. if (url.username !== "" || url.password !== "") {
  916. output += url.username;
  917. if (url.password !== "") {
  918. output += ":" + url.password;
  919. }
  920. output += "@";
  921. }
  922. output += serializeHost(url.host);
  923. if (url.port !== null) {
  924. output += ":" + url.port;
  925. }
  926. } else if (url.host === null && url.scheme === "file") {
  927. output += "//";
  928. }
  929. if (url.cannotBeABaseURL) {
  930. output += url.path[0];
  931. } else {
  932. if (url.host === null && url.path.length > 1 && url.path[0] === "") {
  933. output += "/.";
  934. }
  935. for (const segment of url.path) {
  936. output += "/" + segment;
  937. }
  938. }
  939. if (url.query !== null) {
  940. output += "?" + url.query;
  941. }
  942. if (!excludeFragment && url.fragment !== null) {
  943. output += "#" + url.fragment;
  944. }
  945. return output;
  946. }
  947. function serializeOrigin(tuple) {
  948. let result = tuple.scheme + "://";
  949. result += serializeHost(tuple.host);
  950. if (tuple.port !== null) {
  951. result += ":" + tuple.port;
  952. }
  953. return result;
  954. }
  955. module.exports.serializeURL = serializeURL;
  956. module.exports.serializeURLOrigin = function (url) {
  957. // https://url.spec.whatwg.org/#concept-url-origin
  958. switch (url.scheme) {
  959. case "blob":
  960. try {
  961. return module.exports.serializeURLOrigin(module.exports.parseURL(url.path[0]));
  962. } catch (e) {
  963. // serializing an opaque origin returns "null"
  964. return "null";
  965. }
  966. case "ftp":
  967. case "http":
  968. case "https":
  969. case "ws":
  970. case "wss":
  971. return serializeOrigin({
  972. scheme: url.scheme,
  973. host: url.host,
  974. port: url.port
  975. });
  976. case "file":
  977. // The spec says:
  978. // > Unfortunate as it is, this is left as an exercise to the reader. When in doubt, return a new opaque origin.
  979. // Browsers tested so far:
  980. // - Chrome says "file://", but treats file: URLs as cross-origin for most (all?) purposes; see e.g.
  981. // https://bugs.chromium.org/p/chromium/issues/detail?id=37586
  982. // - Firefox says "null", but treats file: URLs as same-origin sometimes based on directory stuff; see
  983. // https://developer.mozilla.org/en-US/docs/Archive/Misc_top_level/Same-origin_policy_for_file:_URIs
  984. return "null";
  985. default:
  986. // serializing an opaque origin returns "null"
  987. return "null";
  988. }
  989. };
  990. module.exports.basicURLParse = function (input, options) {
  991. if (options === undefined) {
  992. options = {};
  993. }
  994. const usm = new URLStateMachine(input, options.baseURL, options.encodingOverride, options.url, options.stateOverride);
  995. if (usm.failure) {
  996. return null;
  997. }
  998. return usm.url;
  999. };
  1000. module.exports.setTheUsername = function (url, username) {
  1001. url.username = utf8PercentEncodeString(username, isUserinfoPercentEncode);
  1002. };
  1003. module.exports.setThePassword = function (url, password) {
  1004. url.password = utf8PercentEncodeString(password, isUserinfoPercentEncode);
  1005. };
  1006. module.exports.serializeHost = serializeHost;
  1007. module.exports.cannotHaveAUsernamePasswordPort = cannotHaveAUsernamePasswordPort;
  1008. module.exports.serializeInteger = function (integer) {
  1009. return String(integer);
  1010. };
  1011. module.exports.parseURL = function (input, options) {
  1012. if (options === undefined) {
  1013. options = {};
  1014. }
  1015. // We don't handle blobs, so this just delegates:
  1016. return module.exports.basicURLParse(input, { baseURL: options.baseURL, encodingOverride: options.encodingOverride });
  1017. };