HeaderName from bytes unchecked support
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 45/100
- Issue type
- Feature
- Clarity
- Mostly clear
- Activity status
- Quiet
- Tech stack
- rust
- Domain
- api, backend-api-design
Research direction
Start by locating the existing HeaderName byte-conversion and parsing APIs, then compare them with the proposed from_bytes_unchecked and parse_hdr_unchecked signatures. Review the safety and lifetime requirements before deciding how the unchecked path should integrate. Done means the API supports validated header-name bytes without redundant validation while preserving the stated invalid-input guarantees.
Written by the indexing model from the issue text.
Description
Hello,
I would like to request support for a function that accepts pre-validated HTTP header name tokens. This would allow parsers to skip redundant validation when bytes have already been validated upstream.
Proposed API
/// Converts a slice of bytes to an HTTP header name.
///
/// # Safety
///
/// The caller must ensure that `src` contains only valid HTTP header name tokens.
/// This allows the parser to skip validation and normalization for improved performance.
/// Passing invalid bytes will result in undefined behavior.
pub unsafe fn from_bytes_unchecked(src: &[u8]) -> Result<HeaderName, InvalidHeaderName> {
let mut buf = uninit_u8_array();
// SAFETY: see `from_bytes_unchecked` guarantees
parse_hdr_unchecked(src, &mut buf)?
}
/// # Safety
///
/// The caller must ensure that `data` contains only valid HTTP header name tokens.
unsafe fn parse_hdr_unchecked<'a>(
data: &'a [u8],
b: &'a mut [MaybeUninit<u8>; SCRATCH_BUF_SIZE]
) -> Result<HeaderName, InvalidHeaderName> {
match data.len() {
0 => Err(InvalidHeaderName::new()),
len @ 1..=SCRATCH_BUF_SIZE => {
// Read from data into the buffer
data.iter()
.zip(b.iter_mut())
.for_each(|(byte, out)| *out = MaybeUninit::new(*byte as usize));
// SAFETY: len bytes of b were just initialized.
let name: &'a [u8] = unsafe { slice_assume_init(&b[0..len]) };
match StandardHeader::from_bytes(name) {
Some(sh) => Ok(sh.into()),
None => {
let buf = Bytes::copy_from_slice(name);
// SAFETY: see `parse_hdr_unchecked` guarantees
let val = unsafe { ByteStr::from_utf8_unchecked(buf) };
Ok(Custom(val).into())
}
}
},
SCRATCH_BUF_OVERFLOW..=super::MAX_HEADER_NAME_LEN => {
use bytes::{BufMut};
let mut dst = BytesMut::with_capacity(data.len());
dst.extend_from_slice(data);
// SAFETY: see `parse_hdr_unchecked` guarantees
let val = unsafe { ByteStr::from_utf8_unchecked(dst.freeze()) };
Ok(Custom(val).into())
},
_ => Err(InvalidHeaderName::new()),
}
}
Motivation
Many HTTP parsers already perform validation of header name tokens during parsing. This function would allow those parsers to avoid duplicate validation by reusing the existing validation results. Additionally, the unchecked parser could return HeaderName directly, since the invariant of valid header tokens would be guaranteed by the caller.
- Dominant language
- Rust
- Stars
- 1.4k
- Forks
- 378
- Avg merge
- 1d 21h
- Merged PRs (30d)
- 5
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from hyperium/http
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
Difficulty 3/5 1-2 days Newbie friendliness 62/100
-
Difficulty 3/5 1-2 days Newbie friendliness 58/100
-
Difficulty 3/5 1-2 days Newbie friendliness 62/100
-
Difficulty 5/5 Over a week Newbie friendliness 30/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Eynzof/Hermes-CN-Desktop#610 ·
-
bug team:backend track:services-maintenance
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
cowprotocol/services#4950 ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
gitbutlerapp/gitbutler#15998 · 1 comment ·
-
bug triage:deciding
Difficulty 1/5 Under an hour Newbie friendliness 88/100
open-telemetry/otel-arrow#4132 ·