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
//! HTTP3 backend.
use std::fmt::Debug;
use std::io;
use std::net::SocketAddr;
use std::sync::Arc;
use body::QuicIncomingBody;
use scuffle_context::ContextFutExt;
#[cfg(feature = "tracing")]
use tracing::Instrument;
use utils::copy_response_body;
use crate::error::HttpError;
use crate::service::{HttpService, HttpServiceFactory};
pub mod body;
mod utils;
/// A backend that handles incoming HTTP3 connections.
///
/// This is used internally by the [`HttpServer`](crate::server::HttpServer) but can be used directly if preferred.
///
/// Call [`run`](Http3Backend::run) to start the server.
#[derive(bon::Builder, Debug, Clone)]
pub struct Http3Backend<F> {
/// The [`scuffle_context::Context`] this server will live by.
#[builder(default = scuffle_context::Context::global())]
ctx: scuffle_context::Context,
/// The number of worker tasks to spawn for each server backend.
#[builder(default = 1)]
worker_tasks: usize,
/// The service factory that will be used to create new services.
service_factory: F,
/// The address to bind to.
///
/// Use `[::]` for a dual-stack listener.
/// For example, use `[::]:80` to bind to port 80 on both IPv4 and IPv6.
bind: SocketAddr,
/// rustls config.
///
/// Use this field to set the server into TLS mode.
/// It will only accept TLS connections when this is set.
rustls_config: rustls::ServerConfig,
}
impl<F> Http3Backend<F>
where
F: HttpServiceFactory + Clone + Send + 'static,
F::Error: std::error::Error + Send,
F::Service: Clone + Send + 'static,
<F::Service as HttpService>::Error: std::error::Error + Send + Sync,
<F::Service as HttpService>::ResBody: Send,
<<F::Service as HttpService>::ResBody as http_body::Body>::Data: Send,
<<F::Service as HttpService>::ResBody as http_body::Body>::Error: std::error::Error + Send + Sync,
{
/// Run the HTTP3 server
///
/// This function will bind to the address specified in `bind`, listen for incoming connections and handle requests.
#[cfg_attr(feature = "tracing", tracing::instrument(skip_all, fields(bind = %self.bind)))]
pub async fn run(mut self) -> Result<(), HttpError<F>> {
#[cfg(feature = "tracing")]
tracing::debug!("starting server");
// not quite sure why this is necessary but it is
self.rustls_config.max_early_data_size = u32::MAX;
let crypto = h3_quinn::quinn::crypto::rustls::QuicServerConfig::try_from(self.rustls_config)?;
let server_config = h3_quinn::quinn::ServerConfig::with_crypto(Arc::new(crypto));
// Bind the UDP socket
let socket = std::net::UdpSocket::bind(self.bind)?;
// Runtime for the quinn endpoint
let runtime = h3_quinn::quinn::default_runtime().ok_or_else(|| io::Error::other("no async runtime found"))?;
// Create a child context for the workers so we can shut them down if one of them fails without shutting down the main context
let (worker_ctx, worker_handler) = self.ctx.new_child();
let workers = (0..self.worker_tasks).map(|_n| {
let ctx = worker_ctx.clone();
let service_factory = self.service_factory.clone();
let server_config = server_config.clone();
let socket = socket.try_clone().expect("failed to clone socket");
let runtime = Arc::clone(&runtime);
let worker_fut = async move {
let endpoint = h3_quinn::quinn::Endpoint::new(
h3_quinn::quinn::EndpointConfig::default(),
Some(server_config),
socket,
runtime,
)?;
#[cfg(feature = "tracing")]
tracing::trace!("waiting for connections");
while let Some(Some(new_conn)) = endpoint.accept().with_context(&ctx).await {
let mut service_factory = service_factory.clone();
let ctx = ctx.clone();
tokio::spawn(async move {
let _res: Result<_, HttpError<F>> = async move {
let Some(conn) = new_conn.with_context(&ctx).await.transpose()? else {
#[cfg(feature = "tracing")]
tracing::trace!("context done while accepting connection");
return Ok(());
};
let addr = conn.remote_address();
#[cfg(feature = "tracing")]
tracing::debug!(addr = %addr, "accepted quic connection");
let connection_fut = async move {
let Some(mut h3_conn) = h3::server::Connection::new(h3_quinn::Connection::new(conn))
.with_context(&ctx)
.await
.transpose()?
else {
#[cfg(feature = "tracing")]
tracing::trace!("context done while establishing connection");
return Ok(());
};
// make a new service for this connection
let http_service = service_factory
.new_service(addr)
.await
.map_err(|e| HttpError::ServiceFactoryError(e))?;
loop {
match h3_conn.accept().with_context(&ctx).await {
Some(Ok(Some(resolver))) => {
let (req, stream) = match resolver.resolve_request().await {
Ok(r) => r,
Err(_err) => {
#[cfg(feature = "tracing")]
tracing::warn!("error on accept: {}", _err);
continue;
}
};
#[cfg(feature = "tracing")]
tracing::debug!(method = %req.method(), uri = %req.uri(), "received request");
let (mut send, recv) = stream.split();
let size_hint = req
.headers()
.get(http::header::CONTENT_LENGTH)
.and_then(|len| len.to_str().ok().and_then(|x| x.parse().ok()));
let body = QuicIncomingBody::new(recv, size_hint);
let req = req.map(|_| crate::body::IncomingBody::from(body));
let ctx = ctx.clone();
let mut http_service = http_service.clone();
tokio::spawn(async move {
let _res: Result<_, HttpError<F>> = async move {
let resp = http_service
.call(req)
.await
.map_err(|e| HttpError::ServiceError(e))?;
let (parts, body) = resp.into_parts();
send.send_response(http::Response::from_parts(parts, ())).await?;
copy_response_body(send, body).await?;
Ok(())
}
.await;
#[cfg(feature = "tracing")]
if let Err(e) = _res {
tracing::warn!(err = %e, "error handling request");
}
// This moves the context into the async block because it is dropped here
drop(ctx);
});
}
// indicating no more streams to be received
Some(Ok(None)) => {
break;
}
Some(Err(err)) => return Err(err.into()),
// context is done
None => {
#[cfg(feature = "tracing")]
tracing::trace!("context done, stopping connection loop");
break;
}
}
}
#[cfg(feature = "tracing")]
tracing::trace!("connection closed");
Ok(())
};
#[cfg(feature = "tracing")]
let connection_fut = connection_fut.instrument(tracing::trace_span!("connection", addr = %addr));
connection_fut.await
}
.await;
#[cfg(feature = "tracing")]
if let Err(err) = _res {
tracing::warn!(err = %err, "error handling connection");
}
});
}
// shut down gracefully
// wait for connections to be closed before exiting
endpoint.wait_idle().await;
Ok::<_, crate::error::HttpError<F>>(())
};
#[cfg(feature = "tracing")]
let worker_fut = worker_fut.instrument(tracing::trace_span!("worker", n = _n));
tokio::spawn(worker_fut)
});
if let Err(_e) = futures::future::try_join_all(workers).await {
#[cfg(feature = "tracing")]
tracing::error!(err = %_e, "error running workers");
}
drop(worker_ctx);
worker_handler.shutdown().await;
#[cfg(feature = "tracing")]
tracing::debug!("all workers finished");
Ok(())
}
}