uri.ts 20.0 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import { isWindows } from 'vs/base/common/platform';
7
import { CharCode } from 'vs/base/common/charCode';
8
import * as paths from 'vs/base/common/path';
9

10 11 12
const _schemePattern = /^\w[\w\d+.-]*$/;
const _singleSlashStart = /^\//;
const _doubleSlashStart = /^\/\//;
13

14
function _validateUri(ret: URI, _strict?: boolean): void {
15

16
	// scheme, must be set
17 18
	if (!ret.scheme && _strict) {
		throw new Error(`[UriError]: Scheme is missing: {scheme: "", authority: "${ret.authority}", path: "${ret.path}", query: "${ret.query}", fragment: "${ret.fragment}"}`);
19
	}
20

21 22
	// scheme, https://tools.ietf.org/html/rfc3986#section-3.1
	// ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
23
	if (ret.scheme && !_schemePattern.test(ret.scheme)) {
24 25 26 27 28 29 30 31 32 33
		throw new Error('[UriError]: Scheme contains illegal characters.');
	}

	// path, http://tools.ietf.org/html/rfc3986#section-3.3
	// If a URI contains an authority component, then the path component
	// must either be empty or begin with a slash ("/") character.  If a URI
	// does not contain an authority component, then the path cannot begin
	// with two slash characters ("//").
	if (ret.path) {
		if (ret.authority) {
34
			if (!_singleSlashStart.test(ret.path)) {
35 36 37
				throw new Error('[UriError]: If a URI contains an authority component, then the path component must either be empty or begin with a slash ("/") character');
			}
		} else {
38
			if (_doubleSlashStart.test(ret.path)) {
39 40 41 42 43 44
				throw new Error('[UriError]: If a URI does not contain an authority component, then the path cannot begin with two slash characters ("//")');
			}
		}
	}
}

45 46 47 48 49
// for a while we allowed uris *without* schemes and this is the migration
// for them, e.g. an uri without scheme and without strict-mode warns and falls
// back to the file-scheme. that should cause the least carnage and still be a
// clear warning
function _schemeFix(scheme: string, _strict: boolean): string {
50 51
	if (!scheme && !_strict) {
		return 'file';
52 53 54 55
	}
	return scheme;
}

56 57 58 59 60 61 62 63 64 65 66 67
// implements a bit of https://tools.ietf.org/html/rfc3986#section-5
function _referenceResolution(scheme: string, path: string): string {

	// the slash-character is our 'default base' as we don't
	// support constructing URIs relative to other URIs. This
	// also means that we alter and potentially break paths.
	// see https://tools.ietf.org/html/rfc3986#section-5.1.4
	switch (scheme) {
		case 'https':
		case 'http':
		case 'file':
			if (!path) {
68 69 70
				path = _slash;
			} else if (path[0] !== _slash) {
				path = _slash + path;
71 72 73 74 75 76
			}
			break;
	}
	return path;
}

77 78 79
const _empty = '';
const _slash = '/';
const _regexp = /^(([^:/?#]+?):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/;
80

E
Erich Gamma 已提交
81 82
/**
 * Uniform Resource Identifier (URI) http://tools.ietf.org/html/rfc3986.
83
 * This class is a simple parser which creates the basic component parts
E
Erich Gamma 已提交
84 85 86
 * (http://tools.ietf.org/html/rfc3986#section-3) with minimal validation
 * and encoding.
 *
87
 * ```txt
88
 *       foo://example.com:8042/over/there?name=ferret#nose
E
Erich Gamma 已提交
89 90 91 92 93 94
 *       \_/   \______________/\_________/ \_________/ \__/
 *        |           |            |            |        |
 *     scheme     authority       path        query   fragment
 *        |   _____________________|__
 *       / \ /                        \
 *       urn:example:animal:ferret:nose
95
 * ```
E
Erich Gamma 已提交
96
 */
97
export class URI implements UriComponents {
E
Erich Gamma 已提交
98

99 100 101 102 103 104 105 106 107 108 109
	static isUri(thing: any): thing is URI {
		if (thing instanceof URI) {
			return true;
		}
		if (!thing) {
			return false;
		}
		return typeof (<URI>thing).authority === 'string'
			&& typeof (<URI>thing).fragment === 'string'
			&& typeof (<URI>thing).path === 'string'
			&& typeof (<URI>thing).query === 'string'
J
Johannes Rieken 已提交
110 111 112 113
			&& typeof (<URI>thing).scheme === 'string'
			&& typeof (<URI>thing).fsPath === 'function'
			&& typeof (<URI>thing).with === 'function'
			&& typeof (<URI>thing).toString === 'function';
114 115
	}

E
Erich Gamma 已提交
116 117 118 119
	/**
	 * scheme is the 'http' part of 'http://www.msft.com/some/path?query#fragment'.
	 * The part before the first colon.
	 */
J
Johannes Rieken 已提交
120
	readonly scheme: string;
E
Erich Gamma 已提交
121 122 123 124 125

	/**
	 * authority is the 'www.msft.com' part of 'http://www.msft.com/some/path?query#fragment'.
	 * The part between the first double slashes and the next slash.
	 */
J
Johannes Rieken 已提交
126
	readonly authority: string;
E
Erich Gamma 已提交
127 128 129 130

	/**
	 * path is the '/some/path' part of 'http://www.msft.com/some/path?query#fragment'.
	 */
J
Johannes Rieken 已提交
131
	readonly path: string;
E
Erich Gamma 已提交
132 133 134 135

	/**
	 * query is the 'query' part of 'http://www.msft.com/some/path?query#fragment'.
	 */
J
Johannes Rieken 已提交
136
	readonly query: string;
E
Erich Gamma 已提交
137 138 139 140

	/**
	 * fragment is the 'fragment' part of 'http://www.msft.com/some/path?query#fragment'.
	 */
J
Johannes Rieken 已提交
141 142 143 144 145
	readonly fragment: string;

	/**
	 * @internal
	 */
146
	protected constructor(scheme: string, authority?: string, path?: string, query?: string, fragment?: string, _strict?: boolean);
J
Johannes Rieken 已提交
147

J
Johannes Rieken 已提交
148 149 150 151
	/**
	 * @internal
	 */
	protected constructor(components: UriComponents);
J
Johannes Rieken 已提交
152

J
Johannes Rieken 已提交
153 154 155
	/**
	 * @internal
	 */
156
	protected constructor(schemeOrData: string | UriComponents, authority?: string, path?: string, query?: string, fragment?: string, _strict: boolean = false) {
J
Johannes Rieken 已提交
157 158

		if (typeof schemeOrData === 'object') {
159 160 161 162 163
			this.scheme = schemeOrData.scheme || _empty;
			this.authority = schemeOrData.authority || _empty;
			this.path = schemeOrData.path || _empty;
			this.query = schemeOrData.query || _empty;
			this.fragment = schemeOrData.fragment || _empty;
J
Johannes Rieken 已提交
164 165 166 167
			// no validation because it's this URI
			// that creates uri components.
			// _validateUri(this);
		} else {
168
			this.scheme = _schemeFix(schemeOrData, _strict);
169 170 171 172
			this.authority = authority || _empty;
			this.path = _referenceResolution(this.scheme, path || _empty);
			this.query = query || _empty;
			this.fragment = fragment || _empty;
173

174
			_validateUri(this, _strict);
J
Johannes Rieken 已提交
175
		}
E
Erich Gamma 已提交
176 177 178 179 180
	}

	// ---- filesystem path -----------------------

	/**
P
Pascal Borreli 已提交
181
	 * Returns a string representing the corresponding file system path of this URI.
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
	 * Will handle UNC paths, normalizes windows drive letters to lower-case, and uses the
	 * platform specific path separator.
	 *
	 * * Will *not* validate the path for invalid characters and semantics.
	 * * Will *not* look at the scheme of this URI.
	 * * The result shall *not* be used for display purposes but for accessing a file on disk.
	 *
	 *
	 * The *difference* to `URI#path` is the use of the platform specific separator and the handling
	 * of UNC paths. See the below sample of a file-uri with an authority (UNC path).
	 *
	 * ```ts
		const u = URI.parse('file://server/c$/folder/file.txt')
		u.authority === 'server'
		u.path === '/shares/c$/file.txt'
		u.fsPath === '\\server\c$\folder\file.txt'
	```
	 *
	 * Using `URI#path` to read a file (using fs-apis) would not be enough because parts of the path,
	 * namely the server name, would be missing. Therefore `URI#fsPath` exists - it's sugar to ease working
	 * with URIs that represent files on disk (`file` scheme).
E
Erich Gamma 已提交
203
	 */
J
Johannes Rieken 已提交
204
	get fsPath(): string {
205 206 207
		// if (this.scheme !== 'file') {
		// 	console.warn(`[UriError] calling fsPath with scheme ${this.scheme}`);
		// }
208
		return uriToFsPath(this, false);
E
Erich Gamma 已提交
209 210 211 212
	}

	// ---- modify to new -------------------------

213
	with(change: { scheme?: string; authority?: string | null; path?: string | null; query?: string | null; fragment?: string | null }): URI {
214 215 216 217 218

		if (!change) {
			return this;
		}

219
		let { scheme, authority, path, query, fragment } = change;
R
Rob Lourens 已提交
220
		if (scheme === undefined) {
221
			scheme = this.scheme;
J
Johannes Rieken 已提交
222
		} else if (scheme === null) {
223
			scheme = _empty;
224
		}
R
Rob Lourens 已提交
225
		if (authority === undefined) {
226
			authority = this.authority;
J
Johannes Rieken 已提交
227
		} else if (authority === null) {
228
			authority = _empty;
229
		}
R
Rob Lourens 已提交
230
		if (path === undefined) {
231
			path = this.path;
J
Johannes Rieken 已提交
232
		} else if (path === null) {
233
			path = _empty;
234
		}
R
Rob Lourens 已提交
235
		if (query === undefined) {
236
			query = this.query;
J
Johannes Rieken 已提交
237
		} else if (query === null) {
238
			query = _empty;
239
		}
R
Rob Lourens 已提交
240
		if (fragment === undefined) {
241
			fragment = this.fragment;
J
Johannes Rieken 已提交
242
		} else if (fragment === null) {
243
			fragment = _empty;
244
		}
245 246 247 248 249 250 251 252 253 254

		if (scheme === this.scheme
			&& authority === this.authority
			&& path === this.path
			&& query === this.query
			&& fragment === this.fragment) {

			return this;
		}

255
		return new Uri(scheme, authority, path, query, fragment);
E
Erich Gamma 已提交
256 257 258 259
	}

	// ---- parse & validate ------------------------

260 261 262 263 264 265
	/**
	 * Creates a new URI from a string, e.g. `http://www.msft.com/some/path`,
	 * `file:///usr/home`, or `scheme:with/path`.
	 *
	 * @param value A string which represents an URI (see `URI#toString`).
	 */
266
	static parse(value: string, _strict: boolean = false): URI {
267
		const match = _regexp.exec(value);
J
Johannes Rieken 已提交
268
		if (!match) {
269
			return new Uri(_empty, _empty, _empty, _empty, _empty);
J
Johannes Rieken 已提交
270
		}
271
		return new Uri(
272
			match[2] || _empty,
273 274 275 276
			percentDecode(match[4] || _empty),
			percentDecode(match[5] || _empty),
			percentDecode(match[7] || _empty),
			percentDecode(match[9] || _empty),
277
			_strict
J
Johannes Rieken 已提交
278
		);
E
Erich Gamma 已提交
279 280
	}

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
	/**
	 * Creates a new URI from a file system path, e.g. `c:\my\files`,
	 * `/usr/home`, or `\\server\share\some\path`.
	 *
	 * The *difference* between `URI#parse` and `URI#file` is that the latter treats the argument
	 * as path, not as stringified-uri. E.g. `URI.file(path)` is **not the same as**
	 * `URI.parse('file://' + path)` because the path might contain characters that are
	 * interpreted (# and ?). See the following sample:
	 * ```ts
	const good = URI.file('/coding/c#/project1');
	good.scheme === 'file';
	good.path === '/coding/c#/project1';
	good.fragment === '';
	const bad = URI.parse('file://' + '/coding/c#/project1');
	bad.scheme === 'file';
	bad.path === '/coding/c'; // path is now broken
	bad.fragment === '/project1';
	```
	 *
	 * @param path A file system path (see `URI#fsPath`)
	 */
J
Johannes Rieken 已提交
302
	static file(path: string): URI {
E
Erich Gamma 已提交
303

304
		let authority = _empty;
305

306
		// normalize to fwd-slashes on windows,
A
typo  
Andre Weinand 已提交
307
		// on other systems bwd-slashes are valid
J
Johannes Rieken 已提交
308
		// filename character, eg /f\oo/ba\r.txt
309
		if (isWindows) {
310
			path = path.replace(/\\/g, _slash);
311
		}
312 313 314

		// check for authority as used in UNC shares
		// or use the path as given
315 316
		if (path[0] === _slash && path[1] === _slash) {
			const idx = path.indexOf(_slash, 2);
317
			if (idx === -1) {
J
Johannes Rieken 已提交
318
				authority = path.substring(2);
319
				path = _slash;
320
			} else {
J
Johannes Rieken 已提交
321
				authority = path.substring(2, idx);
322
				path = path.substring(idx) || _slash;
323 324 325
			}
		}

326
		return new Uri('file', authority, path, _empty, _empty);
E
Erich Gamma 已提交
327 328
	}

329
	static from(components: { scheme: string; authority?: string; path?: string; query?: string; fragment?: string }): URI {
330
		return new Uri(
J
Johannes Rieken 已提交
331 332 333 334 335 336
			components.scheme,
			components.authority,
			components.path,
			components.query,
			components.fragment,
		);
E
Erich Gamma 已提交
337 338
	}

339 340 341
	/**
	 * Join a URI path with path fragments and normalizes the resulting path.
	 *
342
	 * @param uri The input URI.
343 344 345
	 * @param pathFragment The path fragment to add to the URI path.
	 * @returns The resulting URI.
	 */
346 347
	static joinPath(uri: URI, ...pathFragment: string[]): URI {
		if (!uri.path) {
D
David Sanders 已提交
348
			throw new Error(`[UriError]: cannot call joinPath on URI without path`);
349
		}
350 351
		let newPath: string;
		if (isWindows && uri.scheme === 'file') {
352
			newPath = URI.file(paths.win32.join(uriToFsPath(uri, true), ...pathFragment)).path;
353 354 355 356
		} else {
			newPath = paths.posix.join(uri.path, ...pathFragment);
		}
		return uri.with({ path: newPath });
357 358
	}

E
Erich Gamma 已提交
359 360
	// ---- printing/externalize ---------------------------

361
	/**
A
Anuj 已提交
362
	 * Creates a string representation for this URI. It's guaranteed that calling
363 364 365 366 367 368
	 * `URI.parse` with the result of this function creates an URI which is equal
	 * to this URI.
	 *
	 * * The result shall *not* be used for display purposes but for externalization or transport.
	 * * The result will be encoded using the percentage encoding and encoding happens mostly
	 * ignore the scheme-specific encoding rules.
369
	 *
370
	 * @param skipEncoding Do not encode the result, default is `false`
371
	 */
J
Johannes Rieken 已提交
372
	toString(skipEncoding: boolean = false): string {
373
		return _asFormatted(this, skipEncoding);
374 375
	}

J
Johannes Rieken 已提交
376
	toJSON(): UriComponents {
377
		return this;
378 379
	}

J
Johannes Rieken 已提交
380
	static revive(data: UriComponents | URI): URI;
381 382 383 384
	static revive(data: UriComponents | URI | undefined): URI | undefined;
	static revive(data: UriComponents | URI | null): URI | null;
	static revive(data: UriComponents | URI | undefined | null): URI | undefined | null;
	static revive(data: UriComponents | URI | undefined | null): URI | undefined | null {
J
Johannes Rieken 已提交
385
		if (!data) {
386
			return data;
J
Johannes Rieken 已提交
387 388 389
		} else if (data instanceof URI) {
			return data;
		} else {
390
			const result = new Uri(data);
391
			result._formatted = (<UriState>data).external;
392
			result._fsPath = (<UriState>data)._sep === _pathSepMarker ? (<UriState>data).fsPath : null;
J
Johannes Rieken 已提交
393 394
			return result;
		}
395 396 397
	}
}

J
Johannes Rieken 已提交
398
export interface UriComponents {
399 400 401 402 403 404 405 406
	scheme: string;
	authority: string;
	path: string;
	query: string;
	fragment: string;
}

interface UriState extends UriComponents {
407
	$mid: number;
408
	external: string;
J
Johannes Rieken 已提交
409
	fsPath: string;
410
	_sep: 1 | undefined;
411 412
}

413
const _pathSepMarker = isWindows ? 1 : undefined;
414

415
// This class exists so that URI is compatibile with vscode.Uri (API).
416
class Uri extends URI {
417

418
	_formatted: string | null = null;
419
	_fsPath: string | null = null;
420 421 422

	get fsPath(): string {
		if (!this._fsPath) {
423
			this._fsPath = uriToFsPath(this, false);
424 425 426 427
		}
		return this._fsPath;
	}

J
Johannes Rieken 已提交
428
	toString(skipEncoding: boolean = false): string {
429 430 431 432 433 434
		if (!skipEncoding) {
			if (!this._formatted) {
				this._formatted = _asFormatted(this, false);
			}
			return this._formatted;
		} else {
435
			// we don't cache that
436
			return _asFormatted(this, true);
J
Johannes Rieken 已提交
437 438
		}
	}
439

J
Johannes Rieken 已提交
440
	toJSON(): UriComponents {
441
		const res = <UriState>{
442
			$mid: 1
443 444 445 446
		};
		// cached state
		if (this._fsPath) {
			res.fsPath = this._fsPath;
447
			res._sep = _pathSepMarker;
448
		}
449 450
		if (this._formatted) {
			res.external = this._formatted;
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
		}
		// uri components
		if (this.path) {
			res.path = this.path;
		}
		if (this.scheme) {
			res.scheme = this.scheme;
		}
		if (this.authority) {
			res.authority = this.authority;
		}
		if (this.query) {
			res.query = this.query;
		}
		if (this.fragment) {
			res.fragment = this.fragment;
		}
		return res;
	}
470 471
}

472
// reserved characters: https://tools.ietf.org/html/rfc3986#section-2.2
473
const encodeTable: { [ch: number]: string } = {
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 522 523
	[CharCode.Colon]: '%3A', // gen-delims
	[CharCode.Slash]: '%2F',
	[CharCode.QuestionMark]: '%3F',
	[CharCode.Hash]: '%23',
	[CharCode.OpenSquareBracket]: '%5B',
	[CharCode.CloseSquareBracket]: '%5D',
	[CharCode.AtSign]: '%40',

	[CharCode.ExclamationMark]: '%21', // sub-delims
	[CharCode.DollarSign]: '%24',
	[CharCode.Ampersand]: '%26',
	[CharCode.SingleQuote]: '%27',
	[CharCode.OpenParen]: '%28',
	[CharCode.CloseParen]: '%29',
	[CharCode.Asterisk]: '%2A',
	[CharCode.Plus]: '%2B',
	[CharCode.Comma]: '%2C',
	[CharCode.Semicolon]: '%3B',
	[CharCode.Equals]: '%3D',

	[CharCode.Space]: '%20',
};

function encodeURIComponentFast(uriComponent: string, allowSlash: boolean): string {
	let res: string | undefined = undefined;
	let nativeEncodePos = -1;

	for (let pos = 0; pos < uriComponent.length; pos++) {
		const code = uriComponent.charCodeAt(pos);

		// unreserved characters: https://tools.ietf.org/html/rfc3986#section-2.3
		if (
			(code >= CharCode.a && code <= CharCode.z)
			|| (code >= CharCode.A && code <= CharCode.Z)
			|| (code >= CharCode.Digit0 && code <= CharCode.Digit9)
			|| code === CharCode.Dash
			|| code === CharCode.Period
			|| code === CharCode.Underline
			|| code === CharCode.Tilde
			|| (allowSlash && code === CharCode.Slash)
		) {
			// check if we are delaying native encode
			if (nativeEncodePos !== -1) {
				res += encodeURIComponent(uriComponent.substring(nativeEncodePos, pos));
				nativeEncodePos = -1;
			}
			// check if we write into a new string (by default we try to return the param)
			if (res !== undefined) {
				res += uriComponent.charAt(pos);
			}
524

525 526 527 528 529
		} else {
			// encoding needed, we need to allocate a new string
			if (res === undefined) {
				res = uriComponent.substr(0, pos);
			}
530

531 532 533
			// check with default table first
			const escaped = encodeTable[code];
			if (escaped !== undefined) {
J
Johannes Rieken 已提交
534

535 536 537 538 539
				// check if we are delaying native encode
				if (nativeEncodePos !== -1) {
					res += encodeURIComponent(uriComponent.substring(nativeEncodePos, pos));
					nativeEncodePos = -1;
				}
J
Johannes Rieken 已提交
540

541 542 543 544 545 546 547
				// append escaped variant to result
				res += escaped;

			} else if (nativeEncodePos === -1) {
				// use native encode only when needed
				nativeEncodePos = pos;
			}
J
Johannes Rieken 已提交
548 549 550
		}
	}

551 552
	if (nativeEncodePos !== -1) {
		res += encodeURIComponent(uriComponent.substring(nativeEncodePos));
J
Johannes Rieken 已提交
553 554
	}

555
	return res !== undefined ? res : uriComponent;
556 557
}

558 559 560 561 562 563 564
function encodeURIComponentMinimal(path: string): string {
	let res: string | undefined = undefined;
	for (let pos = 0; pos < path.length; pos++) {
		const code = path.charCodeAt(pos);
		if (code === CharCode.Hash || code === CharCode.QuestionMark) {
			if (res === undefined) {
				res = path.substr(0, pos);
J
Johannes Rieken 已提交
565
			}
566 567 568 569
			res += encodeTable[code];
		} else {
			if (res !== undefined) {
				res += path[pos];
J
Johannes Rieken 已提交
570 571 572
			}
		}
	}
573
	return res !== undefined ? res : path;
J
Johannes Rieken 已提交
574 575
}

576 577 578
/**
 * Compute `fsPath` for the given uri
 */
579
export function uriToFsPath(uri: URI, keepDriveLetterCasing: boolean): string {
580

581 582 583 584 585
	let value: string;
	if (uri.authority && uri.path.length > 1 && uri.scheme === 'file') {
		// unc path: file://shares/c$/far/boo
		value = `//${uri.authority}${uri.path}`;
	} else if (
586
		uri.path.charCodeAt(0) === CharCode.Slash
587 588 589
		&& (uri.path.charCodeAt(1) >= CharCode.A && uri.path.charCodeAt(1) <= CharCode.Z || uri.path.charCodeAt(1) >= CharCode.a && uri.path.charCodeAt(1) <= CharCode.z)
		&& uri.path.charCodeAt(2) === CharCode.Colon
	) {
590 591 592 593 594 595
		if (!keepDriveLetterCasing) {
			// windows drive letter: file:///c:/far/boo
			value = uri.path[1].toLowerCase() + uri.path.substr(2);
		} else {
			value = uri.path.substr(1);
		}
596 597 598 599 600 601 602 603 604
	} else {
		// other path
		value = uri.path;
	}
	if (isWindows) {
		value = value.replace(/\//g, '\\');
	}
	return value;
}
605

606 607 608
/**
 * Create the external version of a uri
 */
609 610 611 612 613
function _asFormatted(uri: URI, skipEncoding: boolean): string {

	const encoder = !skipEncoding
		? encodeURIComponentFast
		: encodeURIComponentMinimal;
E
Erich Gamma 已提交
614

615
	let res = '';
616
	let { scheme, authority, path, query, fragment } = uri;
617
	if (scheme) {
618 619
		res += scheme;
		res += ':';
620 621
	}
	if (authority || scheme === 'file') {
622 623
		res += _slash;
		res += _slash;
624 625
	}
	if (authority) {
626 627 628 629 630 631 632 633
		let idx = authority.indexOf('@');
		if (idx !== -1) {
			// <user>@<auth>
			const userinfo = authority.substr(0, idx);
			authority = authority.substr(idx + 1);
			idx = userinfo.indexOf(':');
			if (idx === -1) {
				res += encoder(userinfo, false);
J
Johannes Rieken 已提交
634
			} else {
635 636 637 638
				// <user>:<pass>@<auth>
				res += encoder(userinfo.substr(0, idx), false);
				res += ':';
				res += encoder(userinfo.substr(idx + 1), false);
E
Erich Gamma 已提交
639
			}
640
			res += '@';
J
Johannes Rieken 已提交
641
		}
642 643 644 645 646 647 648 649
		authority = authority.toLowerCase();
		idx = authority.indexOf(':');
		if (idx === -1) {
			res += encoder(authority, false);
		} else {
			// <auth>:<port>
			res += encoder(authority.substr(0, idx), false);
			res += authority.substr(idx);
J
Johannes Rieken 已提交
650
		}
651 652
	}
	if (path) {
653 654 655 656 657 658 659 660 661 662 663
		// lower-case windows drive letters in /C:/fff or C:/fff
		if (path.length >= 3 && path.charCodeAt(0) === CharCode.Slash && path.charCodeAt(2) === CharCode.Colon) {
			const code = path.charCodeAt(1);
			if (code >= CharCode.A && code <= CharCode.Z) {
				path = `/${String.fromCharCode(code + 32)}:${path.substr(3)}`; // "/c:".length === 3
			}
		} else if (path.length >= 2 && path.charCodeAt(1) === CharCode.Colon) {
			const code = path.charCodeAt(0);
			if (code >= CharCode.A && code <= CharCode.Z) {
				path = `${String.fromCharCode(code + 32)}:${path.substr(2)}`; // "/c:".length === 3
			}
664
		}
665 666
		// encode the rest of the path
		res += encoder(path, true);
667 668
	}
	if (query) {
669
		res += '?';
670
		res += encoder(query, false);
671 672
	}
	if (fragment) {
673
		res += '#';
674
		res += !skipEncoding ? encodeURIComponentFast(fragment, false) : fragment;
E
Erich Gamma 已提交
675
	}
676
	return res;
E
Erich Gamma 已提交
677
}
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700

// --- decode

function decodeURIComponentGraceful(str: string): string {
	try {
		return decodeURIComponent(str);
	} catch {
		if (str.length > 3) {
			return str.substr(0, 3) + decodeURIComponentGraceful(str.substr(3));
		} else {
			return str;
		}
	}
}

const _rEncodedAsHex = /(%[0-9A-Za-z][0-9A-Za-z])+/g;

function percentDecode(str: string): string {
	if (!str.match(_rEncodedAsHex)) {
		return str;
	}
	return str.replace(_rEncodedAsHex, (match) => decodeURIComponentGraceful(match));
}