Skip to content

Commit

Permalink
Added some performance improvements, improving public API, and updaing
Browse files Browse the repository at this point in the history
the README
  • Loading branch information
Albassel committed Oct 21, 2024
1 parent 8f207c2 commit a6b468e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 29 deletions.
17 changes: 11 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,25 @@ version = "0.1.0"
edition = "2021"
authors=["Basel"]
description="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"
keywords=["http", "parser", "http 1.1", "http/1.1"]
categories=["Compilers", "Parser implementations", "Parsing tools"]
keywords=["http", "parser"]
repository="https://github.com/Albassel/htpp"
license="MIT"
exclude = [
"benches/parse.rs",
"src/tests.rs",
".gitignore",
"README.md"
]
#documentation="URL of package docs"
#readme="path to README file"
#repository="URL to github repo"
#license="licenseName"
#license-file="pathToLicenseFile"


[[bench]]
harness = false
path = "benches/parse.rs"
name = "parse"

[dependencies]

[dev-dependencies]
criterion = "0.5.1"

20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
# Htpp

An **fast** and simple HTTP 1.1 parser written in Rust

# Contribution

Feel free to make a pull request if you think you can improve the code
A **fast** and simple HTTP 1.1 parser written in Rust

# Usage

To parse an HTTP request:

```rust
let req = b"GET /index.html HTTP/1.1\r\n";
let req = b"GET /index.html HTTP/1.1\r\n\r\n";
let parsed_req = htpp::RequestParser::new(req).parse().unwrap();
assert(parsed_req.method() == htpp::Method::Get);
assert(parsed_req.path() == "/index.html");
assert!(parsed_req.method() == htpp::Method::Get);
assert!(parsed_req.path() == "/index.html");
```

To parse an HTTP response:

```rust
let resp = b"HTTP/1.1 200 OK\r\n\r\n";
let parsed_resp = htpp::ResponseParser::new(resp).parse().unwrap();
assert(parsed_resp.status() == 200);
assert(parsed_resp.reason() == "OK");
assert!(parsed_resp.status() == 200);
assert!(parsed_resp.reason() == "OK");
```

# Contribution

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

3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ pub mod request;
/// Contains types and functions for parsing and generating http responses
pub mod response;

pub use request::{Method, Request, RequestParser};
pub use response::{Response, ResponseParser};


const SPACE: u8 = 32;
const CR: u8 = 13;
Expand Down
9 changes: 5 additions & 4 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl<'a> RequestParser<'a> {
#[inline]
// must only call if you know the buffer has >n characters
fn advance(&mut self, n: usize) {
self.bytes = &self.bytes[n..self.bytes.len()];
self.bytes = &self.bytes[n..];
}
#[inline]
fn find_substr(&self, substr: &'a [u8]) -> Option<usize> {
Expand Down Expand Up @@ -147,11 +147,12 @@ impl<'a> RequestParser<'a> {
fn parse_method(&mut self) -> Result<Method> {
if &self.bytes[0..4] == b"GET " {
self.advance(4);
Ok(Method::Get)
return Ok(Method::Get);
} else if &self.bytes[0..5] == b"POST " {
self.advance(5);
Ok(Method::Get)
} else {Err(Error::Malformed)}
return Ok(Method::Get);
}
Err(Error::Malformed)
}
#[inline]
// parses the path and removes the space after making sure it only contains URL safe characters
Expand Down
18 changes: 9 additions & 9 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl<'a> ResponseParser<'a> {
#[inline]
// must only call if you know the buffer has >n characters
fn advance(&mut self, n: usize) {
self.bytes = &self.bytes[n..self.bytes.len()];
self.bytes = &self.bytes[n..];
}
#[inline]
fn find_substr(&self, substr: &'a [u8]) -> Option<usize> {
Expand All @@ -118,6 +118,7 @@ impl<'a> ResponseParser<'a> {
};
let (res, body) = self.bytes.split_at(idx+2);
self.bytes = res;
if res.len() < 12 {return Err(Error::Malformed);}
self.parse_http_version()?;
let (status, reason) = self.parse_status()?;
let headers = self.parse_headers()?;
Expand All @@ -131,15 +132,14 @@ impl<'a> ResponseParser<'a> {
#[inline]
//removes the space after
fn parse_http_version(&mut self) -> Result<HttpVer> {
match self.bytes.get(0..9) {
Some(b"HTTP/1.1 ") => {
self.advance(9);
Ok(HttpVer::One)
}, Some(b"HTTP/2.0 ") => {
self.advance(9);
Ok(HttpVer::Two)
}, _ => {Err(Error::Malformed)}
if &self.bytes[0..9] == b"HTTP/1.1 " {
self.advance(9);
return Ok(HttpVer::One);
} else if &self.bytes[0..9] == b"HTTP/2.0 " {
self.advance(9);
return Ok(HttpVer::Two);
}
Err(Error::Malformed)
}
#[inline]
//parses the method and removes white space after it
Expand Down

0 comments on commit a6b468e

Please sign in to comment.