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
366
367
368
369
use url::Url;
use url::Host as UrlHost;
use hyper::header::Host;
use result::{WebSocketResult, WSUrlErrorKind};
pub trait ToWebSocketUrlComponents {
fn to_components(&self) -> WebSocketResult<(Host, String, bool)>;
}
impl ToWebSocketUrlComponents for str {
fn to_components(&self) -> WebSocketResult<(Host, String, bool)> {
parse_url_str(&self)
}
}
impl ToWebSocketUrlComponents for Url {
fn to_components(&self) -> WebSocketResult<(Host, String, bool)> {
parse_url(&self)
}
}
impl ToWebSocketUrlComponents for (Host, String, bool) {
fn to_components(&self) -> WebSocketResult<(Host, String, bool)> {
let (mut host, mut resource_name, secure) = self.clone();
host.port = Some(match host.port {
Some(port) => port,
None => if secure { 443 } else { 80 },
});
if resource_name.is_empty() {
resource_name = "/".to_owned();
}
Ok((host, resource_name, secure))
}
}
impl<'a> ToWebSocketUrlComponents for (Host, &'a str, bool) {
fn to_components(&self) -> WebSocketResult<(Host, String, bool)> {
(self.0.clone(), self.1.to_owned(), self.2).to_components()
}
}
impl<'a> ToWebSocketUrlComponents for (Host, &'a str) {
fn to_components(&self) -> WebSocketResult<(Host, String, bool)> {
(self.0.clone(), self.1.to_owned(), false).to_components()
}
}
impl ToWebSocketUrlComponents for (Host, String) {
fn to_components(&self) -> WebSocketResult<(Host, String, bool)> {
(self.0.clone(), self.1.clone(), false).to_components()
}
}
impl ToWebSocketUrlComponents for (UrlHost, u16, String, bool) {
fn to_components(&self) -> WebSocketResult<(Host, String, bool)> {
(Host {
hostname: self.0.serialize(),
port: Some(self.1)
}, self.2.clone(), self.3).to_components()
}
}
impl<'a> ToWebSocketUrlComponents for (UrlHost, u16, &'a str, bool) {
fn to_components(&self) -> WebSocketResult<(Host, String, bool)> {
(Host {
hostname: self.0.serialize(),
port: Some(self.1)
}, self.2, self.3).to_components()
}
}
impl<'a, T: ToWebSocketUrlComponents> ToWebSocketUrlComponents for &'a T {
fn to_components(&self) -> WebSocketResult<(Host, String, bool)> {
(**self).to_components()
}
}
pub fn parse_url_str(url_str: &str) -> WebSocketResult<(Host, String, bool)> {
let parsed_url = try!(Url::parse(url_str));
parse_url(&parsed_url)
}
pub fn parse_url(url: &Url) -> WebSocketResult<(Host, String, bool)> {
if url.fragment != None {
return Err(From::from(WSUrlErrorKind::CannotSetFragment));
}
let secure = match url.scheme.as_ref() {
"ws" => false,
"wss" => true,
_ => return Err(From::from(WSUrlErrorKind::InvalidScheme)),
};
let host = url.host().unwrap().serialize();
let port = url.port_or_default();
let mut resource = "/".to_owned();
resource.push_str(url.path().unwrap().join("/").as_ref());
if let Some(ref query) = url.query {
resource.push('?');
resource.push_str(query);
}
Ok((Host { hostname: host, port: port }, resource, secure))
}
#[cfg(all(feature = "nightly", test))]
mod tests {
use super::*;
use url::{Url, SchemeData, RelativeSchemeData, Host};
use result::{WebSocketError, WSUrlErrorKind};
fn url_for_test() -> Url {
Url {
fragment: None,
scheme: "ws".to_owned(),
scheme_data: SchemeData::Relative(RelativeSchemeData {
username: "".to_owned(),
password: None,
host: Host::Domain("www.example.com".to_owned()),
port: Some(8080),
default_port: Some(80),
path: vec!["some".to_owned(), "path".to_owned()]
}),
query: Some("a=b&c=d".to_owned()),
}
}
#[test]
fn test_parse_url_fragments_not_accepted() {
let url = &mut url_for_test();
url.fragment = Some("non_null_fragment".to_owned());
let result = parse_url(url);
match result {
Err(WebSocketError::WebSocketUrlError(
WSUrlErrorKind::CannotSetFragment)) => (),
Err(e) => panic!("Expected WSUrlErrorKind::CannotSetFragment but got {}", e),
Ok(_) => panic!("Expected WSUrlErrorKind::CannotSetFragment but got Ok")
}
}
#[test]
fn test_parse_url_invalid_schemes_return_error() {
let url = &mut url_for_test();
let invalid_schemes = &["http", "https", "gopher", "file", "ftp", "other"];
for scheme in invalid_schemes {
url.scheme = scheme.to_string();
let result = parse_url(url);
match result {
Err(WebSocketError::WebSocketUrlError(
WSUrlErrorKind::InvalidScheme)) => (),
Err(e) => panic!("Expected WSUrlErrorKind::InvalidScheme but got {}", e),
Ok(_) => panic!("Expected WSUrlErrorKind::InvalidScheme but got Ok")
}
}
}
#[test]
fn test_parse_url_valid_schemes_return_ok() {
let url = &mut url_for_test();
let valid_schemes = &["ws", "wss"];
for scheme in valid_schemes {
url.scheme = scheme.to_string();
let result = parse_url(url);
match result {
Ok(_) => (),
Err(e) => panic!("Expected Ok, but got {}", e)
}
}
}
#[test]
fn test_parse_url_ws_returns_unset_secure_flag() {
let url = &mut url_for_test();
url.scheme = "ws".to_owned();
let result = parse_url(url);
let secure = match result {
Ok((_, _, secure)) => secure,
Err(e) => panic!(e),
};
assert!(!secure);
}
#[test]
fn test_parse_url_wss_returns_set_secure_flag() {
let url = &mut url_for_test();
url.scheme = "wss".to_owned();
let result = parse_url(url);
let secure = match result {
Ok((_, _, secure)) => secure,
Err(e) => panic!(e),
};
assert!(secure);
}
#[test]
fn test_parse_url_generates_proper_output() {
let url = &url_for_test();
let result = parse_url(url);
let (host, resource) = match result {
Ok((host, resource, _)) => (host, resource),
Err(e) => panic!(e),
};
assert_eq!(host.hostname, "www.example.com".to_owned());
assert_eq!(resource, "/some/path?a=b&c=d".to_owned());
match host.port {
Some(port) => assert_eq!(port, 8080),
_ => panic!("Port should not be None"),
}
}
#[test]
fn test_parse_url_empty_path_should_give_slash() {
let url = &mut url_for_test();
match url.scheme_data {
SchemeData::Relative(ref mut scheme_data) => { scheme_data.path = vec![]; },
_ => ()
}
let result = parse_url(url);
let resource = match result {
Ok((_, resource, _)) => resource,
Err(e) => panic!(e),
};
assert_eq!(resource, "/?a=b&c=d".to_owned());
}
#[test]
fn test_parse_url_none_query_should_not_append_question_mark() {
let url = &mut url_for_test();
url.query = None;
let result = parse_url(url);
let resource = match result {
Ok((_, resource, _)) => resource,
Err(e) => panic!(e),
};
assert_eq!(resource, "/some/path".to_owned());
}
#[test]
fn test_parse_url_none_port_should_use_default_port() {
let url = &mut url_for_test();
match url.scheme_data {
SchemeData::Relative(ref mut scheme_data) => {
scheme_data.port = None;
},
_ => ()
}
let result = parse_url(url);
let host = match result {
Ok((host, _, _)) => host,
Err(e) => panic!(e),
};
match host.port {
Some(80) => (),
Some(p) => panic!("Expected port to be 80 but got {}", p),
None => panic!("Expected port to be 80 but got `None`"),
}
}
#[test]
fn test_parse_url_str_valid_url1() {
let url_str = "ws://www.example.com/some/path?a=b&c=d";
let result = parse_url_str(url_str);
let (host, resource, secure) = match result {
Ok((host, resource, secure)) => (host, resource, secure),
Err(e) => panic!(e),
};
match host.port {
Some(80) => (),
Some(p) => panic!("Expected port 80 but got {}", p),
None => panic!("Expected port 80 but got `None`")
}
assert_eq!(host.hostname, "www.example.com".to_owned());
assert_eq!(resource, "/some/path?a=b&c=d".to_owned());
assert!(!secure);
}
#[test]
fn test_parse_url_str_valid_url2() {
let url_str = "wss://www.example.com";
let result = parse_url_str(url_str);
let (host, resource, secure) = match result {
Ok((host, resource, secure)) => (host, resource, secure),
Err(e) => panic!(e)
};
match host.port {
Some(443) => (),
Some(p) => panic!("Expected port 443 but got {}", p),
None => panic!("Expected port 443 but got `None`")
}
assert_eq!(host.hostname, "www.example.com".to_owned());
assert_eq!(resource, "/".to_owned());
assert!(secure);
}
#[test]
fn test_parse_url_str_invalid_relative_url() {
let url_str = "/some/relative/path?a=b&c=d";
let result = parse_url_str(url_str);
match result {
Err(WebSocketError::UrlError(_)) => (),
Err(e) => panic!("Expected UrlError, but got unexpected error {}", e),
Ok(_) => panic!("Expected UrlError, but got Ok"),
}
}
#[test]
fn test_parse_url_str_invalid_url_scheme() {
let url_str = "http://www.example.com/some/path?a=b&c=d";
let result = parse_url_str(url_str);
match result {
Err(WebSocketError::WebSocketUrlError(WSUrlErrorKind::InvalidScheme)) => (),
Err(e) => panic!("Expected InvalidScheme, but got unexpected error {}", e),
Ok(_) => panic!("Expected InvalidScheme, but got Ok"),
}
}
#[test]
fn test_parse_url_str_invalid_url_fragment() {
let url_str = "http://www.example.com/some/path#some-id";
let result = parse_url_str(url_str);
match result {
Err(WebSocketError::WebSocketUrlError(WSUrlErrorKind::CannotSetFragment)) => (),
Err(e) => panic!("Expected CannotSetFragment, but got unexpected error {}", e),
Ok(_) => panic!("Expected CannotSetFragment, but got Ok"),
}
}
}