armature-core 0.8.2

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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
//! ETag and conditional request handling.
//!
//! This module provides support for HTTP conditional requests, enabling
//! efficient caching and optimistic concurrency control.
//!
//! # Supported Headers
//!
//! - `ETag` - Entity tag for resource versioning
//! - `If-None-Match` - Conditional GET (return 304 if ETag matches)
//! - `If-Match` - Conditional PUT/DELETE (fail if ETag doesn't match)
//! - `If-Modified-Since` - Conditional GET based on modification time
//! - `If-Unmodified-Since` - Conditional PUT/DELETE based on modification time
//!
//! # Examples
//!
//! ## Conditional GET with ETag
//!
//! ```
//! use armature_core::conditional::{ETag, ConditionalRequest};
//! use armature_core::HttpRequest;
//!
//! fn handle_get(request: &HttpRequest) -> Result<(), ()> {
//!     let etag = ETag::strong("abc123");
//!
//!     // Check if client has current version
//!     if request.if_none_match_matches(&etag) {
//!         // Return 304 Not Modified
//!         return Ok(());
//!     }
//!
//!     // Return full response with ETag
//!     Ok(())
//! }
//! ```
//!
//! ## Optimistic Concurrency with If-Match
//!
//! ```
//! use armature_core::conditional::{ETag, ConditionalRequest};
//! use armature_core::HttpRequest;
//!
//! fn handle_update(request: &HttpRequest, current_etag: &ETag) -> Result<(), ()> {
//!     // Verify client has current version before updating
//!     if !request.if_match_matches(current_etag) {
//!         // Return 412 Precondition Failed
//!         return Err(());
//!     }
//!
//!     // Proceed with update
//!     Ok(())
//! }
//! ```

use crate::{Error, HttpRequest, HttpResponse};
use bytes::Bytes;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::time::SystemTime;

// ============================================================================
// ETag
// ============================================================================

/// Represents an HTTP ETag (Entity Tag).
///
/// ETags come in two varieties:
/// - **Strong ETags**: Byte-for-byte identical (`"abc123"`)
/// - **Weak ETags**: Semantically equivalent (`W/"abc123"`)
///
/// # Examples
///
/// ```
/// use armature_core::conditional::ETag;
///
/// // Create a strong ETag
/// let strong = ETag::strong("abc123");
/// assert_eq!(strong.to_header_value(), "\"abc123\"");
///
/// // Create a weak ETag
/// let weak = ETag::weak("abc123");
/// assert_eq!(weak.to_header_value(), "W/\"abc123\"");
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ETag {
    /// The tag value (without quotes)
    pub value: String,
    /// Whether this is a weak ETag
    pub weak: bool,
}

impl ETag {
    /// Create a strong ETag.
    ///
    /// Strong ETags indicate byte-for-byte identity. Use for content
    /// that must be exactly identical.
    pub fn strong(value: impl Into<String>) -> Self {
        Self {
            value: value.into(),
            weak: false,
        }
    }

    /// Create a weak ETag.
    ///
    /// Weak ETags indicate semantic equivalence. Use when minor
    /// variations (like whitespace) are acceptable.
    pub fn weak(value: impl Into<String>) -> Self {
        Self {
            value: value.into(),
            weak: true,
        }
    }

    /// Parse an ETag from a header value.
    ///
    /// # Examples
    ///
    /// ```
    /// use armature_core::conditional::ETag;
    ///
    /// let strong = ETag::parse("\"abc123\"").unwrap();
    /// assert!(!strong.weak);
    /// assert_eq!(strong.value, "abc123");
    ///
    /// let weak = ETag::parse("W/\"abc123\"").unwrap();
    /// assert!(weak.weak);
    /// assert_eq!(weak.value, "abc123");
    /// ```
    pub fn parse(s: &str) -> Option<Self> {
        let s = s.trim();

        let (weak, value_part) = if s.starts_with("W/") || s.starts_with("w/") {
            (true, &s[2..])
        } else {
            (false, s)
        };

        // Extract value from quotes
        let value = value_part.strip_prefix('"')?.strip_suffix('"')?.to_string();

        Some(Self { value, weak })
    }

    /// Generate an ETag from bytes using a hash.
    ///
    /// # Examples
    ///
    /// ```
    /// use armature_core::conditional::ETag;
    ///
    /// let data = b"Hello, World!";
    /// let etag = ETag::from_bytes(data);
    /// assert!(!etag.weak);
    /// ```
    pub fn from_bytes(data: &[u8]) -> Self {
        use std::collections::hash_map::DefaultHasher;

        let mut hasher = DefaultHasher::new();
        data.hash(&mut hasher);
        let hash = hasher.finish();

        Self::strong(format!("{:x}", hash))
    }

    /// Generate a weak ETag from bytes.
    pub fn weak_from_bytes(data: &[u8]) -> Self {
        let mut etag = Self::from_bytes(data);
        etag.weak = true;
        etag
    }

    /// Generate an ETag from a string using a hash.
    #[allow(clippy::should_implement_trait)]
    pub fn from_str(s: &str) -> Self {
        Self::from_bytes(s.as_bytes())
    }

    /// Generate an ETag from file metadata.
    ///
    /// Creates an ETag based on file size and modification time.
    pub fn from_file_metadata(size: u64, modified: SystemTime) -> Self {
        let modified_unix = modified
            .duration_since(SystemTime::UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);

        Self::strong(format!("{:x}-{:x}", size, modified_unix))
    }

    /// Generate an ETag from a version number or revision.
    pub fn from_version(version: u64) -> Self {
        Self::strong(format!("v{}", version))
    }

    /// Get the header value representation.
    pub fn to_header_value(&self) -> String {
        if self.weak {
            format!("W/\"{}\"", self.value)
        } else {
            format!("\"{}\"", self.value)
        }
    }

    /// Check if this ETag matches another using strong comparison.
    ///
    /// Strong comparison: Both ETags must be strong and have identical values.
    pub fn strong_match(&self, other: &ETag) -> bool {
        !self.weak && !other.weak && self.value == other.value
    }

    /// Check if this ETag matches another using weak comparison.
    ///
    /// Weak comparison: Values must match (weak flag is ignored).
    pub fn weak_match(&self, other: &ETag) -> bool {
        self.value == other.value
    }
}

impl fmt::Display for ETag {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.to_header_value())
    }
}

// ============================================================================
// ETag List (for If-None-Match, If-Match)
// ============================================================================

/// Represents a list of ETags from If-None-Match or If-Match headers.
#[derive(Debug, Clone, Default)]
pub struct ETagList {
    /// The list of ETags
    pub etags: Vec<ETag>,
    /// Whether the header contains a wildcard "*"
    pub any: bool,
}

impl ETagList {
    /// Create an empty ETag list.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create an ETag list that matches any ETag.
    pub fn any() -> Self {
        Self {
            etags: Vec::new(),
            any: true,
        }
    }

    /// Parse an ETag list from a header value.
    ///
    /// # Examples
    ///
    /// ```
    /// use armature_core::conditional::ETagList;
    ///
    /// let list = ETagList::parse("\"abc\", \"def\", W/\"ghi\"");
    /// assert_eq!(list.etags.len(), 3);
    ///
    /// let any = ETagList::parse("*");
    /// assert!(any.any);
    /// ```
    pub fn parse(header: &str) -> Self {
        let header = header.trim();

        // Check for wildcard
        if header == "*" {
            return Self::any();
        }

        let etags: Vec<ETag> = header
            .split(',')
            .filter_map(|s| ETag::parse(s.trim()))
            .collect();

        Self { etags, any: false }
    }

    /// Check if any ETag in the list matches (weak comparison).
    pub fn contains_weak(&self, etag: &ETag) -> bool {
        if self.any {
            return true;
        }
        self.etags.iter().any(|e| e.weak_match(etag))
    }

    /// Check if any ETag in the list matches (strong comparison).
    pub fn contains_strong(&self, etag: &ETag) -> bool {
        if self.any {
            // RFC 7232 §3.1: "*" matches whenever the resource has any
            // current representation, regardless of ETag weakness.
            return true;
        }
        self.etags.iter().any(|e| e.strong_match(etag))
    }

    /// Check if the list is empty (no ETags and not a wildcard).
    pub fn is_empty(&self) -> bool {
        !self.any && self.etags.is_empty()
    }
}

// ============================================================================
// Conditional Request Headers
// ============================================================================

/// Parsed conditional request headers.
#[derive(Debug, Clone, Default)]
pub struct ConditionalHeaders {
    /// If-None-Match header (for conditional GET)
    pub if_none_match: Option<ETagList>,
    /// If-Match header (for conditional PUT/DELETE)
    pub if_match: Option<ETagList>,
    /// If-Modified-Since header
    pub if_modified_since: Option<SystemTime>,
    /// If-Unmodified-Since header
    pub if_unmodified_since: Option<SystemTime>,
}

impl ConditionalHeaders {
    /// Parse conditional headers from an HTTP request.
    ///
    /// Lookups use the canonical casing only: `HeaderMap::get` compares names
    /// with `eq_ignore_ascii_case`, so a client sending `if-none-match` is
    /// matched by the same call that matches `If-None-Match`.
    pub fn from_request(request: &HttpRequest) -> Self {
        // One lookup each: header names intern case-insensitively, so the
        // lowercased retry was always redundant.
        let if_none_match = request.headers.get("If-None-Match").map(ETagList::parse);

        let if_match = request.headers.get("If-Match").map(ETagList::parse);

        let if_modified_since = request
            .headers
            .get("If-Modified-Since")
            .and_then(|h| httpdate::parse_http_date(h).ok());

        let if_unmodified_since = request
            .headers
            .get("If-Unmodified-Since")
            .and_then(|h| httpdate::parse_http_date(h).ok());

        Self {
            if_none_match,
            if_match,
            if_modified_since,
            if_unmodified_since,
        }
    }

    /// Check if the resource should return 304 Not Modified.
    ///
    /// Returns true if:
    /// - If-None-Match contains a matching ETag (weak comparison), or
    /// - If-Modified-Since is after the resource's last modification
    pub fn is_not_modified(&self, etag: Option<&ETag>, last_modified: Option<SystemTime>) -> bool {
        // Check If-None-Match first (takes precedence)
        if let Some(ref if_none_match) = self.if_none_match
            && let Some(etag) = etag
        {
            return if_none_match.contains_weak(etag);
        }

        // Check If-Modified-Since
        if let (Some(if_modified_since), Some(last_modified)) =
            (self.if_modified_since, last_modified)
        {
            return last_modified <= if_modified_since;
        }

        false
    }

    /// Check if the precondition fails (should return 412).
    ///
    /// Returns true if:
    /// - If-Match is present and no ETag matches (strong comparison), or
    /// - If-Unmodified-Since is before the resource's last modification
    pub fn precondition_failed(
        &self,
        etag: Option<&ETag>,
        last_modified: Option<SystemTime>,
    ) -> bool {
        // Check If-Match first (takes precedence)
        if let Some(ref if_match) = self.if_match {
            if let Some(etag) = etag {
                return !if_match.contains_strong(etag);
            } else {
                // If-Match present but no ETag to compare - fail
                return !if_match.any;
            }
        }

        // Check If-Unmodified-Since
        if let (Some(if_unmodified_since), Some(last_modified)) =
            (self.if_unmodified_since, last_modified)
        {
            return last_modified > if_unmodified_since;
        }

        false
    }
}

// ============================================================================
// Request Extensions
// ============================================================================

/// Extension trait for conditional request handling on HttpRequest.
pub trait ConditionalRequest {
    /// Get parsed conditional headers from the request.
    fn conditional_headers(&self) -> ConditionalHeaders;

    /// Get the If-None-Match header as an ETag list.
    fn if_none_match(&self) -> Option<ETagList>;

    /// Get the If-Match header as an ETag list.
    fn if_match(&self) -> Option<ETagList>;

    /// Get the If-Modified-Since header as a SystemTime.
    fn if_modified_since(&self) -> Option<SystemTime>;

    /// Get the If-Unmodified-Since header as a SystemTime.
    fn if_unmodified_since(&self) -> Option<SystemTime>;

    /// Check if If-None-Match contains a matching ETag (weak comparison).
    ///
    /// Returns true if the request should get a 304 Not Modified response.
    fn if_none_match_matches(&self, etag: &ETag) -> bool;

    /// Check if If-Match contains a matching ETag (strong comparison).
    ///
    /// Returns false if the precondition fails (should return 412).
    fn if_match_matches(&self, etag: &ETag) -> bool;

    /// Check if If-Modified-Since indicates the resource hasn't changed.
    fn not_modified_since(&self, last_modified: SystemTime) -> bool;

    /// Check if If-Unmodified-Since precondition fails.
    fn modified_since_precondition(&self, last_modified: SystemTime) -> bool;

    /// Evaluate all conditional headers and return the appropriate response.
    ///
    /// Returns:
    /// - `Some(304)` if resource is not modified
    /// - `Some(412)` if precondition failed
    /// - `None` if request should proceed normally
    fn evaluate_conditionals(
        &self,
        etag: Option<&ETag>,
        last_modified: Option<SystemTime>,
    ) -> Option<u16>;
}

impl ConditionalRequest for HttpRequest {
    fn conditional_headers(&self) -> ConditionalHeaders {
        ConditionalHeaders::from_request(self)
    }

    // One lookup per accessor: header names intern case-insensitively, so the
    // lowercased retry was always redundant.
    fn if_none_match(&self) -> Option<ETagList> {
        self.headers.get("If-None-Match").map(ETagList::parse)
    }

    fn if_match(&self) -> Option<ETagList> {
        self.headers.get("If-Match").map(ETagList::parse)
    }

    fn if_modified_since(&self) -> Option<SystemTime> {
        self.headers
            .get("If-Modified-Since")
            .and_then(|h| httpdate::parse_http_date(h).ok())
    }

    fn if_unmodified_since(&self) -> Option<SystemTime> {
        self.headers
            .get("If-Unmodified-Since")
            .and_then(|h| httpdate::parse_http_date(h).ok())
    }

    fn if_none_match_matches(&self, etag: &ETag) -> bool {
        self.if_none_match()
            .map(|list| list.contains_weak(etag))
            .unwrap_or(false)
    }

    fn if_match_matches(&self, etag: &ETag) -> bool {
        match self.if_match() {
            Some(list) => list.contains_strong(etag),
            None => true, // No If-Match header means proceed
        }
    }

    fn not_modified_since(&self, last_modified: SystemTime) -> bool {
        self.if_modified_since()
            .map(|since| last_modified <= since)
            .unwrap_or(false)
    }

    fn modified_since_precondition(&self, last_modified: SystemTime) -> bool {
        self.if_unmodified_since()
            .map(|since| last_modified > since)
            .unwrap_or(false)
    }

    fn evaluate_conditionals(
        &self,
        etag: Option<&ETag>,
        last_modified: Option<SystemTime>,
    ) -> Option<u16> {
        let headers = self.conditional_headers();

        // Check preconditions first (412)
        if headers.precondition_failed(etag, last_modified) {
            return Some(412);
        }

        // Only GET and HEAD get conditional-GET treatment; the other safe
        // methods (OPTIONS/TRACE/QUERY) have no 304 semantics.
        let is_safe = matches!(self.method, crate::Method::Get | crate::Method::Head);

        if is_safe {
            // Check not modified (304) - only for safe methods
            if headers.is_not_modified(etag, last_modified) {
                return Some(304);
            }
        } else if let (Some(if_none_match), Some(etag)) = (&headers.if_none_match, etag) {
            // RFC 7232 §3.2: a matching If-None-Match on an unsafe method
            // (PUT/POST/DELETE/...) must fail with 412 Precondition Failed.
            if if_none_match.contains_weak(etag) {
                return Some(412);
            }
        }

        None
    }
}

// ============================================================================
// Response Extensions
// ============================================================================

/// Extension trait for conditional response handling on HttpResponse.
pub trait ConditionalResponse {
    /// Set the ETag header on the response.
    fn with_etag(self, etag: &ETag) -> Self;

    /// Set the Last-Modified header on the response.
    fn with_last_modified(self, time: SystemTime) -> Self;

    /// Create a 304 Not Modified response.
    fn not_modified() -> Self;

    /// Create a 304 Not Modified response with an ETag.
    fn not_modified_with_etag(etag: &ETag) -> Self;

    /// Create a 412 Precondition Failed response.
    fn precondition_failed() -> Self;

    /// Create a 412 Precondition Failed response with a message.
    fn precondition_failed_with_message(message: &str) -> Self;
}

impl ConditionalResponse for HttpResponse {
    fn with_etag(mut self, etag: &ETag) -> Self {
        self.headers
            .insert("ETag".to_string(), etag.to_header_value());
        self
    }

    fn with_last_modified(mut self, time: SystemTime) -> Self {
        let formatted = httpdate::fmt_http_date(time);
        self.headers.insert("Last-Modified".to_string(), formatted);
        self
    }

    fn not_modified() -> Self {
        Self::new(304)
    }

    fn not_modified_with_etag(etag: &ETag) -> Self {
        let mut response = Self::new(304);
        response
            .headers
            .insert("ETag".to_string(), etag.to_header_value());
        response
    }

    fn precondition_failed() -> Self {
        Self::new(412)
    }

    fn precondition_failed_with_message(message: &str) -> Self {
        let body = serde_json::json!({
            "error": "Precondition Failed",
            "message": message,
            "status": 412
        });

        let mut response = Self::new(412);
        if let Ok(body_bytes) = serde_json::to_vec(&body) {
            response.body = Bytes::from(body_bytes);
            response
                .headers
                .insert("Content-Type".to_string(), "application/json".to_string());
        }
        response
    }
}

// ============================================================================
// Helper Functions
// ============================================================================

/// Check conditional headers and return appropriate response or proceed.
///
/// This is a convenience function that handles the common pattern of:
/// 1. Check preconditions (return 412 if failed)
/// 2. Check not-modified (return 304 if not modified)
/// 3. Continue with normal processing
///
/// # Example
///
/// ```ignore
/// use armature_core::conditional::{check_conditionals, ETag};
///
/// #[get("/resource/:id")]
/// async fn get_resource(request: HttpRequest) -> Result<HttpResponse, Error> {
///     let resource = load_resource();
///     let etag = ETag::from_version(resource.version);
///     let last_modified = resource.updated_at;
///
///     // Check conditionals - returns early if 304 or 412
///     if let Some(response) = check_conditionals(&request, Some(&etag), Some(last_modified)) {
///         return Ok(response);
///     }
///
///     // Normal response
///     HttpResponse::ok()
///         .with_etag(&etag)
///         .with_last_modified(last_modified)
///         .with_json(&resource)
/// }
/// ```
pub fn check_conditionals(
    request: &HttpRequest,
    etag: Option<&ETag>,
    last_modified: Option<SystemTime>,
) -> Option<HttpResponse> {
    match request.evaluate_conditionals(etag, last_modified) {
        Some(304) => {
            let mut response = HttpResponse::not_modified();
            if let Some(etag) = etag {
                response = response.with_etag(etag);
            }
            if let Some(lm) = last_modified {
                response = response.with_last_modified(lm);
            }
            Some(response)
        }
        Some(412) => Some(HttpResponse::precondition_failed_with_message(
            "Resource has been modified",
        )),
        _ => None,
    }
}

/// Generate a cache-friendly response with ETag and Last-Modified headers.
///
/// # Example
///
/// ```ignore
/// use armature_core::conditional::{cacheable_response, ETag};
///
/// let data = get_data();
/// let etag = ETag::from_bytes(&serde_json::to_vec(&data)?);
/// let response = cacheable_response(data, &etag, Some(last_modified))?;
/// ```
pub fn cacheable_response<T: serde::Serialize>(
    data: &T,
    etag: &ETag,
    last_modified: Option<SystemTime>,
) -> Result<HttpResponse, Error> {
    let mut response = HttpResponse::ok().with_json(data)?.with_etag(etag);

    if let Some(lm) = last_modified {
        response = response.with_last_modified(lm);
    }

    // Add cache headers
    response.headers.insert(
        "Cache-Control".to_string(),
        "private, must-revalidate".to_string(),
    );
    response
        .headers
        .insert("Vary".to_string(), "Accept, Accept-Encoding".to_string());

    Ok(response)
}

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

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

    #[test]
    fn test_etag_strong() {
        let etag = ETag::strong("abc123");
        assert!(!etag.weak);
        assert_eq!(etag.value, "abc123");
        assert_eq!(etag.to_header_value(), "\"abc123\"");
    }

    #[test]
    fn test_etag_weak() {
        let etag = ETag::weak("abc123");
        assert!(etag.weak);
        assert_eq!(etag.value, "abc123");
        assert_eq!(etag.to_header_value(), "W/\"abc123\"");
    }

    #[test]
    fn test_etag_parse_strong() {
        let etag = ETag::parse("\"abc123\"").unwrap();
        assert!(!etag.weak);
        assert_eq!(etag.value, "abc123");
    }

    #[test]
    fn test_etag_parse_weak() {
        let etag = ETag::parse("W/\"abc123\"").unwrap();
        assert!(etag.weak);
        assert_eq!(etag.value, "abc123");
    }

    #[test]
    fn test_etag_parse_weak_lowercase() {
        let etag = ETag::parse("w/\"abc123\"").unwrap();
        assert!(etag.weak);
        assert_eq!(etag.value, "abc123");
    }

    #[test]
    fn test_etag_from_bytes() {
        let data = b"Hello, World!";
        let etag1 = ETag::from_bytes(data);
        let etag2 = ETag::from_bytes(data);
        assert_eq!(etag1.value, etag2.value);
        assert!(!etag1.weak);
    }

    #[test]
    fn test_etag_from_version() {
        let etag = ETag::from_version(42);
        assert_eq!(etag.value, "v42");
        assert!(!etag.weak);
    }

    #[test]
    fn test_etag_strong_match() {
        let e1 = ETag::strong("abc");
        let e2 = ETag::strong("abc");
        let e3 = ETag::weak("abc");

        assert!(e1.strong_match(&e2));
        assert!(!e1.strong_match(&e3)); // Weak doesn't strong-match
    }

    #[test]
    fn test_etag_weak_match() {
        let e1 = ETag::strong("abc");
        let e2 = ETag::weak("abc");

        assert!(e1.weak_match(&e2)); // Values match
    }

    #[test]
    fn test_etag_list_parse() {
        let list = ETagList::parse("\"abc\", \"def\", W/\"ghi\"");
        assert_eq!(list.etags.len(), 3);
        assert!(!list.any);
    }

    #[test]
    fn test_etag_list_parse_wildcard() {
        let list = ETagList::parse("*");
        assert!(list.any);
        assert!(list.etags.is_empty());
    }

    #[test]
    fn test_etag_list_contains_weak() {
        let list = ETagList::parse("\"abc\", W/\"def\"");
        let strong_abc = ETag::strong("abc");
        let weak_abc = ETag::weak("abc");
        let strong_xyz = ETag::strong("xyz");

        assert!(list.contains_weak(&strong_abc));
        assert!(list.contains_weak(&weak_abc)); // Weak comparison
        assert!(!list.contains_weak(&strong_xyz));
    }

    #[test]
    fn test_etag_list_contains_strong() {
        let list = ETagList::parse("\"abc\", W/\"def\"");
        let strong_abc = ETag::strong("abc");
        let weak_abc = ETag::weak("abc");
        let strong_def = ETag::strong("def");

        assert!(list.contains_strong(&strong_abc));
        assert!(!list.contains_strong(&weak_abc)); // Weak ETag doesn't strong-match
        assert!(!list.contains_strong(&strong_def)); // W/"def" doesn't strong-match "def"
    }

    #[test]
    fn test_etag_list_wildcard_contains() {
        let list = ETagList::any();
        let etag = ETag::strong("anything");

        assert!(list.contains_weak(&etag));
        assert!(list.contains_strong(&etag));
    }

    #[test]
    fn test_etag_list_wildcard_matches_weak_etag() {
        // RFC 7232 §3.1: "*" succeeds whenever the resource exists,
        // even if its current ETag is weak.
        let list = ETagList::any();
        let weak = ETag::weak("abc123");

        assert!(list.contains_strong(&weak));
        assert!(list.contains_weak(&weak));
    }

    #[test]
    fn test_if_match_wildcard_with_weak_etag_succeeds() {
        let mut request = HttpRequest::new("PUT", "/resource".to_string());
        request.headers.insert("If-Match", "*".to_string());

        let weak = ETag::weak("abc123");
        assert_eq!(request.evaluate_conditionals(Some(&weak), None), None);
    }

    #[test]
    fn test_if_none_match_unsafe_method_412() {
        // RFC 7232 §3.2: matching If-None-Match on an unsafe method → 412
        for method in ["PUT", "POST", "DELETE", "PATCH"] {
            let mut request = HttpRequest::new(method.to_string(), "/resource".to_string());
            request
                .headers
                .insert("If-None-Match", "\"abc123\"".to_string());

            let etag = ETag::strong("abc123");
            assert_eq!(
                request.evaluate_conditionals(Some(&etag), None),
                Some(412),
                "expected 412 for {}",
                method
            );
        }
    }

    #[test]
    fn test_if_none_match_unsafe_method_no_match_proceeds() {
        let mut request = HttpRequest::new("PUT", "/resource".to_string());
        request
            .headers
            .insert("If-None-Match", "\"abc123\"".to_string());

        let etag = ETag::strong("different");
        assert_eq!(request.evaluate_conditionals(Some(&etag), None), None);
    }

    #[test]
    fn test_conditional_headers_if_none_match() {
        let mut request = HttpRequest::new("GET", "/resource".to_string());
        request
            .headers
            .insert("If-None-Match", "\"abc123\"".to_string());

        let headers = ConditionalHeaders::from_request(&request);
        assert!(headers.if_none_match.is_some());

        let etag = ETag::strong("abc123");
        assert!(headers.is_not_modified(Some(&etag), None));
    }

    /// Clients are free to send header names in any casing, and HTTP/2 and
    /// HTTP/3 send them lowercased on the wire, so parsing must not depend on
    /// the canonical spelling used in the lookups above.
    #[test]
    fn test_conditional_headers_lowercase_header_names() {
        let mut request = HttpRequest::new("GET".to_string(), "/resource".to_string());
        request
            .headers
            .insert("if-none-match", "\"abc123\"".to_string());
        request.headers.insert("if-match", "\"abc123\"".to_string());
        request.headers.insert(
            "if-modified-since",
            "Sun, 06 Nov 1994 08:49:37 GMT".to_string(),
        );
        request.headers.insert(
            "if-unmodified-since",
            "Sun, 06 Nov 1994 08:49:37 GMT".to_string(),
        );

        let headers = ConditionalHeaders::from_request(&request);
        assert!(headers.if_none_match.is_some());
        assert!(headers.if_match.is_some());
        assert!(headers.if_modified_since.is_some());
        assert!(headers.if_unmodified_since.is_some());

        let etag = ETag::strong("abc123");
        assert!(headers.is_not_modified(Some(&etag), None));

        // The ConditionalRequest accessors read the same headers directly.
        assert!(request.if_none_match().is_some());
        assert!(request.if_match().is_some());
        assert!(request.if_modified_since().is_some());
        assert!(request.if_unmodified_since().is_some());
    }

    #[test]
    fn test_conditional_headers_if_match() {
        let mut request = HttpRequest::new("PUT", "/resource".to_string());
        request.headers.insert("If-Match", "\"abc123\"".to_string());

        let headers = ConditionalHeaders::from_request(&request);
        assert!(headers.if_match.is_some());

        let matching = ETag::strong("abc123");
        let non_matching = ETag::strong("xyz789");

        assert!(!headers.precondition_failed(Some(&matching), None));
        assert!(headers.precondition_failed(Some(&non_matching), None));
    }

    #[test]
    fn test_request_if_none_match_matches() {
        let mut request = HttpRequest::new("GET", "/resource".to_string());
        request
            .headers
            .insert("If-None-Match", "\"abc123\"".to_string());

        let matching = ETag::strong("abc123");
        let non_matching = ETag::strong("xyz789");

        assert!(request.if_none_match_matches(&matching));
        assert!(!request.if_none_match_matches(&non_matching));
    }

    #[test]
    fn test_request_if_match_matches() {
        let mut request = HttpRequest::new("PUT", "/resource".to_string());
        request.headers.insert("If-Match", "\"abc123\"".to_string());

        let matching = ETag::strong("abc123");
        let non_matching = ETag::strong("xyz789");

        assert!(request.if_match_matches(&matching));
        assert!(!request.if_match_matches(&non_matching));
    }

    #[test]
    fn test_request_evaluate_conditionals_304() {
        let mut request = HttpRequest::new("GET", "/resource".to_string());
        request
            .headers
            .insert("If-None-Match", "\"abc123\"".to_string());

        let etag = ETag::strong("abc123");
        assert_eq!(request.evaluate_conditionals(Some(&etag), None), Some(304));
    }

    #[test]
    fn test_request_evaluate_conditionals_412() {
        let mut request = HttpRequest::new("PUT", "/resource".to_string());
        request.headers.insert("If-Match", "\"abc123\"".to_string());

        let etag = ETag::strong("xyz789");
        assert_eq!(request.evaluate_conditionals(Some(&etag), None), Some(412));
    }

    #[test]
    fn test_request_evaluate_conditionals_proceed() {
        let request = HttpRequest::new("GET", "/resource".to_string());
        let etag = ETag::strong("abc123");

        // No conditional headers - should proceed
        assert_eq!(request.evaluate_conditionals(Some(&etag), None), None);
    }

    #[test]
    fn test_response_with_etag() {
        let etag = ETag::strong("abc123");
        let response = HttpResponse::ok().with_etag(&etag);

        assert_eq!(
            response.headers.get("ETag"),
            Some(&"\"abc123\"".to_string())
        );
    }

    #[test]
    fn test_response_not_modified() {
        let response = HttpResponse::not_modified();
        assert_eq!(response.status, 304);
    }

    #[test]
    fn test_response_precondition_failed() {
        let response = HttpResponse::precondition_failed();
        assert_eq!(response.status, 412);
    }

    #[test]
    fn test_check_conditionals_returns_304() {
        let mut request = HttpRequest::new("GET", "/resource".to_string());
        request
            .headers
            .insert("If-None-Match", "\"abc123\"".to_string());

        let etag = ETag::strong("abc123");
        let response = check_conditionals(&request, Some(&etag), None);

        assert!(response.is_some());
        assert_eq!(response.unwrap().status, 304);
    }

    #[test]
    fn test_check_conditionals_returns_412() {
        let mut request = HttpRequest::new("PUT", "/resource".to_string());
        request.headers.insert("If-Match", "\"abc123\"".to_string());

        let etag = ETag::strong("different");
        let response = check_conditionals(&request, Some(&etag), None);

        assert!(response.is_some());
        assert_eq!(response.unwrap().status, 412);
    }

    #[test]
    fn test_check_conditionals_returns_none() {
        let request = HttpRequest::new("GET", "/resource".to_string());
        let etag = ETag::strong("abc123");

        let response = check_conditionals(&request, Some(&etag), None);
        assert!(response.is_none());
    }
}