Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/axios/lib/helpers/isURLSameOrigin.js
1126 views
1
'use strict';
2
3
var utils = require('./../utils');
4
5
module.exports = (
6
utils.isStandardBrowserEnv() ?
7
8
// Standard browser envs have full support of the APIs needed to test
9
// whether the request URL is of the same origin as current location.
10
(function standardBrowserEnv() {
11
var msie = /(msie|trident)/i.test(navigator.userAgent);
12
var urlParsingNode = document.createElement('a');
13
var originURL;
14
15
/**
16
* Parse a URL to discover it's components
17
*
18
* @param {String} url The URL to be parsed
19
* @returns {Object}
20
*/
21
function resolveURL(url) {
22
var href = url;
23
24
if (msie) {
25
// IE needs attribute set twice to normalize properties
26
urlParsingNode.setAttribute('href', href);
27
href = urlParsingNode.href;
28
}
29
30
urlParsingNode.setAttribute('href', href);
31
32
// urlParsingNode provides the UrlUtils interface - http://url.spec.whatwg.org/#urlutils
33
return {
34
href: urlParsingNode.href,
35
protocol: urlParsingNode.protocol ? urlParsingNode.protocol.replace(/:$/, '') : '',
36
host: urlParsingNode.host,
37
search: urlParsingNode.search ? urlParsingNode.search.replace(/^\?/, '') : '',
38
hash: urlParsingNode.hash ? urlParsingNode.hash.replace(/^#/, '') : '',
39
hostname: urlParsingNode.hostname,
40
port: urlParsingNode.port,
41
pathname: (urlParsingNode.pathname.charAt(0) === '/') ?
42
urlParsingNode.pathname :
43
'/' + urlParsingNode.pathname
44
};
45
}
46
47
originURL = resolveURL(window.location.href);
48
49
/**
50
* Determine if a URL shares the same origin as the current location
51
*
52
* @param {String} requestURL The URL to test
53
* @returns {boolean} True if URL shares the same origin, otherwise false
54
*/
55
return function isURLSameOrigin(requestURL) {
56
var parsed = (utils.isString(requestURL)) ? resolveURL(requestURL) : requestURL;
57
return (parsed.protocol === originURL.protocol &&
58
parsed.host === originURL.host);
59
};
60
})() :
61
62
// Non standard browser envs (web workers, react-native) lack needed support.
63
(function nonStandardBrowserEnv() {
64
return function isURLSameOrigin() {
65
return true;
66
};
67
})()
68
);
69
70