Skip to main content

aria2_protocol/http/
encoding.rs

1use flate2::read::{DeflateDecoder, GzDecoder};
2use std::io::Read;
3use tracing::debug;
4
5pub struct HttpEncoding;
6
7impl HttpEncoding {
8    pub fn decode(body: &[u8], content_encoding: Option<&str>) -> Result<Vec<u8>, String> {
9        let encoding = content_encoding.unwrap_or("").to_lowercase();
10        match encoding.as_str() {
11            "" | "identity" => Ok(body.to_vec()),
12            "gzip" | "x-gzip" => Self::decode_gzip(body),
13            "deflate" => Self::decode_deflate(body),
14            "br" => Err("Brotli compression not supported yet".to_string()),
15            "compress" => Err("compress compression format is deprecated".to_string()),
16            other => Err(format!("Unsupported Content-Encoding: {}", other)),
17        }
18    }
19
20    fn decode_gzip(data: &[u8]) -> Result<Vec<u8>, String> {
21        if data.is_empty() {
22            return Ok(Vec::new());
23        }
24        let mut decoder = GzDecoder::new(data);
25        let mut decompressed = Vec::with_capacity(data.len() * 4);
26        decoder
27            .read_to_end(&mut decompressed)
28            .map_err(|e| format!("gzip decompression failed: {}", e))?;
29        debug!(
30            "gzip decompression complete: {} -> {} bytes",
31            data.len(),
32            decompressed.len()
33        );
34        Ok(decompressed)
35    }
36
37    fn decode_deflate(data: &[u8]) -> Result<Vec<u8>, String> {
38        if data.is_empty() {
39            return Ok(Vec::new());
40        }
41        let mut decoder = DeflateDecoder::new(data);
42        let mut decompressed = Vec::with_capacity(data.len() * 4);
43        decoder
44            .read_to_end(&mut decompressed)
45            .map_err(|e| format!("deflate decompression failed: {}", e))?;
46        debug!(
47            "deflate decompression complete: {} -> {} bytes",
48            data.len(),
49            decompressed.len()
50        );
51        Ok(decompressed)
52    }
53
54    pub fn detect_best_accept_encoding() -> &'static str {
55        "gzip, deflate"
56    }
57
58    pub fn is_compressed(content_encoding: Option<&str>) -> bool {
59        matches!(
60            content_encoding.map(|e| e.to_lowercase()).as_deref(),
61            Some("gzip" | "deflate" | "br" | "compress")
62        )
63    }
64}
65
66pub struct ChunkedDecoder;
67
68impl ChunkedDecoder {
69    pub fn decode(data: &[u8]) -> Result<Vec<u8>, String> {
70        let mut result = Vec::new();
71        let mut pos = 0;
72
73        while pos < data.len() {
74            let line_end = data[pos..]
75                .iter()
76                .position(|&b| b == b'\r' || b == b'\n')
77                .ok_or("Chunked encoding format error: cannot find chunk size line")?;
78
79            let size_str = std::str::from_utf8(&data[pos..pos + line_end])
80                .map_err(|e| format!("Invalid UTF-8 in chunk size line: {}", e))?;
81            let size_str = size_str.trim();
82            let chunk_size: usize = usize::from_str_radix(size_str, 16)
83                .map_err(|e| format!("Chunk size parsing failed: {}", e))?;
84
85            pos += line_end;
86            if pos < data.len() && data[pos] == b'\r' {
87                pos += 1;
88            }
89            if pos < data.len() && data[pos] == b'\n' {
90                pos += 1;
91            }
92
93            if chunk_size == 0 {
94                break;
95            }
96
97            if pos + chunk_size > data.len() {
98                return Err("Chunked data truncated".to_string());
99            }
100
101            result.extend_from_slice(&data[pos..pos + chunk_size]);
102            pos += chunk_size;
103
104            if pos + 1 < data.len() && data[pos] == b'\r' && data[pos + 1] == b'\n' {
105                pos += 2;
106            } else if pos < data.len() && data[pos] == b'\n' {
107                pos += 1;
108            }
109        }
110
111        Ok(result)
112    }
113}
114
115#[cfg(test)]
116mod tests {
117    use super::*;
118
119    #[test]
120    fn test_decode_identity() {
121        let data = b"hello world";
122        let result = HttpEncoding::decode(data, None).unwrap();
123        assert_eq!(result, b"hello world");
124
125        let result = HttpEncoding::decode(data, Some("identity")).unwrap();
126        assert_eq!(result, b"hello world");
127    }
128
129    #[test]
130    fn test_gzip_roundtrip() {
131        use flate2::Compression;
132        use flate2::write::GzEncoder;
133        use std::io::Write;
134
135        let original = b"Hello, this is a test string that should compress well!";
136        let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
137        encoder.write_all(original).unwrap();
138        let compressed = encoder.finish().unwrap();
139
140        let decompressed = HttpEncoding::decode(&compressed, Some("gzip")).unwrap();
141        assert_eq!(decompressed, original);
142    }
143
144    #[test]
145    fn test_detect_compression() {
146        assert!(HttpEncoding::is_compressed(Some("gzip")));
147        assert!(HttpEncoding::is_compressed(Some("deflate")));
148        assert!(HttpEncoding::is_compressed(Some("br")));
149        assert!(!HttpEncoding::is_compressed(None));
150        assert!(!HttpEncoding::is_compressed(Some("identity")));
151    }
152
153    #[test]
154    fn test_chunked_decoder() {
155        let raw_data = "5\r\nhello\r\n6\r\n world\r\n0\r\n\r\n";
156        let decoded = ChunkedDecoder::decode(raw_data.as_bytes()).unwrap();
157        assert_eq!(decoded, b"hello world");
158    }
159}