refactor(protocol): centralize header parsing and validation
This commit is contained in:
+8
-13
@@ -9,7 +9,7 @@ use tokio::{
|
||||
};
|
||||
|
||||
use crate::protocol::{
|
||||
parse_header,
|
||||
decode_header,
|
||||
Header,
|
||||
HEADER_SIZE,
|
||||
MAX_BODY_SIZE,
|
||||
@@ -87,24 +87,19 @@ impl Connection {
|
||||
if bytes_read == 0 {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
// Читаем остальную часть буфера от 1 до конца
|
||||
self.stream.read_exact(&mut buffer[1..]).await?;
|
||||
|
||||
self.stream.read_exact(&mut buffer[1..]).await?;
|
||||
|
||||
let (remaining, header) =
|
||||
parse_header(&buffer).map_err(|error| {
|
||||
//Парсим скачанный заголовок через nocloud_core::protocol parse_header
|
||||
let header =
|
||||
decode_header(&buffer).map_err(|error| {
|
||||
io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
format!("Ошибка разбора заголовка: {error:?}"),
|
||||
)
|
||||
})?;
|
||||
|
||||
// Мы дали парсеру ровно HEADER_SIZE байт.
|
||||
debug_assert!(remaining.is_empty());
|
||||
|
||||
header.validate().map_err(|error| {
|
||||
io::Error::new(io::ErrorKind::InvalidData, error)
|
||||
})?;
|
||||
|
||||
|
||||
Ok(Some(header))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user