Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_steel/tests/api_http10.rs

7.3 KiB, 1 run

created by r1870400018:65937, 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//! An `api_routes` proxy to an HTTP/1.0 upstream, such as Python's `http.server`.
2//!
3//! The rehearsal of ontheism.org's move to Steel found an `api_routes` entry pointing at
4//! `http.server` answering the visitor with nothing at all. The outbound client refused the
5//! upstream's `HTTP/1.0` status line as an unrecognised version, the error left `handle_get`
6//! unhandled, and the connection was dropped.
7//!
8//! A real HTTP/1.0 upstream is stood up here on loopback, written byte by byte rather than with a
9//! library so that it says exactly what `http.server` says, and a vhost proxies four paths to it
10//! through `handle_https` over TLS: a reply with a `Content-Length`; one with none, which
11//! HTTP/1.0 ends by closing the connection; a `POST` echoed back; and a `204`. What is held: the
12//! visitor gets the status and the whole body, in HTTP/1.1 and not in the upstream's version; and
13//! a visitor who asks Steel itself in HTTP/1.0 is answered too, since one parser reads both.
14//!
15//! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\
16//! Anthropic Claude
17
18mod vhost_rig;
19
20use vhost_rig::{
21 Rig,
22 Scratch,
23 Site,
24};
25
26use oxedyne_fe2o3_core::prelude::*;
27use oxedyne_fe2o3_steel::srv::cfg::ApiRoute;
28
29use tokio::{
30 io::{
31 AsyncReadExt,
32 AsyncWriteExt,
33 },
34 net::TcpListener,
35};
36
37const HOST: &str = "api.test";
38
39fn body_of(len: usize) -> String {
40 (0..len).map(|i| (b'a' + (i % 26) as u8) as char).collect()
41}
42
43/// An upstream that answers `HTTP/1.0` and closes after each reply, as `http.server` does.
44async fn upstream() -> Outcome<u16> {
45 let listener = res!(TcpListener::bind("127.0.0.1:0").await, Network, Init);
46 let port = res!(listener.local_addr(), Network, Init).port();
47 tokio::spawn(async move {
48 loop {
49 let (mut tcp, _) = match listener.accept().await {
50 Ok(c) => c,
51 Err(_) => return,
52 };
53 tokio::spawn(async move {
54 let mut buf = Vec::new();
55 let mut chunk = [0u8; 4096];
56 let split = loop {
57 if let Some(i) = buf.windows(4).position(|w| w == b"\r\n\r\n") {
58 break i;
59 }
60 match tcp.read(&mut chunk).await {
61 Ok(0) | Err(_) => return,
62 Ok(n) => buf.extend_from_slice(&chunk[..n]),
63 }
64 };
65 let head = String::from_utf8_lossy(&buf[..split]).to_string();
66 let want = head.lines()
67 .filter_map(|l| l.split_once(':'))
68 .find(|(k, _)| k.trim().eq_ignore_ascii_case("content-length"))
69 .and_then(|(_, v)| v.trim().parse::<usize>().ok())
70 .unwrap_or(0);
71 while buf.len() < split + 4 + want {
72 match tcp.read(&mut chunk).await {
73 Ok(0) | Err(_) => break,
74 Ok(n) => buf.extend_from_slice(&chunk[..n]),
75 }
76 }
77 let body = buf[split + 4..].to_vec();
78 let line = head.lines().next().unwrap_or("").to_string();
79 let mut out: Vec<u8> = Vec::new();
80 if line.contains(" /with-length ") {
81 let text = body_of(9_000);
82 out.extend_from_slice(fmt!("HTTP/1.0 200 OK\r\nServer: BaseHTTP/0.6 Python/3\r\n\
83 Content-Type: text/plain\r\nContent-Length: {}\r\n\r\n{}",
84 text.len(), text).as_bytes());
85 } else if line.contains(" /no-length ") {
86 out.extend_from_slice(fmt!("HTTP/1.0 200 OK\r\nServer: BaseHTTP/0.6 Python/3\r\n\
87 Content-Type: text/plain\r\n\r\n{}", body_of(9_000)).as_bytes());
88 } else if line.contains(" /echo ") {
89 out.extend_from_slice(fmt!("HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\
90 Content-Length: {}\r\n\r\n", body.len()).as_bytes());
91 out.extend_from_slice(&body);
92 } else if line.contains(" /empty ") {
93 out.extend_from_slice(b"HTTP/1.0 204 No Content\r\n\r\n");
94 } else {
95 out.extend_from_slice(b"HTTP/1.0 404 Not Found\r\nContent-Length: 0\r\n\r\n");
96 }
97 let _ = tcp.write_all(&out).await;
98 let _ = tcp.shutdown().await;
99 });
100 }
101 });
102 Ok(port)
103}
104
105fn route(path: &str, upstream_path: &str, port: u16) -> ApiRoute {
106 ApiRoute {
107 path: path.to_string(),
108 upstream_host: Some("127.0.0.1".to_string()),
109 upstream_port: Some(port),
110 upstream_path: Some(upstream_path.to_string()),
111 upstream_tls: false,
112 headers: Vec::new(),
113 handler: None,
114 config: Vec::new(),
115 }
116}
117
118#[test]
119fn an_api_route_reaches_an_http_1_0_upstream() -> Outcome<()> {
120 let web = res!(Scratch::new("api_http10"));
121 res!(web.put("index.html", "<p>home</p>"));
122 res!(web.put("file.txt", "a plain file"));
123 let runtime = res!(vhost_rig::runtime());
124 runtime.block_on(async {
125 let port = res!(upstream().await);
126 let mut site = Site::new(HOST, &web.0);
127 site.api_routes = vec![
128 route("/h10/with-length", "/with-length", port),
129 route("/h10/no-length", "/no-length", port),
130 route("/h10/echo", "/echo", port),
131 route("/h10/empty", "/empty", port),
132 ];
133 let rig = res!(Rig::new(vec![site]));
134
135 // A reply that states its length, and one that does not and ends with the connection.
136 for path in ["/h10/with-length", "/h10/no-length"] {
137 let r = res!(rig.fetch(HOST, "GET", path, &[], "").await);
138 assert!(r.head.starts_with("HTTP/1.1 200"),
139 "GET {} must be answered in HTTP/1.1 with the upstream's 200: {:?}", path, r.head);
140 assert_eq!(r.body.len(), 9_000, "GET {} lost part of the body", path);
141 assert_eq!(r.body, body_of(9_000), "GET {}", path);
142 assert_eq!(r.field("content-length").as_deref(), Some("9000"), "GET {}", path);
143 }
144
145 // A POST whose body the upstream echoes.
146 let r = res!(rig.fetch(HOST, "POST", "/h10/echo",
147 &["Content-Type: text/plain"], "round trip 0123456789").await);
148 assert!(r.head.starts_with("HTTP/1.1 200"), "{:?}", r.head);
149 assert_eq!(r.body, "round trip 0123456789");
150
151 // A status that has no body ends at its head, with the connection left open by the
152 // upstream or not.
153 let r = res!(rig.fetch(HOST, "GET", "/h10/empty", &[], "").await);
154 assert!(r.head.starts_with("HTTP/1.1 204"), "{:?}", r.head);
155 assert!(r.body.is_empty(), "{:?}", r);
156
157 // Steel is asked in HTTP/1.0 as well, and answers in HTTP/1.1, as RFC 9112 2.3 has a
158 // server do; the same parser reads both, and the request used to be dropped unread.
159 let r = res!(rig.fetch_as(HOST, "HTTP/1.0", "GET", "/file.txt", &[], "").await);
160 assert!(r.head.starts_with("HTTP/1.1 200"), "{:?}", r.head);
161 assert_eq!(r.body, "a plain file");
162 let r = res!(rig.fetch_as(HOST, "HTTP/1.0", "GET", "/h10/no-length", &[], "").await);
163 assert_eq!(r.body, body_of(9_000));
164 Ok(())
165 })
166}