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
use http::header::{self, HeaderName, HeaderValue, CONTENT_LENGTH};
use std::ops::{Deref, DerefMut};
use {util, Error, Header, ToValues};
#[derive(Clone, Debug, PartialEq)]
pub struct ContentLength(pub u64);
impl Deref for ContentLength {
type Target = u64;
#[inline]
fn deref(&self) -> &u64 {
&self.0
}
}
impl DerefMut for ContentLength {
#[inline]
fn deref_mut(&mut self) -> &mut u64 {
&mut self.0
}
}
impl Header for ContentLength {
#[inline]
fn name() -> &'static HeaderName {
&CONTENT_LENGTH
}
#[inline]
fn from_values(
values: &mut header::ValueIter<HeaderValue>,
) -> Result<Option<ContentLength>, Error> {
let mut length = None;
for value in values {
let value = value.to_str().map_err(|_| Error::invalid_value())?;
if value.trim().is_empty() {
return Err(Error::invalid_value());
}
for elem in value.split(',') {
let elem = elem.trim();
if elem.is_empty() {
continue;
}
let elem = elem.parse().map_err(|_| Error::invalid_value())?;
match length {
Some(length) if length != elem => return Err(Error::invalid_value()),
Some(_) => {}
None => length = Some(elem),
}
}
}
Ok(length.map(ContentLength))
}
#[inline]
fn to_values(&self, values: &mut ToValues) {
util::encode_single_value(&self.0, values);
}
}