mirror of
https://github.com/YGGverse/ggemini.git
synced 2026-04-02 10:05:36 +00:00
update argument types
This commit is contained in:
parent
cbaa4c0e81
commit
cc0625d920
3 changed files with 48 additions and 35 deletions
|
|
@ -60,15 +60,22 @@ impl Client {
|
||||||
.as_ref(),
|
.as_ref(),
|
||||||
move |result| match result {
|
move |result| match result {
|
||||||
Ok(connection) => {
|
Ok(connection) => {
|
||||||
match Connection::from(network_address, connection, certificate) {
|
match Connection::new_for(
|
||||||
|
&connection,
|
||||||
|
certificate.as_ref(),
|
||||||
|
Some(&network_address),
|
||||||
|
) {
|
||||||
Ok(result) => request_async(
|
Ok(result) => request_async(
|
||||||
result,
|
result,
|
||||||
uri.to_string(),
|
uri.to_string(),
|
||||||
match priority {
|
match priority {
|
||||||
Some(priority) => priority,
|
Some(priority) => Some(priority),
|
||||||
None => Priority::DEFAULT,
|
None => Some(Priority::DEFAULT),
|
||||||
|
},
|
||||||
|
match cancellable {
|
||||||
|
Some(ref cancellable) => Some(cancellable.clone()),
|
||||||
|
None => None::<Cancellable>,
|
||||||
},
|
},
|
||||||
cancellable.unwrap(), // @TODO
|
|
||||||
move |result| callback(result),
|
move |result| callback(result),
|
||||||
),
|
),
|
||||||
Err(reason) => callback(Err(Error::Connection(reason))),
|
Err(reason) => callback(Err(Error::Connection(reason))),
|
||||||
|
|
@ -83,30 +90,36 @@ impl Client {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Private helpers
|
/// Make new request for constructed `Connection`
|
||||||
|
/// * callback with `Result`on success or `Error` on failure
|
||||||
fn request_async(
|
pub fn request_async(
|
||||||
connection: Connection,
|
connection: Connection,
|
||||||
query: String,
|
query: String,
|
||||||
priority: Priority,
|
priority: Option<Priority>,
|
||||||
cancellable: Cancellable,
|
cancellable: Option<Cancellable>,
|
||||||
callback: impl Fn(Result<Response, Error>) + 'static,
|
callback: impl Fn(Result<Response, Error>) + 'static,
|
||||||
) {
|
) {
|
||||||
connection.stream().output_stream().write_bytes_async(
|
connection.stream().output_stream().write_bytes_async(
|
||||||
&Bytes::from(format!("{query}\r\n").as_bytes()),
|
&Bytes::from(format!("{query}\r\n").as_bytes()),
|
||||||
priority,
|
match priority {
|
||||||
Some(&cancellable.clone()),
|
Some(priority) => priority,
|
||||||
|
None => Priority::DEFAULT,
|
||||||
|
},
|
||||||
|
match cancellable {
|
||||||
|
Some(ref cancellable) => Some(cancellable.clone()),
|
||||||
|
None => None::<Cancellable>,
|
||||||
|
}
|
||||||
|
.as_ref(),
|
||||||
move |result| match result {
|
move |result| match result {
|
||||||
Ok(_) => Response::from_request_async(
|
Ok(_) => {
|
||||||
connection,
|
Response::from_request_async(connection, priority, cancellable, move |result| {
|
||||||
Some(priority),
|
callback(match result {
|
||||||
Some(cancellable),
|
Ok(response) => Ok(response),
|
||||||
move |result| match result {
|
Err(reason) => Err(Error::Response(reason)),
|
||||||
Ok(response) => callback(Ok(response)),
|
})
|
||||||
Err(reason) => callback(Err(Error::Response(reason))),
|
})
|
||||||
},
|
}
|
||||||
),
|
Err(reason) => callback(Err(Error::OutputStream(reason))),
|
||||||
Err(reason) => callback(Err(Error::Write(reason))),
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,10 +19,10 @@ impl Connection {
|
||||||
// Constructors
|
// Constructors
|
||||||
|
|
||||||
/// Create new `Self`
|
/// Create new `Self`
|
||||||
pub fn from(
|
pub fn new_for(
|
||||||
network_address: NetworkAddress, // @TODO struct cert as sni
|
socket_connection: &SocketConnection,
|
||||||
socket_connection: SocketConnection,
|
certificate: Option<&TlsCertificate>,
|
||||||
certificate: Option<TlsCertificate>,
|
server_identity: Option<&NetworkAddress>,
|
||||||
) -> Result<Self, Error> {
|
) -> Result<Self, Error> {
|
||||||
if socket_connection.is_closed() {
|
if socket_connection.is_closed() {
|
||||||
return Err(Error::SocketConnectionClosed);
|
return Err(Error::SocketConnectionClosed);
|
||||||
|
|
@ -31,7 +31,7 @@ impl Connection {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
socket_connection: socket_connection.clone(),
|
socket_connection: socket_connection.clone(),
|
||||||
tls_client_connection: match certificate {
|
tls_client_connection: match certificate {
|
||||||
Some(certificate) => match auth(network_address, socket_connection, certificate) {
|
Some(certificate) => match auth(socket_connection, certificate, server_identity) {
|
||||||
Ok(tls_client_connection) => Some(tls_client_connection),
|
Ok(tls_client_connection) => Some(tls_client_connection),
|
||||||
Err(reason) => return Err(reason),
|
Err(reason) => return Err(reason),
|
||||||
},
|
},
|
||||||
|
|
@ -53,19 +53,19 @@ impl Connection {
|
||||||
// Tools
|
// Tools
|
||||||
|
|
||||||
pub fn auth(
|
pub fn auth(
|
||||||
server_identity: NetworkAddress, // @TODO impl IsA<SocketConnectable> ?
|
socket_connection: &SocketConnection,
|
||||||
socket_connection: SocketConnection,
|
certificate: &TlsCertificate,
|
||||||
certificate: TlsCertificate,
|
server_identity: Option<&NetworkAddress>,
|
||||||
) -> Result<TlsClientConnection, Error> {
|
) -> Result<TlsClientConnection, Error> {
|
||||||
if socket_connection.is_closed() {
|
if socket_connection.is_closed() {
|
||||||
return Err(Error::SocketConnectionClosed);
|
return Err(Error::SocketConnectionClosed);
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://geminiprotocol.net/docs/protocol-specification.gmi#the-use-of-tls
|
// https://geminiprotocol.net/docs/protocol-specification.gmi#the-use-of-tls
|
||||||
match TlsClientConnection::new(&socket_connection, Some(&server_identity)) {
|
match TlsClientConnection::new(socket_connection, server_identity) {
|
||||||
Ok(tls_client_connection) => {
|
Ok(tls_client_connection) => {
|
||||||
// https://geminiprotocol.net/docs/protocol-specification.gmi#client-certificates
|
// https://geminiprotocol.net/docs/protocol-specification.gmi#client-certificates
|
||||||
tls_client_connection.set_certificate(&certificate);
|
tls_client_connection.set_certificate(certificate);
|
||||||
|
|
||||||
// @TODO handle exceptions
|
// @TODO handle exceptions
|
||||||
// https://geminiprotocol.net/docs/protocol-specification.gmi#closing-connections
|
// https://geminiprotocol.net/docs/protocol-specification.gmi#closing-connections
|
||||||
|
|
|
||||||
|
|
@ -6,9 +6,9 @@ pub enum Error {
|
||||||
Connectable(String),
|
Connectable(String),
|
||||||
Connection(crate::client::connection::Error),
|
Connection(crate::client::connection::Error),
|
||||||
NetworkAddress(crate::gio::network_address::Error),
|
NetworkAddress(crate::gio::network_address::Error),
|
||||||
|
OutputStream(glib::Error),
|
||||||
Request(glib::Error),
|
Request(glib::Error),
|
||||||
Response(crate::client::response::Error),
|
Response(crate::client::response::Error),
|
||||||
Write(glib::Error),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for Error {
|
impl Display for Error {
|
||||||
|
|
@ -26,15 +26,15 @@ impl Display for Error {
|
||||||
Self::NetworkAddress(reason) => {
|
Self::NetworkAddress(reason) => {
|
||||||
write!(f, "Network address error: {reason}")
|
write!(f, "Network address error: {reason}")
|
||||||
}
|
}
|
||||||
|
Self::OutputStream(reason) => {
|
||||||
|
write!(f, "Output stream error: {reason}")
|
||||||
|
}
|
||||||
Self::Request(reason) => {
|
Self::Request(reason) => {
|
||||||
write!(f, "Request error: {reason}")
|
write!(f, "Request error: {reason}")
|
||||||
}
|
}
|
||||||
Self::Response(reason) => {
|
Self::Response(reason) => {
|
||||||
write!(f, "Response error: {reason}")
|
write!(f, "Response error: {reason}")
|
||||||
}
|
}
|
||||||
Self::Write(reason) => {
|
|
||||||
write!(f, "I/O Write error: {reason}")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue