mirror of
https://github.com/YGGverse/ggemini.git
synced 2026-03-31 17:15:31 +00:00
update request api
This commit is contained in:
parent
52141f3dca
commit
5e52e74870
6 changed files with 37 additions and 78 deletions
|
|
@ -6,13 +6,15 @@ pub use error::Error;
|
|||
pub use request::{Gemini, Request, Titan};
|
||||
pub use response::Response;
|
||||
|
||||
// Local dependencies
|
||||
|
||||
use gio::{
|
||||
prelude::{IOStreamExt, OutputStreamExt, TlsConnectionExt},
|
||||
prelude::{IOStreamExt, OutputStreamExtManual, TlsConnectionExt},
|
||||
Cancellable, IOStream, NetworkAddress, SocketConnection, TlsCertificate, TlsClientConnection,
|
||||
};
|
||||
use glib::{
|
||||
object::{Cast, ObjectExt},
|
||||
Bytes, Priority,
|
||||
Priority,
|
||||
};
|
||||
|
||||
pub struct Connection {
|
||||
|
|
@ -58,24 +60,8 @@ impl Connection {
|
|||
cancellable: Cancellable,
|
||||
callback: impl FnOnce(Result<Response, Error>) + 'static,
|
||||
) {
|
||||
self.bytes_request_async(&request.to_bytes(), priority, cancellable, callback);
|
||||
}
|
||||
|
||||
/// Low-level shared method to send raw bytes array over
|
||||
/// [Gemini](https://geminiprotocol.net/docs/protocol-specification.gmi) or
|
||||
/// [Titan](gemini://transjovian.org/titan/page/The%20Titan%20Specification) protocol
|
||||
/// * bytes array should include formatted header according to protocol selected
|
||||
/// * for high-level requests see `gemini_request_async` and `titan_request_async` methods
|
||||
/// * to construct multi-protocol request with single function, use `request_async` method
|
||||
pub fn bytes_request_async(
|
||||
self,
|
||||
request: &Bytes,
|
||||
priority: Priority,
|
||||
cancellable: Cancellable,
|
||||
callback: impl FnOnce(Result<Response, Error>) + 'static,
|
||||
) {
|
||||
self.stream().output_stream().write_bytes_async(
|
||||
request,
|
||||
self.stream().output_stream().write_async(
|
||||
request.header().into_bytes(),
|
||||
priority,
|
||||
Some(&cancellable.clone()),
|
||||
move |result| match result {
|
||||
|
|
@ -88,7 +74,7 @@ impl Connection {
|
|||
})
|
||||
})
|
||||
}
|
||||
Err(e) => callback(Err(Error::Stream(e))),
|
||||
Err((b, e)) => callback(Err(Error::Request((b, e)))),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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