armature-core 0.5.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
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
//! SmallVec-Based HTTP Header Storage
//!
//! This module provides a stack-allocated header map for typical HTTP requests.
//! Most requests have fewer than 8-12 headers, so storing them inline avoids
//! heap allocations entirely.
//!
//! ## Performance
//!
//! - **Inline storage**: Up to 12 headers stored on stack (no allocation)
//! - **O(n) lookup**: Linear scan, but N is typically small (<20)
//! - **Cache-friendly**: Contiguous memory layout
//! - **Zero heap alloc**: For typical requests
//!
//! ## Comparison
//!
//! | Operation | HashMap | HeaderMap (SmallVec) |
//! |-----------|---------|---------------------|
//! | Insert (first 12) | Heap alloc | Stack only |
//! | Lookup | O(1) hash | O(n) linear |
//! | Memory | ~48 bytes min + heap | ~384 bytes inline |
//! | Cache | Pointer chasing | Contiguous |
//!
//! For typical HTTP workloads with <12 headers, SmallVec is faster due to
//! avoiding allocator overhead and better cache locality.

use smallvec::SmallVec;
use std::collections::HashMap;
use std::fmt;

/// Number of headers to store inline (on stack).
/// 12 headers × 32 bytes = 384 bytes, reasonable stack footprint.
/// Most HTTP requests have 5-10 headers.
pub const INLINE_HEADERS: usize = 12;

/// A header name-value pair.
#[derive(Clone, PartialEq, Eq)]
pub struct Header {
    /// Header name (case-insensitive for lookup)
    pub name: String,
    /// Header value
    pub value: String,
}

impl Header {
    /// Create a new header
    #[inline]
    pub fn new(name: impl Into<String>, value: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            value: value.into(),
        }
    }

    /// Check if name matches (case-insensitive)
    #[inline]
    pub fn name_eq(&self, name: &str) -> bool {
        self.name.eq_ignore_ascii_case(name)
    }
}

impl fmt::Debug for Header {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}: {}", self.name, self.value)
    }
}

/// A compact header map using SmallVec for inline storage.
///
/// Stores up to 12 headers inline (on the stack), only allocating
/// on the heap if more headers are added.
///
/// # Example
///
/// ```rust
/// use armature_core::headers::HeaderMap;
///
/// let mut headers = HeaderMap::new();
/// headers.insert("Content-Type", "application/json");
/// headers.insert("Accept", "text/html");
///
/// assert_eq!(headers.get("content-type"), Some(&"application/json".to_string()));
/// assert!(headers.is_inline()); // Still on stack
/// ```
#[derive(Clone, Default)]
pub struct HeaderMap {
    inner: SmallVec<[Header; INLINE_HEADERS]>,
}

impl HeaderMap {
    /// Create a new empty header map.
    #[inline]
    pub const fn new() -> Self {
        Self {
            inner: SmallVec::new_const(),
        }
    }

    /// Create with pre-allocated capacity.
    ///
    /// If capacity <= INLINE_HEADERS, no heap allocation occurs.
    #[inline]
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            inner: SmallVec::with_capacity(capacity),
        }
    }

    /// Check if storage is inline (no heap allocation).
    #[inline]
    pub fn is_inline(&self) -> bool {
        !self.inner.spilled()
    }

    /// Get the number of headers.
    #[inline]
    pub fn len(&self) -> usize {
        self.inner.len()
    }

    /// Check if empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }

    /// Get header value by name (case-insensitive).
    #[inline]
    pub fn get(&self, name: &str) -> Option<&String> {
        self.inner
            .iter()
            .find(|h| h.name_eq(name))
            .map(|h| &h.value)
    }

    /// Get header value by name, with lowercase fallback.
    ///
    /// First tries exact match, then lowercase. This handles
    /// the common case where headers might be stored in different cases.
    #[inline]
    pub fn get_ignore_case(&self, name: &str) -> Option<&String> {
        self.get(name)
    }

    /// Check if header exists (case-insensitive).
    #[inline]
    pub fn contains(&self, name: &str) -> bool {
        self.inner.iter().any(|h| h.name_eq(name))
    }

    /// Check if header exists (case-insensitive).
    ///
    /// HashMap-compatible alias for [`contains`](Self::contains).
    #[inline]
    pub fn contains_key(&self, name: &str) -> bool {
        self.contains(name)
    }

    /// Insert a header, replacing any existing header with same name.
    ///
    /// Returns the old value if replaced.
    #[inline]
    pub fn insert(&mut self, name: impl Into<String>, value: impl Into<String>) -> Option<String> {
        let name = name.into();
        let value = value.into();

        // Check if header exists
        for h in &mut self.inner {
            if h.name_eq(&name) {
                let old = std::mem::replace(&mut h.value, value);
                return Some(old);
            }
        }

        // New header
        self.inner.push(Header { name, value });
        None
    }

    /// Append a header (allows duplicates).
    ///
    /// Unlike `insert`, this doesn't replace existing headers.
    /// Use for headers that can have multiple values (e.g., Set-Cookie).
    #[inline]
    pub fn append(&mut self, name: impl Into<String>, value: impl Into<String>) {
        self.inner.push(Header {
            name: name.into(),
            value: value.into(),
        });
    }

    /// Remove a header by name (case-insensitive).
    ///
    /// Returns the removed value if found.
    #[inline]
    pub fn remove(&mut self, name: &str) -> Option<String> {
        if let Some(pos) = self.inner.iter().position(|h| h.name_eq(name)) {
            Some(self.inner.remove(pos).value)
        } else {
            None
        }
    }

    /// Remove all headers with given name (case-insensitive).
    ///
    /// Returns number of headers removed.
    #[inline]
    pub fn remove_all(&mut self, name: &str) -> usize {
        let before = self.inner.len();
        self.inner.retain(|h| !h.name_eq(name));
        before - self.inner.len()
    }

    /// Iterate over all headers.
    #[inline]
    pub fn iter(&self) -> impl Iterator<Item = (&String, &String)> {
        self.inner.iter().map(|h| (&h.name, &h.value))
    }

    /// Iterate over header names.
    #[inline]
    pub fn names(&self) -> impl Iterator<Item = &String> {
        self.inner.iter().map(|h| &h.name)
    }

    /// Iterate over header names.
    ///
    /// HashMap-compatible alias for [`names`](Self::names).
    #[inline]
    pub fn keys(&self) -> impl Iterator<Item = &String> {
        self.names()
    }

    /// Iterate over header values.
    #[inline]
    pub fn values(&self) -> impl Iterator<Item = &String> {
        self.inner.iter().map(|h| &h.value)
    }

    /// Get all values for a header name (for multi-value headers).
    #[inline]
    pub fn get_all(&self, name: &str) -> Vec<&String> {
        self.inner
            .iter()
            .filter(|h| h.name_eq(name))
            .map(|h| &h.value)
            .collect()
    }

    /// Clear all headers.
    #[inline]
    pub fn clear(&mut self) {
        self.inner.clear();
    }

    /// Extend with headers from iterator.
    #[inline]
    pub fn extend<I, K, V>(&mut self, iter: I)
    where
        I: IntoIterator<Item = (K, V)>,
        K: Into<String>,
        V: Into<String>,
    {
        for (k, v) in iter {
            self.insert(k, v);
        }
    }

    /// Convert to HashMap (for compatibility).
    #[inline]
    pub fn to_hash_map(&self) -> HashMap<String, String> {
        self.inner
            .iter()
            .map(|h| (h.name.clone(), h.value.clone()))
            .collect()
    }

    /// Create from HashMap.
    #[inline]
    pub fn from_hash_map(map: HashMap<String, String>) -> Self {
        let mut headers = Self::with_capacity(map.len());
        for (k, v) in map {
            headers.inner.push(Header { name: k, value: v });
        }
        headers
    }

    // ========================================================================
    // Common Header Accessors
    // ========================================================================

    /// Get Content-Type header.
    #[inline]
    pub fn content_type(&self) -> Option<&String> {
        self.get("Content-Type")
    }

    /// Get Content-Length header as usize.
    #[inline]
    pub fn content_length(&self) -> Option<usize> {
        self.get("Content-Length")?.parse().ok()
    }

    /// Get Accept header.
    #[inline]
    pub fn accept(&self) -> Option<&String> {
        self.get("Accept")
    }

    /// Get Authorization header.
    #[inline]
    pub fn authorization(&self) -> Option<&String> {
        self.get("Authorization")
    }

    /// Get User-Agent header.
    #[inline]
    pub fn user_agent(&self) -> Option<&String> {
        self.get("User-Agent")
    }

    /// Get Host header.
    #[inline]
    pub fn host(&self) -> Option<&String> {
        self.get("Host")
    }

    /// Get Cookie header.
    #[inline]
    pub fn cookie(&self) -> Option<&String> {
        self.get("Cookie")
    }

    /// Check if Keep-Alive connection.
    #[inline]
    pub fn is_keep_alive(&self) -> bool {
        self.get("Connection")
            .map(|v| v.eq_ignore_ascii_case("keep-alive"))
            .unwrap_or(true) // HTTP/1.1 default is keep-alive
    }

    /// Check if chunked transfer encoding.
    #[inline]
    pub fn is_chunked(&self) -> bool {
        self.get("Transfer-Encoding")
            .map(|v| v.contains("chunked"))
            .unwrap_or(false)
    }

    /// Set Content-Type header.
    #[inline]
    pub fn set_content_type(&mut self, value: impl Into<String>) {
        self.insert("Content-Type", value);
    }

    /// Set Content-Length header.
    #[inline]
    pub fn set_content_length(&mut self, len: usize) {
        self.insert("Content-Length", len.to_string());
    }
}

impl fmt::Debug for HeaderMap {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_map()
            .entries(self.inner.iter().map(|h| (&h.name, &h.value)))
            .finish()
    }
}

impl<K, V> FromIterator<(K, V)> for HeaderMap
where
    K: Into<String>,
    V: Into<String>,
{
    fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
        let iter = iter.into_iter();
        let (min, max) = iter.size_hint();
        let mut map = HeaderMap::with_capacity(max.unwrap_or(min));
        for (k, v) in iter {
            map.insert(k, v);
        }
        map
    }
}

impl Extend<(String, String)> for HeaderMap {
    fn extend<I: IntoIterator<Item = (String, String)>>(&mut self, iter: I) {
        for (k, v) in iter {
            self.insert(k, v);
        }
    }
}

impl<'a> IntoIterator for &'a HeaderMap {
    type Item = (&'a String, &'a String);
    type IntoIter =
        std::iter::Map<std::slice::Iter<'a, Header>, fn(&'a Header) -> (&'a String, &'a String)>;

    fn into_iter(self) -> Self::IntoIter {
        self.inner.iter().map(|h| (&h.name, &h.value))
    }
}

impl IntoIterator for HeaderMap {
    type Item = (String, String);
    type IntoIter = std::iter::Map<
        smallvec::IntoIter<[Header; INLINE_HEADERS]>,
        fn(Header) -> (String, String),
    >;

    fn into_iter(self) -> Self::IntoIter {
        self.inner.into_iter().map(|h| (h.name, h.value))
    }
}

// Allow HashMap-like indexing
impl std::ops::Index<&str> for HeaderMap {
    type Output = String;

    fn index(&self, name: &str) -> &Self::Output {
        self.get(name).expect("header not found")
    }
}

// ============================================================================
// Conversion from/to HashMap for backwards compatibility
// ============================================================================

impl From<HashMap<String, String>> for HeaderMap {
    fn from(map: HashMap<String, String>) -> Self {
        Self::from_hash_map(map)
    }
}

impl From<HeaderMap> for HashMap<String, String> {
    fn from(map: HeaderMap) -> Self {
        map.to_hash_map()
    }
}

// ============================================================================
// Tests
// ============================================================================

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

    #[test]
    fn test_new_is_inline() {
        let headers = HeaderMap::new();
        assert!(headers.is_inline());
        assert!(headers.is_empty());
    }

    #[test]
    fn test_insert_and_get() {
        let mut headers = HeaderMap::new();
        headers.insert("Content-Type", "application/json");
        headers.insert("Accept", "text/html");

        assert_eq!(headers.len(), 2);
        assert_eq!(
            headers.get("Content-Type"),
            Some(&"application/json".to_string())
        );
        assert_eq!(
            headers.get("content-type"),
            Some(&"application/json".to_string())
        ); // case insensitive
    }

    #[test]
    fn test_insert_replaces() {
        let mut headers = HeaderMap::new();
        headers.insert("Content-Type", "text/plain");
        let old = headers.insert("Content-Type", "application/json");

        assert_eq!(old, Some("text/plain".to_string()));
        assert_eq!(headers.len(), 1);
        assert_eq!(
            headers.get("Content-Type"),
            Some(&"application/json".to_string())
        );
    }

    #[test]
    fn test_append_duplicates() {
        let mut headers = HeaderMap::new();
        headers.append("Set-Cookie", "session=abc");
        headers.append("Set-Cookie", "user=123");

        assert_eq!(headers.len(), 2);
        let cookies = headers.get_all("Set-Cookie");
        assert_eq!(cookies.len(), 2);
    }

    #[test]
    fn test_remove() {
        let mut headers = HeaderMap::new();
        headers.insert("Content-Type", "application/json");
        headers.insert("Accept", "text/html");

        let removed = headers.remove("Content-Type");
        assert_eq!(removed, Some("application/json".to_string()));
        assert_eq!(headers.len(), 1);
        assert!(!headers.contains("Content-Type"));
    }

    #[test]
    fn test_inline_capacity() {
        let mut headers = HeaderMap::new();

        // Add INLINE_HEADERS headers
        for i in 0..INLINE_HEADERS {
            headers.insert(format!("Header-{}", i), format!("Value-{}", i));
        }

        assert!(headers.is_inline()); // Still inline

        // Add one more
        headers.insert("Extra-Header", "Extra-Value");

        // Now it should have spilled to heap
        assert!(!headers.is_inline());
    }

    #[test]
    fn test_iter() {
        let mut headers = HeaderMap::new();
        headers.insert("A", "1");
        headers.insert("B", "2");

        let pairs: Vec<_> = headers.iter().collect();
        assert_eq!(pairs.len(), 2);
    }

    #[test]
    fn test_common_accessors() {
        let mut headers = HeaderMap::new();
        headers.insert("Content-Type", "application/json");
        headers.insert("Content-Length", "100");
        headers.insert("Connection", "keep-alive");
        headers.insert("Transfer-Encoding", "chunked");

        assert_eq!(
            headers.content_type(),
            Some(&"application/json".to_string())
        );
        assert_eq!(headers.content_length(), Some(100));
        assert!(headers.is_keep_alive());
        assert!(headers.is_chunked());
    }

    #[test]
    fn test_from_hash_map() {
        let mut map = HashMap::new();
        map.insert("Content-Type".to_string(), "application/json".to_string());
        map.insert("Accept".to_string(), "text/html".to_string());

        let headers = HeaderMap::from_hash_map(map);
        assert_eq!(headers.len(), 2);
        assert!(headers.contains("Content-Type"));
    }

    #[test]
    fn test_to_hash_map() {
        let mut headers = HeaderMap::new();
        headers.insert("Content-Type", "application/json");

        let map = headers.to_hash_map();
        assert_eq!(
            map.get("Content-Type"),
            Some(&"application/json".to_string())
        );
    }

    #[test]
    fn test_from_iterator() {
        let headers: HeaderMap = [
            ("Content-Type", "application/json"),
            ("Accept", "text/html"),
        ]
        .into_iter()
        .collect();

        assert_eq!(headers.len(), 2);
    }

    #[test]
    fn test_indexing() {
        let mut headers = HeaderMap::new();
        headers.insert("Content-Type", "application/json");

        assert_eq!(&headers["Content-Type"], "application/json");
    }

    #[test]
    fn test_contains_key() {
        let mut headers = HeaderMap::new();
        headers.insert("Content-Type", "application/json");

        // Case-insensitive, HashMap-compatible alias for `contains`.
        assert!(headers.contains_key("Content-Type"));
        assert!(headers.contains_key("content-type"));
        assert!(!headers.contains_key("Accept"));
    }

    #[test]
    fn test_keys() {
        let mut headers = HeaderMap::new();
        headers.insert("Content-Type", "application/json");
        headers.insert("Accept", "text/html");

        let keys: Vec<_> = headers.keys().cloned().collect();
        assert_eq!(keys.len(), 2);
        assert!(keys.contains(&"Content-Type".to_string()));
        assert!(keys.contains(&"Accept".to_string()));
    }

    #[test]
    fn test_values() {
        let mut headers = HeaderMap::new();
        headers.insert("Content-Type", "application/json");
        headers.insert("Accept", "text/html");

        let values: Vec<_> = headers.values().cloned().collect();
        assert_eq!(values.len(), 2);
        assert!(values.contains(&"application/json".to_string()));
        assert!(values.contains(&"text/html".to_string()));
    }

    #[test]
    fn test_is_empty() {
        let mut headers = HeaderMap::new();
        assert!(headers.is_empty());
        headers.insert("Content-Type", "application/json");
        assert!(!headers.is_empty());
    }

    #[test]
    fn test_default() {
        let headers = HeaderMap::default();
        assert!(headers.is_empty());
        assert!(headers.is_inline());
    }

    #[test]
    fn test_extend_trait() {
        let mut headers = HeaderMap::new();
        headers.insert("Existing", "1");

        // Exercise the `Extend<(String, String)>` trait impl explicitly.
        let extra: Vec<(String, String)> = vec![
            ("Content-Type".to_string(), "application/json".to_string()),
            ("Accept".to_string(), "text/html".to_string()),
        ];
        Extend::extend(&mut headers, extra);

        assert_eq!(headers.len(), 3);
        assert_eq!(
            headers.get("Content-Type"),
            Some(&"application/json".to_string())
        );
    }

    #[test]
    fn test_into_iterator_owned() {
        let mut headers = HeaderMap::new();
        headers.insert("A", "1");
        headers.insert("B", "2");

        let collected: Vec<(String, String)> = headers.into_iter().collect();
        assert_eq!(collected.len(), 2);
    }

    #[test]
    fn test_into_iterator_ref() {
        let mut headers = HeaderMap::new();
        headers.insert("A", "1");

        let collected: Vec<(&String, &String)> = (&headers).into_iter().collect();
        assert_eq!(collected.len(), 1);
    }

    #[test]
    fn test_hashmap_roundtrip() {
        let mut map = HashMap::new();
        map.insert("Content-Type".to_string(), "application/json".to_string());

        // HashMap -> HeaderMap -> HashMap round-trips via `From`.
        let headers: HeaderMap = map.clone().into();
        assert!(headers.contains_key("content-type")); // case-insensitive
        let back: HashMap<String, String> = headers.into();
        assert_eq!(back.get("Content-Type"), map.get("Content-Type"));
    }
}