Skip to content

Commit 12a3a2e

Browse files
committed
feat: added functions encodings_in_single_header and encoding_header_positions to find headers postions containing encodings
1 parent dc8171d commit 12a3a2e

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

header-plz/src/body_headers/encoding_info.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,29 @@ impl EncodingInfo {
3636
}
3737
}
3838

39+
pub fn encodings_in_single_header(encoding_info: &[EncodingInfo]) -> Option<usize> {
40+
let mut iter = encoding_info.iter();
41+
let first = iter.next().unwrap().header_index;
42+
iter.all(|elem| elem.header_index == first)
43+
.then(|| encoding_info[0].header_index)
44+
}
45+
46+
pub fn encoding_header_positions(encoding_info: &[EncodingInfo]) -> impl Iterator<Item = usize> {
47+
encoding_info
48+
.iter()
49+
.map(|einfo| einfo.header_index)
50+
.scan(None, |state, idx| {
51+
let out = if Some(idx) != *state {
52+
*state = Some(idx);
53+
Some(idx)
54+
} else {
55+
None
56+
};
57+
Some(out)
58+
})
59+
.flatten()
60+
}
61+
3962
#[cfg(test)]
4063
mod test {
4164
use super::*;
@@ -52,4 +75,54 @@ mod test {
5275
];
5376
assert_eq!(result, verify);
5477
}
78+
79+
#[test]
80+
fn test_are_encodings_in_same_header_same() {
81+
let input = vec![
82+
EncodingInfo::from((9, ContentEncoding::Gzip)),
83+
EncodingInfo::from((9, ContentEncoding::Deflate)),
84+
EncodingInfo::from((9, ContentEncoding::Brotli)),
85+
EncodingInfo::from((9, ContentEncoding::Compress)),
86+
];
87+
88+
assert_eq!(encodings_in_single_header(&input), Some(9));
89+
}
90+
91+
#[test]
92+
fn test_are_encodings_in_same_header_diff() {
93+
let input = vec![
94+
EncodingInfo::from((0, ContentEncoding::Gzip)),
95+
EncodingInfo::from((1, ContentEncoding::Deflate)),
96+
EncodingInfo::from((2, ContentEncoding::Brotli)),
97+
EncodingInfo::from((3, ContentEncoding::Compress)),
98+
];
99+
100+
assert!(encodings_in_single_header(&input).is_none());
101+
}
102+
103+
#[test]
104+
fn test_header_positions() {
105+
let input = vec![
106+
EncodingInfo::from((0, ContentEncoding::Gzip)),
107+
EncodingInfo::from((1, ContentEncoding::Deflate)),
108+
EncodingInfo::from((2, ContentEncoding::Brotli)),
109+
EncodingInfo::from((3, ContentEncoding::Compress)),
110+
];
111+
let pos: Vec<usize> = encoding_header_positions(&input).collect();
112+
113+
assert_eq!(pos, vec![0, 1, 2, 3]);
114+
}
115+
116+
#[test]
117+
fn test_header_positions_duplicate() {
118+
let input = vec![
119+
EncodingInfo::from((0, ContentEncoding::Gzip)),
120+
EncodingInfo::from((0, ContentEncoding::Deflate)),
121+
EncodingInfo::from((1, ContentEncoding::Brotli)),
122+
EncodingInfo::from((1, ContentEncoding::Compress)),
123+
];
124+
let pos: Vec<usize> = encoding_header_positions(&input).collect();
125+
126+
assert_eq!(pos, vec![0, 1]);
127+
}
55128
}

0 commit comments

Comments
 (0)