Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing custom configuration to parse_headers #126

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
29 changes: 28 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -891,11 +891,38 @@ fn parse_code(bytes: &mut Bytes<'_>) -> Result<u16> {
/// ][..]))));
/// ```
pub fn parse_headers<'b: 'h, 'h>(
src: &'b [u8],
dst: &'h mut [Header<'b>],
) -> Result<(usize, &'h [Header<'b>])> {
parse_headers_with_config(src, dst, &ParserConfig::default())
}

/// Parse a buffer of bytes as headers with a specified configuration.
///
/// The return value, if complete and successful, includes the index of the
/// buffer that parsing stopped at, and a sliced reference to the parsed
/// headers. The length of the slice will be equal to the number of properly
/// parsed headers.
///
/// # Example
///
/// ```
/// let buf = b"Host: foo.bar\nAccept: */*\n\nblah blah";
/// let mut headers = [httparse::EMPTY_HEADER; 4];
/// assert_eq!(httparse::parse_headers_with_config(buf, &mut headers, &httparse::ParserConfig::default()),
/// Ok(httparse::Status::Complete((27, &[
/// httparse::Header { name: "Host", value: b"foo.bar" },
/// httparse::Header { name: "Accept", value: b"*/*" }
/// ][..]))));
/// ```
#[inline]
pub fn parse_headers_with_config<'b: 'h, 'h>(
src: &'b [u8],
mut dst: &'h mut [Header<'b>],
config: &ParserConfig,
) -> Result<(usize, &'h [Header<'b>])> {
let mut iter = Bytes::new(src);
let pos = complete!(parse_headers_iter(&mut dst, &mut iter, &ParserConfig::default()));
let pos = complete!(parse_headers_iter(&mut dst, &mut iter, config));
Ok(Status::Complete((pos, dst)))
}

Expand Down