aquatic_http: move request parsing into protocol crate

This commit is contained in:
Joakim Frostegård 2020-07-20 20:13:56 +02:00
parent 2521c89109
commit 95e25710dc
6 changed files with 43 additions and 30 deletions

View file

@ -23,6 +23,7 @@ anyhow = "1"
bendy = { version = "0.3", features = ["std", "serde"] }
hashbrown = { version = "0.7", features = ["serde"] }
hex = { version = "0.4", default-features = false }
httparse = "1"
itoa = "0.4"
log = "0.4"
memchr = "2"

View file

@ -109,6 +109,12 @@ impl ScrapeRequest {
}
pub enum RequestParseError {
NeedMoreData,
Invalid(anyhow::Error),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Request {
Announce(AnnounceRequest),
@ -117,6 +123,30 @@ pub enum Request {
impl Request {
/// Parse Request from HTTP request bytes
pub fn from_bytes(bytes: &[u8]) -> Result<Self, RequestParseError> {
let mut headers = [httparse::EMPTY_HEADER; 16];
let mut http_request = httparse::Request::new(&mut headers);
match http_request.parse(bytes){
Ok(httparse::Status::Complete(_)) => {
if let Some(path) = http_request.path {
let res_request = Self::from_http_get_path(path);
res_request.map_err(RequestParseError::Invalid)
} else {
Err(RequestParseError::Invalid(anyhow::anyhow!("no http path")))
}
},
Ok(httparse::Status::Partial) => {
Err(RequestParseError::NeedMoreData)
},
Err(err) => {
Err(RequestParseError::Invalid(anyhow::anyhow!("httparse: {:?}", err)))
}
}
}
/// Parse Request from http path (GET `/announce?info_hash=...`)
///
/// Existing serde-url decode crates were insufficient, so the decision was