mirror of
https://github.com/YGGverse/titanite.git
synced 2026-03-31 17:15:33 +00:00
implement shared header_bytes method
This commit is contained in:
parent
d0fdbf6ac4
commit
7264d935be
2 changed files with 22 additions and 16 deletions
|
|
@ -4,6 +4,8 @@ pub mod titan;
|
|||
pub use gemini::Gemini;
|
||||
pub use titan::Titan;
|
||||
|
||||
pub const HEADER_MAX_LEN: usize = 1024;
|
||||
|
||||
use anyhow::{bail, Result};
|
||||
|
||||
/// https://geminiprotocol.net/docs/protocol-specification.gmi#requests
|
||||
|
|
@ -30,3 +32,19 @@ impl<'a> Request<'a> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn header_bytes(buffer: &[u8]) -> Result<&[u8]> {
|
||||
match buffer.iter().position(|&b| b == b'\r') {
|
||||
Some(n) => {
|
||||
if n > HEADER_MAX_LEN {
|
||||
bail!("Header bytes length reached")
|
||||
}
|
||||
if buffer.get(n + 1).is_some_and(|&b| b == b'\n') {
|
||||
Ok(&buffer[..n])
|
||||
} else {
|
||||
bail!("LF byte not found")
|
||||
}
|
||||
}
|
||||
None => bail!("CR byte not found"),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use anyhow::{bail, Result};
|
||||
use anyhow::Result;
|
||||
|
||||
/// [Gemini](https://geminiprotocol.net/docs/protocol-specification.gmi) request
|
||||
pub struct Gemini {
|
||||
|
|
@ -7,21 +7,9 @@ pub struct Gemini {
|
|||
|
||||
impl Gemini {
|
||||
pub fn from_bytes(buffer: &[u8]) -> Result<Self> {
|
||||
if buffer.len() > 1024 {
|
||||
bail!("Header bytes length reached")
|
||||
}
|
||||
match buffer.iter().position(|&b| b == b'\r') {
|
||||
Some(n) => {
|
||||
if buffer.get(n + 1).is_some_and(|&b| b == b'\n') {
|
||||
Ok(Self {
|
||||
url: String::from_utf8(buffer[..n].to_vec())?,
|
||||
})
|
||||
} else {
|
||||
bail!("LF byte not found")
|
||||
}
|
||||
}
|
||||
None => bail!("CR byte not found"),
|
||||
}
|
||||
Ok(Self {
|
||||
url: String::from_utf8(crate::request::header_bytes(buffer)?.to_vec())?,
|
||||
})
|
||||
}
|
||||
pub fn into_bytes(self) -> Vec<u8> {
|
||||
let mut bytes = Vec::with_capacity(self.url.len() + 2);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue