-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathdecoder.rs
346 lines (319 loc) · 11.8 KB
/
decoder.rs
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
use std::fmt;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use async_std::io::{self, Read};
use futures_core::ready;
use http_types::trailers::{Sender, Trailers};
/// Decodes a chunked body according to
/// https://tools.ietf.org/html/rfc7230#section-4.1
#[derive(Debug)]
pub struct ChunkedDecoder<R: Read> {
/// The underlying stream
inner: R,
/// Current state.
state: State,
/// Current chunk size (increased while parsing size, decreased while reading chunk)
chunk_size: u64,
/// Trailer channel sender.
trailer_sender: Option<Sender>,
}
impl<R: Read> ChunkedDecoder<R> {
pub(crate) fn new(inner: R, trailer_sender: Sender) -> Self {
ChunkedDecoder {
inner,
state: State::ChunkSize,
chunk_size: 0,
trailer_sender: Some(trailer_sender),
}
}
}
/// Decoder state.
enum State {
/// Parsing bytes from a chunk size
ChunkSize,
/// Expecting the \n at the end of a chunk size
ChunkSizeExpectLf,
/// Parsing the chunk body
ChunkBody,
/// Expecting the \r at the end of a chunk body
ChunkBodyExpectCr,
/// Expecting the \n at the end of a chunk body
ChunkBodyExpectLf,
/// Parsing trailers.
Trailers(usize, Box<[u8; 8192]>),
/// Sending trailers over the channel.
TrailerSending(Pin<Box<dyn Future<Output = ()> + 'static + Send + Sync>>),
/// All is said and done.
Done,
}
impl fmt::Debug for State {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
State::ChunkSize => write!(f, "State::ChunkSize"),
State::ChunkSizeExpectLf => write!(f, "State::ChunkSizeExpectLf"),
State::ChunkBody => write!(f, "State::ChunkBody"),
State::ChunkBodyExpectCr => write!(f, "State::ChunkBodyExpectCr"),
State::ChunkBodyExpectLf => write!(f, "State::ChunkBodyExpectLf"),
State::Trailers(len, _) => write!(f, "State::Trailers({}, _)", len),
State::TrailerSending(_) => write!(f, "State::TrailerSending"),
State::Done => write!(f, "State::Done"),
}
}
}
impl<R: Read + Unpin> ChunkedDecoder<R> {
fn poll_read_byte(&mut self, cx: &mut Context<'_>) -> Poll<io::Result<u8>> {
let mut byte = [0u8];
if ready!(Pin::new(&mut self.inner).poll_read(cx, &mut byte))? == 1 {
Poll::Ready(Ok(byte[0]))
} else {
eof()
}
}
fn expect_byte(
&mut self,
cx: &mut Context<'_>,
expected_byte: u8,
expected: &'static str,
) -> Poll<io::Result<()>> {
let byte = ready!(self.poll_read_byte(cx))?;
if byte == expected_byte {
Poll::Ready(Ok(()))
} else {
unexpected(byte, expected)
}
}
fn send_trailers(&mut self, trailers: Trailers) {
let sender = self
.trailer_sender
.take()
.expect("invalid chunked state, tried sending multiple trailers");
let fut = Box::pin(sender.send(trailers));
self.state = State::TrailerSending(fut);
}
}
fn eof<T>() -> Poll<io::Result<T>> {
Poll::Ready(Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"Unexpected EOF when decoding chunked data",
)))
}
fn unexpected<T>(byte: u8, expected: &'static str) -> Poll<io::Result<T>> {
Poll::Ready(Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("Unexpected byte {}; expected {}", byte, expected),
)))
}
fn overflow() -> io::Error {
io::Error::new(io::ErrorKind::InvalidData, "Chunk size overflowed 64 bits")
}
impl<R: Read + Unpin> Read for ChunkedDecoder<R> {
#[allow(missing_doc_code_examples)]
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
let this = &mut *self;
loop {
match this.state {
State::ChunkSize => {
let byte = ready!(this.poll_read_byte(cx))?;
let digit = match byte {
b'0'..=b'9' => byte - b'0',
b'a'..=b'f' => 10 + byte - b'a',
b'A'..=b'F' => 10 + byte - b'A',
b'\r' => {
this.state = State::ChunkSizeExpectLf;
continue;
}
_ => {
return unexpected(byte, "hex digit or CR");
}
};
this.chunk_size = this
.chunk_size
.checked_mul(16)
.ok_or_else(overflow)?
.checked_add(digit as u64)
.ok_or_else(overflow)?;
}
State::ChunkSizeExpectLf => {
ready!(this.expect_byte(cx, b'\n', "LF"))?;
if this.chunk_size == 0 {
this.state = State::Trailers(0, Box::new([0u8; 8192]));
} else {
this.state = State::ChunkBody;
}
}
State::ChunkBody => {
let max_bytes = std::cmp::min(
buf.len(),
std::cmp::min(this.chunk_size, usize::MAX as u64) as usize,
);
let bytes_read =
ready!(Pin::new(&mut this.inner).poll_read(cx, &mut buf[..max_bytes]))?;
this.chunk_size -= bytes_read as u64;
if bytes_read == 0 {
return eof();
} else if this.chunk_size == 0 {
this.state = State::ChunkBodyExpectCr;
}
return Poll::Ready(Ok(bytes_read));
}
State::ChunkBodyExpectCr => {
ready!(this.expect_byte(cx, b'\r', "CR"))?;
this.state = State::ChunkBodyExpectLf;
}
State::ChunkBodyExpectLf => {
ready!(this.expect_byte(cx, b'\n', "LF"))?;
this.state = State::ChunkSize;
}
State::Trailers(ref mut len, ref mut buf) => {
let bytes_read =
ready!(Pin::new(&mut this.inner).poll_read(cx, &mut buf[*len..]))?;
*len += bytes_read;
let len = *len;
if len == 0 {
this.send_trailers(Trailers::new());
continue;
}
if bytes_read == 0 {
return eof();
}
let mut headers = [httparse::EMPTY_HEADER; 16];
let parse_result = httparse::parse_headers(&buf[..len], &mut headers)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
use httparse::Status;
match parse_result {
Status::Partial => {
if len == buf.len() {
return eof();
} else {
return Poll::Pending;
}
}
Status::Complete((offset, headers)) => {
if offset != len {
return unexpected(buf[offset], "end of trailers");
}
let mut trailers = Trailers::new();
for header in headers {
trailers.insert(
header.name,
String::from_utf8_lossy(header.value).as_ref(),
);
}
this.send_trailers(trailers);
}
}
}
State::TrailerSending(ref mut fut) => {
let _ = ready!(Pin::new(fut).poll(cx));
this.state = State::Done;
}
State::Done => return Poll::Ready(Ok(0)),
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use async_std::prelude::*;
#[test]
fn test_chunked_wiki() {
async_std::task::block_on(async move {
let input = async_std::io::Cursor::new(
"4\r\n\
Wiki\r\n\
5\r\n\
pedia\r\n\
E\r\n in\r\n\
\r\n\
chunks.\r\n\
0\r\n\
\r\n"
.as_bytes(),
);
let (s, _r) = async_channel::bounded(1);
let sender = Sender::new(s);
let mut decoder = ChunkedDecoder::new(input, sender);
let mut output = String::new();
decoder.read_to_string(&mut output).await.unwrap();
assert_eq!(
output,
"Wikipedia in\r\n\
\r\n\
chunks."
);
});
}
#[test]
fn test_chunked_big() {
async_std::task::block_on(async move {
let mut input: Vec<u8> = b"800\r\n".to_vec();
input.extend(vec![b'X'; 2048]);
input.extend(b"\r\n1800\r\n");
input.extend(vec![b'Y'; 6144]);
input.extend(b"\r\n800\r\n");
input.extend(vec![b'Z'; 2048]);
input.extend(b"\r\n0\r\n\r\n");
let (s, _r) = async_channel::bounded(1);
let sender = Sender::new(s);
let mut decoder = ChunkedDecoder::new(async_std::io::Cursor::new(input), sender);
let mut output = String::new();
decoder.read_to_string(&mut output).await.unwrap();
let mut expected = vec![b'X'; 2048];
expected.extend(vec![b'Y'; 6144]);
expected.extend(vec![b'Z'; 2048]);
assert_eq!(output.len(), 10240);
assert_eq!(output.as_bytes(), expected.as_slice());
});
}
#[test]
fn test_chunked_mdn() {
async_std::task::block_on(async move {
let input = async_std::io::Cursor::new(
"7\r\n\
Mozilla\r\n\
9\r\n\
Developer\r\n\
7\r\n\
Network\r\n\
0\r\n\
Expires: Wed, 21 Oct 2015 07:28:00 GMT\r\n\
\r\n"
.as_bytes(),
);
let (s, r) = async_channel::bounded(1);
let sender = Sender::new(s);
let mut decoder = ChunkedDecoder::new(input, sender);
let mut output = String::new();
decoder.read_to_string(&mut output).await.unwrap();
assert_eq!(output, "MozillaDeveloperNetwork");
let trailers = r.recv().await.unwrap();
assert_eq!(trailers.iter().count(), 1);
assert_eq!(trailers["Expires"], "Wed, 21 Oct 2015 07:28:00 GMT");
});
}
#[test]
fn test_ff7() {
async_std::task::block_on(async move {
let mut input: Vec<u8> = b"FF7\r\n".to_vec();
input.extend(vec![b'X'; 0xFF7]);
input.extend(b"\r\n4\r\n");
input.extend(vec![b'Y'; 4]);
input.extend(b"\r\n0\r\n\r\n");
let (s, _r) = async_channel::bounded(1);
let sender = Sender::new(s);
let mut decoder = ChunkedDecoder::new(async_std::io::Cursor::new(input), sender);
let mut output = String::new();
decoder.read_to_string(&mut output).await.unwrap();
assert_eq!(
output,
"X".to_string().repeat(0xFF7) + &"Y".to_string().repeat(4)
);
});
}
}