joist 1.0.0

A backend framework for Rust: routing, middleware, JSON, config and plugins, with no third-party dependencies.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
use std::io::{BufRead, Read};
use std::net::SocketAddr;

use crate::body::Body;
use crate::error::{Error, Result};
use crate::headers::Headers;
use crate::json::Json;
use crate::method::Method;

/// Caps applied while parsing, before any handler sees the request. Without
/// them a hostile client can exhaust memory with one very long line or an
/// endless run of header fields.
const MAX_LINE: usize = 8 * 1024;
const MAX_HEADERS: usize = 100;

/// An incoming HTTP request.
///
/// The server builds one from a socket. Anyone else builds one with
/// [`Request::new`] and the `with_` methods, which is what makes a handler
/// testable without a listener:
///
/// ```
/// use joist::{Method, Request, Response};
///
/// fn handler(req: &Request) -> Response {
///     Response::text(format!("user {}", req.param("id").unwrap_or("?")))
/// }
///
/// let req = Request::new(Method::Get, "/users/42").with_param("id", "42");
/// assert_eq!(handler(&req).body().as_bytes(), Some(&b"user 42"[..]));
/// ```
#[derive(Debug)]
pub struct Request {
    pub method: Method,
    /// The raw request target, before splitting off the query.
    pub target: String,
    /// Percent-decoded path.
    pub path: String,
    /// Raw query string, without the leading `?`.
    pub query: String,
    pub version: String,
    pub headers: Headers,
    /// The peer address, when the request came from a socket.
    pub peer: Option<SocketAddr>,
    /// Private for the same reason the response body is: a `Vec<u8>` field is a
    /// promise that the whole request is in memory.
    body: Body,
    params: Vec<(String, String)>,
}

impl Request {
    /// An empty `HTTP/1.1` request for a target, which is split into a path and
    /// a query the same way the parser splits one off the wire.
    pub fn new(method: Method, target: &str) -> Request {
        let (raw_path, query) = match target.split_once('?') {
            Some((path, query)) => (path, query),
            None => (target, ""),
        };
        Request {
            method,
            target: target.to_string(),
            path: percent_decode(raw_path),
            query: query.to_string(),
            version: "HTTP/1.1".to_string(),
            headers: Headers::new(),
            peer: None,
            body: Body::empty(),
            params: Vec::new(),
        }
    }

    /// Add a header field. Repeats are kept rather than replaced, the way the
    /// parser keeps them, so a test can build the duplicate a client sent.
    pub fn with_header(mut self, name: &str, value: &str) -> Request {
        self.headers.append(name, value);
        self
    }

    pub fn with_body<B: Into<Body>>(mut self, body: B) -> Request {
        self.body = body.into();
        self
    }

    /// Set a path parameter, as a matching route would have. Handlers read
    /// these with [`Request::param`], so a test has to be able to supply them.
    pub fn with_param(mut self, name: &str, value: &str) -> Request {
        self.params.push((name.to_string(), value.to_string()));
        self
    }

    pub fn with_version(mut self, version: &str) -> Request {
        self.version = version.to_string();
        self
    }

    pub fn with_peer(mut self, peer: SocketAddr) -> Request {
        self.peer = Some(peer);
        self
    }

    pub fn body(&self) -> &Body {
        &self.body
    }

    pub fn into_body(self) -> Body {
        self.body
    }

    /// A path parameter captured by the route, such as `:id`.
    pub fn param(&self, name: &str) -> Option<&str> {
        self.params
            .iter()
            .find(|(k, _)| k == name)
            .map(|(_, v)| v.as_str())
    }

    pub fn params(&self) -> impl Iterator<Item = (&str, &str)> {
        self.params.iter().map(|(k, v)| (k.as_str(), v.as_str()))
    }

    pub(crate) fn set_params(&mut self, params: Vec<(String, String)>) {
        self.params = params;
    }

    /// A decoded query-string parameter. The first occurrence wins.
    pub fn query_param(&self, name: &str) -> Option<String> {
        for pair in self.query.split('&') {
            if pair.is_empty() {
                continue;
            }
            let (raw_key, raw_value) = match pair.split_once('=') {
                Some((k, v)) => (k, v),
                None => (pair, ""),
            };
            if percent_decode(raw_key) == name {
                return Some(percent_decode(raw_value));
            }
        }
        None
    }

    pub fn content_type(&self) -> Option<&str> {
        self.headers.get("Content-Type")
    }

    /// The body as text, if it is valid UTF-8.
    pub fn body_str(&self) -> Result<&str> {
        let bytes = self
            .body
            .as_bytes()
            .ok_or(Error::Unsupported("reading a streaming body as text"))?;
        std::str::from_utf8(bytes)
            .map_err(|_| Error::BadRequest("request body is not valid utf-8".into()))
    }

    /// Parse the body as JSON.
    pub fn json(&self) -> Result<Json> {
        Json::parse(self.body_str()?)
    }

    /// Whether the connection should be reused after this request. HTTP/1.1
    /// keeps alive unless told otherwise; HTTP/1.0 is the reverse.
    pub fn keep_alive(&self) -> bool {
        match self.headers.get("Connection") {
            Some(v) if v.eq_ignore_ascii_case("close") => false,
            Some(v) if v.eq_ignore_ascii_case("keep-alive") => true,
            _ => self.version != "HTTP/1.0",
        }
    }

    /// Read one request from a buffered stream.
    ///
    /// Returns `Ok(None)` when the peer closed the connection cleanly before
    /// sending anything, which is normal for a keep-alive connection going idle
    /// and must not be logged as an error.
    pub fn read_from<R: BufRead>(reader: &mut R, max_body: usize) -> Result<Option<Request>> {
        let mut line = Vec::new();

        // Request line.
        if read_line(reader, &mut line)? == 0 {
            return Ok(None);
        }
        // Take owned copies before the header loop reuses `line`, or the
        // borrows into it would still be alive when it is next written to.
        let (method, target, version) = {
            let start = trim_line(&line);
            if start.is_empty() {
                return Err(Error::BadRequest("empty request line".into()));
            }
            let text = std::str::from_utf8(start)
                .map_err(|_| Error::BadRequest("request line is not valid utf-8".into()))?;

            let mut parts = text.split(' ').filter(|p| !p.is_empty());
            let method_token = parts
                .next()
                .ok_or_else(|| Error::BadRequest("missing method".into()))?;
            let target = parts
                .next()
                .ok_or_else(|| Error::BadRequest("missing request target".into()))?;
            let version = parts.next().unwrap_or("HTTP/1.1");
            if parts.next().is_some() {
                return Err(Error::BadRequest("malformed request line".into()));
            }

            let method = Method::parse(method_token)
                .ok_or_else(|| Error::BadRequest(format!("unknown method {method_token}")))?;

            (method, target.to_string(), version.to_string())
        };

        // Header fields.
        let mut headers = Headers::new();
        loop {
            if read_line(reader, &mut line)? == 0 {
                return Err(Error::BadRequest("connection closed inside headers".into()));
            }
            let field = trim_line(&line);
            if field.is_empty() {
                break;
            }
            if headers.len() >= MAX_HEADERS {
                return Err(Error::BadRequest("too many header fields".into()));
            }
            let field = std::str::from_utf8(field)
                .map_err(|_| Error::BadRequest("header is not valid utf-8".into()))?;
            let (name, value) = field
                .split_once(':')
                .ok_or_else(|| Error::BadRequest("header without a colon".into()))?;
            if name.is_empty() || name.contains(' ') {
                return Err(Error::BadRequest("malformed header name".into()));
            }
            headers.append(name, value.trim());
        }

        // Body framing. Chunked is not implemented, and saying so beats
        // silently treating the chunk markers as content.
        if let Some(te) = headers.get("Transfer-Encoding") {
            if te.to_ascii_lowercase().contains("chunked") {
                return Err(Error::Unsupported("chunked request bodies"));
            }
        }

        let length = match headers.get("Content-Length") {
            Some(v) => v
                .trim()
                .parse::<usize>()
                .map_err(|_| Error::BadRequest("invalid Content-Length".into()))?,
            None => 0,
        };
        if length > max_body {
            return Err(Error::BodyTooLarge { limit: max_body });
        }

        let mut body = vec![0u8; length];
        if length > 0 {
            std::io::Read::read_exact(reader, &mut body)?;
        }

        let (raw_path, query) = match target.split_once('?') {
            Some((p, q)) => (p.to_string(), q.to_string()),
            None => (target.clone(), String::new()),
        };

        Ok(Some(Request {
            method,
            target,
            path: percent_decode(&raw_path),
            query,
            version,
            headers,
            peer: None,
            body: Body::from(body),
            params: Vec::new(),
        }))
    }
}

fn read_line<R: BufRead>(reader: &mut R, buf: &mut Vec<u8>) -> Result<usize> {
    buf.clear();
    // Reading the whole line first and measuring it afterwards is not a cap:
    // `read_until` would buffer however many bytes the peer sent before its
    // newline, which is the allocation MAX_LINE exists to prevent. Capping the
    // reader instead stops at the limit, and one byte over it with no newline in
    // sight is a line that is already too long.
    let n = reader
        .by_ref()
        .take(MAX_LINE as u64 + 1)
        .read_until(b'\n', buf)?;
    if n > MAX_LINE {
        return Err(Error::BadRequest("line too long".into()));
    }
    Ok(n)
}

fn trim_line(line: &[u8]) -> &[u8] {
    let mut end = line.len();
    while end > 0 && (line[end - 1] == b'\n' || line[end - 1] == b'\r') {
        end -= 1;
    }
    &line[..end]
}

/// Decode `%XX` escapes, and `+` as a space. Invalid escapes are left as
/// literal text rather than dropped, so a malformed path still round-trips into
/// something a handler can log.
pub fn percent_decode(input: &str) -> String {
    let bytes = input.as_bytes();
    let mut out: Vec<u8> = Vec::with_capacity(bytes.len());
    let mut i = 0;
    while i < bytes.len() {
        match bytes[i] {
            b'%' if i + 2 < bytes.len() => {
                let hi = hex_value(bytes[i + 1]);
                let lo = hex_value(bytes[i + 2]);
                match (hi, lo) {
                    (Some(h), Some(l)) => {
                        out.push(h * 16 + l);
                        i += 3;
                    }
                    _ => {
                        out.push(bytes[i]);
                        i += 1;
                    }
                }
            }
            b'+' => {
                out.push(b' ');
                i += 1;
            }
            b => {
                out.push(b);
                i += 1;
            }
        }
    }
    String::from_utf8_lossy(&out).into_owned()
}

fn hex_value(b: u8) -> Option<u8> {
    match b {
        b'0'..=b'9' => Some(b - b'0'),
        b'a'..=b'f' => Some(b - b'a' + 10),
        b'A'..=b'F' => Some(b - b'A' + 10),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::BufReader;

    fn parse(raw: &str) -> Result<Option<Request>> {
        let mut reader = BufReader::new(raw.as_bytes());
        Request::read_from(&mut reader, 64 * 1024)
    }

    #[test]
    fn parses_a_simple_get() {
        let req = parse("GET /users?page=2 HTTP/1.1\r\nHost: example.com\r\n\r\n")
            .unwrap()
            .unwrap();
        assert_eq!(req.method, Method::Get);
        assert_eq!(req.path, "/users");
        assert_eq!(req.query, "page=2");
        assert_eq!(req.headers.get("host"), Some("example.com"));
        assert_eq!(req.body().is_empty(), Some(true));
    }

    #[test]
    fn reads_a_body_of_the_declared_length() {
        let req = parse("POST /x HTTP/1.1\r\nContent-Length: 5\r\n\r\nhello")
            .unwrap()
            .unwrap();
        assert_eq!(req.body_str().unwrap(), "hello");
    }

    #[test]
    fn body_beyond_content_length_is_not_consumed() {
        // The extra bytes belong to the next request on a keep-alive connection.
        let req = parse("POST /x HTTP/1.1\r\nContent-Length: 2\r\n\r\nhiEXTRA")
            .unwrap()
            .unwrap();
        assert_eq!(req.body_str().unwrap(), "hi");
    }

    #[test]
    fn clean_eof_is_not_an_error() {
        assert!(parse("").unwrap().is_none());
    }

    #[test]
    fn decodes_percent_escapes_in_the_path() {
        let req = parse("GET /a%20b/c%2Fd HTTP/1.1\r\n\r\n").unwrap().unwrap();
        assert_eq!(req.path, "/a b/c/d");
    }

    #[test]
    fn decodes_query_parameters() {
        let req = parse("GET /s?q=hello+world&n=%41 HTTP/1.1\r\n\r\n")
            .unwrap()
            .unwrap();
        assert_eq!(req.query_param("q").as_deref(), Some("hello world"));
        assert_eq!(req.query_param("n").as_deref(), Some("A"));
        assert_eq!(req.query_param("missing"), None);
    }

    #[test]
    fn rejects_bad_input() {
        assert!(parse("BREW /coffee HTTP/1.1\r\n\r\n").is_err());
        assert!(parse("GET\r\n\r\n").is_err());
        assert!(parse("GET / HTTP/1.1\r\nNoColonHere\r\n\r\n").is_err());
        assert!(parse("POST /x HTTP/1.1\r\nContent-Length: abc\r\n\r\n").is_err());
    }

    #[test]
    fn rejects_chunked_rather_than_mangling_it() {
        let err = parse("POST /x HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\n").unwrap_err();
        assert!(matches!(err, Error::Unsupported(_)));
    }

    #[test]
    fn enforces_the_body_limit() {
        let mut reader = BufReader::new(&b"POST /x HTTP/1.1\r\nContent-Length: 100\r\n\r\n"[..]);
        let err = Request::read_from(&mut reader, 10).unwrap_err();
        assert!(matches!(err, Error::BodyTooLarge { limit: 10 }));
    }

    #[test]
    fn keep_alive_defaults_by_version() {
        let one_one = parse("GET / HTTP/1.1\r\n\r\n").unwrap().unwrap();
        assert!(one_one.keep_alive());

        let one_zero = parse("GET / HTTP/1.0\r\n\r\n").unwrap().unwrap();
        assert!(!one_zero.keep_alive());

        let closing = parse("GET / HTTP/1.1\r\nConnection: close\r\n\r\n")
            .unwrap()
            .unwrap();
        assert!(!closing.keep_alive());
    }

    #[test]
    fn a_line_that_never_ends_is_capped_rather_than_buffered() {
        // A megabyte with no newline in it. The cap has to bound what is read,
        // not just reject it once it is all in memory.
        let source = std::io::repeat(b'a').take(1024 * 1024);
        let mut reader = BufReader::new(source);
        let mut line = Vec::new();

        let err = read_line(&mut reader, &mut line).unwrap_err();
        assert!(matches!(err, Error::BadRequest(_)), "got {err:?}");
        assert!(
            line.len() <= MAX_LINE + 1,
            "buffered {} bytes for an {MAX_LINE} byte cap",
            line.len()
        );
    }

    #[test]
    fn a_request_can_be_built_without_a_socket() {
        // A downstream test of a handler has no listener and no parser, so this
        // is the only way it gets a Request at all.
        let req = Request::new(Method::Post, "/users/42?verbose=1")
            .with_header("Content-Type", "application/json")
            .with_param("id", "42")
            .with_body(r#"{"name":"joist"}"#);

        assert_eq!(req.method, Method::Post);
        assert_eq!(req.path, "/users/42");
        assert_eq!(req.query, "verbose=1");
        assert_eq!(req.query_param("verbose").as_deref(), Some("1"));
        assert_eq!(req.param("id"), Some("42"));
        assert_eq!(req.content_type(), Some("application/json"));
        assert_eq!(
            req.json().unwrap().get("name").and_then(Json::as_str),
            Some("joist")
        );
    }

    #[test]
    fn a_built_request_decodes_its_path_like_the_parser_does() {
        let req = Request::new(Method::Get, "/a%20b/c");
        assert_eq!(req.path, "/a b/c");
        assert_eq!(req.target, "/a%20b/c");
        assert_eq!(req.version, "HTTP/1.1");
        assert!(req.keep_alive());
    }

    #[test]
    fn debug_does_not_print_the_body() {
        let req = Request::new(Method::Post, "/login").with_body("password=hunter2");
        let text = format!("{req:?}");
        assert!(text.contains("Body(16 bytes)"), "got {text}");
        assert!(!text.contains("hunter2"), "got {text}");
    }

    #[test]
    fn parses_a_json_body() {
        let req = parse("POST /x HTTP/1.1\r\nContent-Length: 13\r\n\r\n{\"ok\":true}\r\n")
            .unwrap()
            .unwrap();
        // Content-Length covers the trailing CRLF, so trim before parsing.
        let value = Json::parse(req.body_str().unwrap().trim()).unwrap();
        assert_eq!(value.get("ok").and_then(Json::as_bool), Some(true));
    }
}