capnweb_server/
h3_server.rs

1use crate::Server;
2use quinn::{Endpoint, ServerConfig as QuinnServerConfig};
3use std::net::SocketAddr;
4use std::sync::Arc;
5use tracing::info;
6
7/// HTTP/3 server for Cap'n Web protocol
8pub struct H3Server {
9    #[allow(dead_code)]
10    server: Arc<Server>,
11    endpoint: Option<Endpoint>,
12}
13
14impl H3Server {
15    /// Create a new HTTP/3 server
16    pub fn new(server: Arc<Server>) -> Self {
17        Self {
18            server,
19            endpoint: None,
20        }
21    }
22
23    /// Start the HTTP/3 server on the specified address
24    pub async fn listen(&mut self, addr: SocketAddr) -> Result<(), Box<dyn std::error::Error>> {
25        // Create server configuration with self-signed certificate for testing
26        let server_config = configure_server()?;
27
28        // Create endpoint
29        let endpoint = Endpoint::server(server_config, addr)?;
30        info!("HTTP/3 server listening on {}", addr);
31
32        self.endpoint = Some(endpoint.clone());
33
34        // For now, just accept connections without processing
35        // Full H3 implementation would require more complex stream handling
36        while let Some(_incoming) = endpoint.accept().await {
37            // TODO: Implement H3 request handling when API stabilizes
38        }
39
40        Ok(())
41    }
42
43    /// Shutdown the server
44    pub fn shutdown(&mut self) {
45        if let Some(endpoint) = &self.endpoint {
46            endpoint.close(0u32.into(), b"server shutdown");
47        }
48    }
49}
50
51/// Configure server with self-signed certificate for testing
52fn configure_server() -> Result<QuinnServerConfig, Box<dyn std::error::Error>> {
53    let rcgen::CertifiedKey { cert, signing_key } =
54        rcgen::generate_simple_self_signed(vec!["localhost".into()])?;
55    let cert_der = rustls::pki_types::CertificateDer::from(cert.der().to_vec());
56    let priv_key = rustls::pki_types::PrivateKeyDer::try_from(signing_key.serialize_der())?;
57
58    let cert_chain = vec![cert_der];
59
60    let mut server_config = rustls::ServerConfig::builder()
61        .with_no_client_auth()
62        .with_single_cert(cert_chain, priv_key)?;
63
64    server_config.alpn_protocols = vec![b"h3".to_vec()];
65
66    let server_config = QuinnServerConfig::with_crypto(Arc::new(
67        quinn::crypto::rustls::QuicServerConfig::try_from(server_config)?,
68    ));
69
70    Ok(server_config)
71}
72
73#[cfg(test)]
74mod tests {
75    #[test]
76    fn test_h3_server_creation() {
77        // Basic test to ensure the module compiles
78        // Full integration tests would require running server
79    }
80}