#http-parser #http-request #http-response #parser #secure #prevent #attacks

htpp

A simple, fast, and secure http parser. It adheres strictly to the http specification, which is intentional as to be efficient and prevent possible http related attacks. It only supports http 1.1 but support for http 2.0 is planned

6 releases

0.2.2 Oct 23, 2024
0.2.1 Oct 22, 2024
0.1.2 Oct 21, 2024

#5 in #prevent

Download history 143/week @ 2024-10-13 506/week @ 2024-10-20 18/week @ 2024-10-27 4/week @ 2024-11-03

536 downloads per month

MIT license

21KB
412 lines

Htpp

A fast and simple HTTP 1.1 parser written in Rust

Usage

To parse an HTTP request:

let req = b"GET /index.html HTTP/1.1\r\nAccept: */*\r\n\r\n";
let parsed = htpp::Request::parse(req).unwrap();
assert!(parsed.method() == htpp::Method::Get);
assert!(parsed.path() == "/index.html");
assert!(parsed.headers().len() == 1);
assert!(parsed.headers()[0].name == "Accept");
assert!(parsed.headers()[0].val == b"*/*");

To parse an HTTP response:

let res = b"HTTP/1.1 200 OK\r\nAccept: */*\r\n\r\n";
let parsed = htpp::Response::parse(res).unwrap();
assert!(parsed.status() == 200);
assert!(parsed.reason() == "OK");
assert!(parsed.headers().len() == 1);
assert!(parsed.headers()[0].name == "Accept");
assert!(parsed.headers()[0].val == b"*/*");

Contribution

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


lib.rs:

htpp

A library for parsing HTTP requests and responses. The focus is on speed and safety. It is intentionally strict to prevent possible HTTP attacks

Working with [Request]

You can parse a request as follows:

use htpp::Request;

let req = b"GET /index.html HTTP/1.1\r\n\r\n";
let parsed = Request::parse(req).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 headers = vec![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;

let req = b"HTTP/1.1 200 OK\r\n\r\n";
let parsed = Response::parse(req).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 headers = vec![Header::new("Connection", b"keep-alive")];
let req = Response::new(status, reason, headers, b"");

No runtime deps