oxedyne/fe2o3/fe2o3_steel/tests/route_dispatch.rs
14.9 KiB, 8 runs
created by r1870400018:65945, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! Route dispatch driven through `handle_https` against a real socket: an exact proxy route, and a |
| 2 | //! redirect that carries the query string. |
| 3 | //! |
| 4 | //! The unit tests in `srv::cfg` show that `ProxyRoute::matches` tells an exact route from a prefix |
| 5 | //! one, and that `RedirectRule::resolve_target` fills `{uri}`. What they cannot show is that the |
| 6 | //! dispatcher uses them, so this puts a TLS connection into `handle_https` with two vhosts: one whose |
| 7 | //! only route is an exact `/subscribe`, with a capturing upstream behind it that records every |
| 8 | //! request it is sent, and one that redirects everything to the first. |
| 9 | //! |
| 10 | //! What it holds for the proxy: the exact path reaches the upstream, with its body and one |
| 11 | //! `X-Forwarded-For` naming the client; the upstream's status and body come back untouched; the |
| 12 | //! neighbours of the path (`/subscribe.py`, `/subscribers.txt`, `/subscribe/`) never reach the |
| 13 | //! upstream; and an upstream that is down is a 502. For the redirect: the target names the path and |
| 14 | //! the query exactly as they arrived. |
| 15 | //! |
| 16 | //! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\ |
| 17 | //! Anthropic Claude |
| 18 | |
| 19 | use oxedyne_fe2o3_steel::{ |
| 20 | app::https::AppWebHandler, |
| 21 | srv::{ |
| 22 | api::ApiHandlerRegistry, |
| 23 | cfg::{ |
| 24 | ProxyRoute, |
| 25 | RedirectMatch, |
| 26 | RedirectRule, |
| 27 | ServerConfig, |
| 28 | }, |
| 29 | context::{ |
| 30 | self, |
| 31 | Protocol, |
| 32 | ServerContext, |
| 33 | VhostRuntime, |
| 34 | }, |
| 35 | id, |
| 36 | webhook::WebhookRegistry, |
| 37 | ws::{ |
| 38 | handler::AppWebSocketHandler, |
| 39 | syntax::WebSocketSyntax, |
| 40 | }, |
| 41 | }, |
| 42 | }; |
| 43 | |
| 44 | use oxedyne_fe2o3_core::{ |
| 45 | file::OsPath, |
| 46 | path::NormalPath, |
| 47 | prelude::*, |
| 48 | }; |
| 49 | use oxedyne_fe2o3_jdat::version::SemVer; |
| 50 | |
| 51 | use std::{ |
| 52 | collections::HashMap, |
| 53 | path::Path, |
| 54 | sync::{ |
| 55 | Arc, |
| 56 | Mutex, |
| 57 | RwLock, |
| 58 | }, |
| 59 | }; |
| 60 | |
| 61 | use tokio::{ |
| 62 | io::{ |
| 63 | AsyncReadExt, |
| 64 | AsyncWriteExt, |
| 65 | }, |
| 66 | net::{ |
| 67 | TcpListener, |
| 68 | TcpStream, |
| 69 | }, |
| 70 | }; |
| 71 | use tokio_rustls::{ |
| 72 | rustls::{ |
| 73 | pki_types::{ |
| 74 | CertificateDer, |
| 75 | PrivateKeyDer, |
| 76 | PrivatePkcs8KeyDer, |
| 77 | ServerName, |
| 78 | }, |
| 79 | ClientConfig, |
| 80 | RootCertStore, |
| 81 | ServerConfig as TlsServerConfig, |
| 82 | }, |
| 83 | TlsAcceptor, |
| 84 | TlsConnector, |
| 85 | }; |
| 86 | |
| 87 | const HOST: &str = "subscribe.test"; |
| 88 | const WWW: &str = "www.subscribe.test"; |
| 89 | |
| 90 | /// What the upstream was sent: the request line, the `X-Forwarded-For` lines, and the body. |
| 91 | #[derive(Clone, Debug)] |
| 92 | struct Seen { |
| 93 | line: String, |
| 94 | xff: Vec<String>, |
| 95 | body: String, |
| 96 | } |
| 97 | |
| 98 | fn no_dbs<T>(_: fn(&Path, &[u8], bool) -> Outcome<T>) |
| 99 | -> Arc<RwLock<HashMap<String, (Arc<RwLock<T>>, id::Uid)>>> |
| 100 | { |
| 101 | Arc::new(RwLock::new(HashMap::new())) |
| 102 | } |
| 103 | |
| 104 | /// A one-shot-per-connection HTTP upstream. It answers 400 when the body says `bad` and 200 |
| 105 | /// otherwise, closing after each reply as `http.server` does, and records what it was sent. |
| 106 | async fn upstream(seen: Arc<Mutex<Vec<Seen>>>) -> Outcome<u16> { |
| 107 | let listener = res!(TcpListener::bind("127.0.0.1:0").await, Network, Init); |
| 108 | let port = res!(listener.local_addr(), Network, Init).port(); |
| 109 | tokio::spawn(async move { |
| 110 | loop { |
| 111 | let (mut tcp, _) = match listener.accept().await { |
| 112 | Ok(c) => c, |
| 113 | Err(_) => return, |
| 114 | }; |
| 115 | let seen = seen.clone(); |
| 116 | tokio::spawn(async move { |
| 117 | let mut buf = Vec::new(); |
| 118 | let mut chunk = [0u8; 4096]; |
| 119 | let split = loop { |
| 120 | if let Some(i) = buf.windows(4).position(|w| w == b"\r\n\r\n") { |
| 121 | break i; |
| 122 | } |
| 123 | match tcp.read(&mut chunk).await { |
| 124 | Ok(0) | Err(_) => return, |
| 125 | Ok(n) => buf.extend_from_slice(&chunk[..n]), |
| 126 | } |
| 127 | }; |
| 128 | let head = String::from_utf8_lossy(&buf[..split]).to_string(); |
| 129 | let want = head.lines() |
| 130 | .filter_map(|l| l.split_once(':')) |
| 131 | .find(|(k, _)| k.trim().eq_ignore_ascii_case("content-length")) |
| 132 | .and_then(|(_, v)| v.trim().parse::<usize>().ok()) |
| 133 | .unwrap_or(0); |
| 134 | while buf.len() < split + 4 + want { |
| 135 | match tcp.read(&mut chunk).await { |
| 136 | Ok(0) | Err(_) => break, |
| 137 | Ok(n) => buf.extend_from_slice(&chunk[..n]), |
| 138 | } |
| 139 | } |
| 140 | let body = String::from_utf8_lossy(&buf[split + 4..]).to_string(); |
| 141 | let xff = head.lines() |
| 142 | .filter_map(|l| l.split_once(':')) |
| 143 | .filter(|(k, _)| k.trim().eq_ignore_ascii_case("x-forwarded-for")) |
| 144 | .map(|(_, v)| v.trim().to_string()) |
| 145 | .collect(); |
| 146 | let (code, text, reply) = if body.contains("bad") { |
| 147 | (400, "Bad Request", "{\"message\": \"Invalid request.\"}") |
| 148 | } else { |
| 149 | (200, "OK", "{\"message\": \"Subscribed. Thank you.\"}") |
| 150 | }; |
| 151 | if let Ok(mut g) = seen.lock() { |
| 152 | g.push(Seen { |
| 153 | line: head.lines().next().unwrap_or("").to_string(), |
| 154 | xff, |
| 155 | body, |
| 156 | }); |
| 157 | } |
| 158 | let out = fmt!("HTTP/1.0 {} {}\r\nContent-Type: application/json\r\n\ |
| 159 | Content-Length: {}\r\nConnection: close\r\n\r\n{}", |
| 160 | code, text, reply.len(), reply); |
| 161 | let _ = tcp.write_all(out.as_bytes()).await; |
| 162 | let _ = tcp.shutdown().await; |
| 163 | }); |
| 164 | } |
| 165 | }); |
| 166 | Ok(port) |
| 167 | } |
| 168 | |
| 169 | /// A port nothing listens on: bound, noted and released. |
| 170 | async fn closed_port() -> Outcome<u16> { |
| 171 | let listener = res!(TcpListener::bind("127.0.0.1:0").await, Network, Init); |
| 172 | Ok(res!(listener.local_addr(), Network, Init).port()) |
| 173 | } |
| 174 | |
| 175 | fn route(path: &str, exact: bool, port: u16) -> ProxyRoute { |
| 176 | ProxyRoute { |
| 177 | path_prefix: path.to_string(), |
| 178 | exact, |
| 179 | upstream_host: "127.0.0.1".to_string(), |
| 180 | upstream_port: port, |
| 181 | upstream_tls: false, |
| 182 | strip_prefix: false, |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | #[test] |
| 187 | fn an_exact_proxy_route_and_a_redirect_that_keeps_its_query() -> Outcome<()> { |
| 188 | let runtime = res!(tokio::runtime::Runtime::new(), Init); |
| 189 | runtime.block_on(async { drive().await }) |
| 190 | } |
| 191 | |
| 192 | async fn drive() -> Outcome<()> { |
| 193 | oxedyne_fe2o3_net::tls::ensure_crypto_provider(); |
| 194 | let cert = res!(rcgen::generate_simple_self_signed(vec![HOST.to_string(), WWW.to_string()]), Init); |
| 195 | let der = res!(cert.serialize_der(), Init); |
| 196 | let key = PrivateKeyDer::Pkcs8(PrivatePkcs8KeyDer::from(cert.serialize_private_key_der())); |
| 197 | let server_tls = res!(TlsServerConfig::builder() |
| 198 | .with_no_client_auth() |
| 199 | .with_single_cert(vec![CertificateDer::from(der.clone())], key), Init); |
| 200 | let mut roots = RootCertStore::empty(); |
| 201 | res!(roots.add(CertificateDer::from(der)), Init); |
| 202 | let client_tls = ClientConfig::builder().with_root_certificates(roots).with_no_client_auth(); |
| 203 | let tls = (Arc::new(server_tls), Arc::new(client_tls)); |
| 204 | |
| 205 | let seen = Arc::new(Mutex::new(Vec::new())); |
| 206 | let up_port = res!(upstream(seen.clone()).await); |
| 207 | let dead_port = res!(closed_port().await); |
| 208 | |
| 209 | let cfg = ServerConfig::default(); |
| 210 | let web = || -> AppWebHandler<HashMap<String, OsPath>> { |
| 211 | AppWebHandler::new( |
| 212 | cfg.clone(), |
| 213 | std::env::temp_dir().join(fmt!("steel_route_dispatch_{}", std::process::id())), |
| 214 | HashMap::new(), |
| 215 | vec![fmt!("index.html")], |
| 216 | true, |
| 217 | Vec::new(), |
| 218 | Vec::new(), |
| 219 | Arc::new(WebhookRegistry::new()), |
| 220 | Arc::new(ApiHandlerRegistry::new()), |
| 221 | None, |
| 222 | None, |
| 223 | None, |
| 224 | None, |
| 225 | None, |
| 226 | Arc::new(Vec::new()), |
| 227 | ) |
| 228 | }; |
| 229 | let vhost = |name: &str, redirects: Vec<RedirectRule>, proxy_routes: Vec<ProxyRoute>| |
| 230 | -> Outcome<Arc<VhostRuntime<_, _>>> |
| 231 | { |
| 232 | Ok(Arc::new(VhostRuntime { |
| 233 | hostnames: vec![name.to_string()], |
| 234 | web_handler: web(), |
| 235 | ws_handler: AppWebSocketHandler::new(None), |
| 236 | ws_syntax: res!(WebSocketSyntax::new("steel_ws", &SemVer::new(0, 1, 0), "Route test")), |
| 237 | redirects, |
| 238 | proxy_routes, |
| 239 | ws_routes: Vec::new(), |
| 240 | term_manager: None, |
| 241 | uses_sessions: false, |
| 242 | permissions_policy: None, |
| 243 | tiles: None, |
| 244 | access_log: false, |
| 245 | response_headers: Vec::new(), |
| 246 | path_headers: Vec::new(), |
| 247 | })) |
| 248 | }; |
| 249 | let mut vhosts = HashMap::new(); |
| 250 | vhosts.insert(HOST.to_string(), res!(vhost(HOST, Vec::new(), vec![ |
| 251 | route("/subscribe", true, up_port), |
| 252 | route("/down", true, dead_port), |
| 253 | ]))); |
| 254 | // The www vhost sends everything to the first, as the live configs do. |
| 255 | vhosts.insert(WWW.to_string(), res!(vhost(WWW, vec![RedirectRule { |
| 256 | match_kind: RedirectMatch::All, |
| 257 | match_path: String::new(), |
| 258 | target: fmt!("https://{}{{uri}}", HOST), |
| 259 | status: 301, |
| 260 | }], Vec::new()))); |
| 261 | let protocol = Protocol::Web { |
| 262 | vhosts: Arc::new(vhosts), |
| 263 | default_vhost: HOST.to_string(), |
| 264 | dev_mode: true, |
| 265 | }; |
| 266 | let root = std::env::temp_dir().normalise().absolute(); |
| 267 | let context = ServerContext::new(cfg, root, no_dbs(context::new_db), Vec::new(), protocol, |
| 268 | None, None); |
| 269 | |
| 270 | // One request over a fresh TLS connection: the head, lower cased, and the body. |
| 271 | let fetch = async |host: &str, method: &str, path: &str, body: &str| -> Outcome<(String, String)> { |
| 272 | let listener = res!(TcpListener::bind("127.0.0.1:0").await, Network, Init); |
| 273 | let addr = res!(listener.local_addr(), Network, Init); |
| 274 | let ctx = context.clone(); |
| 275 | let acceptor = TlsAcceptor::from(tls.0.clone()); |
| 276 | let host_owned = host.to_string(); |
| 277 | let server = tokio::spawn(async move { |
| 278 | let (tcp, peer) = match listener.accept().await { |
| 279 | Ok(c) => c, |
| 280 | Err(e) => return Err(err!(e, "Accept failed."; Network)), |
| 281 | }; |
| 282 | let stream = match acceptor.accept(tcp).await { |
| 283 | Ok(s) => s, |
| 284 | Err(e) => return Err(err!(e, "TLS accept failed."; Network)), |
| 285 | }; |
| 286 | ctx.handle_https(stream, Some(host_owned), peer, false).await |
| 287 | }); |
| 288 | let tcp = res!(TcpStream::connect(addr).await, Network, Init); |
| 289 | let name = res!(ServerName::try_from(host.to_string()), Invalid); |
| 290 | let mut stream = res!(TlsConnector::from(tls.1.clone()).connect(name, tcp).await, Network); |
| 291 | let request = fmt!("{} {} HTTP/1.1\r\nHost: {}\r\nContent-Type: application/json\r\n\ |
| 292 | X-Forwarded-For: 9.9.9.9\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}", |
| 293 | method, path, host, body.len(), body); |
| 294 | res!(stream.write_all(request.as_bytes()).await, Network, Write); |
| 295 | res!(stream.flush().await, Network, Write); |
| 296 | // Read one whole response, framed by its Content-Length, and no further. Steel keeps a |
| 297 | // connection open for the next request even when this one said `Connection: close`, so |
| 298 | // reading to the end would wait out its header-read timeout (fifteen seconds) on every |
| 299 | // answer it gives itself rather than relays. |
| 300 | let mut reply = Vec::new(); |
| 301 | let mut chunk = [0u8; 4096]; |
| 302 | loop { |
| 303 | if let Some(i) = reply.windows(4).position(|w| w == b"\r\n\r\n") { |
| 304 | let head = String::from_utf8_lossy(&reply[..i]).to_lowercase(); |
| 305 | let want = head.lines() |
| 306 | .filter_map(|l| l.strip_prefix("content-length:")) |
| 307 | .filter_map(|v| v.trim().parse::<usize>().ok()) |
| 308 | .next() |
| 309 | .unwrap_or(0); |
| 310 | if reply.len() >= i + 4 + want { |
| 311 | break; |
| 312 | } |
| 313 | } |
| 314 | match stream.read(&mut chunk).await { |
| 315 | Ok(0) | Err(_) => break, |
| 316 | Ok(n) => reply.extend_from_slice(&chunk[..n]), |
| 317 | } |
| 318 | } |
| 319 | drop(stream); |
| 320 | let _ = server.await; |
| 321 | let split = match reply.windows(4).position(|w| w == b"\r\n\r\n") { |
| 322 | Some(i) => i, |
| 323 | None => return Err(err!("No complete response: {:?}.", |
| 324 | String::from_utf8_lossy(&reply); Test)), |
| 325 | }; |
| 326 | Ok(( |
| 327 | String::from_utf8_lossy(&reply[..split]).to_lowercase(), |
| 328 | String::from_utf8_lossy(&reply[split + 4..]).to_string(), |
| 329 | )) |
| 330 | }; |
| 331 | let hits = || seen.lock().map(|g| g.len()).unwrap_or(usize::MAX); |
| 332 | |
| 333 | // The exact path reaches the upstream, body intact, status and body coming back untouched. |
| 334 | let (head, body) = res!(fetch(HOST, "POST", "/subscribe", "{\"email\": \"a@b.example\"}").await); |
| 335 | assert!(head.starts_with("http/1.0 200") || head.starts_with("http/1.1 200"), "{}", head); |
| 336 | assert!(body.contains("Subscribed. Thank you."), "{}", body); |
| 337 | assert_eq!(hits(), 1); |
| 338 | let first = match seen.lock() { |
| 339 | Ok(g) => g[0].clone(), |
| 340 | Err(_) => return Err(err!("The capture lock is poisoned."; Test)), |
| 341 | }; |
| 342 | assert_eq!(first.line, "POST /subscribe HTTP/1.1"); |
| 343 | assert_eq!(first.body, "{\"email\": \"a@b.example\"}"); |
| 344 | // One forwarded address, this hop's, and the forged one the caller sent is gone. |
| 345 | assert_eq!(first.xff, vec![fmt!("127.0.0.1")], "{:?}", first); |
| 346 | |
| 347 | // An error status and its body are passed through, not replaced. |
| 348 | let (head, body) = res!(fetch(HOST, "POST", "/subscribe", "bad").await); |
| 349 | assert!(head.contains(" 400"), "{}", head); |
| 350 | assert!(body.contains("Invalid request."), "{}", body); |
| 351 | assert_eq!(hits(), 2); |
| 352 | |
| 353 | // The neighbours of the path never reach the upstream. |
| 354 | for path in ["/subscribe.py", "/subscribers.txt", "/subscribe/", "/subscribe/x", "/subscribe?x=1x"] { |
| 355 | let (head, _) = res!(fetch(HOST, "GET", path, "").await); |
| 356 | let proxied = path == "/subscribe?x=1x"; |
| 357 | assert!(proxied || head.contains(" 404"), "{} should be a 404, got: {}", path, head); |
| 358 | assert_eq!(hits(), if proxied { 3 } else { 2 }, "{} reached the upstream", path); |
| 359 | } |
| 360 | |
| 361 | // An upstream that is down is a 502. |
| 362 | let (head, _) = res!(fetch(HOST, "POST", "/down", "{}").await); |
| 363 | assert!(head.contains(" 502"), "{}", head); |
| 364 | |
| 365 | // A redirect carries the query string as well as the path, byte for byte, and no more. |
| 366 | let (head, _) = res!(fetch(WWW, "GET", "/credits.html?ref=x&y=1", "").await); |
| 367 | assert!(head.starts_with("http/1.1 301"), "{}", head); |
| 368 | assert!(head.contains("location: https://subscribe.test/credits.html?ref=x&y=1\r\n") |
| 369 | || head.ends_with("location: https://subscribe.test/credits.html?ref=x&y=1"), "{}", head); |
| 370 | let (head, _) = res!(fetch(WWW, "GET", "/", "").await); |
| 371 | assert!(head.contains("location: https://subscribe.test/\r\n") |
| 372 | || head.ends_with("location: https://subscribe.test/"), "{}", head); |
| 373 | Ok(()) |
| 374 | } |