Skip to content

Commit

Permalink
Merge pull request #876 from portier/feat/less-deps
Browse files Browse the repository at this point in the history
Reduce dependencies
  • Loading branch information
stephank committed May 3, 2024
2 parents 26b682a + 8dc40c2 commit 0043fe4
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 55 deletions.
36 changes: 0 additions & 36 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 3 additions & 8 deletions Cargo.toml
Expand Up @@ -43,9 +43,7 @@ http-body-util = "0.1.0"
hyper-staticfile = "0.10.0"
idna = "0.5.0"
ipnetwork = "0.20.0"
lazy_static = "1.4.0"
listenfd = "1.0.0"
matches = "0.1.8"
mustache = "0.9.0"
percent-encoding = "2.1.0"
serde_json = "1.0.57"
Expand All @@ -67,12 +65,12 @@ default-features = false

[dependencies.hyper]
version = "1.0.0"
features = ["full"]
default-features = false
features = ["http1", "server"]

[dependencies.hyper-rustls]
optional = true
version = "0.27.0"
features = ["http2"]

[dependencies.hyper-tls]
optional = true
Expand Down Expand Up @@ -105,7 +103,7 @@ features = ["script", "tokio-comp"]
[dependencies.reqwest]
version = "0.12.3"
default-features = false
features = ["http2", "macos-system-configuration"]
features = ["macos-system-configuration"]

# Alias ring -> aws-lc-rs.
# Both are nearly API compatible, and Rustls also switched backends.
Expand Down Expand Up @@ -145,9 +143,6 @@ features = ["tokio-runtime"]
version = "2.1.1"
features = ["serde"]

[target.'cfg(unix)'.dependencies]
sd-notify = "0.4.0"

# Per `rsa` crate docs, significantly speeds up key generation for debug builds.
[profile.dev.package.num-bigint-dig]
opt-level = 3
2 changes: 1 addition & 1 deletion src/email_address.rs
Expand Up @@ -3,7 +3,7 @@ use std::hash::{Hash, Hasher};
use thiserror::Error;

fn is_invalid_domain_char(c: char) -> bool {
matches::matches!(
matches!(
c,
'\0' | '\t' | '\n' | '\r' | ' ' | '#' | '%' | '/' | ':' | '?' | '@' | '[' | '\\' | ']'
)
Expand Down
10 changes: 5 additions & 5 deletions src/main.rs
Expand Up @@ -219,8 +219,9 @@ async fn start_server(builder: ConfigBuilder) {
let mut connections = JoinSet::new();

#[cfg(unix)]
sd_notify::notify(true, &[sd_notify::NotifyState::Ready])
.expect("Failed to signal ready to the service manager");
let sd_notify = utils::SdNotify::new();
#[cfg(unix)]
sd_notify.notify_ready();

// Snippet for the connection loop with graceful shutdown.
// This is shared between TCP and Unix socket code.
Expand Down Expand Up @@ -281,9 +282,8 @@ async fn start_server(builder: ConfigBuilder) {
}

#[cfg(unix)]
if let Err(err) = sd_notify::notify(true, &[sd_notify::NotifyState::Stopping]) {
log::error!("Failed to signal stopping to the service manager: {}", err);
}
sd_notify.notify_stopping();

while connections.join_next().await.is_some() {}
log::info!("Shutdown complete");
}
Expand Down
5 changes: 5 additions & 0 deletions src/utils/mod.rs
Expand Up @@ -20,5 +20,10 @@ pub use real_ip::*;
pub use rng::*;
pub use time::*;

#[cfg(unix)]
mod sd_notify;
#[cfg(unix)]
pub use sd_notify::*;

pub type BoxError = Box<dyn Error + Send + Sync>;
pub type BoxFuture<T> = Pin<Box<dyn Future<Output = T> + Send>>;
8 changes: 3 additions & 5 deletions src/utils/real_ip.rs
Expand Up @@ -2,9 +2,7 @@ use http::{header::HeaderName, Request};
use ipnetwork::IpNetwork;
use std::net::{IpAddr, SocketAddr};

lazy_static::lazy_static! {
static ref X_FORWARDED_FOR: HeaderName = HeaderName::from_static("x-forwarded-for");
}
static X_FORWARDED_FOR: HeaderName = HeaderName::from_static("x-forwarded-for");

/// Get the real IP-address of the client.
///
Expand All @@ -20,7 +18,7 @@ pub fn real_ip<B>(
) -> IpAddr {
let list: Vec<_> = req
.headers()
.get(&*X_FORWARDED_FOR)
.get(&X_FORWARDED_FOR)
.and_then(|input| input.to_str().ok())
.and_then(|input| {
input
Expand Down Expand Up @@ -134,7 +132,7 @@ mod tests {
let mut req = http::Request::new(());
if !header.is_empty() {
req.headers_mut()
.insert(&*X_FORWARDED_FOR, HeaderValue::from_static(header));
.insert(&X_FORWARDED_FOR, HeaderValue::from_static(header));
}

let ip = real_ip(received_from, &req, &trusted);
Expand Down
41 changes: 41 additions & 0 deletions src/utils/sd_notify.rs
@@ -0,0 +1,41 @@
use std::{env, ffi::OsString, io, os::unix::net::UnixDatagram};

// Sends state notifications to systemd.
//
// Based on https://github.com/lnicola/sd-notify (MIT/Apache-2.0)
pub struct SdNotify {
path: Option<OsString>,
}

impl SdNotify {
pub fn new() -> Self {
let path = env::var_os("NOTIFY_SOCKET");
env::remove_var("NOTIFY_SOCKET");
Self { path }
}

pub fn notify_ready(&self) {
self.send(b"READY=1\n")
.expect("Failed to signal ready to the service manager");
}

pub fn notify_stopping(&self) {
if let Err(err) = self.send(b"STOPPING=1\n") {
log::error!("Failed to signal stopping to the service manager: {err}");
}
}

fn send(&self, msg: &[u8]) -> io::Result<()> {
let Some(path) = self.path.as_ref() else {
return Ok(());
};

let sock = UnixDatagram::unbound()?;
let len = sock.send_to(msg, path)?;
if len != msg.len() {
return Err(io::Error::new(io::ErrorKind::WriteZero, "incomplete write"));
}

Ok(())
}
}

0 comments on commit 0043fe4

Please sign in to comment.