oxedyne/fe2o3/fe2o3_net/src/http/relay.rs
26.5 KiB, 204 runs
created by r1870400018:74700, 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 | //! Streams one HTTP/1.1 message body from a reader to a writer by its framing, for a proxy that |
| 2 | //! must pass on bodies of any size (model files run to gigabytes) without holding them. |
| 3 | //! |
| 4 | //! Unlike `msg::read_chunked`, which decodes a chunked body into memory with the same |
| 5 | //! `chunk_size` check, the relay never decodes: chunk-size lines, extensions and trailers go out |
| 6 | //! exactly as they came in, and every line is checked strictly first, since a proxy and the |
| 7 | //! server behind it that disagree on where a body ends is how requests are smuggled. |
| 8 | |
| 9 | use crate::constant; |
| 10 | use crate::http::grammar; |
| 11 | use crate::http::grammar::is_ctl; |
| 12 | use crate::http::grammar::is_tchar; |
| 13 | use crate::tls; |
| 14 | |
| 15 | use oxedyne_fe2o3_core::prelude::*; |
| 16 | |
| 17 | use std::future::Future; |
| 18 | use std::sync::atomic::AtomicU16; |
| 19 | use std::sync::atomic::AtomicU64; |
| 20 | use std::sync::atomic::Ordering; |
| 21 | use std::sync::Arc; |
| 22 | use std::time::Duration; |
| 23 | |
| 24 | use tokio::io::AsyncRead; |
| 25 | use tokio::io::AsyncReadExt; |
| 26 | use tokio::io::AsyncWrite; |
| 27 | use tokio::io::AsyncWriteExt; |
| 28 | use tokio::net::TcpStream; |
| 29 | use tokio_rustls::rustls::ClientConfig; |
| 30 | |
| 31 | |
| 32 | pub const RELAY_BUF_SIZE: usize = 64 * 1_024; // One read. |
| 33 | pub const RELAY_CHUNK_HEX_MAX: usize = 16; // Digits in a chunk size, 64 bits. |
| 34 | pub const RELAY_TRAILER_MAX: usize = 64 * 1_024; // The whole trailer section. |
| 35 | |
| 36 | /// How a message body is delimited (RFC 9112 §6.3). |
| 37 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 38 | pub enum Framing { |
| 39 | Length(u64), // `Content-Length`. |
| 40 | Chunked, // `Transfer-Encoding: chunked`. |
| 41 | Close, // A reply that runs to the end of the connection. |
| 42 | None, // No body. |
| 43 | } |
| 44 | |
| 45 | /// Which message the header fields belong to, since a reply's framing depends on its status and |
| 46 | /// on the method of the request it answers. |
| 47 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 48 | pub enum Subject<'a> { |
| 49 | Request, |
| 50 | Reply { |
| 51 | status: u16, |
| 52 | method: &'a str, // The request's. |
| 53 | }, |
| 54 | } |
| 55 | |
| 56 | /// The framing the header fields declare, refusing every ambiguous form rather than guessing: |
| 57 | /// `Content-Length` together with `Transfer-Encoding`, a second `Content-Length` (even one |
| 58 | /// agreeing with the first), a length that is not plain decimal digits, and any |
| 59 | /// `Transfer-Encoding` other than exactly `chunked`. |
| 60 | /// |
| 61 | /// The fields are the raw lines as received, because `HeaderFields` keeps one `Content-Length` |
| 62 | /// and drops it in favour of a `Transfer-Encoding`, which hides precisely the conflicts this |
| 63 | /// must see. A reply to `HEAD`, a `1xx`, `204` or `304`, and a `2xx` to `CONNECT` have no body |
| 64 | /// whatever the fields say, though the fields are still checked. |
| 65 | pub fn framing<I, N, V>(subject: Subject<'_>, fields: I) -> Outcome<Framing> |
| 66 | where |
| 67 | I: IntoIterator<Item = (N, V)>, |
| 68 | N: AsRef<str>, |
| 69 | V: AsRef<str>, |
| 70 | { |
| 71 | let ows = |c: char| c == ' ' || c == '\t'; |
| 72 | let mut len: Option<u64> = None; |
| 73 | let mut codings: Vec<String> = Vec::new(); |
| 74 | for (n, v) in fields { |
| 75 | let (n, v) = (n.as_ref(), v.as_ref().trim_matches(ows)); |
| 76 | if n.eq_ignore_ascii_case("content-length") { |
| 77 | if len.is_some() { |
| 78 | return Err(err!( |
| 79 | "A message carries more than one Content-Length."; Invalid, Input)); |
| 80 | } |
| 81 | if v.is_empty() || v.len() > 19 || !v.bytes().all(|b| b.is_ascii_digit()) { |
| 82 | return Err(err!("A Content-Length is not a decimal length."; Invalid, Input)); |
| 83 | } |
| 84 | len = Some(res!(v.parse::<u64>(), Invalid, Input)); |
| 85 | } else if n.eq_ignore_ascii_case("transfer-encoding") { |
| 86 | for c in v.split(',') { |
| 87 | codings.push(c.trim_matches(ows).to_ascii_lowercase()); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | let chunked = match codings.as_slice() { |
| 92 | [] => false, |
| 93 | [c] if c == "chunked" => true, |
| 94 | _ => return Err(err!( |
| 95 | "A Transfer-Encoding is not exactly 'chunked'."; Invalid, Input)), |
| 96 | }; |
| 97 | if chunked && len.is_some() { |
| 98 | return Err(err!( |
| 99 | "A message carries both Content-Length and Transfer-Encoding."; Invalid, Input)); |
| 100 | } |
| 101 | if let Subject::Reply { status, method } = subject { |
| 102 | let bodiless = (100..200).contains(&status) |
| 103 | || status == 204 |
| 104 | || status == 304 |
| 105 | || method.eq_ignore_ascii_case("HEAD") |
| 106 | || (method.eq_ignore_ascii_case("CONNECT") && (200..300).contains(&status)); |
| 107 | if bodiless { |
| 108 | return Ok(Framing::None); |
| 109 | } |
| 110 | } |
| 111 | Ok(match (chunked, len, subject) { |
| 112 | (true, _, _) => Framing::Chunked, |
| 113 | (false, Some(n), _) => Framing::Length(n), |
| 114 | (false, None, Subject::Request) => Framing::None, |
| 115 | (false, None, Subject::Reply{..}) => Framing::Close, |
| 116 | }) |
| 117 | } |
| 118 | |
| 119 | /// The policy of one `relay`. The default bounds nothing and lets trailers through. |
| 120 | #[derive(Clone, Copy, Debug)] |
| 121 | pub struct RelayLimits { |
| 122 | pub bound: Option<u64>, // Bytes the body may put on the writer, framing included. |
| 123 | pub idle: Option<Duration>, // For any one read or write. |
| 124 | pub trailers: bool, // May a chunked body's trailer section carry fields? |
| 125 | } |
| 126 | |
| 127 | impl Default for RelayLimits { |
| 128 | fn default() -> Self { |
| 129 | Self { bound: None, idle: None, trailers: true } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /// Copies exactly one message body from `r` to `w`, as framed, and returns the bytes read past |
| 134 | /// its end: the start of the next message on the connection. |
| 135 | /// |
| 136 | /// A chunked body goes out verbatim, chunk-size lines with their extensions and the trailer |
| 137 | /// section included, each line checked first: a size of 1 to 16 hex digits, an extension only |
| 138 | /// after a `;`, no control characters, and CRLF line ends with no bare CR or LF. Memory stays at |
| 139 | /// about one read buffer whatever the body's size. |
| 140 | /// |
| 141 | /// `count` has every byte written added to it, framing included, as it is written, and `rest` |
| 142 | /// holds the bytes already read from `r` after the head, which begin the body. A `Length` over |
| 143 | /// `lim.bound` is refused before anything is written, and any other body errs when it would pass |
| 144 | /// it. A hop that strips fields from the head sets `lim.trailers` false, or what it strips could |
| 145 | /// come in behind the body. |
| 146 | pub async fn relay<R, W>( |
| 147 | r: &mut R, |
| 148 | w: &mut W, |
| 149 | framing: Framing, |
| 150 | count: &AtomicU64, |
| 151 | rest: Vec<u8>, |
| 152 | lim: RelayLimits, |
| 153 | ) |
| 154 | -> Outcome<Vec<u8>> |
| 155 | where |
| 156 | R: AsyncRead + Unpin, |
| 157 | W: AsyncWrite + Unpin, |
| 158 | { |
| 159 | let mut p = Pipe { r, w, buf: rest, pos: 0, count, sent: 0, lim }; |
| 160 | match framing { |
| 161 | Framing::None => (), |
| 162 | Framing::Length(n) => { |
| 163 | if let Some(b) = lim.bound { |
| 164 | if n > b { |
| 165 | return Err(err!( |
| 166 | "A body of {} bytes exceeds the relay bound of {}.", n, b; |
| 167 | IO, Network, Input, TooBig)); |
| 168 | } |
| 169 | } |
| 170 | res!(p.copy(n).await); |
| 171 | }, |
| 172 | Framing::Close => { |
| 173 | loop { |
| 174 | if p.avail().is_empty() && !res!(p.fill().await) { |
| 175 | break; |
| 176 | } |
| 177 | let n = p.avail().len(); |
| 178 | res!(p.send(n).await); |
| 179 | } |
| 180 | }, |
| 181 | Framing::Chunked => res!(p.chunked().await), |
| 182 | } |
| 183 | if let Err(e) = p.w.flush().await { |
| 184 | return Err(err!(e, "Flushing a relayed HTTP body."; IO, Network, Wire, Write)); |
| 185 | } |
| 186 | Ok(p.leftover()) |
| 187 | } |
| 188 | |
| 189 | /// The strict reading of a chunk-size line without its CRLF (RFC 9112 §7.1): 1 to 16 hex digits, |
| 190 | /// then nothing, or optional blanks and a `;` opening the extensions, with no control character |
| 191 | /// but a tab anywhere. |
| 192 | pub fn chunk_size(line: &[u8]) -> Outcome<u64> { |
| 193 | let hex = line.iter().position(|b| !b.is_ascii_hexdigit()).unwrap_or(line.len()); |
| 194 | if hex == 0 || hex > RELAY_CHUNK_HEX_MAX { |
| 195 | return Err(err!( |
| 196 | "A chunk-size line has {} hex digits where 1 to {} begin it.", hex, RELAY_CHUNK_HEX_MAX; |
| 197 | IO, Network, Wire, Invalid)); |
| 198 | } |
| 199 | let ext = &line[hex..]; |
| 200 | if !ext.is_empty() { |
| 201 | let semi = ext.iter().position(|&b| b != b' ' && b != b'\t'); |
| 202 | let opens = match semi { |
| 203 | Some(i) => ext[i] == b';', |
| 204 | None => false, |
| 205 | }; |
| 206 | if !opens || ext.iter().any(|&b| is_ctl(b)) { |
| 207 | return Err(err!( |
| 208 | "A chunk-size line has something other than an extension from byte {}.", hex; |
| 209 | IO, Network, Wire, Invalid)); |
| 210 | } |
| 211 | } |
| 212 | let digits = String::from_utf8_lossy(&line[..hex]); |
| 213 | Ok(res!(u64::from_str_radix(&digits, 16), IO, Network, Wire, Invalid)) |
| 214 | } |
| 215 | |
| 216 | struct Pipe<'a, R, W> { |
| 217 | r: &'a mut R, |
| 218 | w: &'a mut W, |
| 219 | buf: Vec<u8>, // Read and not yet written, from `pos`. |
| 220 | pos: usize, |
| 221 | count: &'a AtomicU64, |
| 222 | sent: u64, |
| 223 | lim: RelayLimits, |
| 224 | } |
| 225 | |
| 226 | impl<'a, R, W> Pipe<'a, R, W> |
| 227 | where |
| 228 | R: AsyncRead + Unpin, |
| 229 | W: AsyncWrite + Unpin, |
| 230 | { |
| 231 | fn avail(&self) -> &[u8] { |
| 232 | &self.buf[self.pos..] |
| 233 | } |
| 234 | |
| 235 | fn leftover(mut self) -> Vec<u8> { |
| 236 | self.buf.drain(..self.pos); |
| 237 | self.buf |
| 238 | } |
| 239 | |
| 240 | /// Reads once more onto the buffer, after dropping what has been written. `false` means the |
| 241 | /// peer closed. |
| 242 | async fn fill(&mut self) -> Outcome<bool> { |
| 243 | if self.pos > 0 { |
| 244 | self.buf.drain(..self.pos); |
| 245 | self.pos = 0; |
| 246 | } |
| 247 | let len = self.buf.len(); |
| 248 | self.buf.resize(len + RELAY_BUF_SIZE, 0); |
| 249 | let got = within(self.lim.idle, self.r.read(&mut self.buf[len..])).await; |
| 250 | self.buf.truncate(len + *got.as_ref().unwrap_or(&0)); |
| 251 | match got { |
| 252 | Ok(n) => Ok(n > 0), |
| 253 | Err(e) => Err(err!(e, "Reading an HTTP body to relay."; Read)), |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /// Writes the next `n` buffered bytes. |
| 258 | async fn send(&mut self, n: usize) -> Outcome<()> { |
| 259 | let n64 = n as u64; |
| 260 | if let Some(b) = self.lim.bound { |
| 261 | if self.sent.saturating_add(n64) > b { |
| 262 | return Err(err!( |
| 263 | "A relayed body passed the bound of {} bytes.", b; |
| 264 | IO, Network, Input, TooBig)); |
| 265 | } |
| 266 | } |
| 267 | if let Err(e) = within(self.lim.idle, self.w.write_all(&self.buf[self.pos..self.pos + n])).await { |
| 268 | return Err(err!(e, "Writing a relayed HTTP body."; Write)); |
| 269 | } |
| 270 | self.pos += n; |
| 271 | self.sent += n64; |
| 272 | self.count.fetch_add(n64, Ordering::Relaxed); |
| 273 | Ok(()) |
| 274 | } |
| 275 | |
| 276 | /// Relays exactly `n` bytes. |
| 277 | async fn copy(&mut self, mut n: u64) -> Outcome<()> { |
| 278 | while n > 0 { |
| 279 | if self.avail().is_empty() && !res!(self.fill().await) { |
| 280 | return Err(err!( |
| 281 | "The peer closed with {} bytes of the body still to come.", n; |
| 282 | IO, Network, Wire, Read, Missing)); |
| 283 | } |
| 284 | let k = std::cmp::min(self.avail().len() as u64, n) as usize; |
| 285 | res!(self.send(k).await); |
| 286 | n -= k as u64; |
| 287 | } |
| 288 | Ok(()) |
| 289 | } |
| 290 | |
| 291 | /// The length, CRLF included, of the next line in the buffer, reading until it is whole. A |
| 292 | /// bare LF or CR, or a line longer than `max`, is an error. |
| 293 | async fn line(&mut self, max: usize, what: &str) -> Outcome<usize> { |
| 294 | let mut scanned = 0; |
| 295 | loop { |
| 296 | let a = self.avail(); |
| 297 | if let Some(i) = a[scanned..].iter().position(|&b| b == b'\n') { |
| 298 | let i = scanned + i; |
| 299 | if i == 0 || a[i - 1] != b'\r' || a[..i - 1].contains(&b'\r') { |
| 300 | return Err(err!( |
| 301 | "A {} line in a chunked body has a bare CR or LF.", what; |
| 302 | IO, Network, Wire, Invalid)); |
| 303 | } |
| 304 | return Ok(i + 1); |
| 305 | } |
| 306 | if a.len() > max { |
| 307 | return Err(err!( |
| 308 | "A {} line in a chunked body ran past {} bytes with no CRLF.", what, max; |
| 309 | IO, Network, Input, TooBig)); |
| 310 | } |
| 311 | scanned = a.len(); |
| 312 | if !res!(self.fill().await) { |
| 313 | return Err(err!( |
| 314 | "The peer closed in the middle of a {} line of a chunked body.", what; |
| 315 | IO, Network, Wire, Read, Missing)); |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | async fn chunked(&mut self) -> Outcome<()> { |
| 321 | loop { |
| 322 | let n = res!(self.line(constant::HTTP_CHUNK_LINE_MAX, "chunk-size").await); |
| 323 | let size = res!(chunk_size(&self.avail()[..n - 2])); |
| 324 | res!(self.send(n).await); |
| 325 | if size == 0 { |
| 326 | break; |
| 327 | } |
| 328 | res!(self.copy(size).await); |
| 329 | while self.avail().len() < 2 { |
| 330 | if !res!(self.fill().await) { |
| 331 | return Err(err!( |
| 332 | "The peer closed before the CRLF that ends a chunk."; |
| 333 | IO, Network, Wire, Read, Missing)); |
| 334 | } |
| 335 | } |
| 336 | if &self.avail()[..2] != b"\r\n" { |
| 337 | return Err(err!( |
| 338 | "A chunk of {} bytes is not followed by CRLF.", size; |
| 339 | IO, Network, Wire, Invalid)); |
| 340 | } |
| 341 | res!(self.send(2).await); |
| 342 | } |
| 343 | // The trailer section, to its empty line. |
| 344 | let mut total = 0; |
| 345 | loop { |
| 346 | let n = res!(self.line(constant::HTTP_CHUNK_LINE_MAX, "trailer").await); |
| 347 | total += n; |
| 348 | if total > RELAY_TRAILER_MAX { |
| 349 | return Err(err!( |
| 350 | "A chunked body's trailers ran past {} bytes.", RELAY_TRAILER_MAX; |
| 351 | IO, Network, Input, TooBig)); |
| 352 | } |
| 353 | if n > 2 { |
| 354 | let field = &self.avail()[..n - 2]; |
| 355 | let name = field.iter().position(|&b| b == b':').unwrap_or(0); |
| 356 | if name == 0 |
| 357 | || !self.lim.trailers |
| 358 | || !field[..name].iter().all(|&b| is_tchar(b)) |
| 359 | || field.iter().any(|&b| is_ctl(b)) |
| 360 | { |
| 361 | return Err(err!( |
| 362 | "A trailer line is not a header field, or none is allowed."; |
| 363 | IO, Network, Wire, Invalid)); |
| 364 | } |
| 365 | } |
| 366 | res!(self.send(n).await); |
| 367 | if n == 2 { |
| 368 | return Ok(()); |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | /// `f`, failing as `Timeout` when it takes longer than `wait`. The caller adds `Read` or `Write`. |
| 375 | async fn within<T>(wait: Option<Duration>, f: impl Future<Output = std::io::Result<T>>) |
| 376 | -> Outcome<T> |
| 377 | { |
| 378 | let r = match wait { |
| 379 | None => f.await, |
| 380 | Some(w) => match tokio::time::timeout(w, f).await { |
| 381 | Ok(r) => r, |
| 382 | Err(_) => return Err(err!("An HTTP read or write waited past {:?}.", w; |
| 383 | IO, Network, Timeout)), |
| 384 | }, |
| 385 | }; |
| 386 | Ok(res!(r, IO, Network, Wire)) |
| 387 | } |
| 388 | |
| 389 | /// The caller's bounds on one forwarded request. |
| 390 | #[derive(Clone, Copy, Debug)] |
| 391 | pub struct ForwardBounds { |
| 392 | pub head_cap: usize, // Bytes in a request or reply head. |
| 393 | pub head_wait: Duration, // For the whole request head. |
| 394 | pub connect: Duration, // To dial, and again for the TLS handshake. |
| 395 | pub answer_wait: Duration, // For each whole reply head, from the request's end. |
| 396 | pub idle: Duration, // For each read or write of a body, and each head written. |
| 397 | pub body_cap: u64, // Bytes in a request body, a chunked one's framing included. |
| 398 | } |
| 399 | |
| 400 | /// What a forward moved, counted as it moves, so a forward that failed or was dropped still has |
| 401 | /// its journal line. A `status` of 0 means no reply head came. |
| 402 | #[derive(Debug, Default)] |
| 403 | pub struct ForwardTally { |
| 404 | pub sent: AtomicU64, // To the upstream, head included. |
| 405 | pub got: AtomicU64, // To the client, heads included. |
| 406 | pub status: AtomicU16, // The last reply head's. |
| 407 | } |
| 408 | |
| 409 | /// One request head, read strictly: an origin-form HTTP/1.1 request line, `tchar` field names, |
| 410 | /// CRLF line ends, no folding and no control byte but a tab. No `Debug`, since its values |
| 411 | /// may be secrets. |
| 412 | pub struct RequestHead { |
| 413 | head: String, // CRLF CRLF included. |
| 414 | rest: Vec<u8>, // Read past the head: the start of the body. |
| 415 | } |
| 416 | |
| 417 | impl RequestHead { |
| 418 | pub async fn read<R: AsyncRead + Unpin>(r: &mut R, b: &ForwardBounds) -> Outcome<Self> { |
| 419 | let wait = tokio::time::sleep(b.head_wait); |
| 420 | let (head, rest) = res!(read_head(r, Vec::new(), b.head_cap, wait).await); |
| 421 | let mut part = start(&head).split(' '); |
| 422 | let ok = match (part.next(), part.next(), part.next(), part.next()) { |
| 423 | (Some(m), Some(t), Some(v), None) => !m.is_empty() && m.bytes().all(is_tchar) |
| 424 | && t.starts_with('/') && t.bytes().all(|c| c.is_ascii_graphic()) |
| 425 | && v == "HTTP/1.1", |
| 426 | _ => false, |
| 427 | }; |
| 428 | if !ok { |
| 429 | return Err(err!("A request line is not an origin-form HTTP/1.1 request."; |
| 430 | IO, Network, Wire, Invalid)); |
| 431 | } |
| 432 | // fixed: TRACE and TRACK echo the request, an added key included, back to the client, |
| 433 | // and CONNECT opens a tunnel. Case is ignored: no real method differs from these by case |
| 434 | // alone, and an upstream lenient about case would honour them. |
| 435 | if ["TRACE", "TRACK", "CONNECT"].iter().any(|m| method(&head).eq_ignore_ascii_case(m)) { |
| 436 | return Err(err!("A request's method is one that is never forwarded."; |
| 437 | IO, Network, Wire, Invalid)); |
| 438 | } |
| 439 | Ok(Self { head, rest }) |
| 440 | } |
| 441 | |
| 442 | pub fn method(&self) -> &str { method(&self.head) } |
| 443 | pub fn target(&self) -> &str { start(&self.head).split(' ').nth(1).unwrap_or("") } |
| 444 | |
| 445 | /// The value of the one `Host` field, refusing a head with none, more than one, or one that |
| 446 | /// is empty or holds a blank. The keeper needs no other field, so no other is handed out. |
| 447 | pub fn host(&self) -> Outcome<&str> { |
| 448 | let mut h = fields(&self.head).filter(|(n, _)| n.eq_ignore_ascii_case("host")).map(|(_, v)| v); |
| 449 | match (h.next(), h.next()) { |
| 450 | (Some(v), None) if !v.is_empty() && v.bytes().all(|c| c.is_ascii_graphic()) => Ok(v), |
| 451 | _ => Err(err!("A request head has no Host, more than one, or one not an authority."; |
| 452 | IO, Network, Wire, Invalid)), |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | fn start(head: &str) -> &str { |
| 458 | head.split("\r\n").next().unwrap_or("") |
| 459 | } |
| 460 | |
| 461 | fn method(head: &str) -> &str { |
| 462 | start(head).split(' ').next().unwrap_or("") |
| 463 | } |
| 464 | |
| 465 | /// Each field as (name, value), the value without its surrounding blanks. |
| 466 | fn fields(head: &str) -> impl Iterator<Item = (&str, &str)> { |
| 467 | head.split("\r\n").skip(1).filter_map(|l| l.split_once(':')) |
| 468 | .map(|(n, v)| (n, v.trim_matches(|c| c == ' ' || c == '\t'))) |
| 469 | } |
| 470 | |
| 471 | /// Reads one head through its blank line, of at most `cap` bytes, before `wait` ends, after `buf` |
| 472 | /// already read, and checks every line after the first as a field. Returns it with the bytes |
| 473 | /// read past it. |
| 474 | async fn read_head<R: AsyncRead + Unpin>( |
| 475 | r: &mut R, |
| 476 | mut buf: Vec<u8>, |
| 477 | cap: usize, |
| 478 | wait: impl Future<Output = ()>, |
| 479 | ) |
| 480 | -> Outcome<(String, Vec<u8>)> |
| 481 | { |
| 482 | let mut n = buf.len(); // Bytes held; the rest of `buf` is room, zeroed once. |
| 483 | let read = async { |
| 484 | let mut from = 0; // Searched up to here, less the three bytes a split CRLF CRLF needs. |
| 485 | loop { |
| 486 | match buf[from..n].windows(4).position(|w| w == b"\r\n\r\n") { |
| 487 | Some(i) if from + i + 4 <= cap => return Ok(from + i + 4), |
| 488 | None if n < cap => (), |
| 489 | _ => return Err(err!( |
| 490 | "An HTTP head ran past {} bytes.", cap; IO, Network, Input, TooBig)), |
| 491 | } |
| 492 | if buf.len() < cap { |
| 493 | buf.resize(cap, 0); |
| 494 | } |
| 495 | from = n.saturating_sub(3); |
| 496 | match r.read(&mut buf[n..cap]).await { |
| 497 | Ok(0) => return Err(err!("The peer closed before an HTTP head ended."; |
| 498 | IO, Network, Wire, Read, Missing)), |
| 499 | Ok(k) => n += k, |
| 500 | Err(e) => return Err(err!(e, "Reading an HTTP head."; IO, Network, Wire, Read)), |
| 501 | } |
| 502 | } |
| 503 | }; |
| 504 | let end = tokio::select! { |
| 505 | r = read => res!(r), |
| 506 | _ = wait => return Err(err!("An HTTP head did not end within its wait."; |
| 507 | IO, Network, Read, Timeout)), |
| 508 | }; |
| 509 | buf.truncate(n); |
| 510 | let rest = buf.split_off(end); |
| 511 | // The error is dropped, not kept as a source: it owns the head, keys and all. |
| 512 | let head = match String::from_utf8(buf) { |
| 513 | Ok(h) => h, |
| 514 | Err(_) => return Err(err!("An HTTP head is not UTF-8."; IO, Network, Wire, Invalid)), |
| 515 | }; |
| 516 | res!(grammar::check_head(&head[..end - 4])); |
| 517 | Ok((head, rest)) |
| 518 | } |
| 519 | |
| 520 | /// Serves one request whose head the caller has read, against an upstream the caller names, over |
| 521 | /// TLS: the caller's `Host` and `Connection` give way to the upstream's and `close`, fields named |
| 522 | /// in `strip` (case aside; a trailing `*` matches a prefix) or, exactly, in `add` are dropped, and |
| 523 | /// `add` goes in. The body goes as framed. An ambiguous framing, a declared length over `body_cap` and an |
| 524 | /// added field that is not one are refused before any dial; a chunked body is cut where it passes |
| 525 | /// `body_cap`, by which time the upstream holds the head, `add` with it, and part of the body. The |
| 526 | /// reply streams back until it ends. Bytes the client sends after the request's body may be read, |
| 527 | /// but are dropped, never forwarded. The upstream is closed when this returns or is dropped. |
| 528 | /// |
| 529 | /// Only each wait is bounded, not the whole: `connect` the dial and again the handshake, `idle` |
| 530 | /// each read or write of a body and each head written, and `answer_wait` each reply head, counted |
| 531 | /// from the request's end (or from the upstream ceasing to take it), so every `1xx` head starts |
| 532 | /// it again. The caller bounds the forward with `tokio::time::timeout`. Dropping the future |
| 533 | /// closes only the upstream: the client's stream, which may hold part of an answer, is the |
| 534 | /// caller's to close. |
| 535 | pub async fn forward_one<C: AsyncRead + AsyncWrite + Unpin>( |
| 536 | client: &mut C, |
| 537 | req: RequestHead, |
| 538 | strip: &[&str], |
| 539 | add: &[(&str, &str)], |
| 540 | host: &str, |
| 541 | port: u16, |
| 542 | tls: Arc<ClientConfig>, |
| 543 | b: &ForwardBounds, |
| 544 | tally: &ForwardTally, |
| 545 | ) |
| 546 | -> Outcome<()> |
| 547 | { |
| 548 | let RequestHead { head, rest } = req; |
| 549 | let body = res!(framing(Subject::Request, fields(&head))); |
| 550 | if let Framing::Length(n) = body { |
| 551 | if n > b.body_cap { |
| 552 | return Err(err!("A request body is longer than the cap of {} bytes.", b.body_cap; |
| 553 | IO, Network, Input, TooBig)); |
| 554 | } |
| 555 | } |
| 556 | // A `*` in an added name would read as a strip pattern, so it is refused there. |
| 557 | for (n, v) in add { |
| 558 | if n.is_empty() || !n.bytes().all(|c| is_tchar(c) && c != b'*') || v.bytes().any(is_ctl) { |
| 559 | return Err(err!("The added field {:?} is not a field.", n; Invalid, Input)); |
| 560 | } |
| 561 | } |
| 562 | let named = |n: &str, p: &str| match p.strip_suffix('*') { |
| 563 | Some(p) => n.len() >= p.len() && n[..p.len()].eq_ignore_ascii_case(p), |
| 564 | None => n.eq_ignore_ascii_case(p), |
| 565 | }; |
| 566 | let auth = if host.contains(':') { fmt!("[{}]", host) } else { host.to_string() }; |
| 567 | let auth = if port == 443 { auth } else { fmt!("{}:{}", auth, port) }; // fixed: https's port |
| 568 | let mut out = fmt!("{}\r\nHost: {}\r\n", start(&head), auth); |
| 569 | // fixed: only the two fields replaced above. Not `fwd::is_managed_header`, which also names |
| 570 | // Content-Length and Transfer-Encoding, and those frame the body and must go up. |
| 571 | for (n, v) in fields(&head) { |
| 572 | if !["host", "connection"].iter().chain(strip).any(|p| named(n, p)) |
| 573 | && !add.iter().any(|(a, _)| n.eq_ignore_ascii_case(a)) |
| 574 | { |
| 575 | out.push_str(&fmt!("{}: {}\r\n", n, v)); |
| 576 | } |
| 577 | } |
| 578 | for (n, v) in add { |
| 579 | out.push_str(&fmt!("{}: {}\r\n", n, v)); |
| 580 | } |
| 581 | out.push_str("Connection: close\r\n\r\n"); |
| 582 | let tcp = match tokio::time::timeout(b.connect, TcpStream::connect((host, port))).await { |
| 583 | Ok(r) => res!(r, IO, Network, Init), |
| 584 | Err(_) => return Err(err!("Dialling {}:{} took longer than {:?}.", host, port, b.connect; |
| 585 | IO, Network, Init, Timeout)), |
| 586 | }; |
| 587 | let up = res!(tls::upgrade(tcp, host, tls, b.connect).await); |
| 588 | let ((mut cr, mut cw), (mut ur, mut uw)) = (tokio::io::split(client), tokio::io::split(up)); |
| 589 | // Until the request has gone up, the wait for an answer is the request's own. |
| 590 | let (sent, end) = tokio::sync::watch::channel(false); |
| 591 | let send = async { |
| 592 | res!(within(Some(b.idle), uw.write_all(out.as_bytes())).await, Write); |
| 593 | tally.sent.fetch_add(out.len() as u64, Ordering::Relaxed); |
| 594 | let lim = RelayLimits { bound: Some(b.body_cap), idle: Some(b.idle), trailers: false }; |
| 595 | res!(relay(&mut cr, &mut uw, body, &tally.sent, rest, lim).await); |
| 596 | Ok(()) |
| 597 | }; |
| 598 | let answer = async { |
| 599 | let mut more = Vec::new(); |
| 600 | loop { |
| 601 | let mut end = end.clone(); |
| 602 | let wait = async move { |
| 603 | let _ = end.wait_for(|done| *done).await; |
| 604 | tokio::time::sleep(b.answer_wait).await; |
| 605 | }; |
| 606 | let (h, m) = res!(read_head(&mut ur, more, b.head_cap, wait).await); |
| 607 | // Three digits, then the blank before the optional reason, or the line's end. |
| 608 | let code = h.strip_prefix("HTTP/1.1 ") |
| 609 | .and_then(|s| match (s.get(..3), s.as_bytes().get(3)) { |
| 610 | (Some(d), Some(b' ' | b'\r')) if d.bytes().all(|c| c.is_ascii_digit()) |
| 611 | => d.parse::<u16>().ok(), |
| 612 | _ => None, |
| 613 | }) |
| 614 | .filter(|c| (100..=599).contains(c)); |
| 615 | let code = res!(code.ok_or_else(|| err!( |
| 616 | "A reply's status line is not HTTP/1.1 with a code from 100 to 599."; |
| 617 | IO, Network, Wire, Invalid))); |
| 618 | tally.status.store(code, Ordering::Relaxed); |
| 619 | let f = res!(framing(Subject::Reply { status: code, method: method(&head) }, fields(&h))); |
| 620 | res!(within(Some(b.idle), cw.write_all(h.as_bytes())).await, Write); |
| 621 | tally.got.fetch_add(h.len() as u64, Ordering::Relaxed); |
| 622 | if code >= 200 || code == 101 { |
| 623 | let lim = RelayLimits { idle: Some(b.idle), ..RelayLimits::default() }; |
| 624 | return relay(&mut ur, &mut cw, f, &tally.got, m, lim).await.map(|_| ()); |
| 625 | } |
| 626 | more = m; |
| 627 | } |
| 628 | }; |
| 629 | tokio::pin!(send, answer); |
| 630 | tokio::select! { |
| 631 | r = &mut send => { |
| 632 | sent.send_replace(true); |
| 633 | match r { |
| 634 | // The upstream stopped taking the body: its answer may say why. |
| 635 | Err(e) if !e.tags().contains(&ErrTag::Write) => Err(e), |
| 636 | _ => answer.await, |
| 637 | } |
| 638 | }, |
| 639 | r = &mut answer => r, |
| 640 | } |
| 641 | } |