Skip to main content

parse_response_head

Function parse_response_head 

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

Parses one response head from the start of input with the rules of ClientConn, framed by what the request asked (a HEAD request’s response has no body; a 2xx to CONNECT is a tunnel). Interim (1xx) heads are returned like any other; check ResponseHead::is_informational and parse the next head after len. Allocates nothing.

§Errors

Every response parse error ClientConn::poll can return for a head (UnexpectedEof aside), and HttpError::TooManyHeaders when the head has more fields than fields holds.

§Examples

use http_net::{Asked, BodyFraming, FieldSpan, Limits, Parsed, parse_response_head};

let input = b"HTTP/1.1 200 OK\r\nContent-Length: 100\r\n\r\n";
let mut fields = [FieldSpan::default(); 8];
let asked = Asked { head: true, connect: false };
let Parsed::Complete { head, .. } = parse_response_head(input, &Limits::new(), asked, &mut fields)? else {
    panic!("incomplete");
};
// A response to HEAD has no body, whatever its fields say.
assert_eq!(head.view().framing(), BodyFraming::None);
assert_eq!(head.view().content_length(), Some(100));