forked from yorkie/rust.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
url.rs
128 lines (106 loc) · 3.3 KB
/
url.rs
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
use util::v8;
use url::Url;
extern fn parse(info: v8::FunctionCallbackInfo) {
let arg = info.At(0).ToString().as_string();
// FIXME(Yorkie): convert string to str, any better solution?
let url = Url::parse(&*arg).unwrap();
let obj = v8::Object::New();
// protocol
obj.Set(v8::String::NewFromUtf8("protocol"),
v8::String::NewFromUtf8(url.scheme()));
// username
obj.Set(v8::String::NewFromUtf8("username"),
v8::String::NewFromUtf8(url.username()));
// password
match url.password() {
Some(password) => {
obj.Set(v8::String::NewFromUtf8("password"),
v8::String::NewFromUtf8(password));
},
None => {}
}
// hostname
obj.Set(v8::String::NewFromUtf8("hostname"),
v8::String::NewFromUtf8(url.host_str().unwrap()));
// port
match url.port_or_known_default() {
Some(port) => {
obj.Set(v8::String::NewFromUtf8("port"),
v8::Number::NewFromUInt16(port));
},
None => {}
}
// pathname
obj.Set(v8::String::NewFromUtf8("pathname"),
v8::String::NewFromUtf8(url.path()));
// query
match url.query() {
Some(query) => {
obj.Set(v8::String::NewFromUtf8("query"),
v8::String::NewFromUtf8(query));
},
None => {}
}
// hash
match url.fragment() {
Some(fragment) => {
obj.Set(v8::String::NewFromUtf8("hash"),
v8::String::NewFromUtf8(fragment));
},
None => {}
}
info.GetReturnValue().Set(obj);
}
extern fn format(info: v8::FunctionCallbackInfo) {
let options = info.At(0).ToObject();
let mut url = Url::parse("http://localhost").unwrap();
let protocol = options.Get(v8::String::NewFromUtf8("protocol"));
let hostname = options.Get(v8::String::NewFromUtf8("hostname"));
let port = options.Get(v8::String::NewFromUtf8("port"));
let username = options.Get(v8::String::NewFromUtf8("username"));
let password = options.Get(v8::String::NewFromUtf8("password"));
let pathname = options.Get(v8::String::NewFromUtf8("pathname"));
let query = options.Get(v8::String::NewFromUtf8("query"));
let hash = options.Get(v8::String::NewFromUtf8("hash"));
if protocol.IsString() {
let val = protocol.ToString().as_string();
url.set_scheme(&*val).unwrap();
}
if hostname.IsString() {
let val = hostname.ToString().as_string();
url.set_host(Some(&*val)).unwrap();
}
if !port.IsUndefined() {
let val = port.Int32Value() as u16;
url.set_port(Some(val)).unwrap();
}
if username.IsString() {
let val = username.ToString().as_string();
url.set_username(&*val).unwrap();
}
if password.IsString() {
let val = password.ToString().as_string();
url.set_password(Some(&*val)).unwrap();
}
if pathname.IsString() {
let val = pathname.ToString().as_string();
url.set_path(&*val);
}
if query.IsString() {
let val = query.ToString().as_string();
url.set_query(Some(&*val));
}
if hash.IsString() {
let val = hash.ToString().as_string();
url.set_fragment(Some(&*val));
}
info.GetReturnValue().Set(v8::String::NewFromUtf8(url.as_str()));
}
pub fn Init() -> v8::Object {
let exports = v8::Object::New();
exports.Set(v8::String::NewFromUtf8("parse"),
v8::FunctionTemplate::New(parse).GetFunction());
exports.Set(v8::String::NewFromUtf8("format"),
v8::FunctionTemplate::New(format).GetFunction());
return exports;
}