Skip to main content

parse_request_head

Function parse_request_head 

Source
pub fn parse_request_head<'a>(
    input: &'a [u8],
    limits: &Limits,
    fields: &'a mut [FieldSpan],
) -> Result<Parsed<ParsedRequest<'a>>, HttpError>
Expand description

Parses one request head from the start of input, with the rules and budgets of ServerConn (the same scanner and interpretation code), writing the field records into fields. Allocates nothing. Unlike ServerConn, it does not check for the HTTP/2 connection preface first; call is_h2_preface for that.

It is stateless: given more input after Partial, it parses again from the start. A caller that receives a head in many small pieces and needs linear total work should use ServerConn, which resumes where it stopped. A head with more fields than fields holds is HttpError::TooManyHeaders (as is one over Limits::max_headers).

§Errors

Every request parse error ServerConn::poll can return for a head, except UnexpectedEof (running out of input is Partial here) and Http2Preface (the preface is reported as HttpError::UnsupportedVersion; call is_h2_preface first to tell them apart).

§Examples

use http_net::{FieldSpan, Limits, Parsed, parse_request_head};

let input = b"POST /notes HTTP/1.1\r\nHost: a\r\nContent-Length: 5\r\n\r\nhello";
let mut fields = [FieldSpan::default(); 32];
let Parsed::Complete { head, len } = parse_request_head(input, &Limits::new(), &mut fields)? else {
    panic!("incomplete");
};
let req = head.view();
assert_eq!(req.path(), "/notes");
assert_eq!(req.content_length(), Some(5));
assert_eq!(&input[len..], b"hello");