update ggemini api to v0.15.0 / checkout repository version

This commit is contained in:
yggverse 2025-02-03 01:23:51 +02:00
parent eb6f8bac91
commit fdeb16e4b1
2 changed files with 204 additions and 208 deletions

View file

@ -30,7 +30,7 @@ version = "0.9.1"
[dependencies] [dependencies]
ansi-parser = "0.9.1" ansi-parser = "0.9.1"
ggemini = "0.14.0" ggemini = "0.15.0"
ggemtext = "0.3.0" ggemtext = "0.3.0"
indexmap = "2.7.0" indexmap = "2.7.0"
itertools = "0.14.0" itertools = "0.14.0"
@ -40,7 +40,7 @@ plurify = "0.2.0"
syntect = "5.2.0" syntect = "5.2.0"
# development # development
# [patch.crates-io] [patch.crates-io]
# ggemini = { git = "https://github.com/YGGverse/ggemini.git" } ggemini = { git = "https://github.com/YGGverse/ggemini.git" }
# ggemtext = { git = "https://github.com/YGGverse/ggemtext.git" } # ggemtext = { git = "https://github.com/YGGverse/ggemtext.git" }
# plurify = { git = "https://github.com/YGGverse/plurify.git" } # plurify = { git = "https://github.com/YGGverse/plurify.git" }

View file

@ -1,8 +1,9 @@
use super::{Feature, Page}; use super::{Feature, Page};
use ggemini::client::{ use ggemini::client::connection::response::{
connection::response::{data::Text, meta::Status}, failure::{Permanent, Temporary},
Client, Request, Certificate, Failure, Input, Redirect, Success,
}; };
use ggemini::client::{connection::response::data::Text, Client, Request, Response};
use gtk::glib::Bytes; use gtk::glib::Bytes;
use gtk::glib::{GString, UriFlags}; use gtk::glib::{GString, UriFlags};
use gtk::{ use gtk::{
@ -137,35 +138,33 @@ fn handle(
let page = page.clone(); let page = page.clone();
let redirects = redirects.clone(); let redirects = redirects.clone();
move |result| match result { move |result| match result {
Ok(response) => { Ok((response, connection)) => match response {
match response.meta.status { // https://geminiprotocol.net/docs/protocol-specification.gmi#input-expected
// https://geminiprotocol.net/docs/protocol-specification.gmi#input-expected Response::Input(input) => {
Status::Input | Status::SensitiveInput => { let title = input.to_string();
let title = match response.meta.data { page.set_progress(0.0);
Some(data) => data.to_string(), page.set_title(&title);
None => Status::Input.to_string(), redirects.replace(0); // reset
}; match input {
if matches!(response.meta.status, Status::SensitiveInput) { // https://geminiprotocol.net/docs/protocol-specification.gmi#status-10
page.input.set_new_sensitive( Input::Default { message } => page.input.set_new_response(
page.item_action.clone(), page.item_action.clone(),
uri, uri,
Some(&title), Some(message.as_ref().unwrap_or(&title)),
Some(1024), Some(1024),
); ),
} else { // https://geminiprotocol.net/docs/protocol-specification.gmi#status-11-sensitive-input
page.input.set_new_response( Input::Sensitive { message } => page.input.set_new_sensitive(
page.item_action.clone(), page.item_action.clone(),
uri, uri,
Some(&title), Some(message.as_ref().unwrap_or(&title)),
Some(1024), Some(1024),
); )
}
page.set_progress(0.0);
page.set_title(&title);
redirects.replace(0); // reset
} }
// https://geminiprotocol.net/docs/protocol-specification.gmi#status-20 }
Status::Success => match *feature { // https://geminiprotocol.net/docs/protocol-specification.gmi#status-20
Response::Success(success) => match success {
Success::Default { mime } => match *feature {
Feature::Download => { Feature::Download => {
// Init download widget // Init download widget
let status = page.content.to_status_download( let status = page.content.to_status_download(
@ -174,7 +173,7 @@ fn handle(
&cancellable, &cancellable,
{ {
let cancellable = cancellable.clone(); let cancellable = cancellable.clone();
let stream = response.connection.stream(); let stream = connection.stream();
move |file, action| { move |file, action| {
match file.replace( match file.replace(
None, None,
@ -233,199 +232,196 @@ fn handle(
page.set_title(&status.title()); page.set_title(&status.title());
redirects.replace(0); // reset redirects.replace(0); // reset
}, },
_ => match response.meta.mime { _ => match mime.as_str() {
Some(mime) => match mime.as_str() { "text/gemini" => Text::from_stream_async(
"text/gemini" => Text::from_stream_async( connection.stream(),
response.connection.stream(), Priority::DEFAULT,
Priority::DEFAULT, cancellable.clone(),
cancellable.clone(), move |result| match result {
move |result| match result { Ok(text) => {
Ok(text) => { let widget = if matches!(*feature, Feature::Source) {
let widget = if matches!(*feature, Feature::Source) { page.content.to_text_source(&text.to_string())
page.content.to_text_source(&text.to_string()) } else {
} else { page.content.to_text_gemini(&uri, &text.to_string())
page.content.to_text_gemini(&uri, &text.to_string()) };
}; page.search.set(Some(widget.text_view));
page.search.set(Some(widget.text_view)); page.set_title(&match widget.meta.title {
page.set_title(&match widget.meta.title { Some(title) => title.into(), // @TODO
Some(title) => title.into(), // @TODO None => uri_to_title(&uri),
None => uri_to_title(&uri), });
}); page.set_progress(0.0);
page.set_progress(0.0); page.window_action
page.window_action .find
.find .simple_action
.simple_action .set_enabled(true);
.set_enabled(true); redirects.replace(0); // reset
redirects.replace(0); // reset }
} Err(e) => {
Err(e) => {
let status = page.content.to_status_failure();
status.set_description(Some(&e.to_string()));
page.set_progress(0.0);
page.set_title(&status.title());
redirects.replace(0); // reset
},
},
),
"image/png" | "image/gif" | "image/jpeg" | "image/webp" => {
// Final image size unknown, show loading widget
let status = page.content.to_status_loading(
Some(Duration::from_secs(1)), // show if download time > 1 second
);
// Asynchronously read [IOStream](https://docs.gtk.org/gio/class.IOStream.html)
// to local [MemoryInputStream](https://docs.gtk.org/gio/class.MemoryInputStream.html)
// show bytes count in loading widget, validate max size for incoming data
// * no dependency of Gemini library here, feel free to use any other `IOStream` processor
ggemini::gio::memory_input_stream::from_stream_async(
response.connection.stream(),
cancellable.clone(),
Priority::DEFAULT,
0x400, // 1024 bytes per chunk, optional step for images download tracking
0xA00000, // 10M bytes max to prevent memory overflow if server play with promises
move |_, total|
status.set_description(Some(&format!("Download: {total} bytes"))),
{
let page = page.clone();
move |result| match result {
Ok((memory_input_stream, _)) => {
Pixbuf::from_stream_async(
&memory_input_stream,
Some(&cancellable),
move |result| {
match result {
Ok(buffer) => {
page.set_title(&uri_to_title(&uri));
page.content.to_image(&Texture::for_pixbuf(&buffer));
}
Err(e) => {
let status = page.content.to_status_failure();
status.set_description(Some(e.message()));
page.set_title(&status.title());
}
}
page.set_progress(0.0);
redirects.replace(0); // reset
},
)
}
Err(e) => {
let status = page.content.to_status_failure();
status.set_description(Some(&e.to_string()));
page.set_progress(0.0);
page.set_title(&status.title());
redirects.replace(0); // reset
}
}
},
)
}
mime => {
let status = page
.content
.to_status_mime(mime, Some((&page.item_action, &uri)));
status.set_description(Some(&format!("Content type `{mime}` yet not supported")));
page.set_progress(0.0);
page.set_title(&status.title());
redirects.replace(0); // reset
},
},
None => {
let status = page.content.to_status_failure();
status.set_description(Some("MIME type not found"));
page.set_progress(0.0);
page.set_title(&status.title());
redirects.replace(0); // reset
},
}
},
// https://geminiprotocol.net/docs/protocol-specification.gmi#status-30-temporary-redirection
// https://geminiprotocol.net/docs/protocol-specification.gmi#status-31-permanent-redirection
Status::PermanentRedirect | Status::Redirect => {
// Expected target URL in response meta
match response.meta.data {
Some(data) => match redirection_base(uri).parse_relative(data.as_str(), UriFlags::NONE) {
Ok(target) => {
// Increase client redirection counter
let total = redirects.take() + 1;
// > Client MUST limit the number of redirections they follow to 5 redirections
// > https://geminiprotocol.net/docs/protocol-specification.gmi#redirection
if total > 5 {
let status = page.content.to_status_failure(); let status = page.content.to_status_failure();
status.set_description(Some("Redirection limit reached")); status.set_description(Some(&e.to_string()));
page.set_progress(0.0); page.set_progress(0.0);
page.set_title(&status.title()); page.set_title(&status.title());
redirects.replace(0); // reset redirects.replace(0); // reset
/* @TODO can't find that in specification: },
// Disallow external redirection by protocol restrictions },
} else if "gemini" != target.scheme() ),
|| uri.port() != target.port() "image/png" | "image/gif" | "image/jpeg" | "image/webp" => {
|| uri.host() != target.host() { // Final image size unknown, show loading widget
let status = page.content.to_status_failure(); let status = page.content.to_status_loading(
status.set_description(Some("External redirects not allowed by protocol specification")); Some(Duration::from_secs(1)), // show if download time > 1 second
page.set_progress(0.0); );
page.set_title(&status.title());
redirects.replace(0); // reset // Asynchronously read [IOStream](https://docs.gtk.org/gio/class.IOStream.html)
*/ // to local [MemoryInputStream](https://docs.gtk.org/gio/class.MemoryInputStream.html)
// Valid // show bytes count in loading widget, validate max size for incoming data
} else { // * no dependency of Gemini library here, feel free to use any other `IOStream` processor
if matches!(response.meta.status, Status::PermanentRedirect) { ggemini::gio::memory_input_stream::from_stream_async(
page.navigation.set_request(&target.to_string()); connection.stream(),
cancellable.clone(),
Priority::DEFAULT,
0x400, // 1024 bytes per chunk, optional step for images download tracking
0xA00000, // 10M bytes max to prevent memory overflow if server play with promises
move |_, total|
status.set_description(Some(&format!("Download: {total} bytes"))),
{
let page = page.clone();
move |result| match result {
Ok((memory_input_stream, _)) => {
Pixbuf::from_stream_async(
&memory_input_stream,
Some(&cancellable),
move |result| {
match result {
Ok(buffer) => {
page.set_title(&uri_to_title(&uri));
page.content.to_image(&Texture::for_pixbuf(&buffer));
}
Err(e) => {
let status = page.content.to_status_failure();
status.set_description(Some(e.message()));
page.set_title(&status.title());
}
}
page.set_progress(0.0);
redirects.replace(0); // reset
},
)
}
Err(e) => {
let status = page.content.to_status_failure();
status.set_description(Some(&e.to_string()));
page.set_progress(0.0);
page.set_title(&status.title());
redirects.replace(0); // reset
}
} }
redirects.replace(total); },
page.item_action.load.activate(Some(&target.to_string()), false); )
}
}
Err(e) => {
let status = page.content.to_status_failure();
status.set_description(Some(&e.to_string()));
page.set_progress(0.0);
page.set_title(&status.title());
redirects.replace(0); // reset
}
} }
None => { mime => {
let status = page.content.to_status_failure(); let status = page
status.set_description(Some("Redirection target not found")); .content
.to_status_mime(mime, Some((&page.item_action, &uri)));
status.set_description(Some(&format!("Content type `{mime}` yet not supported")));
page.set_progress(0.0); page.set_progress(0.0);
page.set_title(&status.title()); page.set_title(&status.title());
redirects.replace(0); // reset redirects.replace(0); // reset
},
}
}
},
Response::Redirect(redirect) => match &redirect {
// https://geminiprotocol.net/docs/protocol-specification.gmi#status-30-temporary-redirection
// https://geminiprotocol.net/docs/protocol-specification.gmi#status-31-permanent-redirection
Redirect::Temporary { target } |
Redirect::Permanent { target } => match redirection_base(uri).parse_relative(target, UriFlags::NONE) {
Ok(target) => {
// Increase client redirection counter
let total = redirects.take() + 1;
// > Client MUST limit the number of redirections they follow to 5 redirections
// > https://geminiprotocol.net/docs/protocol-specification.gmi#redirection
if total > 5 {
let status = page.content.to_status_failure();
status.set_description(Some("Redirection limit reached"));
page.set_progress(0.0);
page.set_title(&status.title());
redirects.replace(0); // reset
/* @TODO can't find that in specification:
// Disallow external redirection by protocol restrictions
} else if "gemini" != target.scheme()
|| uri.port() != target.port()
|| uri.host() != target.host() {
let status = page.content.to_status_failure();
status.set_description(Some("External redirects not allowed by protocol specification"));
page.set_progress(0.0);
page.set_title(&status.title());
redirects.replace(0); // reset
*/
// Valid
} else {
if matches!(redirect, Redirect::Permanent { .. }) {
page.navigation.set_request(&target.to_string());
}
redirects.replace(total);
page.item_action.load.activate(Some(&target.to_string()), false);
} }
} }
}, Err(e) => {
let status = page.content.to_status_failure();
status.set_description(Some(&e.to_string()));
page.set_progress(0.0);
page.set_title(&status.title());
redirects.replace(0); // reset
}
}
}
Response::Certificate(ref certificate) => match certificate {
// https://geminiprotocol.net/docs/protocol-specification.gmi#status-60 // https://geminiprotocol.net/docs/protocol-specification.gmi#status-60
Status::CertificateRequest | Certificate::Required { message } |
// https://geminiprotocol.net/docs/protocol-specification.gmi#status-61-certificate-not-authorized // https://geminiprotocol.net/docs/protocol-specification.gmi#status-61-certificate-not-authorized
Status::CertificateUnauthorized | Certificate::NotAuthorized { message } |
// https://geminiprotocol.net/docs/protocol-specification.gmi#status-62-certificate-not-valid // https://geminiprotocol.net/docs/protocol-specification.gmi#status-62-certificate-not-valid
Status::CertificateInvalid => { Certificate::NotValid { message } => {
let status = page.content.to_status_identity(); let status = page.content.to_status_identity();
status.set_description(Some(&match response.meta.data { status.set_description(Some(message.as_ref().unwrap_or(&certificate.to_string())));
Some(data) => data.to_string(),
None => response.meta.status.to_string(),
}));
page.set_progress(0.0); page.set_progress(0.0);
page.set_title(&status.title()); page.set_title(&status.title());
redirects.replace(0); // reset redirects.replace(0); // reset
} }
error => { }
let status = page.content.to_status_failure(); Response::Failure(failure) => match failure {
Failure::Temporary(ref temporary) => match temporary {
status.set_description( Temporary::CgiError { message } |
Some(&match response.meta.data { Temporary::Default { message } |
Some(message) => message.to_string(), Temporary::ProxyError { message } |
None => error.to_string() Temporary::ServerUnavailable { message } |
}) Temporary::SlowDown { message } => {
); let status = page.content.to_status_failure();
page.set_progress(0.0); status.set_description(Some(message.as_ref().unwrap_or(&temporary.to_string())));
page.set_title(&status.title()); page.set_progress(0.0);
redirects.replace(0); // reset page.set_title(&status.title());
redirects.replace(0); // reset
if let Some(callback) = on_failure { if let Some(callback) = on_failure {
callback() callback()
}
} }
}, }
Failure::Permanent(ref permanent) => match permanent {
Permanent::BadRequest { message } |
Permanent::Default { message } |
Permanent::Gone { message } |
Permanent::NotFound { message } |
Permanent::ProxyRequestRefused { message } => {
let status = page.content.to_status_failure();
status.set_description(Some(message.as_ref().unwrap_or(&permanent.to_string())));
page.set_progress(0.0);
page.set_title(&status.title());
redirects.replace(0); // reset
if let Some(callback) = on_failure {
callback()
}
}
}
} }
} }
Err(e) => { Err(e) => {