armature-core 0.9.0

High-performance async HTTP framework core - routing, handlers, middleware
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
//! Form processing and multipart support

use crate::Error;
use serde::de::DeserializeOwned;
use std::collections::HashMap;

/// Parse URL-encoded form data
pub fn parse_form<T: DeserializeOwned>(body: &[u8]) -> Result<T, Error> {
    serde_urlencoded::from_bytes(body)
        .map_err(|e| Error::BadRequest(format!("Failed to parse form data: {}", e)))
}

/// Parse URL-encoded form data into a HashMap
pub fn parse_form_map(body: &[u8]) -> Result<HashMap<String, String>, Error> {
    let form_data: Vec<(String, String)> = serde_urlencoded::from_bytes(body)
        .map_err(|e| Error::BadRequest(format!("Failed to parse form data: {}", e)))?;

    Ok(form_data.into_iter().collect())
}

/// Multipart form field
#[derive(Debug, Clone)]
pub struct FormField {
    /// Field name
    pub name: String,

    /// Field value (for text fields)
    pub value: Option<String>,

    /// File data (for file fields)
    pub file: Option<FormFile>,
}

/// Uploaded file data
#[derive(Debug, Clone)]
pub struct FormFile {
    /// Original filename
    pub filename: String,

    /// Content type (MIME type)
    pub content_type: String,

    /// File size in bytes
    pub size: usize,

    /// File data
    pub data: Vec<u8>,
}

impl FormFile {
    /// Create a new form file
    pub fn new(filename: String, content_type: String, data: Vec<u8>) -> Self {
        let size = data.len();
        Self {
            filename,
            content_type,
            size,
            data,
        }
    }

    /// Get file extension
    pub fn extension(&self) -> Option<&str> {
        self.filename.rsplit('.').next()
    }

    /// Check if file is an image
    pub fn is_image(&self) -> bool {
        self.content_type.starts_with("image/")
    }

    /// Check if file size exceeds limit
    pub fn exceeds_size(&self, max_bytes: usize) -> bool {
        self.size > max_bytes
    }

    /// Save file to disk
    pub fn save_to(&self, path: &str) -> Result<(), Error> {
        std::fs::write(path, &self.data)
            .map_err(|e| Error::Internal(format!("Failed to save file: {}", e)))
    }

    /// Save file to disk asynchronously
    pub async fn save_to_async(&self, path: &str) -> Result<(), Error> {
        tokio::fs::write(path, &self.data)
            .await
            .map_err(|e| Error::Internal(format!("Failed to save file: {}", e)))
    }
}

/// Save multiple files in parallel
///
/// This function saves multiple uploaded files concurrently, providing
/// significant performance improvements over sequential saves.
///
/// # Arguments
///
/// * `files` - Vector of tuples containing (file, destination_path)
///
/// # Returns
///
/// Returns a vector of saved file paths in the same order as input.
///
/// # Performance
///
/// - **Sequential:** O(n * disk_write_time)
/// - **Parallel:** O(max(disk_write_times))
/// - **Speedup:** 5-10x for batch file uploads
///
/// # Examples
///
/// ```no_run
/// # use armature_core::form::*;
/// # async fn example(files: Vec<FormFile>) -> Result<(), armature_core::Error> {
/// // Save 10 files in parallel (5-10x faster)
/// let file_paths: Vec<_> = files.iter()
///     .enumerate()
///     .map(|(i, file)| (file, format!("uploads/file_{}.dat", i)))
///     .collect();
///
/// let saved = save_files_parallel(file_paths).await?;
/// println!("Saved {} files", saved.len());
/// # Ok(())
/// # }
/// ```
pub async fn save_files_parallel(files: Vec<(&FormFile, String)>) -> Result<Vec<String>, Error> {
    use tokio::task::JoinSet;

    let mut set = JoinSet::new();

    for (file, path) in files {
        let data = file.data.clone();
        let path_clone = path.clone();

        set.spawn(async move {
            tokio::fs::write(&path_clone, &data)
                .await
                .map_err(|e| Error::Internal(format!("Failed to save file: {}", e)))?;
            Ok::<_, Error>(path_clone)
        });
    }

    let mut saved_paths = Vec::new();
    while let Some(result) = set.join_next().await {
        saved_paths.push(result.map_err(|e| Error::Internal(e.to_string()))??);
    }

    Ok(saved_paths)
}

/// Multipart form data parser
pub struct MultipartParser {
    boundary: String,
}

impl MultipartParser {
    /// Create a new multipart parser from Content-Type header
    pub fn from_content_type(content_type: &str) -> Result<Self, Error> {
        // Extract boundary from Content-Type header
        // Example: "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
        let boundary = content_type
            .split(';')
            .find_map(|part| {
                let part = part.trim();
                if part.starts_with("boundary=") {
                    Some(
                        part.trim_start_matches("boundary=")
                            .trim_matches('"')
                            .to_string(),
                    )
                } else {
                    None
                }
            })
            .ok_or_else(|| Error::BadRequest("Missing boundary in Content-Type".to_string()))?;

        Ok(Self { boundary })
    }

    /// Parse multipart form data
    pub fn parse(&self, body: &[u8]) -> Result<Vec<FormField>, Error> {
        let mut fields = Vec::new();
        let boundary_marker = format!("--{}", self.boundary);
        let delimiter = boundary_marker.as_bytes();

        // Locate every boundary delimiter with SIMD-accelerated byte search.
        let finder = memchr::memmem::Finder::new(delimiter);
        let positions: Vec<usize> = finder.find_iter(body).collect();

        // Each part lives between two consecutive delimiters.
        for pair in positions.windows(2) {
            let mut part = &body[pair[0] + delimiter.len()..pair[1]];

            // The closing delimiter ("--boundary--") is not a part.
            if part.starts_with(b"--") {
                continue;
            }

            // Strip the CRLF terminating the boundary line...
            if let Some(rest) = part.strip_prefix(b"\r\n") {
                part = rest;
            } else if let Some(rest) = part.strip_prefix(b"\n") {
                part = rest;
            }

            // ...and the CRLF preceding the next boundary.
            if let Some(rest) = part.strip_suffix(b"\r\n") {
                part = rest;
            } else if let Some(rest) = part.strip_suffix(b"\n") {
                part = rest;
            }

            // Parse each part
            if let Some(field) = self.parse_part(part)? {
                fields.push(field);
            }
        }

        Ok(fields)
    }

    /// Parse a single multipart part
    fn parse_part(&self, part: &[u8]) -> Result<Option<FormField>, Error> {
        if part.is_empty() {
            return Ok(None);
        }

        // Split the header block from the body at the first blank line.
        // The body must stay a raw byte slice: file uploads are arbitrary binary.
        let (header_block, content) = match memchr::memmem::find(part, b"\r\n\r\n") {
            Some(pos) => (&part[..pos], &part[pos + 4..]),
            None => match memchr::memmem::find(part, b"\n\n") {
                Some(pos) => (&part[..pos], &part[pos + 2..]),
                None => (part, &part[part.len()..]),
            },
        };

        // Headers are ASCII per RFC 7578, so parsing them as UTF-8 is safe.
        let headers = String::from_utf8_lossy(header_block);

        // Parse headers
        let mut name = None;
        let mut filename = None;
        let mut content_type = None;

        for line in headers.lines() {
            if line.starts_with("Content-Disposition:") {
                // Parse name and filename
                for attr in line.split(';') {
                    let attr = attr.trim();
                    if attr.starts_with("name=") {
                        name = Some(
                            attr.trim_start_matches("name=")
                                .trim_matches('"')
                                .to_string(),
                        );
                    } else if attr.starts_with("filename=") {
                        filename = Some(
                            attr.trim_start_matches("filename=")
                                .trim_matches('"')
                                .to_string(),
                        );
                    }
                }
            } else if line.starts_with("Content-Type:") {
                content_type = Some(line.trim_start_matches("Content-Type:").trim().to_string());
            }
        }

        let name = name.ok_or_else(|| Error::BadRequest("Missing field name".to_string()))?;

        // Create field
        if let Some(filename) = filename {
            // File field: keep the body bytes exactly as uploaded
            let file = FormFile::new(
                filename,
                content_type.unwrap_or_else(|| "application/octet-stream".to_string()),
                content.to_vec(),
            );
            Ok(Some(FormField {
                name,
                value: None,
                file: Some(file),
            }))
        } else {
            // Text field
            Ok(Some(FormField {
                name,
                value: Some(String::from_utf8_lossy(content).into_owned()),
                file: None,
            }))
        }
    }

    /// Convert parsed fields to HashMap
    pub fn to_map(fields: Vec<FormField>) -> HashMap<String, String> {
        fields
            .into_iter()
            .filter_map(|field| field.value.map(|value| (field.name, value)))
            .collect()
    }

    /// Get files from parsed fields
    pub fn get_files(fields: &[FormField]) -> Vec<(String, &FormFile)> {
        fields
            .iter()
            .filter_map(|field| field.file.as_ref().map(|file| (field.name.clone(), file)))
            .collect()
    }
}

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

    #[test]
    fn test_parse_form_map() {
        let body = b"name=John+Doe&email=john%40example.com&age=30";
        let form = parse_form_map(body).unwrap();

        assert_eq!(form.get("name"), Some(&"John Doe".to_string()));
        assert_eq!(form.get("email"), Some(&"john@example.com".to_string()));
        assert_eq!(form.get("age"), Some(&"30".to_string()));
    }

    #[test]
    fn test_form_file_extension() {
        let file = FormFile::new(
            "document.pdf".to_string(),
            "application/pdf".to_string(),
            vec![1, 2, 3],
        );

        assert_eq!(file.extension(), Some("pdf"));
    }

    #[test]
    fn test_form_file_is_image() {
        let image = FormFile::new("photo.jpg".to_string(), "image/jpeg".to_string(), vec![]);
        assert!(image.is_image());

        let doc = FormFile::new("doc.pdf".to_string(), "application/pdf".to_string(), vec![]);
        assert!(!doc.is_image());
    }

    #[test]
    fn test_form_file_size_check() {
        let file = FormFile::new(
            "file.txt".to_string(),
            "text/plain".to_string(),
            vec![0; 1024], // 1KB
        );

        assert!(!file.exceeds_size(2048)); // 2KB limit
        assert!(file.exceeds_size(512)); // 512B limit
    }

    #[test]
    fn test_multipart_parser_from_content_type() {
        let content_type = "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW";
        let parser = MultipartParser::from_content_type(content_type).unwrap();

        assert_eq!(parser.boundary, "----WebKitFormBoundary7MA4YWxkTrZu0gW");
    }

    #[test]
    fn test_multipart_binary_roundtrip() {
        // Non-UTF-8 bytes, embedded CRLFs, and leading/trailing whitespace
        // must all survive the parse exactly as uploaded.
        let file_data: Vec<u8> = vec![
            b' ', b'\t', 0x00, 0xFF, 0xFE, b'\r', b'\n', 0x80, 0xC3, 0x01, b'\n', b' ',
        ];

        let mut body = Vec::new();
        body.extend_from_slice(b"--XBOUNDARY\r\n");
        body.extend_from_slice(
            b"Content-Disposition: form-data; name=\"file\"; filename=\"blob.bin\"\r\n",
        );
        body.extend_from_slice(b"Content-Type: application/octet-stream\r\n\r\n");
        body.extend_from_slice(&file_data);
        body.extend_from_slice(b"\r\n--XBOUNDARY\r\n");
        body.extend_from_slice(b"Content-Disposition: form-data; name=\"note\"\r\n\r\n");
        body.extend_from_slice(b"hello world");
        body.extend_from_slice(b"\r\n--XBOUNDARY--\r\n");

        let parser =
            MultipartParser::from_content_type("multipart/form-data; boundary=XBOUNDARY").unwrap();
        let fields = parser.parse(&body).unwrap();

        assert_eq!(fields.len(), 2);

        let file = fields[0].file.as_ref().unwrap();
        assert_eq!(fields[0].name, "file");
        assert_eq!(file.filename, "blob.bin");
        assert_eq!(file.content_type, "application/octet-stream");
        assert_eq!(file.data, file_data);
        assert_eq!(file.size, file_data.len());

        assert_eq!(fields[1].name, "note");
        assert_eq!(fields[1].value.as_deref(), Some("hello world"));
    }

    #[test]
    fn test_multipart_parser_missing_boundary() {
        let content_type = "multipart/form-data";
        let result = MultipartParser::from_content_type(content_type);

        assert!(result.is_err());
    }
}