summaryrefslogtreecommitdiff
path: root/cli/js/web/url.ts
blob: bbe405f85f3775df952aa198e051e772e22eccac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { build } from "../build.ts";
import { getRandomValues } from "../ops/get_random_values.ts";
import { customInspect } from "./console.ts";
import { urls } from "./url_search_params.ts";

interface URLParts {
  protocol: string;
  username: string;
  password: string;
  hostname: string;
  port: string;
  path: string;
  query: string;
  hash: string;
}

const searchParamsMethods: Array<keyof URLSearchParams> = [
  "append",
  "delete",
  "set",
];

const specialSchemes = ["ftp", "file", "http", "https", "ws", "wss"];

// https://url.spec.whatwg.org/#special-scheme
const schemePorts: { [key: string]: string } = {
  ftp: "21",
  file: "",
  http: "80",
  https: "443",
  ws: "80",
  wss: "443",
};
const MAX_PORT = 2 ** 16 - 1;

// Remove the part of the string that matches the pattern and return the
// remainder (RHS) as well as the first captured group of the matched substring
// (LHS). e.g.
//      takePattern("https://deno.land:80", /^([a-z]+):[/]{2}/)
//        = ["http", "deno.land:80"]
//      takePattern("deno.land:80", /^([^:]+):)
//        = ["deno.land", "80"]
function takePattern(string: string, pattern: RegExp): [string, string] {
  let capture = "";
  const rest = string.replace(pattern, (_, capture_) => {
    capture = capture_;
    return "";
  });
  return [capture, rest];
}

function parse(url: string, isBase = true): URLParts | undefined {
  const parts: Partial<URLParts> = {};
  let restUrl;
  [parts.protocol, restUrl] = takePattern(url.trim(), /^([a-z]+):/);
  if (isBase && parts.protocol == "") {
    return undefined;
  }
  if (parts.protocol == "file") {
    parts.username = "";
    parts.password = "";
    [parts.hostname, restUrl] = takePattern(restUrl, /^[/\\]{2}([^/\\?#]*)/);
    parts.port = "";
  } else if (specialSchemes.includes(parts.protocol)) {
    let restAuthority;
    [restAuthority, restUrl] = takePattern(
      restUrl,
      /^[/\\]{2}[/\\]*([^/\\?#]+)/
    );
    if (isBase && restAuthority == "") {
      return undefined;
    }
    let restAuthentication;
    [restAuthentication, restAuthority] = takePattern(restAuthority, /^(.*)@/);
    [parts.username, restAuthentication] = takePattern(
      restAuthentication,
      /^([^:]*)/
    );
    parts.username = encodeUserinfo(parts.username);
    [parts.password] = takePattern(restAuthentication, /^:(.*)/);
    parts.password = encodeUserinfo(parts.password);
    [parts.hostname, restAuthority] = takePattern(restAuthority, /^([^:]+)/);
    [parts.port] = takePattern(restAuthority, /^:(.*)/);
    if (!isValidPort(parts.port)) {
      return undefined;
    }
  } else {
    parts.username = "";
    parts.password = "";
    parts.hostname = "";
    parts.port = "";
  }
  try {
    parts.hostname = encodeHostname(parts.hostname).toLowerCase();
  } catch {
    return undefined;
  }
  [parts.path, restUrl] = takePattern(restUrl, /^([^?#]*)/);
  parts.path = encodePathname(parts.path.replace(/\\/g, "/"));
  [parts.query, restUrl] = takePattern(restUrl, /^(\?[^#]*)/);
  parts.query = encodeSearch(parts.query);
  [parts.hash] = takePattern(restUrl, /^(#.*)/);
  parts.hash = encodeHash(parts.hash);
  return parts as URLParts;
}

// Based on https://github.com/kelektiv/node-uuid
// TODO(kevinkassimo): Use deno_std version once possible.
function generateUUID(): string {
  return "00000000-0000-4000-8000-000000000000".replace(/[0]/g, (): string =>
    // random integer from 0 to 15 as a hex digit.
    (getRandomValues(new Uint8Array(1))[0] % 16).toString(16)
  );
}

// Keep it outside of URL to avoid any attempts of access.
export const blobURLMap = new Map<string, Blob>();

function isAbsolutePath(path: string): boolean {
  return path.startsWith("/");
}

// Resolves `.`s and `..`s where possible.
// Preserves repeating and trailing `/`s by design.
// On Windows, drive letter paths will be given a leading slash, and also a
// trailing slash if there are no other components e.g. "C:" -> "/C:/".
function normalizePath(path: string, isFilePath = false): string {
  if (build.os == "windows" && isFilePath) {
    path = path.replace(/^\/*([A-Za-z]:)(\/|$)/, "/$1/");
  }
  const isAbsolute = isAbsolutePath(path);
  path = path.replace(/^\//, "");
  const pathSegments = path.split("/");

  const newPathSegments: string[] = [];
  for (let i = 0; i < pathSegments.length; i++) {
    const previous = newPathSegments[newPathSegments.length - 1];
    if (
      pathSegments[i] == ".." &&
      previous != ".." &&
      (previous != undefined || isAbsolute)
    ) {
      newPathSegments.pop();
    } else if (pathSegments[i] != ".") {
      newPathSegments.push(pathSegments[i]);
    }
  }

  let newPath = newPathSegments.join("/");
  if (!isAbsolute) {
    if (newPathSegments.length == 0) {
      newPath = ".";
    }
  } else {
    newPath = `/${newPath}`;
  }
  return newPath;
}

// Standard URL basing logic, applied to paths.
function resolvePathFromBase(
  path: string,
  basePath: string,
  isFilePath = false
): string {
  let normalizedPath = normalizePath(path, isFilePath);
  let normalizedBasePath = normalizePath(basePath, isFilePath);

  let driveLetterPrefix = "";
  if (build.os == "windows" && isFilePath) {
    let driveLetter = "";
    let baseDriveLetter = "";
    [driveLetter, normalizedPath] = takePattern(
      normalizedPath,
      /^(\/[A-Za-z]:)(?=\/)/
    );
    [baseDriveLetter, normalizedBasePath] = takePattern(
      normalizedBasePath,
      /^(\/[A-Za-z]:)(?=\/)/
    );
    driveLetterPrefix = driveLetter || baseDriveLetter;
  }

  if (isAbsolutePath(normalizedPath)) {
    return `${driveLetterPrefix}${normalizedPath}`;
  }
  if (!isAbsolutePath(normalizedBasePath)) {
    throw new TypeError("Base path must be absolute.");
  }

  // Special case.
  if (path == "") {
    return `${driveLetterPrefix}${normalizedBasePath}`;
  }

  // Remove everything after the last `/` in `normalizedBasePath`.
  const prefix = normalizedBasePath.replace(/[^\/]*$/, "");
  // If `normalizedPath` ends with `.` or `..`, add a trailing slash.
  const suffix = normalizedPath.replace(/(?<=(^|\/)(\.|\.\.))$/, "/");

  return `${driveLetterPrefix}${normalizePath(prefix + suffix)}`;
}

function isValidPort(value: string): boolean {
  // https://url.spec.whatwg.org/#port-state
  if (value === "") true;
  const port = Number(value);
  return Number.isInteger(port) && port >= 0 && port <= MAX_PORT;
}

/** @internal */
export const parts = new WeakMap<URL, URLParts>();

export class URLImpl implements URL {
  #searchParams!: URLSearchParams;

  [customInspect](): string {
    const keys = [
      "href",
      "origin",
      "protocol",
      "username",
      "password",
      "host",
      "hostname",
      "port",
      "pathname",
      "hash",
      "search",
    ];
    const objectString = keys
      .map((key: string) => `${key}: "${this[key as keyof this] || ""}"`)
      .join(", ");
    return `URL { ${objectString} }`;
  }

  #updateSearchParams = (): void => {
    const searchParams = new URLSearchParams(this.search);

    for (const methodName of searchParamsMethods) {
      /* eslint-disable @typescript-eslint/no-explicit-any */
      const method: (...args: any[]) => any = searchParams[methodName];
      searchParams[methodName] = (...args: unknown[]): any => {
        method.apply(searchParams, args);
        this.search = searchParams.toString();
      };
      /* eslint-enable */
    }
    this.#searchParams = searchParams;

    urls.set(searchParams, this);
  };

  get hash(): string {
    return parts.get(this)!.hash;
  }

  set hash(value: string) {
    value = unescape(String(value));
    if (!value) {
      parts.get(this)!.hash = "";
    } else {
      if (value.charAt(0) !== "#") {
        value = `#${value}`;
      }
      // hashes can contain % and # unescaped
      parts.get(this)!.hash = encodeHash(value);
    }
  }

  get host(): string {
    return `${this.hostname}${this.port ? `:${this.port}` : ""}`;
  }

  set host(value: string) {
    value = String(value);
    const url = new URL(`http://${value}`);
    parts.get(this)!.hostname = url.hostname;
    parts.get(this)!.port = url.port;
  }

  get hostname(): string {
    return parts.get(this)!.hostname;
  }

  set hostname(value: string) {
    value = String(value);
    try {
      parts.get(this)!.hostname = encodeHostname(value);
    } catch {}
  }

  get href(): string {
    const authentication =
      this.username || this.password
        ? `${this.username}${this.password ? ":" + this.password : ""}@`
        : "";
    let slash = "";
    if (this.host || this.protocol === "file:") {
      slash = "//";
    }
    return `${this.protocol}${slash}${authentication}${this.host}${this.pathname}${this.search}${this.hash}`;
  }

  set href(value: string) {
    value = String(value);
    if (value !== this.href) {
      const url = new URL(value);
      parts.set(this, { ...parts.get(url)! });
      this.#updateSearchParams();
    }
  }

  get origin(): string {
    if (this.host) {
      return `${this.protocol}//${this.host}`;
    }
    return "null";
  }

  get password(): string {
    return parts.get(this)!.password;
  }

  set password(value: string) {
    value = String(value);
    parts.get(this)!.password = encodeUserinfo(value);
  }

  get pathname(): string {
    return parts.get(this)?.path || "/";
  }

  set pathname(value: string) {
    value = unescape(String(value));
    if (!value || value.charAt(0) !== "/") {
      value = `/${value}`;
    }
    // paths can contain % unescaped
    parts.get(this)!.path = encodePathname(value);
  }

  get port(): string {
    const port = parts.get(this)!.port;
    if (schemePorts[parts.get(this)!.protocol] === port) {
      return "";
    }

    return port;
  }

  set port(value: string) {
    if (!isValidPort(value)) {
      return;
    }
    parts.get(this)!.port = value.toString();
  }

  get protocol(): string {
    return `${parts.get(this)!.protocol}:`;
  }

  set protocol(value: string) {
    value = String(value);
    if (value) {
      if (value.charAt(value.length - 1) === ":") {
        value = value.slice(0, -1);
      }
      parts.get(this)!.protocol = encodeURIComponent(value);
    }
  }

  get search(): string {
    return parts.get(this)!.query;
  }

  set search(value: string) {
    value = String(value);
    const query = value == "" || value.charAt(0) == "?" ? value : `?${value}`;
    parts.get(this)!.query = encodeSearch(query);
    this.#updateSearchParams();
  }

  get username(): string {
    return parts.get(this)!.username;
  }

  set username(value: string) {
    value = String(value);
    parts.get(this)!.username = encodeUserinfo(value);
  }

  get searchParams(): URLSearchParams {
    return this.#searchParams;
  }

  constructor(url: string | URL, base?: string | URL) {
    let baseParts: URLParts | undefined;
    if (base) {
      baseParts = typeof base === "string" ? parse(base) : parts.get(base);
      if (baseParts == undefined) {
        throw new TypeError("Invalid base URL.");
      }
    }

    const urlParts =
      typeof url === "string" ? parse(url, !baseParts) : parts.get(url);
    if (urlParts == undefined) {
      throw new TypeError("Invalid URL.");
    }

    if (urlParts.protocol) {
      urlParts.path = normalizePath(urlParts.path, urlParts.protocol == "file");
      parts.set(this, urlParts);
    } else if (baseParts) {
      parts.set(this, {
        protocol: baseParts.protocol,
        username: baseParts.username,
        password: baseParts.password,
        hostname: baseParts.hostname,
        port: baseParts.port,
        path: resolvePathFromBase(
          urlParts.path,
          baseParts.path || "/",
          baseParts.protocol == "file"
        ),
        query: urlParts.query,
        hash: urlParts.hash,
      });
    } else {
      throw new TypeError("Invalid URL.");
    }

    this.#updateSearchParams();
  }

  toString(): string {
    return this.href;
  }

  toJSON(): string {
    return this.href;
  }

  // TODO(kevinkassimo): implement MediaSource version in the future.
  static createObjectURL(b: Blob): string {
    const origin = "http://deno-opaque-origin";
    const key = `blob:${origin}/${generateUUID()}`;
    blobURLMap.set(key, b);
    return key;
  }

  static revokeObjectURL(url: string): void {
    let urlObject;
    try {
      urlObject = new URL(url);
    } catch {
      throw new TypeError("Provided URL string is not valid");
    }
    if (urlObject.protocol !== "blob:") {
      return;
    }
    // Origin match check seems irrelevant for now, unless we implement
    // persisten storage for per globalThis.location.origin at some point.
    blobURLMap.delete(url);
  }
}

function charInC0ControlSet(c: string): boolean {
  return c >= "\u0000" && c <= "\u001F";
}

function charInSearchSet(c: string): boolean {
  // prettier-ignore
  return charInC0ControlSet(c) || ["\u0020", "\u0022", "\u0023", "\u0027", "\u003C", "\u003E"].includes(c) || c > "\u007E";
}

function charInFragmentSet(c: string): boolean {
  // prettier-ignore
  return charInC0ControlSet(c) || ["\u0020", "\u0022", "\u003C", "\u003E", "\u0060"].includes(c);
}

function charInPathSet(c: string): boolean {
  // prettier-ignore
  return charInFragmentSet(c) || ["\u0023", "\u003F", "\u007B", "\u007D"].includes(c);
}

function charInUserinfoSet(c: string): boolean {
  // "\u0027" ("'") seemingly isn't in the spec, but matches Chrome and Firefox.
  // prettier-ignore
  return charInPathSet(c) || ["\u0027", "\u002F", "\u003A", "\u003B", "\u003D", "\u0040", "\u005B", "\u005C", "\u005D", "\u005E", "\u007C"].includes(c);
}

function encodeChar(c: string): string {
  return `%${c.charCodeAt(0).toString(16)}`.toUpperCase();
}

function encodeUserinfo(s: string): string {
  return [...s].map((c) => (charInUserinfoSet(c) ? encodeChar(c) : c)).join("");
}

function encodeHostname(s: string): string {
  // FIXME: https://url.spec.whatwg.org/#idna
  if (s.includes(":")) {
    throw new TypeError("Invalid hostname.");
  }
  return encodeURIComponent(s);
}

function encodePathname(s: string): string {
  return [...s].map((c) => (charInPathSet(c) ? encodeChar(c) : c)).join("");
}

function encodeSearch(s: string): string {
  return [...s].map((c) => (charInSearchSet(c) ? encodeChar(c) : c)).join("");
}

function encodeHash(s: string): string {
  return [...s].map((c) => (charInFragmentSet(c) ? encodeChar(c) : c)).join("");
}