http/encoding/
chunked.rs

1use std::io::{Read, Result, Write};
2
3/// A reader for [HTTP Chunked transfer encoding]
4///
5/// [HTTP Chunked transfer encoding]: <https://en.wikipedia.org/wiki/Chunked_transfer_encoding>
6pub struct Chunked<R: Read, const CHUNK_SIZE: usize = 1024> {
7    reader: R,
8    chunk: Vec<u8>,
9    offset: usize,
10}
11
12impl<R: Read> Chunked<R> {
13    /// Creates a `Chunked` struct with the default size.
14    pub fn with_default_size(reader: R) -> Chunked<R> {
15        Self::new(reader)
16    }
17}
18
19impl<R: Read, const CHUNK_SIZE: usize> Chunked<R, CHUNK_SIZE> {
20    /// The size of the chunks
21    pub const CHUNK_SIZE: usize = CHUNK_SIZE;
22
23    pub fn new(reader: R) -> Self {
24        Chunked {
25            reader,
26            chunk: Vec::with_capacity(CHUNK_SIZE + 8),
27            offset: 0,
28        }
29    }
30    fn next_chunk(&mut self) -> Result<bool> {
31        self.chunk.clear();
32        self.offset = 0;
33
34        let mut tmpbuf: [u8; CHUNK_SIZE] = [0; CHUNK_SIZE];
35        let n = self.reader.read(&mut tmpbuf)?;
36        if n == 0 {
37            return Ok(false);
38        }
39        self.chunk.write_all(format!("{n:X}\r\n").as_bytes())?;
40        self.chunk.write_all(&tmpbuf[0..n])?;
41        self.chunk.write_all(b"\r\n")?;
42        Ok(true)
43    }
44
45    /// Returns the current chunk
46    ///
47    /// # NOTE
48    /// This method returns the whole chunk, even the parts alredy
49    /// read. If you want to know the remaining portion of the chunk
50    /// that hasn't been polled, see [offset](Self::offset)
51    pub fn current_chunk(&self) -> &[u8] {
52        &self.chunk
53    }
54
55    /// Returns the current offset. This is: The offset to the
56    /// part of the current chunk that hasn't been read yet
57    pub fn offset(&self) -> usize {
58        self.offset
59    }
60}
61
62impl<R: Read, const CHUNK_SIZE: usize> Read for Chunked<R, CHUNK_SIZE> {
63    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
64        if self.offset >= self.chunk.len() && !self.next_chunk()? {
65            return Ok(0);
66        }
67        let mut n = self.chunk.len() - self.offset;
68        if n > buf.len() {
69            n = buf.len();
70        }
71        buf.as_mut()
72            .write_all(&self.chunk[self.offset..self.offset + n])?;
73        self.offset += n;
74        Ok(n)
75    }
76}
77
78impl<R: Read + Default> Default for Chunked<R> {
79    fn default() -> Self {
80        Self::new(R::default())
81    }
82}
83
84#[cfg(test)]
85mod test {
86    pub use super::*;
87
88    const SIZE: usize = 1024;
89
90    fn test_chunks(input: &str) {
91        let mut chunked = Chunked::<_, SIZE>::new(input.as_bytes());
92        let mut out = Vec::new();
93
94        chunked.read_to_end(&mut out).unwrap();
95
96        let mut expected = Vec::new();
97        for chunk in input.as_bytes().chunks(SIZE) {
98            expected.extend_from_slice(format!("{:X}\r\n", chunk.len()).as_bytes());
99            expected.extend_from_slice(chunk);
100            expected.extend_from_slice(b"\r\n");
101        }
102        assert_eq!(out, expected);
103    }
104
105    #[test]
106    fn small() {
107        test_chunks("abcdefg");
108        test_chunks(&"0".repeat(SIZE - 50));
109    }
110
111    #[test]
112    fn exact_chunks() {
113        test_chunks(&"a".repeat(SIZE));
114        test_chunks(&"a".repeat(SIZE * 2));
115    }
116
117    #[test]
118    fn with_remaining() {
119        test_chunks(&"a".repeat(SIZE + 200));
120    }
121}