[edge] Tests first: QC repros for 1xx, auth timeout, name resolution, Upgrade tokens, Remote-* stripping (red)

This commit is contained in:
2026-11-07 15:36:00 +01:00
parent fd4b4a7775
commit d42c178a13
6 changed files with 414 additions and 134 deletions
+101
View File
@@ -0,0 +1,101 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! A raw TCP HTTP/1.1 server that answers with scripted bytes: 1xx heads, truncated bodies,
//! hangs. One request per connection, then the connection is closed.
use std::net::SocketAddr;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener;
use crate::upstream::Recorded;
pub enum Reply {
/// Write these bytes, then close.
Bytes(Vec<u8>),
/// Accept, read the request, never answer (until the peer gives up).
Hang,
}
/// A complete response with Content-Length and `Connection: close`.
pub fn response(status: u16, reason: &str, headers: &[(&str, &str)], body: &[u8]) -> Vec<u8> {
let mut out = format!("HTTP/1.1 {status} {reason}\r\n");
for (n, v) in headers {
out.push_str(&format!("{n}: {v}\r\n"));
}
out.push_str(&format!("Content-Length: {}\r\nConnection: close\r\n\r\n", body.len()));
let mut bytes = out.into_bytes();
bytes.extend_from_slice(body);
bytes
}
type Handler = dyn Fn(&Recorded) -> Reply + Send + Sync;
#[derive(Clone)]
pub struct RawServer {
pub addr: SocketAddr,
requests: Arc<Mutex<Vec<Recorded>>>,
}
impl RawServer {
pub async fn start(handler: impl Fn(&Recorded) -> Reply + Send + Sync + 'static) -> RawServer {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let server = RawServer { addr: listener.local_addr().unwrap(), requests: Arc::default() };
let handler: Arc<Handler> = Arc::new(handler);
let requests = server.requests.clone();
tokio::spawn(async move {
loop {
let Ok((mut stream, _)) = listener.accept().await else { continue };
let handler = handler.clone();
let requests = requests.clone();
tokio::spawn(async move {
let mut buf = Vec::new();
let mut chunk = [0u8; 4096];
while !buf.windows(4).any(|w| w == b"\r\n\r\n") && buf.len() < 65536 {
match stream.read(&mut chunk).await {
Ok(0) | Err(_) => return,
Ok(n) => buf.extend_from_slice(&chunk[..n]),
}
}
let Some(req) = parse(&buf) else { return };
requests.lock().unwrap().push(req.clone());
match handler(&req) {
Reply::Bytes(b) => {
let _ = stream.write_all(&b).await;
let _ = stream.shutdown().await;
}
Reply::Hang => tokio::time::sleep(Duration::from_secs(600)).await,
}
});
}
});
server
}
pub fn requests(&self) -> Vec<Recorded> {
self.requests.lock().unwrap().clone()
}
pub fn url(&self) -> String {
format!("http://{}", self.addr)
}
}
fn parse(buf: &[u8]) -> Option<Recorded> {
let end = buf.windows(4).position(|w| w == b"\r\n\r\n")?;
let head = std::str::from_utf8(&buf[..end]).ok()?;
let mut lines = head.split("\r\n");
let mut first = lines.next()?.split(' ');
let method = first.next()?.to_owned();
let target = first.next()?.to_owned();
let path = target.split('?').next().unwrap_or("").to_owned();
let headers = lines
.filter_map(|l| l.split_once(':'))
.map(|(n, v)| (n.trim().to_ascii_lowercase(), v.trim().as_bytes().to_vec()))
.collect();
Some(Recorded { method, path, headers, body: Vec::new() })
}