servlet 1.0.0

A small application runtime with an explicit operational contract
Documentation
//! Pure checks performed before the future startup path acquires resources.

use std::net::SocketAddr;

use super::{AppConfig, ConfigError};

impl AppConfig {
    /// Validate capacities, budgets, and administrative listener intent.
    ///
    /// This performs no I/O. Actual address availability and platform-specific
    /// dual-stack binding behavior must also be checked when acquiring listeners.
    pub fn validate(&self) -> Result<(), ConfigError> {
        for (field, value) in [
            ("request_body_limit", self.request_body_limit),
            ("max_in_flight", self.max_in_flight),
            ("max_connections", self.max_connections),
        ] {
            if value == 0 {
                return Err(ConfigError::ZeroValue(field));
            }
        }
        if self.max_in_flight > tokio::sync::Semaphore::MAX_PERMITS {
            return Err(ConfigError::CapacityTooLarge);
        }
        for (field, value) in [
            ("header_timeout", self.header_timeout),
            ("body_idle_timeout", self.body_idle_timeout),
            ("request_timeout", self.request_timeout),
            ("startup_timeout", self.startup_timeout),
            ("shutdown_timeout", self.shutdown_timeout),
            ("cleanup_reserve", self.cleanup_reserve),
        ] {
            if std::time::Instant::now().checked_add(value).is_none() {
                return Err(ConfigError::UnrepresentableBudget(field));
            }
            if value.is_zero() {
                return Err(ConfigError::ZeroValue(field));
            }
        }
        if self.cleanup_reserve >= self.shutdown_timeout {
            return Err(ConfigError::CleanupReserveExhaustsShutdown);
        }
        if let Some(admin) = self.admin_bind {
            if !admin.ip().is_loopback() && !self.allow_remote_admin {
                return Err(ConfigError::RemoteAdminRequiresOptIn);
            }
            if overlaps(self.bind, admin) {
                return Err(ConfigError::ConflictingListeners);
            }
        }
        Ok(())
    }
}

fn overlaps(application: SocketAddr, admin: SocketAddr) -> bool {
    application.port() != 0
        && application.port() == admin.port()
        && application.is_ipv4() == admin.is_ipv4()
        && (application.ip() == admin.ip()
            || application.ip().is_unspecified()
            || admin.ip().is_unspecified())
}

#[cfg(test)]
#[path = "validation_test.rs"]
mod validation_test;