#http-parser #http-header #parser #http-request #request-headers #secure #response-headers

htpp

A simple, fast, and secure http parser. It strictly adheres to the http specification. It can also parse URLs including query parameters

12 releases

0.3.5 Jan 1, 2025
0.3.4 Dec 18, 2024
0.2.2 Oct 23, 2024
0.1.2 Oct 21, 2024

#1569 in Network programming

Download history 479/week @ 2024-10-16 185/week @ 2024-10-23 4/week @ 2024-10-30 3/week @ 2024-11-06 4/week @ 2024-12-04 285/week @ 2024-12-11 350/week @ 2024-12-18 145/week @ 2025-01-01

784 downloads per month

MIT license

29KB
551 lines

Htpp

A fast and simple HTTP 1.1 parser written in Rust

Usage

You can parse a request as follows:

use htpp::{Request, EMPTY_HEADER};

let req = b"GET /index.html HTTP/1.1\r\n\r\n";
let mut headers = [EMPTY_HEADER; 10];
let parsed = Request::parse(req, &mut headers).unwrap();
assert!(parsed.method == htpp::Method::Get);
assert!(parsed.path == "/index.html");

You can create a request as follows:

use htpp::{Method, Request, Header};

let method = Method::Get;
let path = "/index.html";
let mut headers = [Header::new("Accept", b"*/*")];
let req = Request::new(method, path, &headers, b"");

Working with [Response]

You can parse a response as follows:

use htpp::{Response, EMPTY_HEADER};

let req = b"HTTP/1.1 200 OK\r\n\r\n";
let mut headers = [EMPTY_HEADER; 10];
let parsed = Response::parse(req, &mut headers).unwrap();
assert!(parsed.status == 200);
assert!(parsed.reason == "OK");

You can create a response as follows:

use htpp::{Response, Header};

let status = 200;
let reason = "OK";
let mut headers = [Header::new("Connection", b"keep-alive")];
let req = Response::new(status, reason, &mut headers, b"");

After parsing a request, you can also parse the path part of the request inclusing query parameters as follows:

use htpp::{Request, EMPTY_QUERY, Url, EMPTY_HEADER};

let req = b"GET /index.html?query1=value&query2=value HTTP/1.1\r\n\r\n";
let mut headers = [EMPTY_HEADER; 10];
let parsed_req = Request::parse(req, &mut headers).unwrap();
let mut queries_buf = [EMPTY_QUERY; 10];
let url = Url::parse(parsed_req.path.as_bytes(), &mut queries_buf).unwrap();
assert!(url.path == "/index.html");
assert!(url.query_params.unwrap()[0].name == "query1");
assert!(url.query_params.unwrap()[0].val == "value");

Contribution

Feel free to make a pull request if you think you can improve the code

No runtime deps