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
use dope::manifold::connector;
use o3::buffer;
use sark_core::http::codec::chunked::{BodyDecoder, DecodeEvent};
use sark_core::http::codec::{BodyKind, DecodeMode, Parse};
const DEFAULT_MAX_RESPONSE_BODY: usize = 16 * 1024 * 1024;
pub struct Head {
pub full: buffer::Shared,
pub head_len: usize,
pub error: Option<&'static str>,
}
enum Framing {
Sized {
head_len: usize,
total: usize,
},
Chunked {
head_len: usize,
decoder: BodyDecoder,
scanned: usize,
},
}
#[derive(Default)]
pub struct ParseState {
framing: Option<Framing>,
pub error: bool,
}
pub struct Codec {
pub max_response_body: usize,
}
impl Default for Codec {
fn default() -> Self {
Self {
max_response_body: DEFAULT_MAX_RESPONSE_BODY,
}
}
}
impl connector::Codec for Codec {
type Head = Head;
type ParseState = ParseState;
fn parse(&self, state: &mut ParseState, buf: &buffer::Shared) -> Option<(Head, usize)> {
let bytes = buf.as_ref();
if state.framing.is_none() {
let head = match Parse::head(bytes, DecodeMode::Response) {
Ok(Some(h)) => h,
Ok(None) => return None,
Err(_) => return None,
};
state.framing = Some(match head.body_kind {
BodyKind::NoBody => Framing::Sized {
head_len: head.header_len,
total: head.header_len,
},
BodyKind::ContentLength(n) => Framing::Sized {
head_len: head.header_len,
total: head.header_len + n,
},
BodyKind::Chunked => Framing::Chunked {
head_len: head.header_len,
decoder: BodyDecoder::new(),
scanned: 0,
},
BodyKind::UntilEof => {
state.error = true;
return Some((
Head {
full: buf.slice(0..head.header_len),
head_len: head.header_len,
error: Some("EOF-delimited response body is not supported"),
},
bytes.len(),
));
}
});
}
match state.framing.as_mut()? {
Framing::Sized { head_len, total } => {
let head_len = *head_len;
let total = *total;
let body_len = total.saturating_sub(head_len);
if body_len > self.max_response_body {
state.framing = None;
state.error = true;
return Some((
Head {
full: buf.slice(0..head_len),
head_len,
error: Some("response body exceeds size limit"),
},
bytes.len(),
));
}
if bytes.len() < total {
return None;
}
state.framing = None;
Some((
Head {
full: buf.slice(0..total),
head_len,
error: None,
},
total,
))
}
Framing::Chunked {
head_len,
decoder,
scanned,
} => {
let head_len = *head_len;
loop {
if *scanned > self.max_response_body {
state.framing = None;
state.error = true;
return Some((
Head {
full: buf.slice(0..head_len),
head_len,
error: Some("response body exceeds size limit"),
},
bytes.len(),
));
}
let input = &bytes[head_len + *scanned..];
let (consumed, event) = match decoder.decode(input) {
Ok(pair) => pair,
Err(_) => return None,
};
*scanned += consumed;
match event {
DecodeEvent::NeedMore => return None,
DecodeEvent::Chunk(_) => continue,
DecodeEvent::Done(_) => {
let total = head_len + *scanned;
state.framing = None;
return Some((
Head {
full: buf.slice(0..total),
head_len,
error: None,
},
total,
));
}
}
}
}
}
}
}