url-state-machine.js 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  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 === "") {
  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. if (this.stateOverride === "hostname") {
  635. return false;
  636. }
  637. const host = parseHost(this.buffer, isNotSpecial(this.url));
  638. if (host === failure) {
  639. return failure;
  640. }
  641. this.url.host = host;
  642. this.buffer = "";
  643. this.state = "port";
  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. this.url.host = "";
  711. if (c === 47 || c === 92) {
  712. if (c === 92) {
  713. this.parseError = true;
  714. }
  715. this.state = "file slash";
  716. } else if (this.base !== null && this.base.scheme === "file") {
  717. this.url.host = this.base.host;
  718. this.url.path = this.base.path.slice();
  719. this.url.query = this.base.query;
  720. if (c === 63) {
  721. this.url.query = "";
  722. this.state = "query";
  723. } else if (c === 35) {
  724. this.url.fragment = "";
  725. this.state = "fragment";
  726. } else if (!isNaN(c)) {
  727. this.url.query = null;
  728. if (!startsWithWindowsDriveLetter(this.input, this.pointer)) {
  729. shortenPath(this.url);
  730. } else {
  731. this.parseError = true;
  732. this.url.path = [];
  733. }
  734. this.state = "path";
  735. --this.pointer;
  736. }
  737. } else {
  738. this.state = "path";
  739. --this.pointer;
  740. }
  741. return true;
  742. };
  743. URLStateMachine.prototype["parse file slash"] = function parseFileSlash(c) {
  744. if (c === 47 || c === 92) {
  745. if (c === 92) {
  746. this.parseError = true;
  747. }
  748. this.state = "file host";
  749. } else {
  750. if (this.base !== null && this.base.scheme === "file") {
  751. if (!startsWithWindowsDriveLetter(this.input, this.pointer) &&
  752. isNormalizedWindowsDriveLetterString(this.base.path[0])) {
  753. this.url.path.push(this.base.path[0]);
  754. }
  755. this.url.host = this.base.host;
  756. }
  757. this.state = "path";
  758. --this.pointer;
  759. }
  760. return true;
  761. };
  762. URLStateMachine.prototype["parse file host"] = function parseFileHost(c, cStr) {
  763. if (isNaN(c) || c === 47 || c === 92 || c === 63 || c === 35) {
  764. --this.pointer;
  765. if (!this.stateOverride && isWindowsDriveLetterString(this.buffer)) {
  766. this.parseError = true;
  767. this.state = "path";
  768. } else if (this.buffer === "") {
  769. this.url.host = "";
  770. if (this.stateOverride) {
  771. return false;
  772. }
  773. this.state = "path start";
  774. } else {
  775. let host = parseHost(this.buffer, isNotSpecial(this.url));
  776. if (host === failure) {
  777. return failure;
  778. }
  779. if (host === "localhost") {
  780. host = "";
  781. }
  782. this.url.host = host;
  783. if (this.stateOverride) {
  784. return false;
  785. }
  786. this.buffer = "";
  787. this.state = "path start";
  788. }
  789. } else {
  790. this.buffer += cStr;
  791. }
  792. return true;
  793. };
  794. URLStateMachine.prototype["parse path start"] = function parsePathStart(c) {
  795. if (isSpecial(this.url)) {
  796. if (c === 92) {
  797. this.parseError = true;
  798. }
  799. this.state = "path";
  800. if (c !== 47 && c !== 92) {
  801. --this.pointer;
  802. }
  803. } else if (!this.stateOverride && c === 63) {
  804. this.url.query = "";
  805. this.state = "query";
  806. } else if (!this.stateOverride && c === 35) {
  807. this.url.fragment = "";
  808. this.state = "fragment";
  809. } else if (c !== undefined) {
  810. this.state = "path";
  811. if (c !== 47) {
  812. --this.pointer;
  813. }
  814. }
  815. return true;
  816. };
  817. URLStateMachine.prototype["parse path"] = function parsePath(c) {
  818. if (isNaN(c) || c === 47 || (isSpecial(this.url) && c === 92) ||
  819. (!this.stateOverride && (c === 63 || c === 35))) {
  820. if (isSpecial(this.url) && c === 92) {
  821. this.parseError = true;
  822. }
  823. if (isDoubleDot(this.buffer)) {
  824. shortenPath(this.url);
  825. if (c !== 47 && !(isSpecial(this.url) && c === 92)) {
  826. this.url.path.push("");
  827. }
  828. } else if (isSingleDot(this.buffer) && c !== 47 &&
  829. !(isSpecial(this.url) && c === 92)) {
  830. this.url.path.push("");
  831. } else if (!isSingleDot(this.buffer)) {
  832. if (this.url.scheme === "file" && this.url.path.length === 0 && isWindowsDriveLetterString(this.buffer)) {
  833. this.buffer = this.buffer[0] + ":";
  834. }
  835. this.url.path.push(this.buffer);
  836. }
  837. this.buffer = "";
  838. if (c === 63) {
  839. this.url.query = "";
  840. this.state = "query";
  841. }
  842. if (c === 35) {
  843. this.url.fragment = "";
  844. this.state = "fragment";
  845. }
  846. } else {
  847. // TODO: If c is not a URL code point and not "%", parse error.
  848. if (c === 37 &&
  849. (!infra.isASCIIHex(this.input[this.pointer + 1]) ||
  850. !infra.isASCIIHex(this.input[this.pointer + 2]))) {
  851. this.parseError = true;
  852. }
  853. this.buffer += utf8PercentEncodeCodePoint(c, isPathPercentEncode);
  854. }
  855. return true;
  856. };
  857. URLStateMachine.prototype["parse cannot-be-a-base-URL path"] = function parseCannotBeABaseURLPath(c) {
  858. if (c === 63) {
  859. this.url.query = "";
  860. this.state = "query";
  861. } else if (c === 35) {
  862. this.url.fragment = "";
  863. this.state = "fragment";
  864. } else {
  865. // TODO: Add: not a URL code point
  866. if (!isNaN(c) && c !== 37) {
  867. this.parseError = true;
  868. }
  869. if (c === 37 &&
  870. (!infra.isASCIIHex(this.input[this.pointer + 1]) ||
  871. !infra.isASCIIHex(this.input[this.pointer + 2]))) {
  872. this.parseError = true;
  873. }
  874. if (!isNaN(c)) {
  875. this.url.path[0] += utf8PercentEncodeCodePoint(c, isC0ControlPercentEncode);
  876. }
  877. }
  878. return true;
  879. };
  880. URLStateMachine.prototype["parse query"] = function parseQuery(c, cStr) {
  881. if (!isSpecial(this.url) || this.url.scheme === "ws" || this.url.scheme === "wss") {
  882. this.encodingOverride = "utf-8";
  883. }
  884. if ((!this.stateOverride && c === 35) || isNaN(c)) {
  885. const queryPercentEncodePredicate = isSpecial(this.url) ? isSpecialQueryPercentEncode : isQueryPercentEncode;
  886. this.url.query += utf8PercentEncodeString(this.buffer, queryPercentEncodePredicate);
  887. this.buffer = "";
  888. if (c === 35) {
  889. this.url.fragment = "";
  890. this.state = "fragment";
  891. }
  892. } else if (!isNaN(c)) {
  893. // TODO: If c is not a URL code point and not "%", parse error.
  894. if (c === 37 &&
  895. (!infra.isASCIIHex(this.input[this.pointer + 1]) ||
  896. !infra.isASCIIHex(this.input[this.pointer + 2]))) {
  897. this.parseError = true;
  898. }
  899. this.buffer += cStr;
  900. }
  901. return true;
  902. };
  903. URLStateMachine.prototype["parse fragment"] = function parseFragment(c) {
  904. if (!isNaN(c)) {
  905. // TODO: If c is not a URL code point and not "%", parse error.
  906. if (c === 37 &&
  907. (!infra.isASCIIHex(this.input[this.pointer + 1]) ||
  908. !infra.isASCIIHex(this.input[this.pointer + 2]))) {
  909. this.parseError = true;
  910. }
  911. this.url.fragment += utf8PercentEncodeCodePoint(c, isFragmentPercentEncode);
  912. }
  913. return true;
  914. };
  915. function serializeURL(url, excludeFragment) {
  916. let output = url.scheme + ":";
  917. if (url.host !== null) {
  918. output += "//";
  919. if (url.username !== "" || url.password !== "") {
  920. output += url.username;
  921. if (url.password !== "") {
  922. output += ":" + url.password;
  923. }
  924. output += "@";
  925. }
  926. output += serializeHost(url.host);
  927. if (url.port !== null) {
  928. output += ":" + url.port;
  929. }
  930. }
  931. if (url.cannotBeABaseURL) {
  932. output += url.path[0];
  933. } else {
  934. if (url.host === null && url.path.length > 1 && url.path[0] === "") {
  935. output += "/.";
  936. }
  937. for (const segment of url.path) {
  938. output += "/" + segment;
  939. }
  940. }
  941. if (url.query !== null) {
  942. output += "?" + url.query;
  943. }
  944. if (!excludeFragment && url.fragment !== null) {
  945. output += "#" + url.fragment;
  946. }
  947. return output;
  948. }
  949. function serializeOrigin(tuple) {
  950. let result = tuple.scheme + "://";
  951. result += serializeHost(tuple.host);
  952. if (tuple.port !== null) {
  953. result += ":" + tuple.port;
  954. }
  955. return result;
  956. }
  957. module.exports.serializeURL = serializeURL;
  958. module.exports.serializeURLOrigin = function (url) {
  959. // https://url.spec.whatwg.org/#concept-url-origin
  960. switch (url.scheme) {
  961. case "blob":
  962. try {
  963. return module.exports.serializeURLOrigin(module.exports.parseURL(url.path[0]));
  964. } catch (e) {
  965. // serializing an opaque origin returns "null"
  966. return "null";
  967. }
  968. case "ftp":
  969. case "http":
  970. case "https":
  971. case "ws":
  972. case "wss":
  973. return serializeOrigin({
  974. scheme: url.scheme,
  975. host: url.host,
  976. port: url.port
  977. });
  978. case "file":
  979. // The spec says:
  980. // > Unfortunate as it is, this is left as an exercise to the reader. When in doubt, return a new opaque origin.
  981. // Browsers tested so far:
  982. // - Chrome says "file://", but treats file: URLs as cross-origin for most (all?) purposes; see e.g.
  983. // https://bugs.chromium.org/p/chromium/issues/detail?id=37586
  984. // - Firefox says "null", but treats file: URLs as same-origin sometimes based on directory stuff; see
  985. // https://developer.mozilla.org/en-US/docs/Archive/Misc_top_level/Same-origin_policy_for_file:_URIs
  986. return "null";
  987. default:
  988. // serializing an opaque origin returns "null"
  989. return "null";
  990. }
  991. };
  992. module.exports.basicURLParse = function (input, options) {
  993. if (options === undefined) {
  994. options = {};
  995. }
  996. const usm = new URLStateMachine(input, options.baseURL, options.encodingOverride, options.url, options.stateOverride);
  997. if (usm.failure) {
  998. return null;
  999. }
  1000. return usm.url;
  1001. };
  1002. module.exports.setTheUsername = function (url, username) {
  1003. url.username = utf8PercentEncodeString(username, isUserinfoPercentEncode);
  1004. };
  1005. module.exports.setThePassword = function (url, password) {
  1006. url.password = utf8PercentEncodeString(password, isUserinfoPercentEncode);
  1007. };
  1008. module.exports.serializeHost = serializeHost;
  1009. module.exports.cannotHaveAUsernamePasswordPort = cannotHaveAUsernamePasswordPort;
  1010. module.exports.serializeInteger = function (integer) {
  1011. return String(integer);
  1012. };
  1013. module.exports.parseURL = function (input, options) {
  1014. if (options === undefined) {
  1015. options = {};
  1016. }
  1017. // We don't handle blobs, so this just delegates:
  1018. return module.exports.basicURLParse(input, { baseURL: options.baseURL, encodingOverride: options.encodingOverride });
  1019. };