lynx_core/
utils.rs

1use std::path::PathBuf;
2use std::time::{SystemTime, UNIX_EPOCH};
3
4use anyhow::{anyhow, Error};
5use http_body_util::combinators::BoxBody;
6use http_body_util::{BodyExt, Empty, Full};
7use hyper::body::Bytes;
8
9pub fn host_addr(uri: &http::Uri) -> Option<String> {
10    uri.authority().map(|auth| auth.to_string())
11}
12
13pub fn empty() -> BoxBody<Bytes, Error> {
14    Empty::<Bytes>::new()
15        .map_err(|never| anyhow!(never))
16        .boxed()
17}
18
19pub fn full<T: Into<Bytes>>(chunk: T) -> BoxBody<Bytes, Error> {
20    Full::new(chunk.into())
21        .map_err(|never| anyhow!(never))
22        .boxed()
23}
24
25pub fn is_http(uri: &http::Uri) -> bool {
26    uri.scheme_str().map(|s| s == "http").unwrap_or(false)
27}
28
29pub fn is_https(uri: &http::Uri) -> bool {
30    matches!(uri.port_u16(), Some(443))
31}
32
33pub fn get_current_timestamp_millis() -> u128 {
34    let start = SystemTime::now();
35    let since_the_epoch = start
36        .duration_since(UNIX_EPOCH)
37        .expect("Time went backwards");
38    since_the_epoch.as_millis()
39}
40
41pub async fn read_file(file_path: PathBuf) -> anyhow::Result<String> {
42    let content = tokio::fs::read_to_string(file_path).await?;
43    Ok(content)
44}