From 1b5fbe87751ab924e133f1d885eb1ef96e8a3ff7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Mon, 1 Aug 2022 14:31:53 +0200 Subject: [PATCH] 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. --- aquatic_http_protocol/src/request.rs | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/aquatic_http_protocol/src/request.rs b/aquatic_http_protocol/src/request.rs index 4abb318..61b1dd2 100644 --- a/aquatic_http_protocol/src/request.rs +++ b/aquatic_http_protocol/src/request.rs @@ -262,25 +262,17 @@ impl Request { let mut headers = [httparse::EMPTY_HEADER; 16]; 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(_)) => { if let Some(path) = http_request.path { - path + Self::from_http_get_path(path).map_err(RequestParseError::Invalid) } else { - return Err(RequestParseError::Invalid(anyhow::anyhow!("no http path"))); + Err(RequestParseError::Invalid(anyhow::anyhow!("no http path"))) } } - Ok(httparse::Status::Partial) => { - if let Some(path) = http_request.path { - 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) + Ok(httparse::Status::Partial) => Err(RequestParseError::NeedMoreData), + Err(err) => Err(RequestParseError::Invalid(anyhow::Error::from(err))), + } } /// Parse Request from http path (GET `/announce?info_hash=...`)