mirror of
https://github.com/YGGverse/ggemini.git
synced 2026-04-01 17:45:35 +00:00
begin header holder implementation with lazy parser by getters, add request::Mode, add common header_bytes helper
This commit is contained in:
parent
a12a73d311
commit
7c518cecf6
13 changed files with 267 additions and 151 deletions
43
src/client/connection/response/success/default/header.rs
Normal file
43
src/client/connection/response/success/default/header.rs
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
pub mod error;
|
||||
pub use error::Error;
|
||||
|
||||
pub struct Header(Vec<u8>);
|
||||
|
||||
impl Header {
|
||||
// Constructors
|
||||
|
||||
pub fn parse(buffer: &[u8]) -> Result<Self, Error> {
|
||||
if !buffer.starts_with(super::CODE) {
|
||||
return Err(Error::Code);
|
||||
}
|
||||
Ok(Self(
|
||||
crate::client::connection::response::header_bytes(buffer)
|
||||
.map_err(|e| Error::Header(e))?
|
||||
.to_vec(),
|
||||
))
|
||||
}
|
||||
|
||||
// Getters
|
||||
|
||||
/// Parse content type for `Self`
|
||||
pub fn mime(&self) -> Result<String, Error> {
|
||||
glib::Regex::split_simple(
|
||||
r"^\d{2}\s([^\/]+\/[^\s;]+)",
|
||||
std::str::from_utf8(&self.0).map_err(|e| Error::Utf8Error(e))?,
|
||||
glib::RegexCompileFlags::DEFAULT,
|
||||
glib::RegexMatchFlags::DEFAULT,
|
||||
)
|
||||
.get(1)
|
||||
.map(|s| s.trim())
|
||||
.filter(|s| !s.is_empty())
|
||||
.map_or(Err(Error::Mime), |s| Ok(s.to_lowercase()))
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.0.len()
|
||||
}
|
||||
|
||||
pub fn as_bytes(&self) -> &[u8] {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue