Skip to content

Commit 44ed510

Browse files
committed
feat(decompression-plz): added apply_compression function to apply compressions based on EncodingType
1 parent b59557f commit 44ed510

File tree

3 files changed

+45
-60
lines changed

3 files changed

+45
-60
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ where
3333
encoding,
3434
ContentEncoding::Identity | ContentEncoding::Chunked
3535
) {
36-
dbg!(compression_index);
3736
continue;
3837
}
3938
let result = decompress_single(curs, &mut writer, encoding);

decompression-plz/src/dtraits.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ pub trait DecompressTrait {
2525

2626
fn set_body(&mut self, body: Body);
2727

28-
//
29-
//
3028
//fn remove_header_on_position(&mut self, position: usize); // depends - header_map_as_mut
3129
//
3230
//fn update_header_value_on_position(

decompression-plz/src/lib.rs

Lines changed: 45 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
#![allow(warnings, unused)]
22

33
use bytes::BytesMut;
4-
use header_plz::body_headers::BodyHeader;
4+
use header_plz::body_headers::{BodyHeader, encoding_info::EncodingInfo};
55

66
use crate::{
77
content_length::add_body_and_update_cl,
8-
decompression::multi::error::MultiDecompressErrorReason, state::runner,
8+
decompression::multi::error::{
9+
MultiDecompressError, MultiDecompressErrorReason,
10+
},
11+
dtraits::DecompressTrait,
12+
encoding_type::EncodingType,
13+
state::runner,
914
};
1015
mod content_length;
1116
pub mod decompression;
1217
pub mod dstruct;
1318
pub mod dtraits;
19+
mod encoding_type;
1420
mod error;
1521
mod state;
1622

@@ -20,72 +26,54 @@ pub fn decompress<T>(
2026
buf: &mut BytesMut,
2127
) -> Result<(), error::DecompressErrorStruct>
2228
where
23-
T: dtraits::DecompressTrait,
29+
T: DecompressTrait,
2430
{
2531
let mut body = message.get_body().into_bytes().unwrap();
2632
let mut body_headers = message.body_headers_as_mut().take();
2733

28-
//
29-
if let Some(BodyHeader {
30-
transfer_encoding: Some(einfo_list),
31-
..
32-
}) = body_headers.as_mut()
33-
{
34-
match runner(&body, extra_body.as_deref(), einfo_list, buf) {
35-
Ok(state) => {
36-
(body, extra_body) = state.into();
37-
}
38-
Err(e) => {
39-
eprintln!("{:?}", e);
40-
match e.reason {
41-
MultiDecompressErrorReason::Partial {
42-
partial_body,
43-
header_index,
44-
compression_index,
45-
} => {
46-
todo!()
47-
}
48-
_ => {
49-
return Ok(());
50-
}
51-
}
52-
}
53-
}
54-
}
34+
apply_encoding(
35+
&mut message,
36+
EncodingType::TransferEncoding,
37+
body_headers.as_mut(),
38+
&body,
39+
extra_body.as_deref(),
40+
buf,
41+
);
5542

56-
//
57-
if let Some(BodyHeader {
58-
content_encoding: Some(einfo_list),
59-
..
60-
}) = body_headers.as_mut()
61-
{
62-
match runner(&body, extra_body.as_deref(), einfo_list, buf) {
63-
Ok(state) => {
64-
(body, extra_body) = state.into();
65-
}
66-
Err(e) => {
67-
eprintln!("{:?}", e);
68-
match e.reason {
69-
MultiDecompressErrorReason::Partial {
70-
partial_body,
71-
header_index,
72-
compression_index,
73-
} => {
74-
todo!()
75-
}
76-
_ => {
77-
return Ok(());
78-
}
79-
}
80-
}
81-
}
82-
}
43+
apply_encoding(
44+
&mut message,
45+
EncodingType::ContentEncoding,
46+
body_headers.as_mut(),
47+
&body,
48+
extra_body.as_deref(),
49+
buf,
50+
);
8351

8452
//
8553
add_body_and_update_cl(&mut message, body, body_headers);
8654
Ok(())
8755
}
8856

57+
fn apply_encoding<T>(
58+
message: &mut T,
59+
encoding_type: EncodingType,
60+
body_headers: Option<&mut BodyHeader>,
61+
body: &[u8],
62+
extra_body: Option<&[u8]>,
63+
buf: &mut BytesMut,
64+
) -> Result<(), MultiDecompressError>
65+
where
66+
T: DecompressTrait,
67+
{
68+
if let Some(einfo_list) = encoding_type.encoding_info(body_headers) {
69+
match runner(&body, extra_body.as_deref(), einfo_list, buf) {
70+
Ok(_) => todo!(),
71+
Err(_) => todo!(),
72+
}
73+
};
74+
Ok(())
75+
}
76+
8977
// helper function for tests
9078
#[cfg(test)]
9179
pub mod tests {

0 commit comments

Comments
 (0)