forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl.js
638 lines (582 loc) · 17.5 KB
/
url.js
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/**
* Copyright 2015 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {urls} from './config';
import {LruCache} from './core/data-structures/lru-cache';
import * as mode from './core/mode';
import {arrayOrSingleItemToArray} from './core/types/array';
import {dict, hasOwn} from './core/types/object';
import {endsWith} from './core/types/string';
import {parseQueryString} from './core/types/string/url';
import {userAssert} from './log';
const SERVING_TYPE_PREFIX = new Set([
// No viewer
'c',
// In viewer
'v',
// Ad landing page
'a',
// Ad
'ad',
]);
/**
* Cached a-tag to avoid memory allocation during URL parsing.
* @type {HTMLAnchorElement}
*/
let cachedAnchorEl;
/**
* We cached all parsed URLs. As of now there are no use cases
* of AMP docs that would ever parse an actual large number of URLs,
* but we often parse the same one over and over again.
* @type {LruCache}
*/
let urlCache;
// eslint-disable-next-line no-script-url
const INVALID_PROTOCOLS = ['javascript:', 'data:', 'vbscript:'];
/** @const {string} */
export const SOURCE_ORIGIN_PARAM = '__amp_source_origin';
/**
* Coerces a url into a location;
* @function
* @param {string|!Location} url
* @return {!Location}
*/
const urlAsLocation = (url) =>
typeof url == 'string' ? parseUrlDeprecated(url) : url;
/**
* Returns the correct origin for a given window.
* TODO(rcebulko): This really belongs under #core/window somewhere, not in url
* @param {!Window} win
* @return {string} origin
*/
export function getWinOrigin(win) {
return win.origin || parseUrlDeprecated(win.location.href).origin;
}
/**
* Returns a Location-like object for the given URL. If it is relative,
* the URL gets resolved.
* Consider the returned object immutable. This is enforced during
* testing by freezing the object.
* TODO(#34453): The URL constructor isn't supported in IE11, but is supported
* everywhere else. There's a lot of code paths (and all uses of the LruCache)
* that are built around this polyfill. Once we can drop IE11 support and just
* use the URL constructor, we can clear out all of parseWithA, all the URL
* cache logic (incl. additional caches in other call-sites). Most is guarded by
* IS_ESM and is only included in nomodule builds, but still.
* @param {string} url
* @param {boolean=} opt_nocache
* Cache is always ignored on ESM builds, see https://go.amp.dev/pr/31594
* @return {!Location}
*/
export function parseUrlDeprecated(url, opt_nocache) {
if (!cachedAnchorEl) {
cachedAnchorEl = /** @type {!HTMLAnchorElement} */ (
self.document.createElement('a')
);
urlCache = IS_ESM
? null
: self.__AMP_URL_CACHE || (self.__AMP_URL_CACHE = new LruCache(100));
}
return parseUrlWithA(
cachedAnchorEl,
url,
IS_ESM || opt_nocache ? null : urlCache
);
}
/**
* Returns a Location-like object for the given URL. If it is relative,
* the URL gets resolved.
* Consider the returned object immutable. This is enforced during
* testing by freezing the object.
* @param {!HTMLAnchorElement} anchorEl
* @param {string} url
* @param {LruCache=} opt_cache
* Cache is always ignored on ESM builds, see https://go.amp.dev/pr/31594
* @return {!Location}
* @restricted
*/
export function parseUrlWithA(anchorEl, url, opt_cache) {
if (IS_ESM) {
// Doing this causes the <a> to auto-set its own href to the resolved path,
// which would be the baseUrl for the URL constructor.
anchorEl.href = '';
return /** @type {?} */ (new URL(url, anchorEl.href));
}
if (opt_cache && opt_cache.has(url)) {
return opt_cache.get(url);
}
anchorEl.href = url;
// IE11 doesn't provide full URL components when parsing relative URLs.
// Assigning to itself again does the trick #3449.
if (!anchorEl.protocol) {
anchorEl.href = anchorEl.href;
}
const info = /** @type {!Location} */ ({
href: anchorEl.href,
protocol: anchorEl.protocol,
host: anchorEl.host,
hostname: anchorEl.hostname,
port: anchorEl.port == '0' ? '' : anchorEl.port,
pathname: anchorEl.pathname,
search: anchorEl.search,
hash: anchorEl.hash,
origin: null, // Set below.
});
// Some IE11 specific polyfills.
// 1) IE11 strips out the leading '/' in the pathname.
if (info.pathname[0] !== '/') {
info.pathname = '/' + info.pathname;
}
// 2) For URLs with implicit ports, IE11 parses to default ports while
// other browsers leave the port field empty.
if (
(info.protocol == 'http:' && info.port == 80) ||
(info.protocol == 'https:' && info.port == 443)
) {
info.port = '';
info.host = info.hostname;
}
// For data URI anchorEl.origin is equal to the string 'null' which is not useful.
// We instead return the actual origin which is the full URL.
let origin;
if (anchorEl.origin && anchorEl.origin != 'null') {
origin = anchorEl.origin;
} else if (info.protocol == 'data:' || !info.host) {
origin = info.href;
} else {
origin = info.protocol + '//' + info.host;
}
info.origin = origin;
// Freeze during testing to avoid accidental mutation.
const frozen = mode.isTest() && Object.freeze ? Object.freeze(info) : info;
if (opt_cache) {
opt_cache.put(url, frozen);
}
return frozen;
}
/**
* Appends the string just before the fragment part (or optionally
* to the front of the query string) of the URL.
* @param {string} url
* @param {string} paramString
* @param {boolean=} opt_addToFront
* @return {string}
*/
export function appendEncodedParamStringToUrl(
url,
paramString,
opt_addToFront
) {
if (!paramString) {
return url;
}
const mainAndFragment = url.split('#', 2);
const mainAndQuery = mainAndFragment[0].split('?', 2);
let newUrl =
mainAndQuery[0] +
(mainAndQuery[1]
? opt_addToFront
? `?${paramString}&${mainAndQuery[1]}`
: `?${mainAndQuery[1]}&${paramString}`
: `?${paramString}`);
newUrl += mainAndFragment[1] ? `#${mainAndFragment[1]}` : '';
return newUrl;
}
/**
* @param {string} key
* @param {string} value
* @return {string}
*/
function urlEncodeKeyValue(key, value) {
return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
}
/**
* Appends a query string field and value to a url. `key` and `value`
* will be ran through `encodeURIComponent` before appending.
* @param {string} url
* @param {string} key
* @param {string} value
* @param {boolean=} opt_addToFront
* @return {string}
*/
export function addParamToUrl(url, key, value, opt_addToFront) {
return appendEncodedParamStringToUrl(
url,
urlEncodeKeyValue(key, value),
opt_addToFront
);
}
/**
* Appends query string fields and values to a url. The `params` objects'
* `key`s and `value`s will be transformed into query string keys/values.
* @param {string} url
* @param {!JsonObject<string, string|!Array<string>>} params
* @return {string}
*/
export function addParamsToUrl(url, params) {
return appendEncodedParamStringToUrl(url, serializeQueryString(params));
}
/**
* Append query string fields and values to a url, only if the key does not
* exist in current query string.
* @param {string} url
* @param {!JsonObject<string, string|!Array<string>>} params
* @return {string}
*/
export function addMissingParamsToUrl(url, params) {
const location = parseUrlDeprecated(url);
const existingParams = parseQueryString(location.search);
const paramsToAdd = dict({});
const keys = Object.keys(params);
for (let i = 0; i < keys.length; i++) {
if (!hasOwn(existingParams, keys[i])) {
paramsToAdd[keys[i]] = params[keys[i]];
}
}
return addParamsToUrl(url, paramsToAdd);
}
/**
* Serializes the passed parameter map into a query string with both keys
* and values encoded.
* @param {!JsonObject<string, string|!Array<string>>} params
* @return {string}
*/
export function serializeQueryString(params) {
const s = [];
for (const k in params) {
let v = params[k];
if (v == null) {
continue;
}
v = arrayOrSingleItemToArray(v);
for (let i = 0; i < v.length; i++) {
s.push(urlEncodeKeyValue(k, v[i]));
}
}
return s.join('&');
}
/**
* Returns `true` if the URL is secure: either HTTPS or localhost (for testing).
* @param {string|!Location} url
* @return {boolean}
*/
export function isSecureUrlDeprecated(url) {
url = urlAsLocation(url);
return (
url.protocol == 'https:' ||
url.hostname == 'localhost' ||
url.hostname == '127.0.0.1' ||
endsWith(url.hostname, '.localhost')
);
}
/**
* Asserts that a given url is HTTPS or protocol relative. It's a user-level
* assert.
*
* Provides an exception for localhost.
*
* @param {?string|undefined} urlString
* @param {!Element|string} elementContext Element where the url was found.
* @param {string=} sourceName Used for error messages.
* @return {string}
*/
export function assertHttpsUrl(
urlString,
elementContext,
sourceName = 'source'
) {
userAssert(
urlString != null,
'%s %s must be available',
elementContext,
sourceName
);
userAssert(
isSecureUrlDeprecated(urlString) || /^\/\//.test(urlString),
'%s %s must start with ' +
'"https://" or "//" or be relative and served from ' +
'either https or from localhost. Invalid value: %s',
elementContext,
sourceName,
urlString
);
return urlString;
}
/**
* Asserts that a given url is an absolute HTTP or HTTPS URL.
* @param {string} urlString
* @return {string}
*/
export function assertAbsoluteHttpOrHttpsUrl(urlString) {
userAssert(
/^https?\:/i.test(urlString),
'URL must start with "http://" or "https://". Invalid value: %s',
urlString
);
return parseUrlDeprecated(urlString).href;
}
/**
* Returns the URL without fragment. If URL doesn't contain fragment, the same
* string is returned.
* @param {string} url
* @return {string}
*/
export function removeFragment(url) {
const index = url.indexOf('#');
if (index == -1) {
return url;
}
return url.substring(0, index);
}
/**
* Returns the fragment from the URL. If the URL doesn't contain fragment,
* the empty string is returned.
* @param {string} url
* @return {string}
*/
export function getFragment(url) {
const index = url.indexOf('#');
if (index == -1) {
return '';
}
return url.substring(index);
}
/**
* Returns whether the URL has the origin of a proxy.
* @param {string|!Location} url URL of an AMP document.
* @return {boolean}
*/
export function isProxyOrigin(url) {
return urls.cdnProxyRegex.test(urlAsLocation(url).origin);
}
/**
* Returns whether the URL origin is localhost.
* @param {string|!Location} url URL of an AMP document.
* @return {boolean}
*/
export function isLocalhostOrigin(url) {
return urls.localhostRegex.test(urlAsLocation(url).origin);
}
/**
* @param {string} uri
* @return {boolean}
*/
export function isAmpScriptUri(uri) {
return uri.startsWith('amp-script:');
}
/**
* For proxy-origin URLs, returns the serving type. Otherwise, returns null.
* E.g., 'https://amp-com.cdn.ampproject.org/a/s/amp.com/amp_document.html'
* returns 'a'.
* @param {string|!Location} url URL of an AMP document.
* @return {?string}
*/
export function getProxyServingType(url) {
url = urlAsLocation(url);
if (!isProxyOrigin(url)) {
return null;
}
const path = url.pathname.split('/', 2);
return path[1];
}
/**
* Returns whether the URL has valid protocol.
* Deep link protocol is valid, but not javascript etc.
* @param {string|!Location} url
* @return {boolean}
*/
export function isProtocolValid(url) {
return !(url && INVALID_PROTOCOLS.includes(urlAsLocation(url).protocol));
}
/**
* Returns a URL without AMP JS parameters.
* @param {string} url
* @return {string}
*/
export function removeAmpJsParamsFromUrl(url) {
const {hash, origin, pathname, search} = parseUrlDeprecated(url);
const searchRemoved = removeAmpJsParamsFromSearch(search);
return origin + pathname + searchRemoved + hash;
}
/**
* Returns a URL without a query string.
* @param {string} url
* @return {string}
*/
export function removeSearch(url) {
const index = url.indexOf('?');
if (index == -1) {
return url;
}
const fragment = getFragment(url);
return url.substring(0, index) + fragment;
}
/**
* Removes parameters that start with amp js parameter pattern and returns the
* new search string.
* @param {string} urlSearch
* @return {string}
*/
function removeAmpJsParamsFromSearch(urlSearch) {
// The below regex is a combo of these original patterns. Combining these,
// removing the corresponding `.replace` calls, and reusing
// removeParamsFromSearch saves ~175B. Matches params in query string:
// - /[?&]amp_js[^&]*/ amp_js_*
// - /[?&]amp_gsa[^&]*/ amp_gsa
// - /[?&]amp_r[^&]*/ amp_r
// - /[?&]amp_kit[^&]*/ amp_kit
// - /[?&]usqp[^&]*/ usqp (from goog experiment)
return removeParamsFromSearch(urlSearch, '(amp_(js[^&=]*|gsa|r|kit)|usqp)');
}
/**
* Removes parameters with param name and returns the new search string.
* @param {string} urlSearch
* @param {string} paramName
* @return {string}
*/
export function removeParamsFromSearch(urlSearch, paramName) {
// TODO: Accept paramNames as an array.
if (!urlSearch || urlSearch == '?') {
return '';
}
const paramRegex = new RegExp(`[?&]${paramName}\\b[^&]*`, 'g');
const search = urlSearch.replace(paramRegex, '').replace(/^[?&]/, '');
return search ? '?' + search : '';
}
/**
* Returns the source URL of an AMP document for documents served
* on a proxy origin or directly.
* @param {string|!Location} url URL of an AMP document.
* @return {string}
*/
export function getSourceUrl(url) {
url = urlAsLocation(url);
// Not a proxy URL - return the URL itself.
if (!isProxyOrigin(url)) {
return url.href;
}
// A proxy URL.
// Example path that is being matched here.
// https://cdn.ampproject.org/c/s/www.origin.com/foo/
// The /s/ is optional and signals a secure origin.
const path = url.pathname.split('/');
const prefix = path[1];
userAssert(
SERVING_TYPE_PREFIX.has(prefix),
'Unknown path prefix in url %s',
url.href
);
const domainOrHttpsSignal = path[2];
const origin =
domainOrHttpsSignal == 's'
? 'https://' + decodeURIComponent(path[3])
: 'http://' + decodeURIComponent(domainOrHttpsSignal);
// Sanity test that what we found looks like a domain.
userAssert(origin.indexOf('.') > 0, 'Expected a . in origin %s', origin);
path.splice(1, domainOrHttpsSignal == 's' ? 3 : 2);
return (
origin +
path.join('/') +
removeAmpJsParamsFromSearch(url.search) +
(url.hash || '')
);
}
/**
* Returns the source origin of an AMP document for documents served
* on a proxy origin or directly.
* @param {string|!Location} url URL of an AMP document.
* @return {string} The source origin of the URL.
*/
export function getSourceOrigin(url) {
return parseUrlDeprecated(getSourceUrl(url)).origin;
}
/**
* Returns absolute URL resolved based on the relative URL and the base.
* @param {string} relativeUrlString
* @param {string|!Location} baseUrl
* @return {string}
*/
export function resolveRelativeUrl(relativeUrlString, baseUrl) {
baseUrl = urlAsLocation(baseUrl);
if (IS_ESM || typeof URL == 'function') {
return new URL(relativeUrlString, baseUrl.href).toString();
}
return resolveRelativeUrlFallback_(relativeUrlString, baseUrl);
}
/**
* Fallback for URL resolver when URL class is not available.
* @param {string} relativeUrlString
* @param {string|!Location} baseUrl
* @return {string}
* @private @visibleForTesting
*/
export function resolveRelativeUrlFallback_(relativeUrlString, baseUrl) {
baseUrl = urlAsLocation(baseUrl);
relativeUrlString = relativeUrlString.replace(/\\/g, '/');
const relativeUrl = parseUrlDeprecated(relativeUrlString);
// Absolute URL.
if (relativeUrlString.toLowerCase().startsWith(relativeUrl.protocol)) {
return relativeUrl.href;
}
// Protocol-relative URL.
if (relativeUrlString.startsWith('//')) {
return baseUrl.protocol + relativeUrlString;
}
// Absolute path.
if (relativeUrlString.startsWith('/')) {
return baseUrl.origin + relativeUrlString;
}
// Relative path.
return (
baseUrl.origin +
baseUrl.pathname.replace(/\/[^/]*$/, '/') +
relativeUrlString
);
}
/**
* Add "__amp_source_origin" query parameter to the URL.
* @param {!Window} win
* @param {string} url
* @return {string}
*/
export function getCorsUrl(win, url) {
checkCorsUrl(url);
const sourceOrigin = getSourceOrigin(win.location.href);
return addParamToUrl(url, SOURCE_ORIGIN_PARAM, sourceOrigin);
}
/**
* Checks if the url has __amp_source_origin and throws if it does.
* @param {string} url
*/
export function checkCorsUrl(url) {
const parsedUrl = parseUrlDeprecated(url);
const query = parseQueryString(parsedUrl.search);
userAssert(
!(SOURCE_ORIGIN_PARAM in query),
'Source origin is not allowed in %s',
url
);
}
/**
* Adds the path to the given url.
*
* @param {!Location} url
* @param {string} path
* @return {string}
*/
export function appendPathToUrl(url, path) {
const pathname = url.pathname.replace(/\/?$/, '/') + path.replace(/^\//, '');
return url.origin + pathname + url.search + url.hash;
}