mirror of
https://github.com/YGGverse/titanite.git
synced 2026-04-01 17:45:37 +00:00
use global Header trait
This commit is contained in:
parent
91077d3420
commit
44419afc16
18 changed files with 163 additions and 362 deletions
|
|
@ -11,31 +11,21 @@ impl NotValid {
|
|||
/// Build `Self` from UTF-8 header bytes
|
||||
/// * expected buffer includes leading status code, message, CRLF
|
||||
pub fn from_bytes(buffer: &[u8]) -> Result<Self> {
|
||||
// calculate length once
|
||||
let len = buffer.len();
|
||||
// validate headers for this response type
|
||||
if !(3..=1024).contains(&len) {
|
||||
bail!("Unexpected header length")
|
||||
}
|
||||
if buffer
|
||||
.get(..2)
|
||||
use crate::Header;
|
||||
let h = buffer.header_bytes()?;
|
||||
if h.get(..2)
|
||||
.is_none_or(|c| c[0] != CODE[0] || c[1] != CODE[1])
|
||||
{
|
||||
bail!("Invalid status code")
|
||||
}
|
||||
// collect data bytes
|
||||
let mut m = Vec::with_capacity(len);
|
||||
for b in buffer[3..].iter() {
|
||||
if *b == b'\r' {
|
||||
continue;
|
||||
}
|
||||
if *b == b'\n' {
|
||||
break;
|
||||
}
|
||||
m.push(*b)
|
||||
}
|
||||
Ok(Self {
|
||||
message: String::from_utf8(m).map(|m| if m.is_empty() { None } else { Some(m) })?,
|
||||
message: String::from_utf8((&h[3..]).to_vec()).map(|m| {
|
||||
if m.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(m)
|
||||
}
|
||||
})?,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue