Skip to content

Commit 355d060

Browse files
committed
feat(decompression-plz): changed Option<BodyHeader> to BodyHeader in DecodeStruct
1 parent 12d8dec commit 355d060

File tree

2 files changed

+15
-25
lines changed

2 files changed

+15
-25
lines changed

decompression-plz/src/decode_struct.rs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub struct DecodeStruct<'a, T> {
1414
pub message: &'a mut T,
1515
pub body: BytesMut,
1616
pub extra_body: Option<BytesMut>,
17-
pub body_headers: Option<BodyHeader>,
17+
pub body_headers: BodyHeader,
1818
pub buf: &'a mut BytesMut,
1919
}
2020

@@ -31,7 +31,8 @@ where
3131
}
3232
};
3333
let extra_body = message.get_extra_body();
34-
let body_headers = message.body_headers_as_mut().take();
34+
let body_headers =
35+
message.body_headers_as_mut().take().unwrap_or_default();
3536
Self {
3637
message,
3738
body_headers,
@@ -43,11 +44,7 @@ where
4344

4445
// TODO: implement new method in BodyHeader
4546
pub fn is_chunked_te(&self) -> bool {
46-
self.body_headers
47-
.as_ref()
48-
.and_then(|bh| bh.transfer_type.as_ref())
49-
.map(|tt| tt == &TransferType::Chunked)
50-
.unwrap_or(false)
47+
self.body_headers.transfer_type == Some(TransferType::Chunked)
5148
}
5249

5350
pub fn chunked_to_raw(&mut self) {
@@ -56,33 +53,27 @@ where
5653
}
5754

5855
pub fn transfer_encoding_is_some(&self) -> bool {
59-
self.body_headers
60-
.as_ref()
61-
.and_then(|bh| bh.transfer_encoding.as_ref())
62-
.is_some()
56+
self.body_headers.transfer_encoding.is_some()
6357
}
6458

6559
pub fn content_encoding_is_some(&self) -> bool {
66-
self.body_headers
67-
.as_ref()
68-
.and_then(|bh| bh.content_encoding.as_ref())
69-
.is_some()
60+
self.body_headers.content_encoding.is_some()
7061
}
7162

7263
pub fn get_transfer_encoding(&mut self) -> Vec<EncodingInfo> {
73-
self.body_headers.as_mut().unwrap().transfer_encoding.take().unwrap()
64+
self.body_headers.transfer_encoding.take().unwrap()
7465
}
7566

7667
pub fn get_content_encoding(&mut self) -> Vec<EncodingInfo> {
77-
self.body_headers.as_mut().unwrap().content_encoding.take().unwrap()
68+
self.body_headers.content_encoding.take().unwrap()
7869
}
7970

8071
pub fn set_transfer_encoding(&mut self, te: Vec<EncodingInfo>) {
81-
self.body_headers.as_mut().unwrap().transfer_encoding = Some(te);
72+
self.body_headers.transfer_encoding = Some(te);
8273
}
8374

8475
pub fn set_content_encoding(&mut self, ce: Vec<EncodingInfo>) {
85-
self.body_headers.as_mut().unwrap().content_encoding = Some(ce);
76+
self.body_headers.content_encoding = Some(ce);
8677
}
8778

8879
pub fn extra_body_is_some(&self) -> bool {

decompression-plz/src/state.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,18 @@ where
4848
Ok(next_state)
4949
}
5050
DecodeState::TransferEncoding(mut ds, mut encoding_infos) => {
51-
// Convert chunked to raw
52-
// http/1 only
53-
// remove chunked te from encoding_infos
5451
if ds.is_chunked_te() {
5552
ds.chunked_to_raw();
5653
// remove chunked TE
54+
// Chunked TE must be the last
5755
encoding_infos
5856
.last_mut()
5957
.unwrap()
6058
.encodings_as_mut()
6159
.pop();
6260
}
63-
64-
let mut next_state = if is_chunked_te_only(&encoding_infos) {
61+
// If only chunked was present then Vec<EncodingInfo> is empty
62+
let mut next_state = if is_empty_encodings(&encoding_infos) {
6563
ds.message.header_map_as_mut().remove_header_on_position(
6664
encoding_infos[0].header_index,
6765
);
@@ -202,6 +200,7 @@ where
202200
}
203201
}
204202

205-
fn is_chunked_te_only(einfo_vec: &[EncodingInfo]) -> bool {
203+
// Only chunked was present
204+
fn is_empty_encodings(einfo_vec: &[EncodingInfo]) -> bool {
206205
einfo_vec.len() == 1 && einfo_vec[0].encodings().is_empty()
207206
}

0 commit comments

Comments
 (0)