tokio_quiche/http3/driver/
server.rs1use std::ops::Deref;
28use std::sync::Arc;
29
30use tokio::sync::mpsc;
31
32use super::datagram;
33use super::datagram::PendingDgramBuffer;
34use super::DgramFlowId;
35use super::DriverHooks;
36use super::H3Command;
37use super::H3ConnectionError;
38use super::H3ConnectionResult;
39use super::H3Controller;
40use super::H3Driver;
41use super::H3Event;
42use super::InboundFrame;
43use super::InboundHeaders;
44use super::IncomingH3Headers;
45use super::StreamCtx;
46use super::STREAM_CAPACITY;
47use crate::http3::settings::Http3Settings;
48use crate::http3::settings::Http3SettingsEnforcer;
49use crate::http3::settings::Http3TimeoutType;
50use crate::http3::settings::TimeoutKey;
51use crate::quic::HandshakeInfo;
52use crate::quic::QuicCommand;
53use crate::quic::QuicheConnection;
54
55pub type ServerH3Driver = H3Driver<ServerHooks>;
59pub type ServerH3Controller = H3Controller<ServerHooks>;
62pub type ServerEventStream = mpsc::UnboundedReceiver<ServerH3Event>;
66
67#[derive(Clone, Debug)]
68pub struct RawPriorityValue(Vec<u8>);
69
70impl From<Vec<u8>> for RawPriorityValue {
71 fn from(value: Vec<u8>) -> Self {
72 RawPriorityValue(value)
73 }
74}
75
76impl Deref for RawPriorityValue {
77 type Target = [u8];
78
79 fn deref(&self) -> &Self::Target {
80 &self.0
81 }
82}
83
84#[derive(Clone, Debug)]
86pub struct IsInEarlyData(bool);
87
88impl IsInEarlyData {
89 fn new(is_in_early_data: bool) -> Self {
90 IsInEarlyData(is_in_early_data)
91 }
92}
93
94impl Deref for IsInEarlyData {
95 type Target = bool;
96
97 fn deref(&self) -> &Self::Target {
98 &self.0
99 }
100}
101
102#[derive(Debug)]
104pub enum ServerH3Event {
105 Core(H3Event),
106
107 Headers {
108 incoming_headers: IncomingH3Headers,
109 priority: Option<RawPriorityValue>,
111 is_in_early_data: IsInEarlyData,
112 },
113}
114
115impl From<H3Event> for ServerH3Event {
116 fn from(ev: H3Event) -> Self {
117 match ev {
118 H3Event::IncomingHeaders(incoming_headers) => {
119 Self::Headers {
125 incoming_headers,
126 priority: None,
127 is_in_early_data: IsInEarlyData::new(false),
128 }
129 },
130 _ => Self::Core(ev),
131 }
132 }
133}
134
135#[derive(Debug)]
137pub enum ServerH3Command {
138 Core(H3Command),
139}
140
141impl From<H3Command> for ServerH3Command {
142 fn from(cmd: H3Command) -> Self {
143 Self::Core(cmd)
144 }
145}
146
147impl From<QuicCommand> for ServerH3Command {
148 fn from(cmd: QuicCommand) -> Self {
149 Self::Core(H3Command::QuicCmd(cmd))
150 }
151}
152
153const PRE_HEADERS_BOOSTED_PRIORITY_URGENCY: u8 = 64;
157const PRE_HEADERS_BOOSTED_PRIORITY_INCREMENTAL: bool = false;
160
161pub struct ServerHooks {
162 settings_enforcer: Http3SettingsEnforcer,
164 requests: u64,
166
167 post_accept_timeout: Option<TimeoutKey>,
170 extended_connect_enabled: bool,
173 pending_dgrams: PendingDgramBuffer,
176}
177
178impl ServerHooks {
179 fn handle_request(
184 driver: &mut H3Driver<Self>, qconn: &mut QuicheConnection,
185 headers: InboundHeaders,
186 ) -> H3ConnectionResult<()> {
187 let InboundHeaders {
188 stream_id,
189 headers,
190 has_body,
191 } = headers;
192
193 if driver.stream_map.contains_key(&stream_id) {
197 return Ok(());
198 }
199
200 let (mut stream_ctx, send, recv) = StreamCtx::new(
201 stream_id,
202 STREAM_CAPACITY,
203 driver.ready_stream_sender.clone(),
204 );
205
206 let dgram_flow = if driver.hooks.extended_connect_enabled {
214 if let Some(flow_id) =
215 datagram::extract_quarter_stream_id(stream_id, &headers)
216 {
217 stream_ctx.associated_dgram_flow_id = Some(flow_id);
218 Some(driver.establish_flow(qconn, stream_id, flow_id)?)
219 } else {
220 None
221 }
222 } else {
223 None
224 };
225 if let Some(flow_id) = stream_ctx.associated_dgram_flow_id {
228 if let Some(flow) = driver.flow_map.get_mut(&flow_id) {
229 driver.hooks.pending_dgrams.flush_to_flow(flow_id, flow);
230 }
231 }
232
233 let latest_priority_update: Option<RawPriorityValue> = driver
234 .conn_mut()?
235 .take_last_priority_update(stream_id)
236 .ok()
237 .map(|v| v.into());
238
239 qconn
243 .stream_priority(
244 stream_id,
245 PRE_HEADERS_BOOSTED_PRIORITY_URGENCY,
246 PRE_HEADERS_BOOSTED_PRIORITY_INCREMENTAL,
247 )
248 .ok();
249
250 let headers = IncomingH3Headers {
251 stream_id,
252 headers,
253 send,
254 recv,
255 read_fin: !has_body,
256 h3_audit_stats: Arc::clone(&stream_ctx.audit_stats),
257 dgram_flow,
258 };
259
260 stream_ctx.wait_for_recv();
261 driver.insert_stream(stream_id, stream_ctx);
262
263 driver.process_writable_stream(qconn, stream_id)?;
269
270 driver
271 .h3_event_sender
272 .send(ServerH3Event::Headers {
273 incoming_headers: headers,
274 priority: latest_priority_update,
275 is_in_early_data: IsInEarlyData::new(qconn.is_in_early_data()),
276 })
277 .map_err(|_| H3ConnectionError::ControllerWentAway)?;
278 driver.hooks.requests += 1;
279
280 Ok(())
281 }
282
283 #[cfg(test)]
284 pub(crate) fn pending_dgrams_mut(&mut self) -> &mut PendingDgramBuffer {
286 &mut self.pending_dgrams
287 }
288}
289
290#[allow(private_interfaces)]
291impl DriverHooks for ServerHooks {
292 type Command = ServerH3Command;
293 type Event = ServerH3Event;
294
295 fn new(settings: &Http3Settings) -> Self {
296 Self {
297 settings_enforcer: settings.into(),
298 requests: 0,
299 post_accept_timeout: None,
300 extended_connect_enabled: settings.enable_extended_connect,
301 pending_dgrams: PendingDgramBuffer::new(),
302 }
303 }
304
305 fn conn_established(
306 driver: &mut H3Driver<Self>, qconn: &mut QuicheConnection,
307 handshake_info: &HandshakeInfo,
308 ) -> H3ConnectionResult<()> {
309 assert!(
310 qconn.is_server(),
311 "ServerH3Driver requires a server-side QUIC connection"
312 );
313
314 if let Some(post_accept_timeout) =
315 driver.hooks.settings_enforcer.post_accept_timeout()
316 {
317 let remaining = post_accept_timeout
318 .checked_sub(handshake_info.elapsed())
319 .ok_or(H3ConnectionError::PostAcceptTimeout)?;
320
321 let key = driver
322 .hooks
323 .settings_enforcer
324 .add_timeout(Http3TimeoutType::PostAccept, remaining);
325 driver.hooks.post_accept_timeout = Some(key);
326 }
327
328 Ok(())
329 }
330
331 fn headers_received(
332 driver: &mut H3Driver<Self>, qconn: &mut QuicheConnection,
333 headers: InboundHeaders,
334 ) -> H3ConnectionResult<()> {
335 if driver
336 .hooks
337 .settings_enforcer
338 .enforce_requests_limit(driver.hooks.requests)
339 {
340 let _ =
341 qconn.close(true, quiche::h3::WireErrorCode::NoError as u64, &[]);
342 return Ok(());
343 }
344
345 if let Some(timeout) = driver.hooks.post_accept_timeout.take() {
346 driver.hooks.settings_enforcer.cancel_timeout(timeout);
349 }
350
351 Self::handle_request(driver, qconn, headers)
352 }
353
354 fn stream_recv_closed(_driver: &mut H3Driver<Self>, _stream_id: u64) {}
355
356 fn stream_closed(driver: &mut H3Driver<Self>, stream_id: u64) {
357 let _ = driver
358 .h3_event_sender
359 .send(H3Event::StreamClosed { stream_id }.into());
360 }
361
362 fn handle_incoming_dgram(
369 driver: &mut H3Driver<Self>, flow_id: DgramFlowId, dgram: InboundFrame,
370 ) {
371 if !driver.hooks.extended_connect_enabled {
374 return;
375 }
376
377 if let Some(flow) = driver.get_flow(flow_id) {
379 flow.send_best_effort(dgram);
380 return;
381 }
382
383 driver.hooks.pending_dgrams.push(flow_id, dgram);
385 }
386
387 fn expire_pending_dgrams(&mut self) {
388 self.pending_dgrams.expire();
389 }
390
391 fn conn_command(
392 driver: &mut H3Driver<Self>, qconn: &mut QuicheConnection,
393 cmd: Self::Command,
394 ) -> H3ConnectionResult<()> {
395 let ServerH3Command::Core(cmd) = cmd;
396 driver.handle_core_command(qconn, cmd)
397 }
398
399 fn has_wait_action(driver: &mut H3Driver<Self>) -> bool {
400 driver.hooks.settings_enforcer.has_pending_timeouts()
401 }
402
403 async fn wait_for_action(
404 &mut self, qconn: &mut QuicheConnection,
405 ) -> H3ConnectionResult<()> {
406 self.settings_enforcer.enforce_timeouts(qconn).await?;
407 Err(H3ConnectionError::PostAcceptTimeout)
408 }
409}