mirror of
https://github.com/YGGverse/ggemini.git
synced 2026-03-31 17:15:31 +00:00
stop chunk iteration on match len < chunk condition (some servers may close the connection immediately); hold memory_input_stream in the error returned
This commit is contained in:
parent
d4f076f074
commit
62f53304aa
3 changed files with 23 additions and 17 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "ggemini"
|
name = "ggemini"
|
||||||
version = "0.15.1"
|
version = "0.16.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
|
|
||||||
|
|
@ -53,19 +53,27 @@ pub fn for_memory_input_stream_async(
|
||||||
Some(&cancellable.clone()),
|
Some(&cancellable.clone()),
|
||||||
move |result| match result {
|
move |result| match result {
|
||||||
Ok(bytes) => {
|
Ok(bytes) => {
|
||||||
total += bytes.len();
|
let len = bytes.len(); // calculate once
|
||||||
on_chunk(bytes.len(), total);
|
|
||||||
|
|
||||||
if total > limit {
|
total += len;
|
||||||
return on_complete(Err(Error::BytesTotal(total, limit)));
|
on_chunk(len, total);
|
||||||
}
|
|
||||||
|
|
||||||
if bytes.len() == 0 {
|
|
||||||
return on_complete(Ok((memory_input_stream, total)));
|
|
||||||
}
|
|
||||||
|
|
||||||
memory_input_stream.add_bytes(&bytes);
|
memory_input_stream.add_bytes(&bytes);
|
||||||
|
|
||||||
|
// prevent memory overflow on size `limit` reached
|
||||||
|
// * add last received bytes into the `memory_input_stream` anyway (to prevent data lost),
|
||||||
|
// it's safe because limited to the `chunk` size
|
||||||
|
if total > limit {
|
||||||
|
return on_complete(Err(Error::BytesTotal(memory_input_stream, total, limit)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// is the next iteration required?
|
||||||
|
if len < chunk // some servers may close the connection after first chunk request (@TODO this condition wants review)
|
||||||
|
|| len == 0
|
||||||
|
{
|
||||||
|
return on_complete(Ok((memory_input_stream, total)));
|
||||||
|
}
|
||||||
|
|
||||||
// continue reading..
|
// continue reading..
|
||||||
for_memory_input_stream_async(
|
for_memory_input_stream_async(
|
||||||
memory_input_stream,
|
memory_input_stream,
|
||||||
|
|
@ -76,9 +84,7 @@ pub fn for_memory_input_stream_async(
|
||||||
(on_chunk, on_complete),
|
(on_chunk, on_complete),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => on_complete(Err(Error::InputStream(memory_input_stream, e))),
|
||||||
on_complete(Err(Error::InputStream(e)));
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,17 +2,17 @@ use std::fmt::{Display, Formatter, Result};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
BytesTotal(usize, usize),
|
BytesTotal(gio::MemoryInputStream, usize, usize),
|
||||||
InputStream(glib::Error),
|
InputStream(gio::MemoryInputStream, glib::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::BytesTotal(total, limit) => {
|
Self::BytesTotal(_, total, limit) => {
|
||||||
write!(f, "Bytes total limit reached: {total} / {limit}")
|
write!(f, "Bytes total limit reached: {total} / {limit}")
|
||||||
}
|
}
|
||||||
Self::InputStream(e) => {
|
Self::InputStream(_, e) => {
|
||||||
write!(f, "Input stream error: {e}")
|
write!(f, "Input stream error: {e}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue