1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = isURL;
|
7 |
|
8 | var _assertString = _interopRequireDefault(require("./util/assertString"));
|
9 |
|
10 | var _isFQDN = _interopRequireDefault(require("./isFQDN"));
|
11 |
|
12 | var _isIP = _interopRequireDefault(require("./isIP"));
|
13 |
|
14 | var _merge = _interopRequireDefault(require("./util/merge"));
|
15 |
|
16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
17 |
|
18 | var default_url_options = {
|
19 | protocols: ['http', 'https', 'ftp'],
|
20 | require_tld: true,
|
21 | require_protocol: false,
|
22 | require_host: true,
|
23 | require_valid_protocol: true,
|
24 | allow_underscores: false,
|
25 | allow_trailing_dot: false,
|
26 | allow_protocol_relative_urls: false
|
27 | };
|
28 | var wrapped_ipv6 = /^\[([^\]]+)\](?::([0-9]+))?$/;
|
29 |
|
30 | function isRegExp(obj) {
|
31 | return Object.prototype.toString.call(obj) === '[object RegExp]';
|
32 | }
|
33 |
|
34 | function checkHost(host, matches) {
|
35 | for (var i = 0; i < matches.length; i++) {
|
36 | var match = matches[i];
|
37 |
|
38 | if (host === match || isRegExp(match) && match.test(host)) {
|
39 | return true;
|
40 | }
|
41 | }
|
42 |
|
43 | return false;
|
44 | }
|
45 |
|
46 | function isURL(url, options) {
|
47 | (0, _assertString.default)(url);
|
48 |
|
49 | if (!url || url.length >= 2083 || /[\s<>]/.test(url)) {
|
50 | return false;
|
51 | }
|
52 |
|
53 | if (url.indexOf('mailto:') === 0) {
|
54 | return false;
|
55 | }
|
56 |
|
57 | options = (0, _merge.default)(options, default_url_options);
|
58 | var protocol, auth, host, hostname, port, port_str, split, ipv6;
|
59 | split = url.split('#');
|
60 | url = split.shift();
|
61 | split = url.split('?');
|
62 | url = split.shift();
|
63 | split = url.split('://');
|
64 |
|
65 | if (split.length > 1) {
|
66 | protocol = split.shift().toLowerCase();
|
67 |
|
68 | if (options.require_valid_protocol && options.protocols.indexOf(protocol) === -1) {
|
69 | return false;
|
70 | }
|
71 | } else if (options.require_protocol) {
|
72 | return false;
|
73 | } else if (url.substr(0, 2) === '//') {
|
74 | if (!options.allow_protocol_relative_urls) {
|
75 | return false;
|
76 | }
|
77 |
|
78 | split[0] = url.substr(2);
|
79 | }
|
80 |
|
81 | url = split.join('://');
|
82 |
|
83 | if (url === '') {
|
84 | return false;
|
85 | }
|
86 |
|
87 | split = url.split('/');
|
88 | url = split.shift();
|
89 |
|
90 | if (url === '' && !options.require_host) {
|
91 | return true;
|
92 | }
|
93 |
|
94 | split = url.split('@');
|
95 |
|
96 | if (split.length > 1) {
|
97 | if (options.disallow_auth) {
|
98 | return false;
|
99 | }
|
100 |
|
101 | auth = split.shift();
|
102 |
|
103 | if (auth.indexOf(':') >= 0 && auth.split(':').length > 2) {
|
104 | return false;
|
105 | }
|
106 | }
|
107 |
|
108 | hostname = split.join('@');
|
109 | port_str = null;
|
110 | ipv6 = null;
|
111 | var ipv6_match = hostname.match(wrapped_ipv6);
|
112 |
|
113 | if (ipv6_match) {
|
114 | host = '';
|
115 | ipv6 = ipv6_match[1];
|
116 | port_str = ipv6_match[2] || null;
|
117 | } else {
|
118 | split = hostname.split(':');
|
119 | host = split.shift();
|
120 |
|
121 | if (split.length) {
|
122 | port_str = split.join(':');
|
123 | }
|
124 | }
|
125 |
|
126 | if (port_str !== null) {
|
127 | port = parseInt(port_str, 10);
|
128 |
|
129 | if (!/^[0-9]+$/.test(port_str) || port <= 0 || port > 65535) {
|
130 | return false;
|
131 | }
|
132 | }
|
133 |
|
134 | if (!(0, _isIP.default)(host) && !(0, _isFQDN.default)(host, options) && (!ipv6 || !(0, _isIP.default)(ipv6, 6))) {
|
135 | return false;
|
136 | }
|
137 |
|
138 | host = host || ipv6;
|
139 |
|
140 | if (options.host_whitelist && !checkHost(host, options.host_whitelist)) {
|
141 | return false;
|
142 | }
|
143 |
|
144 | if (options.host_blacklist && checkHost(host, options.host_blacklist)) {
|
145 | return false;
|
146 | }
|
147 |
|
148 | return true;
|
149 | }
|
150 |
|
151 | module.exports = exports.default; |
\ | No newline at end of file |