-
Notifications
You must be signed in to change notification settings - Fork 7
/
uris.js
365 lines (361 loc) · 9.29 KB
/
uris.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
/**
* @fileOverview
*
* URI support as per rfc3986
* - Client and Serverside ECMA-262 v3 compatible, node.js friendly
* - All classes extend the native String implementation
* - Full test suite can be launched by running URI.Test();
*
* @author Nathan <http://webr3.org/nathan#me>
* @version 2017-06-07T02:21:00Z
* @license http://creativecommons.org/publicdomain/zero/1.0/
*
* source: <http://github.com/webr3/URI>
* To the extent possible under law, <http://webr3.org/nathan#me>
* has waived all copyright and related or neighboring rights to
* this work.
*/
/**
* URI Class
*
* @constructor
* @extends String
* @param {String} value
* @requires URI.HeirPart
* @requires URI.Authority
* @requires URI.Path
* @return void
*/
(URI = function( value ) {
this.value = value;
this.length = value.length;
String.call( this , value );
}).prototype = {
__proto__: String.prototype,
value: '', length: 0,
toString: function() { return this.value.toString(); },
valueOf: function() { return this.value; },
/**
* @member URI
* @return {URI} The URI with any fragment removed
*/
defrag: function() {
var i = this.indexOf("#");
if (i < 0) return this;
return new URI( this.slice( 0, i ) );
},
/**
* @member URI
* @return {String} The scheme of this URI or null if schemeless
*/
scheme: function() {
var scheme = this.match(/^[a-z0-9\-\+\.]+:/i);
if (scheme == null ) return null;
return scheme.shift();
},
/**
* @member URI
* @return {URI.HeirPart} The heir-part of this URI containing authority and path
*/
heirpart: function() {
var heirpart = this.value;
var q = heirpart.indexOf("?")
if( q >= 0 ) {
heirpart = heirpart.substring(0,q);
} else {
q = heirpart.indexOf("#")
if( q >= 0 ) {
heirpart = heirpart.substring(0,q);
}
}
q = this.scheme();
if( q ) {
heirpart = heirpart.slice( q.length );
}
return new URI.HeirPart(heirpart);
},
/**
* @member URI
* @return {String} The query of the URI or null if no query
*/
querystring: function() {
var q = this.indexOf("?");
if (q < 0) return null;
var f = this.indexOf("#");
if (f < 0) return this.slice(q);
return this.substring( q , f );
},
/**
* @member URI
* @return {String} The fragment of the URI or null if no fragment
*/
fragment: function() {
var i = this.indexOf("#");
if (i < 0) return null;
return this.slice( i );
},
/**
* @member URI
* @return {Boolean} True if the URI is an Absolute URI
*/
isAbsolute: function() {
return this.scheme() != null && this.heirpart() != null && this.fragment() == null;
},
/**
* @member URI
* @return {URI} A normalised Absolute URI with paths resolved and fragment removed
*/
toAbsolute: function() {
if( this.scheme() == null || this.heirpart() == null ) throw 'URI must have a scheme and a hierpart.';
var out = this.resolveReference( this );
return out.defrag();
},
/**
* Implementation of remove-dot-segments from rfc3986
*
* @private
* @member URI
* @param {String} input
* @return {String}
*/
removeDotSegments: function( input ) {
var output = '';
var q = null;
while( input.length > 0 ) {
if( input.substr(0,3) == '../' || input.substr(0,2) == './' ) {
input = input.slice(input.indexOf('/'));
} else if( input == '/.' ) {
input = '/';
} else if( input.substr(0,3) == '/./' ) {
input = input.slice(2);
} else if( input.substr(0,4) == '/../' || input == '/..' ) {
if( input == '/..' ) {
input = '/';
} else {
input = input.slice(3);
}
q = output.lastIndexOf('/');
if( q ) {
output = output.substring(0,q);
} else {
output = '';
}
} else if( input.substr(0,2) == '..' || input.substr(0,1) == '.' ) {
input = input.slice(input.indexOf('.') + 1);
q = input.indexOf('.') + 1;
if(q) {
input = input.slice(q);
}
} else {
if( input.substr(0,1) == '/' ) {
output += '/';
input = input.slice(1);
}
q = input.indexOf('/');
if( q < 0 ) {
output += input;
input = '';
} else {
output += input.substring(0,q);
input = input.slice(q);
}
}
}
return output;
},
/**
* Implementation of Reference Resolution from rfc3986
* Resolves a URI Reference using the current instance as Base URI
*
* @member URI
* @param {String} reference The URI Reference to resolve
* @return {URI} Resolved URI Reference
*/
resolveReference: function( reference ) {
if(typeof reference == 'string') {
reference = new URI(reference);
}
if( !(reference instanceof URI ) ) {
throw 'Expected an URI or a String';
}
var T = { scheme:'',authority:'',path:'',query:'',fragment:''};
var q = null;
if( reference.scheme() ) {
T.scheme = reference.scheme();
q = reference.heirpart().authority();
T.authority += q ? '//' + q : '';
T.path = this.removeDotSegments( reference.heirpart().path() );
q = reference.querystring();
T.query += q ? q : '';
} else {
q = reference.heirpart().authority();
if( q ) {
T.authority = q ? '//' + q : '';
T.path = this.removeDotSegments( reference.heirpart().path() );
q = reference.querystring();
T.query += q ? q : '';
} else {
q = reference.heirpart().path();
if( q == "" ) {
T.path = this.heirpart().path();
q = reference.querystring();
if( q ) {
T.query += q ? q : '';
} else {
q = this.querystring();
T.query += q ? q : '';
}
} else {
if( q.substring(0,1) == '/' ) {
T.path = this.removeDotSegments( q );
} else {
if( this.heirpart().path() ) {
q = this.heirpart().path().lastIndexOf('/') + 1;
if( q ) {
T.path = this.heirpart().path().substring(0,q);
}
T.path += reference.heirpart().path();
} else {
T.path = '/' + q;
}
T.path = this.removeDotSegments( T.path );
}
q = reference.querystring();
T.query += q ? q : '';
}
q = this.heirpart().authority();
T.authority = q ? '//' + q : '';
}
T.scheme = this.scheme();
}
q = reference.fragment();
T.fragment = q ? q : '';
return new URI( T.scheme + T.authority + T.path + T.query + T.fragment );
}
};
/**
* URI.HeirPart
*
* @constructor
* @extends String
* @param {String} value
* @return void
*/
(URI.HeirPart = function( value ) {
this.value = value;
this.length = value.length;
String.call( this , value );
}).prototype = {
__proto__: String.prototype,
toString: function() { return this.value.toString(); },
valueOf: function() { return this.value; },
/**
* @member URI.HeirPart
* @return URI.Authority The Authority of the URI or null
*/
authority: function() {
if( '//' != this.substring(0,2) ) {
return null;
}
var authority = this.slice(2);
var q = authority.indexOf('/');
if( q >= 0 ) {
authority = authority.substr(0,q);
}
return new URI.Authority(authority);
},
/**
* @member URI.HeirPart
* @return URI.Path The Path of the URI
*/
path: function() {
var q = this.authority();
if( !q ) return new URI.Path(this);
return new URI.Path( this.slice(q.length + 2) );
}
};
/**
* URI.Authority
*
* @constructor
* @extends String
* @param {String} value
* @return void
*/
(URI.Authority = function( value ) {
this.value = value;
this.length = value.length;
String.call( this , value );
}).prototype = {
__proto__: String.prototype,
toString: function() { return this.value.toString(); },
valueOf: function() { return this.value; },
/**
* @member URI.Authority
* @return String The user-info of the URI or null
*/
userinfo: function() {
var q = this.indexOf("@");
if(q < 0) return null;
return this.substr(0,q);
},
/**
* @member URI.Authority
* @return String The host of the URI, one of ipv4, ipv6, ipvFuture or reg-name
*/
host: function() {
var host = this.value;
// check if userinfo and remove
var q = host.indexOf("@");
if(q >= 0) host = host.slice(++q);
// check if ipv6 or ipfuture
if( host.indexOf("[") == 0 ) {
q = host.indexOf("]");
if( q > 0 ) return host.substring(0,++q);
}
// check if we have a port and remove
q = host.lastIndexOf(":");
if( q >= 0 ) return host.substring(0,q);
return host;
},
/**
* @member URI.Authority
* @return String The port of the URI or null
*/
port: function() {
var port = this.value;
// remove user and pass
var q = port.indexOf("@");
if(q >= 0) port = port.slice(q);
// strip ipv6 or ipfuture if we have one
if( port.indexOf("[") == 0 ) {
q = port.indexOf("]");
if( q > 0 ) port = port.slice(q);
}
// check if we have a port return
q = port.lastIndexOf(":");
if( q < 0 ) {
return null;
}
port = port.slice(++q);
return ( port.length == 0 ) ? null : port;
}
};
/**
* URI.Path
*
* @constructor
* @extends String
* @param {String} value
* @return void
*/
(URI.Path = function( value ) {
this.value = value;
this.length = value.length;
String.call( this , value );
}).prototype = {
__proto__: String.prototype,
toString: function() { return this.value.toString(); },
valueOf: function() { return this.value; }
};
try { module.exports = URI; } catch(ex) {}