fix maximum payload of 16 kB by using write_all method, hold bytes on request error

This commit is contained in:
yggverse 2025-02-24 07:49:41 +02:00
parent 4f6799a495
commit 1ff38ee838
3 changed files with 20 additions and 11 deletions

View file

@ -9,12 +9,12 @@ pub use response::Response;
// Local dependencies // Local dependencies
use gio::{ use gio::{
prelude::{IOStreamExt, OutputStreamExt, OutputStreamExtManual, TlsConnectionExt}, prelude::{IOStreamExt, OutputStreamExtManual, TlsConnectionExt},
Cancellable, IOStream, NetworkAddress, SocketConnection, TlsCertificate, TlsClientConnection, Cancellable, IOStream, NetworkAddress, SocketConnection, TlsCertificate, TlsClientConnection,
}; };
use glib::{ use glib::{
object::{Cast, ObjectExt}, object::{Cast, ObjectExt},
Priority, Bytes, Priority,
}; };
pub struct Connection { pub struct Connection {
@ -61,8 +61,11 @@ impl Connection {
callback: impl FnOnce(Result<(Response, Self), Error>) + 'static, callback: impl FnOnce(Result<(Response, Self), Error>) + 'static,
) { ) {
let output_stream = self.stream().output_stream(); let output_stream = self.stream().output_stream();
output_stream.clone().write_async( // Make sure **all header bytes** sent to the destination
request.header().into_bytes(), // > A partial write is performed with the size of a message block, which is 16kB
// > https://docs.openssl.org/3.0/man3/SSL_write/#notes
output_stream.clone().write_all_async(
Bytes::from_owned(request.header()),
priority, priority,
Some(&cancellable.clone()), Some(&cancellable.clone()),
move |result| match result { move |result| match result {
@ -78,8 +81,11 @@ impl Connection {
}) })
}, },
), ),
Request::Titan { data, .. } => output_stream.write_bytes_async( // Make sure **all data bytes** sent to the destination
&data, // > A partial write is performed with the size of a message block, which is 16kB
// > https://docs.openssl.org/3.0/man3/SSL_write/#notes
Request::Titan { data, .. } => output_stream.write_all_async(
data,
priority, priority,
Some(&cancellable.clone()), Some(&cancellable.clone()),
move |result| match result { move |result| match result {
@ -94,11 +100,11 @@ impl Connection {
}) })
}, },
), ),
Err(e) => callback(Err(Error::Request(e))), Err((b, e)) => callback(Err(Error::Request(b, e))),
}, },
), ),
}, },
Err((_, e)) => callback(Err(Error::Request(e))), Err((b, e)) => callback(Err(Error::Request(b, e))),
}, },
) )
} }

View file

@ -2,7 +2,7 @@ use std::fmt::{Display, Formatter, Result};
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
Request(glib::Error), Request(glib::Bytes, glib::Error),
Response(crate::client::connection::response::Error), Response(crate::client::connection::response::Error),
TlsClientConnection(glib::Error), TlsClientConnection(glib::Error),
} }
@ -10,7 +10,7 @@ pub enum Error {
impl Display for Error { impl Display for Error {
fn fmt(&self, f: &mut Formatter) -> Result { fn fmt(&self, f: &mut Formatter) -> Result {
match self { match self {
Self::Request(e) => { Self::Request(_, e) => {
write!(f, "Request error: {e}") write!(f, "Request error: {e}")
} }
Self::Response(e) => { Self::Response(e) => {

View file

@ -44,7 +44,10 @@ pub fn from_stream_async(
return on_complete(Ok((file_output_stream, total))); return on_complete(Ok((file_output_stream, total)));
} }
file_output_stream.clone().write_async( // Make sure **all bytes** sent to the destination
// > A partial write is performed with the size of a message block, which is 16kB
// > https://docs.openssl.org/3.0/man3/SSL_write/#notes
file_output_stream.clone().write_all_async(
bytes.clone(), bytes.clone(),
priority, priority,
Some(&cancellable.clone()), Some(&cancellable.clone()),