Learn about httparse in Rust using simple example.

What is httparse?

A push parser for the HTTP 1.x protocol in Rust.

Here are it’s main features:

  1. Avoids allocations.
  2. No copy.
  3. Fast.
  4. Works with no_std, simply disable the std Cargo feature.

Example

Here is a simple usage example:

let mut headers = [httparse::EMPTY_HEADER; 64];
let mut req = httparse::Request::new(&mut headers);

let buf = b"GET /index.html HTTP/1.1\r\nHost";
assert!(req.parse(buf)?.is_partial());

// a partial request, so we try again once we have more data

let buf = b"GET /index.html HTTP/1.1\r\nHost: example.domain\r\n\r\n";
assert!(req.parse(buf)?.is_complete());

Reference

Read more here.

Categorized in: