forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.rs
More file actions
34 lines (29 loc) · 977 Bytes
/
utils.rs
File metadata and controls
34 lines (29 loc) · 977 Bytes
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
use crate::common::*;
use std::error::Error;
use std::fmt::Write;
use std::net::SocketAddr;
use std::time::SystemTime;
pub fn get_connection_id(remote_address: &SocketAddr) -> ConnectionId {
match std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH) {
Ok(duration) => ConnectionId(
((duration.as_secs() / 3600) | ((remote_address.port() as u64) << 36)) as i64,
),
Err(_) => ConnectionId(0x7FFFFFFFFFFFFFFF),
}
}
pub fn current_time() -> u64 {
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs()
}
pub fn url_encode_bytes(content: &[u8]) -> Result<String, Box<dyn Error>> {
let mut out: String = String::new();
for byte in content.iter() {
match *byte as char {
'0'..='9' | 'a'..='z' | 'A'..='Z' | '.' | '-' | '_' | '~' => out.push(*byte as char),
_ => write!(&mut out, "%{:02x}", byte)?,
};
}
Ok(out)
}