Skip to content

Commit 47b1a69

Browse files
committed
rust/htp: convert to nom 8
1 parent 1b6457c commit 47b1a69

12 files changed

Lines changed: 183 additions & 194 deletions

rust/Cargo.lock.in

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

rust/htp/Cargo.toml.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ crate-type = ["staticlib", "rlib", "cdylib"]
2424
base64 = "0.22.1"
2525
bstr = "1.12.0"
2626
libc = "0.2"
27-
nom = "7.1.3"
27+
nom = "8.0.0"
2828
lzma-rs = { version = "0.2.0", features = ["stream"] }
2929
flate2 = { version = "~1.0.35", features = ["zlib-default"], default-features = false }
3030
brotli = "~8.0.1"

rust/htp/src/decompressors.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -455,10 +455,10 @@ impl GzipBufWriter {
455455
fn parse_start(data: &[u8]) -> nom::IResult<&[u8], u8> {
456456
use nom::bytes::streaming::tag;
457457
use nom::number::streaming::{le_i32, le_u8};
458-
use nom::sequence::tuple;
458+
use nom::Parser;
459459

460460
let (rest, (_, flags, _mtime, _xfl, _operating_system)) =
461-
tuple((tag(b"\x1f\x8b\x08"), le_u8, le_i32, le_u8, le_u8))(data)?;
461+
(tag(&b"\x1f\x8b\x08"[..]), le_u8, le_i32, le_u8, le_u8).parse(data)?;
462462
Ok((rest, flags))
463463
}
464464
}
@@ -467,7 +467,7 @@ impl Write for GzipBufWriter {
467467
fn write(&mut self, data: &[u8]) -> std::io::Result<usize> {
468468
use nom::bytes::streaming::{tag, take_until};
469469
use nom::number::streaming::le_u16;
470-
use nom::sequence::tuple;
470+
use nom::Parser;
471471

472472
const FHCRC: u8 = 1 << 1;
473473
const FEXTRA: u8 = 1 << 2;
@@ -534,10 +534,10 @@ impl Write for GzipBufWriter {
534534
}
535535
GzState::Filename => {
536536
if self.flags & FNAME != 0 {
537-
match tuple((
538-
take_until::<&[u8], &[u8], nom::error::Error<&[u8]>>(b"\0" as &[u8]),
539-
tag(b"\0"),
540-
))(parse)
537+
match (
538+
take_until::<&[u8], &[u8], nom::error::Error<&[u8]>>(&b"\0"[..]),
539+
tag(&b"\0"[..]),
540+
).parse(parse)
541541
{
542542
Ok((rest, _)) => {
543543
parse = rest;
@@ -557,10 +557,10 @@ impl Write for GzipBufWriter {
557557
}
558558
GzState::Comment => {
559559
if self.flags & FCOMMENT != 0 {
560-
match tuple((
561-
take_until::<&[u8], &[u8], nom::error::Error<&[u8]>>(b"\0" as &[u8]),
562-
tag(b"\0"),
563-
))(parse)
560+
match (
561+
take_until::<&[u8], &[u8], nom::error::Error<&[u8]>>(&b"\0"[..]),
562+
tag(&b"\0"[..]),
563+
).parse(parse)
564564
{
565565
Ok((rest, _)) => {
566566
parse = rest;

rust/htp/src/headers.rs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
use crate::util::{is_token, trimmed, FlagOperations};
1+
use crate::util::{is_space, is_token, trimmed, FlagOperations};
22
use nom::{
33
branch::alt,
44
bytes::complete::tag as complete_tag,
55
bytes::streaming::{tag, take_till, take_while, take_while1},
6-
character::{is_space, streaming::space0},
6+
character::streaming::space0,
77
combinator::{complete, map, not, opt, peek},
8-
sequence::tuple,
98
Err::Incomplete,
109
IResult, Needed,
10+
Parser as NomParser,
1111
};
1212

1313
/// Helper for Parsed bytes and corresponding HeaderFlags
@@ -133,9 +133,9 @@ impl Parser {
133133
complete_tag("\n\r"),
134134
complete_tag("\n"),
135135
complete_tag("\r"),
136-
))(input)
136+
)).parse(input)
137137
} else {
138-
alt((complete_tag("\r\n"), complete_tag("\n")))(input)
138+
alt((complete_tag("\r\n"), complete_tag("\n"))).parse(input)
139139
}
140140
}
141141
}
@@ -146,38 +146,38 @@ impl Parser {
146146
if self.side == Side::Response {
147147
alt((
148148
map(
149-
tuple((
149+
(
150150
complete_tag("\n\r\r\n"),
151151
peek(alt((complete_tag("\n"), complete_tag("\r\n")))),
152-
)),
152+
),
153153
|(eol, _)| (eol, HeaderFlags::DEFORMED_EOL),
154154
),
155155
map(
156-
tuple((
156+
(
157157
complete_tag("\r\n\r"),
158158
take_while1(|c| c == b'\r' || c == b' ' || c == b'\t'),
159159
opt(complete_tag("\n")),
160160
not(alt((complete_tag("\n"), complete_tag("\r\n")))),
161-
)),
161+
),
162162
|(eol1, eol2, eol3, _): (&[u8], &[u8], Option<&[u8]>, _)| {
163163
(
164164
&input[..(eol1.len() + eol2.len() + eol3.unwrap_or(b"").len())],
165165
HeaderFlags::DEFORMED_EOL,
166166
)
167167
},
168168
),
169-
))(input)
169+
)).parse(input)
170170
} else {
171171
map(
172172
alt((
173-
tuple((
173+
(
174174
complete_tag("\n\r\r\n"),
175175
peek(alt((complete_tag("\n"), complete_tag("\r\n")))),
176-
)),
177-
tuple((complete_tag("\n\r"), peek(complete_tag("\r\n")))),
176+
),
177+
(complete_tag("\n\r"), peek(complete_tag("\r\n"))),
178178
)),
179179
|(eol, _)| (eol, HeaderFlags::DEFORMED_EOL),
180-
)(input)
180+
).parse(input)
181181
}
182182
}
183183
}
@@ -188,46 +188,46 @@ impl Parser {
188188
alt((
189189
self.complete_eol_deformed(),
190190
map(self.complete_eol_regular(), |eol| (eol, 0)),
191-
))(input)
191+
)).parse(input)
192192
}
193193
}
194194

195195
/// Parse one header end of line, and guarantee that it is not folding
196196
fn eol(&self) -> impl Fn(&[u8]) -> IResult<&[u8], ParsedBytes> + '_ {
197197
move |input| {
198198
map(
199-
tuple((self.complete_eol(), not(folding_lws))),
199+
(self.complete_eol(), not(folding_lws)),
200200
|(end, _)| end,
201-
)(input)
201+
).parse(input)
202202
}
203203
}
204204

205205
/// Parse one null byte or one end of line, and guarantee that it is not folding
206206
fn null_or_eol(&self) -> impl Fn(&[u8]) -> IResult<&[u8], ParsedBytes> + '_ {
207-
move |input| alt((null, self.eol()))(input)
207+
move |input| alt((null, self.eol())).parse(input)
208208
}
209209

210210
/// Parse one null byte or complete end of line
211211
fn complete_null_or_eol(&self) -> impl Fn(&[u8]) -> IResult<&[u8], ParsedBytes> + '_ {
212-
move |input| alt((null, self.complete_eol()))(input)
212+
move |input| alt((null, self.complete_eol())).parse(input)
213213
}
214214

215215
/// Parse header folding bytes (eol + whitespace or eol + special cases)
216216
fn folding(&self) -> impl Fn(&[u8]) -> IResult<&[u8], FoldingBytes> + '_ {
217217
move |input| {
218218
if self.side == Side::Response {
219219
map(
220-
tuple((
220+
(
221221
map(self.complete_eol_regular(), |eol| (eol, 0)),
222222
folding_lws,
223-
)),
223+
),
224224
|((eol, flags), (lws, other_flags))| (eol, lws, flags | other_flags),
225-
)(input)
225+
).parse(input)
226226
} else {
227227
map(
228-
tuple((self.complete_eol(), folding_lws)),
228+
(self.complete_eol(), folding_lws),
229229
|((eol, flags), (lws, other_flags))| (eol, lws, flags | other_flags),
230-
)(input)
230+
).parse(input)
231231
}
232232
}
233233
}
@@ -242,7 +242,7 @@ impl Parser {
242242
((end, flags), Some(fold))
243243
})),
244244
map(self.complete_null_or_eol(), |end| (end, None)),
245-
))(input)
245+
)).parse(input)
246246
}
247247
}
248248

@@ -256,7 +256,7 @@ impl Parser {
256256
((end, flags), Some(fold))
257257
}),
258258
map(self.null_or_eol(), |end| (end, None)),
259-
))(input)
259+
)).parse(input)
260260
}
261261
}
262262

@@ -297,7 +297,7 @@ impl Parser {
297297
loop {
298298
if self.side == Side::Response {
299299
// Peek ahead for ambiguous name with lws vs. value with folding
300-
match tuple((token_chars, separator_regular))(i) {
300+
match (token_chars, separator_regular).parse(i) {
301301
Ok((_, ((_, tokens, _), (_, _)))) if !tokens.is_empty() => {
302302
flags.unset(HeaderFlags::FOLDING_SPECIAL_CASE);
303303
if value.is_empty() {
@@ -397,13 +397,13 @@ impl Parser {
397397

398398
/// Parse a separator between header name and value
399399
fn separator(&self) -> impl Fn(&[u8]) -> IResult<&[u8], u64> + '_ {
400-
move |input| map(separator_regular, |_| 0)(input)
400+
move |input| map(separator_regular, |_| 0).parse(input)
401401
}
402402

403403
/// Parse data before an eol with no colon as an empty name with the data as the value
404404
fn header_sans_colon(&self) -> impl Fn(&[u8]) -> IResult<&[u8], Header> + '_ {
405405
move |input| {
406-
let (remaining, (_, value)) = tuple((not(complete_tag("\r\n")), self.value()))(input)?;
406+
let (remaining, (_, value)) = (not(complete_tag("\r\n")), self.value()).parse(input)?;
407407

408408
let flags = value.flags | HeaderFlags::MISSING_COLON;
409409
Ok((
@@ -417,19 +417,19 @@ impl Parser {
417417
fn header_with_colon(&self) -> impl Fn(&[u8]) -> IResult<&[u8], Header> + '_ {
418418
move |input| {
419419
map(
420-
tuple((self.name(), self.separator(), self.value())),
420+
(self.name(), self.separator(), self.value()),
421421
|(mut name, flag, mut value)| {
422422
name.flags |= flag;
423423
value.flags |= flag;
424424
Header::new(name, value)
425425
},
426-
)(input)
426+
).parse(input)
427427
}
428428
}
429429

430430
/// Parses a header name and value with, or without a colon separator
431431
fn header(&self) -> impl Fn(&[u8]) -> IResult<&[u8], Header> + '_ {
432-
move |input| alt((complete(self.header_with_colon()), self.header_sans_colon()))(input)
432+
move |input| alt((complete(self.header_with_colon()), self.header_sans_colon())).parse(input)
433433
}
434434

435435
/// Parse multiple headers and indicate if end of headers or null was found
@@ -475,25 +475,25 @@ impl Parser {
475475
fn null(input: &[u8]) -> IResult<&[u8], ParsedBytes<'_>> {
476476
map(complete_tag("\0"), |null| {
477477
(null, HeaderFlags::NULL_TERMINATED)
478-
})(input)
478+
}).parse(input)
479479
}
480480

481481
/// Extracts folding lws (whitespace only)
482482
fn folding_lws(input: &[u8]) -> IResult<&[u8], ParsedBytes<'_>> {
483483
map(alt((tag(" "), tag("\t"), tag("\0"))), |fold| {
484484
(fold, HeaderFlags::FOLDING)
485-
})(input)
485+
}).parse(input)
486486
}
487487

488488
/// Parse a regular separator (colon followed by optional spaces) between header name and value
489489
fn separator_regular(input: &[u8]) -> IResult<&[u8], (&[u8], &[u8])> {
490-
tuple((complete_tag(":"), space0))(input)
490+
(complete_tag(":"), space0).parse(input)
491491
}
492492

493493
type leading_token_trailing<'a> = (&'a [u8], &'a [u8], &'a [u8]);
494494
/// Parse token characters with leading and trailing whitespace
495495
fn token_chars(input: &[u8]) -> IResult<&[u8], leading_token_trailing<'_>> {
496-
tuple((space0, take_while(is_token), space0))(input)
496+
(space0, take_while(is_token), space0).parse(input)
497497
}
498498

499499
#[cfg(test)]

0 commit comments

Comments
 (0)