1 | (function(module) {
|
2 | 'use strict';
|
3 |
|
4 | module.exports.is_uri = is_iri;
|
5 | module.exports.is_http_uri = is_http_iri;
|
6 | module.exports.is_https_uri = is_https_iri;
|
7 | module.exports.is_web_uri = is_web_iri;
|
8 |
|
9 | module.exports.isUri = is_iri;
|
10 | module.exports.isHttpUri = is_http_iri;
|
11 | module.exports.isHttpsUri = is_https_iri;
|
12 | module.exports.isWebUri = is_web_iri;
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | var splitUri = function(uri) {
|
18 | var splitted = uri.match(/(?:([^:\/?#]+):)?(?:\/\/([^\/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?/);
|
19 | return splitted;
|
20 | };
|
21 |
|
22 | function is_iri(value) {
|
23 | if (!value) {
|
24 | return;
|
25 | }
|
26 |
|
27 |
|
28 | if (/[^a-z0-9\:\/\?\#\[\]\@\!\$\&\'\(\)\*\+\,\;\=\.\-\_\~\%]/i.test(value)) return;
|
29 |
|
30 |
|
31 | if (/%[^0-9a-f]/i.test(value)) return;
|
32 | if (/%[0-9a-f](:?[^0-9a-f]|$)/i.test(value)) return;
|
33 |
|
34 | var splitted = [];
|
35 | var scheme = '';
|
36 | var authority = '';
|
37 | var path = '';
|
38 | var query = '';
|
39 | var fragment = '';
|
40 | var out = '';
|
41 |
|
42 |
|
43 | splitted = splitUri(value);
|
44 | scheme = splitted[1];
|
45 | authority = splitted[2];
|
46 | path = splitted[3];
|
47 | query = splitted[4];
|
48 | fragment = splitted[5];
|
49 |
|
50 |
|
51 | if (!(scheme && scheme.length && path.length >= 0)) return;
|
52 |
|
53 |
|
54 | if (authority && authority.length) {
|
55 | if (!(path.length === 0 || /^\//.test(path))) return;
|
56 | } else {
|
57 |
|
58 | if (/^\/\//.test(path)) return;
|
59 | }
|
60 |
|
61 |
|
62 | if (!/^[a-z][a-z0-9\+\-\.]*$/.test(scheme.toLowerCase())) return;
|
63 |
|
64 |
|
65 | out += scheme + ':';
|
66 | if (authority && authority.length) {
|
67 | out += '//' + authority;
|
68 | }
|
69 |
|
70 | out += path;
|
71 |
|
72 | if (query && query.length) {
|
73 | out += '?' + query;
|
74 | }
|
75 |
|
76 | if (fragment && fragment.length) {
|
77 | out += '#' + fragment;
|
78 | }
|
79 |
|
80 | return out;
|
81 | }
|
82 |
|
83 | function is_http_iri(value, allowHttps) {
|
84 | if (!is_iri(value)) {
|
85 | return;
|
86 | }
|
87 |
|
88 | var splitted = [];
|
89 | var scheme = '';
|
90 | var authority = '';
|
91 | var path = '';
|
92 | var port = '';
|
93 | var query = '';
|
94 | var fragment = '';
|
95 | var out = '';
|
96 |
|
97 |
|
98 | splitted = splitUri(value);
|
99 | scheme = splitted[1];
|
100 | authority = splitted[2];
|
101 | path = splitted[3];
|
102 | query = splitted[4];
|
103 | fragment = splitted[5];
|
104 |
|
105 | if (!scheme) return;
|
106 |
|
107 | if(allowHttps) {
|
108 | if (scheme.toLowerCase() != 'https') return;
|
109 | } else {
|
110 | if (scheme.toLowerCase() != 'http') return;
|
111 | }
|
112 |
|
113 |
|
114 |
|
115 | if (!authority) {
|
116 | return;
|
117 | }
|
118 |
|
119 |
|
120 | if (/:(\d+)$/.test(authority)) {
|
121 | port = authority.match(/:(\d+)$/)[0];
|
122 | authority = authority.replace(/:\d+$/, '');
|
123 | }
|
124 |
|
125 | out += scheme + ':';
|
126 | out += '//' + authority;
|
127 |
|
128 | if (port) {
|
129 | out += port;
|
130 | }
|
131 |
|
132 | out += path;
|
133 |
|
134 | if(query && query.length){
|
135 | out += '?' + query;
|
136 | }
|
137 |
|
138 | if(fragment && fragment.length){
|
139 | out += '#' + fragment;
|
140 | }
|
141 |
|
142 | return out;
|
143 | }
|
144 |
|
145 | function is_https_iri(value) {
|
146 | return is_http_iri(value, true);
|
147 | }
|
148 |
|
149 | function is_web_iri(value) {
|
150 | return (is_http_iri(value) || is_https_iri(value));
|
151 | }
|
152 |
|
153 | })(module);
|