Skip to content

Commit 09f9836

Browse files
more CR
1 parent 5d79158 commit 09f9836

File tree

1 file changed

+33
-34
lines changed

1 file changed

+33
-34
lines changed

src/chunked.rs

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::fmt;
22
use std::future::Future;
3+
use std::ops::Range;
34
use std::pin::Pin;
45
use std::str::FromStr;
56
use std::task::{Context, Poll};
@@ -24,8 +25,8 @@ pub(crate) struct ChunkedDecoder<R: Read> {
2425
inner: R,
2526
/// Buffer for the already read, but not yet parsed data.
2627
buffer: Block<'static>,
27-
/// Position of valid read data into buffer.
28-
current: Position,
28+
/// Range of valid read data into buffer.
29+
current: Range<usize>,
2930
/// Whether we should attempt to decode whatever is currently inside the buffer.
3031
/// False indicates that we know for certain that the buffer is incomplete.
3132
initial_decode: bool,
@@ -44,7 +45,7 @@ impl<R: Read> ChunkedDecoder<R> {
4445
ChunkedDecoder {
4546
inner,
4647
buffer: POOL.alloc(INITIAL_CAPACITY),
47-
current: Position::default(),
48+
current: Range { start: 0, end: 0 },
4849
initial_decode: false, // buffer is empty initially, nothing to decode}
4950
state: State::Init,
5051
trailer_sender: sender,
@@ -62,7 +63,7 @@ impl<R: Read + Unpin> ChunkedDecoder<R> {
6263
&mut self,
6364
cx: &mut Context<'_>,
6465
buffer: Block<'static>,
65-
pos: &Position,
66+
pos: &Range<usize>,
6667
buf: &mut [u8],
6768
current: u64,
6869
len: u64,
@@ -100,6 +101,7 @@ impl<R: Read + Unpin> ChunkedDecoder<R> {
100101
});
101102
}
102103

104+
// attempt to fill the buffer
103105
match Pin::new(&mut self.inner).poll_read(cx, &mut buf[read..read + to_read]) {
104106
Poll::Ready(val) => {
105107
let n = val?;
@@ -135,7 +137,7 @@ impl<R: Read + Unpin> ChunkedDecoder<R> {
135137
&mut self,
136138
cx: &mut Context<'_>,
137139
buffer: Block<'static>,
138-
pos: &Position,
140+
pos: &Range<usize>,
139141
buf: &mut [u8],
140142
) -> io::Result<DecodeResult> {
141143
match self.state {
@@ -196,7 +198,7 @@ impl<R: Read + Unpin> Read for ChunkedDecoder<R> {
196198
) -> Poll<io::Result<usize>> {
197199
let this = &mut *self;
198200

199-
let mut n = std::mem::replace(&mut this.current, Position::default());
201+
let mut n = std::mem::replace(&mut this.current, 0..0);
200202
let buffer = std::mem::replace(&mut this.buffer, POOL.alloc(INITIAL_CAPACITY));
201203
let mut needs_read = if let State::Chunk(_, _) = this.state {
202204
false // Do not attempt to fill the buffer when we are reading a chunk
@@ -205,6 +207,7 @@ impl<R: Read + Unpin> Read for ChunkedDecoder<R> {
205207
};
206208

207209
let mut buffer = if n.len() > 0 && this.initial_decode {
210+
// initial buffer filling, if needed
208211
match this.poll_read_inner(cx, buffer, &n, buf)? {
209212
DecodeResult::Some {
210213
read,
@@ -279,7 +282,7 @@ impl<R: Read + Unpin> Read for ChunkedDecoder<R> {
279282
// to decode it next time
280283
this.initial_decode = true;
281284
this.state = new_state;
282-
this.current = new_pos;
285+
this.current = new_pos.clone();
283286
n = new_pos;
284287

285288
if State::Done == this.state || read > 0 {
@@ -299,7 +302,7 @@ impl<R: Read + Unpin> Read for ChunkedDecoder<R> {
299302
DecodeResult::None(buf) => {
300303
buffer = buf;
301304

302-
if this.buffer.is_empty() || n.is_zero() {
305+
if this.buffer.is_empty() || n.start == 0 && n.end == 0 {
303306
// "logical buffer" is empty, there is nothing to decode on the next step
304307
this.initial_decode = false;
305308
this.buffer = buffer;
@@ -315,43 +318,39 @@ impl<R: Read + Unpin> Read for ChunkedDecoder<R> {
315318
}
316319
}
317320

318-
/// A semantically explicit slice of a buffer.
319-
#[derive(Eq, PartialEq, Debug, Copy, Clone, Default)]
320-
struct Position {
321-
start: usize,
322-
end: usize,
323-
}
324-
325-
impl Position {
326-
fn len(&self) -> usize {
327-
self.end - self.start
328-
}
329-
330-
fn is_zero(&self) -> bool {
331-
self.start == 0 && self.end == 0
332-
}
333-
}
334-
321+
/// Possible return values from calling `decode` methods.
335322
enum DecodeResult {
323+
/// Something was decoded successfully.
336324
Some {
337-
/// How much was read
325+
/// How much data was read.
338326
read: usize,
339-
/// Remaining data.
327+
/// The passed in block returned.
340328
buffer: Block<'static>,
341-
new_pos: Position,
329+
/// The new range of valid data in `buffer`.
330+
new_pos: Range<usize>,
331+
/// The new state.
342332
new_state: State,
333+
/// Should poll return `Pending`.
343334
pending: bool,
344335
},
336+
/// Nothing was decoded.
345337
None(Block<'static>),
346338
}
347339

340+
/// Decoder state.
348341
#[derive(Debug, PartialEq, Clone)]
349342
enum State {
343+
/// Initial state.
350344
Init,
345+
/// Decoding a chunk, first value is the current position, second value is the length of the chunk.
351346
Chunk(u64, u64),
347+
/// Decoding the end part of a chunk.
352348
ChunkEnd,
349+
/// Decoding trailers.
353350
Trailer,
351+
/// Trailers were decoded, are now set to the decoded trailers.
354352
TrailerDone(Vec<(HeaderName, HeaderValue)>),
353+
/// All is said and done.
355354
Done,
356355
}
357356

@@ -377,11 +376,11 @@ impl fmt::Debug for DecodeResult {
377376
}
378377
}
379378

380-
fn decode_init(buffer: Block<'static>, pos: &Position) -> io::Result<DecodeResult> {
379+
fn decode_init(buffer: Block<'static>, pos: &Range<usize>) -> io::Result<DecodeResult> {
381380
use httparse::Status;
382381
match httparse::parse_chunk_size(&buffer[pos.start..pos.end]) {
383382
Ok(Status::Complete((used, chunk_len))) => {
384-
let new_pos = Position {
383+
let new_pos = Range {
385384
start: pos.start + used,
386385
end: pos.end,
387386
};
@@ -405,7 +404,7 @@ fn decode_init(buffer: Block<'static>, pos: &Position) -> io::Result<DecodeResul
405404
}
406405
}
407406

408-
fn decode_chunk_end(buffer: Block<'static>, pos: &Position) -> io::Result<DecodeResult> {
407+
fn decode_chunk_end(buffer: Block<'static>, pos: &Range<usize>) -> io::Result<DecodeResult> {
409408
if pos.len() < 2 {
410409
return Ok(DecodeResult::None(buffer));
411410
}
@@ -415,7 +414,7 @@ fn decode_chunk_end(buffer: Block<'static>, pos: &Position) -> io::Result<Decode
415414
return Ok(DecodeResult::Some {
416415
read: 0,
417416
buffer,
418-
new_pos: Position {
417+
new_pos: Range {
419418
start: pos.start + 2,
420419
end: pos.end,
421420
},
@@ -427,7 +426,7 @@ fn decode_chunk_end(buffer: Block<'static>, pos: &Position) -> io::Result<Decode
427426
Err(io::Error::from(io::ErrorKind::InvalidData))
428427
}
429428

430-
fn decode_trailer(buffer: Block<'static>, pos: &Position) -> io::Result<DecodeResult> {
429+
fn decode_trailer(buffer: Block<'static>, pos: &Range<usize>) -> io::Result<DecodeResult> {
431430
use httparse::Status;
432431

433432
// read headers
@@ -450,7 +449,7 @@ fn decode_trailer(buffer: Block<'static>, pos: &Position) -> io::Result<DecodeRe
450449
read: 0,
451450
buffer,
452451
new_state: State::TrailerDone(headers),
453-
new_pos: Position {
452+
new_pos: Range {
454453
start: pos.start + used,
455454
end: pos.end,
456455
},

0 commit comments

Comments
 (0)