|
52 | 52 | F2: FnOnce(&mut Builder) -> Bu + Send + 'static,
|
53 | 53 | {
|
54 | 54 | // Spawn new runtime in thread to prevent reactor execution context conflict
|
| 55 | + let test_name = thread::current().name().unwrap_or("<unknown>").to_string(); |
55 | 56 | thread::spawn(move || {
|
56 | 57 | let rt = runtime::Builder::new_current_thread()
|
57 | 58 | .enable_all()
|
|
68 | 69 | let (panic_tx, panic_rx) = std_mpsc::channel();
|
69 | 70 | let tname = format!(
|
70 | 71 | "test({})-support-server",
|
71 |
| - thread::current().name().unwrap_or("<unknown>") |
| 72 | + test_name, |
72 | 73 | );
|
73 | 74 | thread::Builder::new()
|
74 | 75 | .name(tname)
|
@@ -110,3 +111,109 @@ where
|
110 | 111 | .join()
|
111 | 112 | .unwrap()
|
112 | 113 | }
|
| 114 | + |
| 115 | +#[cfg(feature = "http3")] |
| 116 | +pub fn http3<F1, Fut>(func: F1) -> Server |
| 117 | +where |
| 118 | + F1: Fn(http::Request<http_body_util::combinators::BoxBody<bytes::Bytes, h3::Error>>) -> Fut |
| 119 | + + Clone |
| 120 | + + Send |
| 121 | + + 'static, |
| 122 | + Fut: Future<Output = http::Response<reqwest::Body>> + Send + 'static, |
| 123 | +{ |
| 124 | + use bytes::Buf; |
| 125 | + use http_body_util::BodyExt; |
| 126 | + use quinn::crypto::rustls::QuicServerConfig; |
| 127 | + use std::sync::Arc; |
| 128 | + |
| 129 | + // Spawn new runtime in thread to prevent reactor execution context conflict |
| 130 | + let test_name = thread::current().name().unwrap_or("<unknown>").to_string(); |
| 131 | + thread::spawn(move || { |
| 132 | + let rt = runtime::Builder::new_current_thread() |
| 133 | + .enable_all() |
| 134 | + .build() |
| 135 | + .expect("new rt"); |
| 136 | + |
| 137 | + let cert = std::fs::read("tests/support/server.cert").unwrap().into(); |
| 138 | + let key = std::fs::read("tests/support/server.key").unwrap().try_into().unwrap(); |
| 139 | + |
| 140 | + let mut tls_config = rustls::ServerConfig::builder() |
| 141 | + .with_no_client_auth() |
| 142 | + .with_single_cert(vec![cert], key) |
| 143 | + .unwrap(); |
| 144 | + tls_config.max_early_data_size = u32::MAX; |
| 145 | + tls_config.alpn_protocols = vec![b"h3".into()]; |
| 146 | + |
| 147 | + let server_config = quinn::ServerConfig::with_crypto(Arc::new(QuicServerConfig::try_from(tls_config).unwrap())); |
| 148 | + let endpoint = rt.block_on(async move { |
| 149 | + quinn::Endpoint::server(server_config, "[::1]:0".parse().unwrap()).unwrap() |
| 150 | + }); |
| 151 | + let addr = endpoint.local_addr().unwrap(); |
| 152 | + |
| 153 | + let (shutdown_tx, mut shutdown_rx) = oneshot::channel(); |
| 154 | + let (panic_tx, panic_rx) = std_mpsc::channel(); |
| 155 | + let tname = format!( |
| 156 | + "test({})-support-server", |
| 157 | + test_name, |
| 158 | + ); |
| 159 | + thread::Builder::new() |
| 160 | + .name(tname) |
| 161 | + .spawn(move || { |
| 162 | + rt.block_on(async move { |
| 163 | + |
| 164 | + loop { |
| 165 | + tokio::select! { |
| 166 | + _ = &mut shutdown_rx => { |
| 167 | + break; |
| 168 | + } |
| 169 | + Some(accepted) = endpoint.accept() => { |
| 170 | + let conn = accepted.await.expect("accepted"); |
| 171 | + let mut h3_conn = h3::server::Connection::new(h3_quinn::Connection::new(conn)).await.unwrap(); |
| 172 | + let func = func.clone(); |
| 173 | + tokio::spawn(async move { |
| 174 | + while let Ok(Some((req, stream))) = h3_conn.accept().await { |
| 175 | + let func = func.clone(); |
| 176 | + tokio::spawn(async move { |
| 177 | + let (mut tx, rx) = stream.split(); |
| 178 | + let body = futures_util::stream::unfold(rx, |mut rx| async move { |
| 179 | + match rx.recv_data().await { |
| 180 | + Ok(Some(mut buf)) => { |
| 181 | + Some((Ok(hyper::body::Frame::data(buf.copy_to_bytes(buf.remaining()))), rx)) |
| 182 | + }, |
| 183 | + Ok(None) => None, |
| 184 | + Err(err) => { |
| 185 | + Some((Err(err), rx)) |
| 186 | + } |
| 187 | + } |
| 188 | + }); |
| 189 | + let body = BodyExt::boxed(http_body_util::StreamBody::new(body)); |
| 190 | + let resp = func(req.map(move |()| body)).await; |
| 191 | + let (parts, mut body) = resp.into_parts(); |
| 192 | + let resp = http::Response::from_parts(parts, ()); |
| 193 | + tx.send_response(resp).await.unwrap(); |
| 194 | + |
| 195 | + while let Some(Ok(frame)) = body.frame().await { |
| 196 | + if let Ok(data) = frame.into_data() { |
| 197 | + tx.send_data(data).await.unwrap(); |
| 198 | + } |
| 199 | + } |
| 200 | + tx.finish().await.unwrap(); |
| 201 | + }); |
| 202 | + } |
| 203 | + }); |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + let _ = panic_tx.send(()); |
| 208 | + }); |
| 209 | + }) |
| 210 | + .expect("thread spawn"); |
| 211 | + Server { |
| 212 | + addr, |
| 213 | + panic_rx, |
| 214 | + shutdown_tx: Some(shutdown_tx), |
| 215 | + } |
| 216 | + }) |
| 217 | + .join() |
| 218 | + .unwrap() |
| 219 | +} |
0 commit comments