Skip to content

Commit 12d8dec

Browse files
committed
refactor(decompression-plz): cargo fmt linting
1 parent 4142354 commit 12d8dec

File tree

5 files changed

+288
-31
lines changed

5 files changed

+288
-31
lines changed

decompression-plz/src/decompression/state.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ pub fn decompression_runner<'a>(
218218
let mut state =
219219
DecompressionState::start(main, extra, encodings, buf.writer());
220220
loop {
221-
dbg!(&state);
222221
state = state.try_next()?;
223222
if state.is_ended() {
224223
return Ok(state);

decompression-plz/src/state.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use body_plz::variants::Body;
22
use bytes::BytesMut;
3-
use header_plz::body_headers::{
4-
content_encoding::ContentEncoding, encoding_info::EncodingInfo,
5-
};
3+
use header_plz::body_headers::encoding_info::EncodingInfo;
64

75
use crate::{
86
decode_struct::DecodeStruct,
@@ -61,9 +59,6 @@ where
6159
.unwrap()
6260
.encodings_as_mut()
6361
.pop();
64-
//if encoding_infos.last().unwrap().encodings().is_empty() {
65-
// encoding_infos.pop();
66-
//}
6762
}
6863

6964
let mut next_state = if is_chunked_te_only(&encoding_infos) {

decompression-plz/tests/test_cases/chunked.rs

Lines changed: 190 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::*;
22
use body_plz::variants::chunked::ChunkType;
33
use decompression_plz::chunked::{chunked_to_raw, partial_chunked_to_raw};
4-
use tests_utils::all_compressed_data;
4+
use tests_utils::{INPUT, all_compressed_data};
55

66
const HEADERS: &str = "Host: example.com\r\n\
77
Content-Type: text/html; charset=utf-8\r\n\
@@ -129,51 +129,192 @@ fn build_all_compressed_chunk_body() -> Body {
129129
Body::Chunked(chunk_vec)
130130
}
131131

132-
#[test]
133-
fn test_chunked_with_compression() {
134-
let headers = "Host: example.com\r\n\
135-
Content-Type: text/html; charset=utf-8\r\n\
136-
Transfer-Encoding: br, deflate, identity, gzip, zstd, chunked\r\n\
137-
\r\n";
132+
#[track_caller]
133+
fn assert_chunked_encoding(
134+
headers: &str,
135+
with_ce: bool,
136+
extra: Option<BytesMut>,
137+
) {
138138
let mut buf = BytesMut::new();
139139

140+
let verify = if extra.is_none() {
141+
"Host: example.com\r\n\
142+
Content-Type: text/html; charset=utf-8\r\n\
143+
Content-Length: 11\r\n\r\n\
144+
hello world"
145+
} else {
146+
"Host: example.com\r\n\
147+
Content-Type: text/html; charset=utf-8\r\n\
148+
Content-Length: 22\r\n\r\n\
149+
hello worldhello world"
150+
};
151+
140152
let mut tm = TestMessage::build(
141153
headers.into(),
142154
build_all_compressed_chunk_body(),
143-
None,
155+
extra,
144156
);
157+
145158
let mut state = DecodeState::init(&mut tm, &mut buf);
146159
state = state.try_next().unwrap();
147160
assert!(matches!(state, DecodeState::TransferEncoding(..)));
148161

162+
if with_ce {
163+
state = state.try_next().unwrap();
164+
assert!(matches!(state, DecodeState::ContentEncoding(..)));
165+
}
166+
149167
state = state.try_next().unwrap();
150168
assert!(matches!(state, DecodeState::UpdateContentLength(..)));
151169

152170
state = state.try_next().unwrap();
153171
assert!(state.is_ended());
154172

155-
let verify = "Host: example.com\r\n\
156-
Content-Type: text/html; charset=utf-8\r\n\
157-
Content-Length: 11\r\n\r\n\
158-
hello world";
159173
let result = tm.into_bytes();
160174
assert_eq!(result, verify);
161175
}
162176

177+
#[test]
178+
fn test_chunked_with_compression() {
179+
let headers = "Host: example.com\r\n\
180+
Content-Type: text/html; charset=utf-8\r\n\
181+
Transfer-Encoding: br, deflate, identity, gzip, zstd, chunked\r\n\
182+
\r\n";
183+
184+
assert_chunked_encoding(headers, false, None);
185+
}
186+
187+
#[test]
188+
fn test_chunked_with_compression_extra_raw() {
189+
let headers = "Host: example.com\r\n\
190+
Content-Type: text/html; charset=utf-8\r\n\
191+
Transfer-Encoding: br, deflate, identity, gzip, zstd, chunked\r\n\
192+
\r\n";
193+
194+
assert_chunked_encoding(headers, false, Some(INPUT.into()));
195+
}
196+
197+
#[test]
198+
fn test_chunked_with_compress_extra_compressed_together() {
199+
let body = all_compressed_data(); // len 53
200+
let mut chunk_vec = vec![
201+
ChunkType::Size("10\r\n".into()),
202+
ChunkType::Chunk(body[0..10].into()),
203+
ChunkType::Size("10\r\n".into()),
204+
ChunkType::Chunk(body[10..20].into()),
205+
ChunkType::Size("10\r\n".into()),
206+
ChunkType::Chunk(body[20..30].into()),
207+
ChunkType::Size("10\r\n".into()),
208+
ChunkType::Chunk(body[30..40].into()),
209+
ChunkType::Size("10\r\n".into()),
210+
];
211+
212+
for chunk in chunk_vec.iter_mut() {
213+
if let ChunkType::Chunk(chunk) = chunk {
214+
chunk.extend_from_slice("\r\n".as_bytes());
215+
}
216+
}
217+
let chunk_body = Body::Chunked(chunk_vec);
218+
let extra = BytesMut::from(&body[40..]);
219+
220+
let headers = "Host: example.com\r\n\
221+
Content-Type: text/html; charset=utf-8\r\n\
222+
Transfer-Encoding: br, deflate, identity, gzip, zstd, chunked\r\n\
223+
\r\n";
224+
225+
let mut tm = TestMessage::build(headers.into(), chunk_body, Some(extra));
226+
227+
let mut buf = BytesMut::new();
228+
let mut state = DecodeState::init(&mut tm, &mut buf);
229+
state = state.try_next().unwrap();
230+
assert!(matches!(state, DecodeState::TransferEncoding(..)));
231+
232+
state = state.try_next().unwrap();
233+
assert!(matches!(state, DecodeState::UpdateContentLength(..)));
234+
235+
state = state.try_next().unwrap();
236+
assert!(state.is_ended());
237+
assert_eq!(tm.into_bytes(), VERIFY_SINGLE_HEADER_BODY_ONLY);
238+
}
239+
240+
#[test]
241+
fn test_chunked_with_compress_extra_compressed_separate() {
242+
let body = build_all_compressed_chunk_body();
243+
let extra = all_compressed_data();
244+
245+
let headers = "Host: example.com\r\n\
246+
Content-Type: text/html; charset=utf-8\r\n\
247+
Transfer-Encoding: br, deflate, identity, gzip, zstd, chunked\r\n\
248+
\r\n";
249+
250+
let mut tm = TestMessage::build(headers.into(), body, Some(extra));
251+
252+
let mut buf = BytesMut::new();
253+
let mut state = DecodeState::init(&mut tm, &mut buf);
254+
state = state.try_next().unwrap();
255+
assert!(matches!(state, DecodeState::TransferEncoding(..)));
256+
257+
state = state.try_next().unwrap();
258+
assert!(matches!(state, DecodeState::UpdateContentLength(..)));
259+
260+
state = state.try_next().unwrap();
261+
assert!(state.is_ended());
262+
assert_eq!(tm.into_bytes(), VERIFY_SINGLE_HEADER_BODY_AND_EXTRA);
263+
}
264+
265+
/// Ce
163266
#[test]
164267
fn test_chunked_with_ce_compression() {
165268
let headers = "Host: example.com\r\n\
166269
Content-Type: text/html; charset=utf-8\r\n\
167270
Transfer-Encoding: chunked\r\n\
271+
Content-Encoding: br, deflate, identity, gzip, zstd\r\n\r\n";
272+
273+
assert_chunked_encoding(headers, true, None);
274+
}
275+
276+
#[test]
277+
fn test_chunked_with_ce_compression_extra_raw() {
278+
let headers = "Host: example.com\r\n\
279+
Content-Type: text/html; charset=utf-8\r\n\
280+
Transfer-Encoding: chunked\r\n\
281+
Content-Encoding: br, deflate, identity, gzip, zstd\r\n\r\n";
282+
283+
assert_chunked_encoding(headers, true, Some(INPUT.into()));
284+
}
285+
286+
#[test]
287+
fn test_chunked_with_ce_compress_extra_compressed_together() {
288+
let body = all_compressed_data(); // len 53
289+
let mut chunk_vec = vec![
290+
ChunkType::Size("10\r\n".into()),
291+
ChunkType::Chunk(body[0..10].into()),
292+
ChunkType::Size("10\r\n".into()),
293+
ChunkType::Chunk(body[10..20].into()),
294+
ChunkType::Size("10\r\n".into()),
295+
ChunkType::Chunk(body[20..30].into()),
296+
ChunkType::Size("10\r\n".into()),
297+
ChunkType::Chunk(body[30..40].into()),
298+
ChunkType::Size("10\r\n".into()),
299+
];
300+
301+
for chunk in chunk_vec.iter_mut() {
302+
if let ChunkType::Chunk(chunk) = chunk {
303+
chunk.extend_from_slice("\r\n".as_bytes());
304+
}
305+
}
306+
let chunk_body = Body::Chunked(chunk_vec);
307+
let extra = BytesMut::from(&body[40..]);
308+
309+
let headers = "Host: example.com\r\n\
310+
Content-Type: text/html; charset=utf-8\r\n\
168311
Content-Encoding: br, deflate, identity, gzip, zstd\r\n\
312+
Transfer-Encoding: chunked\r\n\
169313
\r\n";
170-
let mut buf = BytesMut::new();
171314

172-
let mut tm = TestMessage::build(
173-
headers.into(),
174-
build_all_compressed_chunk_body(),
175-
None,
176-
);
315+
let mut tm = TestMessage::build(headers.into(), chunk_body, Some(extra));
316+
317+
let mut buf = BytesMut::new();
177318
let mut state = DecodeState::init(&mut tm, &mut buf);
178319
state = state.try_next().unwrap();
179320
assert!(matches!(state, DecodeState::TransferEncoding(..)));
@@ -186,11 +327,36 @@ fn test_chunked_with_ce_compression() {
186327

187328
state = state.try_next().unwrap();
188329
assert!(state.is_ended());
330+
assert_eq!(tm.into_bytes(), VERIFY_SINGLE_HEADER_BODY_ONLY);
331+
}
189332

190-
let verify = "Host: example.com\r\n\
191-
Content-Type: text/html; charset=utf-8\r\n\
192-
Content-Length: 11\r\n\r\n\
193-
hello world";
194-
let result = tm.into_bytes();
195-
assert_eq!(result, verify);
333+
#[test]
334+
fn test_chunked_with_ce_compress_extra_compressed_separate() {
335+
let body = build_all_compressed_chunk_body();
336+
let extra = all_compressed_data();
337+
338+
let headers = "Host: example.com\r\n\
339+
Content-Type: text/html; charset=utf-8\r\n\
340+
Content-Encoding: br, deflate, identity, gzip, zstd\r\n\
341+
Transfer-Encoding: chunked\r\n\
342+
\r\n";
343+
344+
let mut tm = TestMessage::build(headers.into(), body, Some(extra));
345+
346+
let mut buf = BytesMut::new();
347+
let mut state = DecodeState::init(&mut tm, &mut buf);
348+
state = state.try_next().unwrap();
349+
assert!(matches!(state, DecodeState::TransferEncoding(..)));
350+
351+
state = state.try_next().unwrap();
352+
assert!(matches!(state, DecodeState::ContentEncoding(..)));
353+
354+
state = state.try_next().unwrap();
355+
assert!(matches!(state, DecodeState::UpdateContentLength(..)));
356+
357+
state = state.try_next().unwrap();
358+
assert!(state.is_ended());
359+
assert_eq!(tm.into_bytes(), VERIFY_SINGLE_HEADER_BODY_AND_EXTRA);
196360
}
361+
362+
// Partial
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,90 @@
11
use super::*;
2+
3+
#[track_caller]
4+
fn assert_partial_decompressed_multi_header(
5+
encoding: &str,
6+
extra: Option<BytesMut>,
7+
) {
8+
let headers = format!(
9+
"Host: example.com\r\n\
10+
Content-Type: text/html; charset=utf-8\r\n\
11+
{encoding}: br\r\n\
12+
Some-Other-Header: value\r\n\
13+
{encoding}: deflate, gzip, {ALL_COMPRESSIONS}\r\n\
14+
Content-Length: 11\r\n\r\n",
15+
);
16+
17+
let expected_state = encoding_state(encoding);
18+
19+
let verify = if extra.is_none() {
20+
format!(
21+
"Host: example.com\r\n\
22+
Content-Type: text/html; charset=utf-8\r\n\
23+
{encoding}: br\r\n\
24+
Some-Other-Header: value\r\n\
25+
{encoding}: deflate, gzip\r\n\
26+
Content-Length: 11\r\n\r\n\
27+
hello world",
28+
)
29+
} else {
30+
format!(
31+
"Host: example.com\r\n\
32+
Content-Type: text/html; charset=utf-8\r\n\
33+
{encoding}: br\r\n\
34+
Some-Other-Header: value\r\n\
35+
{encoding}: deflate, gzip\r\n\
36+
Content-Length: 22\r\n\r\n\
37+
hello worldhello world",
38+
)
39+
};
40+
41+
let mut tm = TestMessage::build(
42+
headers.as_bytes().into(),
43+
Body::Raw(all_compressed_data()),
44+
extra,
45+
);
46+
47+
let mut buf = BytesMut::new();
48+
let mut state = DecodeState::init(&mut tm, &mut buf);
49+
state = state.try_next().unwrap();
50+
assert!((expected_state)(&state));
51+
52+
state = state.try_next().unwrap();
53+
assert!(matches!(state, DecodeState::UpdateContentLengthAndErr(..)));
54+
55+
if let Err(e) = state.try_next() {
56+
assert!(matches!(e, MultiDecompressErrorReason::Partial { .. }));
57+
let result = tm.into_bytes();
58+
59+
assert_eq!(result, verify);
60+
} else {
61+
panic!()
62+
}
63+
}
64+
65+
#[test]
66+
fn test_partial_te_multi_header() {
67+
assert_partial_decompressed_multi_header(TRANSFER_ENCODING, None);
68+
}
69+
70+
#[test]
71+
fn test_partial_te_multi_header_extra_raw() {
72+
assert_partial_decompressed_multi_header(
73+
TRANSFER_ENCODING,
74+
Some(INPUT.into()),
75+
);
76+
}
77+
78+
//
79+
#[test]
80+
fn test_partial_ce_multi_header() {
81+
assert_partial_decompressed_multi_header(CONTENT_ENCODING, None);
82+
}
83+
84+
#[test]
85+
fn test_partial_ce_multi_header_extra_raw() {
86+
assert_partial_decompressed_multi_header(
87+
CONTENT_ENCODING,
88+
Some(INPUT.into()),
89+
);
90+
}

decompression-plz/tests/test_cases/partial/single_header.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,11 @@ fn test_partial_te_single_header_extra_raw() {
9595
fn test_partial_ce_single_header() {
9696
assert_partial_decompressed_single_header(CONTENT_ENCODING, None);
9797
}
98+
99+
#[test]
100+
fn test_partial_ce_single_header_extra_raw() {
101+
assert_partial_decompressed_single_header(
102+
CONTENT_ENCODING,
103+
Some(INPUT.into()),
104+
);
105+
}

0 commit comments

Comments
 (0)