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
//! HTTP/3 connection driver - background task that reads packets and routes them to streams.
//!
//! The driver owns the QUIC connection and UdpSocket.
use bytes::{Bytes, BytesMut};
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;
use tokio::net::UdpSocket;
use tokio::sync::mpsc;
use tokio::sync::oneshot;
use tokio::time::sleep;
use tracing;
use crate::error::{Error, Result};
use quiche::h3::NameValue;
/// Command sent from handle to driver
#[derive(Debug)]
pub enum DriverCommand {
/// Send a request and get response via oneshot
SendRequest {
method: http::Method,
uri: http::Uri,
headers: Vec<(String, String)>,
body: Option<Bytes>,
response_tx: oneshot::Sender<Result<StreamResponse>>,
},
}
#[derive(Debug)]
pub struct StreamResponse {
pub status: u16,
pub headers: Vec<(String, String)>,
pub body: Bytes,
}
/// Per-stream state tracked by driver
struct DriverStreamState {
/// Oneshot sender for response completion
response_tx: Option<oneshot::Sender<Result<StreamResponse>>>,
/// Accumulated response status
status: Option<u16>,
/// Accumulated response headers
headers: Vec<(String, String)>,
/// Accumulated response body
body: BytesMut,
}
impl DriverStreamState {
fn new(response_tx: oneshot::Sender<Result<StreamResponse>>) -> Self {
Self {
response_tx: Some(response_tx),
status: None,
headers: Vec::new(),
body: BytesMut::new(),
}
}
}
/// HTTP/3 connection driver
pub struct H3Driver {
command_rx: mpsc::Receiver<DriverCommand>,
conn: quiche::Connection,
h3_conn: quiche::h3::Connection,
socket: Arc<UdpSocket>,
peer_addr: SocketAddr,
streams: HashMap<u64, DriverStreamState>,
}
impl H3Driver {
pub fn new(
command_rx: mpsc::Receiver<DriverCommand>,
conn: quiche::Connection,
h3_conn: quiche::h3::Connection,
socket: Arc<UdpSocket>,
peer_addr: SocketAddr,
) -> Self {
Self {
command_rx,
conn,
h3_conn,
socket,
peer_addr,
streams: HashMap::new(),
}
}
pub async fn drive(mut self) -> Result<()> {
let result = self.drive_loop().await;
// Propagate error to all pending streams
if let Err(ref e) = result {
tracing::error!("H3 Driver error: {}", e);
for (_, mut stream) in self.streams.drain() {
if let Some(tx) = stream.response_tx.take() {
let _ = tx.send(Err(Error::Quic(format!("Driver error: {}", e))));
}
}
}
result
}
async fn drive_loop(&mut self) -> Result<()> {
let mut buf = vec![0u8; 65535];
let mut out = vec![0u8; 1350];
loop {
// 1. Process sending any pending packets first (egress)
loop {
match self.conn.send(&mut out) {
Ok((len, _)) => {
if let Err(e) = self.socket.send_to(&out[..len], self.peer_addr).await {
tracing::error!("H3 socket send error: {}", e);
return Err(Error::Io(e));
}
}
Err(quiche::Error::Done) => break,
Err(e) => {
tracing::error!("H3 quiche send error: {}", e);
return Err(Error::Quic(format!("QUIC send error: {}", e)));
}
}
}
// 2. Select: Recv Packet OR Command OR Timeout
let timeout_duration = self.conn.timeout().unwrap_or(Duration::from_secs(60));
tokio::select! {
// Incoming Command
cmd = self.command_rx.recv() => {
match cmd {
Some(c) => self.handle_command(c).await?,
None => {
match self.conn.close(true, 0x00, b"Client shutdown") {
Ok(_) => {},
Err(quiche::Error::Done) => {},
Err(_) => {}
}
while let Ok((len, _)) = self.conn.send(&mut out) {
let _ = self.socket.send_to(&out[..len], self.peer_addr).await;
}
return Ok(());
}
}
}
// Incoming Packet
res = self.socket.recv_from(&mut buf) => {
match res {
Ok((len, from)) => {
if from == self.peer_addr {
let info = quiche::RecvInfo {
from,
to: self.socket.local_addr().unwrap(),
// to: self.socket.local_addr().unwrap(), // Need to handle unchecked?
// The original code unwrapped, presumably safe if bound.
};
match self.conn.recv(&mut buf[..len], info) {
Ok(_) => {
self.process_h3_events()?;
}
Err(quiche::Error::Done) => {},
Err(e) => {
tracing::warn!("QUIC recv error: {}", e);
}
}
}
}
Err(e) => return Err(Error::Io(e)),
}
}
// Timer
_ = sleep(timeout_duration) => {
self.conn.on_timeout();
}
}
// Check for connection closure
if self.conn.is_closed() {
tracing::info!("H3 Driver: Connection closed");
for (_id, mut stream) in self.streams.drain() {
if let Some(tx) = stream.response_tx.take() {
let _ = tx.send(Err(Error::Connection("Connection closed".into())));
}
}
return Ok(());
}
}
}
async fn handle_command(&mut self, cmd: DriverCommand) -> Result<()> {
match cmd {
DriverCommand::SendRequest {
method,
uri,
headers,
body,
response_tx,
} => {
// Construct H3 headers
let path = uri.path();
let path = if path.is_empty() { "/" } else { path };
let host = uri.host().unwrap_or("").to_string();
let mut h3_headers = vec![
quiche::h3::Header::new(b":method", method.as_str().as_bytes()),
quiche::h3::Header::new(b":scheme", b"https"),
quiche::h3::Header::new(b":authority", host.as_bytes()),
quiche::h3::Header::new(b":path", path.as_bytes()),
];
for (k, v) in &headers {
let k_lower = k.to_lowercase();
// Filter pseudo and prohibited headers
if !k.starts_with(':')
&& k_lower != "connection"
&& k_lower != "keep-alive"
&& k_lower != "proxy-connection"
&& k_lower != "transfer-encoding"
&& k_lower != "upgrade"
{
h3_headers.push(quiche::h3::Header::new(k.as_bytes(), v.as_bytes()));
}
}
// Send request logic
let fin = body.is_none();
match self.h3_conn.send_request(&mut self.conn, &h3_headers, fin) {
Ok(stream_id) => {
// Store stream state
let mut state = DriverStreamState::new(response_tx);
// Send body if present
if let Some(data) = body {
if let Err(e) =
self.h3_conn
.send_body(&mut self.conn, stream_id, &data, true)
{
// Error sending body
if let Some(tx) = state.response_tx.take() {
let _ = tx
.send(Err(Error::Quic(format!("Send body failed: {}", e))));
}
return Ok(());
}
}
self.streams.insert(stream_id, state);
}
Err(e) => {
let _ = response_tx
.send(Err(Error::Quic(format!("Send request failed: {}", e))));
}
}
}
}
Ok(())
}
fn process_h3_events(&mut self) -> Result<()> {
loop {
match self.h3_conn.poll(&mut self.conn) {
Ok((stream_id, quiche::h3::Event::Headers { list, .. })) => {
if let Some(stream) = self.streams.get_mut(&stream_id) {
for header in list {
let name = String::from_utf8_lossy(header.name());
let value = String::from_utf8_lossy(header.value());
if name == ":status" {
stream.status = value.parse().ok();
} else {
stream.headers.push((name.into_owned(), value.into_owned()));
}
}
}
}
Ok((stream_id, quiche::h3::Event::Data)) => {
if let Some(stream) = self.streams.get_mut(&stream_id) {
let mut buf = vec![0u8; 65535];
while let Ok(len) =
self.h3_conn.recv_body(&mut self.conn, stream_id, &mut buf)
{
stream.body.extend_from_slice(&buf[..len]);
}
}
}
Ok((stream_id, quiche::h3::Event::Finished)) => {
if let Some(mut stream) = self.streams.remove(&stream_id) {
if let Some(tx) = stream.response_tx.take() {
let resp = StreamResponse {
status: stream.status.unwrap_or(0),
headers: stream.headers,
body: stream.body.freeze(),
};
let _ = tx.send(Ok(resp));
}
}
}
Ok((stream_id, quiche::h3::Event::Reset(error_code))) => {
if let Some(mut stream) = self.streams.remove(&stream_id) {
if let Some(tx) = stream.response_tx.take() {
let _ =
tx.send(Err(Error::Quic(format!("Stream reset: {}", error_code))));
}
}
}
Err(quiche::h3::Error::Done) => break,
Ok(_) => {} // Ignore other events
Err(e) => {
tracing::warn!("H3 poll error: {}", e);
return Err(Error::Quic(format!("H3 poll error: {}", e)));
}
}
}
Ok(())
}
}