a3s-boot 0.1.0

Adapter-first modular Rust web framework for A3S inspired by Nest.js
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
use super::header::{
    accepts_event_stream_response, accepts_json_response, get_header, is_json_media_type,
    matches_media_type, normalize_header_name, normalize_headers, parse_content_length,
    parse_cookie_header_values, strict_content_length_values, validate_header_name,
    validate_header_value,
};
use super::method::HttpMethod;
use super::query::{parse_query, parse_query_pairs, split_path_query};
use crate::percent::validate_percent_encoding;
use crate::{BootError, Result};
use serde::{de::DeserializeOwned, Serialize};
use std::collections::BTreeMap;

/// Framework-neutral HTTP request passed to Boot route handlers.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BootRequest {
    pub method: HttpMethod,
    pub path: String,
    pub query_string: Option<String>,
    pub query: BTreeMap<String, String>,
    pub params: BTreeMap<String, String>,
    pub headers: BTreeMap<String, String>,
    pub appended_headers: Vec<(String, String)>,
    pub body: Vec<u8>,
}

impl BootRequest {
    pub fn new(method: HttpMethod, path: impl Into<String>) -> Self {
        let (path, query_string, query) = split_path_query(path.into());
        Self {
            method,
            path,
            query_string,
            query,
            params: BTreeMap::new(),
            headers: BTreeMap::new(),
            appended_headers: Vec::new(),
            body: Vec::new(),
        }
    }

    pub fn method(&self) -> HttpMethod {
        self.method
    }

    pub fn path(&self) -> &str {
        &self.path
    }

    pub fn query_string(&self) -> Option<&str> {
        self.query_string.as_deref()
    }

    pub fn with_query_string(mut self, query_string: impl Into<String>) -> Self {
        let query_string = query_string.into();
        self.query = parse_query(&query_string);
        self.query_string = Some(query_string);
        self
    }

    pub fn with_path_params(mut self, params: BTreeMap<String, String>) -> Self {
        self.params = params;
        self
    }

    pub fn with_param(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.params.insert(name.into(), value.into());
        self
    }

    pub fn with_query_param(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.query.insert(name.into(), value.into());
        self.query_string = None;
        self
    }

    pub fn with_body(mut self, body: impl Into<Vec<u8>>) -> Self {
        self.body = body.into();
        self
    }

    pub fn body(&self) -> &[u8] {
        &self.body
    }

    pub fn into_body(self) -> Vec<u8> {
        self.body
    }

    pub fn with_text(self, body: impl Into<String>) -> Self {
        self.with_body(body.into())
            .with_header("content-type", "text/plain; charset=utf-8")
    }

    pub fn with_json<T>(self, body: &T) -> Result<Self>
    where
        T: Serialize,
    {
        let body = serde_json::to_vec(body).map_err(|err| BootError::Internal(err.to_string()))?;
        Ok(self
            .with_body(body)
            .with_header("content-type", "application/json"))
    }

    pub fn with_content_type(self, content_type: impl Into<String>) -> Self {
        self.with_header("content-type", content_type)
    }

    pub fn with_content_length(self, content_length: u64) -> Self {
        self.with_header("content-length", content_length.to_string())
    }

    pub fn with_header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.headers
            .insert(normalize_header_name(name), value.into());
        self
    }

    pub fn with_headers(mut self, headers: BTreeMap<String, String>) -> Self {
        self.headers = normalize_headers(headers);
        self
    }

    pub fn append_header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.appended_headers
            .push((normalize_header_name(name), value.into()));
        self
    }

    pub fn header_entries(&self) -> impl Iterator<Item = (&str, &str)> {
        self.headers
            .iter()
            .map(|(name, value)| (name.as_str(), value.as_str()))
            .chain(
                self.appended_headers
                    .iter()
                    .map(|(name, value)| (name.as_str(), value.as_str())),
            )
    }

    pub fn validate_headers(&self) -> Result<()> {
        for (name, value) in self.header_entries() {
            validate_request_header(name, value)?;
        }

        Ok(())
    }

    pub fn text(&self) -> Result<String> {
        String::from_utf8(self.body.clone()).map_err(|err| BootError::BadRequest(err.to_string()))
    }

    pub fn param(&self, name: &str) -> Option<&str> {
        self.params.get(name).map(String::as_str)
    }

    pub fn params<T>(&self) -> Result<T>
    where
        T: DeserializeOwned,
    {
        let params = serde_urlencoded::to_string(&self.params)
            .map_err(|err| BootError::BadRequest(err.to_string()))?;
        serde_urlencoded::from_str(&params).map_err(|err| BootError::BadRequest(err.to_string()))
    }

    pub fn query_param(&self, name: &str) -> Option<&str> {
        self.query.get(name).map(String::as_str)
    }

    pub fn query_value(&self, name: &str) -> Result<Option<String>> {
        Ok(self
            .query_pairs()?
            .into_iter()
            .find_map(|(key, value)| (key == name).then_some(value)))
    }

    pub fn query_values(&self, name: &str) -> Result<Vec<String>> {
        Ok(self
            .query_pairs()?
            .into_iter()
            .filter_map(|(key, value)| (key == name).then_some(value))
            .collect())
    }

    pub fn query_pairs(&self) -> Result<Vec<(String, String)>> {
        match self.query_string.as_deref() {
            Some(query) => parse_query_pairs(query),
            None => Ok(self
                .query
                .iter()
                .map(|(key, value)| (key.clone(), value.clone()))
                .collect()),
        }
    }

    pub fn header(&self, name: &str) -> Option<&str> {
        get_header(&self.headers, name)
    }

    pub fn header_values(&self, name: &str) -> Vec<&str> {
        let mut values = self.header(name).into_iter().collect::<Vec<_>>();
        values.extend(
            self.appended_headers
                .iter()
                .filter(|(key, _)| key.eq_ignore_ascii_case(name))
                .map(|(_, value)| value.as_str()),
        );
        values
    }

    pub fn authorization(&self) -> Option<&str> {
        self.header_values("authorization").into_iter().next()
    }

    pub fn bearer_token(&self) -> Option<&str> {
        let authorization = self.authorization()?.trim();
        let mut parts = authorization.splitn(2, char::is_whitespace);
        let scheme = parts.next()?;
        let token = parts.next()?.trim();

        if scheme.eq_ignore_ascii_case("bearer") && !token.is_empty() {
            Some(token)
        } else {
            None
        }
    }

    pub fn require_bearer_token(&self) -> Result<&str> {
        self.bearer_token()
            .ok_or_else(|| BootError::Unauthorized("missing bearer token".to_string()))
    }

    pub fn cookie_pairs(&self) -> Result<Vec<(String, String)>> {
        parse_cookie_header_values(&self.header_values("cookie"))
    }

    pub fn cookie(&self, name: &str) -> Result<Option<String>> {
        Ok(self
            .cookie_pairs()?
            .into_iter()
            .find_map(|(key, value)| (key == name).then_some(value)))
    }

    pub fn require_cookie(&self, name: &str) -> Result<String> {
        self.cookie(name)?
            .ok_or_else(|| BootError::Unauthorized(format!("missing cookie: {name}")))
    }

    pub fn cookie_values(&self, name: &str) -> Result<Vec<String>> {
        Ok(self
            .cookie_pairs()?
            .into_iter()
            .filter_map(|(key, value)| (key == name).then_some(value))
            .collect())
    }

    pub fn cookies(&self) -> Result<BTreeMap<String, String>> {
        let mut cookies = BTreeMap::new();
        for (name, value) in self.cookie_pairs()? {
            cookies.entry(name).or_insert(value);
        }
        Ok(cookies)
    }

    pub fn content_type(&self) -> Option<&str> {
        self.header_values("content-type").into_iter().next()
    }

    pub fn content_length(&self) -> Result<Option<u64>> {
        let Some(content_length) = self.header_values("content-length").into_iter().next() else {
            return Ok(None);
        };

        parse_content_length(content_length)
            .map(Some)
            .ok_or_else(|| {
                BootError::BadRequest(format!("invalid content-length header: {content_length}"))
            })
    }

    pub fn strict_content_length(&self) -> Result<Option<u64>> {
        strict_content_length_values(
            self.header_values("content-length"),
            |content_length| {
                BootError::BadRequest(format!("invalid content-length header: {content_length}"))
            },
            |expected_content_length, content_length| {
                BootError::BadRequest(format!(
                    "conflicting content-length headers: {expected_content_length} != {content_length}"
                ))
            },
        )
    }

    pub fn validate_content_length(&self) -> Result<()> {
        let Some(content_length) = self.strict_content_length()? else {
            return Ok(());
        };
        let actual_body_length = self.body.len() as u64;
        if actual_body_length == content_length {
            return Ok(());
        }

        Err(BootError::BadRequest(format!(
            "content-length header does not match request body length: expected {content_length}, got {actual_body_length}"
        )))
    }

    pub fn validate_body_limit(&self, body_limit: usize) -> Result<()> {
        if self
            .strict_content_length()?
            .is_some_and(|content_length| content_length > body_limit as u64)
            || self.body.len() > body_limit
        {
            return Err(BootError::PayloadTooLarge(format!(
                "request body exceeds {body_limit} bytes"
            )));
        }

        Ok(())
    }

    pub fn validate(&self) -> Result<()> {
        self.validate_headers()?;
        self.validate_content_length()
    }

    pub fn validate_with_body_limit(&self, body_limit: usize) -> Result<()> {
        self.validate_headers()?;
        self.validate_body_limit(body_limit)?;
        self.validate_content_length()
    }

    pub fn is_content_type(&self, media_type: &str) -> bool {
        self.content_type()
            .is_some_and(|content_type| matches_media_type(content_type, media_type))
    }

    pub fn is_json_content_type(&self) -> bool {
        self.content_type().is_some_and(is_json_media_type)
    }

    pub fn require_json_content_type(&self) -> Result<()> {
        if self.is_json_content_type() {
            return Ok(());
        }

        let message = match self.content_type() {
            Some(content_type) => format!("expected JSON content type, got {content_type}"),
            None => "expected JSON content type".to_string(),
        };
        Err(BootError::UnsupportedMediaType(message))
    }

    pub fn accepts_json(&self) -> bool {
        accepts_json_response(&self.header_values("accept"))
    }

    pub fn require_accepts_json(&self) -> Result<()> {
        if self.accepts_json() {
            return Ok(());
        }

        Err(BootError::NotAcceptable(
            "expected client to accept JSON response".to_string(),
        ))
    }

    pub fn accepts_event_stream(&self) -> bool {
        accepts_event_stream_response(&self.header_values("accept"))
    }

    pub fn require_accepts_event_stream(&self) -> Result<()> {
        if self.accepts_event_stream() {
            return Ok(());
        }

        Err(BootError::NotAcceptable(
            "expected client to accept text/event-stream response".to_string(),
        ))
    }

    pub fn json<T>(&self) -> Result<T>
    where
        T: DeserializeOwned,
    {
        serde_json::from_slice(&self.body).map_err(|err| BootError::BadRequest(err.to_string()))
    }

    pub fn json_with_content_type<T>(&self) -> Result<T>
    where
        T: DeserializeOwned,
    {
        self.require_json_content_type()?;
        self.json()
    }

    pub fn query<T>(&self) -> Result<T>
    where
        T: DeserializeOwned,
    {
        let query = match self.query_string.as_deref() {
            Some(query) => {
                validate_percent_encoding(query)?;
                query.to_string()
            }
            None => serde_urlencoded::to_string(&self.query)
                .map_err(|err| BootError::BadRequest(err.to_string()))?,
        };
        serde_urlencoded::from_str(&query).map_err(|err| BootError::BadRequest(err.to_string()))
    }
}

fn validate_request_header(name: &str, value: &str) -> Result<()> {
    validate_header_name(name).map_err(|message| {
        BootError::BadRequest(format!("invalid request header name {name:?}: {message}"))
    })?;
    validate_header_value(value).map_err(|message| {
        BootError::BadRequest(format!(
            "invalid request header value for {name:?}: {message}"
        ))
    })
}