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
use http::{HeaderMap, Method, StatusCode, Uri, Version};
pub(crate) use self::h1::{dispatch, Conn, ServerTransaction};
use self::body_length::DecodedLength;
pub(crate) mod h1;
pub(crate) mod h2;
#[derive(Clone, Debug, Default, PartialEq)]
pub struct MessageHead<S> {
pub version: Version,
pub subject: S,
pub headers: HeaderMap,
}
pub type RequestHead = MessageHead<RequestLine>;
#[derive(Debug, Default, PartialEq)]
pub struct RequestLine(pub Method, pub Uri);
pub type ResponseHead = MessageHead<StatusCode>;
#[derive(Debug)]
pub enum BodyLength {
Known(u64),
Unknown,
}
pub(crate) enum Dispatched {
Shutdown,
Upgrade(::upgrade::Pending),
}
mod body_length {
use std::fmt;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct DecodedLength(u64);
const MAX_LEN: u64 = ::std::u64::MAX - 2;
impl DecodedLength {
pub(crate) const CLOSE_DELIMITED: DecodedLength = DecodedLength(::std::u64::MAX);
pub(crate) const CHUNKED: DecodedLength = DecodedLength(::std::u64::MAX - 1);
pub(crate) const ZERO: DecodedLength = DecodedLength(0);
#[cfg(test)]
pub(crate) fn new(len: u64) -> Self {
debug_assert!(len <= MAX_LEN);
DecodedLength(len)
}
#[inline]
pub(crate) fn danger_len(self) -> u64 {
debug_assert!(self.0 < Self::CHUNKED.0);
self.0
}
pub(crate) fn into_opt(self) -> Option<u64> {
match self {
DecodedLength::CHUNKED |
DecodedLength::CLOSE_DELIMITED => None,
DecodedLength(known) => Some(known)
}
}
pub(crate) fn checked_new(len: u64) -> Result<Self, ::error::Parse> {
if len <= MAX_LEN {
Ok(DecodedLength(len))
} else {
warn!("content-length bigger than maximum: {} > {}", len, MAX_LEN);
Err(::error::Parse::TooLarge)
}
}
}
impl fmt::Display for DecodedLength {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
DecodedLength::CLOSE_DELIMITED => f.write_str("close-delimited"),
DecodedLength::CHUNKED => f.write_str("chunked encoding"),
DecodedLength::ZERO => f.write_str("empty"),
DecodedLength(n) => write!(f, "content-length ({} bytes)", n),
}
}
}
}