implement titan and gemini requests in single file

This commit is contained in:
yggverse 2025-01-27 20:42:17 +02:00
parent 0cb5ff9cbc
commit 6da4c2ed52
5 changed files with 87 additions and 112 deletions

View file

@ -1,30 +0,0 @@
use glib::Uri;
/// [Gemini](https://geminiprotocol.net/docs/protocol-specification.gmi) protocol enum object for `Request`
pub struct Gemini {
pub uri: Uri,
}
impl Gemini {
// Getters
/// Get header string for `Self`
pub fn header(&self) -> String {
format!("{}\r\n", self.uri)
}
}
#[test]
fn header() {
use super::{super::Request, Gemini};
use glib::UriFlags;
const REQUEST: &str = "gemini://geminiprotocol.net/";
assert_eq!(
Request::Gemini(Gemini {
uri: Uri::parse(REQUEST, UriFlags::NONE).unwrap()
})
.header(),
format!("{REQUEST}\r\n")
);
}

View file

@ -1,63 +0,0 @@
use glib::{Bytes, Uri, UriHideFlags};
/// Formatted [Titan](gemini://transjovian.org/titan/page/The%20Titan%20Specification) `Request`
pub struct Titan {
pub uri: Uri,
pub data: Bytes,
/// MIME type is optional attribute by Titan protocol specification,
/// but server MAY reject the request without `mime` value provided.
pub mime: Option<String>,
pub token: Option<String>,
}
impl Titan {
// Getters
/// Get header string for `Self`
pub fn header(&self) -> String {
let mut header = format!(
"{};size={}",
self.uri.to_string_partial(UriHideFlags::QUERY),
self.data.len()
);
if let Some(ref mime) = self.mime {
header.push_str(&format!(";mime={mime}"));
}
if let Some(ref token) = self.token {
header.push_str(&format!(";token={token}"));
}
if let Some(query) = self.uri.query() {
header.push_str(&format!("?{query}"));
}
header.push_str("\r\n");
header
}
}
#[test]
fn header() {
use super::{super::Request, Titan};
use glib::UriFlags;
const DATA: &[u8] = &[1, 2, 3];
const MIME: &str = "plain/text";
const TOKEN: &str = "token";
assert_eq!(
Request::Titan(Titan {
uri: Uri::parse(
"titan://geminiprotocol.net/raw/path?key=value",
UriFlags::NONE
)
.unwrap(),
data: Bytes::from(DATA),
mime: Some(MIME.to_string()),
token: Some(TOKEN.to_string())
})
.header(),
format!(
"titan://geminiprotocol.net/raw/path;size={};mime={MIME};token={TOKEN}?key=value\r\n",
DATA.len(),
)
);
}