-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuri.js
87 lines (81 loc) · 1.61 KB
/
uri.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
/*
Licensed using GNU GPL terms
(c) 2010, Andrey Smirnov
TODO: proper (c) boilerplate
*/
function URI ( uri )
{
if ( uri )
this.parse_uri( uri );
/*
Dynamic members:
this.uri_original
this.schema
this.schema_specific
this.authority
this.path
this.segment_id
this.query_string
this.query_object
*/
};
URI.prototype = {
authority_is_address: function ()
{
if ( ! this.authority )
return false;
if ( this.authority.match( /^\d+$/ ) )
return true;
return false;
},
parse_uri: function ( uri )
{
this.uri_original = uri;
// Get schema
var parts = uri.split( ':', 2 );
if ( parts[1] != '' )
{
this.schema = parts[0];
this.schema_specific = parts[1];
}
else
{
this.schema = null;
this.schema_specific = parts[0];
}
// Get segment_id
var partsh = this.schema_specific.split( '#', 2 );
var actual_uri = partsh[0];
if ( partsh[1] != '' )
this.segment_id = partsh[1]; /// TODOOO
// Get query string
var partq = actual_uri.split( '?', 2 );
var endpoint = partq[0];
this.query_string = partq[1];
// Get path and authority
var parts1 = endpoint.split( '//', 2 );
if ( parts1[0] == '' )
{
var parts2 = parts1[1].split( '/', 2 );
this.authority = parts2[0];
this.path = '/' + ( parts2[1] || '' );
}
else
{
this.authority = null;
this.path = parts1[0];
}
// Split query_string into object
if ( this.query_string )
{
var q_o = {};
var q_parts = this.query_string.split( '&' );
for ( var i in q_parts )
{
var v_parts = q_parts[i].split( '=', 2 );
q_o[ v_parts[0] ] = v_parts[1];
}
this.query_object = q_o;
}
}
};