nitr-http 0.0.0-beta.7

The Nitr HTTP server layer: hyper server, configuration, and the request/response Lua bridge.
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
// SPDX-License-Identifier: MIT OR Apache-2.0
// This file is part of Nitr.
// See https://nitrweb.com/ for more information
// Copyright (C) 2024-present Jose Quintana <joseluisq.net>

//! Route `input` declarations: what a route accepts (`body`, `query`,
//! `params`, `headers`), compiled at load and enforced in Rust before the
//! handler runs. [`run`] is the enforcement; [`spool`], [`multipart`] and
//! [`raw`] handle uploads (behind the `multipart` feature).

use std::sync::Arc;

use hyper::header::{CONTENT_TYPE, HeaderMap};
use mlua::{Lua, Table, UserData, Value};
use nitr_std::validation::{
    CompiledSchema, compile_file_rule, compile_schema, compile_text_schema,
};

#[cfg(feature = "multipart")]
mod multipart;
#[cfg(feature = "multipart")]
mod raw;
pub(crate) mod run;
#[cfg(feature = "multipart")]
pub(crate) mod spool;

/// The media types a body declaration may accept.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Content {
    /// `application/json` (also a body with no `Content-Type` at all).
    Json,
    /// `application/x-www-form-urlencoded`.
    Form,
    /// `multipart/form-data`.
    Multipart,
    /// Anything: the whole body is one file.
    Raw,
}

impl Content {
    fn parse(name: &str) -> Option<Self> {
        Some(match name {
            "json" => Self::Json,
            "form" => Self::Form,
            "multipart" => Self::Multipart,
            "raw" => Self::Raw,
            _ => None?,
        })
    }

    /// The media type named in a 415.
    pub(crate) fn label(self) -> &'static str {
        match self {
            Self::Json => "application/json",
            Self::Form => "application/x-www-form-urlencoded",
            Self::Multipart => "multipart/form-data",
            Self::Raw => "*/*",
        }
    }

    fn accepts(self, media_type: Option<&str>) -> bool {
        match self {
            Self::Json => matches!(media_type, None | Some("application/json")),
            Self::Form => media_type == Some("application/x-www-form-urlencoded"),
            Self::Multipart => media_type == Some("multipart/form-data"),
            Self::Raw => true,
        }
    }
}

/// What the body is validated against.
#[derive(Debug, Clone)]
pub(crate) enum BodyRule {
    /// A schema over the decoded fields (JSON, form, multipart).
    Schema(CompiledSchema),
    /// One `file` rule over the whole body (`"raw"`). Only a build with
    /// the `multipart` feature can reach it (the declaration is refused
    /// at load otherwise), so the schema is read only there.
    File(#[cfg_attr(not(feature = "multipart"), allow(dead_code))] CompiledSchema),
}

/// What the deployment allows, for load-time checks.
#[derive(Debug, Clone, Default)]
pub(crate) struct InputEnv {
    /// `[multipart] upload_dir`: needed by any `file` rule.
    pub(crate) upload_root: Option<std::sync::Arc<std::path::PathBuf>>,
    /// URL paths the server answers before the router (the OpenAPI
    /// document, the Swagger UI page and everything under it), each with
    /// the configuration key that claimed it: a route there is a
    /// compile-time error naming the key.
    pub(crate) reserved: Vec<(String, &'static str)>,
    /// `[rate_limit] trust_forwarded_for`, for a route's own limit.
    pub(crate) trust_forwarded_for: bool,
    /// A missing `app:static` directory is skipped with a warning instead
    /// of refusing the load (`Config::static_dirs_optional`).
    pub(crate) static_dirs_optional: bool,
}

/// A route's compiled input declaration.
#[derive(Debug, Clone)]
pub(crate) struct InputSchemas {
    pub(crate) body: Option<(BodyRule, Vec<Content>)>,
    pub(crate) query: Option<CompiledSchema>,
    pub(crate) params: Option<CompiledSchema>,
    pub(crate) headers: Option<CompiledSchema>,
    /// The route-level `strict` override.
    pub(crate) strict: Option<bool>,
}

/// The per-state userdata a compiled chain points at, so the budgeted
/// validation function can reach its schemas from Lua arguments.
pub(crate) struct InputHolder(pub(crate) Arc<InputSchemas>);

impl UserData for InputHolder {}

const INPUT_KEYS: &[&str] = &["body", "query", "params", "headers", "strict"];

fn site_error(site: &str, msg: impl std::fmt::Display) -> mlua::Error {
    mlua::Error::RuntimeError(format!("{site}: {msg}"))
}

/// The parameter names a route pattern captures (`:id` → `id`, a
/// trailing `*rest` → `rest`, a bare `*` → `splat`).
pub(crate) fn param_names(path: &str) -> Vec<String> {
    let segments: Vec<&str> = path.split('/').collect();
    let last = segments.len().saturating_sub(1);
    segments
        .iter()
        .enumerate()
        .filter_map(|(i, seg)| match *seg {
            "*" if i == last => Some("splat".to_string()),
            s if s.starts_with(':') && s.len() > 1 => Some(s[1..].to_string()),
            s if s.starts_with('*') && s.len() > 1 && i == last => Some(s[1..].to_string()),
            _ => None,
        })
        .collect()
}

fn compile_body(
    lua: &Lua,
    value: Value,
    env: &InputEnv,
    site: &str,
) -> mlua::Result<(BodyRule, Vec<Content>)> {
    let what = format!("{site}: input.body");
    let (schema_value, file_value, content) = match &value {
        // `{ schema = S, content = {...} }` or `{ file = R, content = {...} }`:
        // a table with those keys and no rule `type`.
        Value::Table(t)
            if t.get::<Value>("type")?.is_nil()
                && (!t.get::<Value>("schema")?.is_nil()
                    || !t.get::<Value>("file")?.is_nil()
                    || !t.get::<Value>("content")?.is_nil()) =>
        {
            for pair in t.pairs::<Value, Value>() {
                let (key, _) = pair?;
                let key = match key {
                    Value::String(s) => s.to_string_lossy().to_string(),
                    _ => return Err(site_error(&what, "keys must be strings")),
                };
                if !["schema", "file", "content"].contains(&key.as_str()) {
                    return Err(site_error(
                        &what,
                        format!("unknown key `{key}` (allowed: schema, file, content)"),
                    ));
                }
            }
            let content: Option<Vec<String>> = t.get("content")?;
            (t.get::<Value>("schema")?, t.get::<Value>("file")?, content)
        }
        other => (other.clone(), Value::Nil, None),
    };
    let contents: Vec<Content> = match content {
        None => vec![Content::Json, Content::Form],
        Some(names) => {
            if names.is_empty() {
                return Err(site_error(&what, "`content` must not be empty"));
            }
            let mut out = Vec::new();
            for name in names {
                let content = Content::parse(&name).ok_or_else(|| {
                    site_error(
                        &what,
                        format!("unknown content `{name}` (expected json, form, multipart or raw)"),
                    )
                })?;
                if !out.contains(&content) {
                    out.push(content);
                }
            }
            out
        }
    };
    let rule = match (schema_value, file_value) {
        (Value::Nil, Value::Nil) => {
            return Err(site_error(&what, "needs a `schema` or a `file` rule"));
        }
        (schema, Value::Nil) => BodyRule::Schema(compile_schema(lua, schema, &what)?),
        (Value::Nil, file) => {
            if contents != [Content::Raw] {
                return Err(site_error(
                    &what,
                    "a `file` body needs `content = { \"raw\" }`",
                ));
            }
            BodyRule::File(compile_file_rule(lua, file, &what)?)
        }
        _ => {
            return Err(site_error(
                &what,
                "`schema` and `file` are alternatives; give one",
            ));
        }
    };
    let has_files = match &rule {
        BodyRule::Schema(s) => s.has_file_rules(),
        BodyRule::File(_) => true,
    };
    if matches!(rule, BodyRule::Schema(_)) && contents.contains(&Content::Raw) {
        return Err(site_error(
            &what,
            "`content = { \"raw\" }` takes a `file` rule, not a schema",
        ));
    }
    if has_files
        && !contents
            .iter()
            .any(|c| matches!(c, Content::Multipart | Content::Raw))
    {
        return Err(site_error(
            &what,
            "a `file` rule needs `content = { \"multipart\" }` (or \"raw\"), which is opt-in",
        ));
    }
    let needs_uploads = contents
        .iter()
        .any(|c| matches!(c, Content::Multipart | Content::Raw));
    if needs_uploads {
        if cfg!(not(feature = "multipart")) {
            return Err(site_error(
                &what,
                "multipart and raw bodies need the `multipart` Cargo feature: rebuild with `--features multipart` (or `all`)",
            ));
        }
        if has_files && env.upload_root.is_none() {
            return Err(site_error(
                &what,
                "a `file` rule needs `[multipart] upload_dir`: set it to the directory validated uploads are spooled and saved under",
            ));
        }
    }
    Ok((rule, contents))
}

impl InputSchemas {
    /// Compiles a route's `input = { ... }` table. `site` names the route
    /// and its registration line for errors.
    pub(crate) fn parse(
        lua: &Lua,
        table: &Table,
        route_params: &[String],
        env: &InputEnv,
        site: &str,
    ) -> mlua::Result<Self> {
        for pair in table.pairs::<Value, Value>() {
            let (key, _) = pair?;
            let key = match key {
                Value::String(s) => s.to_string_lossy().to_string(),
                _ => return Err(site_error(site, "input keys must be strings")),
            };
            if !INPUT_KEYS.contains(&key.as_str()) {
                return Err(site_error(
                    site,
                    format!(
                        "unknown input key `{key}` (allowed: {})",
                        INPUT_KEYS.join(", ")
                    ),
                ));
            }
        }
        let body = match table.get::<Value>("body")? {
            Value::Nil => None,
            value => Some(compile_body(lua, value, env, site)?),
        };
        let text_part = |name: &str| -> mlua::Result<Option<CompiledSchema>> {
            match table.get::<Value>(name)? {
                Value::Nil => Ok(None),
                value => Ok(Some(compile_text_schema(
                    lua,
                    value,
                    &format!("{site}: input.{name}"),
                )?)),
            }
        };
        let query = text_part("query")?;
        let params = text_part("params")?;
        let headers = text_part("headers")?;
        if let Some(params) = &params {
            for name in params.field_names() {
                if !route_params.iter().any(|p| p == name) {
                    return Err(site_error(
                        site,
                        format!(
                            "input.params names `{name}`, which the route pattern does not capture (captured: {})",
                            if route_params.is_empty() {
                                "none".to_string()
                            } else {
                                route_params.join(", ")
                            }
                        ),
                    ));
                }
            }
        }
        if let Some(headers) = &headers {
            for name in headers.field_names() {
                if name != name.to_ascii_lowercase() {
                    return Err(site_error(
                        site,
                        format!(
                            "input.headers names `{name}`: header names are lowercase (`{}`)",
                            name.to_ascii_lowercase()
                        ),
                    ));
                }
            }
        }
        let strict = match table.get::<Value>("strict")? {
            Value::Nil => None,
            Value::Boolean(b) => Some(b),
            other => {
                return Err(site_error(
                    site,
                    format!(
                        "input.strict must be true or false, got {}",
                        other.type_name()
                    ),
                ));
            }
        };
        Ok(Self {
            body,
            query,
            params,
            headers,
            strict,
        })
    }

    /// Which body media type the request carries, or the list a 415 names.
    pub(crate) fn negotiate(&self, headers: &HeaderMap) -> Result<Option<Content>, String> {
        let Some((_, contents)) = &self.body else {
            return Ok(None);
        };
        let media_type = headers
            .get(CONTENT_TYPE)
            .and_then(|v| v.to_str().ok())
            .map(|s| {
                s.split(';')
                    .next()
                    .unwrap_or_default()
                    .trim()
                    .to_ascii_lowercase()
            });
        for content in contents {
            if content.accepts(media_type.as_deref()) {
                return Ok(Some(*content));
            }
        }
        Err(contents
            .iter()
            .map(|c| c.label())
            .collect::<Vec<_>>()
            .join(", "))
    }

    /// Whether any part declares a `file` rule.
    pub(crate) fn has_file_rules(&self) -> bool {
        matches!(&self.body, Some((BodyRule::File(_), _)))
            || matches!(&self.body, Some((BodyRule::Schema(s), _)) if s.has_file_rules())
    }

    /// Whether this request may spool uploads (multipart or raw bodies).
    #[cfg_attr(not(feature = "multipart"), allow(dead_code))]
    pub(crate) fn needs_spool(&self, content: Option<Content>) -> bool {
        matches!(content, Some(Content::Multipart | Content::Raw))
    }
}

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

    #[test]
    fn route_patterns_yield_their_parameter_names() {
        assert_eq!(param_names("/users/:id/posts/:post"), vec!["id", "post"]);
        assert_eq!(param_names("/files/*"), vec!["splat"]);
        assert_eq!(param_names("/files/*rest"), vec!["rest"]);
        assert!(param_names("/").is_empty());
    }

    #[test]
    fn content_negotiation_ignores_parameters_and_names_the_accepted_types() {
        let lua = Lua::new();
        let fields: Table = lua.load(r#"{ a = "string" }"#).eval().unwrap();
        let input = lua.create_table().unwrap();
        input.set("body", fields).unwrap();
        let schemas =
            InputSchemas::parse(&lua, &input, &[], &InputEnv::default(), "route").unwrap();
        let mut headers = HeaderMap::new();
        headers.insert(
            CONTENT_TYPE,
            "application/json; charset=utf-8".parse().unwrap(),
        );
        assert_eq!(schemas.negotiate(&headers).unwrap(), Some(Content::Json));
        headers.insert(CONTENT_TYPE, "text/plain".parse().unwrap());
        assert_eq!(
            schemas.negotiate(&headers).unwrap_err(),
            "application/json, application/x-www-form-urlencoded"
        );
        assert_eq!(
            schemas.negotiate(&HeaderMap::new()).unwrap(),
            Some(Content::Json)
        );
    }

    #[test]
    fn input_declarations_are_checked_at_load() {
        let lua = Lua::new();
        let parse = |def: &str, params: &[&str]| -> Result<InputSchemas, String> {
            let table: Table = lua.load(def).eval().unwrap();
            let params: Vec<String> = params.iter().map(|p| (*p).to_string()).collect();
            InputSchemas::parse(
                &lua,
                &table,
                &params,
                &InputEnv::default(),
                "POST /x (app.lua:1)",
            )
            .map_err(|e| e.to_string())
        };
        assert!(parse(r#"{ params = { id = "integer" } }"#, &["id"]).is_ok());
        for (def, needle) in [
            (r#"{ params = { id = "integer" } }"#, "does not capture"),
            (
                r#"{ headers = { ["X-Team"] = "string" } }"#,
                "header names are lowercase",
            ),
            (
                r#"{ bodyy = { a = "string" } }"#,
                "unknown input key `bodyy`",
            ),
            (
                r#"{ body = { schema = { a = "string" }, content = { "xml" } } }"#,
                "unknown content `xml`",
            ),
            (
                r#"{ body = { schema = { a = "string" }, content = { "raw" } } }"#,
                "takes a `file` rule",
            ),
            (
                r#"{ body = { content = { "json" } } }"#,
                "needs a `schema` or a `file`",
            ),
            (
                r#"{ body = { schema = { f = { type = "file", max_bytes = 1 } } } }"#,
                "needs `content = { \"multipart\" }`",
            ),
            (
                r#"{ query = { a = { type = "any", max_bytes = 1 } } }"#,
                "text input cannot carry",
            ),
            (r#"{ strict = "yes" }"#, "must be true or false"),
        ] {
            let err = parse(def, &[]).expect_err(def);
            assert!(err.contains(needle), "{def}: {err}");
        }
    }
}