Skip to content

Commit 11a3f7c

Browse files
committed
feat(decompression-plz): added ExtraRaw variant to DecompressionError
1 parent b572f7f commit 11a3f7c

File tree

3 files changed

+40
-20
lines changed

3 files changed

+40
-20
lines changed

decompression-plz/src/decompression/dstruct.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use header_plz::body_headers::{
88
use crate::decompression::{
99
magic_bytes::is_compressed,
1010
multi::{decompress_multi, error::MultiDecompressError},
11-
single::decompress_single,
11+
single::{decompress_single, error::DecompressError},
1212
};
1313

1414
pub struct DecompressionStruct<'a> {
@@ -128,7 +128,7 @@ impl<'a> DecompressionStruct<'a> {
128128
);
129129
if let Err(e) = result {
130130
self.push_last_encoding(last_encoding);
131-
return Err(e);
131+
return Err(e.into());
132132
}
133133
let output = self.writer.get_mut().split();
134134
if !self.is_encodings_empty() {
@@ -144,20 +144,22 @@ impl<'a> DecompressionStruct<'a> {
144144
mut writer: &mut Writer<&mut BytesMut>,
145145
content_encoding: &ContentEncoding,
146146
len: u64,
147-
) -> Result<(), MultiDecompressError> {
147+
) -> Result<(), DecompressError> {
148148
if let ContentEncoding::Deflate = content_encoding {
149149
let mut reader = flate2::read::ZlibDecoder::new(input);
150-
std::io::copy(&mut reader, &mut writer)?;
150+
std::io::copy(&mut reader, &mut writer)
151+
.map_err(DecompressError::Copy)?;
151152
if reader.total_in() != len {
152-
return Err(MultiDecompressError::extra_raw());
153+
return Err(DecompressError::deflate());
153154
}
154155
return Ok(());
155156
}
156-
decompress_single(&mut input, &mut writer, content_encoding)
157-
.map_err(|_| MultiDecompressError::extra_raw())?;
157+
// others
158+
decompress_single(&mut input, &mut writer, content_encoding)?;
158159
let (_, extra_curs) = input.get_ref();
160+
// brotli
159161
if extra_curs.position() == 0 {
160-
return Err(MultiDecompressError::extra_raw());
162+
return Err(DecompressError::extra_raw(content_encoding.clone()));
161163
}
162164
Ok(())
163165
}
@@ -462,8 +464,16 @@ mod tests {
462464

463465
let err = ds.try_decompress_main_plus_extra().unwrap_err();
464466

465-
assert_eq!(err.reason, MultiDecompressErrorReason::ExtraRaw);
466-
assert!(matches!(err.error, DecompressError::Copy(_)));
467+
assert_eq!(err.reason, MultiDecompressErrorReason::Corrupt);
468+
match encoding {
469+
ContentEncoding::Zstd => {
470+
assert!(matches!(err.error, DecompressError::Zstd(_)))
471+
}
472+
_ => {
473+
assert!(matches!(err.error, DecompressError::ExtraRaw(_)))
474+
}
475+
}
476+
467477
assert_eq!(ds.encoding_info, original_info);
468478
}
469479
}

decompression-plz/src/decompression/multi/error.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,6 @@ impl MultiDecompressError {
2929
Self::corrupt(DecompressError::Deflate(e))
3030
}
3131

32-
pub fn extra_raw() -> Self {
33-
let e = std::io::Error::from(std::io::ErrorKind::InvalidData);
34-
MultiDecompressError::new(
35-
MultiDecompressErrorReason::ExtraRaw,
36-
DecompressError::Copy(e),
37-
)
38-
}
39-
4032
pub fn reason(&self) -> &MultiDecompressErrorReason {
4133
&self.reason
4234
}
@@ -80,6 +72,12 @@ impl From<MultiDecompressError> for DecompressError {
8072
}
8173
}
8274

75+
impl From<DecompressError> for MultiDecompressError {
76+
fn from(e: DecompressError) -> Self {
77+
MultiDecompressError::new(MultiDecompressErrorReason::Corrupt, e)
78+
}
79+
}
80+
8381
#[cfg_attr(test, derive(PartialEq, Eq))]
8482
#[derive(Debug, Error)]
8583
pub enum MultiDecompressErrorReason {
@@ -93,8 +91,6 @@ pub enum MultiDecompressErrorReason {
9391
},
9492
#[error("Copy")]
9593
Copy,
96-
#[error("ExtraRaw")]
97-
ExtraRaw,
9894
}
9995

10096
impl MultiDecompressErrorReason {

decompression-plz/src/decompression/single/error.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use header_plz::body_headers::content_encoding::ContentEncoding;
12
use std::io::Error;
23
use thiserror::Error;
34

@@ -18,4 +19,17 @@ pub enum DecompressError {
1819
Identity(Error),
1920
#[error("unknown| {0}")]
2021
Unknown(String),
22+
23+
#[error("extra raw")]
24+
ExtraRaw(ContentEncoding),
25+
}
26+
27+
impl DecompressError {
28+
pub fn deflate() -> Self {
29+
DecompressError::ExtraRaw(ContentEncoding::Deflate)
30+
}
31+
32+
pub fn extra_raw(encoding: ContentEncoding) -> Self {
33+
DecompressError::ExtraRaw(encoding)
34+
}
2135
}

0 commit comments

Comments
 (0)