Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changes

## [2.4.0] - 2026-11-12

* Add `Request::path_ascii`, set when the request target is pure ASCII, so
callers can skip UTF-8 validation of the target

* Add `Request::query`, the position of the first `?` of the request target,
found by the same SIMD scan as the target

## [2.3.0] - 2026-10-20

* Use constant-stride AVX2 loops for request targets, header names and values
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ntex-httparse"
version = "2.3.0"
version = "2.4.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>", "Sean McArthur <sean@seanmonstar.com>"]
license = "MIT OR Apache-2.0"
description = "A tiny, safe, speedy, zero-copy HTTP/1.x parser."
Expand Down
174 changes: 173 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,12 @@ pub struct Request {
/// Request target. May contain bytes `0x80..=0xFF`, so it is not
/// guaranteed to be UTF-8.
pub path: SlicePos,
/// `true` if the request target contains only ASCII bytes, so it is valid
/// UTF-8 and needs no further UTF-8 check.
pub path_ascii: bool,
/// Position of the first `?` in the request target, an index into the
/// parsed buffer like `path`, `None` if the target has no query.
pub query: Option<usize>,
/// HTTP version, `0` for HTTP/1.0 and `1` for HTTP/1.1.
pub version: u8,
}
Expand Down Expand Up @@ -310,10 +316,16 @@ impl Request {
}
if bytes.st.state == SPACES_BEFORE_URI {
complete!(utils::skip_spaces(&mut bytes));
self.path_ascii = true;
self.query = None;
bytes.st.state = URI;
}
if bytes.st.state == URI {
self.path = complete!(parse_uri_inner(&mut bytes));
self.path = complete!(parse_target_inner(
&mut bytes,
&mut self.query,
&mut self.path_ascii
));
bytes.st.state = SPACES_BEFORE_VERSION;
}
if bytes.st.state == SPACES_BEFORE_VERSION {
Expand Down Expand Up @@ -642,6 +654,61 @@ fn parse_uri_inner(bytes: &mut Bytes<'_, '_>) -> Result<SlicePos> {
}
}

/// Parses a request target like [`parse_uri_inner`], and records whether it
/// is ASCII and where its query starts.
///
/// `ascii` and `query` must be `true` and `None` when the target starts, and
/// are kept between `Partial` results, so a resumed scan does not start over.
#[inline]
fn parse_target_inner(
bytes: &mut Bytes<'_, '_>,
query: &mut Option<usize>,
ascii: &mut bool,
) -> Result<SlicePos> {
let start = bytes.start();
// the ASCII scans stop at the first `?` and the first high byte, a
// non-ASCII target is finished with the plain scan
loop {
if !*ascii {
simd::match_uri_vectored(bytes);
break;
}
if query.is_none() {
simd::match_uri_ascii_vectored::<true>(bytes);
} else {
simd::match_uri_ascii_vectored::<false>(bytes);
}
match bytes.peek() {
Some(b'?') if query.is_none() => {
*query = Some(bytes.cursor());
bytes.advance(1);
}
Some(b) if b >= 0x80 => *ascii = false,
_ => break,
}
}
let end = bytes.cursor();

if next!(bytes) == b' ' {
// URI must have at least one char
if start == end {
return Err(Error::Token);
}
// the plain scan does not stop at `?`
if !*ascii && query.is_none() {
*query = bytes
.since(start)
.iter()
.position(|&b| b == b'?')
.map(|i| start + i);
}
bytes.commit();
Ok(Status::Complete(SlicePos { start, end }))
} else {
Err(Error::Token)
}
}

#[inline]
fn parse_code(bytes: &mut Bytes<'_, '_>) -> Result<u16> {
let hundreds = expect!(bytes.next() == b'0'..=b'9' => Err(Error::Status));
Expand Down Expand Up @@ -2329,6 +2396,111 @@ mod tests {
assert!(str::from_utf8(&BUF[req.path.start..req.path.end]).is_err());
}

/// STATEMENT: `Request::path_ascii` reports whether the request target is
/// pure ASCII, including targets completed across `Partial` results.
///
/// HOW:
/// - Parse ASCII targets shorter and longer than a SIMD block and check the
/// flag is set.
/// - Parse targets with a high byte at the start, middle and end, and check
/// the flag is cleared even when the request reuses a previous `Request`.
/// - Feed a non-ASCII target in two pieces through `parse_with_state` and
/// check the flag once the line completes.
#[test]
fn test_path_ascii() {
let long = "/a".repeat(40);
let mut req = Request::default();
for path in ["/", "/baseline11?a=13&b=42", long.as_str()] {
let buf = format!("GET {path} HTTP/1.1\r\n");
assert!(req.parse(buf.as_bytes()).unwrap().is_complete());
assert!(req.path_ascii, "{path}");
}

let long = [b"/".repeat(40), b"\xC3\xA9".to_vec(), b"/".repeat(40)].concat();
for path in [&b"/\xC3\xA9"[..], b"\xFF/a?b", b"/a?b=\x80", &long] {
req.path_ascii = true;
let buf = [b"GET ", path, b" HTTP/1.1\r\n"].concat();
assert!(req.parse(&buf).unwrap().is_complete());
assert!(!req.path_ascii, "{path:?}");
}

const BUF: &[u8] = b"GET /caf\xC3\xA9?q=1 HTTP/1.1\r\n";
let mut req = Request {
path_ascii: true,
..Request::default()
};
let mut st = State::default();
assert_eq!(
req.parse_with_state(&BUF[..9], &mut st),
Ok(Status::Partial)
);
assert!(req.parse_with_state(BUF, &mut st).unwrap().is_complete());
assert!(!req.path_ascii);
assert_eq!(&BUF[req.path.start..req.path.end], b"/caf\xC3\xA9?q=1");
}

/// STATEMENT: `Request::query` is the position of the first `?` of the
/// request target, for ASCII and non-ASCII targets, whole or resumed.
///
/// HOW:
/// - Parse targets with no `?`, one or two `?` at offsets around the SIMD
/// block sizes, and high bytes before or after the `?`, reusing one
/// `Request`, and compare `query` and `path_ascii` with a scalar search.
/// - Feed each request through `parse_with_state` one prefix length at a
/// time and check the completed result matches the one-shot parse.
#[test]
fn test_request_query() {
let mut targets: Vec<Vec<u8>> = vec![
b"/".to_vec(),
b"?".to_vec(),
b"/a?".to_vec(),
b"/a?b?c".to_vec(),
b"/\xC3\xA9?q=1".to_vec(),
b"/q?\xC3\xA9".to_vec(),
b"/\xC3\xA9/no-query".to_vec(),
b"/baseline11?a=13&b=42".to_vec(),
];
for pos in [1, 7, 8, 15, 16, 17, 31, 32, 33, 63, 64, 65] {
let mut t = vec![b'/'; 70];
t[pos] = b'?';
targets.push(t.clone());
t[pos + 3] = b'?';
targets.push(t.clone());
t[pos / 2] = 0x80;
targets.push(t);
}

let mut req = Request::default();
for target in &targets {
let buf = [b"GET ", &target[..], b" HTTP/1.1\r\n"].concat();
let query = target.iter().position(|&b| b == b'?').map(|i| i + 4);
let ascii = target.is_ascii();

assert!(req.parse(&buf).unwrap().is_complete());
assert_eq!(&buf[req.path.start..req.path.end], &target[..]);
assert_eq!(req.query, query, "{target:?}");
assert_eq!(req.path_ascii, ascii, "{target:?}");

let mut resumed = Request::default();
let mut st = State::default();
for n in 1..buf.len() {
assert_eq!(
resumed.parse_with_state(&buf[..n], &mut st),
Ok(Status::Partial)
);
}
assert!(
resumed
.parse_with_state(&buf, &mut st)
.unwrap()
.is_complete()
);
assert_eq!(resumed.path, req.path);
assert_eq!(resumed.query, query, "resumed {target:?}");
assert_eq!(resumed.path_ascii, ascii, "resumed {target:?}");
}
}

#[test]
fn test_parse_uri_delimiter() {
for input in [&b"/path "[..], b"/path ", b"/path HTTP/1.1\r\n"] {
Expand Down
33 changes: 33 additions & 0 deletions src/simd/avx2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,39 @@ pub unsafe fn match_uri_vectored(bytes: &mut Bytes) {
super::swar::match_uri_vectored(bytes);
}

/// Like [`match_uri_vectored`], but also stops at bytes `0x80..=0xFF`, and
/// at `?` if `Q` is set.
#[inline]
#[target_feature(enable = "avx2")]
pub unsafe fn match_uri_ascii_vectored<const Q: bool>(bytes: &mut Bytes) {
#[cfg(target_arch = "x86")]
use core::arch::x86::*;
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64::*;

while bytes.as_ref().len() >= 32 {
// %x21-%x7e, signed comparison rejects %x80-%xff
let dat = _mm256_lddqu_si256(bytes.as_ref().as_ptr() as *const _);
let low = _mm256_cmpgt_epi8(dat, _mm256_set1_epi8(0x20));
let mut stop = _mm256_cmpeq_epi8(dat, _mm256_set1_epi8(0x7f));
if Q {
stop = _mm256_or_si256(
stop,
_mm256_cmpeq_epi8(dat, _mm256_set1_epi8(b'?'.cast_signed())),
);
}
let res = _mm256_movemask_epi8(_mm256_andnot_si256(stop, low)) as u32;
let advance = res.trailing_ones() as usize;

if advance != 32 {
bytes.advance(advance);
return;
}
bytes.advance(32);
}
super::swar::match_uri_ascii_vectored::<Q>(bytes);
}

#[inline(always)]
#[allow(non_snake_case, overflowing_literals)]
#[allow(unused)]
Expand Down
100 changes: 100 additions & 0 deletions src/simd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ mod sse42_compile_time {
unsafe { super::sse42::match_uri_vectored(b) }
}

#[inline(always)]
pub fn match_uri_ascii_vectored<const Q: bool>(b: &mut crate::iter::Bytes<'_, '_>) {
unsafe { super::sse42::match_uri_ascii_vectored::<Q>(b) }
}

#[inline(always)]
pub fn match_header_value_vectored(b: &mut crate::iter::Bytes<'_, '_>) {
unsafe { super::sse42::match_header_value_vectored(b) }
Expand Down Expand Up @@ -111,6 +116,11 @@ mod avx2_compile_time {
unsafe { super::avx2::match_uri_vectored(b) }
}

#[inline(always)]
pub fn match_uri_ascii_vectored<const Q: bool>(b: &mut crate::iter::Bytes<'_, '_>) {
unsafe { super::avx2::match_uri_ascii_vectored::<Q>(b) }
}

#[inline(always)]
pub fn match_header_value_vectored(b: &mut crate::iter::Bytes<'_, '_>) {
unsafe { super::avx2::match_header_value_vectored(b) }
Expand All @@ -137,3 +147,93 @@ mod neon;
target_feature = "neon",
))]
pub use self::neon::*;

#[cfg(test)]
mod tests {
use crate::iter::Bytes;

fn stops_at(b: u8, q: bool) -> bool {
!(0x21..=0x7e).contains(&b) || (q && b == b'?')
}

/// Runs `f` over `_` filled buffers with `byte` at every position and
/// checks it stops exactly at that byte, or consumes everything.
fn check<const Q: bool>(name: &str, f: &dyn Fn(&mut Bytes<'_, '_>)) {
for len in [1, 7, 8, 9, 15, 16, 17, 31, 32, 33, 40, 70] {
for pos in 0..len {
for byte in 0..=255u8 {
let mut buf = vec![b'_'; len];
buf[pos] = byte;
let mut st = crate::State::default();
let mut bytes = Bytes::new(&buf, &mut st);
f(&mut bytes);
let expected = if stops_at(byte, Q) { pos } else { len };
assert_eq!(
bytes.cursor(),
expected,
"{name}::<{Q}> byte {byte:#04x} at {pos} of {len}"
);
}
}
}
}

/// STATEMENT: Every `match_uri_ascii_vectored` backend accepts exactly
/// `0x21..=0x7E`, and also stops at `?` when `Q` is set.
///
/// HOW:
/// - For the SWAR backend, the selected backend, and the SSE4.2 and AVX2
/// backends when compiled in and supported, scan buffers of lengths
/// around the 8, 16 and 32 byte block sizes.
/// - Put each byte value at each position and check the cursor stops at
/// it when it is outside the set, and reaches the end otherwise.
#[test]
fn match_uri_ascii_vectored_backends() {
check::<true>("swar", &|b| {
super::swar::match_uri_ascii_vectored::<true>(b);
});
check::<false>("swar", &|b| {
super::swar::match_uri_ascii_vectored::<false>(b);
});
check::<true>("selected", &|b| super::match_uri_ascii_vectored::<true>(b));
check::<false>("selected", &|b| super::match_uri_ascii_vectored::<false>(b));

#[cfg(all(
not(any(httparse_disable_simd, miri)),
any(feature = "std", target_feature = "sse4.2"),
not(target_feature = "avx2"),
any(target_arch = "x86", target_arch = "x86_64"),
))]
if std::is_x86_feature_detected!("sse4.2") {
#[allow(clippy::undocumented_unsafe_blocks)]
unsafe {
check::<true>("sse42", &|b| {
super::sse42::match_uri_ascii_vectored::<true>(b);
});
check::<false>("sse42", &|b| {
super::sse42::match_uri_ascii_vectored::<false>(b);
});
}
}

#[cfg(all(
not(any(httparse_disable_simd, miri)),
any(
target_feature = "avx2",
all(feature = "std", not(target_feature = "sse4.2"))
),
any(target_arch = "x86", target_arch = "x86_64"),
))]
if std::is_x86_feature_detected!("avx2") {
#[allow(clippy::undocumented_unsafe_blocks)]
unsafe {
check::<true>("avx2", &|b| {
super::avx2::match_uri_ascii_vectored::<true>(b);
});
check::<false>("avx2", &|b| {
super::avx2::match_uri_ascii_vectored::<false>(b);
});
}
}
}
}
Loading