mirror of
https://github.com/YGGverse/ggemini.git
synced 2026-04-01 17:45:35 +00:00
implement Titan protocol features
This commit is contained in:
parent
66a0de6a8e
commit
29b835411d
8 changed files with 204 additions and 27 deletions
22
src/client/connection/request/gemini.rs
Normal file
22
src/client/connection/request/gemini.rs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
use glib::{Bytes, Uri};
|
||||
|
||||
/// [Gemini](https://geminiprotocol.net/docs/protocol-specification.gmi) protocol enum object for `Request`
|
||||
pub struct Gemini {
|
||||
pub uri: Uri,
|
||||
}
|
||||
|
||||
impl Gemini {
|
||||
// Constructors
|
||||
|
||||
/// Build valid new `Self`
|
||||
pub fn build(uri: Uri) -> Self {
|
||||
Self { uri } // @TODO validate
|
||||
}
|
||||
|
||||
// Getters
|
||||
|
||||
/// Copy `Self` to [Bytes](https://docs.gtk.org/glib/struct.Bytes.html)
|
||||
pub fn to_bytes(&self) -> Bytes {
|
||||
Bytes::from(format!("{}\r\n", self.uri).as_bytes())
|
||||
}
|
||||
}
|
||||
53
src/client/connection/request/titan.rs
Normal file
53
src/client/connection/request/titan.rs
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
use glib::{Bytes, Uri};
|
||||
|
||||
/// [Titan](gemini://transjovian.org/titan/page/The%20Titan%20Specification) protocol enum object for `Request`
|
||||
pub struct Titan {
|
||||
pub uri: Uri,
|
||||
pub size: usize,
|
||||
pub mime: String,
|
||||
pub token: Option<String>,
|
||||
pub data: Vec<u8>,
|
||||
}
|
||||
|
||||
impl Titan {
|
||||
// Constructors
|
||||
|
||||
/// Build valid new `Self`
|
||||
pub fn build(
|
||||
uri: Uri,
|
||||
size: usize,
|
||||
mime: String,
|
||||
token: Option<String>,
|
||||
data: Vec<u8>,
|
||||
) -> Self {
|
||||
Self {
|
||||
uri,
|
||||
size,
|
||||
mime,
|
||||
token,
|
||||
data,
|
||||
} // @TODO validate
|
||||
}
|
||||
|
||||
// Getters
|
||||
|
||||
/// Copy `Self` to [Bytes](https://docs.gtk.org/glib/struct.Bytes.html)
|
||||
pub fn to_bytes(&self) -> Bytes {
|
||||
// Build header
|
||||
let mut header = format!("{};size={};mime={}", self.uri, self.size, self.mime);
|
||||
if let Some(ref token) = self.token {
|
||||
header.push_str(&format!(";token={token}"));
|
||||
}
|
||||
header.push_str("\r\n");
|
||||
|
||||
let header_bytes = header.into_bytes();
|
||||
|
||||
// Build request
|
||||
let mut bytes: Vec<u8> = Vec::with_capacity(self.size + header_bytes.len());
|
||||
bytes.extend(header_bytes);
|
||||
bytes.extend(&self.data);
|
||||
|
||||
// Wrap result
|
||||
Bytes::from(&bytes)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue