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
//! Content-Length typed header.
//!
//! See [`ContentLength`] docs.

use std::{convert::Infallible, str};

use actix_web::{
    error::ParseError,
    http::header::{
        from_one_raw_str, Header, HeaderName, HeaderValue, TryIntoHeaderValue, CONTENT_LENGTH,
    },
    HttpMessage,
};

/// `Content-Length` header, defined in [RFC 9110 §8.6].
///
/// The "Content-Length" header field indicates the associated representation's data length as a
/// decimal non-negative integer number of octets.
///
/// # ABNF
/// ```plain
/// Content-Length = 1*DIGIT
/// ```
///
/// [RFC 9110 §8.6]: https://www.rfc-editor.org/rfc/rfc9110#name-content-length
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct ContentLength(usize);

impl ContentLength {
    /// Returns Content-Length value.
    pub fn into_inner(&self) -> usize {
        self.0
    }
}

impl str::FromStr for ContentLength {
    type Err = <usize as str::FromStr>::Err;

    #[inline]
    fn from_str(val: &str) -> Result<Self, Self::Err> {
        let val = val.trim();

        // decoder prevents this case
        debug_assert!(!val.starts_with('+'));

        val.parse().map(Self)
    }
}

impl TryIntoHeaderValue for ContentLength {
    type Error = Infallible;

    fn try_into_value(self) -> Result<HeaderValue, Self::Error> {
        Ok(HeaderValue::from(self.0))
    }
}

impl Header for ContentLength {
    fn name() -> HeaderName {
        CONTENT_LENGTH
    }

    fn parse<M: HttpMessage>(msg: &M) -> Result<Self, ParseError> {
        let val = from_one_raw_str(msg.headers().get(Self::name()))?;

        // decoder prevents multiple CL headers
        debug_assert_eq!(msg.headers().get_all(Self::name()).count(), 1);

        Ok(val)
    }
}

impl From<ContentLength> for usize {
    fn from(ContentLength(len): ContentLength) -> Self {
        len
    }
}

impl From<usize> for ContentLength {
    fn from(len: usize) -> Self {
        ContentLength(len)
    }
}

impl PartialEq<usize> for ContentLength {
    fn eq(&self, other: &usize) -> bool {
        self.0 == *other
    }
}

impl PartialEq<ContentLength> for usize {
    fn eq(&self, other: &ContentLength) -> bool {
        *self == other.0
    }
}

impl PartialOrd<usize> for ContentLength {
    fn partial_cmp(&self, other: &usize) -> Option<std::cmp::Ordering> {
        self.0.partial_cmp(other)
    }
}

impl PartialOrd<ContentLength> for usize {
    fn partial_cmp(&self, other: &ContentLength) -> Option<std::cmp::Ordering> {
        self.partial_cmp(&other.0)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::header::{assert_parse_eq, assert_parse_fail};

    #[test]
    fn missing_header() {
        assert_parse_fail::<ContentLength, _, _>([""; 0]);
        assert_parse_fail::<ContentLength, _, _>([""]);
    }

    #[test]
    fn bad_header() {
        assert_parse_fail::<ContentLength, _, _>(["-123"]);
        assert_parse_fail::<ContentLength, _, _>(["123_456"]);
        assert_parse_fail::<ContentLength, _, _>(["123.456"]);

        // too large for u64 (2^64, 2^64 + 1)
        assert_parse_fail::<ContentLength, _, _>(["18446744073709551616"]);
        assert_parse_fail::<ContentLength, _, _>(["18446744073709551617"]);

        // hex notation
        assert_parse_fail::<ContentLength, _, _>(["0x123"]);

        // multi-value
        assert_parse_fail::<ContentLength, _, _>(["0, 123"]);
    }

    #[test]
    #[should_panic]
    fn bad_header_plus() {
        // prevented by decoder anyway
        assert_parse_fail::<ContentLength, _, _>(["+123"]);
    }

    #[test]
    #[should_panic]
    fn bad_multiple_value() {
        assert_parse_fail::<ContentLength, _, _>(["0", "123"]);
    }

    #[test]
    fn good_header() {
        assert_parse_eq::<ContentLength, _, _>(["0"], ContentLength(0));
        assert_parse_eq::<ContentLength, _, _>(["1"], ContentLength(1));
        assert_parse_eq::<ContentLength, _, _>(["123"], ContentLength(123));

        // value that looks like octal notation is not interpreted as such
        assert_parse_eq::<ContentLength, _, _>(["0123"], ContentLength(123));

        // whitespace variations
        assert_parse_eq::<ContentLength, _, _>([" 0"], ContentLength(0));
        assert_parse_eq::<ContentLength, _, _>(["0 "], ContentLength(0));
        assert_parse_eq::<ContentLength, _, _>([" 0 "], ContentLength(0));

        // large value (2^64 - 1)
        assert_parse_eq::<ContentLength, _, _>(
            ["18446744073709551615"],
            ContentLength(18_446_744_073_709_551_615),
        );
    }

    #[test]
    fn equality() {
        assert!(ContentLength(0) == ContentLength(0));
        assert!(ContentLength(0) == 0);
        assert!(0 != ContentLength(123));
    }

    #[test]
    fn ordering() {
        assert!(ContentLength(0) < ContentLength(123));
        assert!(ContentLength(0) < 123);
        assert!(0 < ContentLength(123));
    }
}