Crate htpp

Source
Expand description

§htpp

A library for parsing HTTP requests and responses. The focus is on speed and safety. It is intentionally strict to minimize HTTP attacks. It can also parse URLs

§Working with Request

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");

Structs§

Enums§

  • All parsing errors possible
  • Possible http versions
  • The http method of a request. Only GET, POST, and PUT are supported
  • All errors that could result from parsing a URL

Constants§

  • An empty header to make it easier to construct a header buffer to parse headers into
  • An ampty query parameter for ease of parsing

Type Aliases§

  • A result holding a parse error