rust-web-server 18.10.0

Single-binary web server and reverse proxy: automatic HTTPS (Let's Encrypt), HTTP/1.1, HTTP/2 and HTTP/3, load balancing, auth and rate limiting, configured in one TOML file. Also a Rust library: routing, middleware, MCP server, ORM, background jobs. No hyper, no Tower; HTTP/2 and HTTP/3 framing via h2 and quinn.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! HTTP/2 upstreams: [`H2ReverseProxy`](super::H2ReverseProxy),
//! [`GrpcProxy`](super::GrpcProxy) and the config file's `grpc` action.
//!
//! Connections are pooled (one multiplexed connection per backend and trust
//! setting) and live on one process-wide runtime, `rws-h2-upstream`: tokio
//! I/O belongs to the runtime that made it, and the handler calling
//! [`forward`] may run under the shared runtime, a per-core one or none at
//! all (`TestClient`), so none of those can own them.
//!
//! Bodies stream both ways. The request body is sent as the handler's
//! [`Body`](crate::request::Body) yields it — a stream when the server
//! received it as one, so gRPC client streaming and bidirectional calls work —
//! and the response head is returned as soon as it arrives, with the body in
//! `stream_pipe` and the upstream's trailers (`grpc-status`, `grpc-message`)
//! in [`Response::trailers`], filled when the body ends. Small non-gRPC
//! responses with a known length are buffered instead, as the HTTP/1.1
//! forwarder does.

use std::collections::HashMap;
use std::io::Read;
use std::sync::{Arc, Mutex, OnceLock};
use std::time::Duration;

use bytes::Bytes;

use super::Backend;
use crate::header::Header;
use crate::range::Range;
use crate::request::{Body, Request};
use crate::core::New;
use crate::response::{Response, Trailers};

/// Responses up to this size with a `content-length` (and not gRPC) are
/// buffered; larger ones, and every gRPC response, stream.
const BUFFER_LIMIT: u64 = 1 << 20;

/// Upstream body chunks in flight between the connection and the writer.
const BODY_CHANNEL: usize = 4;

/// Request headers that describe a single connection and aren't forwarded
/// (`te: trailers` is, separately — gRPC servers require it).
const NOT_FORWARDED: &[&str] = &[
    "connection", "keep-alive", "proxy-authenticate", "proxy-authorization", "proxy-connection",
    "te", "trailer", "transfer-encoding", "upgrade", "host",
];

/// Response headers HTTP/2 forbids or rws regenerates.
const NOT_RETURNED: &[&str] = &["connection", "keep-alive", "transfer-encoding", "upgrade", "proxy-connection", "te"];

/// Where to send a request: a backend and, for TLS, the client configuration
/// (its ALPN must offer `h2`).
pub(crate) struct Target<'a> {
    pub(super) backend: &'a Backend,
    pub(super) tls: Option<&'a Arc<rustls::ClientConfig>>,
    pub(super) connect_timeout: Duration,
    pub(super) read_timeout: Duration,
    /// `:authority` to send instead of the backend's address (the Gateway
    /// API router keeps the client's `Host`).
    pub(super) authority: Option<&'a str>,
}

fn runtime() -> &'static tokio::runtime::Runtime {
    static RUNTIME: OnceLock<tokio::runtime::Runtime> = OnceLock::new();
    RUNTIME.get_or_init(|| {
        tokio::runtime::Builder::new_multi_thread()
            .worker_threads(2)
            .thread_name("rws-h2-upstream")
            .enable_all()
            .build()
            .expect("the HTTP/2 upstream runtime")
    })
}

/// Pooled connections by key, each with an id so a closing connection
/// removes itself and not a newer one under the same key.
type Pool = Mutex<HashMap<String, (u64, h2::client::SendRequest<Bytes>)>>;

static NEXT_CONNECTION: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);

fn pool() -> &'static Pool {
    static POOL: OnceLock<Pool> = OnceLock::new();
    POOL.get_or_init(Default::default)
}

/// The client configuration rws uses for `h2s://` / `grpcs://` backends
/// without a `tls_ca_file`: the WebPKI roots, ALPN `h2`.
pub(crate) fn default_tls() -> &'static Arc<rustls::ClientConfig> {
    static CONFIG: OnceLock<Arc<rustls::ClientConfig>> = OnceLock::new();
    CONFIG.get_or_init(|| {
        let roots = rustls::RootCertStore::from_iter(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
        Arc::new(with_h2_alpn(crate::tls_client_builder().with_root_certificates(roots).with_no_client_auth()))
    })
}

/// `config` offering only HTTP/2 by ALPN.
pub(crate) fn with_h2_alpn(mut config: rustls::ClientConfig) -> rustls::ClientConfig {
    config.alpn_protocols = vec![b"h2".to_vec()];
    config
}

/// Forward `request` and return the response head — its body streaming in
/// `stream_pipe` unless it was small enough to buffer. Errors before the
/// head arrives come back as `Err` (the caller answers 502 or tries another
/// backend); errors after it end the body with an error, which the HTTP/2 and
/// HTTP/3 writers turn into a stream reset.
pub(crate) fn forward(request: &Request, client: crate::proxy::Peer<'_>, target: &Target<'_>) -> Result<Response, String> {
    let backend = target.backend;
    let addr = format!("{}:{}", backend.host, backend.port);
    let head = request_head(request, client, target.authority.unwrap_or(&addr), target.tls.is_some())?;
    let key = pool_key(&addr, target.tls);
    let (head_tx, head_rx) = std::sync::mpsc::sync_channel(1);
    let exchange = Exchange {
        key,
        addr,
        host: backend.host.clone(),
        tls: target.tls.cloned(),
        connect_timeout: target.connect_timeout,
        read_timeout: target.read_timeout,
    };
    let body = request.body.clone();
    runtime().spawn(async move {
        let result = exchange.run(head, body, &head_tx).await;
        if let Err(e) = result {
            // Only reaches the caller if the head wasn't sent yet.
            let _ = head_tx.try_send(Err(e));
        }
    });
    let wait = target.connect_timeout + target.read_timeout + Duration::from_secs(1);
    match head_rx.recv_timeout(wait) {
        Ok(result) => result,
        Err(_) => Err(format!("h2 proxy: no response head within {} ms", wait.as_millis())),
    }
}

/// One pool entry per backend address and trust setting (a `tls_ca_file`
/// upstream doesn't share connections with a WebPKI one).
fn pool_key(addr: &str, tls: Option<&Arc<rustls::ClientConfig>>) -> String {
    match tls {
        None => format!("h2c|{addr}"),
        Some(config) => format!("h2s:{:x}|{addr}", Arc::as_ptr(config) as usize),
    }
}

fn request_head(request: &Request, client: crate::proxy::Peer<'_>, addr: &str, tls: bool) -> Result<http::Request<()>, String> {
    let scheme = if tls { "https" } else { "http" };
    let uri: http::Uri = format!("{scheme}://{addr}{}", request.request_uri).parse().map_err(|e: http::uri::InvalidUri| e.to_string())?;
    let method = http::Method::from_bytes(request.method.as_bytes()).map_err(|e| e.to_string())?;
    let mut builder = http::Request::builder().method(method).uri(uri);
    for h in &request.headers {
        let lower = h.name.to_ascii_lowercase();
        if lower.is_empty() || NOT_FORWARDED.contains(&lower.as_str()) {
            if lower == "te" && h.value.split(',').any(|v| v.trim().eq_ignore_ascii_case("trailers")) {
                builder = builder.header("te", "trailers");
            }
            continue;
        }
        builder = builder.header(h.name.as_ref(), h.value.as_ref());
    }
    builder = builder.header("x-forwarded-for", client.ip).header("via", "2 rws");
    if request.header("X-Forwarded-Proto").is_none() {
        builder = builder.header("x-forwarded-proto", client.proto());
    }
    if request.header("X-Forwarded-Host").is_none() {
        if let Some(host) = request.header("host").filter(|h| !h.value.is_empty()) {
            builder = builder.header("x-forwarded-host", host.value.as_ref());
        }
    }
    builder.body(()).map_err(|e| e.to_string())
}

type HeadSender = std::sync::mpsc::SyncSender<Result<Response, String>>;

struct Exchange {
    key: String,
    addr: String,
    host: String,
    tls: Option<Arc<rustls::ClientConfig>>,
    connect_timeout: Duration,
    read_timeout: Duration,
}

impl Exchange {
    async fn run(self, head: http::Request<()>, body: Body, head_tx: &HeadSender) -> Result<(), String> {
        let end_of_stream = matches!(&body, Body::Bytes(bytes) if bytes.is_empty());
        let (response, send_stream) = self.send(head, end_of_stream).await?;
        if !end_of_stream {
            tokio::spawn(send_body(send_stream, body));
        }
        let response = self.within(response).await?.map_err(|e| format!("h2 proxy: {} answered with an error: {e}", self.addr))?;
        self.respond(response, head_tx).await;
        Ok(())
    }

    /// Send the request head on a pooled connection, or a new one when the
    /// pooled one is gone. A request is retried only before it was sent.
    async fn send(
        &self,
        head: http::Request<()>,
        end_of_stream: bool,
    ) -> Result<(h2::client::ResponseFuture, h2::SendStream<Bytes>), String> {
        let pooled = pool().lock().unwrap_or_else(|e| e.into_inner()).get(&self.key).map(|(_, s)| s.clone());
        let ready = match pooled {
            Some(sender) => match self.within(sender.ready()).await? {
                Ok(ready) => ready,
                Err(_) => {
                    pool().lock().unwrap_or_else(|e| e.into_inner()).remove(&self.key);
                    self.connect().await?
                }
            },
            None => self.connect().await?,
        };
        let mut ready = ready;
        ready.send_request(head, end_of_stream).map_err(|e| format!("h2 proxy: sending to {} failed: {e}", self.addr))
    }

    async fn connect(&self) -> Result<h2::client::SendRequest<Bytes>, String> {
        let tcp = tokio::time::timeout(self.connect_timeout, tokio::net::TcpStream::connect(&self.addr))
            .await
            .map_err(|_| format!("h2 proxy: connect to {} timed out", self.addr))?
            .map_err(|e| format!("h2 proxy: connect to {} failed: {e}", self.addr))?;
        let _ = tcp.set_nodelay(true);
        let sender = match &self.tls {
            None => self.handshake(tcp).await?,
            Some(config) => {
                let name = rustls::pki_types::ServerName::try_from(self.host.clone())
                    .map_err(|e| format!("invalid upstream hostname '{}': {e}", self.host))?;
                let tls = self
                    .within(tokio_rustls::TlsConnector::from(Arc::clone(config)).connect(name, tcp))
                    .await?
                    .map_err(|e| format!("h2 proxy: TLS handshake with {} failed: {e}", self.addr))?;
                self.handshake(tls).await?
            }
        };
        let ready = self.within(sender.clone().ready()).await?.map_err(|e| format!("h2 proxy: {} refused: {e}", self.addr))?;
        Ok(ready)
    }

    async fn handshake<T>(&self, io: T) -> Result<h2::client::SendRequest<Bytes>, String>
    where
        T: tokio::io::AsyncRead + tokio::io::AsyncWrite + Unpin + Send + 'static,
    {
        let (sender, connection) = self
            .within(h2::client::handshake(io))
            .await?
            .map_err(|e| format!("h2 proxy: handshake with {} failed: {e}", self.addr))?;
        let key = self.key.clone();
        let id = NEXT_CONNECTION.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        pool().lock().unwrap_or_else(|e| e.into_inner()).insert(key.clone(), (id, sender.clone()));
        tokio::spawn(async move {
            let _ = connection.await;
            // Closed (GOAWAY, error, idle close): the next request connects again.
            let mut pool = pool().lock().unwrap_or_else(|e| e.into_inner());
            if pool.get(&key).is_some_and(|(current, _)| *current == id) {
                pool.remove(&key);
            }
        });
        Ok(sender)
    }

    /// Return the head to the handler, then deliver the body and trailers.
    async fn respond(&self, upstream: http::Response<h2::RecvStream>, head_tx: &HeadSender) {
        let (parts, mut body) = upstream.into_parts();
        let mut response = Response::new();
        response.status_code = parts.status.as_u16() as i16;
        response.reason_phrase = parts.status.canonical_reason().unwrap_or("").to_string();
        let content_type = header(&parts.headers, "content-type").unwrap_or("application/octet-stream").to_string();
        let length = header(&parts.headers, "content-length").and_then(|v| v.parse::<u64>().ok());
        let grpc = content_type.starts_with("application/grpc");
        for (name, value) in &parts.headers {
            if NOT_RETURNED.contains(&name.as_str()) {
                continue;
            }
            if let Ok(value) = value.to_str() {
                response.headers.push(Header::new(name.as_str().to_string(), value.to_string()));
            }
        }
        // Headers only, the stream already ended (a gRPC trailers-only
        // error): no body, so the client's HEADERS end the stream too — gRPC
        // clients read the status from them only then.
        if body.is_end_stream() {
            response.headers.retain(|h| !h.name.eq_ignore_ascii_case("content-length"));
            return send_head(head_tx, Ok(response));
        }
        let trailers = Trailers::new();
        response.trailers = Some(trailers.clone());

        if !grpc && length.is_some_and(|n| n <= BUFFER_LIMIT) {
            let mut bytes = Vec::with_capacity(length.unwrap_or(0) as usize);
            loop {
                match self.within(body.data()).await {
                    Ok(Some(Ok(chunk))) => {
                        let _ = body.flow_control().release_capacity(chunk.len());
                        bytes.extend_from_slice(&chunk);
                    }
                    Ok(None) => break,
                    Ok(Some(Err(e))) => return send_head(head_tx, Err(format!("h2 proxy: body from {}: {e}", self.addr))),
                    Err(e) => return send_head(head_tx, Err(e)),
                }
            }
            if let Ok(Ok(Some(fields))) = self.within(body.trailers()).await {
                trailers.set(header_list(&fields));
            }
            if !bytes.is_empty() {
                response.content_range_list = vec![Range::get_content_range(bytes, content_type)];
            }
            return send_head(head_tx, Ok(super::normalize_upstream_response(response)));
        }

        // Streamed: the writer reads the body as it arrives. Its length isn't
        // repeated: an HTTP/1.1 client gets it chunked.
        response.headers.retain(|h| !h.name.eq_ignore_ascii_case("content-length"));
        let (tx, rx) = tokio::sync::mpsc::channel(BODY_CHANNEL);
        response.stream_pipe = Some(Box::new(crate::server::body_stream::ChannelReader::new(rx)));
        if head_tx.try_send(Ok(response)).is_err() {
            return; // the handler gave up waiting
        }
        loop {
            let next = match self.within(body.data()).await {
                Ok(Some(Ok(chunk))) => {
                    let _ = body.flow_control().release_capacity(chunk.len());
                    Ok(chunk.to_vec())
                }
                Ok(None) => break,
                Ok(Some(Err(e))) => Err(std::io::Error::other(format!("h2 proxy: body from {}: {e}", self.addr))),
                Err(e) => Err(std::io::Error::new(std::io::ErrorKind::TimedOut, e)),
            };
            let failed = next.is_err();
            if tx.send(next).await.is_err() || failed {
                return; // the client went away, or the upstream failed: the reader reports it
            }
        }
        match self.within(body.trailers()).await {
            Ok(Ok(Some(fields))) => trailers.set(header_list(&fields)),
            Ok(Ok(None)) => {}
            Ok(Err(e)) => {
                let _ = tx.send(Err(std::io::Error::other(format!("h2 proxy: trailers from {}: {e}", self.addr)))).await;
            }
            Err(e) => {
                let _ = tx.send(Err(std::io::Error::new(std::io::ErrorKind::TimedOut, e))).await;
            }
        }
        // Dropping `tx` ends the body; the trailers are set by then.
    }

    /// Await `f`, failing after the read timeout — applied to each wait, so a
    /// slow but progressing response (or a quiet gRPC stream shorter than the
    /// timeout) isn't cut off.
    async fn within<F: std::future::Future>(&self, f: F) -> Result<F::Output, String> {
        tokio::time::timeout(self.read_timeout, f)
            .await
            .map_err(|_| format!("h2 proxy: {} sent nothing for {} ms", self.addr, self.read_timeout.as_millis()))
    }
}

fn send_head(head_tx: &HeadSender, result: Result<Response, String>) {
    let _ = head_tx.try_send(result);
}

fn header<'a>(headers: &'a http::HeaderMap, name: &str) -> Option<&'a str> {
    headers.get(name).and_then(|v| v.to_str().ok())
}

fn header_list(fields: &http::HeaderMap) -> Vec<Header> {
    fields
        .iter()
        .filter_map(|(name, value)| Some(Header::new(name.as_str().to_string(), value.to_str().ok()?.to_string())))
        .collect()
}

/// Send the request body as it becomes available; a body that fails to read
/// (the client reset its stream) resets the upstream stream.
async fn send_body(mut stream: h2::SendStream<Bytes>, body: Body) {
    let mut chunks = match body {
        Body::Bytes(bytes) => {
            let _ = send_data(&mut stream, Bytes::from(bytes), true).await;
            return;
        }
        Body::Stream(_) => read_in_background(body.into_reader()),
    };
    while let Some(chunk) = chunks.recv().await {
        match chunk {
            Ok(data) => {
                if send_data(&mut stream, Bytes::from(data), false).await.is_err() {
                    return;
                }
            }
            Err(_) => {
                stream.send_reset(h2::Reason::CANCEL);
                return;
            }
        }
    }
    let _ = stream.send_data(Bytes::new(), true);
}

/// Read a blocking body on the blocking pool, a chunk at a time.
fn read_in_background(mut reader: Box<dyn Read + Send>) -> tokio::sync::mpsc::Receiver<std::io::Result<Vec<u8>>> {
    let (tx, rx) = tokio::sync::mpsc::channel(BODY_CHANNEL);
    tokio::task::spawn_blocking(move || {
        let mut buf = vec![0u8; 16 * 1024];
        loop {
            let next = match reader.read(&mut buf) {
                Ok(0) => return,
                Ok(n) => Ok(buf[..n].to_vec()),
                Err(e) => Err(e),
            };
            let failed = next.is_err();
            if tx.blocking_send(next).is_err() || failed {
                return;
            }
        }
    });
    rx
}

/// `data` within the upstream's flow-control window.
async fn send_data(stream: &mut h2::SendStream<Bytes>, mut data: Bytes, end: bool) -> Result<(), h2::Error> {
    if data.is_empty() {
        return if end { stream.send_data(Bytes::new(), true) } else { Ok(()) };
    }
    while !data.is_empty() {
        stream.reserve_capacity(data.len());
        let granted = match std::future::poll_fn(|cx| stream.poll_capacity(cx)).await {
            Some(granted) => granted?,
            None => return Err(h2::Error::from(h2::Reason::CANCEL)),
        };
        if granted == 0 {
            continue;
        }
        let chunk = data.split_to(granted.min(data.len()));
        stream.send_data(chunk, end && data.is_empty())?;
    }
    Ok(())
}

#[cfg(test)]
mod tests;