Skip to main content

rust_web_server/proxy/
mod.rs

1//! Reverse proxy middleware with round-robin load balancing.
2//!
3//! `ReverseProxy` implements [`Middleware`] — wrap any application with it and
4//! all matching requests are forwarded to one of the configured backends over
5//! plain HTTP/1.1.  Failed backends are skipped and the next one is tried
6//! before returning `502 Bad Gateway`.
7//!
8//! # Example
9//!
10//! ```rust,no_run
11//! use rust_web_server::app::App;
12//! use rust_web_server::core::New;
13//! use rust_web_server::proxy::{LoadBalancing, ReverseProxy};
14//!
15//! // Proxy every request across two backends in round-robin order.
16//! let app = App::new()
17//!     .wrap(ReverseProxy::new(["http://backend-1:8080", "http://backend-2:8080"])
18//!         .strategy(LoadBalancing::RoundRobin));
19//!
20//! // Only proxy /api/* requests; everything else is handled locally.
21//! let app2 = App::new()
22//!     .wrap(ReverseProxy::new(["http://api-service:3000"])
23//!         .path_prefix("/api"));
24//! ```
25
26pub mod pool;
27
28#[cfg(test)]
29mod tests;
30
31use std::io::{Read, Write};
32use std::net::{TcpStream, ToSocketAddrs};
33use std::sync::atomic::{AtomicUsize, Ordering};
34use std::sync::Arc;
35use std::time::Duration;
36
37pub use pool::ConnPool;
38
39use crate::application::Application;
40use crate::core::New;
41use crate::middleware::Middleware;
42use crate::mime_type::MimeType;
43use crate::range::Range;
44use crate::request::Request;
45use crate::response::{Response, STATUS_CODE_REASON_PHRASE};
46use crate::server::ConnectionInfo;
47
48// Hop-by-hop headers that must not be forwarded (RFC 7230 §6.1)
49const HOP_BY_HOP: &[&str] = &[
50    "connection",
51    "keep-alive",
52    "proxy-authenticate",
53    "proxy-authorization",
54    "te",
55    "trailers",
56    "transfer-encoding",
57    "upgrade",
58];
59
60/// Load balancing strategy used by [`ReverseProxy`].
61pub enum LoadBalancing {
62    /// Distribute requests across backends in a cyclic order.
63    RoundRobin,
64}
65
66/// Reverse proxy middleware.
67///
68/// Forwards incoming requests to one of the configured backends over HTTP/1.1.
69/// On connection failure the next backend in the list is tried; when all
70/// backends have failed the middleware returns `502 Bad Gateway`.
71///
72/// Hop-by-hop headers are stripped before forwarding.  `X-Forwarded-For` and
73/// `Via` are added to every forwarded request.
74///
75/// Idle connections are pooled and reused across requests (up to
76/// [`ConnPool::new_default`] limits: 8 idle per backend, 60-second timeout).
77/// This eliminates per-request TCP handshake overhead and ephemeral-port
78/// exhaustion.  Use [`ReverseProxy::with_pool`] to share a pool across
79/// multiple proxy instances or to tune pool parameters.
80pub struct ReverseProxy {
81    backends: Vec<Backend>,
82    path_prefix: Option<String>,
83    connect_timeout: Duration,
84    read_timeout: Duration,
85    counter: AtomicUsize,
86    pool: Arc<ConnPool>,
87    breaker: Option<Arc<dyn crate::circuit_breaker::Breaker>>,
88}
89
90impl ReverseProxy {
91    /// Create a proxy that distributes requests across `backends` in
92    /// round-robin order.  Each entry must be `"http://host:port"` or
93    /// `"host:port"` (port defaults to 80).
94    pub fn new<I, S>(backends: I) -> Self
95    where
96        I: IntoIterator<Item = S>,
97        S: AsRef<str>,
98    {
99        Self {
100            backends: backends
101                .into_iter()
102                .filter_map(|u| Backend::parse(u.as_ref()))
103                .collect(),
104            path_prefix: None,
105            connect_timeout: Duration::from_secs(5),
106            read_timeout: Duration::from_secs(30),
107            counter: AtomicUsize::new(0),
108            pool: Arc::new(ConnPool::new_default()),
109            breaker: None,
110        }
111    }
112
113    /// Wire a [`crate::circuit_breaker::CircuitBreaker`] or
114    /// [`crate::circuit_breaker::RedisCircuitBreaker`] into this proxy:
115    /// before dialing a backend, its breaker state is checked (an `Open`
116    /// backend is skipped with no TCP attempt, exactly like a caller
117    /// manually checking `is_available` around each proxied call); after a
118    /// dial, `record_success`/`record_failure` is called automatically.
119    /// No breaker is wired by default — existing `ReverseProxy` callers see
120    /// no behavior change unless they opt in here.
121    ///
122    /// ```rust,no_run
123    /// use std::sync::Arc;
124    /// use rust_web_server::circuit_breaker::global as global_breaker;
125    /// use rust_web_server::proxy::ReverseProxy;
126    ///
127    /// let proxy = ReverseProxy::new(["http://backend-1:8080", "http://backend-2:8080"])
128    ///     .with_circuit_breaker(Arc::new(global_breaker()));
129    /// ```
130    pub fn with_circuit_breaker(mut self, breaker: Arc<dyn crate::circuit_breaker::Breaker>) -> Self {
131        self.breaker = Some(breaker);
132        self
133    }
134
135    /// Only proxy requests whose URI starts with `prefix`.
136    ///
137    /// Other requests are passed through to the next layer in the middleware
138    /// chain (or the inner application).
139    pub fn path_prefix(mut self, prefix: impl Into<String>) -> Self {
140        self.path_prefix = Some(prefix.into());
141        self
142    }
143
144    /// Override the load balancing strategy (currently only `RoundRobin`).
145    pub fn strategy(self, _strategy: LoadBalancing) -> Self {
146        self
147    }
148
149    /// Override the TCP connect timeout (default: 5 000 ms).
150    pub fn connect_timeout_ms(mut self, ms: u64) -> Self {
151        self.connect_timeout = Duration::from_millis(ms);
152        self
153    }
154
155    /// Override the response read timeout (default: 30 000 ms).
156    pub fn read_timeout_ms(mut self, ms: u64) -> Self {
157        self.read_timeout = Duration::from_millis(ms);
158        self
159    }
160
161    /// Attach a shared connection pool.
162    ///
163    /// Useful for sharing one pool across multiple `ReverseProxy` instances
164    /// or for tuning pool parameters (capacity, idle timeout).
165    pub fn with_pool(mut self, pool: Arc<ConnPool>) -> Self {
166        self.pool = pool;
167        self
168    }
169
170    /// Set the maximum number of idle connections per backend (default: 8).
171    pub fn max_idle_conns(mut self, n: usize) -> Self {
172        self.pool = Arc::new(ConnPool::new(n, Duration::from_secs(60)));
173        self
174    }
175
176    fn proxy(&self, request: &Request, connection: &ConnectionInfo) -> Result<Response, String> {
177        if self.backends.is_empty() {
178            return Err("no backends configured".to_string());
179        }
180        let n = self.backends.len();
181        let start = self.counter.fetch_add(1, Ordering::Relaxed);
182        let mut last_err = "all backends failed".to_string();
183        for attempt in 0..n {
184            let idx = (start + attempt) % n;
185            let backend = &self.backends[idx];
186            let key = format!("{}:{}", backend.host, backend.port);
187
188            if let Some(breaker) = &self.breaker {
189                if !breaker.is_available(&key) {
190                    last_err = format!("circuit open for {}", key);
191                    continue;
192                }
193            }
194
195            match self.try_backend(request, connection, backend) {
196                Ok(resp) => {
197                    if let Some(breaker) = &self.breaker {
198                        breaker.record_success(&key);
199                    }
200                    return Ok(resp);
201                }
202                Err(e) => {
203                    if let Some(breaker) = &self.breaker {
204                        breaker.record_failure(&key);
205                    }
206                    last_err = e;
207                }
208            }
209        }
210        Err(last_err)
211    }
212
213    fn try_backend(
214        &self,
215        request: &Request,
216        connection: &ConnectionInfo,
217        backend: &Backend,
218    ) -> Result<Response, String> {
219        let key = format!("{}:{}", backend.host, backend.port);
220
221        // Try a pooled connection first; fall back to a fresh one.
222        let stream = if let Some(pooled) = self.pool.acquire(&key) {
223            pooled
224        } else {
225            let addr_str = key.as_str();
226            let sock_addr = addr_str
227                .to_socket_addrs()
228                .map_err(|e| format!("DNS lookup for {} failed: {}", addr_str, e))?
229                .next()
230                .ok_or_else(|| format!("no address resolved for {}", addr_str))?;
231            TcpStream::connect_timeout(&sock_addr, self.connect_timeout)
232                .map_err(|e| format!("connect to {} failed: {}", addr_str, e))?
233        };
234
235        stream.set_read_timeout(Some(self.read_timeout)).map_err(|e| e.to_string())?;
236        stream.set_write_timeout(Some(Duration::from_secs(10))).map_err(|e| e.to_string())?;
237
238        // keep_alive = true: send Connection: keep-alive so the server holds
239        // the connection open after responding.
240        let req_bytes = build_request(request, &backend.host, &connection.client.ip, true);
241        let mut stream = stream;
242        stream.write_all(&req_bytes).map_err(|e| format!("write to backend failed: {}", e))?;
243
244        let mut tmp = [0u8; 4096];
245        let (header_bytes, body_prefix) = read_headers_only(&mut stream, &mut tmp)?;
246        let header_lower =
247            std::str::from_utf8(&header_bytes).unwrap_or("").to_ascii_lowercase();
248
249        if should_stream_response(&header_lower) {
250            // Streaming path — pipe bytes straight to the client.
251            // The connection cannot be reused while the body is in flight.
252            let mut resp = parse_status_and_headers(&header_bytes)?;
253            resp.stream_pipe =
254                Some(Box::new(ConcatReader::new(body_prefix, stream)));
255            Ok(resp)
256        } else {
257            // Buffered path — read the full body, then optionally return the
258            // connection to the pool.
259            let (resp_bytes, reusable) =
260                read_response_from_partial(&mut stream, header_bytes, body_prefix, &mut tmp)?;
261            if reusable {
262                self.pool.release(&key, stream);
263            }
264            Response::parse(&resp_bytes)
265        }
266    }
267}
268
269impl Middleware for ReverseProxy {
270    fn handle(
271        &self,
272        request: &Request,
273        connection: &ConnectionInfo,
274        next: &dyn Application,
275    ) -> Result<Response, String> {
276        if let Some(prefix) = &self.path_prefix {
277            if !request.request_uri.starts_with(prefix.as_str()) {
278                return next.execute(request, connection);
279            }
280        }
281        match self.proxy(request, connection) {
282            Ok(resp) => Ok(resp),
283            Err(_) => Ok(bad_gateway()),
284        }
285    }
286}
287
288// ── helpers ───────────────────────────────────────────────────────────────────
289
290pub(crate) fn build_request(
291    request: &Request,
292    backend_host: &str,
293    client_ip: &str,
294    keep_alive: bool,
295) -> Vec<u8> {
296    let mut out: Vec<u8> = Vec::new();
297    let _ = write!(
298        out,
299        "{} {} HTTP/1.1\r\nHost: {}\r\n",
300        request.method, request.request_uri, backend_host
301    );
302    for h in &request.headers {
303        let lower = h.name.to_lowercase();
304        if HOP_BY_HOP.contains(&lower.as_str()) || lower == "host" {
305            continue;
306        }
307        let _ = write!(out, "{}: {}\r\n", h.name, h.value);
308    }
309    let _ = write!(out, "X-Forwarded-For: {}\r\n", client_ip);
310    let _ = write!(out, "Via: 1.1 rws\r\n");
311    if keep_alive {
312        let _ = write!(out, "Connection: keep-alive\r\n");
313    } else {
314        let _ = write!(out, "Connection: close\r\n");
315    }
316    if !request.body.is_empty() {
317        let _ = write!(out, "Content-Length: {}\r\n", request.body.len());
318    }
319    let _ = write!(out, "\r\n");
320    out.extend_from_slice(&request.body);
321    out
322}
323
324/// Decode HTTP/1.1 chunked transfer-encoding from `stream`.
325///
326/// `buf[header_end..]` may already contain some body bytes that arrived in
327/// the same read as the headers.  Returns the fully decoded body.
328fn decode_chunked(
329    stream: &mut TcpStream,
330    buf: &[u8],
331    header_end: usize,
332    tmp: &mut [u8],
333) -> Result<Vec<u8>, String> {
334    // Seed `raw` with any body bytes already buffered alongside the headers.
335    let mut raw: Vec<u8> = buf[header_end..].to_vec();
336    let mut decoded: Vec<u8> = Vec::new();
337
338    loop {
339        // Wait until we have at least one complete chunk-size line (ends with \r\n).
340        let crlf = loop {
341            if let Some(p) = raw.windows(2).position(|w| w == b"\r\n") {
342                break p;
343            }
344            let n = stream.read(tmp).map_err(|e| e.to_string())?;
345            if n == 0 {
346                return Err("chunked: premature EOF reading chunk size".to_string());
347            }
348            raw.extend_from_slice(&tmp[..n]);
349        };
350
351        // Chunk size is hex, optionally followed by chunk-extensions (";…").
352        let size_line = std::str::from_utf8(&raw[..crlf])
353            .map_err(|_| "chunked: non-UTF-8 chunk size line".to_string())?;
354        let size_str = size_line.split(';').next().unwrap_or("").trim();
355        let chunk_size = usize::from_str_radix(size_str, 16)
356            .map_err(|_| format!("chunked: invalid chunk size '{}'", size_str))?;
357        raw.drain(..crlf + 2); // consume "<size>\r\n"
358
359        if chunk_size == 0 {
360            // Last chunk — consume the trailing CRLF ("0\r\n\r\n" → trailing "\r\n" still pending).
361            while raw.len() < 2 {
362                let n = stream.read(tmp).map_err(|e| e.to_string())?;
363                if n == 0 {
364                    break;
365                }
366                raw.extend_from_slice(&tmp[..n]);
367            }
368            break;
369        }
370
371        // Read chunk data + trailing CRLF.
372        while raw.len() < chunk_size + 2 {
373            let n = stream.read(tmp).map_err(|e| e.to_string())?;
374            if n == 0 {
375                return Err("chunked: premature EOF reading chunk body".to_string());
376            }
377            raw.extend_from_slice(&tmp[..n]);
378        }
379        decoded.extend_from_slice(&raw[..chunk_size]);
380        raw.drain(..chunk_size + 2); // consume "<data>\r\n"
381    }
382
383    Ok(decoded)
384}
385
386/// Rewrite `buf` in-place: strip `Transfer-Encoding`, add `Content-Length`,
387/// replace the old (undecoded) body with `decoded`.
388fn rewrite_as_content_length(buf: &mut Vec<u8>, header_end: usize, decoded: &[u8]) {
389    let header_str = std::str::from_utf8(&buf[..header_end]).unwrap_or("").to_string();
390    buf.clear();
391    for line in header_str.lines() {
392        if line.to_ascii_lowercase().starts_with("transfer-encoding:") || line.is_empty() {
393            continue;
394        }
395        buf.extend_from_slice(line.as_bytes());
396        buf.extend_from_slice(b"\r\n");
397    }
398    let _ = write!(buf, "Content-Length: {}\r\n\r\n", decoded.len());
399    buf.extend_from_slice(decoded);
400}
401
402/// Non-pooled version of the response reader, used by callers that send
403/// `Connection: close` (e.g. `proxy_http1`, `proxy_https1`).
404pub(crate) fn read_response(stream: &mut TcpStream) -> Result<Vec<u8>, String> {
405    read_response_from(stream)
406}
407
408pub(crate) fn read_response_from<R: Read>(stream: &mut R) -> Result<Vec<u8>, String> {
409    let mut buf: Vec<u8> = Vec::with_capacity(8192);
410    let mut tmp = [0u8; 4096];
411
412    let header_end = loop {
413        let n = stream.read(&mut tmp).map_err(|e| e.to_string())?;
414        if n == 0 {
415            return if buf.is_empty() {
416                Err("backend closed connection without sending a response".to_string())
417            } else {
418                Ok(buf)
419            };
420        }
421        buf.extend_from_slice(&tmp[..n]);
422        if let Some(pos) = buf.windows(4).position(|w| w == b"\r\n\r\n") {
423            break pos + 4;
424        }
425    };
426
427    let content_length = std::str::from_utf8(&buf[..header_end])
428        .unwrap_or("")
429        .lines()
430        .find_map(|line| {
431            line.to_lowercase()
432                .starts_with("content-length:")
433                .then(|| line.splitn(2, ':').nth(1)?.trim().parse::<usize>().ok())
434                .flatten()
435        });
436
437    match content_length {
438        Some(len) => {
439            while buf.len() < header_end + len {
440                let n = stream.read(&mut tmp).map_err(|e| e.to_string())?;
441                if n == 0 {
442                    break;
443                }
444                buf.extend_from_slice(&tmp[..n]);
445            }
446        }
447        None => loop {
448            let n = stream.read(&mut tmp).map_err(|e| e.to_string())?;
449            if n == 0 {
450                break;
451            }
452            buf.extend_from_slice(&tmp[..n]);
453        },
454    }
455
456    Ok(buf)
457}
458
459/// Forward a single HTTP/1.1 request to `host:port` and return the response.
460///
461/// This is the shared low-level building block used by [`crate::canary`] and
462/// [`crate::ingress`] so they don't have to duplicate the TCP + request/response
463/// marshalling code.
464pub(crate) fn proxy_http1(
465    request: &Request,
466    client_ip: &str,
467    host: &str,
468    port: u16,
469    connect_timeout: Duration,
470    read_timeout: Duration,
471) -> Result<Response, String> {
472    use std::net::ToSocketAddrs;
473    let addr_str = format!("{}:{}", host, port);
474    let sock_addr = addr_str
475        .to_socket_addrs()
476        .map_err(|e| format!("DNS lookup for {} failed: {}", addr_str, e))?
477        .next()
478        .ok_or_else(|| format!("no address resolved for {}", addr_str))?;
479    let stream = TcpStream::connect_timeout(&sock_addr, connect_timeout)
480        .map_err(|e| format!("connect to {} failed: {}", addr_str, e))?;
481    stream.set_read_timeout(Some(read_timeout)).map_err(|e| e.to_string())?;
482    stream.set_write_timeout(Some(Duration::from_secs(10))).map_err(|e| e.to_string())?;
483    let req_bytes = build_request(request, host, client_ip, false);
484    let mut stream = stream;
485    stream.write_all(&req_bytes).map_err(|e| format!("write to backend failed: {}", e))?;
486    let resp_bytes = read_response(&mut stream)?;
487    Response::parse(&resp_bytes)
488}
489
490/// Forward a single HTTPS/1.1 request to `host:port` over TLS and return the
491/// response. Requires the `http-client` or `http2` feature (both bring in
492/// `rustls` + `webpki-roots`).
493#[cfg(any(feature = "http-client", feature = "http2"))]
494pub(crate) fn proxy_https1(
495    request: &Request,
496    client_ip: &str,
497    host: &str,
498    port: u16,
499    connect_timeout: Duration,
500    read_timeout: Duration,
501) -> Result<Response, String> {
502    use rustls::pki_types::ServerName;
503    use rustls::ClientConfig;
504    use std::net::ToSocketAddrs;
505    use std::sync::Arc;
506
507    let addr_str = format!("{}:{}", host, port);
508    let sock_addr = addr_str
509        .to_socket_addrs()
510        .map_err(|e| format!("DNS lookup for {} failed: {}", addr_str, e))?
511        .next()
512        .ok_or_else(|| format!("no address resolved for {}", addr_str))?;
513
514    let stream = TcpStream::connect_timeout(&sock_addr, connect_timeout)
515        .map_err(|e| format!("connect to {} failed: {}", addr_str, e))?;
516    stream.set_read_timeout(Some(read_timeout)).map_err(|e| e.to_string())?;
517    stream.set_write_timeout(Some(Duration::from_secs(10))).map_err(|e| e.to_string())?;
518
519    let root_store =
520        rustls::RootCertStore::from_iter(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
521    let config = Arc::new(
522        ClientConfig::builder()
523            .with_root_certificates(root_store)
524            .with_no_client_auth(),
525    );
526    let server_name = ServerName::try_from(host.to_string())
527        .map_err(|e| format!("invalid upstream hostname '{}': {}", host, e))?;
528    let conn = rustls::ClientConnection::new(config, server_name).map_err(|e| e.to_string())?;
529    let mut tls = rustls::StreamOwned::new(conn, stream);
530
531    let req_bytes = build_request(request, host, client_ip, false);
532    tls.write_all(&req_bytes)
533        .map_err(|e| format!("write to upstream failed: {}", e))?;
534
535    let resp_bytes = read_response_from(&mut tls)?;
536    Response::parse(&resp_bytes)
537}
538
539fn bad_gateway() -> Response {
540    let cr = Range::get_content_range(
541        b"502 Bad Gateway".to_vec(),
542        MimeType::TEXT_PLAIN.to_string(),
543    );
544    let mut r = Response::new();
545    r.status_code = *STATUS_CODE_REASON_PHRASE.n502_bad_gateway.status_code;
546    r.reason_phrase = STATUS_CODE_REASON_PHRASE
547        .n502_bad_gateway
548        .reason_phrase
549        .to_string();
550    r.content_range_list = vec![cr];
551    r
552}
553
554// ── Streaming proxy helpers ───────────────────────────────────────────────────
555
556/// Responses larger than this threshold are streamed instead of buffered.
557const STREAM_THRESHOLD: usize = 1024 * 1024; // 1 MB
558
559/// A `Read` implementation that drains a prefix buffer before reading from the
560/// inner stream. Used to replay body bytes that arrived with the HTTP headers.
561pub(crate) struct ConcatReader<R: Read + Send> {
562    prefix: Vec<u8>,
563    prefix_pos: usize,
564    inner: R,
565}
566
567impl<R: Read + Send> ConcatReader<R> {
568    fn new(prefix: Vec<u8>, inner: R) -> Self {
569        ConcatReader { prefix, prefix_pos: 0, inner }
570    }
571}
572
573impl<R: Read + Send> Read for ConcatReader<R> {
574    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
575        if self.prefix_pos < self.prefix.len() {
576            let avail = &self.prefix[self.prefix_pos..];
577            let n = buf.len().min(avail.len());
578            buf[..n].copy_from_slice(&avail[..n]);
579            self.prefix_pos += n;
580            return Ok(n);
581        }
582        self.inner.read(buf)
583    }
584}
585
586/// Read exactly the HTTP response headers (up to and including `\r\n\r\n`).
587///
588/// Returns `(header_bytes, body_prefix)` where `body_prefix` contains any
589/// body bytes that arrived in the same TCP segment as the headers.
590fn read_headers_only(stream: &mut TcpStream, tmp: &mut [u8]) -> Result<(Vec<u8>, Vec<u8>), String> {
591    let mut buf: Vec<u8> = Vec::with_capacity(4096);
592    loop {
593        let n = stream.read(tmp).map_err(|e| e.to_string())?;
594        if n == 0 {
595            return Err("backend closed connection before headers were complete".to_string());
596        }
597        buf.extend_from_slice(&tmp[..n]);
598        if let Some(pos) = buf.windows(4).position(|w| w == b"\r\n\r\n") {
599            let body_prefix = buf[pos + 4..].to_vec();
600            buf.truncate(pos + 4);
601            return Ok((buf, body_prefix));
602        }
603    }
604}
605
606/// Returns `true` when the response should be streamed rather than buffered.
607///
608/// Streams when any of the following hold:
609/// - `Content-Type: text/event-stream` — SSE
610/// - `Transfer-Encoding: chunked` — AI token streams, etc.
611/// - `Content-Length` exceeds 1 MB — large file downloads
612pub(crate) fn should_stream_response(header_lower: &str) -> bool {
613    let is_sse = header_lower.lines().any(|l| {
614        l.starts_with("content-type:") && l.contains("text/event-stream")
615    });
616    let is_chunked = header_lower.lines().any(|l| {
617        l.starts_with("transfer-encoding:") && l.contains("chunked")
618    });
619    let content_length: Option<usize> = header_lower.lines().find_map(|l| {
620        l.strip_prefix("content-length:")?.trim().parse().ok()
621    });
622    let is_large = content_length.map_or(false, |n| n > STREAM_THRESHOLD);
623    is_sse || is_chunked || is_large
624}
625
626/// Parse the status line and headers from raw header bytes (ending at `\r\n\r\n`).
627fn parse_status_and_headers(header_bytes: &[u8]) -> Result<Response, String> {
628    let s = std::str::from_utf8(header_bytes)
629        .map_err(|e| format!("non-UTF-8 response headers: {}", e))?;
630    let mut lines = s.lines();
631    let status_line = lines.next().ok_or("empty backend response")?;
632    let mut parts = status_line.splitn(3, ' ');
633    let http_version = parts.next().unwrap_or("HTTP/1.1").to_string();
634    let status_code: i16 = parts
635        .next()
636        .unwrap_or("502")
637        .parse()
638        .map_err(|_| format!("invalid status code in '{}'", status_line))?;
639    let reason_phrase = parts.next().unwrap_or("").trim_end_matches('\r').to_string();
640    let mut headers = Vec::new();
641    for line in lines {
642        let line = line.trim_end_matches('\r');
643        if line.is_empty() { break; }
644        if let Some(colon) = line.find(':') {
645            headers.push(crate::header::Header {
646                name: line[..colon].trim().to_string(),
647                value: line[colon + 1..].trim().to_string(),
648            });
649        }
650    }
651    Ok(Response {
652        http_version,
653        status_code,
654        reason_phrase,
655        headers,
656        content_range_list: vec![],
657        stream_file: None,
658        stream_pipe: None,
659    })
660}
661
662/// Read the remaining body after headers have already been read.
663///
664/// `header_bytes` ends with `\r\n\r\n`; `body_prefix` holds any body bytes
665/// that arrived in the same TCP read.  Handles all three body mechanisms
666/// (chunked, content-length, read-to-EOF).  Returns `(full_response_bytes, can_reuse)`.
667fn read_response_from_partial(
668    stream: &mut TcpStream,
669    header_bytes: Vec<u8>,
670    body_prefix: Vec<u8>,
671    tmp: &mut [u8],
672) -> Result<(Vec<u8>, bool), String> {
673    let header_end = header_bytes.len();
674    let mut buf = header_bytes;
675    buf.extend_from_slice(&body_prefix);
676
677    let header_lower =
678        std::str::from_utf8(&buf[..header_end]).unwrap_or("").to_ascii_lowercase();
679    let connection_close =
680        header_lower.lines().any(|l| l.starts_with("connection:") && l.contains("close"));
681    let is_chunked = header_lower
682        .lines()
683        .any(|l| l.starts_with("transfer-encoding:") && l.contains("chunked"));
684    let content_length: Option<usize> = header_lower.lines().find_map(|l| {
685        l.strip_prefix("content-length:")?.trim().parse().ok()
686    });
687
688    if is_chunked {
689        let decoded = decode_chunked(stream, &buf, header_end, tmp)?;
690        rewrite_as_content_length(&mut buf, header_end, &decoded);
691        Ok((buf, !connection_close))
692    } else if let Some(len) = content_length {
693        while buf.len() < header_end + len {
694            let n = stream.read(tmp).map_err(|e| e.to_string())?;
695            if n == 0 { break; }
696            buf.extend_from_slice(&tmp[..n]);
697        }
698        Ok((buf, !connection_close))
699    } else {
700        loop {
701            let n = stream.read(tmp).map_err(|e| e.to_string())?;
702            if n == 0 { break; }
703            buf.extend_from_slice(&tmp[..n]);
704        }
705        Ok((buf, false))
706    }
707}
708
709// ── Backend URL parsing ───────────────────────────────────────────────────────
710
711struct Backend {
712    host: String,
713    port: u16,
714    /// Whether the upstream connection should use TLS.
715    /// Set when the URL scheme is `https://`, `h2s://`, or `grpcs://`.
716    #[cfg_attr(not(feature = "http2"), allow(dead_code))]
717    tls: bool,
718}
719
720impl Backend {
721    fn parse(url: &str) -> Option<Self> {
722        let (rest, tls, default_port) = if let Some(r) = url.strip_prefix("https://") {
723            (r, true, 443u16)
724        } else if let Some(r) = url.strip_prefix("h2s://") {
725            (r, true, 443u16)
726        } else if let Some(r) = url.strip_prefix("grpcs://") {
727            (r, true, 443u16)
728        } else if let Some(r) = url.strip_prefix("http://") {
729            (r, false, 80u16)
730        } else if let Some(r) = url.strip_prefix("h2://") {
731            (r, false, 80u16)
732        } else if let Some(r) = url.strip_prefix("grpc://") {
733            (r, false, 80u16)
734        } else {
735            (url, false, 80u16)
736        };
737        // Drop any path component.
738        let host_port = rest.split('/').next().unwrap_or(rest);
739        let (host, port) = if let Some(colon) = host_port.rfind(':') {
740            let port_str = &host_port[colon + 1..];
741            if let Ok(p) = port_str.parse::<u16>() {
742                (host_port[..colon].to_string(), p)
743            } else {
744                (host_port.to_string(), default_port)
745            }
746        } else {
747            (host_port.to_string(), default_port)
748        };
749        if host.is_empty() {
750            return None;
751        }
752        Some(Backend { host, port, tls })
753    }
754}
755
756// ── HTTP/2 reverse proxy ──────────────────────────────────────────────────────
757
758/// Reverse proxy that forwards requests to HTTP/2 backends.
759///
760/// Wraps [`ReverseProxy`] and forces HTTP/2 (`h2`) for all upstream connections.
761/// Requires the `http2` Cargo feature.
762///
763/// This proxy also transparently handles gRPC traffic
764/// (`Content-Type: application/grpc*`) — gRPC DATA frames are forwarded
765/// as-is because gRPC is layered directly on HTTP/2.
766#[cfg(feature = "http2")]
767pub struct H2ReverseProxy {
768    inner: ReverseProxy,
769}
770
771#[cfg(feature = "http2")]
772impl H2ReverseProxy {
773    /// Create a proxy distributing requests across `backends` in round-robin order.
774    ///
775    /// Each backend entry can be:
776    /// - `"host:port"` — plain TCP (HTTP/2 cleartext)
777    /// - `"h2://host:port"` — plain TCP (explicit scheme)
778    /// - `"h2s://host:port"` — TLS (HTTP/2 over HTTPS; port defaults to 443)
779    /// - `"https://host:port"` — TLS (same as `h2s://`)
780    ///
781    /// TLS backends require the `http2` Cargo feature (includes `rustls` +
782    /// `webpki-roots`).  Certificate verification uses the WebPKI trust store.
783    pub fn new<I, S>(backends: I) -> Self
784    where
785        I: IntoIterator<Item = S>,
786        S: AsRef<str>,
787    {
788        H2ReverseProxy {
789            inner: ReverseProxy::new(backends),
790        }
791    }
792
793    /// Only proxy requests whose URI starts with `prefix`; pass others through.
794    pub fn path_prefix(mut self, prefix: impl Into<String>) -> Self {
795        self.inner = self.inner.path_prefix(prefix);
796        self
797    }
798
799    /// Override the TCP connect timeout (default: 5 s).
800    pub fn connect_timeout_ms(mut self, ms: u64) -> Self {
801        self.inner = self.inner.connect_timeout_ms(ms);
802        self
803    }
804
805    /// Override the response read timeout (default: 30 s).
806    pub fn read_timeout_ms(mut self, ms: u64) -> Self {
807        self.inner = self.inner.read_timeout_ms(ms);
808        self
809    }
810}
811
812#[cfg(feature = "http2")]
813impl crate::middleware::Middleware for H2ReverseProxy {
814    fn handle(
815        &self,
816        request: &crate::request::Request,
817        connection: &crate::server::ConnectionInfo,
818        next: &dyn crate::application::Application,
819    ) -> Result<crate::response::Response, String> {
820        if let Some(prefix) = &self.inner.path_prefix {
821            if !request.request_uri.starts_with(prefix.as_str()) {
822                return next.execute(request, connection);
823            }
824        }
825        if self.inner.backends.is_empty() {
826            return Ok(bad_gateway());
827        }
828        let n = self.inner.backends.len();
829        let start = self.inner.counter.fetch_add(1, Ordering::Relaxed);
830        for attempt in 0..n {
831            let idx = (start + attempt) % n;
832            match try_backend_h2(request, &connection.client.ip, &self.inner.backends[idx],
833                                  self.inner.connect_timeout, self.inner.read_timeout) {
834                Ok(resp) => return Ok(resp),
835                Err(_) if attempt + 1 < n => continue,
836                Err(_) => break,
837            }
838        }
839        Ok(bad_gateway())
840    }
841}
842
843#[cfg(feature = "http2")]
844fn try_backend_h2(
845    request: &Request,
846    client_ip: &str,
847    backend: &Backend,
848    connect_timeout: Duration,
849    _read_timeout: Duration,
850) -> Result<Response, String> {
851    // `block_on_isolated` works on any tokio runtime flavor (including
852    // `current_thread`, unlike `tokio::task::block_in_place`, which requires
853    // `multi_thread` and panics otherwise) and doesn't pull a worker thread
854    // out of a shared pool; it also works with no runtime at all (HTTP/1.1
855    // thread pool), unlike the previous implementation, which just gave up.
856    crate::async_bridge::block_on_isolated(|| forward_h2_async(request, client_ip, backend, connect_timeout))
857}
858
859#[cfg(feature = "http2")]
860async fn forward_h2_async(
861    request: &Request,
862    client_ip: &str,
863    backend: &Backend,
864    connect_timeout: Duration,
865) -> Result<Response, String> {
866    let addr = format!("{}:{}", backend.host, backend.port);
867    let tcp = tokio::time::timeout(
868        connect_timeout,
869        tokio::net::TcpStream::connect(&addr),
870    )
871    .await
872    .map_err(|_| format!("h2 proxy: connect to {} timed out", addr))?
873    .map_err(|e| format!("h2 proxy: connect to {} failed: {}", addr, e))?;
874
875    if backend.tls {
876        use rustls::pki_types::ServerName;
877        use rustls::ClientConfig;
878        use std::sync::Arc;
879        use tokio_rustls::TlsConnector;
880
881        let root_store = rustls::RootCertStore::from_iter(
882            webpki_roots::TLS_SERVER_ROOTS.iter().cloned(),
883        );
884        let mut config = ClientConfig::builder()
885            .with_root_certificates(root_store)
886            .with_no_client_auth();
887        // Advertise h2 via ALPN so the server selects HTTP/2.
888        config.alpn_protocols = vec![b"h2".to_vec()];
889        let connector = TlsConnector::from(Arc::new(config));
890        let server_name = ServerName::try_from(backend.host.as_str())
891            .map_err(|e| format!("invalid upstream hostname '{}': {}", backend.host, e))?
892            .to_owned();
893        let tls_stream = connector
894            .connect(server_name, tcp)
895            .await
896            .map_err(|e| format!("h2 proxy: TLS handshake with {} failed: {}", addr, e))?;
897        send_h2_request(request, client_ip, backend, tls_stream).await
898    } else {
899        send_h2_request(request, client_ip, backend, tcp).await
900    }
901}
902
903/// Drive the h2 client handshake + request/response over any async I/O stream.
904///
905/// Accepts both plain `TcpStream` and `TlsStream<TcpStream>` — anything that
906/// satisfies `AsyncRead + AsyncWrite + Unpin + Send + 'static`.
907#[cfg(feature = "http2")]
908async fn send_h2_request<T>(
909    request: &Request,
910    client_ip: &str,
911    backend: &Backend,
912    stream: T,
913) -> Result<Response, String>
914where
915    T: tokio::io::AsyncRead + tokio::io::AsyncWrite + Unpin + Send + 'static,
916{
917    use bytes::Bytes;
918    use http as hc;
919
920    let addr = format!("{}:{}", backend.host, backend.port);
921
922    let (send_req, conn) = h2::client::handshake(stream)
923        .await
924        .map_err(|e| format!("h2 proxy: handshake with {} failed: {}", addr, e))?;
925
926    tokio::spawn(async move {
927        let _ = conn.await;
928    });
929
930    let scheme = if backend.tls { "https" } else { "http" };
931    let uri_str = format!("{}://{}{}", scheme, addr, request.request_uri);
932    let uri: hc::Uri = uri_str.parse().map_err(|e: hc::uri::InvalidUri| e.to_string())?;
933    let method = hc::Method::from_bytes(request.method.as_bytes()).map_err(|e| e.to_string())?;
934
935    let mut builder = hc::Request::builder().method(method).uri(uri);
936    builder = builder.header("host", &backend.host);
937    for h in &request.headers {
938        let lower = h.name.to_lowercase();
939        if HOP_BY_HOP.contains(&lower.as_str()) || lower == "host" {
940            continue;
941        }
942        builder = builder.header(&h.name, &h.value);
943    }
944    builder = builder.header("x-forwarded-for", client_ip);
945    builder = builder.header("via", "2 rws");
946
947    let body_bytes = Bytes::from(request.body.clone());
948    let end_of_stream = body_bytes.is_empty();
949    let http_req = builder.body(()).map_err(|e| e.to_string())?;
950
951    let mut send_req = send_req.ready().await.map_err(|e| e.to_string())?;
952    let (resp_future, mut req_body) = send_req
953        .send_request(http_req, end_of_stream)
954        .map_err(|e| e.to_string())?;
955    if !end_of_stream {
956        req_body.send_data(body_bytes, true).map_err(|e| e.to_string())?;
957    }
958
959    let resp = resp_future.await.map_err(|e| e.to_string())?;
960    let (parts, mut body) = resp.into_parts();
961
962    let content_type = parts
963        .headers
964        .get("content-type")
965        .and_then(|v| v.to_str().ok())
966        .unwrap_or("application/octet-stream")
967        .to_string();
968
969    let mut body_bytes: Vec<u8> = Vec::new();
970    while let Some(chunk) = body.data().await {
971        body_bytes.extend_from_slice(&chunk.map_err(|e| e.to_string())?);
972    }
973
974    let mut response = Response::new();
975    response.status_code = parts.status.as_u16() as i16;
976    response.reason_phrase = parts.status.canonical_reason().unwrap_or("").to_string();
977
978    const H2_HOP: &[&str] = &[
979        "connection", "keep-alive", "transfer-encoding", "upgrade", "proxy-connection", "te",
980    ];
981    for (name, value) in &parts.headers {
982        let lower = name.as_str().to_lowercase();
983        if H2_HOP.contains(&lower.as_str()) {
984            continue;
985        }
986        if let Ok(v) = value.to_str() {
987            response.headers.push(crate::header::Header {
988                name: name.as_str().to_string(),
989                value: v.to_string(),
990            });
991        }
992    }
993
994    if !body_bytes.is_empty() {
995        response.content_range_list = vec![Range::get_content_range(body_bytes, content_type)];
996    }
997
998    Ok(response)
999}
1000
1001// ── gRPC proxy ────────────────────────────────────────────────────────────────
1002
1003/// gRPC reverse proxy middleware.
1004///
1005/// Recognises requests with `Content-Type: application/grpc*` and forwards them
1006/// to a backend over HTTP/2, leaving all other requests to the next layer.
1007///
1008/// Requires the `http2` Cargo feature.
1009///
1010/// # Example
1011///
1012/// ```rust,no_run
1013/// use rust_web_server::app::App;
1014/// use rust_web_server::core::New;
1015/// use rust_web_server::proxy::GrpcProxy;
1016///
1017/// let app = App::new()
1018///     .wrap(GrpcProxy::new(["grpc-service:50051"]));
1019/// ```
1020#[cfg(feature = "http2")]
1021pub struct GrpcProxy {
1022    inner: H2ReverseProxy,
1023}
1024
1025#[cfg(feature = "http2")]
1026impl GrpcProxy {
1027    /// Create a proxy distributing gRPC connections across `backends` in round-robin order.
1028    ///
1029    /// Each backend entry can be:
1030    /// - `"host:port"` — plain TCP (gRPC cleartext)
1031    /// - `"grpc://host:port"` — plain TCP (explicit scheme)
1032    /// - `"grpcs://host:port"` — TLS (gRPC over TLS; port defaults to 443)
1033    /// - `"https://host:port"` — TLS (same as `grpcs://`)
1034    ///
1035    /// TLS backends require the `http2` Cargo feature.
1036    pub fn new<I, S>(backends: I) -> Self
1037    where
1038        I: IntoIterator<Item = S>,
1039        S: AsRef<str>,
1040    {
1041        GrpcProxy { inner: H2ReverseProxy::new(backends) }
1042    }
1043
1044    /// Only proxy requests whose URI starts with `prefix`; pass others through.
1045    pub fn path_prefix(mut self, prefix: impl Into<String>) -> Self {
1046        self.inner = self.inner.path_prefix(prefix);
1047        self
1048    }
1049}
1050
1051#[cfg(feature = "http2")]
1052impl crate::middleware::Middleware for GrpcProxy {
1053    fn handle(
1054        &self,
1055        request: &crate::request::Request,
1056        connection: &crate::server::ConnectionInfo,
1057        next: &dyn crate::application::Application,
1058    ) -> Result<crate::response::Response, String> {
1059        let ct = request
1060            .get_header("content-type".to_string())
1061            .map(|h| h.value.as_str())
1062            .unwrap_or("");
1063        if ct.starts_with("application/grpc") {
1064            self.inner.handle(request, connection, next)
1065        } else {
1066            next.execute(request, connection)
1067        }
1068    }
1069}
1070
1071// ── Backend::parse unit tests ─────────────────────────────────────────────────
1072
1073#[cfg(test)]
1074mod backend_parse_tests {
1075    use super::Backend;
1076
1077    fn parse(url: &str) -> Option<(String, u16, bool)> {
1078        Backend::parse(url).map(|b| (b.host, b.port, b.tls))
1079    }
1080
1081    #[test]
1082    fn bare_host_port() {
1083        assert_eq!(Some(("api.example.com".into(), 8080, false)), parse("api.example.com:8080"));
1084    }
1085
1086    #[test]
1087    fn http_scheme() {
1088        assert_eq!(Some(("backend".into(), 3000, false)), parse("http://backend:3000"));
1089    }
1090
1091    #[test]
1092    fn h2_scheme_plain() {
1093        assert_eq!(Some(("svc".into(), 50051, false)), parse("h2://svc:50051"));
1094    }
1095
1096    #[test]
1097    fn grpc_scheme_plain() {
1098        assert_eq!(Some(("svc".into(), 50051, false)), parse("grpc://svc:50051"));
1099    }
1100
1101    #[test]
1102    fn https_scheme_sets_tls_and_default_port() {
1103        assert_eq!(Some(("api.example.com".into(), 443, true)), parse("https://api.example.com"));
1104    }
1105
1106    #[test]
1107    fn https_scheme_explicit_port() {
1108        assert_eq!(Some(("api.example.com".into(), 8443, true)), parse("https://api.example.com:8443"));
1109    }
1110
1111    #[test]
1112    fn h2s_scheme_sets_tls() {
1113        assert_eq!(Some(("svc".into(), 443, true)), parse("h2s://svc"));
1114    }
1115
1116    #[test]
1117    fn h2s_scheme_explicit_port() {
1118        assert_eq!(Some(("svc".into(), 8443, true)), parse("h2s://svc:8443"));
1119    }
1120
1121    #[test]
1122    fn grpcs_scheme_sets_tls() {
1123        assert_eq!(Some(("grpc-svc".into(), 443, true)), parse("grpcs://grpc-svc"));
1124    }
1125
1126    #[test]
1127    fn grpcs_scheme_explicit_port() {
1128        assert_eq!(Some(("grpc-svc".into(), 50052, true)), parse("grpcs://grpc-svc:50052"));
1129    }
1130
1131    #[test]
1132    fn empty_host_returns_none() {
1133        assert_eq!(None, parse("https://"));
1134    }
1135
1136    #[test]
1137    fn bare_host_no_port_defaults_to_80() {
1138        assert_eq!(Some(("myhost".into(), 80, false)), parse("myhost"));
1139    }
1140}