mirror of
https://github.com/YGGverse/ggemini.git
synced 2026-04-01 01:25:32 +00:00
update request api
This commit is contained in:
parent
52141f3dca
commit
5e52e74870
6 changed files with 37 additions and 78 deletions
|
|
@ -2,16 +2,16 @@ use std::fmt::{Display, Formatter, Result};
|
|||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
Request((Vec<u8>, glib::Error)),
|
||||
Response(crate::client::connection::response::Error),
|
||||
Stream(glib::Error),
|
||||
TlsClientConnection(glib::Error),
|
||||
}
|
||||
|
||||
impl Display for Error {
|
||||
fn fmt(&self, f: &mut Formatter) -> Result {
|
||||
match self {
|
||||
Self::Stream(e) => {
|
||||
write!(f, "TLS client connection error: {e}")
|
||||
Self::Request((_, e)) => {
|
||||
write!(f, "Request error: {e}")
|
||||
}
|
||||
Self::Response(e) => {
|
||||
write!(f, "Response error: {e}")
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ pub use gemini::Gemini;
|
|||
pub use titan::Titan;
|
||||
|
||||
use gio::NetworkAddress;
|
||||
use glib::{Bytes, Uri};
|
||||
|
||||
/// Single `Request` implementation for different protocols
|
||||
pub enum Request {
|
||||
|
|
@ -16,30 +15,13 @@ pub enum Request {
|
|||
}
|
||||
|
||||
impl Request {
|
||||
// Constructors
|
||||
|
||||
/// Create new `Self` for [Gemini protocol](https://geminiprotocol.net)
|
||||
pub fn gemini(uri: Uri) -> Self {
|
||||
Self::Gemini(Gemini { uri })
|
||||
}
|
||||
|
||||
/// Create new `Self` for [Titan protocol](gemini://transjovian.org/titan/page/The%20Titan%20Specification)
|
||||
pub fn titan(uri: Uri, data: Vec<u8>, mime: Option<String>, token: Option<String>) -> Self {
|
||||
Self::Titan(Titan {
|
||||
uri,
|
||||
data,
|
||||
mime,
|
||||
token,
|
||||
})
|
||||
}
|
||||
|
||||
// Getters
|
||||
|
||||
/// Get [Bytes](https://docs.gtk.org/glib/struct.Bytes.html) for `Self`
|
||||
pub fn to_bytes(&self) -> Bytes {
|
||||
/// Get header string for `Self`
|
||||
pub fn header(&self) -> String {
|
||||
match self {
|
||||
Self::Gemini(ref request) => request.to_bytes(),
|
||||
Self::Titan(ref request) => request.to_bytes(),
|
||||
Self::Gemini(ref this) => this.header(),
|
||||
Self::Titan(ref this) => this.header(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use glib::{Bytes, Uri};
|
||||
use glib::Uri;
|
||||
|
||||
/// [Gemini](https://geminiprotocol.net/docs/protocol-specification.gmi) protocol enum object for `Request`
|
||||
pub struct Gemini {
|
||||
|
|
@ -8,8 +8,8 @@ pub struct Gemini {
|
|||
impl Gemini {
|
||||
// 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())
|
||||
/// Get header string for `Self`
|
||||
pub fn header(&self) -> String {
|
||||
format!("{}\r\n", self.uri)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ use glib::{Bytes, Uri, UriHideFlags};
|
|||
/// [Titan](gemini://transjovian.org/titan/page/The%20Titan%20Specification) protocol enum object for `Request`
|
||||
pub struct Titan {
|
||||
pub uri: Uri,
|
||||
pub data: Vec<u8>,
|
||||
pub data: Bytes,
|
||||
pub mime: Option<String>,
|
||||
pub token: Option<String>,
|
||||
}
|
||||
|
|
@ -11,8 +11,8 @@ pub struct Titan {
|
|||
impl Titan {
|
||||
// Getters
|
||||
|
||||
/// Copy `Self` to [Bytes](https://docs.gtk.org/glib/struct.Bytes.html)
|
||||
pub fn to_bytes(&self) -> Bytes {
|
||||
/// Get header string for `Self`
|
||||
pub fn header(&self) -> String {
|
||||
// Calculate data size
|
||||
let size = self.data.len();
|
||||
|
||||
|
|
@ -31,13 +31,6 @@ impl Titan {
|
|||
header.push_str(&format!("?{query}"));
|
||||
}
|
||||
header.push_str("\r\n");
|
||||
|
||||
// Build request
|
||||
let mut bytes: Vec<u8> = Vec::with_capacity(size + 1024); // @TODO
|
||||
bytes.extend(header.into_bytes());
|
||||
bytes.extend(&self.data);
|
||||
|
||||
// Wrap result
|
||||
Bytes::from(&bytes)
|
||||
header
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue