Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.

url-state-machine.js 29KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  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|#|%|\/|:|<|>|\?|@|\[|\\|\]|\^|\|/u) !== -1;
  43. }
  44. function containsForbiddenHostCodePointExcludingPercent(string) {
  45. return string.search(/\u0000|\u0009|\u000A|\u000D|\u0020|#|\/|:|<|>|\?|@|\[|\\|\]|\^|\|/u) !== -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]/u;
  72. if (R === 10) {
  73. regex = /[^0-9]/u;
  74. }
  75. if (R === 16) {
  76. regex = /[^0-9A-Fa-f]/u;
  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] >= 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 * 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]+$/ug, "");
  341. }
  342. function trimTabAndNewline(url) {
  343. return url.replace(/\u0009|\u000A|\u000D/ug, "");
  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]:$/u.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 > 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. } else if (this.stateOverride && this.url.host === null) {
  815. this.url.path.push("");
  816. }
  817. return true;
  818. };
  819. URLStateMachine.prototype["parse path"] = function parsePath(c) {
  820. if (isNaN(c) || c === 47 || (isSpecial(this.url) && c === 92) ||
  821. (!this.stateOverride && (c === 63 || c === 35))) {
  822. if (isSpecial(this.url) && c === 92) {
  823. this.parseError = true;
  824. }
  825. if (isDoubleDot(this.buffer)) {
  826. shortenPath(this.url);
  827. if (c !== 47 && !(isSpecial(this.url) && c === 92)) {
  828. this.url.path.push("");
  829. }
  830. } else if (isSingleDot(this.buffer) && c !== 47 &&
  831. !(isSpecial(this.url) && c === 92)) {
  832. this.url.path.push("");
  833. } else if (!isSingleDot(this.buffer)) {
  834. if (this.url.scheme === "file" && this.url.path.length === 0 && isWindowsDriveLetterString(this.buffer)) {
  835. this.buffer = `${this.buffer[0]}:`;
  836. }
  837. this.url.path.push(this.buffer);
  838. }
  839. this.buffer = "";
  840. if (c === 63) {
  841. this.url.query = "";
  842. this.state = "query";
  843. }
  844. if (c === 35) {
  845. this.url.fragment = "";
  846. this.state = "fragment";
  847. }
  848. } else {
  849. // TODO: If c is not a URL code point and not "%", parse error.
  850. if (c === 37 &&
  851. (!infra.isASCIIHex(this.input[this.pointer + 1]) ||
  852. !infra.isASCIIHex(this.input[this.pointer + 2]))) {
  853. this.parseError = true;
  854. }
  855. this.buffer += utf8PercentEncodeCodePoint(c, isPathPercentEncode);
  856. }
  857. return true;
  858. };
  859. URLStateMachine.prototype["parse cannot-be-a-base-URL path"] = function parseCannotBeABaseURLPath(c) {
  860. if (c === 63) {
  861. this.url.query = "";
  862. this.state = "query";
  863. } else if (c === 35) {
  864. this.url.fragment = "";
  865. this.state = "fragment";
  866. } else {
  867. // TODO: Add: not a URL code point
  868. if (!isNaN(c) && c !== 37) {
  869. this.parseError = true;
  870. }
  871. if (c === 37 &&
  872. (!infra.isASCIIHex(this.input[this.pointer + 1]) ||
  873. !infra.isASCIIHex(this.input[this.pointer + 2]))) {
  874. this.parseError = true;
  875. }
  876. if (!isNaN(c)) {
  877. this.url.path[0] += utf8PercentEncodeCodePoint(c, isC0ControlPercentEncode);
  878. }
  879. }
  880. return true;
  881. };
  882. URLStateMachine.prototype["parse query"] = function parseQuery(c, cStr) {
  883. if (!isSpecial(this.url) || this.url.scheme === "ws" || this.url.scheme === "wss") {
  884. this.encodingOverride = "utf-8";
  885. }
  886. if ((!this.stateOverride && c === 35) || isNaN(c)) {
  887. const queryPercentEncodePredicate = isSpecial(this.url) ? isSpecialQueryPercentEncode : isQueryPercentEncode;
  888. this.url.query += utf8PercentEncodeString(this.buffer, queryPercentEncodePredicate);
  889. this.buffer = "";
  890. if (c === 35) {
  891. this.url.fragment = "";
  892. this.state = "fragment";
  893. }
  894. } else if (!isNaN(c)) {
  895. // TODO: If c is not a URL code point and not "%", parse error.
  896. if (c === 37 &&
  897. (!infra.isASCIIHex(this.input[this.pointer + 1]) ||
  898. !infra.isASCIIHex(this.input[this.pointer + 2]))) {
  899. this.parseError = true;
  900. }
  901. this.buffer += cStr;
  902. }
  903. return true;
  904. };
  905. URLStateMachine.prototype["parse fragment"] = function parseFragment(c) {
  906. if (!isNaN(c)) {
  907. // TODO: If c is not a URL code point and not "%", parse error.
  908. if (c === 37 &&
  909. (!infra.isASCIIHex(this.input[this.pointer + 1]) ||
  910. !infra.isASCIIHex(this.input[this.pointer + 2]))) {
  911. this.parseError = true;
  912. }
  913. this.url.fragment += utf8PercentEncodeCodePoint(c, isFragmentPercentEncode);
  914. }
  915. return true;
  916. };
  917. function serializeURL(url, excludeFragment) {
  918. let output = `${url.scheme}:`;
  919. if (url.host !== null) {
  920. output += "//";
  921. if (url.username !== "" || url.password !== "") {
  922. output += url.username;
  923. if (url.password !== "") {
  924. output += `:${url.password}`;
  925. }
  926. output += "@";
  927. }
  928. output += serializeHost(url.host);
  929. if (url.port !== null) {
  930. output += `:${url.port}`;
  931. }
  932. }
  933. if (url.cannotBeABaseURL) {
  934. output += url.path[0];
  935. } else {
  936. if (url.host === null && url.path.length > 1 && url.path[0] === "") {
  937. output += "/.";
  938. }
  939. for (const segment of url.path) {
  940. output += `/${segment}`;
  941. }
  942. }
  943. if (url.query !== null) {
  944. output += `?${url.query}`;
  945. }
  946. if (!excludeFragment && url.fragment !== null) {
  947. output += `#${url.fragment}`;
  948. }
  949. return output;
  950. }
  951. function serializeOrigin(tuple) {
  952. let result = `${tuple.scheme}://`;
  953. result += serializeHost(tuple.host);
  954. if (tuple.port !== null) {
  955. result += `:${tuple.port}`;
  956. }
  957. return result;
  958. }
  959. module.exports.serializeURL = serializeURL;
  960. module.exports.serializeURLOrigin = function (url) {
  961. // https://url.spec.whatwg.org/#concept-url-origin
  962. switch (url.scheme) {
  963. case "blob":
  964. try {
  965. return module.exports.serializeURLOrigin(module.exports.parseURL(url.path[0]));
  966. } catch (e) {
  967. // serializing an opaque origin returns "null"
  968. return "null";
  969. }
  970. case "ftp":
  971. case "http":
  972. case "https":
  973. case "ws":
  974. case "wss":
  975. return serializeOrigin({
  976. scheme: url.scheme,
  977. host: url.host,
  978. port: url.port
  979. });
  980. case "file":
  981. // The spec says:
  982. // > Unfortunate as it is, this is left as an exercise to the reader. When in doubt, return a new opaque origin.
  983. // Browsers tested so far:
  984. // - Chrome says "file://", but treats file: URLs as cross-origin for most (all?) purposes; see e.g.
  985. // https://bugs.chromium.org/p/chromium/issues/detail?id=37586
  986. // - Firefox says "null", but treats file: URLs as same-origin sometimes based on directory stuff; see
  987. // https://developer.mozilla.org/en-US/docs/Archive/Misc_top_level/Same-origin_policy_for_file:_URIs
  988. return "null";
  989. default:
  990. // serializing an opaque origin returns "null"
  991. return "null";
  992. }
  993. };
  994. module.exports.basicURLParse = function (input, options) {
  995. if (options === undefined) {
  996. options = {};
  997. }
  998. const usm = new URLStateMachine(input, options.baseURL, options.encodingOverride, options.url, options.stateOverride);
  999. if (usm.failure) {
  1000. return null;
  1001. }
  1002. return usm.url;
  1003. };
  1004. module.exports.setTheUsername = function (url, username) {
  1005. url.username = utf8PercentEncodeString(username, isUserinfoPercentEncode);
  1006. };
  1007. module.exports.setThePassword = function (url, password) {
  1008. url.password = utf8PercentEncodeString(password, isUserinfoPercentEncode);
  1009. };
  1010. module.exports.serializeHost = serializeHost;
  1011. module.exports.cannotHaveAUsernamePasswordPort = cannotHaveAUsernamePasswordPort;
  1012. module.exports.serializeInteger = function (integer) {
  1013. return String(integer);
  1014. };
  1015. module.exports.parseURL = function (input, options) {
  1016. if (options === undefined) {
  1017. options = {};
  1018. }
  1019. // We don't handle blobs, so this just delegates:
  1020. return module.exports.basicURLParse(input, { baseURL: options.baseURL, encodingOverride: options.encodingOverride });
  1021. };