-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathUrl.hx
231 lines (182 loc) · 5.84 KB
/
Url.hx
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
package moon.web;
import moon.core.Struct;
using StringTools;
/**
* http://blog.stevenlevithan.com/archives/parseuri
* @author Munir Hussin
*/
class Url
{
private static var key:Array<String> =
[
"source", "protocol", "authority", "userInfo",
"user", "password", "host", "port", "relative",
"path", "directory", "file", "query", "anchor"
];
private static var strict:EReg = ~/^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/;
private static var loose:EReg = ~/^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/;
private static var qrx:EReg = ~/(?:^|&)([^&=]*)=?([^&]*)/g;
public var source(default, set):String;
public var protocol(default, set):String;
public var authority(default, set):String;
public var userInfo(default, set):String;
public var user(default, set):String;
public var password(default, set):String;
public var host(default, set):String;
public var port(default, set):String;
public var relative(default, set):String;
public var path(default, set):String;
public var directory(default, set):String;
public var file(default, set):String;
public var query(default, set):String;
public var anchor(default, set):String;
public var queryKey:TypedStruct<String>;
private var strictMode:Bool;
public function new(url:String, strictMode:Bool = false)
{
reparse(url, strictMode);
}
public function reparse(url:String, strictMode:Bool = false):Void
{
this.strictMode = strictMode;
var rx:EReg = strictMode ? strict : loose;
var i:Int = 14;
rx.match(url);
while (i-->0)
Reflect.setField(this, key[i], rx.matched(i));
queryKey = new TypedStruct<String>();
qrx.map(query, function(x:EReg):String
{
var s:String = x.matched(1);
if (s != null && s.length > 0)
{
queryKey[x.matched(1)] = x.matched(2);
}
return "";
});
}
public function format():String
{
var s:String = "";
if (protocol != null) s += protocol + "://";
if (authority != null)
{
s += authority;
}
if (relative != null)
{
s += relative;
}
return s;
}
public function toString():String
{
var s:String = "";
for (i in 0...key.length)
{
s += key[i] + ": " + Reflect.field(this, key[i]) + (i == key.length - 1 ? "" : "\n");
}
//Lib.println(this.queryKey);
return s;
}
public static function parse(url:String):Url
{
return new Url(url);
}
private static inline function x(v:String):String
{
return (v != null) ? v : "";
}
private static inline function ax(v:String, a:String):String
{
return (v != null) ? a + v : "";
}
private static inline function xa(v:String, a:String):String
{
return (v != null) ? v + a : "";
}
private function set_source(value:String):String
{
source = value;
reparse(source, strictMode);
return value;
}
private function set_protocol(value:String):String
{
protocol = value;
set_source(xa(protocol, ":") + ax(authority, "//") + x(relative));
return value;
}
private function set_authority(value:String):String
{
authority = value;
set_source(xa(protocol, ":") + ax(authority, "//") + x(relative));
return value;
}
private function set_userInfo(value:String):String
{
userInfo = value;
set_authority(xa(userInfo, "@") + x(host) + ax(port, ":"));
return value;
}
private function set_user(value:String):String
{
user = value;
set_userInfo(x(user) + ax(password, ":"));
return value;
}
private function set_password(value:String):String
{
password = value;
set_userInfo(x(user) + ax(password, ":"));
return value;
}
private function set_host(value:String):String
{
host = value;
set_authority(xa(userInfo, "@") + x(host) + ax(port, ":"));
return value;
}
private function set_port(value:String):String
{
port = value;
set_authority(xa(userInfo, "@") + x(host) + ax(port, ":"));
return value;
}
private function set_relative(value:String):String
{
relative = value;
set_source(xa(protocol, ":") + ax(authority, "//") + x(relative));
return value;
}
private function set_path(value:String):String
{
path = value;
set_relative(x(path) + ax(query, "?") + ax(anchor, "#"));
return value;
}
private function set_directory(value:String):String
{
directory = value;
set_path(x(directory) + x(file));
return value;
}
private function set_file(value:String):String
{
file = value;
set_path(x(directory) + x(file));
return value;
}
private function set_query(value:String):String
{
query = value;
set_relative(x(path) + ax(query, "?") + ax(anchor, "#"));
return value;
}
private function set_anchor(value:String):String
{
anchor = value;
set_relative(x(path) + ax(query, "?") + ax(anchor, "#"));
return value;
}
}