http protocol: return NeedMoreData until headers are fully parsed

This prevents an issue in aquatic_http where it could
theoretically start sending back data before initial
request was fully received.
This commit is contained in:
Joakim Frostegård 2022-08-01 14:31:53 +02:00
parent fcf18c845f
commit 1b5fbe8775

View file

@ -262,25 +262,17 @@ impl Request {
let mut headers = [httparse::EMPTY_HEADER; 16]; let mut headers = [httparse::EMPTY_HEADER; 16];
let mut http_request = httparse::Request::new(&mut headers); let mut http_request = httparse::Request::new(&mut headers);
let path = match http_request.parse(bytes) { match http_request.parse(bytes) {
Ok(httparse::Status::Complete(_)) => { Ok(httparse::Status::Complete(_)) => {
if let Some(path) = http_request.path { if let Some(path) = http_request.path {
path Self::from_http_get_path(path).map_err(RequestParseError::Invalid)
} else { } else {
return Err(RequestParseError::Invalid(anyhow::anyhow!("no http path"))); Err(RequestParseError::Invalid(anyhow::anyhow!("no http path")))
} }
} }
Ok(httparse::Status::Partial) => { Ok(httparse::Status::Partial) => Err(RequestParseError::NeedMoreData),
if let Some(path) = http_request.path { Err(err) => Err(RequestParseError::Invalid(anyhow::Error::from(err))),
path }
} else {
return Err(RequestParseError::NeedMoreData);
}
}
Err(err) => return Err(RequestParseError::Invalid(anyhow::Error::from(err))),
};
Self::from_http_get_path(path).map_err(RequestParseError::Invalid)
} }
/// Parse Request from http path (GET `/announce?info_hash=...`) /// Parse Request from http path (GET `/announce?info_hash=...`)