capnweb-transport 0.1.0

Transport layer implementations for Cap'n Web protocol (HTTP, WebSocket, WebTransport)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
// HTTP/3 Transport Implementation for Cap'n Web Protocol
// Provides HTTP/3 support with QUIC multiplexing for high-performance RPC

use crate::{RpcTransport, TransportError};
use async_trait::async_trait;
use capnweb_core::{decode_message, encode_message, Message};
use quinn::{Connection, Endpoint, RecvStream, SendStream};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::{mpsc, Mutex, RwLock};

/// HTTP/3 transport configuration
#[derive(Debug, Clone)]
pub struct Http3Config {
    /// Maximum concurrent streams per connection
    pub max_concurrent_streams: u32,
    /// Stream idle timeout in seconds
    pub stream_idle_timeout: u64,
    /// Connection idle timeout in seconds
    pub connection_idle_timeout: u64,
    /// Enable stream multiplexing
    pub enable_multiplexing: bool,
    /// Request compression
    pub enable_compression: bool,
}

impl Default for Http3Config {
    fn default() -> Self {
        Self {
            max_concurrent_streams: 1000,
            stream_idle_timeout: 30,
            connection_idle_timeout: 300,
            enable_multiplexing: true,
            enable_compression: false, // Disabled by default for simplicity
        }
    }
}

/// HTTP/3 request/response headers
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Http3Headers {
    pub method: String,
    pub path: String,
    pub authority: String,
    pub scheme: String,
    pub content_type: String,
    pub user_agent: String,
    pub custom_headers: HashMap<String, String>,
}

impl Default for Http3Headers {
    fn default() -> Self {
        Self {
            method: "POST".to_string(),
            path: "/rpc".to_string(),
            authority: "localhost".to_string(),
            scheme: "https".to_string(),
            content_type: "application/json".to_string(),
            user_agent: "CapnWeb-Rust/1.0".to_string(),
            custom_headers: HashMap::new(),
        }
    }
}

/// HTTP/3 stream handler for individual RPC streams
#[derive(Debug)]
struct Http3Stream {
    stream_id: u64,
    send_stream: SendStream,
    recv_stream: RecvStream,
    headers: Http3Headers,
}

impl Http3Stream {
    /// Create a new HTTP/3 stream
    fn new(stream_id: u64, send_stream: SendStream, recv_stream: RecvStream) -> Self {
        Self {
            stream_id,
            send_stream,
            recv_stream,
            headers: Http3Headers::default(),
        }
    }

    /// Send an HTTP/3 request with RPC payload
    async fn send_request(&mut self, message: &Message) -> Result<(), TransportError> {
        // Encode the message
        let payload = encode_message(message).map_err(|e| TransportError::Codec(e.to_string()))?;

        // Create HTTP/3 pseudo-headers (simplified version)
        let headers_frame = self.create_headers_frame(&payload)?;

        // Send headers frame
        self.send_stream
            .write_all(&headers_frame)
            .await
            .map_err(|e| TransportError::Io(std::io::Error::new(std::io::ErrorKind::Other, e)))?;

        // Send data frame
        let data_frame = self.create_data_frame(&payload)?;
        self.send_stream
            .write_all(&data_frame)
            .await
            .map_err(|e| TransportError::Io(std::io::Error::new(std::io::ErrorKind::Other, e)))?;

        self.send_stream
            .finish()
            .await
            .map_err(|e| TransportError::Io(std::io::Error::new(std::io::ErrorKind::Other, e)))?;

        tracing::trace!(
            stream_id = self.stream_id,
            payload_size = payload.len(),
            "HTTP/3 request sent"
        );
        Ok(())
    }

    /// Receive an HTTP/3 response
    async fn receive_response(&mut self) -> Result<Message, TransportError> {
        // Read HTTP/3 frames (simplified implementation)
        // In a full HTTP/3 implementation, we would parse actual HTTP/3 frames

        // For now, we'll read a length-prefixed message similar to our other transports
        let mut len_bytes = [0u8; 4];
        self.recv_stream
            .read_exact(&mut len_bytes)
            .await
            .map_err(|e| TransportError::Protocol(format!("Failed to read length: {}", e)))?;

        let len = u32::from_be_bytes(len_bytes) as usize;

        let mut payload = vec![0u8; len];
        self.recv_stream
            .read_exact(&mut payload)
            .await
            .map_err(|e| TransportError::Protocol(format!("Failed to read payload: {}", e)))?;

        let message = decode_message(&payload).map_err(|e| TransportError::Codec(e.to_string()))?;

        tracing::trace!(
            stream_id = self.stream_id,
            payload_size = payload.len(),
            "HTTP/3 response received"
        );
        Ok(message)
    }

    /// Create HTTP/3 HEADERS frame (simplified)
    fn create_headers_frame(&self, payload: &[u8]) -> Result<Vec<u8>, TransportError> {
        // Simplified HTTP/3 HEADERS frame
        // In a real implementation, this would use QPACK compression
        let mut frame = Vec::new();

        // Frame type (HEADERS = 0x01)
        frame.push(0x01);

        // Frame length (placeholder - would be calculated properly)
        frame.extend_from_slice(&(200u32.to_be_bytes())); // Placeholder length

        // Pseudo-headers (simplified encoding)
        frame.extend_from_slice(format!(":method {}\n", self.headers.method).as_bytes());
        frame.extend_from_slice(format!(":path {}\n", self.headers.path).as_bytes());
        frame.extend_from_slice(format!(":authority {}\n", self.headers.authority).as_bytes());
        frame.extend_from_slice(format!(":scheme {}\n", self.headers.scheme).as_bytes());
        frame.extend_from_slice(format!("content-type {}\n", self.headers.content_type).as_bytes());
        frame.extend_from_slice(format!("content-length {}\n", payload.len()).as_bytes());
        frame.extend_from_slice(format!("user-agent {}\n", self.headers.user_agent).as_bytes());

        // Add custom headers
        for (name, value) in &self.headers.custom_headers {
            frame.extend_from_slice(format!("{} {}\n", name, value).as_bytes());
        }

        Ok(frame)
    }

    /// Create HTTP/3 DATA frame (simplified)
    fn create_data_frame(&self, payload: &[u8]) -> Result<Vec<u8>, TransportError> {
        let mut frame = Vec::new();

        // Frame type (DATA = 0x00)
        frame.push(0x00);

        // Frame length
        frame.extend_from_slice(&(payload.len() as u32).to_be_bytes());

        // Payload
        frame.extend_from_slice(payload);

        Ok(frame)
    }
}

/// HTTP/3 transport implementation
pub struct Http3Transport {
    /// QUIC connection
    connection: Arc<Connection>,
    /// Active streams
    streams: Arc<RwLock<HashMap<u64, Http3Stream>>>,
    /// Stream counter
    next_stream_id: Arc<Mutex<u64>>,
    /// Configuration
    config: Http3Config,
    /// Message queues for multiplexing
    outgoing_queue: Arc<Mutex<mpsc::UnboundedReceiver<Message>>>,
    incoming_queue: Arc<Mutex<mpsc::UnboundedSender<Message>>>,
    /// Queue senders/receivers
    queue_tx: mpsc::UnboundedSender<Message>,
    queue_rx: mpsc::UnboundedReceiver<Message>,
}

impl Http3Transport {
    /// Create a new HTTP/3 transport
    pub fn new(connection: Connection, config: Http3Config) -> Self {
        let (queue_tx, queue_rx) = mpsc::unbounded_channel();
        let (incoming_tx, incoming_rx) = mpsc::unbounded_channel();

        Self {
            connection: Arc::new(connection),
            streams: Arc::new(RwLock::new(HashMap::new())),
            next_stream_id: Arc::new(Mutex::new(0)),
            config,
            outgoing_queue: Arc::new(Mutex::new(queue_rx)),
            incoming_queue: Arc::new(Mutex::new(incoming_tx)),
            queue_tx,
            queue_rx: incoming_rx,
        }
    }

    /// Create a new stream for HTTP/3 communication
    async fn create_stream(&self) -> Result<u64, TransportError> {
        let (send_stream, recv_stream) = self.connection.open_bi().await.map_err(|e| {
            TransportError::Protocol(format!("Failed to open bidirectional stream: {}", e))
        })?;

        let mut stream_id_guard = self.next_stream_id.lock().await;
        let stream_id = *stream_id_guard;
        *stream_id_guard += 1;

        let stream = Http3Stream::new(stream_id, send_stream, recv_stream);
        self.streams.write().await.insert(stream_id, stream);

        tracing::debug!(stream_id = stream_id, "HTTP/3 stream created");
        Ok(stream_id)
    }

    /// Get or create a stream for sending
    async fn get_send_stream(&self) -> Result<u64, TransportError> {
        if self.config.enable_multiplexing {
            // Create a new stream for each message when multiplexing
            self.create_stream().await
        } else {
            // Reuse the first stream when not multiplexing
            let streams = self.streams.read().await;
            if let Some(&stream_id) = streams.keys().next() {
                Ok(stream_id)
            } else {
                drop(streams);
                self.create_stream().await
            }
        }
    }

    /// Process outgoing messages in the background
    pub async fn start_background_processing(&self) {
        let streams = self.streams.clone();
        let config = self.config.clone();
        let connection = self.connection.clone();

        tokio::spawn(async move {
            tracing::info!("HTTP/3 background processing started");

            // This would handle connection management, stream cleanup, etc.
            loop {
                tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;

                // Clean up idle streams
                let mut streams_lock = streams.write().await;
                let initial_count = streams_lock.len();

                // Remove streams that have been idle (simplified logic)
                streams_lock.retain(|&stream_id, _stream| {
                    // In a real implementation, track stream activity timestamps
                    true // Keep all streams for now
                });

                if streams_lock.len() != initial_count {
                    tracing::debug!(
                        cleaned_streams = initial_count - streams_lock.len(),
                        remaining_streams = streams_lock.len(),
                        "Cleaned up idle HTTP/3 streams"
                    );
                }
            }
        });
    }

    /// Get connection statistics
    pub async fn get_stats(&self) -> Http3Stats {
        let streams = self.streams.read().await;
        let connection_stats = self.connection.stats();

        Http3Stats {
            active_streams: streams.len(),
            max_concurrent_streams: self.config.max_concurrent_streams,
            bytes_sent: connection_stats.udp_tx.bytes,
            bytes_received: connection_stats.udp_rx.bytes,
            packets_sent: connection_stats.udp_tx.datagrams,
            packets_received: connection_stats.udp_rx.datagrams,
            connection_errors: 0, // Would track errors in real implementation
            stream_errors: 0,     // Would track errors in real implementation
        }
    }
}

#[async_trait]
impl RpcTransport for Http3Transport {
    async fn send(&mut self, message: Message) -> Result<(), TransportError> {
        let stream_id = self.get_send_stream().await?;

        let mut streams = self.streams.write().await;
        let stream = streams
            .get_mut(&stream_id)
            .ok_or_else(|| TransportError::Protocol("Stream not found".to_string()))?;

        stream.send_request(&message).await?;

        tracing::trace!(stream_id = stream_id, "Message sent via HTTP/3");
        Ok(())
    }

    async fn recv(&mut self) -> Result<Option<Message>, TransportError> {
        // In a real implementation, this would coordinate with background tasks
        // that handle incoming streams and populate the incoming queue

        match self.queue_rx.try_recv() {
            Ok(message) => {
                tracing::trace!("Message received via HTTP/3");
                Ok(Some(message))
            }
            Err(mpsc::error::TryRecvError::Empty) => Ok(None),
            Err(mpsc::error::TryRecvError::Disconnected) => Err(TransportError::ConnectionClosed),
        }
    }

    async fn close(&mut self) -> Result<(), TransportError> {
        tracing::info!("Closing HTTP/3 transport");

        // Close all streams
        let mut streams = self.streams.write().await;
        streams.clear();

        // Close the QUIC connection
        self.connection.close(0u32.into(), b"transport closed");

        tracing::info!("HTTP/3 transport closed");
        Ok(())
    }
}

/// HTTP/3 client for establishing connections
pub struct Http3Client {
    endpoint: Endpoint,
    config: Http3Config,
}

impl Http3Client {
    /// Create a new HTTP/3 client
    pub fn new(endpoint: Endpoint, config: Http3Config) -> Self {
        Self { endpoint, config }
    }

    /// Connect to an HTTP/3 server
    pub async fn connect(&self, server_addr: &str) -> Result<Http3Transport, TransportError> {
        tracing::info!(server_addr = server_addr, "Connecting to HTTP/3 server");

        let connection = self
            .endpoint
            .connect(
                server_addr.parse().map_err(|e| {
                    TransportError::Protocol(format!("Invalid server address: {}", e))
                })?,
                "localhost",
            )
            .map_err(|e| TransportError::Protocol(format!("Connection failed: {}", e)))?
            .await
            .map_err(|e| TransportError::Protocol(format!("Connection failed: {}", e)))?;

        let transport = Http3Transport::new(connection, self.config.clone());
        transport.start_background_processing().await;

        tracing::info!(server_addr = server_addr, "HTTP/3 connection established");
        Ok(transport)
    }
}

/// HTTP/3 transport statistics
#[derive(Debug, Clone)]
pub struct Http3Stats {
    pub active_streams: usize,
    pub max_concurrent_streams: u32,
    pub bytes_sent: u64,
    pub bytes_received: u64,
    pub packets_sent: u64,
    pub packets_received: u64,
    pub connection_errors: u64,
    pub stream_errors: u64,
}

/// Create a client endpoint with HTTP/3 configuration
pub fn make_http3_client_endpoint(config: Http3Config) -> Result<Endpoint, TransportError> {
    let mut client_cfg = configure_http3_client(config);
    let endpoint = Endpoint::client("0.0.0.0:0".parse().unwrap())
        .map_err(|e| TransportError::Protocol(format!("Failed to create endpoint: {}", e)))?;

    Ok(endpoint)
}

fn configure_http3_client(config: Http3Config) -> quinn::ClientConfig {
    let mut transport_config = quinn::TransportConfig::default();

    // Configure HTTP/3 specific settings
    transport_config.max_concurrent_bidi_streams(config.max_concurrent_streams.into());
    transport_config.max_idle_timeout(Some(
        std::time::Duration::from_secs(config.connection_idle_timeout)
            .try_into()
            .unwrap(),
    ));

    let crypto = rustls::ClientConfig::builder()
        .dangerous()
        .with_custom_certificate_verifier(SkipServerVerification::new())
        .with_no_client_auth();

    let mut client_config = quinn::ClientConfig::new(Arc::new(
        quinn::crypto::rustls::QuicClientConfig::try_from(crypto).unwrap(),
    ));
    client_config.transport_config(Arc::new(transport_config));

    client_config
}

/// Skip certificate verification for testing
/// WARNING: Only use this for testing!
#[derive(Debug)]
struct SkipServerVerification;

impl SkipServerVerification {
    fn new() -> Arc<Self> {
        Arc::new(Self)
    }
}

impl rustls::client::danger::ServerCertVerifier for SkipServerVerification {
    fn verify_server_cert(
        &self,
        _end_entity: &rustls::pki_types::CertificateDer<'_>,
        _intermediates: &[rustls::pki_types::CertificateDer<'_>],
        _server_name: &rustls::pki_types::ServerName<'_>,
        _ocsp_response: &[u8],
        _now: rustls::pki_types::UnixTime,
    ) -> Result<rustls::client::danger::ServerCertVerified, rustls::Error> {
        Ok(rustls::client::danger::ServerCertVerified::assertion())
    }

    fn verify_tls12_signature(
        &self,
        _message: &[u8],
        _cert: &rustls::pki_types::CertificateDer<'_>,
        _dss: &rustls::DigitallySignedStruct,
    ) -> Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
        Ok(rustls::client::danger::HandshakeSignatureValid::assertion())
    }

    fn verify_tls13_signature(
        &self,
        _message: &[u8],
        _cert: &rustls::pki_types::CertificateDer<'_>,
        _dss: &rustls::DigitallySignedStruct,
    ) -> Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
        Ok(rustls::client::danger::HandshakeSignatureValid::assertion())
    }

    fn supported_verify_schemes(&self) -> Vec<rustls::SignatureScheme> {
        vec![
            rustls::SignatureScheme::RSA_PKCS1_SHA256,
            rustls::SignatureScheme::ECDSA_NISTP256_SHA256,
            rustls::SignatureScheme::RSA_PKCS1_SHA384,
            rustls::SignatureScheme::ECDSA_NISTP384_SHA384,
            rustls::SignatureScheme::RSA_PKCS1_SHA512,
            rustls::SignatureScheme::RSA_PSS_SHA256,
            rustls::SignatureScheme::RSA_PSS_SHA384,
            rustls::SignatureScheme::RSA_PSS_SHA512,
            rustls::SignatureScheme::ED25519,
        ]
    }
}

/// Advanced HTTP/3 features
pub mod advanced {
    use super::*;

    /// HTTP/3 connection pool for managing multiple connections
    #[derive(Debug)]
    pub struct Http3ConnectionPool {
        connections: Arc<RwLock<HashMap<String, Arc<Http3Transport>>>>,
        config: Http3Config,
        max_connections_per_host: usize,
    }

    impl Http3ConnectionPool {
        /// Create a new connection pool
        pub fn new(config: Http3Config) -> Self {
            Self {
                connections: Arc::new(RwLock::new(HashMap::new())),
                config,
                max_connections_per_host: 10,
            }
        }

        /// Get or create a connection to the specified host
        pub async fn get_connection(
            &self,
            host: &str,
        ) -> Result<Arc<Http3Transport>, TransportError> {
            let connections = self.connections.read().await;
            if let Some(transport) = connections.get(host) {
                return Ok(transport.clone());
            }
            drop(connections);

            // Create new connection
            let endpoint = make_http3_client_endpoint(self.config.clone())?;
            let client = Http3Client::new(endpoint, self.config.clone());
            let transport = client.connect(host).await?;
            let transport_arc = Arc::new(transport);

            // Store in pool
            let mut connections = self.connections.write().await;
            connections.insert(host.to_string(), transport_arc.clone());

            tracing::info!(host = host, "New HTTP/3 connection added to pool");
            Ok(transport_arc)
        }

        /// Get pool statistics
        pub async fn get_pool_stats(&self) -> HashMap<String, Http3Stats> {
            let connections = self.connections.read().await;
            let mut stats = HashMap::new();

            for (host, transport) in connections.iter() {
                stats.insert(host.clone(), transport.get_stats().await);
            }

            stats
        }
    }

    /// HTTP/3 load balancer for distributing requests
    #[derive(Debug)]
    pub struct Http3LoadBalancer {
        servers: Vec<String>,
        pool: Http3ConnectionPool,
        current_index: Arc<Mutex<usize>>,
    }

    impl Http3LoadBalancer {
        /// Create a new load balancer
        pub fn new(servers: Vec<String>, config: Http3Config) -> Self {
            Self {
                servers,
                pool: Http3ConnectionPool::new(config),
                current_index: Arc::new(Mutex::new(0)),
            }
        }

        /// Get the next server using round-robin
        pub async fn get_next_connection(&self) -> Result<Arc<Http3Transport>, TransportError> {
            if self.servers.is_empty() {
                return Err(TransportError::Protocol(
                    "No servers configured".to_string(),
                ));
            }

            let mut index = self.current_index.lock().await;
            let server = &self.servers[*index];
            *index = (*index + 1) % self.servers.len();
            drop(index);

            self.pool.get_connection(server).await
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_http3_config_default() {
        let config = Http3Config::default();
        assert_eq!(config.max_concurrent_streams, 1000);
        assert!(config.enable_multiplexing);
        assert!(!config.enable_compression);
    }

    #[test]
    fn test_http3_headers_default() {
        let headers = Http3Headers::default();
        assert_eq!(headers.method, "POST");
        assert_eq!(headers.path, "/rpc");
        assert_eq!(headers.content_type, "application/json");
    }

    #[tokio::test]
    async fn test_http3_transport_creation() {
        // This test would require a mock QUIC connection
        // For now, we just test that the structures compile
        let config = Http3Config::default();
        assert!(config.max_concurrent_streams > 0);
    }

    #[tokio::test]
    async fn test_http3_connection_pool() {
        let config = Http3Config::default();
        let pool = advanced::Http3ConnectionPool::new(config);

        // Test that the pool can be created
        assert_eq!(pool.max_connections_per_host, 10);
    }
}