diff --git a/Cargo.toml b/Cargo.toml index 2ae580d..6430317 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "octane" -version = "0.2.0" +version = "0.3.0" authors = ["weegee <41485688+Daksh14@users.noreply.github.com>", "aplet123 "] description = "A web server which is express-like, intuitive and high-powered" license-file = "LICENSE" @@ -34,6 +34,8 @@ default = ["std", "faithful", "query_strings", "cookies", "url_variables", "raw_ std = ["tokio", "socket2", "octane_http/std"] faithful = ["std"] test_crate = ["std"] +# no effect: header names are always kept as they were sent. Kept so that +# feature lists naming it still build raw_headers = ["std"] query_strings = ["std"] extended_queries = ["std"] diff --git a/benchmarks/httparena/.gitignore b/benchmarks/httparena/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/benchmarks/httparena/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/benchmarks/httparena/Cargo.toml b/benchmarks/httparena/Cargo.toml new file mode 100644 index 0000000..184904b --- /dev/null +++ b/benchmarks/httparena/Cargo.toml @@ -0,0 +1,22 @@ +# Octane's entry for HttpArena (https://github.com/MDA2AV/HttpArena), where +# this directory is submitted as frameworks/octane. +[package] +name = "httparena-octane" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +# keep-alive, chunked request bodies and async handlers are in octane 0.3 +octane = { version = "0.3", features = ["rustls", "json"] } +tokio = { version = "1", features = ["macros", "rt-multi-thread", "time"] } +serde = { version = "1", features = ["derive"] } +serde_json = "1" + +[profile.release] +opt-level = 3 +codegen-units = 1 +lto = "thin" + +# not part of the octane workspace while it lives in the octane repository +[workspace] diff --git a/benchmarks/httparena/Dockerfile b/benchmarks/httparena/Dockerfile new file mode 100644 index 0000000..de8df80 --- /dev/null +++ b/benchmarks/httparena/Dockerfile @@ -0,0 +1,11 @@ +FROM rust:1.94 AS build +WORKDIR /app +COPY Cargo.toml . +RUN mkdir src && echo "fn main() {}" > src/main.rs && cargo build --release && rm -rf src/ target/release/httparena-octane* target/release/deps/httparena_octane* +COPY src ./src +RUN cargo build --release + +FROM debian:bookworm-slim +COPY --from=build /app/target/release/httparena-octane /server +EXPOSE 8080 8081 +CMD ["/server"] diff --git a/benchmarks/httparena/README.md b/benchmarks/httparena/README.md new file mode 100644 index 0000000..908af25 --- /dev/null +++ b/benchmarks/httparena/README.md @@ -0,0 +1,55 @@ +# octane + +[Octane](https://github.com/OctaneWeb/Octane) on the multi-threaded Tokio +runtime, default configuration. Octane parses HTTP/1.x itself rather than +sitting on hyper. + +## Stack + +- **Language:** Rust 1.94 +- **Framework:** Octane 0.2 +- **TLS:** rustls (ring) +- **Build:** Multi-stage, `debian:bookworm-slim` runtime + +## Endpoints + +| Endpoint | Method | Description | +|----------|--------|-------------| +| `/pipeline` | GET | Returns `ok` (plain text) | +| `/baseline11` | GET | Sums query parameter values | +| `/baseline11` | POST | Sums query parameters + request body | +| `/delay/{ms}` | GET | Waits `ms` milliseconds and returns the delay | +| `/json/{count}?m=N` | GET | First `count` dataset items with `total = price * quantity * m` | +| `/echo` | POST | Returns the request body back verbatim | + +The same routes are served over TLS on port 8081 (`json-tls`, `8gbit`). + +## Notes + +- Routing and path parameters (`/delay/:ms`, `/json/:count`) through the octane + router, query parameters through `req.get_query()` +- Request bodies arrive whole in `req.body`, read by octane from either a + `Content-Length` or chunked framing +- JSON through `res.json`, serialized by serde_json per request +- The dataset is leaked once at startup so responses borrow it instead of + cloning +- Connections are persistent (octane's default 5 s idle keep-alive) and + pipelined requests are answered in order +- `max_connections` is raised from octane's default of 1,024 to 65,536, since + `async` holds 32,000 connections open and `baseline` 4,096 +- `/delay/{ms}` is an async handler (`route_async_stop!`) waiting on + `tokio::time::sleep`, so a pending request holds a timer, not a worker + thread. Every other handler is synchronous and runs straight on the + connection's task +- json-tls and 8gbit share the TLS listener on 8081, which octane runs next to + the plaintext one and which advertises ALPN `http/1.1`. Without `/certs` it + logs a warning and only plaintext is served +- No `json-comp`: octane has no response compression, and standard mode + requires the framework's own +- Completeness: routes with path parameters are matched by the router; + `app.add` / `add_route` middleware runs before or after the + route handlers in registration order, app-wide or per path, and can stop + the chain; the request + carries path and query parameters, headers, cookies and the buffered body; + responses are declared with `res.status(..).set(..).send(..)`, `res.json` + and `res.send_file` diff --git a/benchmarks/httparena/meta.json b/benchmarks/httparena/meta.json new file mode 100644 index 0000000..0cb891b --- /dev/null +++ b/benchmarks/httparena/meta.json @@ -0,0 +1,28 @@ +{ + "display_name": "octane", + "language": "Rust", + "type": "emerging", + "mode": "standard", + "completeness": { + "routing": true, + "middleware": true, + "request": true, + "response": true + }, + "engine": "octane", + "description": "Octane on the multi-threaded Tokio runtime with its own HTTP/1.x parser, default configuration. Routes and path parameters through the octane router, query parameters through req.get_query(), bodies read whole from req.body (Content-Length or chunked), JSON serialized per request with serde_json through res.json. TLS on :8081 through rustls (ring), ALPN http/1.1. /delay is an async handler waiting on tokio::time::sleep, the rest are synchronous handlers. max_connections raised to 65,536 for the held-connection profiles.", + "repo": "https://github.com/OctaneWeb/Octane", + "enabled": true, + "tests": [ + "baseline", + "async", + "latency-1m", + "latency-10k", + "latency-500k-8cpu", + "pipelined", + "limited-conn", + "json-tls", + "8gbit" + ], + "maintainers": ["Daksh14"] +} diff --git a/benchmarks/httparena/src/main.rs b/benchmarks/httparena/src/main.rs new file mode 100644 index 0000000..1f13f4c --- /dev/null +++ b/benchmarks/httparena/src/main.rs @@ -0,0 +1,191 @@ +//! Octane's entry for HttpArena: every endpoint goes through octane's router, +//! request and response API, on the multi-threaded tokio runtime. + +use octane::prelude::*; +use octane::StatusCode; +use serde::{Deserialize, Serialize}; +use octane::request::Query; +use std::error::Error; +use std::time::Duration; + +#[derive(Deserialize)] +struct Rating { + score: i64, + count: i64, +} + +#[derive(Deserialize)] +struct DatasetItem { + id: i64, + name: String, + category: String, + price: i64, + quantity: i64, + active: bool, + tags: Vec, + rating: Rating, +} + +#[derive(Serialize)] +struct RatingOut { + score: i64, + count: i64, +} + +// Field order is the wire order: id..rating, then the computed total. +#[derive(Serialize)] +struct ProcessedItem<'a> { + id: i64, + name: &'a str, + category: &'a str, + price: i64, + quantity: i64, + active: bool, + tags: &'a [String], + rating: RatingOut, + total: i64, +} + +#[derive(Serialize)] +struct JsonResponse<'a> { + items: Vec>, + count: usize, +} + +fn load_dataset() -> Vec { + let path = std::env::var("DATASET_PATH").unwrap_or_else(|_| "/data/dataset.json".to_string()); + match std::fs::read_to_string(path) { + Ok(data) => serde_json::from_str(&data).unwrap_or_default(), + Err(_) => Vec::new(), + } +} + +// A query parameter as an integer, 0 when it is missing or isn't one +fn int(query: &Query, name: &str) -> i64 { + query + .get(name) + .and_then(|value| value.parse().ok()) + .unwrap_or(0) +} + +#[tokio::main] +async fn main() -> Result<(), Box> { + // Leaked once at startup so handlers borrow the items instead of cloning + // every string into each response. + let dataset: &'static [DatasetItem] = Box::leak(load_dataset().into_boxed_slice()); + + let mut app = Octane::new(); + // The async profile holds 32,000 connections open at once, octane serves + // 1,024 at a time by default. + app.settings.max_connections = 65_536; + + app.get( + "/pipeline", + route_stop!(|req, res| { + res.with_type("text/plain").send("ok"); + }), + )?; + + app.get( + "/baseline11", + route_stop!(|req, res| { + let query = req.get_query(); + let sum = int(&query, "a") + int(&query, "b"); + res.with_type("text/plain").send_owned(sum.to_string()); + }), + )?; + + // The body arrives whole, whether it was sent with a Content-Length or + // chunked. + app.post( + "/baseline11", + route_stop!(|req, res| { + let query = req.get_query(); + let body = std::str::from_utf8(req.body) + .ok() + .and_then(|body| body.trim().parse::().ok()) + .unwrap_or(0); + let sum = int(&query, "a") + int(&query, "b") + body; + res.with_type("text/plain").send_owned(sum.to_string()); + }), + )?; + + // An async handler: the request waits on a tokio timer, its worker thread + // goes on serving other connections. + app.get( + "/delay/:ms", + route_async_stop!(|req, res| { + match req.vars.get("ms").and_then(|ms| ms.parse::().ok()) { + Some(ms) => { + tokio::time::sleep(Duration::from_millis(ms)).await; + res.with_type("text/plain").send_owned(ms.to_string()); + } + None => { + res.status(StatusCode::BadRequest) + .with_type("text/plain") + .send("bad delay"); + } + } + }), + )?; + + // The first `count` items with their totals, serialized per request. + app.get( + "/json/:count", + route_stop!(|req, res| { + match req.vars.get("count").and_then(|count| count.parse::().ok()) { + Some(count) => { + let m = req + .get_query() + .get("m") + .and_then(|m| m.parse::().ok()) + .unwrap_or(1); + let count = count.min(dataset.len()); + let items = dataset[..count] + .iter() + .map(|item| ProcessedItem { + id: item.id, + name: &item.name, + category: &item.category, + price: item.price, + quantity: item.quantity, + active: item.active, + tags: &item.tags, + rating: RatingOut { + score: item.rating.score, + count: item.rating.count, + }, + total: item.price * item.quantity * m, + }) + .collect(); + if res.json(JsonResponse { items, count }).is_err() { + res.status(StatusCode::InternalServerError).send(""); + } + } + None => { + res.status(StatusCode::BadRequest) + .with_type("text/plain") + .send("bad count"); + } + } + }), + )?; + + // The bytes that arrived, unchanged. + app.post( + "/echo", + route_stop!(|req, res| { + res.with_type("application/octet-stream").send(req.body); + }), + )?; + + // json-tls and 8gbit: the same routes over HTTP/1.1 + TLS on 8081 with + // the certificate the harness mounts. Without it octane logs a warning + // and serves plaintext only. + let cert = std::env::var("TLS_CERT").unwrap_or_else(|_| "/certs/server.crt".to_string()); + let key = std::env::var("TLS_KEY").unwrap_or_else(|_| "/certs/server.key".to_string()); + app.ssl(8081).cert(&cert).key(&key); + + app.listen(8080, || println!("octane listening on :8080, and on :8081 over TLS")) + .await +} diff --git a/guests/wasip3-server/src/lib.rs b/guests/wasip3-server/src/lib.rs index a0376b6..61b857c 100644 --- a/guests/wasip3-server/src/lib.rs +++ b/guests/wasip3-server/src/lib.rs @@ -65,7 +65,7 @@ fn routes() -> Result { router.get( "/hello/:name", handler(async |req, res| { - let name = req.vars.get("name").copied().unwrap_or("there"); + let name = req.vars.get("name").unwrap_or("there"); let _ = res .send( StatusCode::Ok, diff --git a/octane_http/fuzz/fuzz_targets/headers.rs b/octane_http/fuzz/fuzz_targets/headers.rs index 472369c..cfc35bd 100644 --- a/octane_http/fuzz/fuzz_targets/headers.rs +++ b/octane_http/fuzz/fuzz_targets/headers.rs @@ -2,7 +2,7 @@ //! //! The input is a header block, the lines between the request line and the //! empty line. Parsing must never panic, every accepted field must be well -//! formed, repeated fields must be merged in order, and the body length must +//! formed, repeated fields must all be found in order, and the body length must //! follow RFC 9112 section 6.3: missing means 0, repeats must agree, anything //! but digits is a 400 and a length over the limit is a 413. @@ -33,14 +33,16 @@ fuzz_target!(|data: &[u8]| { None => return, }; - // every line became one field, in order, holding exactly what was sent + // every line is one field, in order, holding exactly what was sent let lines: Vec<&str> = if block.is_empty() { Vec::new() } else { block.split("\r\n").collect() }; - assert_eq!(headers.raw.len(), lines.len()); - for (field, line) in headers.raw.iter().zip(&lines) { + let fields: Vec<_> = headers.iter().collect(); + assert_eq!(fields.len(), lines.len()); + assert_eq!(headers.len(), lines.len()); + for (field, line) in fields.iter().zip(&lines) { let (name, value) = (field.name(), field.value()); assert!(!name.is_empty() && name.bytes().all(is_tchar), "{:?}", name); assert!(!value @@ -51,24 +53,23 @@ fuzz_target!(|data: &[u8]| { assert_eq!(value, line_value.trim_matches(is_ows)); } - // repeated fields are joined in order, cookies with "; " - let mut joined: HashMap = HashMap::new(); - for field in &headers.raw { - let name = field.name().to_ascii_lowercase(); - let sep = if name == "cookie" { "; " } else { ", " }; - joined - .entry(name) - .and_modify(|v| { - v.push_str(sep); - v.push_str(&field.value()); - }) - .or_insert_with(|| field.value()); + // every name finds its fields in order, whatever the case it's asked in + let mut by_name: HashMap> = HashMap::new(); + for field in &fields { + by_name + .entry(field.name().to_ascii_lowercase()) + .or_default() + .push(field.value()); + } + for (name, values) in &by_name { + assert_eq!(&headers.get_all(&name.to_ascii_uppercase()).collect::>(), values); + assert_eq!(headers.get(name), values.first().copied()); } - assert_eq!(headers.parsed, joined); let length = fuzzing::content_length(&headers, MAX_BODY); - let value = match headers.parsed.get("content-length") { - Some(value) => value, + // the members of every Content-Length field, as one list + let value = match by_name.get("content-length") { + Some(values) => values.join(","), None => return assert_eq!(length, Ok(0)), }; let members: Vec<&str> = value.split(',').map(|m| m.trim_matches(is_ows)).collect(); diff --git a/octane_http/fuzz/fuzz_targets/query.rs b/octane_http/fuzz/fuzz_targets/query.rs index 11ad394..e5ed6f4 100644 --- a/octane_http/fuzz/fuzz_targets/query.rs +++ b/octane_http/fuzz/fuzz_targets/query.rs @@ -23,7 +23,7 @@ fuzz_target!(|data: &[u8]| { let name = pair.split_once('=').map_or(pair, |(name, _)| name); if !name.is_empty() { assert!( - query.contains_key(&fuzzing::query_decode(name)), + query.contains(&fuzzing::query_decode(name)), "{:?}", pair ); diff --git a/octane_http/fuzz/fuzz_targets/request.rs b/octane_http/fuzz/fuzz_targets/request.rs index d5b3a1b..641e813 100644 --- a/octane_http/fuzz/fuzz_targets/request.rs +++ b/octane_http/fuzz/fuzz_targets/request.rs @@ -8,8 +8,11 @@ //! cookies, headers, body) back into headers, cookies and bodies. //! //! Any panic is a crash. The server must always answer with exactly one well -//! framed response, with a body as long as its `Content-Length` and no header -//! line the app didn't set, whatever the request smuggled into the values. +//! framed response, with a body as long as its `Content-Length` (none for a +//! `HEAD` request) and no header line the app didn't set, whatever the +//! request smuggled into the values. Keep-alive is off, so the first request +//! is the only one answered; a client that only ever sends empty lines gets +//! no answer at all. #![no_main] @@ -21,6 +24,8 @@ use std::sync::{Arc, OnceLock}; // every header the server or the app below can send const HEADERS: &[&str] = &[ + "Allow", + "Connection", "Content-Length", "Content-Type", "Date", @@ -30,11 +35,16 @@ const HEADERS: &[&str] = &[ "X-Echo", ]; +// what a client sending `Expect: 100-continue` may get before the response +const CONTINUE: &[u8] = b"HTTP/1.1 100 Continue\r\n\r\n"; + fn app() -> &'static Arc { static APP: OnceLock> = OnceLock::new(); APP.get_or_init(|| { let mut app = Octane::new(); app.settings.max_body_size = 64; + // one response per connection + app.settings.keep_alive = None; app.add(route_next!(|req, res| { res.set("X-Path", &req.request_line.path.to_string()); })) @@ -46,15 +56,15 @@ fn app() -> &'static Arc { app.get( "/redirect", route_stop!(|req, res| { - let next = req.get_query().get("next").cloned().unwrap_or_default(); - res.redirect(&next).send(""); + let query = req.get_query(); + res.redirect(query.get("next").unwrap_or_default()).send(""); }), ) .unwrap(); app.get( "/cookies", route_stop!(|req, res| { - let set = req.get_query().get("set").cloned().unwrap_or_default(); + let set = req.get_query().get("set").unwrap_or_default().to_owned(); let pairs: Vec = req .cookies .iter() @@ -69,7 +79,7 @@ fn app() -> &'static Arc { app.post( "/echo", route_stop!(|req, res| { - let echo = req.headers.get("x-echo").cloned().unwrap_or_default(); + let echo = req.headers.get("x-echo").unwrap_or_default(); res.set("X-Echo", &echo).send(req.body); }), ) @@ -90,6 +100,18 @@ fuzz_target!(|data: &[u8]| { }; let out = fuzzing::serve(app(), wire, *seed as usize % 17 + 1); + // empty lines before a request line are skipped, a lone CR could still + // become one + let mut request = wire; + while let Some(rest) = request.strip_prefix(&b"\r\n"[..]) { + request = rest; + } + if request.is_empty() || request == b"\r" { + assert!(out.is_empty(), "answered no request: {:?}", out); + return; + } + let out = out.strip_prefix(CONTINUE).unwrap_or(&out); + let end = out .windows(4) .position(|w| w == b"\r\n\r\n") @@ -118,10 +140,13 @@ fuzz_target!(|data: &[u8]| { assert!(content_length.is_none(), "two Content-Length headers"); content_length = Some(value.parse::().expect("bad Content-Length")); } + if name == "Connection" { + assert_eq!(value, "close", "the connection is kept open"); + } + } + let content_length = content_length.expect("no Content-Length"); + // the answer to a HEAD request has the length of the body it leaves out + if !(request.starts_with(b"HEAD ") && body.is_empty()) { + assert_eq!(body.len(), content_length, "body doesn't match Content-Length"); } - assert_eq!( - Some(body.len()), - content_length, - "body doesn't match Content-Length" - ); }); diff --git a/src/config.rs b/src/config.rs index 59e5724..289f2a1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -102,17 +102,22 @@ impl Ssl { /// item. If no config is specified then defaults are used. /// pub struct OctaneConfig { - /// The duration for keep alive requests. It is 5 seconds by default + /// How long a persistent (keep-alive) connection may sit idle waiting for + /// its next request before it is closed. It is 5 seconds by default. + /// `None` turns keep-alive off: every response closes its connection. pub keep_alive: Option, /// Maximum number of connections being served at once. Once the limit is /// reached, each listener stops draining the operating system's backlog /// while it waits for capacity. It is 1024 by default; zero is treated as /// one, and values too large for the runtime return an error from `listen`. + /// An idle keep-alive connection holds its place until it is closed. pub max_connections: usize, - /// Maximum total time spent on one connection after it acquires serving - /// capacity, including a TLS handshake when TLS is enabled. `None` - /// disables the deadline. It is 30 seconds by default. Custom Tokio - /// runtimes must enable the time driver with `enable_time` or `enable_all`. + /// Maximum time to receive one request and send its response, and to + /// complete a TLS handshake when TLS is enabled. The first request on a + /// connection must also start within it. A connection that runs out of + /// time is closed. `None` disables the deadline. It is 30 seconds by + /// default. Custom Tokio runtimes must enable the time driver with + /// `enable_time` or `enable_all`. pub connection_timeout: Option, /// An instance of the `Ssl` struct to store the values of key and certificates. pub ssl: Ssl, diff --git a/src/connection.rs b/src/connection.rs new file mode 100644 index 0000000..9c10191 --- /dev/null +++ b/src/connection.rs @@ -0,0 +1,215 @@ +//! The connection the HTTP/1.x server reads requests from and writes +//! responses to. Responses are held back while there are requests left to +//! answer and go out together once the server would wait on the client, so +//! the answers to a batch of pipelined requests cost one write rather than +//! one each. +use std::io; +use std::pin::Pin; +use std::task::{ready, Context, Poll}; +use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; + +// Past this many held bytes, writing doesn't wait for the server to run out +// of requests. Writes at least this large on their own skip the buffer, and +// a buffer that grew past it is released once written, rather than kept for +// as long as the connection stays open. +const WRITE_BUFFER: usize = 64 * 1024; + +pub(crate) struct Connection { + io: S, + // held output, `out[written..]` is what hasn't gone out yet + out: Vec, + written: usize, + // something was written to `io` since it was last flushed, which over + // TLS can still be sitting in a buffer + unflushed: bool, +} + +impl Connection { + pub(crate) fn new(io: S) -> Self { + Self { + io, + out: Vec::new(), + written: 0, + unflushed: false, + } + } + + /// Whether there's output that hasn't reached the client yet + pub(crate) fn has_pending(&self) -> bool { + !self.out.is_empty() || self.unflushed + } + + /// The output held for the client, to append a response to. Anything + /// not written yet stays ahead of it. + pub(crate) fn buffer(&mut self) -> &mut Vec { + &mut self.out + } + + // Write and flush everything held + fn poll_send(&mut self, cx: &mut Context<'_>) -> Poll> { + while self.written < self.out.len() { + let n = ready!(Pin::new(&mut self.io).poll_write(cx, &self.out[self.written..]))?; + if n == 0 { + return Poll::Ready(Err(io::ErrorKind::WriteZero.into())); + } + self.written += n; + self.unflushed = true; + } + // keeps its allocation for the next responses, unless one of them + // made it large + if self.out.capacity() > WRITE_BUFFER { + self.out = Vec::new(); + } else { + self.out.clear(); + } + self.written = 0; + if self.unflushed { + ready!(Pin::new(&mut self.io).poll_flush(cx))?; + self.unflushed = false; + } + Poll::Ready(Ok(())) + } +} + +impl AsyncRead for Connection { + fn poll_read( + self: Pin<&mut Self>, + cx: &mut Context<'_>, + buf: &mut ReadBuf<'_>, + ) -> Poll> { + let this = self.get_mut(); + // everything answered so far goes out before waiting on the client, + // which may be waiting for it before it sends more + ready!(this.poll_send(cx))?; + Pin::new(&mut this.io).poll_read(cx, buf) + } +} + +impl AsyncWrite for Connection { + fn poll_write( + self: Pin<&mut Self>, + cx: &mut Context<'_>, + buf: &[u8], + ) -> Poll> { + let this = self.get_mut(); + if this.out.len() >= WRITE_BUFFER || (buf.len() >= WRITE_BUFFER && !this.out.is_empty()) { + ready!(this.poll_send(cx))?; + } + if buf.len() >= WRITE_BUFFER { + // nothing is held now, so the order is kept without copying it + let n = ready!(Pin::new(&mut this.io).poll_write(cx, buf))?; + this.unflushed = true; + return Poll::Ready(Ok(n)); + } + this.out.extend_from_slice(buf); + Poll::Ready(Ok(buf.len())) + } + + fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + self.get_mut().poll_send(cx) + } + + fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + let this = self.get_mut(); + ready!(this.poll_send(cx))?; + Pin::new(&mut this.io).poll_shutdown(cx) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use tokio::io::{AsyncReadExt, AsyncWriteExt}; + + // Records every write and flush, and reads hand out `input` + #[derive(Default)] + struct Recorder { + input: Vec, + writes: Vec>, + flushes: usize, + shut: bool, + } + + impl AsyncRead for Recorder { + fn poll_read( + mut self: Pin<&mut Self>, + _: &mut Context<'_>, + buf: &mut ReadBuf<'_>, + ) -> Poll> { + let n = self.input.len().min(buf.remaining()); + let rest = self.input.split_off(n); + buf.put_slice(&self.input); + self.input = rest; + Poll::Ready(Ok(())) + } + } + + impl AsyncWrite for Recorder { + fn poll_write( + mut self: Pin<&mut Self>, + _: &mut Context<'_>, + buf: &[u8], + ) -> Poll> { + self.writes.push(buf.to_vec()); + Poll::Ready(Ok(buf.len())) + } + fn poll_flush(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll> { + self.flushes += 1; + Poll::Ready(Ok(())) + } + fn poll_shutdown(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll> { + self.shut = true; + Poll::Ready(Ok(())) + } + } + + #[tokio::test] + async fn responses_go_out_together_before_the_next_read() { + let mut conn = Connection::new(Recorder { + input: b"next".to_vec(), + ..Recorder::default() + }); + for response in [&b"one "[..], b"two ", b"three"].iter() { + conn.write_all(response).await.unwrap(); + } + assert!(conn.io.writes.is_empty()); + let mut buf = [0u8; 4]; + conn.read_exact(&mut buf).await.unwrap(); + assert_eq!(&buf, b"next"); + assert_eq!(conn.io.writes, vec![b"one two three".to_vec()]); + assert_eq!(conn.io.flushes, 1); + // nothing held, nothing to flush + conn.flush().await.unwrap(); + assert_eq!(conn.io.flushes, 1); + } + + #[tokio::test] + async fn a_large_buffer_is_released_once_written() { + let mut conn = Connection::new(Recorder::default()); + // many small responses add up past the limit, without one of them + // finding the buffer full + for _ in 0..(WRITE_BUFFER / 1000 + 1) { + conn.write_all(&[b'x'; 1000]).await.unwrap(); + } + assert!(conn.io.writes.is_empty() && conn.out.len() > WRITE_BUFFER); + conn.flush().await.unwrap(); + assert_eq!(conn.out.capacity(), 0); + // a small one keeps its buffer + conn.write_all(b"small").await.unwrap(); + conn.flush().await.unwrap(); + assert!(conn.out.capacity() > 0); + } + + #[tokio::test] + async fn large_writes_keep_their_order_and_skip_the_buffer() { + let mut conn = Connection::new(Recorder::default()); + let large = vec![b'x'; WRITE_BUFFER]; + conn.write_all(b"head").await.unwrap(); + conn.write_all(&large).await.unwrap(); + conn.write_all(b"tail").await.unwrap(); + assert_eq!(conn.io.writes, vec![b"head".to_vec(), large]); + conn.shutdown().await.unwrap(); + assert_eq!(conn.io.writes.last().unwrap(), b"tail"); + assert!(conn.io.shut); + } +} diff --git a/src/error.rs b/src/error.rs index 76cb097..6105ad2 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,56 +1,27 @@ use crate::constants::NOT_FOUND; use crate::responder::Response; -use crate::Octane; use octane_http::StatusCode; -use std::error; -use std::marker::Unpin; -use tokio::io::AsyncWrite; -/// The Error structure holds the kind of error code and -/// the location of the custom 404 file, if given by the user -/// and manages sending errors on internal http errors and other instances -/// will send with blank content if no 404 file is found or will send the -/// 404 file directly if given. +/// The Error structure builds the responses the server sends on internal +/// http errors: when no route answers a request or when a request can't be +/// served at all. /// /// You will not have to use this manually, to send errors on your own, you /// can do so by just specifying the error code and the content like /// `res.status(status_code).send("")`. -pub struct Error { - kind: StatusCode, -} +pub struct Error; #[derive(Debug, Clone, PartialEq, Eq)] // Custom error type for invalid SSL certificates pub struct InvalidCertError; -/// Takes in a http stream and a error code and sends to the client -#[macro_export] -#[doc(hidden)] -macro_rules! declare_error { - ($stream : expr, $error_type : expr) => { - Error::err($error_type, $stream).await?; - return Ok(()); - }; -} - impl Error { - pub async fn err(status_code: StatusCode, stream: S) -> Result<(), Box> - where - S: AsyncWrite + Unpin, - { - Error { kind: status_code }.send(stream).await - } - async fn send(self, stream: S) -> Result<(), Box> - where - S: AsyncWrite + Unpin, - { + /// The response sent for `status_code` when no route answers a request + /// or it can't be served + pub(crate) fn response(status_code: StatusCode) -> Response<'static> { let mut res = Response::new_from_static(NOT_FOUND.as_bytes()); - res.status(self.kind) - .default_headers() - .set("Content-Type", "text/html"); - - let response = res.into_parts(); - Octane::send(response, stream).await?; - Ok(()) + res.status(status_code).default_headers(); + res.headers.set("Content-Type", "text/html"); + res } } diff --git a/src/fuzzing.rs b/src/fuzzing.rs index c0f7e58..a62bf95 100644 --- a/src/fuzzing.rs +++ b/src/fuzzing.rs @@ -4,11 +4,13 @@ #[cfg(feature = "cookies")] use crate::cookie::{Cookie, CookieError}; use crate::path::PathBuf; +use crate::query::Query; #[cfg(feature = "extended_queries")] use crate::query::QueryValue; use crate::request::{Headers, RequestLine}; use crate::server::Octane; use octane_http::StatusCode; +#[cfg(feature = "extended_queries")] use std::collections::HashMap; use std::io; use std::pin::Pin; @@ -24,8 +26,8 @@ pub fn request_line(method: &[u8], url: &[u8], version: &[u8]) -> Option Option { - Headers::parse(block.to_owned()) +pub fn headers(block: &str) -> Option> { + Headers::parse(block) } /// The body length the server reads for a request with `headers` @@ -39,8 +41,8 @@ pub fn path(path: &str) -> Option> { } /// Parse a query string -pub fn query(query: &str) -> HashMap { - crate::query::parse_query(query) +pub fn query(query: &str) -> Query<'_> { + Query::parse(query) } /// Parse a query string with arrays and objects @@ -51,7 +53,7 @@ pub fn extended_query(query: &str) -> HashMap { /// Decode one query string component pub fn query_decode(component: &str) -> String { - crate::query::unescape_hex(component) + crate::query::decode(component).into_owned() } /// Parse a request `Cookie` header @@ -67,10 +69,15 @@ pub fn set_cookie(cookie: &Cookie<'_>) -> Result { } /// Serve one connection whose client sends `input`, handed to the server -/// `chunk` bytes per read, and return everything the server wrote back +/// `chunk` bytes per read, and return everything the server wrote back. +/// The connection ends when `input` does, after every request in it that can +/// be served is answered. pub fn serve(app: &Arc, input: &[u8], chunk: usize) -> Vec { thread_local! { + // the server keeps idle and request deadlines, which never fire here + // because reading the in-memory stream never waits static RUNTIME: Runtime = Builder::new_current_thread() + .enable_time() .build() .expect("Unable to build tokio runtime"); } diff --git a/src/lib.rs b/src/lib.rs index 44f98ca..42e01ed 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,7 +23,7 @@ //! Get started by adding the lib entry in your cargo.toml file //! //! ```toml -//! octane = "0.1.1" +//! octane = "0.3" //! tokio = { version = "1.54.1", features = ["macros", "rt-multi-thread"] } //! ``` //! @@ -65,7 +65,8 @@ //! - `query_strings`: To enable query string parsing, eg. `?foo=bar&bar=foo` //! - `cookies`: To enable basic cookie parsing and value handling. //! - `url_variables`: To support variables in url. -//! - `raw_headers`: To have access to original, un-normalized headers. +//! - `raw_headers`: No longer has an effect, header names are always kept +//! as they were sent and matched without regard to case. //! - `rustls`: To use rustls for ssl. //! - `openSSL`: To use openssl for ssl. //! - `std`: The tokio based server, everything except [`portable`]. All the @@ -82,7 +83,7 @@ //! WASI, bare-metal TCP stacks like smoltcp, or raw sockets without tokio. //! //! ```toml -//! octane = { version = "0.1.1", default-features = false } +//! octane = { version = "0.3", default-features = false } //! ``` //! //! On `wasm32-wasip3` the `wasm` feature adds `octane::wasip3`: octane's @@ -93,7 +94,7 @@ //! `url_variables` for routes like `/users/:id`: //! //! ```toml -//! octane = { version = "0.1.1", default-features = false, features = ["wasm", "url_variables"] } +//! octane = { version = "0.3", default-features = false, features = ["wasm", "url_variables"] } //! ``` //! //! **Note**: If both `rustls` and `openSSL` features are enabled then @@ -105,6 +106,8 @@ #[cfg(feature = "std")] /// Configurations for Octane web server pub mod config; +#[cfg(feature = "std")] +pub(crate) mod connection; #[cfg(any( feature = "std", all(feature = "wasm", target_os = "wasi", target_env = "p3") @@ -156,6 +159,8 @@ pub use crate::server::Octane; #[cfg(feature = "std")] pub(crate) mod server_builder; #[cfg(feature = "std")] +pub(crate) mod shutdown; +#[cfg(feature = "std")] pub(crate) mod time; #[cfg(feature = "std")] pub(crate) mod tls; @@ -177,14 +182,17 @@ pub use octane_http::StatusCode; /// Prelude here brings in scope, the [`Route`](router/trait.Route.html) and /// [`Config`](config/trait.Config.html) trait, [`Octane`](struct.Octane.html) main server /// and [`Router`](router/struct.Router.html) struct with the [`Flow`](router/enum.Flow.html) -/// enum, and the [`route`](macro.route.html), [`route_next`](macro.route_next.html), -/// [`route_stop`](macro.route_stop.html) macros +/// enum, the [`route`](macro.route.html), [`route_next`](macro.route_next.html), +/// [`route_stop`](macro.route_stop.html) macros and their async counterparts +/// [`route_async`](macro.route_async.html), +/// [`route_async_next`](macro.route_async_next.html) and +/// [`route_async_stop`](macro.route_async_stop.html) #[cfg(feature = "std")] pub mod prelude { pub use crate::config::Config; pub use crate::Octane; pub use crate::{ - route, route_next, route_stop, + route, route_async, route_async_next, route_async_stop, route_next, route_stop, router::{Flow, Route, Router}, }; } diff --git a/src/method.rs b/src/method.rs index 5f41452..fd71821 100644 --- a/src/method.rs +++ b/src/method.rs @@ -33,6 +33,30 @@ impl RequestMethod { pub fn is_some(&self) -> bool { !matches!(self, Self::None) } + /// How many variants there are, see [`index`](#method.index) + pub(crate) const COUNT: usize = 11; + + /// A distinct number below [`COUNT`](#associatedconstant.COUNT) per + /// variant, to keep something per method in an array + pub(crate) fn index(self) -> usize { + self as usize + } + + /// The method's name, empty for `All` and `None` which aren't methods + pub fn as_str(&self) -> &'static str { + match self { + Self::Options => "OPTIONS", + Self::Get => "GET", + Self::Head => "HEAD", + Self::Post => "POST", + Self::Put => "PUT", + Self::Delete => "DELETE", + Self::Trace => "TRACE", + Self::Connect => "CONNECT", + Self::Patch => "PATCH", + Self::All | Self::None => "", + } + } } impl From> for RequestMethod { diff --git a/src/path.rs b/src/path.rs index 98e3431..ccb56aa 100644 --- a/src/path.rs +++ b/src/path.rs @@ -1,9 +1,8 @@ use crate::{default, deref}; -use std::collections::{hash_map, HashMap}; use std::convert::TryFrom; use std::error; use std::fmt; -use std::iter::{FromIterator, Iterator, Map}; +use std::iter::{FromIterator, Iterator}; use std::path::PathBuf as StdPathBuf; use std::str::FromStr; @@ -81,17 +80,14 @@ impl PathBuf { } /// Check if a Pathbuf matches the given pathbuf in url #[cfg(feature = "url_variables")] - pub fn check_matches<'a, 'b>( - &'a self, - other: &'b PathBuf, - ) -> Option> { + pub fn check_matches<'a>(&'a self, other: &'a PathBuf) -> Option> { if self.len() != other.len() { return None; } - let mut vars = HashMap::new(); + let mut vars = Vars::new(); for (a, b) in self.iter().zip(other.iter()) { if a.as_bytes()[0] == b':' { - vars.insert(&a[1..], &b[..]); + vars.vars.push((&a[1..], &b[..])); } else if a != b { return None; } @@ -155,6 +151,36 @@ impl FromStr for PathBuf { } } +/// The url variables of a matched route, like `id` in `/users/:id`, in the +/// order they appear in the route. They are borrowed from the request path. +#[derive(Debug, Clone, PartialEq, Eq, Default)] +pub struct Vars<'a> { + vars: Vec<(&'a str, &'a str)>, +} + +impl<'a> Vars<'a> { + pub fn new() -> Self { + Self { vars: Vec::new() } + } + /// The value of the variable `name` + pub fn get(&self, name: &str) -> Option<&'a str> { + self.vars + .iter() + .find(|(var, _)| *var == name) + .map(|(_, value)| *value) + } + /// Every variable and its value + pub fn iter(&self) -> impl Iterator + '_ { + self.vars.iter().copied() + } + pub fn len(&self) -> usize { + self.vars.len() + } + pub fn is_empty(&self) -> bool { + self.vars.is_empty() + } +} + #[derive(Debug, Clone, PartialEq, Eq)] pub struct PathData { pub orig_path: PathBuf, @@ -164,252 +190,132 @@ pub struct PathData { #[derive(Debug, Clone)] pub struct MatchedPath<'a, T> { #[cfg(feature = "url_variables")] - pub vars: HashMap<&'a str, &'a str>, + pub vars: Vars<'a>, pub data: &'a T, } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub enum PathChunk { - Chunk(String), - CatchAll, - End, -} - +/// A trie of paths, one level per segment #[derive(Debug, Clone)] -pub enum PathNode { - Node(HashMap>), - Leaf(Vec>), +pub struct PathNode { + // literal segments, sorted so the request's segment is found by binary + // search, without hashing or copying it + chunks: Vec<(String, PathNode)>, + // a `:variable` segment + var: Option>>, + // what was inserted at exactly this path + end: Vec>, } impl PathNode { - fn unwrap_node(&self) -> &HashMap> { - if let PathNode::Node(x) = self { - x - } else { - panic!("Tried to unwrap a leaf as a node."); - } - } - - fn unwrap_node_mut(&mut self) -> &mut HashMap> { - if let PathNode::Node(x) = self { - x - } else { - panic!("Tried to unwrap a leaf as a node."); - } - } - - fn unwrap_leaf(&self) -> &Vec> { - if let PathNode::Leaf(x) = self { - x - } else { - panic!("Tried to unwrap a node as a leaf."); - } - } - - fn unwrap_leaf_mut(&mut self) -> &mut Vec> { - if let PathNode::Leaf(x) = self { - x - } else { - panic!("Tried to unwrap a node as a leaf."); - } - } - pub fn new() -> Self { - PathNode::Node(HashMap::new()) + PathNode { + chunks: Vec::new(), + var: None, + end: Vec::new(), + } } pub fn insert(&mut self, path: PathBuf, data: T) { - let mut cur = self.unwrap_node_mut(); - let path_chunks; - path_chunks = path.clone().chunks; - for chunk in path_chunks.into_iter() { - if chunk.as_bytes()[0] == b':' { - cur = cur - .entry(PathChunk::CatchAll) - .or_default() - .unwrap_node_mut(); - continue; - } - cur = cur - .entry(PathChunk::Chunk(chunk)) - .or_default() - .unwrap_node_mut(); - } - cur.entry(PathChunk::End) - .or_insert_with(|| PathNode::Leaf(vec![])) - .unwrap_leaf_mut() - .push(PathData { - orig_path: path, - data, - }); - } - - fn dfs(&self, chunks: &[String]) -> Vec<&PathData> { - let cur = self.unwrap_node(); - if chunks.is_empty() { - return cur - .get(&PathChunk::End) - .map(|v| v.unwrap_leaf().iter().collect()) - .unwrap_or_default(); - } - let mut ret = vec![]; - if let Some(v) = cur.get(&PathChunk::Chunk(chunks[0].clone())) { - let found = v.dfs(&chunks[1..]); - ret = found; - } - #[cfg(feature = "url_variables")] - { - if let Some(v) = cur.get(&PathChunk::CatchAll) { - let found = v.dfs(&chunks[1..]); - ret.extend(found); - } + let mut cur = self; + for chunk in path.chunks.iter() { + // segments are never empty, `PathBuf::parse` drops those + cur = if chunk.as_bytes()[0] == b':' { + &mut **cur.var.get_or_insert_with(|| Box::new(PathNode::new())) + } else { + let at = match cur + .chunks + .binary_search_by(|(key, _)| key.as_str().cmp(chunk)) + { + Ok(at) => at, + Err(at) => { + cur.chunks.insert(at, (chunk.clone(), PathNode::new())); + at + } + }; + &mut cur.chunks[at].1 + }; } - ret - } - - pub fn get<'a>(&'a self, path: &'a PathBuf) -> Vec> { - let matched = self.dfs(path.chunks.as_slice()); - matched - .into_iter() - .map(|data| MatchedPath { - #[cfg(feature = "url_variables")] - vars: data - .orig_path - .check_matches(path) - .unwrap() - .into_iter() - .map(|(k, v)| (k, v)) - .collect(), - data: &data.data, - }) - .collect() - } -} - -// nothing walks a trie by reference yet; `Router.paths` used to be public -#[allow(dead_code)] -pub struct PathNodeIterator<'a, T> { - stack: Vec>>, - curvec: Option<&'a Vec>>, - curind: usize, -} - -pub struct OwnedPathNodeIterator -where - F: FnMut((PathChunk, PathNode)) -> PathNode, -{ - stack: Vec>, F>>, - curvec: Option>>, -} - -impl<'a, T> Iterator for PathNodeIterator<'a, T> { - type Item = &'a PathData; - - fn next(&mut self) -> Option { - // loop due to lack of tail recursive optimizations - loop { - if let Some(v) = self.curvec { - if self.curind < v.len() { - let ret = &v[self.curind]; - self.curind += 1; - return Some(ret); + cur.end.push(PathData { + orig_path: path, + data, + }); + } + + /// Hand everything inserted at a path that matches `path` to `found`, + /// with the url variables of the match + pub(crate) fn find<'a, F>(&'a self, path: &'a PathBuf, found: &mut F) + where + F: FnMut(MatchedPath<'a, T>), + { + self.find_from(&path.chunks, path, found); + } + + fn find_from<'a, F>(&'a self, rest: &[String], path: &'a PathBuf, found: &mut F) + where + F: FnMut(MatchedPath<'a, T>), + { + match rest.split_first() { + None => { + for data in &self.end { + found(MatchedPath { + #[cfg(feature = "url_variables")] + vars: data.orig_path.check_matches(path).unwrap_or_default(), + data: &data.data, + }); } } - self.curind = 0; - self.curvec = None; - if self.stack.is_empty() { - return None; - } - let len = self.stack.len(); - if let Some(v) = self.stack[len - 1].next() { - // using continue as tail recursion - match v { - PathNode::Node(n) => { - self.stack.push(n.values()); - continue; - } - PathNode::Leaf(l) => { - self.curvec = Some(l); - continue; + Some((first, rest)) => { + let at = self + .chunks + .binary_search_by(|(key, _)| key.as_str().cmp(first)); + if let Ok(at) = at { + self.chunks[at].1.find_from(rest, path, found); + } + #[cfg(feature = "url_variables")] + { + if let Some(node) = &self.var { + node.find_from(rest, path, found); } } - } else { - self.stack.pop(); - continue; } } } -} -impl PathNode { - #[allow(dead_code)] - pub fn iter(&self) -> PathNodeIterator<'_, T> { - match self { - PathNode::Node(n) => PathNodeIterator { - stack: vec![n.values()], - curvec: None, - curind: 0, - }, - PathNode::Leaf(l) => PathNodeIterator { - stack: vec![], - curvec: Some(l), - curind: 0, - }, - } + /// Whether anything was inserted at a path that matches `path` + pub(crate) fn matches(&self, path: &PathBuf) -> bool { + let mut found = false; + self.find(path, &mut |_| found = true); + found } -} -impl Iterator for OwnedPathNodeIterator)) -> PathNode> { - type Item = PathData; + /// Everything inserted at a path that matches `path` + #[cfg(test)] + pub fn get<'a>(&'a self, path: &'a PathBuf) -> Vec> { + let mut matched = Vec::new(); + self.find(path, &mut |m| matched.push(m)); + matched + } - fn next(&mut self) -> Option { - // loop due to lack of tail recursive optimizations - loop { - if let Some(v) = &mut self.curvec { - if !v.is_empty() { - return v.pop(); - } - } - self.curvec = None; - if self.stack.is_empty() { - return None; - } - let len = self.stack.len(); - if let Some(v) = self.stack[len - 1].next() { - // using continue as tail recursion - match v { - PathNode::Node(n) => { - self.stack.push(n.into_iter().map(get_second)); - continue; - } - PathNode::Leaf(l) => { - self.curvec = Some(l); - continue; - } - } - } else { - self.stack.pop(); - continue; - } + // Move everything in the trie into `all` + fn drain_into(self, all: &mut Vec>) { + all.extend(self.end); + for (_, node) in self.chunks { + node.drain_into(all); + } + if let Some(node) = self.var { + node.drain_into(all); } } } impl IntoIterator for PathNode { type Item = PathData; - type IntoIter = OwnedPathNodeIterator)) -> PathNode>; + type IntoIter = std::vec::IntoIter>; fn into_iter(self) -> Self::IntoIter { - match self { - PathNode::Node(n) => OwnedPathNodeIterator { - stack: vec![n.into_iter().map(get_second)], - curvec: None, - }, - PathNode::Leaf(l) => OwnedPathNodeIterator { - stack: vec![], - curvec: Some(l), - }, - } + let mut all = Vec::new(); + self.drain_into(&mut all); + all.into_iter() } } @@ -460,10 +366,6 @@ pub fn is_ctl(c: char) -> bool { c <= '\x1f' || c == '\x7f' } -fn get_second(tup: (PathChunk, PathNode)) -> PathNode { - tup.1 -} - #[cfg(test)] mod test { use super::*; @@ -484,11 +386,11 @@ mod test { let path3 = PathBuf::parse("/asdf/test/foo").unwrap(); let path4 = PathBuf::parse("/asdf/test/bad").unwrap(); assert_eq!( - path1.check_matches(&path2).unwrap()["var"], + path1.check_matches(&path2).unwrap().get("var").unwrap(), "test".to_string() ); assert_eq!( - path1.check_matches(&path3).unwrap()["var"], + path1.check_matches(&path3).unwrap().get("var").unwrap(), "test".to_string() ); assert!(path1.check_matches(&path4).is_none()); @@ -540,10 +442,10 @@ mod test { assert!(node.get(&path5).is_empty()); let matched = node.get(&path2).remove(0); assert_eq!(*matched.data, 1); - assert_eq!(matched.vars.get("var").unwrap(), &"test"); + assert_eq!(matched.vars.get("var").unwrap(), "test"); let matched = node.get(&path3).remove(0); assert_eq!(*matched.data, 1); - assert_eq!(matched.vars.get("var").unwrap(), &"test"); + assert_eq!(matched.vars.get("var").unwrap(), "test"); let matched = node.get(&path4).remove(0); assert_eq!(*matched.data, 4); assert!(matched.vars.is_empty()); diff --git a/src/query.rs b/src/query.rs index c653f83..849c673 100644 --- a/src/query.rs +++ b/src/query.rs @@ -1,10 +1,95 @@ +use std::borrow::Cow; +#[cfg(feature = "extended_queries")] use std::collections::HashMap; -// Decode a query component the way browsers encode one (the -// application/x-www-form-urlencoded format): `+` is a space, `%XX` is the -// byte XX, and the bytes are read as UTF-8 with invalid sequences replaced by -// U+FFFD. A `%` that isn't followed by two hex digits is kept as it is. -pub fn unescape_hex(string: &str) -> String { +/// The query of a request, its `name=value` pairs in order, decoded the +/// way browsers encode them (`application/x-www-form-urlencoded`: `+` is a +/// space and `%XX` the byte XX, read as UTF-8). A pair without `=` has an +/// empty value. Names and values with nothing to decode are borrowed from +/// the request rather than copied. +/// +/// ``` +/// use octane::prelude::*; +/// +/// let mut app = Octane::new(); +/// app.get("/", route_stop!(|req, res| { +/// let query = req.get_query(); +/// res.send(query.get("q").unwrap_or("nothing asked")); +/// })); +/// ``` +#[derive(Debug, Clone, PartialEq, Eq, Default)] +pub struct Query<'a> { + pairs: Vec<(Cow<'a, str>, Cow<'a, str>)>, +} + +impl<'a> Query<'a> { + /// Parse a query string, without its leading `?` + pub fn parse(query: &'a str) -> Self { + let mut pairs = Vec::new(); + for tok in query.split('&') { + if cfg!(feature = "faithful") && tok.is_empty() { + continue; + } + let (name, value) = tok.split_once('=').unwrap_or((tok, "")); + if cfg!(feature = "faithful") && name.is_empty() && tok.contains('=') { + continue; + } + pairs.push((decode(name), decode(value))); + } + Self { pairs } + } + + /// The value of `name`, the last one when it's repeated + pub fn get(&self, name: &str) -> Option<&str> { + self.pairs + .iter() + .rev() + .find(|(key, _)| key == name) + .map(|(_, value)| value.as_ref()) + } + + /// Every value of `name`, in order + pub fn get_all<'s>(&'s self, name: &'s str) -> impl Iterator + 's { + self.pairs + .iter() + .filter(move |(key, _)| key == name) + .map(|(_, value)| value.as_ref()) + } + + /// Whether `name` is in the query + pub fn contains(&self, name: &str) -> bool { + self.pairs.iter().any(|(key, _)| key == name) + } + + /// Every pair, in order + pub fn iter(&self) -> impl Iterator { + self.pairs + .iter() + .map(|(key, value)| (key.as_ref(), value.as_ref())) + } + + /// How many pairs there are + pub fn len(&self) -> usize { + self.pairs.len() + } + + pub fn is_empty(&self) -> bool { + self.pairs.is_empty() + } +} + +/// Decode a query component, see [`Query`]. A `%` that isn't followed by +/// two hex digits is kept as it is, and invalid UTF-8 becomes U+FFFD. It is +/// borrowed when there is nothing to decode. +pub fn decode(string: &str) -> Cow<'_, str> { + if string.bytes().any(|b| b == b'%' || b == b'+') { + Cow::Owned(unescape(string)) + } else { + Cow::Borrowed(string) + } +} + +fn unescape(string: &str) -> String { let bytes = string.as_bytes(); let mut ret = Vec::with_capacity(bytes.len()); let mut i = 0; @@ -25,7 +110,8 @@ pub fn unescape_hex(string: &str) -> String { } i += 1; } - String::from_utf8_lossy(&ret).into_owned() + // valid UTF-8 keeps the buffer it was decoded into + String::from_utf8(ret).unwrap_or_else(|e| String::from_utf8_lossy(e.as_bytes()).into_owned()) } pub fn from_hex(chr: char) -> Option { @@ -45,29 +131,6 @@ pub fn from_hex(chr: char) -> Option { None } -pub fn parse_query(query: &str) -> HashMap { - let toks = query.split('&'); - let mut ret: HashMap = HashMap::new(); - for tok in toks { - if cfg!(feature = "faithful") && tok.is_empty() { - continue; - } - match tok.find('=') { - Some(v) => { - let (name, val) = tok.split_at(v); - if cfg!(feature = "faithful") && name.is_empty() { - continue; - } - ret.insert(unescape_hex(name), unescape_hex(&val[1..])); - } - None => { - ret.insert(unescape_hex(tok), "".to_owned()); - } - } - } - ret -} - #[cfg(feature = "extended_queries")] /// An parsed unit from an `extended_query`. We catch a request /// which has an extended query as the path, we call `get_extended_query()` @@ -133,7 +196,7 @@ pub fn parse_extended_query(query: &str) -> HashMap { match tok.find('=') { Some(v) => { let (name, val) = tok.split_at(v); - let unescaped_val = unescape_hex(&val[1..]); + let unescaped_val = decode(&val[1..]).into_owned(); if name.is_empty() { continue; } @@ -176,10 +239,10 @@ pub fn parse_extended_query(query: &str) -> HashMap { } } } - ret.insert(unescape_hex(name), QueryValue::Str(unescaped_val)); + ret.insert(decode(name).into_owned(), QueryValue::Str(unescaped_val)); } None => { - ret.insert(unescape_hex(tok), QueryValue::Str("".to_owned())); + ret.insert(decode(tok).into_owned(), QueryValue::Str("".to_owned())); } } } @@ -229,44 +292,44 @@ mod test { #[test] fn success_standard() { // Parsing should work as expected. - let query = parse_query("abc=def&ab=ab%20cd%4A"); - assert_eq!(query["abc"], "def"); - assert_eq!(query["ab"], "ab cdJ"); + let query = Query::parse("abc=def&ab=ab%20cd%4A"); + assert_eq!(query.get("abc").unwrap(), "def"); + assert_eq!(query.get("ab").unwrap(), "ab cdJ"); } #[test] fn success_blank_query() { // Queries without an "=" should be blank. - let query = parse_query("a&abc=def"); - assert_eq!(query["a"], ""); - assert_eq!(query["abc"], "def"); + let query = Query::parse("a&abc=def"); + assert_eq!(query.get("a").unwrap(), ""); + assert_eq!(query.get("abc").unwrap(), "def"); } #[test] fn success_weird_hex() { // Queries should be able to handle weird hex escapes. - let query = parse_query("a=%0%41&b=%41%0&c=%%41&d=A%5"); - assert_eq!(query["a"], "%0A"); - assert_eq!(query["b"], "A%0"); - assert_eq!(query["c"], "%A"); - assert_eq!(query["d"], "A%5"); + let query = Query::parse("a=%0%41&b=%41%0&c=%%41&d=A%5"); + assert_eq!(query.get("a").unwrap(), "%0A"); + assert_eq!(query.get("b").unwrap(), "A%0"); + assert_eq!(query.get("c").unwrap(), "%A"); + assert_eq!(query.get("d").unwrap(), "A%5"); } #[test] fn success_utf8_and_plus() { // escapes are UTF-8 bytes and `+` is a space, like browsers send them - let query = parse_query("q=caf%C3%A9+au+lait&p=%2B1&bad=%FF"); - assert_eq!(query["q"], "café au lait"); - assert_eq!(query["p"], "+1"); - assert_eq!(query["bad"], "\u{FFFD}"); + let query = Query::parse("q=caf%C3%A9+au+lait&p=%2B1&bad=%FF"); + assert_eq!(query.get("q").unwrap(), "café au lait"); + assert_eq!(query.get("p").unwrap(), "+1"); + assert_eq!(query.get("bad").unwrap(), "\u{FFFD}"); } #[test] #[cfg_attr(not(feature = "faithful"), ignore)] fn success_no_name() { // Queries with no name should not insert anything into the hashmap. - assert_eq!(parse_query("").len(), 0); - assert_eq!(parse_query("=x").len(), 0); - assert_eq!(parse_query("=x&=y").len(), 0); + assert_eq!(Query::parse("").len(), 0); + assert_eq!(Query::parse("=x").len(), 0); + assert_eq!(Query::parse("=x&=y").len(), 0); } } diff --git a/src/request.rs b/src/request.rs index 371d546..e9c96ff 100644 --- a/src/request.rs +++ b/src/request.rs @@ -1,22 +1,37 @@ use crate::constants::*; #[cfg(feature = "cookies")] use crate::cookie::Cookie; -use crate::deref; -use crate::path::is_ctl; use crate::path::PathBuf; -use crate::query::parse_query; +#[cfg(feature = "url_variables")] +pub use crate::path::Vars; +pub use crate::query::Query; #[cfg(feature = "extended_queries")] use crate::query::{parse_extended_query, QueryValue}; -use crate::util::Spliterator; +use crate::util::read_some; +use octane_http::http1x::{find_in_slice, MAX_HEAD_SIZE}; use octane_http::{HttpVersion, StatusCode}; use std::cfg; +#[cfg(feature = "extended_queries")] use std::collections::HashMap; #[cfg(not(feature = "raw_headers"))] use std::marker::PhantomData; +use std::ops::Range; use std::str; -use std::string::ToString; use tokio::io::{AsyncRead, AsyncReadExt}; +// The longest chunk-size line (the size and its extensions) a chunked body +// may have +const MAX_CHUNK_LINE: usize = 4096; + +/// How the end of a request body is found (RFC 9112 section 6.3) +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub(crate) enum BodyFraming { + /// `Content-Length` bytes, 0 when there is no body + Length(usize), + /// `Transfer-Encoding: chunked` + Chunked, +} + pub use crate::method::RequestMethod; /// The RequestLine struct represents the first @@ -121,163 +136,137 @@ pub(crate) fn request_line_parse(method: &[u8], url: &[u8], version: &[u8]) -> O } } -/// The header structure represents a parsed value -/// of unit header that looks like `key: value` -/// and holds both the key and value. You -/// shouldn't use it directly as the parsing has -/// been done for you and all the headers are -/// available in the `Headers` struct which you -/// get as a field in the `req` variable in -/// closures -#[derive(Debug, PartialEq, Eq, Clone)] -pub struct Header { - name: String, - value: String, +/// One header field of a request, `name: value`, borrowed from the request. +/// The name is as the client sent it, the value has the whitespace around +/// it removed. +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub struct Header<'a> { + name: &'a str, + value: &'a str, } -impl Header { - /// Parses a `key: value` header string and - /// returns a Header struct - pub(crate) fn parse(header: impl AsRef) -> Option { - let header = header.as_ref(); - let mut toks = header.splitn(2, ':'); - let name = toks.next()?; - if name.is_empty() { - return None; - } - if cfg!(feature = "faithful") && !is_token(name) { - return None; - } - // whitespace around a field value isn't part of it, and HTAB is the - // only control character allowed inside one - let value = toks.next()?.trim_matches(|c| c == SP || c == HT); - if cfg!(feature = "faithful") && value.chars().any(|c| is_ctl(c) && c != HT) { - return None; - } - Some(Self { - name: name.to_owned(), - value: value.to_owned(), - }) - } - /// Return the name of the header - pub fn name(&self) -> String { - self.name.to_string() +impl<'a> Header<'a> { + /// The name of the header, as it was sent + pub fn name(&self) -> &'a str { + self.name } - /// Return the value of the header - pub fn value(&self) -> String { - self.value.to_string() + /// The value of the header + pub fn value(&self) -> &'a str { + self.value } } -/// The `Headers` struct holds _all_ the headers -/// a request might have in raw form (if the -/// feature is enabled) and in a HashMap with key -/// and value of the type `String` +/// The headers of a request, a view of the request head: nothing is copied +/// out of it. Names are matched without regard to case (RFC 9110 section +/// 5.1), and are kept as the client sent them. /// /// # Example /// /// ``` /// use octane::prelude::*; -/// use octane::request::RequestMethod; /// /// let mut app = Octane::new(); /// app.get("/", /// route!(|req, res| { -/// let some_header = req.headers.get("HeaderName"); +/// let agent = req.headers.get("User-Agent").unwrap_or("unknown"); +/// res.send(agent); /// Flow::Stop /// }), /// ); /// ``` -#[derive(Debug, PartialEq, Eq, Clone)] -pub struct Headers { - /// Headers, serialized to a HashMap - pub parsed: HashMap, - #[cfg(feature = "raw_headers")] - /// Contains headers in raw form - pub raw: Vec
, - #[cfg(not(feature = "raw_headers"))] - raw: PhantomData<()>, - // how many Host fields were sent, they are joined in `parsed` so a - // duplicate can't be told apart from a single comma separated value - host_count: usize, +#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)] +pub struct Headers<'a> { + // every line was checked by `parse`, joined by CRLF, empty for none + block: &'a str, } -impl Headers { - /// Parse all the headers on a request - pub(crate) fn parse(request: impl AsRef) -> Option { - let request = request.as_ref(); - let mut toks = Spliterator::new(request.as_bytes(), B_CRLF); - // an empty block is a request without any header lines - toks.finished = request.is_empty(); - let mut headers: HashMap = HashMap::new(); - #[cfg(feature = "raw_headers")] - let mut raw_headers: Vec
= Vec::new(); - let mut host_count = 0; - for tok in toks { - let parsed = Header::parse(str::from_utf8(tok).ok()?)?; - if parsed.name.eq_ignore_ascii_case("host") { - host_count += 1; - } - #[cfg(feature = "raw_headers")] - { - let name = parsed.name.to_ascii_lowercase(); - // cookie pairs are joined with "; " (RFC 6265 section 5.4) - let sep = if name == "cookie" { "; " } else { ", " }; - headers - .entry(name) - .and_modify(|v| { - v.push_str(sep); - v.push_str(&parsed.value); - }) - .or_insert_with(|| parsed.value.to_owned()); - } - #[cfg(not(feature = "raw_headers"))] - { - // Without raw-header preservation, reuse the allocations made - // by `Header::parse` instead of cloning both strings. - let Header { mut name, value } = parsed; - name.make_ascii_lowercase(); - let sep = if name == "cookie" { "; " } else { ", " }; - match headers.entry(name) { - std::collections::hash_map::Entry::Occupied(mut entry) => { - entry.get_mut().push_str(sep); - entry.get_mut().push_str(&value); - } - std::collections::hash_map::Entry::Vacant(entry) => { - entry.insert(value); - } - } - } - #[cfg(feature = "raw_headers")] - raw_headers.push(parsed); - } - Some(Self { - parsed: headers, - #[cfg(feature = "raw_headers")] - raw: raw_headers, - #[cfg(not(feature = "raw_headers"))] - raw: PhantomData, - host_count, - }) +impl<'a> Headers<'a> { + /// Check a header block, the lines between the request line and the + /// empty line. Every field needs a token for a name and a value without + /// control characters but HTAB, whatever the features: a lenient check + /// is how framing headers get smuggled past a proxy. + pub(crate) fn parse(block: &'a str) -> Option { + octane_http::http1x::Headers::parse(block.as_bytes()).ok()?; + Some(Self { block }) + } + + /// Headers for a block that was already checked by [`Headers::parse`] + pub(crate) fn checked(block: &'a str) -> Self { + Self { block } + } + + /// Every header, in the order they were sent + pub fn iter(&self) -> impl Iterator> + 'a { + let block = self.block; + block + .split("\r\n") + .filter(move |_| !block.is_empty()) + .filter_map(|line| { + let (name, value) = line.split_once(':')?; + Some(Header { + name, + value: value.trim_matches(|c| c == SP || c == HT), + }) + }) + } + + /// The value of the first header named `name` + pub fn get(&self, name: &str) -> Option<&'a str> { + self.get_all(name).next() + } + + /// The values of every header named `name`, in order. A header that can + /// be repeated, like `Accept`, is one list across all of them. + pub fn get_all<'n>(&self, name: &'n str) -> impl Iterator + 'n + where + 'a: 'n, + { + self.iter() + .filter(move |header| header.name.eq_ignore_ascii_case(name)) + .map(|header| header.value) + } + + /// Whether there is a header named `name` + pub fn contains(&self, name: &str) -> bool { + self.get(name).is_some() + } + + /// How many headers there are + pub fn len(&self) -> usize { + self.iter().count() + } + + pub fn is_empty(&self) -> bool { + self.block.is_empty() } /// How many `Host` fields the request had. HTTP/1.1 requests need /// exactly one (RFC 9112 section 3.2) pub(crate) fn host_count(&self) -> usize { - self.host_count + self.get_all("host").count() + } + + /// Whether the `Connection` header lists `option`, like `close` + pub(crate) fn connection_has(&self, option: &str) -> bool { + self.get_all("connection").any(|value| { + value.split(',').any(|member| { + member + .trim_matches(|c| c == SP || c == HT) + .eq_ignore_ascii_case(option) + }) + }) } - /// The body length declared by the `Content-Length` header, 0 if it - /// isn't present (RFC 9112 section 6.3). Repeated identical values like - /// `5, 5` are accepted. A value that isn't a decimal number or values - /// that disagree give a `400`, a length over `max` gives a `413`. + /// The body length declared by `Content-Length`, 0 if it isn't present + /// (RFC 9112 section 6.3). Repeated identical values like `5, 5`, in one + /// field or several, are accepted. A value that isn't a decimal number + /// or values that disagree give a `400`, a length over `max` a `413`. pub(crate) fn content_length(&self, max: usize) -> Result { - let value = match self.get("content-length") { - Some(value) => value, - None => return Ok(0), - }; let mut digits: Option<&str> = None; - for member in value.split(',') { + for member in self + .get_all("content-length") + .flat_map(|value| value.split(',')) + { let member = member.trim_matches(|c| c == SP || c == HT); if member.is_empty() || !member.bytes().all(|b| b.is_ascii_digit()) { return Err(StatusCode::BadRequest); @@ -299,41 +288,46 @@ impl Headers { Ok(length) } - /// Build `Headers` from already-decoded name/value pairs, as produced by - /// the HTTP/2 HPACK decoder. Names are lowercased and duplicates are joined - /// (with `; ` for `cookie` per RFC 9113 §8.2.3, `, ` otherwise); no header - /// block is re-serialized. - pub(crate) fn from_pairs(pairs: &[(String, String)]) -> Self { - let mut parsed: HashMap = HashMap::new(); - #[cfg(feature = "raw_headers")] - let mut raw: Vec
= Vec::new(); - let mut host_count = 0; - for (name, value) in pairs { - let lower = name.to_ascii_lowercase(); - if lower == "host" { - host_count += 1; - } - let sep = if lower == "cookie" { "; " } else { ", " }; - parsed - .entry(lower) - .and_modify(|v| { - v.push_str(sep); - v.push_str(value); - }) - .or_insert_with(|| value.clone()); - #[cfg(feature = "raw_headers")] - raw.push(Header { - name: name.clone(), - value: value.clone(), - }); + /// How the body of a request with these headers is delimited (RFC 9112 + /// section 6.3). `chunked` is the only transfer coding supported, and it + /// has to be the only one: other codings give a `501`, and anything that + /// leaves the length ambiguous gives a `400`, after which the connection + /// must not be reused. + pub(crate) fn body_framing( + &self, + version: HttpVersion, + max: usize, + ) -> Result { + if !self.contains("transfer-encoding") { + return self.content_length(max).map(BodyFraming::Length); + } + // Both headers are how requests get smuggled past a proxy that + // reads the other one (RFC 9112 section 6.1), and HTTP/1.0 has no + // transfer codings, so its framing can't be trusted either + if self.contains("content-length") || version != HttpVersion::Http11 { + return Err(StatusCode::BadRequest); } - Self { - parsed, - #[cfg(feature = "raw_headers")] - raw, - #[cfg(not(feature = "raw_headers"))] - raw: PhantomData, - host_count, + // the codings of every Transfer-Encoding field, in order + let mut codings = self + .get_all("transfer-encoding") + .flat_map(|value| value.split(',')) + .map(|coding| coding.trim_matches(|c| c == SP || c == HT)); + let first = codings.next(); + // the last coding, and the first when there are more + let (last, first) = match codings.last() { + Some(last) => (Some(last), first), + None => (first, None), + }; + // chunked has to come last or the body has no end, and can't be + // applied twice + match last { + Some(last) if last.eq_ignore_ascii_case("chunked") => {} + _ => return Err(StatusCode::BadRequest), + } + match first { + None => Ok(BodyFraming::Chunked), + Some(coding) if coding.eq_ignore_ascii_case("chunked") => Err(StatusCode::BadRequest), + Some(_) => Err(StatusCode::NotImplemented), } } } @@ -369,7 +363,7 @@ pub struct Request<'a> { /// request method, request_uri. pub request_line: RequestLine, /// Headers in the request - pub headers: Headers, + pub headers: Headers<'a>, /// The body of the request pub body: &'a [u8], #[cfg(feature = "cookies")] @@ -382,16 +376,12 @@ impl<'a> Request<'a> { /// and body and return a Request struct pub(crate) fn parse( request_line: RequestLine, - headers: Headers, + headers: Headers<'a>, body: &'a [u8], ) -> Option { + // borrowed from the head like the headers, from every Cookie field #[cfg(feature = "cookies")] - let cookies: Vec> = headers.get("cookie").map_or_else(Vec::new, |value| { - Cookie::parse(value) - .into_iter() - .map(|cookie| Cookie::new(cookie.name().to_owned(), cookie.value().to_owned())) - .collect() - }); + let cookies = headers.get_all("cookie").flat_map(Cookie::parse).collect(); Some(Self { request_line, headers, @@ -400,51 +390,62 @@ impl<'a> Request<'a> { body, }) } - /// Build a [`Request`] from an already-parsed request line and header set, - /// reading any body bytes that weren't already buffered alongside the head. + /// Build a [`Request`] from an already-parsed request line, reading any + /// body bytes that weren't already buffered alongside the head. /// - /// `body_start` is the offset in `data` immediately after the request head. - /// If `Content-Length` calls for more bytes than are already buffered, the - /// rest is streamed off `reader` into that same allocation. Bodies longer - /// than `max_body_size` are refused with a `413` before anything is read. + /// `head` is where the header block is in `data`, already checked by + /// [`Headers::parse`], and `framing` how its body is delimited. The + /// headers are a view of `data`, made once the body is read: reading it + /// may move the buffer. `body_start` is the offset in `data` immediately + /// after the request head. Whatever of the body wasn't buffered yet is + /// streamed off `reader` into that same allocation. A chunked body is + /// decoded in place, so either way the body ends up as + /// `data[body_start..end]` followed by any bytes that were sent after it + /// (the next pipelined request). Returns the request and `end`. Bodies + /// longer than `max_body_size` are refused with a `413`. + #[allow(clippy::too_many_arguments)] pub(crate) async fn from_raw( - headers: Headers, + head: Range, + framing: BodyFraming, request_line: RequestLine, data: &'a mut Vec, body_start: usize, mut reader: T, max_body_size: usize, - ) -> Result, StatusCode> { - let body_len = headers.content_length(max_body_size)?; - let buffered_body_len = data - .len() - .checked_sub(body_start) - .ok_or(StatusCode::BadRequest)?; - let body_end = body_start - .checked_add(body_len) - .ok_or(StatusCode::PayloadTooLarge)?; - - if body_len > buffered_body_len { - // grow the buffer as bytes arrive instead of allocating the - // declared length up front - let missing = (body_len - buffered_body_len) as u64; - (&mut reader) - .take(missing) - .read_to_end(data) - .await - .map_err(|_| StatusCode::BadRequest)?; - // the client hung up before sending the whole body - if data.len() != body_end { - return Err(StatusCode::BadRequest); - } + ) -> Result<(Request<'a>, usize), StatusCode> { + if body_start > data.len() || head.end > body_start { + return Err(StatusCode::BadRequest); } + let body_end = match framing { + BodyFraming::Length(body_len) => { + read_length(data, body_start, body_len, &mut reader).await? + } + BodyFraming::Chunked => { + read_chunked(data, body_start, max_body_size, &mut reader).await? + } + }; - // Bytes already buffered or sent after the declared length are not - // part of this request body. + // nothing is read into the buffer from here on + let data: &'a Vec = data; + let block = str::from_utf8(&data[head]).map_err(|_| StatusCode::BadRequest)?; + // Bytes already buffered or sent after the body are not part of it let body = data .get(body_start..body_end) .ok_or(StatusCode::BadRequest)?; - Self::parse(request_line, headers, body).ok_or(StatusCode::BadRequest) + let request = Self::parse(request_line, Headers::checked(block), body) + .ok_or(StatusCode::BadRequest)?; + Ok((request, body_end)) + } + + /// Whether the connection stays open after this request: HTTP/1.1 + /// unless `Connection: close`, HTTP/1.0 only with `Connection: keep-alive` + /// (RFC 9112 section 9.3) + pub(crate) fn keep_alive(&self) -> bool { + match self.request_line.version { + HttpVersion::Http11 => !self.headers.connection_has("close"), + HttpVersion::Http10 => self.headers.connection_has("keep-alive"), + _ => false, + } } /// Get the first cookie with the given name. When several cookies share @@ -466,8 +467,7 @@ impl<'a> Request<'a> { self.cookies.iter().find(|cookie| cookie.name() == name) } - /// Parse the query and return the key value pairs in the form - /// of an HashMap + /// Parse the query into its decoded `name=value` pairs, see [`Query`] /// /// ``` /// use octane::prelude::*; @@ -479,8 +479,8 @@ impl<'a> Request<'a> { /// let value = parsed_query.get("value"); /// })); /// ``` - pub fn get_query(&self) -> HashMap { - parse_query(&self.request_line.query) + pub fn get_query(&self) -> Query<'_> { + Query::parse(&self.request_line.query) } /// Parse the extended query and return the key value pairs in the form @@ -491,6 +491,182 @@ impl<'a> Request<'a> { } } +// Read a `Content-Length` body starting at `start`, returning where it ends +async fn read_length( + data: &mut Vec, + start: usize, + len: usize, + reader: &mut R, +) -> Result { + let end = start.checked_add(len).ok_or(StatusCode::PayloadTooLarge)?; + if end > data.len() { + // grow the buffer as bytes arrive instead of allocating the + // declared length up front + let missing = (end - data.len()) as u64; + reader + .take(missing) + .read_to_end(data) + .await + .map_err(|_| StatusCode::BadRequest)?; + // the client hung up before sending the whole body + if data.len() != end { + return Err(StatusCode::BadRequest); + } + } + Ok(end) +} + +// Decode a chunked body (RFC 9112 section 7.1) starting at `start`, in place, +// returning where the decoded body ends. +// +// `data[start..end]` holds the body decoded so far and `data[pos..]` the bytes +// not decoded yet. Chunk data is moved down over the framing as it is read, +// which is never more bytes than were read, so decoding costs one copy of the +// body whatever the chunk sizes. Before reading more, the framing between +// `end` and `pos` is dropped, so the buffer holds the decoded body and at most +// one read of framing. +async fn read_chunked( + data: &mut Vec, + start: usize, + max_body_size: usize, + reader: &mut R, +) -> Result { + let mut end = start; + let mut pos = start; + loop { + let line = read_line(data, end, &mut pos, MAX_CHUNK_LINE, reader).await?; + let size = chunk_size(&data[line.0..line.1])?; + if size == 0 { + break; + } + if size > max_body_size - (end - start) { + return Err(StatusCode::PayloadTooLarge); + } + let mut left = size; + while left > 0 { + if pos == data.len() { + fill(data, end, &mut pos, reader).await?; + } + let n = left.min(data.len() - pos); + data.copy_within(pos..pos + n, end); + end += n; + pos += n; + left -= n; + } + // chunk data is followed by CRLF + while data.len() - pos < 2 { + fill(data, end, &mut pos, reader).await?; + } + if data[pos..pos + 2] != *B_CRLF { + return Err(StatusCode::BadRequest); + } + pos += 2; + } + // The trailer section ends at an empty line. Trailer fields are checked, + // but not merged into the headers (RFC 9112 section 7.1.2). + let mut trailer_len = 0; + loop { + let (from, to) = read_line(data, end, &mut pos, MAX_HEAD_SIZE, reader).await?; + if from == to { + break; + } + trailer_len += to - from + B_CRLF.len(); + if trailer_len > MAX_HEAD_SIZE { + return Err(StatusCode::RequestHeaderFieldsTooLarge); + } + if !is_trailer_field(&data[from..to]) { + return Err(StatusCode::BadRequest); + } + } + // what follows the body is the next request + data.drain(end..pos); + Ok(end) +} + +// Find the line at `pos`, reading more until its CRLF arrives, and move `pos` +// past it. Returns where the line starts and ends, without the CRLF. +async fn read_line( + data: &mut Vec, + end: usize, + pos: &mut usize, + max: usize, + reader: &mut R, +) -> Result<(usize, usize), StatusCode> { + loop { + if let Some(len) = find_in_slice(&data[*pos..], B_CRLF) { + if len > max { + return Err(StatusCode::BadRequest); + } + let line = (*pos, *pos + len); + *pos += len + B_CRLF.len(); + return Ok(line); + } + if data.len() - *pos > max { + return Err(StatusCode::BadRequest); + } + fill(data, end, pos, reader).await?; + } +} + +// Drop the framing already decoded and read more of the body +async fn fill( + data: &mut Vec, + end: usize, + pos: &mut usize, + reader: &mut R, +) -> Result<(), StatusCode> { + data.drain(end..*pos); + *pos = end; + match read_some(reader, data).await { + Ok(0) | Err(_) => Err(StatusCode::BadRequest), + Ok(_) => Ok(()), + } +} + +// Whether `line` is a field line: a token name, a colon and a value with no +// control character but HTAB (RFC 9110 section 5). This is checked strictly +// whatever the features: a lenient check lets the bare LF in `0\r\n\n` start a +// "trailer" that swallows the request a proxy sends next, which smuggles the +// one hidden in that request's body (GHSA-2q7f-mf9f-5qhr in actix-http). +fn is_trailer_field(line: &[u8]) -> bool { + let colon = match line.iter().position(|&b| b == b':') { + Some(colon) => colon, + None => return false, + }; + let (name, value) = (&line[..colon], &line[colon + 1..]); + str::from_utf8(name).map_or(false, is_token) + && value + .iter() + .all(|&b| b == b'\t' || (b >= b' ' && b != 0x7f)) +} + +// Parse `chunk-size [ chunk-ext ]`. Extensions are allowed but ignored. +fn chunk_size(line: &[u8]) -> Result { + let (size, ext) = match line.iter().position(|&b| b == b';') { + Some(at) => line.split_at(at), + None => (line, &[][..]), + }; + // whitespace may come before the extensions (BWS), never before the size + let size = match size.iter().rposition(|&b| b != b' ' && b != b'\t') { + Some(last) => &size[..=last], + None => return Err(StatusCode::BadRequest), + }; + if !size.iter().all(u8::is_ascii_hexdigit) { + return Err(StatusCode::BadRequest); + } + // HTAB is the only control character an extension may hold + if ext.iter().any(|&b| (b < b' ' && b != b'\t') || b == 0x7f) { + return Err(StatusCode::BadRequest); + } + size.iter().try_fold(0usize, |n, &b| { + let digit = (b as char).to_digit(16).unwrap_or(0) as usize; + n.checked_mul(16) + .and_then(|n| n.checked_add(digit)) + // too large for any body that could be accepted + .ok_or(StatusCode::PayloadTooLarge) + }) +} + /// The KeepAlive struct represents the value /// parsed in the KeepAlive header. It holds the /// timeout and max duration as a u64, (only http 1.0 and below) @@ -566,7 +742,7 @@ impl KeepAlive { #[derive(Debug, PartialEq, Eq, Clone)] pub struct MatchedRequest<'a> { /// The request coming from the client - pub request: Request<'a>, + pub request: &'a Request<'a>, #[cfg(feature = "url_variables")] /// A Hashmap containing the variables specified in the url with their /// respective keys. @@ -584,11 +760,16 @@ pub struct MatchedRequest<'a> { /// }), /// ); /// ``` - pub vars: HashMap<&'a str, &'a str>, + pub vars: Vars<'a>, } -deref!(MatchedRequest<'a>, Request<'a>, request); -deref!(Headers, HashMap, parsed); +impl<'a> std::ops::Deref for MatchedRequest<'a> { + type Target = Request<'a>; + + fn deref(&self) -> &Self::Target { + self.request + } +} #[cfg(test)] mod test { @@ -682,8 +863,7 @@ mod test { User-Agent: curl/7.58.0\r\n\ Accept: */*\r\n\ Content-Length: 20\r\n\ - Content-Type: application/x-www-form-urlencoded" - .to_string(), + Content-Type: application/x-www-form-urlencoded", ) .unwrap(); assert_eq!(headers.get("host").unwrap(), "localhost:12345"); @@ -697,57 +877,54 @@ mod test { } #[test] - #[cfg(feature = "raw_headers")] - fn success_raw_headers() { - // Parsing should work as expected. - let headers = Headers::parse( - "HOst: localhost:12345\r\n\ - User-Agent: curl/7.58.0" - .to_string(), - ) - .unwrap(); - assert_eq!(headers.raw[0].name(), "HOst"); - assert_eq!(headers.raw[0].value(), "localhost:12345"); - assert_eq!(headers.raw[1].name(), "User-Agent"); - assert_eq!(headers.raw[1].value(), "curl/7.58.0"); + fn names_are_kept_as_sent_and_matched_without_case() { + let headers = Headers::parse("HOst: localhost:12345\r\nUser-Agent: curl/7.58.0").unwrap(); + let fields: Vec<_> = headers.iter().map(|h| (h.name(), h.value())).collect(); + assert_eq!( + fields, + [("HOst", "localhost:12345"), ("User-Agent", "curl/7.58.0")] + ); + assert_eq!(headers.get("host"), Some("localhost:12345")); + assert_eq!(headers.get("USER-AGENT"), Some("curl/7.58.0")); + assert_eq!(headers.len(), 2); + } + + // The one field of a one line block + fn field(line: &str) -> Option<(&str, &str)> { + let headers = Headers::parse(line)?; + let header = headers.iter().next()?; + Some((header.name(), header.value())) } #[test] fn success_standard_header() { // Parsing should work as expected. - let req = Header::parse("Referer: \t\t request://www.example.com/".to_string()).unwrap(); - assert_eq!(req.name(), "Referer"); - assert_eq!(req.value(), "request://www.example.com/"); + let req = field("Referer: \t\t request://www.example.com/"); + assert_eq!(req, Some(("Referer", "request://www.example.com/"))); } #[test] fn success_empty_value() { // Empty values are allowed. - let req = Header::parse("Referer: \t\t ".to_string()).unwrap(); - assert_eq!(req.name(), "Referer"); - assert_eq!(req.value(), ""); - } - - #[test] - #[should_panic] - fn fail_no_value() { - // Having no value should fail. - Header::parse("Referer".to_string()).unwrap(); + assert_eq!(field("Referer: \t\t "), Some(("Referer", ""))); } #[test] - #[should_panic] - fn fail_empty_name() { - // Having no name should fail. - Header::parse(": test".to_string()).unwrap(); - } - - #[test] - #[should_panic] - #[cfg_attr(not(feature = "faithful"), ignore)] - fn fail_malformed_name() { - // Having separators in the name should fail. - Header::parse("Test Header: test".to_string()).unwrap(); + fn malformed_fields_are_refused_whatever_the_features() { + // no colon, no name, separators or control characters in the name, + // control characters in the value + for line in [ + "Referer", + ": test", + "Test Header: test", + "Content-Length : 5", + "X\x0b: 1", + "X: a\x00b", + ] + .iter() + { + assert!(Headers::parse(line).is_none(), "{:?}", line); + } } #[test] @@ -790,7 +967,7 @@ mod test { #[test] fn success_no_headers() { // A request may have no header lines at all. - assert!(Headers::parse(String::new()).unwrap().is_empty()); + assert!(Headers::parse("").unwrap().is_empty()); } #[test] @@ -804,8 +981,7 @@ mod test { #[test] fn success_value_whitespace_and_tabs() { // Whitespace around a value is dropped, tabs inside it are kept. - let req = Header::parse("X-Test: a\tb \t".to_string()).unwrap(); - assert_eq!(req.value(), "a\tb"); + assert_eq!(field("X-Test: a\tb \t"), Some(("X-Test", "a\tb"))); } #[test] @@ -817,18 +993,29 @@ mod test { } #[test] - fn success_cookie_headers_joined_with_semicolons() { - let headers = Headers::parse("Cookie: a=1\r\nCookie: b=2".to_string()).unwrap(); - assert_eq!(headers.get("cookie").unwrap(), "a=1; b=2"); + fn repeated_fields_are_all_kept() { + let headers = Headers::parse("Cookie: a=1\r\nX: 0\r\ncookie: b=2").unwrap(); + assert_eq!(headers.get("cookie"), Some("a=1")); + assert_eq!( + headers.get_all("Cookie").collect::>(), + ["a=1", "b=2"] + ); + #[cfg(feature = "cookies")] + { + let line = RequestLine::parse("GET / HTTP/1.1").unwrap(); + let request = Request::parse(line, headers, b"").unwrap(); + let cookies: Vec<_> = request + .cookies + .iter() + .map(|c| (c.name(), c.value())) + .collect(); + assert_eq!(cookies, [("a", "1"), ("b", "2")]); + } } #[test] fn success_content_length() { - let parse = |block: &str| { - Headers::parse(block.to_string()) - .unwrap() - .content_length(100) - }; + let parse = |block: &str| Headers::parse(block).unwrap().content_length(100); assert_eq!(parse("Host: a"), Ok(0)); assert_eq!(parse("Content-Length: 42"), Ok(42)); assert_eq!(parse("Content-Length: 007"), Ok(7)); @@ -838,11 +1025,7 @@ mod test { #[test] fn fail_content_length() { - let parse = |block: &str| { - Headers::parse(block.to_string()) - .unwrap() - .content_length(100) - }; + let parse = |block: &str| Headers::parse(block).unwrap().content_length(100); let bad = Err(StatusCode::BadRequest); assert_eq!(parse("Content-Length: 5\r\nContent-Length: 6"), bad); assert_eq!(parse("Content-Length: +5"), bad); @@ -858,15 +1041,31 @@ mod test { assert_eq!(parse("Content-Length: 99999999999999999999999"), too_large); } + // `data` as `from_raw` gets it: the header block, then whatever of the + // body was buffered with it + fn buffered(block: &str, body: &[u8]) -> (Vec, Range, usize) { + let mut data = block.as_bytes().to_vec(); + let head = 0..data.len(); + data.extend_from_slice(body); + (data, head, block.len()) + } + + fn framing(block: &str, max: usize) -> Result { + Headers::parse(block) + .unwrap() + .body_framing(HttpVersion::Http11, max) + } + #[tokio::test] async fn from_raw_appends_missing_body_to_the_request_buffer() { - let mut data = Vec::with_capacity(16); - data.extend_from_slice(b"HEADhe"); - let request = Request::from_raw( - Headers::parse("Content-Length: 5").unwrap(), + let block = "Content-Length: 5"; + let (mut data, head, start) = buffered(block, b"he"); + let (request, end) = Request::from_raw( + head, + framing(block, 16).unwrap(), RequestLine::parse("POST / HTTP/1.1").unwrap(), &mut data, - 4, + start, &b"lloignored"[..], 16, ) @@ -874,20 +1073,24 @@ mod test { .unwrap(); assert_eq!(request.body, b"hello"); + assert_eq!(request.headers.get("content-length"), Some("5")); let body_pointer = request.body.as_ptr() as usize; drop(request); - assert_eq!(body_pointer, data.as_ptr() as usize + 4); - assert_eq!(data, b"HEADhello"); + assert_eq!(body_pointer, data.as_ptr() as usize + start); + assert_eq!(&data[start..], b"hello"); + assert_eq!(end, data.len()); } #[tokio::test] async fn from_raw_excludes_already_buffered_trailing_bytes() { - let mut data = b"HEADhello".to_vec(); - let request = Request::from_raw( - Headers::parse("Content-Length: 2").unwrap(), + let block = "Content-Length: 2"; + let (mut data, head, start) = buffered(block, b"hello"); + let (request, end) = Request::from_raw( + head, + framing(block, 16).unwrap(), RequestLine::parse("POST / HTTP/1.1").unwrap(), &mut data, - 4, + start, &b""[..], 16, ) @@ -896,37 +1099,221 @@ mod test { assert_eq!(request.body, b"he"); drop(request); - assert_eq!(data, b"HEADhello"); + // the rest is the next request + assert_eq!(&data[end..], b"llo"); } #[tokio::test] - async fn from_raw_rejects_oversized_and_incomplete_bodies() { - let mut oversized_data = b"HEAD".to_vec(); - let oversized = Request::from_raw( - Headers::parse("Content-Length: 6").unwrap(), - RequestLine::parse("POST / HTTP/1.1").unwrap(), - &mut oversized_data, - 4, - &b"unused"[..], - 5, - ) - .await; - assert!(matches!(oversized, Err(StatusCode::PayloadTooLarge))); - drop(oversized); - assert_eq!(oversized_data, b"HEAD"); + async fn oversized_and_incomplete_bodies_are_refused() { + // too large is known from the headers, before anything is read + assert_eq!( + framing("Content-Length: 6", 5), + Err(StatusCode::PayloadTooLarge) + ); - let mut incomplete_data = b"HEADhe".to_vec(); + let block = "Content-Length: 5"; + let (mut data, head, start) = buffered(block, b"he"); let incomplete = Request::from_raw( - Headers::parse("Content-Length: 5").unwrap(), + head, + framing(block, 5).unwrap(), RequestLine::parse("POST / HTTP/1.1").unwrap(), - &mut incomplete_data, - 4, + &mut data, + start, &b"l"[..], 5, ) .await; assert!(matches!(incomplete, Err(StatusCode::BadRequest))); drop(incomplete); - assert_eq!(incomplete_data, b"HEADhel"); + assert_eq!(&data[start..], b"hel"); + } + + // Hands out at most `step` bytes per read, like a slow client + struct Trickle<'a> { + bytes: &'a [u8], + step: usize, + } + + impl AsyncRead for Trickle<'_> { + fn poll_read( + mut self: std::pin::Pin<&mut Self>, + _: &mut std::task::Context<'_>, + buf: &mut tokio::io::ReadBuf<'_>, + ) -> std::task::Poll> { + let n = self.step.min(self.bytes.len()).min(buf.remaining()); + let (read, rest) = self.bytes.split_at(n); + buf.put_slice(read); + self.bytes = rest; + std::task::Poll::Ready(Ok(())) + } + } + + // Decode `wire` as a chunked body that arrives `step` bytes at a time + // after `buffered`, returning the body and what came after it: the bytes + // left in the buffer followed by those that weren't read at all + async fn chunked( + buffered: &[u8], + wire: &[u8], + step: usize, + max: usize, + ) -> Result<(Vec, Vec), StatusCode> { + let block = "Transfer-Encoding: chunked"; + let (mut data, head, start) = self::buffered(block, buffered); + let mut reader = Trickle { bytes: wire, step }; + let (request, end) = Request::from_raw( + head, + BodyFraming::Chunked, + RequestLine::parse("POST / HTTP/1.1").unwrap(), + &mut data, + start, + &mut reader, + max, + ) + .await?; + let body = request.body.to_vec(); + assert_eq!(request.headers.get("transfer-encoding"), Some("chunked")); + drop(request); + assert_eq!(&data[..start], block.as_bytes()); + assert_eq!(&data[start..end], &body[..]); + let mut rest = data[end..].to_vec(); + rest.extend_from_slice(reader.bytes); + Ok((body, rest)) + } + + #[tokio::test] + async fn chunked_bodies_are_decoded_whatever_the_fragmentation() { + let wire: &[u8] = b"5\r\nhello\r\n1;name=\"v\"\r\n,\r\nA \t;x\r\n world!!!!\r\n0\r\nX-Trailer: 1\r\n\r\nGET / HTTP/1.1\r\n"; + let want = (b"hello, world!!!!".to_vec(), b"GET / HTTP/1.1\r\n".to_vec()); + for step in 1..=wire.len() { + // all of it arriving later, a byte at a time and so on + assert_eq!( + chunked(b"", wire, step, 64).await, + Ok(want.clone()), + "step {}", + step + ); + } + for split in 0..=wire.len() { + // some of it buffered with the head, the rest read later + let (buffered, rest) = wire.split_at(split); + assert_eq!( + chunked(buffered, rest, 3, 64).await, + Ok(want.clone()), + "split {}", + split + ); + } + // trailer values may hold tabs and bytes over 0x7f + let trailers = b"0\r\nX-Sum: a\tb \xc3\xa9\r\nX-Empty:\r\n\r\n"; + assert_eq!( + chunked(trailers, b"", 1, 0).await, + Ok((Vec::new(), Vec::new())) + ); + // an empty body + let empty = chunked(b"0\r\n\r\n", b"", 1, 0).await; + assert_eq!(empty, Ok((Vec::new(), Vec::new()))); + } + + #[tokio::test] + async fn malformed_chunked_bodies_are_refused() { + let bad = Err(StatusCode::BadRequest); + let cases: &[&[u8]] = &[ + // no size, not hex, a sign or whitespace before it + b"\r\nhello\r\n0\r\n\r\n", + b"g\r\nhello\r\n0\r\n\r\n", + b"+5\r\nhello\r\n0\r\n\r\n", + b" 5\r\nhello\r\n0\r\n\r\n", + b"0x5\r\nhello\r\n0\r\n\r\n", + // bare LF line ends + b"5\nhello\r\n0\r\n\r\n", + b"5\r\nhello\n0\r\n\r\n", + // data longer than its size + b"5\r\nhello!\r\n0\r\n\r\n", + // a control character in an extension + b"5;a\x01\r\nhello\r\n0\r\n\r\n", + // a malformed trailer field + b"5\r\nhello\r\n0\r\nno colon\r\n\r\n", + // a bare LF after the last chunk can't start a trailer that + // swallows the next request (GHSA-2q7f-mf9f-5qhr in actix-http) + b"0\r\n\nGET /a:b HTTP/1.1\r\nHost: a\r\n\r\n", + b"0\r\n\nX-Smuggled: 1\r\n\r\n", + b"0\r\nX-Bad\x01: 1\r\n\r\n", + b"0\r\nX-Ok: a\x00b\r\n\r\n", + b"0\r\nBad Name: 1\r\n\r\n", + // the client hangs up before the end + b"5\r\nhel", + b"5\r\nhello\r\n0\r\n", + ]; + for wire in cases { + let got = chunked(b"", wire, 2, 64).await; + assert_eq!(got, bad, "{:?}", String::from_utf8_lossy(wire)); + } + // more than max_body_size, however it is split up + let too_large = Err(StatusCode::PayloadTooLarge); + assert_eq!( + chunked(b"", b"6\r\nhello!\r\n0\r\n\r\n", 4, 5).await, + too_large + ); + assert_eq!( + chunked(b"", b"3\r\nabc\r\n3\r\ndef\r\n0\r\n\r\n", 4, 5).await, + too_large + ); + let overflow = b"ffffffffffffffffffff\r\n"; + assert_eq!(chunked(b"", overflow, 4, 5).await, too_large); + } + + #[test] + fn body_framing_follows_rfc_9112() { + let framing = + |block: &str, version| Headers::parse(block).unwrap().body_framing(version, 100); + let v11 = HttpVersion::Http11; + assert_eq!(framing("", v11), Ok(BodyFraming::Length(0))); + assert_eq!( + framing("Content-Length: 5", v11), + Ok(BodyFraming::Length(5)) + ); + assert_eq!( + framing("Transfer-Encoding: chunked", v11), + Ok(BodyFraming::Chunked) + ); + assert_eq!( + framing("Transfer-Encoding: CHUNKED", v11), + Ok(BodyFraming::Chunked) + ); + let bad = Err(StatusCode::BadRequest); + // smuggling: both framings, or chunked on HTTP/1.0 + let both = "Transfer-Encoding: chunked\r\nContent-Length: 5"; + assert_eq!(framing(both, v11), bad); + let http10 = framing("Transfer-Encoding: chunked", HttpVersion::Http10); + assert_eq!(http10, bad); + // chunked not last, twice, or nothing at all + assert_eq!(framing("Transfer-Encoding: chunked, gzip", v11), bad); + assert_eq!(framing("Transfer-Encoding: chunked, chunked", v11), bad); + let repeated = "Transfer-Encoding: chunked\r\nTransfer-Encoding: chunked"; + assert_eq!(framing(repeated, v11), bad); + assert_eq!(framing("Transfer-Encoding: gzip", v11), bad); + assert_eq!(framing("Transfer-Encoding: ", v11), bad); + // codings that aren't supported + let gzip = framing("Transfer-Encoding: gzip, chunked", v11); + assert_eq!(gzip, Err(StatusCode::NotImplemented)); + } + + #[test] + fn keep_alive_follows_the_version_and_connection_header() { + let keep_alive = |line: &str, block: &str| { + Request::parse( + RequestLine::parse(line).unwrap(), + Headers::parse(block).unwrap(), + b"", + ) + .unwrap() + .keep_alive() + }; + assert!(keep_alive("GET / HTTP/1.1", "")); + assert!(keep_alive("GET / HTTP/1.1", "Connection: keep-alive")); + assert!(!keep_alive("GET / HTTP/1.1", "Connection: close")); + assert!(!keep_alive("GET / HTTP/1.1", "Connection: Upgrade, Close")); + assert!(!keep_alive("GET / HTTP/1.0", "")); + assert!(keep_alive("GET / HTTP/1.0", "Connection: Keep-Alive")); } } diff --git a/src/responder.rs b/src/responder.rs index f6f6d71..4708503 100644 --- a/src/responder.rs +++ b/src/responder.rs @@ -6,7 +6,7 @@ use crate::time::Time; use octane_http::{HttpVersion, StatusCode}; #[cfg(feature = "json")] use serde::Serialize; -use std::collections::HashMap; +use std::borrow::Cow; use std::error::Error; #[cfg(feature = "cookies")] use std::fmt; @@ -64,15 +64,19 @@ pub struct Response<'a> { pub status_code: StatusCode, /// Length of the content which will be sent as the response pub content_len: Option, - /// Http version which the response will use + /// Http version which the response will use, `1.1` when empty pub http_version: String, /// Custom headers which will be sent with the response - pub headers: HashMap, + pub headers: ResponseHeaders, /// Content-Type charset pub charset: Option, /// Cookies that will be sent with the response #[cfg(feature = "cookies")] pub cookies: Vec>, + // add `Content-Length` and `Date` to the head, unless already set + auto_headers: bool, + // `Content-Length` is the length of the body, whatever was set + framed: bool, marker: PhantomData<&'a ()>, } @@ -100,7 +104,7 @@ impl<'a> Response<'a> { /// ); /// ``` pub fn set(&mut self, key: &str, value: &str) -> &mut Self { - self.set_owned(key, value.to_owned()); + self.headers.set(key.to_owned(), value.to_owned()); self } /// Get the header value by name @@ -115,12 +119,12 @@ impl<'a> Response<'a> { /// "/", /// route!(|req, res| { /// res.send("Hello, world"); - /// assert_eq!(res.get("Content-Type"), Some(&"text/html".to_owned())); + /// assert_eq!(res.get("Content-Type"), Some("text/html")); /// Flow::Stop /// }), /// ); /// ``` - pub fn get(&mut self, field: &'static str) -> Option<&String> { + pub fn get(&self, field: &str) -> Option<&str> { self.headers.get(field) } /// Puts the given text to the body and send it @@ -155,20 +159,27 @@ impl<'a> Response<'a> { /// if no content header is sent pub fn default_headers(&mut self) -> &mut Self { if let Some(content_len) = self.content_len { - self.set_owned("Content-Length", content_len.to_string()); + self.headers.set("Content-Length", content_len.to_string()); } if let Some(date) = Time::now_http_date() { - self.set_owned("Date", date); + self.headers.set("Date", date); } - if !self.headers.contains_key("Content-Type") { - let mut format = String::from("text/html"); - if let Some(charset) = &self.charset { - write!(&mut format, " ;charset={:?}", charset) - .expect("writing a charset to a String cannot fail"); + self.default_type(); + self + } + // `Content-Type: text/html`, unless a type was set + fn default_type(&mut self) { + if !self.headers.contains("Content-Type") { + match &self.charset { + Some(charset) => { + let mut format = String::from("text/html"); + write!(&mut format, " ;charset={:?}", charset) + .expect("writing a charset to a String cannot fail"); + self.headers.set("Content-Type", format); + } + None => self.headers.set("Content-Type", "text/html"), } - self.set_owned("Content-Type", format); } - self } /// Modify the `Content-Type` header as passed /// in the argument @@ -193,7 +204,7 @@ impl<'a> Response<'a> { // TODO: // res.with_type("json") => application/json // res.with_type("application/json") => application/json - self.set("Content-Type", _type); + self.headers.set("Content-Type", _type); self } /// Consume the response and get the final formed http @@ -217,7 +228,7 @@ impl<'a> Response<'a> { /// route!( /// |req, res| { /// res.send_file("templates/index.html").expect("file not found"); - /// assert_eq!(res.get("Content-Type"), Some(&"text/html".to_owned())); + /// assert_eq!(res.get("Content-Type"), Some("text/html")); /// Flow::Stop /// } /// ), @@ -226,12 +237,11 @@ impl<'a> Response<'a> { /// ``` pub fn send_file(&mut self, file: &str) -> Result, Box> { let file = FileHandler::handle_file(&PathBuf::from(file))?; - self.headers.insert( - "Content-Type".to_string(), - FileHandler::mime_type(file.extension), - ); + self.headers + .set("Content-Type", FileHandler::mime_type(file.extension)); let len = file.meta.len() as usize; self.content_len = Some(len); + self.auto_headers = true; self.body = ResBody::Sized(len, Box::new(file.file) as BoxReader); Ok(Some(())) } @@ -256,7 +266,7 @@ impl<'a> Response<'a> { /// route!( /// |req, res| { /// // add example here - /// // assert_eq!(res.get("Content-Type"), Some(&"application/json".to_owned())); + /// // assert_eq!(res.get("Content-Type"), Some("application/json")); /// Flow::Stop /// } /// ), @@ -270,7 +280,7 @@ impl<'a> Response<'a> { self.body = ResBody::Bytes(body); self.content_len = Some(len); self.with_type("application/json"); - self.default_headers(); + self.auto_headers = true; Ok(()) } /// Set the status code from the status code enum @@ -445,6 +455,20 @@ impl<'a> Response<'a> { pub(crate) fn has_body(&self) -> bool { self.body.is_some() } + /// Make `Content-Length` the length of the body that will be sent. On a + /// persistent connection it is the only thing telling the client where + /// the response ends, so it can't be missing (`send_file` doesn't set + /// it) or disagree with the body. + pub(crate) fn frame(&mut self) { + let len = match &self.body { + ResBody::None => 0, + ResBody::Bytes(body) => body.len(), + ResBody::Static(body) => body.len(), + ResBody::Sized(len, _) => *len, + }; + self.content_len = Some(len); + self.framed = true; + } // Creates a new response from a slice pub(crate) fn new_from_slice>(body: T) -> Self { let body_slice = body.as_ref(); @@ -460,11 +484,13 @@ impl<'a> Response<'a> { status_code: StatusCode::Ok, body: ResBody::None, content_len: None, - http_version: "1.1".to_owned(), - headers: HashMap::new(), + http_version: String::new(), + headers: ResponseHeaders::new(), charset: None, #[cfg(feature = "cookies")] cookies: Vec::new(), + auto_headers: false, + framed: false, marker: PhantomData, } } @@ -474,11 +500,13 @@ impl<'a> Response<'a> { status_code: StatusCode::Ok, body, content_len, - http_version: "1.1".to_owned(), - headers: HashMap::new(), + http_version: String::new(), + headers: ResponseHeaders::new(), charset: None, #[cfg(feature = "cookies")] cookies: Vec::new(), + auto_headers: false, + framed: false, marker: PhantomData, } } @@ -486,75 +514,185 @@ impl<'a> Response<'a> { let len = body.len(); self.body = ResBody::Bytes(body); self.content_len = Some(len); - self.default_headers(); - } - fn set_owned(&mut self, key: &str, value: String) { - if let Some(existing) = self.headers.get_mut(key) { - *existing = value; - } else { - self.headers.insert(key.to_owned(), value); - } + self.auto_headers = true; + self.default_type(); } pub(crate) fn into_parts(self) -> (String, ResBody) { - let header_bytes = self - .headers - .iter() - .map(|(name, value)| name.len() + value.len() + 4) - .sum::(); + let mut head = Vec::with_capacity(128); + self.write_head(&mut head); + // header values are `&str` and only ASCII bytes get replaced + let head = String::from_utf8(head).expect("a response head is UTF-8"); + (head, self.body) + } + pub(crate) fn into_body(self) -> ResBody { + self.body + } + /// Whether the status allows content: 1xx, `204 No Content` and + /// `304 Not Modified` never have any (RFC 9110 section 6.4.1), so a + /// client reads the next response right after the head + pub(crate) fn status_has_body(&self) -> bool { + let (code, _) = self.status_code.fetch(); + !(code < 200 || code == 204 || code == 304) + } + /// Append the status line and headers to `out` + pub(crate) fn write_head(&self, out: &mut Vec) { let (status_code, reason_phrase) = self.status_code.fetch(); - let mut headers_str = String::with_capacity( - 32 + self.http_version.len() + reason_phrase.len() + header_bytes, - ); - headers_str.push_str("HTTP/"); - headers_str.push_str(&self.http_version); - headers_str.push(SP); - write!(&mut headers_str, "{}", status_code) - .expect("writing a status code to a String cannot fail"); - headers_str.push(SP); - headers_str.extend(reason_phrase.chars().flat_map(char::to_uppercase)); - headers_str.push_str(CRLF); + out.extend_from_slice(b"HTTP/"); + if self.http_version.is_empty() { + out.extend_from_slice(b"1.1"); + } else { + out.extend_from_slice(self.http_version.as_bytes()); + } + out.push(b' '); + push_number(out, status_code as usize); + out.push(b' '); + out.extend(reason_phrase.bytes().map(|b| b.to_ascii_uppercase())); + out.extend_from_slice(CRLF.as_bytes()); - // push normal headers - self.headers - .iter() - .for_each(|data| push_header(&mut headers_str, data.0, data.1)); + // no length either: it is forbidden for 1xx and 204, and for 304 + // could only be the length of the 200 response, which isn't known + let has_body = self.status_has_body(); + let (mut has_length, mut has_date) = (false, false); + for (name, value) in self.headers.iter() { + if name.eq_ignore_ascii_case("content-length") { + if self.framed || !has_body { + continue; + } + has_length = true; + } else if name.eq_ignore_ascii_case("date") { + has_date = true; + } + push_header(out, name, value); + } + if has_body && (self.framed || (self.auto_headers && !has_length)) { + if let Some(len) = self.content_len { + out.extend_from_slice(b"Content-Length: "); + push_number(out, len); + out.extend_from_slice(CRLF.as_bytes()); + } + } + if self.auto_headers && !has_date { + Time::with_http_date(|date| { + out.extend_from_slice(b"Date: "); + out.extend_from_slice(date.as_bytes()); + out.extend_from_slice(CRLF.as_bytes()); + }); + } // push cookies, one Set-Cookie line each, leaving out the ones // that would make an invalid header (see `Cookie::validate`) #[cfg(feature = "cookies")] self.cookies .iter() .filter_map(|cookie| cookie.serialise().ok()) - .for_each(|cookie| push_header(&mut headers_str, "Set-Cookie", &cookie)); - headers_str.push_str(CRLF); - (headers_str, self.body) + .for_each(|cookie| push_header(out, "Set-Cookie", &cookie)); + out.extend_from_slice(CRLF.as_bytes()); } } +/// The headers a response is sent with, in the order they were set. Names +/// are matched without regard to case, and setting a header replaces one of +/// the same name. Static names and values, like the ones octane sets itself, +/// aren't copied. +/// +/// ``` +/// use octane::prelude::*; +/// +/// let mut app = Octane::new(); +/// app.get("/", route_stop!(|req, res| { +/// res.headers.set("Cache-Control", "no-store"); +/// res.headers.set("X-Request-Path", req.request_line.path.to_string()); +/// res.send("ok"); +/// })); +/// ``` +#[derive(Debug, Clone, PartialEq, Eq, Default)] +pub struct ResponseHeaders { + fields: Vec<(Cow<'static, str>, Cow<'static, str>)>, +} + +impl ResponseHeaders { + pub fn new() -> Self { + Self { fields: Vec::new() } + } + /// The value of the header `name` + pub fn get(&self, name: &str) -> Option<&str> { + self.fields + .iter() + .find(|(key, _)| key.eq_ignore_ascii_case(name)) + .map(|(_, value)| value.as_ref()) + } + /// Set the header `name` to `value`, replacing it if it was set + pub fn set(&mut self, name: N, value: V) + where + N: Into>, + V: Into>, + { + let (name, value) = (name.into(), value.into()); + match self + .fields + .iter_mut() + .find(|(key, _)| key.eq_ignore_ascii_case(&name)) + { + Some(field) => *field = (name, value), + None => self.fields.push((name, value)), + } + } + /// Remove the header `name`, returning its value if it was set + pub fn remove(&mut self, name: &str) -> Option> { + let at = self + .fields + .iter() + .position(|(key, _)| key.eq_ignore_ascii_case(name))?; + Some(self.fields.remove(at).1) + } + /// Whether the header `name` is set + pub fn contains(&self, name: &str) -> bool { + self.get(name).is_some() + } + /// Every header, in the order it was first set + pub fn iter(&self) -> impl Iterator { + self.fields + .iter() + .map(|(name, value)| (name.as_ref(), value.as_ref())) + } + pub fn len(&self) -> usize { + self.fields.len() + } + pub fn is_empty(&self) -> bool { + self.fields.is_empty() + } +} + +// Push `n` in decimal +fn push_number(out: &mut Vec, mut n: usize) { + let mut digits = [0u8; 20]; + let mut at = digits.len(); + loop { + at -= 1; + digits[at] = b'0' + (n % 10) as u8; + n /= 10; + if n == 0 { + break; + } + } + out.extend_from_slice(&digits[at..]); +} + // Push a `name: value` line. A name that isn't a token is skipped and control // characters in the value, CR and LF included, are replaced by spaces, so no // value can start another header line or end the head early. -fn push_header(headers_str: &mut String, name: &str, value: &str) { +fn push_header(out: &mut Vec, name: &str, value: &str) { if !is_token(name) { return; } - headers_str.push_str(name); - headers_str.push(':'); - headers_str.push(SP); - if value - .bytes() - .any(|byte| (byte <= b'\x1f' && byte != b'\t') || byte == b'\x7f') - { - headers_str.extend(value.chars().map(|c| { - if (c <= '\x1f' && c != HT) || c == '\x7f' { - SP - } else { - c - } - })); + out.extend_from_slice(name.as_bytes()); + out.extend_from_slice(b": "); + let is_ctl = |byte: u8| (byte <= b'\x1f' && byte != b'\t') || byte == b'\x7f'; + if value.bytes().any(is_ctl) { + out.extend(value.bytes().map(|b| if is_ctl(b) { b' ' } else { b })); } else { - headers_str.push_str(value); + out.extend_from_slice(value.as_bytes()); } - headers_str.push_str(CRLF); + out.extend_from_slice(CRLF.as_bytes()); } #[cfg(feature = "cookies")] diff --git a/src/router.rs b/src/router.rs index 5875951..939c741 100644 --- a/src/router.rs +++ b/src/router.rs @@ -1,22 +1,118 @@ use crate::default; use crate::method::RequestMethod; +#[cfg(feature = "url_variables")] +use crate::path::Vars; use crate::request::{MatchedRequest, Request}; use crate::responder::Response; pub(crate) use crate::routes::RouterResult; use crate::routes::Routes; pub use crate::routes::{Flow, Route}; -#[cfg(feature = "url_variables")] -use std::collections::HashMap; +use std::future::Future; +use std::pin::Pin; -/// The Closure type is a type alias for the type -/// that the routes should return +/// A synchronous handler, made with the [`route!`](macro.route.html), +/// [`route_next!`](macro.route_next.html) and +/// [`route_stop!`](macro.route_stop.html) macros pub type Closure = Box Fn(&'a MatchedRequest, &'a mut Response) -> Flow + Send + Sync>; +/// The future an async handler returns, see [`Handler`] +pub type HandlerFuture<'a> = Pin + Send + 'a>>; + +/// What runs when a route matches. The [`Route`] methods take either kind: +/// +/// - a synchronous [`Closure`], made with the [`route!`](macro.route.html) +/// macros. It runs straight on the connection's task, so it should not +/// block. +/// - an async handler, made with the +/// [`route_async!`](macro.route_async.html) macros. It can `.await` +/// timers, I/O or other tasks without holding up the worker thread. +/// +/// ``` +/// use octane::prelude::*; +/// use std::time::Duration; +/// +/// let mut app = Octane::new(); +/// app.get("/sync", route_stop!(|req, res| res.send("now"))).unwrap(); +/// app.get( +/// "/async", +/// route_async_stop!(|req, res| { +/// tokio::time::sleep(Duration::from_millis(10)).await; +/// res.send("later"); +/// }), +/// ) +/// .unwrap(); +/// ``` +pub struct Handler(Kind); + +enum Kind { + Sync(Closure), + Async(Box), +} + +impl Handler { + // Run the handler, an async one until its future completes + pub(crate) async fn call(&self, req: &MatchedRequest<'_>, res: &mut Response<'_>) -> Flow { + match &self.0 { + Kind::Sync(closure) => closure(req, res), + Kind::Async(handler) => handler.call(req, res).await, + } + } + + /// Box an async closure for the [`Route`] methods, it's what the + /// [`route_async!`](macro.route_async.html) macros expand to. `call` + /// calls `handler` and boxes its future. It is passed in rather than + /// written here so that it is checked where the closure's type is known, + /// which is what proves that future is `Send`. + #[doc(hidden)] + pub fn from_async(handler: F, call: AsyncCall) -> Self + where + F: Send + Sync + 'static, + { + Handler(Kind::Async(Box::new(AsyncClosure { handler, call }))) + } +} + +impl From for Handler { + fn from(closure: Closure) -> Self { + Handler(Kind::Sync(closure)) + } +} + +/// How [`Handler::from_async`] calls the async closure it was given +#[doc(hidden)] +pub type AsyncCall = + for<'a, 'r, 'c> fn(&'a F, &'a MatchedRequest<'r>, &'a mut Response<'c>) -> HandlerFuture<'a>; + +// An async handler, its future may borrow from the handler itself as well as +// from the request and response, so calling it needs no reference counting +trait AsyncHandler: Send + Sync { + fn call<'a>( + &'a self, + req: &'a MatchedRequest<'_>, + res: &'a mut Response<'_>, + ) -> HandlerFuture<'a>; +} + +struct AsyncClosure { + handler: F, + call: AsyncCall, +} + +impl AsyncHandler for AsyncClosure { + fn call<'a>( + &'a self, + req: &'a MatchedRequest<'_>, + res: &'a mut Response<'_>, + ) -> HandlerFuture<'a> { + (self.call)(&self.handler, req, res) + } +} + /// The router structure defines the routes and stores them along with /// their indexes. Router methods can be used with this struct and also /// the main server structure. pub struct Router { - pub(crate) routes: Routes, + pub(crate) routes: Routes, } impl Router { @@ -33,43 +129,117 @@ impl Router { pub(crate) fn append(&mut self, router: Self) { self.routes.append(router.routes); } - // Fetch the closures matching the request path and run them in - // registration order until one returns `Flow::Stop`. - pub(crate) fn run(&self, parsed_request: Request<'_>, res: &mut Response) { - let method = parsed_request.request_line.method; - // `Matched` values borrow their input path when URL variables are - // enabled. A path-only clone lets the router move the much larger - // request (including all headers) into `MatchedRequest` unchanged. - let match_path = parsed_request.request_line.path.clone(); - let routes = self.routes.matched(method, &match_path); + /// The methods with a route for `path`, for the `Allow` header of a + /// `405 Method Not Allowed` + pub(crate) fn allowed(&self, path: &crate::path::PathBuf) -> Vec { + self.routes.allowed(path) + } + // Fetch the handlers matching the request path and run them in + // registration order until one returns `Flow::Stop`. + pub(crate) async fn run(&self, request: &Request<'_>, res: &mut Response<'_>) { + let line = &request.request_line; + let routes = self.routes.matched(line.method, &line.path); #[cfg(feature = "url_variables")] let mut matched = MatchedRequest { - request: parsed_request, - vars: HashMap::new(), + request, + vars: Vars::new(), }; #[cfg(not(feature = "url_variables"))] - let matched = MatchedRequest { - request: parsed_request, - }; + let matched = MatchedRequest { request }; for route in routes { #[cfg(feature = "url_variables")] { matched.vars = route.vars; } - if !(route.data)(&matched, res).should_continue() { + if !route.data.call(&matched, res).await.should_continue() { break; } } } } +/// Make a synchronous handler, a [`Closure`], whose body returns the +/// [`Flow`] +/// +/// # Example +/// +/// ``` +/// use octane::prelude::*; +/// +/// let mut app = Octane::new(); +/// app.get( +/// "/", +/// route!(|req, res| { +/// res.send("Hello, World"); +/// Flow::Stop +/// }), +/// ); +/// ``` #[macro_export] macro_rules! route { ( | $req : ident, $res : ident | $body : expr ) => {{ #[allow(unused_variables)] - Box::new(move |$req, $res| $body) + let closure: $crate::router::Closure = ::std::boxed::Box::new(move |$req, $res| $body); + closure + }}; +} + +/// Make an async handler, whose body can `.await` and returns the [`Flow`]. +/// The body borrows what the closure captured, so captured values are not +/// cloned per request. See [`Handler`]. +/// +/// # Example +/// +/// ``` +/// use octane::prelude::*; +/// use std::time::Duration; +/// +/// let mut app = Octane::new(); +/// app.get( +/// "/", +/// route_async!(|req, res| { +/// tokio::time::sleep(Duration::from_millis(5)).await; +/// res.send("Hello, World"); +/// Flow::Stop +/// }), +/// ); +/// ``` +#[macro_export] +macro_rules! route_async { + ( | $req : ident, $res : ident | $body : expr ) => {{ + $crate::router::Handler::from_async( + #[allow(unused_variables)] + async move |$req: &$crate::request::MatchedRequest<'_>, + $res: &mut $crate::responder::Response<'_>| + -> $crate::router::Flow { $body }, + |handler, req, res| ::std::boxed::Box::pin(handler(req, res)), + ) + }}; +} + +/// Just like [`route_async!`](macro.route_async.html) but returns +/// [`Flow::Next`](enum.Flow.html) implicitly +#[macro_export] +macro_rules! route_async_next { + ( | $req : ident, $res : ident | $body : expr ) => {{ + $crate::route_async!(|$req, $res| { + $body; + $crate::router::Flow::Next + }) + }}; +} + +/// Just like [`route_async!`](macro.route_async.html) but returns +/// [`Flow::Stop`](enum.Flow.html) implicitly +#[macro_export] +macro_rules! route_async_stop { + ( | $req : ident, $res : ident | $body : expr ) => {{ + $crate::route_async!(|$req, $res| { + $body; + $crate::router::Flow::Stop + }) }}; } @@ -132,32 +302,36 @@ macro_rules! route_stop { default!(Router); impl Route for Router { - type Handler = Closure; + type Handler = Handler; - fn head(&mut self, path: &str, closure: Closure) -> RouterResult { - self.routes.insert(RequestMethod::Head, path, closure) + fn head(&mut self, path: &str, handler: impl Into) -> RouterResult { + self.routes + .insert(RequestMethod::Head, path, handler.into()) } - fn put(&mut self, path: &str, closure: Closure) -> RouterResult { - self.routes.insert(RequestMethod::Put, path, closure) + fn put(&mut self, path: &str, handler: impl Into) -> RouterResult { + self.routes.insert(RequestMethod::Put, path, handler.into()) } - fn get(&mut self, path: &str, closure: Closure) -> RouterResult { - self.routes.insert(RequestMethod::Get, path, closure) + fn get(&mut self, path: &str, handler: impl Into) -> RouterResult { + self.routes.insert(RequestMethod::Get, path, handler.into()) } - fn delete(&mut self, path: &str, closure: Closure) -> RouterResult { - self.routes.insert(RequestMethod::Delete, path, closure) + fn delete(&mut self, path: &str, handler: impl Into) -> RouterResult { + self.routes + .insert(RequestMethod::Delete, path, handler.into()) } - fn post(&mut self, path: &str, closure: Closure) -> RouterResult { - self.routes.insert(RequestMethod::Post, path, closure) + fn post(&mut self, path: &str, handler: impl Into) -> RouterResult { + self.routes + .insert(RequestMethod::Post, path, handler.into()) } - fn patch(&mut self, path: &str, closure: Closure) -> RouterResult { - self.routes.insert(RequestMethod::Patch, path, closure) + fn patch(&mut self, path: &str, handler: impl Into) -> RouterResult { + self.routes + .insert(RequestMethod::Patch, path, handler.into()) } - fn add(&mut self, closure: Closure) -> RouterResult { - self.routes.add(closure); + fn add(&mut self, handler: impl Into) -> RouterResult { + self.routes.add(handler.into()); Ok(()) } - fn add_route(&mut self, path: &str, closure: Closure) -> RouterResult { - self.routes.insert(RequestMethod::All, path, closure) + fn add_route(&mut self, path: &str, handler: impl Into) -> RouterResult { + self.routes.insert(RequestMethod::All, path, handler.into()) } } @@ -187,7 +361,7 @@ mod test { .delete("/", route!(|req, res| { Flow::Next })) .unwrap(); // middleware with path - assert_eq!(5, router.routes.paths.len()); + assert_eq!(5, router.routes.paths_with_routes()); // middleware without paths assert_eq!(1, router.routes.middlewares.len()); } @@ -237,8 +411,7 @@ mod test { 2, first_router .routes - .paths - .get(&RequestMethod::Get) + .node(RequestMethod::Get) .unwrap() .get(&PathBuf::parse("/").unwrap()) .len() @@ -248,8 +421,7 @@ mod test { 2, first_router .routes - .paths - .get(&RequestMethod::Delete) + .node(RequestMethod::Delete) .unwrap() .get(&PathBuf::parse("/").unwrap()) .len() @@ -258,8 +430,7 @@ mod test { 2, first_router .routes - .paths - .get(&RequestMethod::Post) + .node(RequestMethod::Post) .unwrap() .get(&PathBuf::parse("/").unwrap()) .len() @@ -268,8 +439,7 @@ mod test { 2, first_router .routes - .paths - .get(&RequestMethod::Head) + .node(RequestMethod::Head) .unwrap() .get(&PathBuf::parse("/").unwrap()) .len() @@ -278,16 +448,15 @@ mod test { 2, first_router .routes - .paths - .get(&RequestMethod::Put) + .node(RequestMethod::Put) .unwrap() .get(&PathBuf::parse("/").unwrap()) .len() ); } - #[test] - fn run_merges_routes_and_middleware_in_registration_order() { + #[tokio::test] + async fn run_merges_routes_and_middleware_in_registration_order() { let seen = Arc::new(Mutex::new(Vec::new())); let mut router = Router::new(); router.add(record(Arc::clone(&seen), 0)).unwrap(); @@ -301,39 +470,78 @@ mod test { b"", ) .unwrap(); - router.run(request, &mut Response::new_empty()); + router.run(&request, &mut Response::new_empty()).await; assert_eq!(*seen.lock().unwrap(), vec![0, 1, 2, 3]); } - #[test] - #[cfg(feature = "url_variables")] - fn run_moves_route_variables_and_clears_them_for_middleware() { + #[tokio::test] + async fn async_handlers_run_in_order_with_sync_ones() { let seen = Arc::new(Mutex::new(Vec::new())); let mut router = Router::new(); - let route_seen = Arc::clone(&seen); + router.add(record(Arc::clone(&seen), 0)).unwrap(); + // the body borrows what the closure captured, across an await + let first = Arc::clone(&seen); router .get( - "/thing/:id", - Box::new(move |request, _| { - route_seen - .lock() - .unwrap() - .push(request.vars.get("id").map(|value| value.to_string())); - Flow::Next + "/", + route_async_next!(|req, res| { + tokio::task::yield_now().await; + first.lock().unwrap().push(1); }), ) .unwrap(); - let middleware_seen = Arc::clone(&seen); + router.get("/", record(Arc::clone(&seen), 2)).unwrap(); + let last = Arc::clone(&seen); router - .add(Box::new(move |request, _| { - middleware_seen - .lock() - .unwrap() - .push(request.vars.get("id").map(|value| value.to_string())); - Flow::Next - })) + .get( + "/", + route_async_stop!(|req, res| { + tokio::task::yield_now().await; + last.lock().unwrap().push(3); + res.send("done"); + }), + ) .unwrap(); + // after a Stop nothing else runs + router.add(record(Arc::clone(&seen), 4)).unwrap(); + + let request = Request::parse( + RequestLine::parse("GET / HTTP/1.1").unwrap(), + Headers::parse("").unwrap(), + b"", + ) + .unwrap(); + let mut res = Response::new_empty(); + router.run(&request, &mut res).await; + + assert_eq!(*seen.lock().unwrap(), vec![0, 1, 2, 3]); + assert!(res.has_body()); + } + + #[tokio::test] + #[cfg(feature = "url_variables")] + async fn run_moves_route_variables_and_clears_them_for_middleware() { + let seen = Arc::new(Mutex::new(Vec::new())); + let mut router = Router::new(); + let route_seen = Arc::clone(&seen); + let route: Closure = Box::new(move |request, _| { + route_seen + .lock() + .unwrap() + .push(request.vars.get("id").map(|value| value.to_string())); + Flow::Next + }); + router.get("/thing/:id", route).unwrap(); + let middleware_seen = Arc::clone(&seen); + let middleware: Closure = Box::new(move |request, _| { + middleware_seen + .lock() + .unwrap() + .push(request.vars.get("id").map(|value| value.to_string())); + Flow::Next + }); + router.add(middleware).unwrap(); let request = Request::parse( RequestLine::parse("GET /thing/42 HTTP/1.1").unwrap(), @@ -341,7 +549,7 @@ mod test { b"", ) .unwrap(); - router.run(request, &mut Response::new_empty()); + router.run(&request, &mut Response::new_empty()).await; assert_eq!(*seen.lock().unwrap(), vec![Some("42".to_owned()), None]); } diff --git a/src/routes.rs b/src/routes.rs index 12ef569..de54a3b 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -4,8 +4,9 @@ //! handler is, sync closure or async function, is up to the router. use crate::method::RequestMethod; +#[cfg(feature = "url_variables")] +use crate::path::Vars; use crate::path::{InvalidPathError, PathBuf, PathNode}; -use std::collections::HashMap; // RouterResult is the type which the app.METHOD methods return pub(crate) type RouterResult = Result<(), InvalidPathError>; @@ -71,8 +72,10 @@ impl Flow { /// struct. It's called Router /// /// The tokio [`Router`](crate::router::Router) and [`Octane`](crate::Octane) -/// take [`Closure`](crate::router::Closure)s, the WASIp3 router takes async -/// handlers: that's the [`Route::Handler`] type. +/// take a [`Handler`](crate::router::Handler), either a synchronous +/// [`Closure`](crate::router::Closure) or an async handler; the WASIp3 +/// router takes async handlers. That's the [`Route::Handler`] type, the +/// methods take anything that converts into it. /// /// TODO: Include router example here pub trait Route { @@ -82,7 +85,7 @@ pub trait Route { /// add_route() is a duplicate of add() but with a /// specified url on where it should run. /// It runs on the given path and on all types of requests - fn add_route(&mut self, path: &str, closure: Self::Handler) -> RouterResult; + fn add_route(&mut self, path: &str, handler: impl Into) -> RouterResult; /// Part of app.METHOD, runs on when the request is on the /// path given and the request method is HEAD /// @@ -102,7 +105,7 @@ pub trait Route { /// ), /// ); /// ``` - fn head(&mut self, path: &str, closure: Self::Handler) -> RouterResult; + fn head(&mut self, path: &str, handler: impl Into) -> RouterResult; /// Part of app.METHOD, runs on when the request is on the /// path given and the request method is POST /// @@ -122,7 +125,7 @@ pub trait Route { /// ), /// ); /// ``` - fn post(&mut self, path: &str, closure: Self::Handler) -> RouterResult; + fn post(&mut self, path: &str, handler: impl Into) -> RouterResult; /// Part of app.METHOD, runs on when the request is on the /// path given and the request method is GET /// @@ -142,7 +145,7 @@ pub trait Route { /// ), /// ); /// ``` - fn get(&mut self, path: &str, closure: Self::Handler) -> RouterResult; + fn get(&mut self, path: &str, handler: impl Into) -> RouterResult; /// Part of app.METHOD, runs on when the request is on the /// path given and the request method is PUT /// @@ -162,7 +165,7 @@ pub trait Route { /// ), /// ); /// ``` - fn put(&mut self, path: &str, closure: Self::Handler) -> RouterResult; + fn put(&mut self, path: &str, handler: impl Into) -> RouterResult; /// Part of app.METHOD, runs on when the request is on the /// path given and the request method is DELETE /// @@ -182,7 +185,7 @@ pub trait Route { /// ), /// ); /// ``` - fn delete(&mut self, path: &str, closure: Self::Handler) -> RouterResult; + fn delete(&mut self, path: &str, handler: impl Into) -> RouterResult; /// Part of app.METHOD, runs on when the request is on the /// path given and the request method is PATCH /// @@ -202,11 +205,11 @@ pub trait Route { /// ), /// ); /// ``` - fn patch(&mut self, path: &str, closure: Self::Handler) -> RouterResult; + fn patch(&mut self, path: &str, handler: impl Into) -> RouterResult; /// add() is like `app.use` in express, it runs on all the /// paths and all types of valid methods, the request comes /// on - fn add(&mut self, entity: Self::Handler) -> RouterResult; + fn add(&mut self, handler: impl Into) -> RouterResult; } /// A handler with the position it was registered at @@ -218,15 +221,18 @@ pub(crate) struct Indexed { /// A handler that matched a request, with the url variables of its path pub(crate) struct Matched<'a, T> { pub(crate) data: &'a T, + // where it was registered, which is the order handlers run in + index: usize, #[cfg(feature = "url_variables")] - pub(crate) vars: HashMap<&'a str, &'a str>, + pub(crate) vars: Vars<'a>, } /// Routes per method and the middleware, numbered in registration order pub(crate) struct Routes { pub(crate) counter: usize, pub(crate) middlewares: Vec>, - pub(crate) paths: HashMap>>, + // one trie per method, indexed by `RequestMethod::index` + paths: [Option>>; RequestMethod::COUNT], } impl Routes { @@ -234,16 +240,15 @@ impl Routes { Self { counter: 0, middlewares: Vec::new(), - paths: HashMap::new(), + paths: std::array::from_fn(|_| None), } } pub(crate) fn insert(&mut self, method: RequestMethod, path: &str, data: T) -> RouterResult { let path = PathBuf::parse(path)?; let index = self.next_index(); - self.paths - .entry(method) - .or_insert_with(PathNode::new) + self.paths[method.index()] + .get_or_insert_with(PathNode::new) .insert(path, Indexed { data, index }); Ok(()) } @@ -256,15 +261,15 @@ impl Routes { /// Append `other`'s routes, which run after the ones already here pub(crate) fn append(&mut self, other: Self) { let offset = self.counter; - for (method, paths) in other.paths.into_iter() { + let tries = IntoIterator::into_iter(other.paths).enumerate(); + for (method, paths) in tries.filter_map(|(i, paths)| Some((i, paths?))) { let updated_paths = paths.into_iter().map(|mut v| { v.data.index += offset; v }); - if let Some(x) = self.paths.get_mut(&method) { - x.extend(updated_paths); - } else { - self.paths.insert(method, updated_paths.collect()); + match &mut self.paths[method] { + Some(x) => x.extend(updated_paths), + none => *none = Some(updated_paths.collect()), } } self.middlewares @@ -283,54 +288,56 @@ impl Routes { method: RequestMethod, path: &'a PathBuf, ) -> Vec> { - let routes = |method| { - self.paths.get(&method).map_or_else(Vec::new, |node| { - let mut routes = node.get(path); - routes.sort_unstable_by_key(|route| route.index); - routes - }) - }; - let mut method_routes = routes(method).into_iter().peekable(); - // RequestMethod::All runs regardless of the request method - let mut all_routes = routes(RequestMethod::All).into_iter().peekable(); - let mut middlewares = self.middlewares.iter().peekable(); - let mut matched = Vec::new(); - loop { - let method_index = method_routes.peek().map_or(usize::MAX, |r| r.index); - let all_index = all_routes.peek().map_or(usize::MAX, |r| r.index); - let middleware_index = middlewares.peek().map_or(usize::MAX, |m| m.index); - if method_index == usize::MAX - && all_index == usize::MAX - && middleware_index == usize::MAX - { - break; - } - // registration indexes are globally ordered, so merging the three - // already sorted lists gives the order to run them in - let route = if method_index <= all_index && method_index <= middleware_index { - method_routes.next() - } else if all_index <= middleware_index { - all_routes.next() - } else { - let middleware = middlewares.next().unwrap(); - matched.push(Matched { - data: &middleware.data, - #[cfg(feature = "url_variables")] - vars: HashMap::new(), + let mut methods = [Some(method), Some(RequestMethod::All)]; + if method == RequestMethod::All { + methods[1] = None; + } + for method in methods.iter().flatten() { + if let Some(node) = self.node(*method) { + node.find(path, &mut |route| { + matched.push(Matched { + data: &route.data.data, + index: route.data.index, + #[cfg(feature = "url_variables")] + vars: route.vars, + }) }); - continue; - }; - let route = route.unwrap(); - matched.push(Matched { - data: &route.data.data, - #[cfg(feature = "url_variables")] - vars: route.vars, - }); + } } + matched.extend(self.middlewares.iter().map(|middleware| Matched { + data: &middleware.data, + index: middleware.index, + #[cfg(feature = "url_variables")] + vars: Vars::new(), + })); + // registration indexes are unique and globally ordered + matched.sort_unstable_by_key(|m| m.index); matched } + /// The methods with a route for `path`, in a fixed order. Routes for + /// every method aren't counted, they answer whatever the method. + pub(crate) fn allowed(&self, path: &PathBuf) -> Vec { + use RequestMethod::*; + [Get, Head, Post, Put, Delete, Patch, Options, Trace, Connect] + .iter() + .copied() + .filter(|method| self.node(*method).map_or(false, |node| node.matches(path))) + .collect() + } + + /// How many methods have routes + #[cfg(test)] + pub(crate) fn paths_with_routes(&self) -> usize { + self.paths.iter().flatten().count() + } + + /// The trie of routes for `method`, if it has any + pub(crate) fn node(&self, method: RequestMethod) -> Option<&PathNode>> { + self.paths[method.index()].as_ref() + } + fn next_index(&mut self) -> usize { let index = self.counter; self.counter += 1; @@ -385,6 +392,6 @@ mod tests { let path = PathBuf::parse("/users/42").unwrap(); let matched = routes.matched(RequestMethod::Get, &path); assert!(matched[0].vars.is_empty()); - assert_eq!(matched[1].vars.get("id"), Some(&"42")); + assert_eq!(matched[1].vars.get("id"), Some("42")); } } diff --git a/src/server.rs b/src/server.rs index 02874ff..dbd7050 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,23 +1,56 @@ use crate::config::{Config, OctaneConfig, Ssl}; +use crate::connection::Connection; +use crate::constants::B_CRLF; +use crate::default; use crate::error::Error; use crate::http::Http; -use crate::request::{Headers, Request}; +use crate::request::{BodyFraming, Headers, Request, RequestMethod}; use crate::responder::{ResBody, Response}; -use crate::router::{Closure, Route, Router, RouterResult}; +use crate::router::{Handler, Route, Router, RouterResult}; use crate::server_builder::ServerBuilder; +use crate::shutdown::{Shutdown, Watch}; use crate::tls::AsMutStream; -use crate::{declare_error, default}; +use crate::util::{read_some, Deadline}; use octane_http::http1x::Http1xReader; -use octane_http::StatusCode; +use octane_http::{HttpVersion, StatusCode}; use std::error::Error as StdError; +use std::future::{pending, poll_fn, Future}; use std::io; use std::marker::Unpin; +use std::pin::pin; use std::str; use std::sync::Arc; +use std::task::Poll; use std::time::Duration; -use tokio::io::{copy, split, AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt}; +use tokio::io::{copy, AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt}; use tokio::sync::Semaphore; +// The interim response a client sending `Expect: 100-continue` waits for +const CONTINUE: &[u8] = b"HTTP/1.1 100 Continue\r\n\r\n"; + +/// How long a closing connection keeps reading what the client still sends, +/// see [`Octane::close`] +const LINGER: Duration = Duration::from_secs(2); + +// The most a connection's read buffer keeps between requests. Past it, the +// buffer is cut down to what the next request has already sent. +const READ_BUFFER_KEPT: usize = 64 * 1024; + +// Whether a client is waiting for a `100 Continue` before sending a body +// that will be read (RFC 9110 section 10.1.1). Not when the body is going to +// be refused, or for HTTP/1.0 which can't ask for it. +fn expects_continue(headers: &Headers, version: HttpVersion, max_body_size: usize) -> bool { + version == HttpVersion::Http11 + && headers + .get("expect") + .map_or(false, |value| value.eq_ignore_ascii_case("100-continue")) + && match headers.body_framing(version, max_body_size) { + Ok(BodyFraming::Length(len)) => len > 0, + Ok(BodyFraming::Chunked) => true, + Err(_) => false, + } +} + fn checked_connection_limit(configured: usize) -> io::Result { let configured = configured.max(1); if configured > Semaphore::MAX_PERMITS { @@ -59,6 +92,7 @@ pub struct Octane { /// Some preferences which decides upon how the web server runs pub settings: OctaneConfig, router: Router, + shutdown: Arc, } impl Octane { @@ -67,6 +101,7 @@ impl Octane { Octane { settings: OctaneConfig::new(), router: Router::new(), + shutdown: Arc::new(Shutdown::new()), } } /// Appends the config of the Octane struct with a custom @@ -140,6 +175,44 @@ impl Octane { pub async fn listen(self, port: u16, exec: F) -> Result<(), Box> where F: FnOnce(), + { + self.listen_with_shutdown(port, exec, pending()).await + } + + /// Like [`listen`](#method.listen), until `signal` completes. Then the + /// server shuts down gracefully: it stops accepting connections, closes + /// the ones waiting for a request, and lets the ones answering a request + /// finish it, with `Connection: close` on the response. It returns once + /// every connection is closed. + /// + /// # Example + /// + /// ```no_run + /// use octane::Octane; + /// use tokio::sync::oneshot; + /// + /// #[tokio::main] + /// async fn main() { + /// let app = Octane::new(); + /// // send on `stop` from anywhere to shut the server down + /// let (stop, stopped) = oneshot::channel::<()>(); + /// # drop(stop); + /// app.listen_with_shutdown(8080, || {}, async { + /// let _ = stopped.await; + /// }) + /// .await + /// .expect("Cannot establish connection"); + /// } + /// ``` + pub async fn listen_with_shutdown( + self, + port: u16, + exec: F, + signal: S, + ) -> Result<(), Box> + where + F: FnOnce(), + S: Future, { let max_connections = checked_connection_limit(self.settings.max_connections)?; let server = Arc::new(self); @@ -175,120 +248,323 @@ impl Octane { }); } exec(); - let server_builder = ServerBuilder::new(port); - server_builder? - .listen( - move |stream, server| async move { Octane::serve(stream, server).await }, - server, - connections, - ) - .await?; - + let shutdown = Arc::clone(&server.shutdown); + let accepting = ServerBuilder::new(port)?.listen( + move |stream, server| async move { Octane::serve(stream, server).await }, + server, + Arc::clone(&connections), + ); + // accepting ends once the signal has started the shutdown + let mut accepting = pin!(accepting); + let mut signal = pin!(async move { + signal.await; + shutdown.start(); + }); + let mut signalled = false; + poll_fn(|cx| { + if !signalled && signal.as_mut().poll(cx).is_ready() { + signalled = true; + } + accepting.as_mut().poll(cx) + }) + .await?; + // every connection holds a permit until it's closed + let mut open = max_connections; + while open > 0 { + let batch = open.min(u32::MAX as usize); + let _ = connections.acquire_many(batch as u32).await; + open -= batch; + } Ok(()) } + /// Register a task that stops when the server shuts down + pub(crate) fn watch_shutdown(&self) -> Watch { + self.shutdown.watch() + } + async fn serve(stream_async: S, server: Arc) -> Result<(), Box> where S: AsyncRead + AsyncWrite + Unpin + AsMutStream, { Octane::serve_http1x(stream_async, server).await } - /// Read one HTTP/1.x request off `stream_async` and write its response. - /// It only needs a byte stream, so the fuzz targets can run it in memory. + /// Serve HTTP/1.x requests off `stream` until the client closes it or + /// asks for it to be closed, sends something that can't be served, or + /// leaves it idle for longer than + /// [`keep_alive`](config/struct.OctaneConfig.html#structfield.keep_alive). + /// Requests sent without waiting for the previous response (pipelining) + /// are answered in order. It only needs a byte stream, so the fuzz + /// targets can run it in memory. pub(crate) async fn serve_http1x( - stream_async: S, + stream: S, server: Arc, ) -> Result<(), Box> where S: AsyncRead + AsyncWrite + Unpin, { - let (mut reader, writer) = split(stream_async); - + let settings = &server.settings; + // responses are held until the server would wait on the client + let mut stream = Connection::new(stream); + let shutdown = server.shutdown.watch(); + // bytes read off the stream that no request has used yet, kept + // between requests so its allocation is reused let mut data = Vec::new(); - - // only http1.1x path for now :) - let (request_line, headers, buffered_body_len) = { - let raw = match Http1xReader::new(&mut reader, &mut data).await { - Ok(raw) => raw, - Err(code) => { - declare_error!(writer, code); + // Waiting for a request to start, and receiving and answering one. + // Nothing has been asked of the connection yet, so the first request + // may take as long to start as answering any request may. + let mut wait = Deadline::new(settings.connection_timeout); + let mut work = Deadline::new(settings.connection_timeout); + loop { + // A server should ignore empty lines before a request line (RFC + // 9112 section 2.2), some clients send one after a body + let blank = data + .chunks_exact(2) + .take_while(|pair| *pair == B_CRLF) + .count(); + data.drain(..blank * 2); + // wait for the next request, or for enough of it to tell it from + // an empty line + if data.is_empty() || data[..] == B_CRLF[..1] { + // the answers so far go out before the wait, which the idle + // time shouldn't count + if stream.has_pending() { + match work.run(stream.flush()).await { + Some(Ok(())) => {} + _ => return Ok(()), + } + } + let read = { + let mut read = pin!(wait.run(read_some(&mut stream, &mut data))); + poll_fn(|cx| match read.as_mut().poll(cx) { + Poll::Ready(read) => Poll::Ready(Some(read)), + // a request that already came in is still answered + Poll::Pending => shutdown.poll_started(cx).map(|()| None), + }) + .await + }; + match read { + Some(Some(Ok(read))) if read > 0 => continue, + // nothing asked of it when the server shuts down + None => { + let _ = stream.shutdown().await; + return Ok(()); + } + // idle for too long, closed or broken: nothing to answer + _ => return Ok(()), } + } + let keep_alive = match work + .run(Octane::serve_one(&mut stream, &server, &mut data)) + .await + { + Some(keep_alive) => keep_alive?, + // the request took too long to arrive or to answer + None => return Ok(()), }; + if !keep_alive { + Octane::close(&mut stream, &mut data).await; + return Ok(()); + } + wait.limit = settings.keep_alive; + // a large request leaves a large buffer behind, which an idle + // connection shouldn't hold on to + if data.capacity() > READ_BUFFER_KEPT { + data.shrink_to(0); + } + } + } + /// End a connection the server is done with, once the client has seen + /// the end of the last response. + /// + /// Our side is shut down first, which ends the stream with close_notify + /// over TLS. Then whatever the client is still sending is read and + /// thrown away until it closes too, for at most [`LINGER`]: closing a + /// socket with input left unread makes the kernel answer with a reset, + /// which can destroy the response before the client reads it. The usual + /// case is a `413` sent before an upload's body was read. + async fn close(stream: &mut Connection, data: &mut Vec) + where + S: AsyncRead + AsyncWrite + Unpin, + { + if stream.shutdown().await.is_err() { + return; + } + let deadline = tokio::time::Instant::now() + LINGER; + loop { + data.clear(); + match tokio::time::timeout_at(deadline, read_some(stream, data)).await { + Ok(Ok(read)) if read > 0 => {} + _ => return, + } + } + } + + /// Read the request at the start of `data`, answer it and remove it from + /// `data`, which then starts with whatever the client sent after it. + /// Returns whether the connection can carry another request. + async fn serve_one( + stream: &mut Connection, + server: &Octane, + data: &mut Vec, + ) -> Result> + where + S: AsyncRead + AsyncWrite + Unpin, + { + let settings = &server.settings; + // The head is a view of `data` until the body is read, which may + // move the buffer: what's needed of it is taken out first, and the + // headers are made again from where they are in `data` afterwards. + let (request_line, head, framing, buffered_body_len, expects) = { + let raw = match Http1xReader::new(&mut *stream, data).await { + Ok(raw) => raw, + Err(code) => return Octane::fail(stream, code).await, + }; + // everything buffered past the head, the body and any request + // after it let buffered_body_len = raw.body_remainder.len(); let request_line = crate::request::request_line_parse( raw.request_method, raw.request_url, raw.request_version, ); - // Headers are owned after parsing, so the raw view can end before - // the request body grows `data`. let headers = str::from_utf8(raw.headers).ok().and_then(Headers::parse); - (request_line, headers, buffered_body_len) + let (request_line, headers) = match (request_line, headers) { + (Some(request_line), Some(headers)) => (request_line, headers), + _ => return Octane::fail(stream, StatusCode::BadRequest).await, + }; + // where the header block is in `data`: right after the request + // line, `METHOD SP target SP version CRLF`, at its start + let start = + raw.request_method.len() + raw.request_url.len() + raw.request_version.len() + 4; + let head = start..start + raw.headers.len(); + let version = request_line.version; + let framing = headers.body_framing(version, settings.max_body_size); + let expects = buffered_body_len == 0 + && expects_continue(&headers, version, settings.max_body_size); + (request_line, head, framing, buffered_body_len, expects) }; // The raw view has ended, so `data` can be borrowed mutably again. let body_start = data.len() - buffered_body_len; + let framing = match framing { + Ok(framing) => framing, + Err(code) => return Octane::fail(stream, code).await, + }; + if expects { + stream.write_all(CONTINUE).await?; + } + let (request, body_end) = match Request::from_raw( + head, + framing, + request_line, + data, + body_start, + &mut *stream, + settings.max_body_size, + ) + .await + { + Ok(parsed) => parsed, + Err(code) => return Octane::fail(stream, code).await, + }; + // Detect http version and validate + let checker = Http::validate(&request); + if checker.is_malformed() { + let code = checker.err_code.unwrap_or(StatusCode::BadRequest); + return Octane::fail(stream, code).await; + } + if !request.request_line.method.is_some() { + return Octane::fail(stream, StatusCode::NotImplemented).await; + } - if let (Some(request_line), Some(headers)) = (request_line, headers) { - let request = match Request::from_raw( - headers, - request_line, - &mut data, - body_start, - reader, - server.settings.max_body_size, - ) - .await - { - Ok(request) => request, - Err(code) => { - declare_error!(writer, code); - } - }; - let mut res = Response::new_empty(); - // Detect http version and validate - { - let checker = Http::validate(&request); - if checker.is_malformed() { - declare_error!(writer, checker.err_code.unwrap()); - } - } - if request.request_line.method.is_some() { - // run closures - server.router.run(request, &mut res); - if !res.has_body() { - declare_error!(writer, StatusCode::NotFound); - } - - Octane::send(res.into_parts(), writer).await?; + let head_only = request.request_line.method == RequestMethod::Head; + let version = request.request_line.version; + let keep_alive = settings.keep_alive.is_some() && request.keep_alive(); + let mut res = Response::new_empty(); + // run closures + server.router.run(&request, &mut res).await; + // a shutdown that started while the handler ran ends the connection + let keep_alive = keep_alive && !server.shutdown.started(); + if !res.has_body() { + // nothing answered: the path doesn't exist, or not for this + // method, which gets a 405 naming the ones it has (RFC 9110 + // section 15.5.6) + let allowed = server.router.allowed(&request.request_line.path); + let method = request.request_line.method; + if allowed.is_empty() || allowed.contains(&method) { + res = Error::response(StatusCode::NotFound); } else { - declare_error!(writer, StatusCode::NotImplemented); + let names: Vec<&str> = allowed.iter().map(RequestMethod::as_str).collect(); + res = Error::response(StatusCode::MethodNotAllowed); + res.headers.set("Allow", names.join(", ")); } - } else { - declare_error!(writer, StatusCode::BadRequest); } - Ok(()) + if !keep_alive { + res.headers.set("Connection", "close"); + } else if version == HttpVersion::Http10 { + // HTTP/1.0 connections only persist when both sides say so + res.headers.set("Connection", "keep-alive"); + } + res.frame(); + Octane::send(res, stream, head_only).await?; + // the request is answered, what follows it is the next one + data.drain(..body_end); + Ok(keep_alive) } + + // Answer a request that can't be served with `code` and give up on the + // connection, whatever the client sent after it can't be trusted to + // start a request. + async fn fail( + stream: &mut Connection, + code: StatusCode, + ) -> Result> + where + S: AsyncWrite + Unpin, + { + let mut res = Error::response(code); + res.headers.set("Connection", "close"); + res.frame(); + Octane::send(res, stream, false).await?; + Ok(false) + } + /// Run the router for a parsed request, filling `res`. Exposed for the /// HTTP/2 driver (a separate module) which cannot reach the private router. - pub(crate) fn route(&self, request: Request<'_>, res: &mut Response) { - self.router.run(request, res); + pub(crate) async fn route(&self, request: Request<'_>, res: &mut Response<'_>) { + self.router.run(&request, res).await; } - pub(crate) async fn send( - response: (String, ResBody), - mut stream_async: S, + + /// Write a response, without its body when `head_only` (the answer to a + /// `HEAD` request) or when its status has none. It is held with the responses before it until the + /// server waits on the client, and goes out with them in one write. + async fn send( + res: Response<'_>, + stream: &mut Connection, + head_only: bool, ) -> Result<(), Box> where S: AsyncWrite + Unpin, { - stream_async.write_all(response.0.as_bytes()).await?; - match response.1 { + let head_only = head_only || !res.status_has_body(); + res.write_head(stream.buffer()); + match res.into_body() { + _ if head_only => {} ResBody::None => {} - ResBody::Bytes(body) => stream_async.write_all(&body).await?, - ResBody::Static(body) => stream_async.write_all(body).await?, + ResBody::Bytes(body) => stream.write_all(&body).await?, + ResBody::Static(body) => stream.write_all(body).await?, ResBody::Sized(len, reader) => { let mut reader = reader.take(len as u64); - copy(&mut reader, &mut stream_async).await?; + let sent = copy(&mut reader, &mut *stream).await?; + // the client is still waiting for the rest of the body, the + // connection can't carry anything after it + if sent < len as u64 { + return Err(Box::new(io::Error::new( + io::ErrorKind::UnexpectedEof, + "the response body ended before its Content-Length", + ))); + } } } Ok(()) @@ -298,31 +574,31 @@ impl Octane { default!(Octane); impl Route for Octane { - type Handler = Closure; + type Handler = Handler; - fn head(&mut self, path: &str, closure: Closure) -> RouterResult { - self.router.head(path, closure) + fn head(&mut self, path: &str, handler: impl Into) -> RouterResult { + self.router.head(path, handler) } - fn put(&mut self, path: &str, closure: Closure) -> RouterResult { - self.router.put(path, closure) + fn put(&mut self, path: &str, handler: impl Into) -> RouterResult { + self.router.put(path, handler) } - fn get(&mut self, path: &str, closure: Closure) -> RouterResult { - self.router.get(path, closure) + fn get(&mut self, path: &str, handler: impl Into) -> RouterResult { + self.router.get(path, handler) } - fn delete(&mut self, path: &str, closure: Closure) -> RouterResult { - self.router.delete(path, closure) + fn delete(&mut self, path: &str, handler: impl Into) -> RouterResult { + self.router.delete(path, handler) } - fn post(&mut self, path: &str, closure: Closure) -> RouterResult { - self.router.post(path, closure) + fn post(&mut self, path: &str, handler: impl Into) -> RouterResult { + self.router.post(path, handler) } - fn patch(&mut self, path: &str, closure: Closure) -> RouterResult { - self.router.patch(path, closure) + fn patch(&mut self, path: &str, handler: impl Into) -> RouterResult { + self.router.patch(path, handler) } - fn add(&mut self, closure: Closure) -> RouterResult { - self.router.add(closure) + fn add(&mut self, handler: impl Into) -> RouterResult { + self.router.add(handler) } - fn add_route(&mut self, path: &str, closure: Closure) -> RouterResult { - self.router.add_route(path, closure) + fn add_route(&mut self, path: &str, handler: impl Into) -> RouterResult { + self.router.add_route(path, handler) } } diff --git a/src/server_builder.rs b/src/server_builder.rs index 560c14e..a701232 100644 --- a/src/server_builder.rs +++ b/src/server_builder.rs @@ -1,14 +1,18 @@ use crate::server::Octane; +use crate::shutdown::Watch; use crate::task; +#[cfg(any(feature = "openSSL", feature = "rustls"))] +use crate::util::within; use socket2::{Domain, SockAddr, Socket, Type}; -use std::future::Future; +use std::future::{poll_fn, Future}; use std::io::Result; use std::net::{IpAddr, Ipv4Addr, SocketAddr}; use std::sync::Arc; +use std::task::Poll; use std::time::Duration; use tokio::net::{TcpListener, TcpStream}; use tokio::sync::{OwnedSemaphorePermit, Semaphore}; -use tokio::time::{sleep, timeout}; +use tokio::time::sleep; #[cfg(feature = "openSSL")] use tokio_openssl::SslStream; #[cfg(feature = "rustls")] @@ -27,20 +31,32 @@ async fn reserve_connection(connections: &Arc) -> OwnedSemaphorePermi .expect("connection semaphore is never closed") } +// The next connection and the capacity to serve it, `None` once the server +// is shutting down async fn accept_bounded( listener: &TcpListener, connections: &Arc, -) -> (TcpStream, OwnedSemaphorePermit) { + shutdown: &Watch, +) -> Option<(TcpStream, OwnedSemaphorePermit)> { loop { - match listener.accept().await { + let accepted = poll_fn(|cx| match shutdown.poll_started(cx) { + Poll::Ready(()) => Poll::Ready(None), + Poll::Pending => listener.poll_accept(cx).map(Some), + }) + .await?; + match accepted { Ok((stream, _)) => { + // Responses are written whole, so there is nothing for + // Nagle's algorithm to coalesce, only a delayed ACK to wait + // on before the end of a response leaves. + let _ = stream.set_nodelay(true); // Only one socket per listener can wait here. Further sockets // stay in the kernel backlog until serving capacity returns. // Acquiring after accept also prevents an idle TLS listener // from reserving capacity needed by the plain listener (and // vice versa). let permit = reserve_connection(connections).await; - return (stream, permit); + return Some((stream, permit)); } Err(_) => { // An intermittent accept error must not take down the server; @@ -51,17 +67,6 @@ async fn accept_bounded( } } -async fn run_with_timeout(future: F, connection_timeout: Option) -where - F: Future, -{ - if let Some(duration) = connection_timeout { - let _ = timeout(duration, future).await; - } else { - future.await; - } -} - impl ServerBuilder { pub fn new(port: u16) -> Result { let socket = Socket::new(Domain::IPV4, Type::STREAM, None)?; @@ -88,15 +93,19 @@ impl ServerBuilder { T: Future + Send + 'static, C: FnOnce(TcpStream, Arc) -> T + Send + 'static + Copy, { - loop { - let (stream, permit) = accept_bounded(&self.socket, &connections).await; + let shutdown = server.watch_shutdown(); + // until the server shuts down, which closes the listening socket + while let Some((stream, permit)) = + accept_bounded(&self.socket, &connections, &shutdown).await + { let server = Arc::clone(&server); - let connection_timeout = server.settings.connection_timeout; + // the connection keeps its own time, see `connection_timeout` task!({ let _permit = permit; - run_with_timeout(exec(stream, server), connection_timeout).await; + exec(stream, server).await; }); } + Ok(()) } #[cfg(feature = "openSSL")] @@ -111,27 +120,23 @@ impl ServerBuilder { C: FnOnce(SslStream, Arc) -> T + Send + 'static + Copy, { let acceptor = crate::tls::openssl::acceptor(&server.settings)?; - loop { - let (tcp_stream, permit) = accept_bounded(&self.socket, &connections).await; + let shutdown = server.watch_shutdown(); + while let Some((tcp_stream, permit)) = + accept_bounded(&self.socket, &connections, &shutdown).await + { let acceptor = acceptor.clone(); let server = Arc::clone(&server); let connection_timeout = server.settings.connection_timeout; task!({ let _permit = permit; - run_with_timeout( - async move { - if let Ok(stream_ssl) = - crate::tls::openssl::accept(&acceptor, tcp_stream).await - { - exec(stream_ssl, server).await; - } - }, - connection_timeout, - ) - .await; + let handshake = crate::tls::openssl::accept(&acceptor, tcp_stream); + if let Some(Ok(stream_ssl)) = within(connection_timeout, handshake).await { + exec(stream_ssl, server).await; + } }); } + Ok(()) } #[cfg(feature = "rustls")] @@ -146,31 +151,30 @@ impl ServerBuilder { C: FnOnce(TlsStream, Arc) -> T + Send + 'static + Copy, { let acceptor = crate::tls::rustls::acceptor(&server.settings)?; - loop { - let (tcp_stream, permit) = accept_bounded(&self.socket, &connections).await; + let shutdown = server.watch_shutdown(); + while let Some((tcp_stream, permit)) = + accept_bounded(&self.socket, &connections, &shutdown).await + { let acceptor = acceptor.clone(); let server = Arc::clone(&server); let connection_timeout = server.settings.connection_timeout; task!({ let _permit = permit; - run_with_timeout( - async move { - if let Ok(stream_ssl) = acceptor.accept(tcp_stream).await { - exec(stream_ssl, server).await; - } - }, - connection_timeout, - ) - .await; + let handshake = acceptor.accept(tcp_stream); + if let Some(Ok(stream_ssl)) = within(connection_timeout, handshake).await { + exec(stream_ssl, server).await; + } }); } + Ok(()) } } #[cfg(test)] mod tests { - use super::{reserve_connection, run_with_timeout}; + use super::reserve_connection; + use crate::util::within; use std::future::pending; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; @@ -212,7 +216,9 @@ mod tests { pending::<()>().await; }; - run_with_timeout(work, Some(Duration::from_millis(10))).await; + assert!(within(Some(Duration::from_millis(10)), work) + .await + .is_none()); assert!(dropped.load(Ordering::SeqCst)); } } diff --git a/src/shutdown.rs b/src/shutdown.rs new file mode 100644 index 0000000..f276020 --- /dev/null +++ b/src/shutdown.rs @@ -0,0 +1,164 @@ +//! Graceful shutdown. Once it starts, listeners stop accepting, connections +//! waiting for a request close, and a connection answering one closes after +//! the response, which says `Connection: close`. +use std::sync::atomic::{AtomicBool, Ordering::SeqCst}; +use std::sync::{Arc, Mutex, MutexGuard, PoisonError}; +use std::task::{Context, Poll, Waker}; + +pub(crate) struct Shutdown { + started: AtomicBool, + // A slot per listener and connection, holding the waker of its task + // while it waits. Each registers once rather than per wait, so waiting + // for a request costs no lock shared with other connections. + waiting: Mutex>>>>, +} + +impl Shutdown { + pub(crate) fn new() -> Self { + Self { + started: AtomicBool::new(false), + waiting: Mutex::new(Slab::new()), + } + } + + pub(crate) fn started(&self) -> bool { + self.started.load(SeqCst) + } + + /// Start shutting down, waking every listener and connection waiting + pub(crate) fn start(&self) { + self.started.store(true, SeqCst); + for slot in lock(&self.waiting).iter() { + if let Some(waker) = lock(slot).take() { + waker.wake(); + } + } + } + + /// Register a task that waits on the shutdown, until the [`Watch`] drops + pub(crate) fn watch(self: &Arc) -> Watch { + let slot = Arc::new(Mutex::new(None)); + let key = lock(&self.waiting).insert(Arc::clone(&slot)); + Watch { + shutdown: Arc::clone(self), + key, + slot, + } + } +} + +/// A task's registration with [`Shutdown`] +pub(crate) struct Watch { + shutdown: Arc, + key: usize, + slot: Arc>>, +} + +impl Watch { + pub(crate) fn started(&self) -> bool { + self.shutdown.started() + } + + /// Ready once the shutdown has started + pub(crate) fn poll_started(&self, cx: &mut Context<'_>) -> Poll<()> { + if self.started() { + return Poll::Ready(()); + } + { + let mut waker = lock(&self.slot); + if !waker.as_ref().map_or(false, |w| w.will_wake(cx.waker())) { + *waker = Some(cx.waker().clone()); + } + } + // `start` may have taken the slot's old waker just before this one + // went in, but then the flag it set first is visible here + if self.started() { + Poll::Ready(()) + } else { + Poll::Pending + } + } +} + +impl Drop for Watch { + fn drop(&mut self) { + lock(&self.shutdown.waiting).remove(self.key); + } +} + +// A lock that a panic elsewhere doesn't make unusable: what's under these +// is consistent after any single operation +fn lock(mutex: &Mutex) -> MutexGuard<'_, T> { + mutex.lock().unwrap_or_else(PoisonError::into_inner) +} + +// Values kept by the key they were inserted at, which stays theirs until +// they're removed. Freed keys are reused, so it grows to the most entries +// held at once. +struct Slab { + entries: Vec>, + free: Vec, +} + +impl Slab { + fn new() -> Self { + Self { + entries: Vec::new(), + free: Vec::new(), + } + } + + fn insert(&mut self, value: T) -> usize { + match self.free.pop() { + Some(key) => { + self.entries[key] = Some(value); + key + } + None => { + self.entries.push(Some(value)); + self.entries.len() - 1 + } + } + } + + fn remove(&mut self, key: usize) { + if self.entries.get_mut(key).and_then(Option::take).is_some() { + self.free.push(key); + } + } + + fn iter(&self) -> impl Iterator { + self.entries.iter().flatten() + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::future::poll_fn; + + #[test] + fn slab_reuses_freed_keys() { + let mut slab = Slab::new(); + let (a, b) = (slab.insert('a'), slab.insert('b')); + slab.remove(a); + slab.remove(a); + assert_eq!(slab.iter().collect::>(), vec![&'b']); + assert_eq!(slab.insert('c'), a); + assert_ne!(a, b); + assert_eq!(slab.entries.len(), 2); + } + + #[tokio::test] + async fn waiting_tasks_wake_when_it_starts() { + let shutdown = Arc::new(Shutdown::new()); + let watch = shutdown.watch(); + let waiting = tokio::spawn(async move { poll_fn(|cx| watch.poll_started(cx)).await }); + tokio::task::yield_now().await; + assert!(!waiting.is_finished()); + shutdown.start(); + waiting.await.unwrap(); + // the finished task dropped its watch + assert_eq!(lock(&shutdown.waiting).iter().count(), 0); + } +} diff --git a/src/time.rs b/src/time.rs index a1a04d9..949b412 100644 --- a/src/time.rs +++ b/src/time.rs @@ -39,6 +39,19 @@ impl Time { Some(date) }) } + /// Call `f` with the current HTTP date, without copying it out of the + /// per-thread cache + pub(crate) fn with_http_date(f: impl FnOnce(&str) -> R) -> Option { + let stamp = Self::unix_timestamp()?; + HTTP_DATE_CACHE.with(|cache| { + let mut cache = cache.borrow_mut(); + if cache.as_ref().map_or(true, |(cached, _)| *cached != stamp) { + let date = Self::time(stamp.try_into().ok()?)?.format(); + *cache = Some((stamp, date)); + } + cache.as_ref().map(|(_, date)| f(date)) + }) + } fn unix_timestamp() -> Option { SystemTime::now() .duration_since(UNIX_EPOCH) diff --git a/src/tls/rustls.rs b/src/tls/rustls.rs index 5b82689..e4324b0 100644 --- a/src/tls/rustls.rs +++ b/src/tls/rustls.rs @@ -11,11 +11,14 @@ use tokio_rustls::{ }; pub fn acceptor(settings: &OctaneConfig) -> Result> { - let config = ServerConfig::builder_with_provider(Arc::new(ring::default_provider())) + let mut config = ServerConfig::builder_with_provider(Arc::new(ring::default_provider())) .with_safe_default_protocol_versions()? .with_no_client_auth() .with_single_cert(settings.get_cert()?, settings.get_key()?)?; - // config.set_protocols(&[b"h2".to_vec()]); + // HTTP/1.1 is the only protocol served over TLS, so that is what ALPN + // selects. A client offering ALPN without it gets `no_application_protocol` + // instead of a connection speaking something it didn't ask for. + config.alpn_protocols = vec![b"http/1.1".to_vec()]; let acceptor = TlsAcceptor::from(Arc::new(config)); Ok(acceptor) } diff --git a/src/util.rs b/src/util.rs index e3d5ea6..c89df24 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,45 +1,88 @@ -use octane_http::http1x::find_in_slice; -use std::iter::FusedIterator; +use std::future::{poll_fn, Future}; +use std::io; +use std::mem::MaybeUninit; +use std::pin::{pin, Pin}; +use std::task::Poll; +use std::time::Duration; +use tokio::io::{AsyncRead, ReadBuf}; +use tokio::time::{sleep_until, Instant, Sleep}; -pub struct Spliterator<'a, T: Eq> { - pub string: &'a [T], - pub finished: bool, - pub seq: &'a [T], -} +// Bytes read at once while waiting for a request or its body +const READ_SIZE: usize = 8192; -impl<'a, T: Eq> Spliterator<'a, T> { - pub fn new(string: &'a [T], seq: &'a [T]) -> Self { - Self { - string, - finished: false, - seq, +/// Append one read's worth of bytes from `reader` to `data`, returning how +/// many arrived, 0 at the end of the stream. +/// +/// The read buffer only lives on the stack while the read is polled, so a +/// connection waiting here for its next request doesn't hold 8 KiB of it. +pub(crate) async fn read_some( + reader: &mut R, + data: &mut Vec, +) -> io::Result { + poll_fn(|cx| { + let mut storage = [MaybeUninit::::uninit(); READ_SIZE]; + let mut buf = ReadBuf::uninit(&mut storage); + match Pin::new(&mut *reader).poll_read(cx, &mut buf) { + Poll::Ready(Ok(())) => { + data.extend_from_slice(buf.filled()); + Poll::Ready(Ok(buf.filled().len())) + } + Poll::Ready(Err(error)) => Poll::Ready(Err(error)), + Poll::Pending => Poll::Pending, } - } + }) + .await +} - pub fn find_seq(&self) -> Option { - find_in_slice(self.string, self.seq) +/// Run `future` for at most `limit`, `None` if it ran out of time. Without a +/// limit it runs to completion. +#[cfg(any(feature = "rustls", feature = "openSSL", test))] +pub(crate) async fn within(limit: Option, future: F) -> Option { + match limit { + Some(limit) => tokio::time::timeout(limit, future).await.ok(), + None => Some(future.await), } } -impl<'a, T: Eq> Iterator for Spliterator<'a, T> { - type Item = &'a [T]; +/// [`within`] for a connection's successive waits, reusing one timer. +/// +/// A new timeout clones the runtime's handle, a reference count every worker +/// thread shares, and registers a timer of its own. Moving this one's +/// deadline later, as each wait does when the limit stays the same, only +/// updates it, so a connection pays for its timer once rather than per +/// request. +pub(crate) struct Deadline { + /// How long each wait may take, `None` for no limit + pub(crate) limit: Option, + sleep: Option>>, +} + +impl Deadline { + pub(crate) fn new(limit: Option) -> Self { + Self { limit, sleep: None } + } - fn next(&mut self) -> Option { - if self.finished { - return None; - } - match self.find_seq() { - Some(v) => { - let (ret, rest) = self.string.split_at(v); - self.string = &rest[self.seq.len()..]; - Some(ret) + /// Run `future` for at most `limit` from now, `None` if it ran out of time + pub(crate) async fn run(&mut self, future: F) -> Option { + let limit = match self.limit { + Some(limit) => limit, + None => return Some(future.await), + }; + let deadline = Instant::now() + limit; + let sleep = match &mut self.sleep { + Some(sleep) => { + sleep.as_mut().reset(deadline); + sleep } - None => { - self.finished = true; - Some(self.string) + None => self.sleep.insert(Box::pin(sleep_until(deadline))), + }; + let mut future = pin!(future); + poll_fn(|cx| { + if let Poll::Ready(output) = future.as_mut().poll(cx) { + return Poll::Ready(Some(output)); } - } + sleep.as_mut().poll(cx).map(|()| None) + }) + .await } } - -impl<'a, T: Eq> FusedIterator for Spliterator<'a, T> {} diff --git a/src/wasip3.rs b/src/wasip3.rs index 65b25da..2646aae 100644 --- a/src/wasip3.rs +++ b/src/wasip3.rs @@ -16,7 +16,7 @@ //! //! let mut router = Router::new(); //! router.get("/users/:id", handler(async |req, res| { -//! let id = req.vars.get("id").copied().unwrap_or(""); +//! let id = req.vars.get("id").unwrap_or(""); //! let _ = res.send(StatusCode::Ok, &[], id.as_bytes()).await; //! Flow::Stop //! }))?; @@ -30,13 +30,13 @@ pub use crate::method::RequestMethod; /// The error the [`Route`] methods return for a path that climbs above the root pub use crate::path::InvalidPathError; use crate::path::PathBuf; +#[cfg(feature = "url_variables")] +pub use crate::path::Vars; pub use crate::routes::{Flow, Route}; use crate::routes::{RouterResult, Routes}; use octane_http::config::Config; use octane_http::http1x::Request; pub use octane_http::wasip3::{BodyWriter, ErrorCode, Responder, SendError}; -#[cfg(feature = "url_variables")] -use std::collections::HashMap; use std::future::Future; use std::net::SocketAddr; use std::ops::Deref; @@ -51,7 +51,7 @@ pub struct MatchedRequest<'r> { pub request: &'r Request<'r>, /// The url variables of the matched route, empty for middleware #[cfg(feature = "url_variables")] - pub vars: HashMap<&'r str, &'r str>, + pub vars: Vars<'r>, } impl<'r> Deref for MatchedRequest<'r> { @@ -140,29 +140,33 @@ default!(Router); impl Route for Router { type Handler = Handler; - fn add_route(&mut self, path: &str, handler: Handler) -> RouterResult { - self.routes.insert(RequestMethod::All, path, handler) + fn add_route(&mut self, path: &str, handler: impl Into) -> RouterResult { + self.routes.insert(RequestMethod::All, path, handler.into()) } - fn head(&mut self, path: &str, handler: Handler) -> RouterResult { - self.routes.insert(RequestMethod::Head, path, handler) + fn head(&mut self, path: &str, handler: impl Into) -> RouterResult { + self.routes + .insert(RequestMethod::Head, path, handler.into()) } - fn post(&mut self, path: &str, handler: Handler) -> RouterResult { - self.routes.insert(RequestMethod::Post, path, handler) + fn post(&mut self, path: &str, handler: impl Into) -> RouterResult { + self.routes + .insert(RequestMethod::Post, path, handler.into()) } - fn get(&mut self, path: &str, handler: Handler) -> RouterResult { - self.routes.insert(RequestMethod::Get, path, handler) + fn get(&mut self, path: &str, handler: impl Into) -> RouterResult { + self.routes.insert(RequestMethod::Get, path, handler.into()) } - fn put(&mut self, path: &str, handler: Handler) -> RouterResult { - self.routes.insert(RequestMethod::Put, path, handler) + fn put(&mut self, path: &str, handler: impl Into) -> RouterResult { + self.routes.insert(RequestMethod::Put, path, handler.into()) } - fn delete(&mut self, path: &str, handler: Handler) -> RouterResult { - self.routes.insert(RequestMethod::Delete, path, handler) + fn delete(&mut self, path: &str, handler: impl Into) -> RouterResult { + self.routes + .insert(RequestMethod::Delete, path, handler.into()) } - fn patch(&mut self, path: &str, handler: Handler) -> RouterResult { - self.routes.insert(RequestMethod::Patch, path, handler) + fn patch(&mut self, path: &str, handler: impl Into) -> RouterResult { + self.routes + .insert(RequestMethod::Patch, path, handler.into()) } - fn add(&mut self, handler: Handler) -> RouterResult { - self.routes.add(handler); + fn add(&mut self, handler: impl Into) -> RouterResult { + self.routes.add(handler.into()); Ok(()) } } diff --git a/tests/async_handlers.rs b/tests/async_handlers.rs new file mode 100644 index 0000000..1c0538a --- /dev/null +++ b/tests/async_handlers.rs @@ -0,0 +1,63 @@ +#![cfg(feature = "std")] +//! Async handlers wait without holding a worker thread. +use octane::prelude::*; +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::sync::Arc; +use std::thread; +use std::time::{Duration, Instant}; +mod common; + +const PORT: u16 = 8093; +const WAIT: Duration = Duration::from_millis(200); +const CONCURRENT: usize = 64; + +#[test] +pub fn async_handlers_wait_concurrently() { + let mut app = Octane::new(); + let served = Arc::new(AtomicUsize::new(0)); + let counter = Arc::clone(&served); + // the body borrows the captured counter across the await + app.get( + "/wait/:ms", + route_async_stop!(|req, res| { + let ms = req + .vars + .get("ms") + .and_then(|ms| ms.parse().ok()) + .unwrap_or(0); + tokio::time::sleep(Duration::from_millis(ms)).await; + counter.fetch_add(1, Ordering::SeqCst); + res.send(ms.to_string()); + }), + ) + .unwrap(); + // a sync handler next to them still answers + app.get("/now", route_stop!(|req, res| res.send("now"))) + .unwrap(); + + common::run(app, PORT, || { + let start = Instant::now(); + let clients: Vec<_> = (0..CONCURRENT) + .map(|_| { + thread::spawn(|| { + let path = format!("/wait/{}", WAIT.as_millis()); + common::request(PORT, "GET", &path, &[]).text().to_owned() + }) + }) + .collect(); + // answered while every async handler is still waiting + assert_eq!("now", common::request(PORT, "GET", "/now", &[]).text()); + for client in clients { + assert_eq!(WAIT.as_millis().to_string(), client.join().unwrap()); + } + let elapsed = start.elapsed(); + assert!(elapsed >= WAIT, "answered before waiting: {:?}", elapsed); + // blocking a worker per request would take CONCURRENT / workers waits + assert!( + elapsed < Duration::from_secs(1), + "async handlers didn't overlap: {:?}", + elapsed + ); + assert_eq!(CONCURRENT, served.load(Ordering::SeqCst)); + }); +} diff --git a/tests/common.rs b/tests/common.rs index 1b6388f..886f50b 100644 --- a/tests/common.rs +++ b/tests/common.rs @@ -43,6 +43,21 @@ impl Response { } } +#[allow(dead_code)] +impl Response { + /// The value of the `name` header, matched without regard to case + pub fn header(&self, name: &str) -> Option<&str> { + self.head.split("\r\n").skip(1).find_map(|line| { + let (key, value) = line.split_once(':')?; + if key.eq_ignore_ascii_case(name) { + Some(value.trim()) + } else { + None + } + }) + } +} + /// Send `method path` with a Host header plus `headers`, and read the response #[allow(dead_code)] pub fn request(port: u16, method: &str, path: &str, headers: &[&str]) -> Response { @@ -55,35 +70,82 @@ pub fn request(port: u16, method: &str, path: &str, headers: &[&str]) -> Respons req.push_str("\r\n"); } req.push_str("\r\n"); - raw_request(port, req.as_bytes()) + let mut stream = connect(port); + stream.write_all(req.as_bytes()).unwrap(); + read_response(&mut stream, method == "HEAD") } -/// Send `bytes` as they are and read the response, the server closes the -/// connection after it +/// Send `bytes` as they are and read the response #[allow(dead_code)] pub fn raw_request(port: u16, bytes: &[u8]) -> Response { - let mut stream = TcpStream::connect(("127.0.0.1", port)).expect("cannot connect"); + let mut stream = connect(port); + stream.write_all(bytes).unwrap(); + read_response(&mut stream, false) +} + +/// Open a connection to the server on `port` +#[allow(dead_code)] +pub fn connect(port: u16) -> TcpStream { + let stream = TcpStream::connect(("127.0.0.1", port)).expect("cannot connect"); stream .set_read_timeout(Some(Duration::from_secs(10))) .unwrap(); - stream.write_all(bytes).unwrap(); + stream +} + +/// Read one response off `stream`, and nothing past it: the body is as long +/// as its `Content-Length`, or runs to the end of the stream without one. +/// The answer to a `HEAD` request (`head_only`) has no body. +#[allow(dead_code)] +pub fn read_response(stream: &mut TcpStream, head_only: bool) -> Response { + // a byte at a time, so the start of the next response stays unread let mut raw = Vec::new(); - stream.read_to_end(&mut raw).expect("cannot read response"); - let end = raw - .windows(4) - .position(|w| w == b"\r\n\r\n") - .expect("response has no end of head"); - let head = String::from_utf8(raw[..end].to_vec()).expect("head is not utf-8"); + let mut byte = [0u8; 1]; + while !raw.ends_with(b"\r\n\r\n") { + let read = stream.read(&mut byte).expect("cannot read response"); + assert_eq!( + read, + 1, + "connection closed in the head {:?}", + String::from_utf8_lossy(&raw) + ); + raw.push(byte[0]); + } + raw.truncate(raw.len() - 4); + let head = String::from_utf8(raw).expect("head is not utf-8"); let status = head .split(' ') .nth(1) .and_then(|code| code.parse().ok()) .expect("response has no status code"); - Response { + let mut response = Response { status, head, - body: raw[end + 4..].to_vec(), + body: Vec::new(), + }; + if !head_only { + match response.header("content-length") { + Some(len) => { + response.body = vec![0; len.parse().expect("bad Content-Length")]; + stream + .read_exact(&mut response.body) + .expect("cannot read body"); + } + None => { + stream + .read_to_end(&mut response.body) + .expect("cannot read body"); + } + } } + response +} + +/// Whether the server has closed `stream`, after waiting for it to +#[allow(dead_code)] +pub fn is_closed(stream: &mut TcpStream) -> bool { + let mut byte = [0u8; 1]; + matches!(stream.read(&mut byte), Ok(0)) } /// GET `path` over https, accepting the self-signed test certificate diff --git a/tests/http1x.rs b/tests/http1x.rs index 5bed0b1..f485aca 100644 --- a/tests/http1x.rs +++ b/tests/http1x.rs @@ -16,14 +16,14 @@ fn app() -> Octane { app.get( "/query", route_next!(|req, res| { - res.send(req.get_query().get("q").cloned().unwrap_or_default()); + res.send(req.get_query().get("q").unwrap_or_default().to_owned()); }), ) .unwrap(); app.get( "/redirect", route_next!(|req, res| { - let next = req.get_query().get("next").cloned().unwrap_or_default(); + let next = req.get_query().get("next").unwrap_or_default().to_owned(); res.redirect(&next).send(""); }), ) diff --git a/tests/keep_alive.rs b/tests/keep_alive.rs new file mode 100644 index 0000000..11b6f51 --- /dev/null +++ b/tests/keep_alive.rs @@ -0,0 +1,276 @@ +#![cfg(feature = "std")] +//! Persistent connections: several requests per connection, pipelined +//! requests, chunked request bodies and the things that end a connection. +use octane::prelude::*; +use octane::StatusCode; +use std::io::{Read, Write}; +use std::thread; +use std::time::Duration; +mod common; + +const PORT: u16 = 8090; +const IDLE_PORT: u16 = 8091; + +fn app() -> Octane { + let mut app = Octane::new(); + app.settings.max_body_size = 16; + app.get("/", route_next!(|req, res| res.send("ok"))) + .unwrap(); + app.head("/", route_next!(|req, res| res.send("ok"))) + .unwrap(); + app.post("/echo", route_next!(|req, res| res.send(req.body))) + .unwrap(); + // statuses that never have content, sent with some anyway + app.get( + "/not-modified", + route_next!(|req, res| res.status(StatusCode::NotModified).send("stale")), + ) + .unwrap(); + app.get( + "/no-content", + route_next!(|req, res| res.status(StatusCode::NoContent).send("nothing")), + ) + .unwrap(); + app +} + +const GET: &[u8] = b"GET / HTTP/1.1\r\nHost: a\r\n\r\n"; + +#[test] +pub fn connections_carry_many_requests() { + common::run(app(), PORT, || { + // one request after another on the same connection + let mut stream = common::connect(PORT); + for _ in 0..3 { + stream.write_all(GET).unwrap(); + let res = common::read_response(&mut stream, false); + assert_eq!((200, "ok"), (res.status, res.text())); + assert_eq!(None, res.header("connection")); + } + + // pipelined: everything sent at once, answered in order, including + // a body-less HEAD, a 404 and a chunked body between the others + let mut stream = common::connect(PORT); + let mut pipeline = Vec::new(); + pipeline.extend_from_slice(GET); + pipeline.extend_from_slice(b"HEAD / HTTP/1.1\r\nHost: a\r\n\r\n"); + pipeline.extend_from_slice(b"GET /missing HTTP/1.1\r\nHost: a\r\n\r\n"); + pipeline.extend_from_slice( + b"POST /echo HTTP/1.1\r\nHost: a\r\nTransfer-Encoding: chunked\r\n\r\n\ + 5\r\nhello\r\n7;ext=1\r\n, world\r\n0\r\nX-Trailer: t\r\n\r\n", + ); + pipeline + .extend_from_slice(b"POST /echo HTTP/1.1\r\nHost: a\r\nContent-Length: 3\r\n\r\nabc"); + pipeline.extend_from_slice(GET); + stream.write_all(&pipeline).unwrap(); + let ok = common::read_response(&mut stream, false); + assert_eq!((200, "ok"), (ok.status, ok.text())); + let head = common::read_response(&mut stream, true); + assert_eq!( + (200, Some("2")), + (head.status, head.header("content-length")) + ); + assert_eq!(404, common::read_response(&mut stream, false).status); + let chunked = common::read_response(&mut stream, false); + assert_eq!((200, "hello, world"), (chunked.status, chunked.text())); + assert_eq!("abc", common::read_response(&mut stream, false).text()); + assert_eq!("ok", common::read_response(&mut stream, false).text()); + + // a client may send an empty line before a request (RFC 9112 2.2) + stream.write_all(b"\r\n").unwrap(); + stream.write_all(GET).unwrap(); + assert_eq!("ok", common::read_response(&mut stream, false).text()); + // even when its CR and LF arrive apart + for part in [&b"\r"[..], b"\n", GET].iter() { + stream.write_all(part).unwrap(); + thread::sleep(Duration::from_millis(20)); + } + assert_eq!("ok", common::read_response(&mut stream, false).text()); + + // a request split over several writes is put back together + let parts: [&[u8]; 4] = [ + b"POST /echo HTTP/1.1\r\nHo", + b"st: a\r\nTransfer-Encoding: chunked\r\n\r\n3", + b"\r\nab", + b"c\r\n0\r\n\r\n", + ]; + for part in parts.iter() { + stream.write_all(part).unwrap(); + thread::sleep(Duration::from_millis(20)); + } + assert_eq!("abc", common::read_response(&mut stream, false).text()); + }); +} + +#[test] +pub fn connections_end_when_asked_or_when_framing_is_lost() { + common::run(app(), PORT + 2, || { + let port = PORT + 2; + let closes = |request: &[u8], status: u16| { + let mut stream = common::connect(port); + stream.write_all(request).unwrap(); + let res = common::read_response(&mut stream, false); + assert_eq!(status, res.status, "{:?}", String::from_utf8_lossy(request)); + assert_eq!(Some("close"), res.header("connection")); + assert!(common::is_closed(&mut stream)); + }; + // asked for + closes( + b"GET / HTTP/1.1\r\nHost: a\r\nConnection: close\r\n\r\n", + 200, + ); + closes(b"GET / HTTP/1.0\r\n\r\n", 200); + // the request can't be framed, so nothing after it can be either + let smuggled = b"POST /echo HTTP/1.1\r\nHost: a\r\nContent-Length: 3\r\n\ + Transfer-Encoding: chunked\r\n\r\n0\r\n\r\n"; + closes(smuggled, 400); + let gzip = b"POST /echo HTTP/1.1\r\nHost: a\r\nTransfer-Encoding: gzip, chunked\r\n\r\n"; + closes(gzip, 501); + let bad_chunk = + b"POST /echo HTTP/1.1\r\nHost: a\r\nTransfer-Encoding: chunked\r\n\r\nz\r\n"; + closes(bad_chunk, 400); + let too_large = b"POST /echo HTTP/1.1\r\nHost: a\r\nTransfer-Encoding: chunked\r\n\r\n\ + 11\r\n12345678901234567\r\n0\r\n\r\n"; + closes(too_large, 413); + closes(b"GET / HTTP/1.1\r\n\r\n", 400); + + // HTTP/1.0 keeps the connection only when it asks to + let mut stream = common::connect(port); + for _ in 0..2 { + stream + .write_all(b"GET / HTTP/1.0\r\nConnection: keep-alive\r\n\r\n") + .unwrap(); + let res = common::read_response(&mut stream, false); + assert_eq!( + ("ok", Some("keep-alive")), + (res.text(), res.header("connection")) + ); + } + }); +} + +#[test] +pub fn a_path_without_the_method_is_405() { + common::run(app(), PORT + 6, || { + let mut stream = common::connect(PORT + 6); + // `/` has GET and HEAD routes, `/echo` only POST + stream + .write_all( + b"DELETE / HTTP/1.1\r\nHost: a\r\n\r\n\ + GET /echo HTTP/1.1\r\nHost: a\r\n\r\n\ + GET /missing HTTP/1.1\r\nHost: a\r\n\r\n\ + GET / HTTP/1.1\r\nHost: a\r\n\r\n", + ) + .unwrap(); + let res = common::read_response(&mut stream, false); + assert_eq!((405, Some("GET, HEAD")), (res.status, res.header("allow"))); + let res = common::read_response(&mut stream, false); + assert_eq!((405, Some("POST")), (res.status, res.header("allow"))); + // the connection carries on, it's the request that was refused + let res = common::read_response(&mut stream, false); + assert_eq!((404, None), (res.status, res.header("allow"))); + assert_eq!("ok", common::read_response(&mut stream, false).text()); + }); +} + +#[test] +pub fn bodiless_statuses_send_no_body() { + common::run(app(), PORT + 5, || { + // whatever the handler sent, the next response follows the head + let mut stream = common::connect(PORT + 5); + stream + .write_all( + b"GET /not-modified HTTP/1.1\r\nHost: a\r\n\r\n\ + GET /no-content HTTP/1.1\r\nHost: a\r\n\r\n\ + GET / HTTP/1.1\r\nHost: a\r\n\r\n", + ) + .unwrap(); + for status in [304, 204].iter() { + let res = common::read_response(&mut stream, true); + assert_eq!(*status, res.status); + assert_eq!(None, res.header("content-length")); + } + assert_eq!("ok", common::read_response(&mut stream, false).text()); + }); +} + +#[test] +pub fn a_refused_upload_still_gets_its_response() { + common::run(app(), PORT + 4, || { + for _ in 0..5 { + let mut stream = common::connect(PORT + 4); + // over max_body_size, refused before the body is read + stream + .write_all(b"POST /echo HTTP/1.1\r\nHost: a\r\nContent-Length: 4194304\r\n\r\n") + .unwrap(); + // the client is still sending when the answer comes + let chunk = vec![b'x'; 64 * 1024]; + for _ in 0..8 { + if stream.write_all(&chunk).is_err() { + break; + } + } + let res = common::read_response(&mut stream, false); + assert_eq!((413, Some("close")), (res.status, res.header("connection"))); + // and the stream ends, rather than being reset over the unread + // body, which can destroy the response before it is read + // (RFC 9112 section 9.6) + assert!(common::is_closed(&mut stream), "the connection was reset"); + } + }); +} + +#[test] +pub fn expect_continue_is_answered_before_the_body_is_read() { + common::run(app(), PORT + 3, || { + let mut stream = common::connect(PORT + 3); + stream + .write_all(b"POST /echo HTTP/1.1\r\nHost: a\r\nContent-Length: 5\r\nExpect: 100-continue\r\n\r\n") + .unwrap(); + let interim = common::read_response(&mut stream, true); + assert_eq!(100, interim.status); + stream.write_all(b"hello").unwrap(); + assert_eq!("hello", common::read_response(&mut stream, false).text()); + + // not for a body that is going to be refused + stream + .write_all(b"POST /echo HTTP/1.1\r\nHost: a\r\nContent-Length: 99\r\nExpect: 100-continue\r\n\r\n") + .unwrap(); + assert_eq!(413, common::read_response(&mut stream, false).status); + }); +} + +#[test] +pub fn idle_connections_are_closed() { + let mut app = app(); + app.settings.keep_alive = Some(Duration::from_millis(200)); + common::run(app, IDLE_PORT, || { + let mut stream = common::connect(IDLE_PORT); + stream.write_all(GET).unwrap(); + assert_eq!("ok", common::read_response(&mut stream, false).text()); + // still open within the idle time + thread::sleep(Duration::from_millis(50)); + stream.write_all(GET).unwrap(); + assert_eq!("ok", common::read_response(&mut stream, false).text()); + // and closed after it, without a response + let mut rest = Vec::new(); + stream.read_to_end(&mut rest).unwrap(); + assert!(rest.is_empty()); + }); + + // without keep-alive every response closes its connection + let mut app = Octane::new(); + app.settings.keep_alive = None; + app.get("/", route_next!(|req, res| res.send("ok"))) + .unwrap(); + common::run(app, IDLE_PORT + 1, || { + let mut stream = common::connect(IDLE_PORT + 1); + stream.write_all(GET).unwrap(); + let res = common::read_response(&mut stream, false); + assert_eq!( + ("ok", Some("close")), + (res.text(), res.header("connection")) + ); + assert!(common::is_closed(&mut stream)); + }); +} diff --git a/tests/server.rs b/tests/server.rs index f871ccf..0618462 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -1,5 +1,6 @@ #![cfg(feature = "std")] use octane::prelude::*; +use std::io::Write; mod common; #[test] @@ -70,8 +71,16 @@ pub fn basic_server_x_req() { for method in &["GET", "POST", "PUT", "PATCH", "DELETE"] { assert_eq!(string, common::request(8082, method, "/", &[]).text()); } - // TODO: HEAD responses still carry the body, they must not - assert_eq!(200, common::request(8082, "HEAD", "/", &[]).status); + // HEAD gets the head GET would, and no body: what follows it on the + // connection is the next response + let mut stream = common::connect(8082); + stream + .write_all(b"HEAD / HTTP/1.1\r\nHost: a\r\n\r\nGET / HTTP/1.1\r\nHost: a\r\n\r\n") + .unwrap(); + let head = common::read_response(&mut stream, true); + assert_eq!(200, head.status); + assert_eq!(Some("12"), head.header("content-length")); + assert_eq!(string, common::read_response(&mut stream, false).text()); }); } diff --git a/tests/shutdown.rs b/tests/shutdown.rs new file mode 100644 index 0000000..cd0420e --- /dev/null +++ b/tests/shutdown.rs @@ -0,0 +1,62 @@ +#![cfg(feature = "std")] +//! Graceful shutdown: idle connections close, a request in flight finishes. +use octane::prelude::*; +use std::io::Write; +use std::time::{Duration, Instant}; +mod common; + +const PORT: u16 = 8095; + +#[test] +pub fn shutdown_finishes_requests_and_closes_idle_connections() { + let mut app = Octane::new(); + app.get("/", route_stop!(|req, res| res.send("ok"))) + .unwrap(); + app.get( + "/slow", + route_async_stop!(|req, res| { + tokio::time::sleep(Duration::from_millis(300)).await; + res.send("done"); + }), + ) + .unwrap(); + let runtime = tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + .unwrap(); + let (stop, stopped) = tokio::sync::oneshot::channel::<()>(); + let server = runtime.spawn(async move { + app.listen_with_shutdown(PORT, || {}, async { + let _ = stopped.await; + }) + .await + .is_ok() + }); + common::wait_for(PORT); + + // an idle keep-alive connection, and one with a request in flight + let mut idle = common::connect(PORT); + idle.write_all(b"GET / HTTP/1.1\r\nHost: a\r\n\r\n") + .unwrap(); + assert_eq!("ok", common::read_response(&mut idle, false).text()); + let mut busy = common::connect(PORT); + busy.write_all(b"GET /slow HTTP/1.1\r\nHost: a\r\n\r\n") + .unwrap(); + std::thread::sleep(Duration::from_millis(50)); + + let start = Instant::now(); + stop.send(()).unwrap(); + // the idle one closes right away + assert!(common::is_closed(&mut idle)); + assert!(start.elapsed() < Duration::from_millis(250)); + // the busy one is answered, told the connection closes, then closed + let res = common::read_response(&mut busy, false); + assert_eq!( + ("done", Some("close")), + (res.text(), res.header("connection")) + ); + assert!(common::is_closed(&mut busy)); + // and listening ends once both are gone + assert!(runtime.block_on(server).unwrap()); + assert!(std::net::TcpStream::connect(("127.0.0.1", PORT)).is_err()); +}