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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
|
use crate::control_socket_address::ControlSocket::{TcpSocket, UnixLocalAbstractSocket, UnixLocalSocket};
use crate::control_socket_address::ControlSocketScheme::{HTTP, HTTPS};
use crate::unit_client::UnitClientError;
use hyper::http::uri::{Authority, PathAndQuery};
use hyper::Uri;
use std::fmt::{Display, Formatter};
use std::fs;
use std::os::unix::fs::FileTypeExt;
use std::path::{PathBuf, MAIN_SEPARATOR};
type AbstractSocketName = String;
type UnixSocketPath = PathBuf;
type Port = u16;
#[derive(Debug, Clone)]
pub enum ControlSocket {
UnixLocalAbstractSocket(AbstractSocketName),
UnixLocalSocket(UnixSocketPath),
TcpSocket(Uri),
}
#[derive(Debug)]
pub enum ControlSocketScheme {
HTTP,
HTTPS,
}
impl ControlSocketScheme {
fn port(&self) -> Port {
match self {
HTTP => 80,
HTTPS => 443,
}
}
}
impl ControlSocket {
pub fn socket_scheme(&self) -> ControlSocketScheme {
match self {
UnixLocalAbstractSocket(_) => ControlSocketScheme::HTTP,
UnixLocalSocket(_) => ControlSocketScheme::HTTP,
TcpSocket(uri) => match uri.scheme_str().expect("Scheme should not be None") {
"http" => ControlSocketScheme::HTTP,
"https" => ControlSocketScheme::HTTPS,
_ => unreachable!("Scheme should be http or https"),
},
}
}
pub fn create_uri_with_path(&self, str_path: &str) -> Uri {
match self {
UnixLocalAbstractSocket(name) => {
let socket_path = PathBuf::from(format!("@{}", name));
hyperlocal::Uri::new(socket_path, str_path).into()
}
UnixLocalSocket(socket_path) => hyperlocal::Uri::new(socket_path, str_path).into(),
TcpSocket(uri) => {
if str_path.is_empty() {
uri.clone()
} else {
let authority = uri.authority().expect("Authority should not be None");
Uri::builder()
.scheme(uri.scheme_str().expect("Scheme should not be None"))
.authority(authority.clone())
.path_and_query(str_path)
.build()
.expect("URI should be valid")
}
}
}
}
}
impl Display for ControlSocket {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
UnixLocalAbstractSocket(name) => f.write_fmt(format_args!("unix:@{}", name)),
UnixLocalSocket(path) => f.write_fmt(format_args!("unix:{}", path.to_string_lossy())),
TcpSocket(uri) => uri.fmt(f),
}
}
}
impl From<ControlSocket> for String {
fn from(val: ControlSocket) -> Self {
val.to_string()
}
}
impl From<ControlSocket> for PathBuf {
fn from(val: ControlSocket) -> Self {
match val {
UnixLocalAbstractSocket(socket_name) => PathBuf::from(format!("@{}", socket_name)),
UnixLocalSocket(socket_path) => socket_path,
TcpSocket(_) => PathBuf::default(),
}
}
}
impl From<ControlSocket> for Uri {
fn from(val: ControlSocket) -> Self {
val.create_uri_with_path("")
}
}
impl ControlSocket {
pub fn validate_http_address(uri: Uri) -> Result<(), UnitClientError> {
let http_address = uri.to_string();
if uri.authority().is_none() {
return Err(UnitClientError::TcpSocketAddressParseError {
message: "No authority found in socket address".to_string(),
control_socket_address: http_address,
});
}
if uri.port_u16().is_none() {
return Err(UnitClientError::TcpSocketAddressNoPortError {
control_socket_address: http_address,
});
}
if !(uri.path().is_empty() || uri.path().eq("/")) {
return Err(UnitClientError::TcpSocketAddressParseError {
message: format!("Path is not empty or is not / [path={}]", uri.path()),
control_socket_address: http_address,
});
}
Ok(())
}
pub fn validate_unix_address(socket: PathBuf) -> Result<(), UnitClientError> {
if !socket.exists() {
return Err(UnitClientError::UnixSocketNotFound {
control_socket_address: socket.to_string_lossy().to_string(),
});
}
let metadata = fs::metadata(&socket).map_err(|error| UnitClientError::UnixSocketAddressError {
source: error,
control_socket_address: socket.to_string_lossy().to_string(),
})?;
let file_type = metadata.file_type();
if !file_type.is_socket() {
return Err(UnitClientError::UnixSocketAddressError {
source: std::io::Error::new(std::io::ErrorKind::Other, "Control socket path is not a socket"),
control_socket_address: socket.to_string_lossy().to_string(),
});
}
Ok(())
}
pub fn validate(&self) -> Result<Self, UnitClientError> {
match self {
UnixLocalAbstractSocket(socket_name) => {
let socket_path = PathBuf::from(format!("@{}", socket_name));
Self::validate_unix_address(socket_path.clone())
}
UnixLocalSocket(socket_path) => Self::validate_unix_address(socket_path.clone()),
TcpSocket(socket_uri) => Self::validate_http_address(socket_uri.clone()),
}
.map(|_| self.to_owned())
}
fn normalize_and_parse_http_address(http_address: String) -> Result<Uri, UnitClientError> {
// Convert *:1 style network addresses to URI format
let address = if http_address.starts_with("*:") {
http_address.replacen("*:", "http://127.0.0.1:", 1)
// Add scheme if not present
} else if !(http_address.starts_with("http://") || http_address.starts_with("https://")) {
format!("http://{}", http_address)
} else {
http_address.to_owned()
};
let is_https = address.starts_with("https://");
let parsed_uri =
Uri::try_from(address.as_str()).map_err(|error| UnitClientError::TcpSocketAddressUriError {
source: error,
control_socket_address: address,
})?;
let authority = parsed_uri.authority().expect("Authority should not be None");
let expected_port = if is_https { HTTPS.port() } else { HTTP.port() };
let normalized_authority = match authority.port_u16() {
Some(_) => authority.to_owned(),
None => {
let host = format!("{}:{}", authority.host(), expected_port);
Authority::try_from(host.as_str()).expect("Authority should be valid")
}
};
let normalized_uri = Uri::builder()
.scheme(parsed_uri.scheme_str().expect("Scheme should not be None"))
.authority(normalized_authority)
.path_and_query(PathAndQuery::from_static(""))
.build()
.map_err(|error| UnitClientError::TcpSocketAddressParseError {
message: error.to_string(),
control_socket_address: http_address.clone(),
})?;
Ok(normalized_uri)
}
/// Flexibly parse a textual representation of a socket address
fn parse_address<S: Into<String>>(socket_address: S) -> Result<Self, UnitClientError> {
let full_socket_address: String = socket_address.into();
let socket_prefix = "unix:";
let socket_uri_prefix = "unix://";
let mut buf = String::with_capacity(socket_prefix.len());
for (i, c) in full_socket_address.char_indices() {
// Abstract unix socket with no prefix
if i == 0 && c == '@' {
return Ok(UnixLocalAbstractSocket(full_socket_address[1..].to_string()));
}
buf.push(c);
// Unix socket with prefix
if i == socket_prefix.len() - 1 && buf.eq(socket_prefix) {
let path_text = full_socket_address[socket_prefix.len()..].to_string();
// Return here if this URI does not have a scheme followed by double slashes
if !path_text.starts_with("//") {
return match path_text.strip_prefix('@') {
Some(name) => Ok(UnixLocalAbstractSocket(name.to_string())),
None => {
let path = PathBuf::from(path_text);
Ok(UnixLocalSocket(path))
}
};
}
}
// Unix socket with URI prefix
if i == socket_uri_prefix.len() - 1 && buf.eq(socket_uri_prefix) {
let uri = Uri::try_from(full_socket_address.as_str()).map_err(|error| {
UnitClientError::TcpSocketAddressParseError {
message: error.to_string(),
control_socket_address: full_socket_address.clone(),
}
})?;
return ControlSocket::try_from(uri);
}
}
/* Sockets on Windows are not supported, so there is no need to check
* if the socket address is a valid path, so we can do this shortcut
* here to see if a path was specified without a unix: prefix. */
if buf.starts_with(MAIN_SEPARATOR) {
let path = PathBuf::from(buf);
return Ok(UnixLocalSocket(path));
}
let uri = Self::normalize_and_parse_http_address(buf)?;
Ok(TcpSocket(uri))
}
pub fn is_local_socket(&self) -> bool {
match self {
UnixLocalAbstractSocket(_) | UnixLocalSocket(_) => true,
TcpSocket(_) => false,
}
}
}
impl TryFrom<String> for ControlSocket {
type Error = UnitClientError;
fn try_from(socket_address: String) -> Result<Self, Self::Error> {
ControlSocket::parse_address(socket_address.as_str())
}
}
impl TryFrom<&str> for ControlSocket {
type Error = UnitClientError;
fn try_from(socket_address: &str) -> Result<Self, Self::Error> {
ControlSocket::parse_address(socket_address)
}
}
impl TryFrom<Uri> for ControlSocket {
type Error = UnitClientError;
fn try_from(socket_uri: Uri) -> Result<Self, Self::Error> {
match socket_uri.scheme_str() {
// URIs with the unix scheme will have a hostname that is a hex encoded string
// representing the path to the socket
Some("unix") => {
let host = match socket_uri.host() {
Some(host) => host,
None => {
return Err(UnitClientError::TcpSocketAddressParseError {
message: "No host found in socket address".to_string(),
control_socket_address: socket_uri.to_string(),
})
}
};
let bytes = hex::decode(host).map_err(|error| UnitClientError::TcpSocketAddressParseError {
message: error.to_string(),
control_socket_address: socket_uri.to_string(),
})?;
let path = String::from_utf8_lossy(&bytes);
ControlSocket::parse_address(path)
}
Some("http") | Some("https") => Ok(TcpSocket(socket_uri)),
Some(unknown) => Err(UnitClientError::TcpSocketAddressParseError {
message: format!("Unsupported scheme found in socket address: {}", unknown).to_string(),
control_socket_address: socket_uri.to_string(),
}),
None => Err(UnitClientError::TcpSocketAddressParseError {
message: "No scheme found in socket address".to_string(),
control_socket_address: socket_uri.to_string(),
}),
}
}
}
#[cfg(test)]
mod tests {
use rand::distributions::{Alphanumeric, DistString};
use std::env::temp_dir;
use std::fmt::Display;
use std::io;
use std::os::unix::net::UnixListener;
use super::*;
struct TempSocket {
socket_path: PathBuf,
_listener: UnixListener,
}
impl TempSocket {
fn shutdown(&mut self) -> io::Result<()> {
fs::remove_file(&self.socket_path)
}
}
impl Display for TempSocket {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "unix:{}", self.socket_path.to_string_lossy().to_string())
}
}
impl Drop for TempSocket {
fn drop(&mut self) {
self.shutdown()
.expect(format!("Unable to shutdown socket {}", self.socket_path.to_string_lossy()).as_str());
}
}
#[test]
fn will_error_with_nonexistent_unix_socket() {
let socket_address = "unix:/tmp/some_random_filename_that_doesnt_exist.sock";
let control_socket =
ControlSocket::try_from(socket_address).expect("No error should be returned until validate() is called");
assert!(control_socket.is_local_socket(), "Not parsed as a local socket");
assert!(control_socket.validate().is_err(), "Socket should not be valid");
}
#[test]
fn can_parse_socket_with_prefix() {
let temp_socket = create_file_socket().expect("Unable to create socket");
let control_socket = ControlSocket::try_from(temp_socket.to_string()).expect("Error parsing good socket path");
assert!(control_socket.is_local_socket(), "Not parsed as a local socket");
if let Err(e) = control_socket.validate() {
panic!("Socket should be valid: {}", e);
}
}
#[test]
fn can_parse_socket_from_uri() {
let temp_socket = create_file_socket().expect("Unable to create socket");
let uri: Uri = hyperlocal::Uri::new(temp_socket.socket_path.clone(), "").into();
let control_socket = ControlSocket::try_from(uri).expect("Error parsing good socket path");
assert!(control_socket.is_local_socket(), "Not parsed as a local socket");
if let Err(e) = control_socket.validate() {
panic!("Socket should be valid: {}", e);
}
}
#[test]
fn can_parse_socket_from_uri_text() {
let temp_socket = create_file_socket().expect("Unable to create socket");
let uri: Uri = hyperlocal::Uri::new(temp_socket.socket_path.clone(), "").into();
let control_socket = ControlSocket::parse_address(uri.to_string()).expect("Error parsing good socket path");
assert!(control_socket.is_local_socket(), "Not parsed as a local socket");
if let Err(e) = control_socket.validate() {
panic!("Socket for input text should be valid: {}", e);
}
}
#[test]
#[cfg(target_os = "linux")]
fn can_parse_abstract_socket_from_uri() {
let temp_socket = create_abstract_socket().expect("Unable to create socket");
let uri: Uri = hyperlocal::Uri::new(temp_socket.socket_path.clone(), "").into();
let control_socket = ControlSocket::try_from(uri).expect("Error parsing good socket path");
assert!(control_socket.is_local_socket(), "Not parsed as a local socket");
if let Err(e) = control_socket.validate() {
panic!("Socket should be valid: {}", e);
}
}
#[test]
#[cfg(target_os = "linux")]
fn can_parse_abstract_socket_from_uri_text() {
let temp_socket = create_abstract_socket().expect("Unable to create socket");
let uri: Uri = hyperlocal::Uri::new(temp_socket.socket_path.clone(), "").into();
let control_socket = ControlSocket::parse_address(uri.to_string()).expect("Error parsing good socket path");
assert!(control_socket.is_local_socket(), "Not parsed as a local socket");
if let Err(e) = control_socket.validate() {
panic!("Socket should be valid: {}", e);
}
}
#[test]
fn can_parse_socket_without_prefix() {
let temp_socket = create_file_socket().expect("Unable to create socket");
let control_socket = ControlSocket::try_from(temp_socket.socket_path.to_string_lossy().to_string())
.expect("Error parsing good socket path");
assert!(control_socket.is_local_socket(), "Not parsed as a local socket");
if let Err(e) = control_socket.validate() {
panic!("Socket should be valid: {}", e);
}
}
#[cfg(target_os = "linux")]
#[test]
fn can_parse_abstract_socket() {
let temp_socket = create_abstract_socket().expect("Unable to create socket");
let control_socket = ControlSocket::try_from(temp_socket.to_string()).expect("Error parsing good socket path");
assert!(control_socket.is_local_socket(), "Not parsed as a local socket");
if let Err(e) = control_socket.validate() {
panic!("Socket should be valid: {}", e);
}
}
#[test]
fn can_normalize_good_http_socket_addresses() {
let valid_socket_addresses = vec![
"http://127.0.0.1:8080",
"https://127.0.0.1:8080",
"http://127.0.0.1:8080/",
"127.0.0.1:8080",
"http://0.0.0.0:8080",
"https://0.0.0.0:8080",
"http://0.0.0.0:8080/",
"0.0.0.0:8080",
"http://localhost:8080",
"https://localhost:8080",
"http://localhost:8080/",
"localhost:8080",
"http://[::1]:8080",
"https://[::1]:8080",
"http://[::1]:8080/",
"[::1]:8080",
"http://[0000:0000:0000:0000:0000:0000:0000:0000]:8080",
"https://[0000:0000:0000:0000:0000:0000:0000:0000]:8080",
"http://[0000:0000:0000:0000:0000:0000:0000:0000]:8080/",
"[0000:0000:0000:0000:0000:0000:0000:0000]:8080",
];
for socket_address in valid_socket_addresses {
let mut expected = if socket_address.starts_with("http") {
socket_address.to_string().trim_end_matches('/').to_string()
} else {
format!("http://{}", socket_address).trim_end_matches('/').to_string()
};
expected.push('/');
let control_socket = ControlSocket::try_from(socket_address).expect("Error parsing good socket path");
assert!(!control_socket.is_local_socket(), "Not parsed as a local socket");
if let Err(e) = control_socket.validate() {
panic!("Socket should be valid: {}", e);
}
}
}
#[test]
fn can_normalize_wildcard_http_socket_address() {
let socket_address = "*:8080";
let expected = "http://127.0.0.1:8080/";
let normalized_result = ControlSocket::normalize_and_parse_http_address(socket_address.to_string());
let normalized = normalized_result
.expect("Unable to normalize socket address")
.to_string();
assert_eq!(normalized, expected);
}
#[test]
fn can_normalize_http_socket_address_with_no_port() {
let socket_address = "http://localhost";
let expected = "http://localhost:80/";
let normalized_result = ControlSocket::normalize_and_parse_http_address(socket_address.to_string());
let normalized = normalized_result
.expect("Unable to normalize socket address")
.to_string();
assert_eq!(normalized, expected);
}
#[test]
fn can_normalize_https_socket_address_with_no_port() {
let socket_address = "https://localhost";
let expected = "https://localhost:443/";
let normalized_result = ControlSocket::normalize_and_parse_http_address(socket_address.to_string());
let normalized = normalized_result
.expect("Unable to normalize socket address")
.to_string();
assert_eq!(normalized, expected);
}
#[test]
fn can_parse_http_addresses() {
let valid_socket_addresses = vec![
"http://127.0.0.1:8080",
"https://127.0.0.1:8080",
"http://127.0.0.1:8080/",
"127.0.0.1:8080",
"http://0.0.0.0:8080",
"https://0.0.0.0:8080",
"http://0.0.0.0:8080/",
"0.0.0.0:8080",
"http://localhost:8080",
"https://localhost:8080",
"http://localhost:8080/",
"localhost:8080",
"http://[::1]:8080",
"https://[::1]:8080",
"http://[::1]:8080/",
"[::1]:8080",
"http://[0000:0000:0000:0000:0000:0000:0000:0000]:8080",
"https://[0000:0000:0000:0000:0000:0000:0000:0000]:8080",
"http://[0000:0000:0000:0000:0000:0000:0000:0000]:8080/",
"[0000:0000:0000:0000:0000:0000:0000:0000]:8080",
];
for socket_address in valid_socket_addresses {
let mut expected = if socket_address.starts_with("http") {
socket_address.to_string().trim_end_matches('/').to_string()
} else {
format!("http://{}", socket_address).trim_end_matches('/').to_string()
};
expected.push('/');
let normalized = ControlSocket::normalize_and_parse_http_address(socket_address.to_string())
.expect("Unable to normalize socket address")
.to_string();
assert_eq!(normalized, expected);
}
}
fn create_file_socket() -> Result<TempSocket, io::Error> {
let random = Alphanumeric.sample_string(&mut rand::thread_rng(), 10);
let socket_name = format!("unit-client-socket-test-{}.sock", random);
let socket_path = temp_dir().join(socket_name);
let listener = UnixListener::bind(&socket_path)?;
Ok(TempSocket {
socket_path,
_listener: listener,
})
}
#[cfg(target_os = "linux")]
fn create_abstract_socket() -> Result<TempSocket, io::Error> {
let random = Alphanumeric.sample_string(&mut rand::thread_rng(), 10);
let socket_name = format!("@unit-client-socket-test-{}.sock", random);
let socket_path = PathBuf::from(socket_name);
let listener = UnixListener::bind(&socket_path)?;
Ok(TempSocket {
socket_path,
_listener: listener,
})
}
}
|