Skip to content

Commit

Permalink
Merge pull request #129 from kpcyrd/http
Browse files Browse the repository at this point in the history
Improve http parser
  • Loading branch information
kpcyrd committed Jan 7, 2024
2 parents 42b9667 + 7c967b3 commit 010b419
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 285 deletions.
105 changes: 56 additions & 49 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ sha2 = "0.10"
dirs-next = "2.0"
libc = "0.2"
uzers = "0.11.3"
bstr = "1"
bstr = { version = "1", features = ["serde"] }
data-encoding = "2.5.0"
clap = { version = "4.4.11", features = ["derive"] }
clap_complete = "4.4.4"
httparse = "1.8.0"

[target.'cfg(target_os="linux")'.dependencies]
syscallz = "0.17"
Expand Down
42 changes: 34 additions & 8 deletions src/centrifuge/http.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,40 @@
use crate::nom_http;

use bstr::BString;
use crate::structs::CentrifugeError;
use crate::structs::http::Request;
use crate::structs::http::{Http, Request, Response};
use httparse::Status;
use std::convert::TryFrom;

pub fn extract(remaining: &[u8]) -> Result<Http, CentrifugeError> {
let mut req_headers = [httparse::EMPTY_HEADER; 256];
let mut resp_headers = [httparse::EMPTY_HEADER; 256];

let mut req = httparse::Request::new(&mut req_headers);
let mut resp = httparse::Response::new(&mut resp_headers);

if let Ok(status) = req.parse(remaining) {
let remaining = match status {
Status::Complete(n) => &remaining[n..],
Status::Partial => &[],
};

pub fn extract(remaining: &[u8]) -> Result<Request, CentrifugeError> {
if let Ok((_remaining, (request, headers))) = nom_http::request(remaining) {
match Request::from_nom(&request, headers) {
Ok(http) => Ok(http),
Err(_) => Err(CentrifugeError::ParsingError),
let mut req = Request::try_from(req)?;
if !remaining.is_empty() {
req.body = Some(BString::from(remaining))
}

Ok(Http::Request(req))
} else if let Ok(status) = resp.parse(remaining) {
let remaining = match status {
Status::Complete(n) => &remaining[n..],
Status::Partial => &[],
};

let mut resp = Response::try_from(resp)?;
if !remaining.is_empty() {
resp.body = Some(BString::from(remaining))
}

Ok(Http::Response(resp))
} else {
Err(CentrifugeError::WrongProtocol)
}
Expand Down
Loading

0 comments on commit 010b419

Please sign in to comment.