mirror of
https://github.com/YGGverse/ggemini.git
synced 2026-03-31 17:15:31 +00:00
validate header len
This commit is contained in:
parent
b62f990bf2
commit
6dbf49cea3
2 changed files with 44 additions and 27 deletions
|
|
@ -60,26 +60,35 @@ impl std::str::FromStr for Success {
|
|||
type Err = Error;
|
||||
fn from_str(header: &str) -> Result<Self, Self::Err> {
|
||||
use glib::{Regex, RegexCompileFlags, RegexMatchFlags};
|
||||
match Regex::split_simple(
|
||||
r"^20\s([^\/]+\/[^\s;]+)",
|
||||
header,
|
||||
RegexCompileFlags::DEFAULT,
|
||||
RegexMatchFlags::DEFAULT,
|
||||
)
|
||||
.get(1)
|
||||
{
|
||||
Some(mime) => {
|
||||
let mime = mime.trim();
|
||||
if mime.is_empty() {
|
||||
Err(Error::Mime)
|
||||
} else {
|
||||
Ok(Self::Default {
|
||||
header: header.to_string(),
|
||||
mime: mime.to_lowercase(),
|
||||
})
|
||||
|
||||
if header.len() > super::HEADER_LEN {
|
||||
return Err(Error::HeaderLen(header.len()));
|
||||
}
|
||||
|
||||
// * keep separator after code as expected by protocol
|
||||
match header.strip_prefix("20") {
|
||||
Some(postfix) => match Regex::split_simple(
|
||||
r"^\s+([^\/]+\/[^\s;]+)",
|
||||
postfix,
|
||||
RegexCompileFlags::DEFAULT,
|
||||
RegexMatchFlags::DEFAULT,
|
||||
)
|
||||
.get(1)
|
||||
{
|
||||
Some(mime) => {
|
||||
let mime = mime.trim();
|
||||
if mime.is_empty() {
|
||||
Err(Error::ContentType)
|
||||
} else {
|
||||
Ok(Self::Default {
|
||||
header: header.to_string(),
|
||||
mime: mime.to_lowercase(),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
None => Err(Error::Protocol),
|
||||
None => Err(Error::ContentType),
|
||||
},
|
||||
None => Err(Error::Code),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,23 +5,31 @@ use std::{
|
|||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
Protocol,
|
||||
Mime,
|
||||
Code,
|
||||
ContentType,
|
||||
HeaderLen(usize),
|
||||
Utf8Error(Utf8Error),
|
||||
}
|
||||
|
||||
impl Display for Error {
|
||||
fn fmt(&self, f: &mut Formatter) -> Result {
|
||||
match self {
|
||||
Self::Code => {
|
||||
write!(f, "Unexpected status code")
|
||||
}
|
||||
Self::ContentType => {
|
||||
write!(f, "Content type required")
|
||||
}
|
||||
Self::HeaderLen(l) => {
|
||||
write!(
|
||||
f,
|
||||
"Header length reached protocol limit ({l} of {} bytes max)",
|
||||
super::super::HEADER_LEN
|
||||
)
|
||||
}
|
||||
Self::Utf8Error(e) => {
|
||||
write!(f, "UTF-8 error: {e}")
|
||||
}
|
||||
Self::Protocol => {
|
||||
write!(f, "Protocol error")
|
||||
}
|
||||
Self::Mime => {
|
||||
write!(f, "MIME error")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue