use std::time::Duration;
use crate::constants::DEFAULT_MAX_RETRIES;
use crate::net::transport::Transport;
#[derive(Debug, Clone)]
pub enum PingOutcome {
Ok(String),
Failed(String),
}
#[must_use]
pub fn ping_server(transport: &Transport, url: &str, timeout: Duration) -> PingOutcome {
let mut wsdl_url = url.trim_end_matches('/').to_owned();
if !wsdl_url.contains('?') {
wsdl_url.push_str("?WSDL");
}
let raw = match transport.get(&wsdl_url, timeout, DEFAULT_MAX_RETRIES) {
Ok(bytes) => bytes,
Err(e) if e.is_tls() => return PingOutcome::Failed(e.to_string()),
Err(e) => return PingOutcome::Failed(format!("Connection failed: {e}")),
};
let body = String::from_utf8_lossy(&raw);
if body.contains("DssSign") && body.contains("SAPIWS") {
return PingOutcome::Ok("CoSign DSS endpoint confirmed".to_owned());
}
if body.contains("<wsdl:") || body.contains("<definitions") {
return PingOutcome::Ok("WSDL found (may not be CoSign)".to_owned());
}
PingOutcome::Failed("Not a recognized CoSign endpoint".to_owned())
}