| @@ -0,0 +1,278 @@ |
| 1 | +//! Raw HTTP/1.1 response framing, end to end. Pipelining a successful response |
| 2 | +//! behind every response without a body catches leaked bytes and final chunks. |
| 3 | + |
| 4 | +use std::io::{BufRead, BufReader, Read, Write}; |
| 5 | +use std::net::TcpStream; |
| 6 | +use std::path::{Path, PathBuf}; |
| 7 | +use std::process::{Child, Command, Stdio}; |
| 8 | +use std::sync::OnceLock; |
| 9 | + |
| 10 | +struct Server(Child); |
| 11 | + |
| 12 | +impl Drop for Server { |
| 13 | + fn drop(&mut self) { |
| 14 | + let _ = self.0.kill(); |
| 15 | + let _ = self.0.wait(); |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +#[derive(Debug)] |
| 20 | +struct Reply { |
| 21 | + head: String, |
| 22 | + status: u16, |
| 23 | + body: Vec<u8>, |
| 24 | +} |
| 25 | + |
| 26 | +fn header<'a>(reply: &'a Reply, name: &str) -> Option<&'a str> { |
| 27 | + reply.head.lines().find_map(|line| { |
| 28 | + let (key, value) = line.split_once(':')?; |
| 29 | + key.eq_ignore_ascii_case(name).then(|| value.trim()) |
| 30 | + }) |
| 31 | +} |
| 32 | + |
| 33 | +fn fixture() -> Option<&'static Path> { |
| 34 | + static EXE: OnceLock<Option<PathBuf>> = OnceLock::new(); |
| 35 | + EXE.get_or_init(|| { |
| 36 | + let louise = Path::new(env!("CARGO_BIN_EXE_louise")); |
| 37 | + if !louise.with_file_name("liblouise_rt.a").is_file() { |
| 38 | + eprintln!("skipping: liblouise_rt.a not built"); |
| 39 | + return None; |
| 40 | + } |
| 41 | + let exe = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("http-response"); |
| 42 | + let out = Command::new(louise) |
| 43 | + .args(["build", "tests/fixtures/http/response.lse", "-o"]) |
| 44 | + .arg(&exe) |
| 45 | + .current_dir(env!("CARGO_MANIFEST_DIR")) |
| 46 | + .output() |
| 47 | + .unwrap(); |
| 48 | + assert!( |
| 49 | + out.status.success(), |
| 50 | + "{}", |
| 51 | + String::from_utf8_lossy(&out.stderr) |
| 52 | + ); |
| 53 | + Some(exe) |
| 54 | + }) |
| 55 | + .as_deref() |
| 56 | +} |
| 57 | + |
| 58 | +fn find(haystack: &[u8], needle: &[u8]) -> Option<usize> { |
| 59 | + haystack |
| 60 | + .windows(needle.len()) |
| 61 | + .position(|window| window == needle) |
| 62 | +} |
| 63 | + |
| 64 | +fn chunked_end(wire: &[u8]) -> usize { |
| 65 | + let mut rest = wire; |
| 66 | + loop { |
| 67 | + let line_end = find(rest, b"\r\n").expect("a chunk-size line"); |
| 68 | + let size = usize::from_str_radix( |
| 69 | + std::str::from_utf8(&rest[..line_end]).expect("an ASCII chunk size"), |
| 70 | + 16, |
| 71 | + ) |
| 72 | + .expect("a hexadecimal chunk size"); |
| 73 | + let consumed = line_end + 2 + size + 2; |
| 74 | + assert!(rest.len() >= consumed, "a complete chunk in {wire:?}"); |
| 75 | + if size == 0 { |
| 76 | + return wire.len() - rest.len() + consumed; |
| 77 | + } |
| 78 | + assert_eq!(&rest[line_end + 2 + size..consumed], b"\r\n"); |
| 79 | + rest = &rest[consumed..]; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +fn conversation(requests: &[(&str, &str)]) -> Option<(Vec<Reply>, String)> { |
| 84 | + let exe = fixture()?; |
| 85 | + let mut child = Command::new(exe).stdout(Stdio::piped()).spawn().unwrap(); |
| 86 | + let mut stdout = BufReader::new(child.stdout.take().unwrap()); |
| 87 | + let mut port = String::new(); |
| 88 | + stdout.read_line(&mut port).unwrap(); |
| 89 | + let port: u16 = port.trim().parse().expect("the server prints its port"); |
| 90 | + let mut server = Server(child); |
| 91 | + |
| 92 | + let mut conn = TcpStream::connect(("127.0.0.1", port)).unwrap(); |
| 93 | + let mut pipeline = Vec::new(); |
| 94 | + for (index, (method, path)) in requests.iter().enumerate() { |
| 95 | + let close = if index + 1 == requests.len() { |
| 96 | + "Connection: close\r\n" |
| 97 | + } else { |
| 98 | + "" |
| 99 | + }; |
| 100 | + pipeline.extend_from_slice( |
| 101 | + format!("{method} {path} HTTP/1.1\r\nHost: x\r\n{close}\r\n").as_bytes(), |
| 102 | + ); |
| 103 | + } |
| 104 | + conn.write_all(&pipeline).unwrap(); |
| 105 | + let mut wire = Vec::new(); |
| 106 | + conn.read_to_end(&mut wire).unwrap(); |
| 107 | + |
| 108 | + let mut replies = Vec::new(); |
| 109 | + let mut rest = wire.as_slice(); |
| 110 | + for (method, path) in requests { |
| 111 | + let head_end = find(rest, b"\r\n\r\n") |
| 112 | + .unwrap_or_else(|| panic!("a complete response to {method} {path}: {rest:?}")); |
| 113 | + let head = std::str::from_utf8(&rest[..head_end]) |
| 114 | + .expect("an ASCII response head") |
| 115 | + .to_owned(); |
| 116 | + assert!( |
| 117 | + head.starts_with("HTTP/1.1 "), |
| 118 | + "the response after {method} {path} starts with a status line: {head:?}" |
| 119 | + ); |
| 120 | + let status = head |
| 121 | + .split_whitespace() |
| 122 | + .nth(1) |
| 123 | + .and_then(|status| status.parse().ok()) |
| 124 | + .expect("a numeric status"); |
| 125 | + rest = &rest[head_end + 4..]; |
| 126 | + let no_body = |
| 127 | + *method == "HEAD" || (100..200).contains(&status) || status == 204 || status == 304; |
| 128 | + let body = if no_body { |
| 129 | + Vec::new() |
| 130 | + } else if head |
| 131 | + .lines() |
| 132 | + .any(|line| line.eq_ignore_ascii_case("Transfer-Encoding: chunked")) |
| 133 | + { |
| 134 | + let end = chunked_end(rest); |
| 135 | + let body = rest[..end].to_vec(); |
| 136 | + rest = &rest[end..]; |
| 137 | + body |
| 138 | + } else { |
| 139 | + let length = head |
| 140 | + .lines() |
| 141 | + .find_map(|line| { |
| 142 | + let (name, value) = line.split_once(':')?; |
| 143 | + name.eq_ignore_ascii_case("Content-Length") |
| 144 | + .then(|| value.trim().parse::<usize>().unwrap()) |
| 145 | + }) |
| 146 | + .unwrap_or_else(|| panic!("a framed response to {method} {path}: {head}")); |
| 147 | + assert!(rest.len() >= length, "the whole response body: {rest:?}"); |
| 148 | + let body = rest[..length].to_vec(); |
| 149 | + rest = &rest[length..]; |
| 150 | + body |
| 151 | + }; |
| 152 | + replies.push(Reply { head, status, body }); |
| 153 | + } |
| 154 | + assert!( |
| 155 | + rest.is_empty(), |
| 156 | + "no bytes left after the pipeline: {rest:?}" |
| 157 | + ); |
| 158 | + |
| 159 | + assert!(server.0.wait().unwrap().success()); |
| 160 | + let mut printed = String::new(); |
| 161 | + stdout.read_to_string(&mut printed).unwrap(); |
| 162 | + Some((replies, printed)) |
| 163 | +} |
| 164 | + |
| 165 | +fn no_body(reply: &Reply, status: u16) { |
| 166 | + assert_eq!(reply.status, status, "{}", reply.head); |
| 167 | + assert_eq!(header(reply, "Content-Length"), None, "{}", reply.head); |
| 168 | + assert_eq!(header(reply, "Transfer-Encoding"), None, "{}", reply.head); |
| 169 | + assert_eq!(header(reply, "Connection"), Some("keep-alive")); |
| 170 | + assert_eq!(header(reply, "ETag"), Some("\"v1\"")); |
| 171 | + assert_eq!(header(reply, "Cache-Control"), Some("max-age=60")); |
| 172 | + assert!(reply.body.is_empty(), "{:?}", reply.body); |
| 173 | +} |
| 174 | + |
| 175 | +fn next(reply: &Reply) { |
| 176 | + assert_eq!(reply.status, 200, "{}", reply.head); |
| 177 | + assert_eq!(header(reply, "Content-Length"), Some("4")); |
| 178 | + assert_eq!(reply.body, b"next"); |
| 179 | +} |
| 180 | + |
| 181 | +#[test] |
| 182 | +fn whole_no_body_statuses_do_not_desynchronize_keep_alive() { |
| 183 | + let Some((replies, printed)) = conversation(&[ |
| 184 | + ("GET", "/whole/204"), |
| 185 | + ("GET", "/ok"), |
| 186 | + ("GET", "/whole/304"), |
| 187 | + ("GET", "/ok"), |
| 188 | + ("GET", "/whole/100"), |
| 189 | + ("GET", "/ok"), |
| 190 | + ]) else { |
| 191 | + return; |
| 192 | + }; |
| 193 | + no_body(&replies[0], 204); |
| 194 | + no_body(&replies[2], 304); |
| 195 | + no_body(&replies[4], 100); |
| 196 | + for reply in [&replies[1], &replies[3], &replies[5]] { |
| 197 | + next(reply); |
| 198 | + } |
| 199 | + assert!(printed.is_empty(), "{printed}"); |
| 200 | +} |
| 201 | + |
| 202 | +#[test] |
| 203 | +fn streamed_no_body_statuses_never_call_the_writer_or_send_a_final_chunk() { |
| 204 | + let Some((replies, printed)) = conversation(&[ |
| 205 | + ("GET", "/stream/204"), |
| 206 | + ("GET", "/ok"), |
| 207 | + ("GET", "/stream/304"), |
| 208 | + ("GET", "/ok"), |
| 209 | + ("GET", "/stream/103"), |
| 210 | + ("GET", "/ok"), |
| 211 | + ("HEAD", "/stream/204"), |
| 212 | + ("GET", "/ok"), |
| 213 | + ("HEAD", "/stream/304"), |
| 214 | + ("GET", "/ok"), |
| 215 | + ]) else { |
| 216 | + return; |
| 217 | + }; |
| 218 | + for (index, status) in [(0, 204), (2, 304), (4, 103), (6, 204), (8, 304)] { |
| 219 | + no_body(&replies[index], status); |
| 220 | + } |
| 221 | + for index in [1, 3, 5, 7, 9] { |
| 222 | + next(&replies[index]); |
| 223 | + } |
| 224 | + assert!( |
| 225 | + printed.is_empty(), |
| 226 | + "no-body streams ran their writer: {printed}" |
| 227 | + ); |
| 228 | +} |
| 229 | + |
| 230 | +#[test] |
| 231 | +fn ordinary_and_head_response_framing_is_unchanged() { |
| 232 | + let Some((replies, printed)) = conversation(&[ |
| 233 | + ("GET", "/whole/200"), |
| 234 | + ("GET", "/stream/200"), |
| 235 | + ("HEAD", "/whole/200"), |
| 236 | + ("GET", "/ok"), |
| 237 | + ("HEAD", "/stream/200"), |
| 238 | + ("GET", "/ok"), |
| 239 | + ("HEAD", "/whole/204"), |
| 240 | + ("GET", "/ok"), |
| 241 | + ("HEAD", "/whole/304"), |
| 242 | + ("GET", "/ok"), |
| 243 | + ("GET", "/whole/205"), |
| 244 | + ("GET", "/ok"), |
| 245 | + ]) else { |
| 246 | + return; |
| 247 | + }; |
| 248 | + |
| 249 | + assert_eq!(replies[0].status, 200); |
| 250 | + assert_eq!(header(&replies[0], "Content-Length"), Some("5")); |
| 251 | + assert_eq!(replies[0].body, b"hello"); |
| 252 | + assert_eq!(replies[1].status, 200); |
| 253 | + assert_eq!(header(&replies[1], "Content-Length"), None); |
| 254 | + assert_eq!(header(&replies[1], "Transfer-Encoding"), Some("chunked")); |
| 255 | + assert_eq!(replies[1].body, b"2\r\nhe\r\n3\r\nllo\r\n0\r\n\r\n"); |
| 256 | + |
| 257 | + assert_eq!(replies[2].status, 200); |
| 258 | + assert_eq!(header(&replies[2], "Content-Length"), Some("5")); |
| 259 | + assert!(replies[2].body.is_empty()); |
| 260 | + next(&replies[3]); |
| 261 | + assert_eq!(replies[4].status, 200); |
| 262 | + assert_eq!(header(&replies[4], "Transfer-Encoding"), Some("chunked")); |
| 263 | + assert_eq!(header(&replies[4], "Content-Length"), None); |
| 264 | + assert!(replies[4].body.is_empty()); |
| 265 | + next(&replies[5]); |
| 266 | + assert_eq!(printed, "headless cleanup\n"); |
| 267 | + |
| 268 | + no_body(&replies[6], 204); |
| 269 | + next(&replies[7]); |
| 270 | + no_body(&replies[8], 304); |
| 271 | + next(&replies[9]); |
| 272 | + |
| 273 | + // 205 Reset Content is not one of RFC 9112's always-bodyless statuses. |
| 274 | + assert_eq!(replies[10].status, 205); |
| 275 | + assert_eq!(header(&replies[10], "Content-Length"), Some("5")); |
| 276 | + assert_eq!(replies[10].body, b"hello"); |
| 277 | + next(&replies[11]); |
| 278 | +} |