mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-02 01:25:27 +00:00
separate handler, begin Titan input integration
This commit is contained in:
parent
c6d68e57b4
commit
6e6c038593
2 changed files with 381 additions and 334 deletions
|
|
@ -3,16 +3,14 @@ mod feature;
|
||||||
mod subject;
|
mod subject;
|
||||||
|
|
||||||
use super::Page;
|
use super::Page;
|
||||||
use crate::tool::format_bytes;
|
|
||||||
use adw::TabPage;
|
use adw::TabPage;
|
||||||
use driver::Driver;
|
use driver::Driver;
|
||||||
use feature::Feature;
|
use feature::Feature;
|
||||||
use gtk::{
|
use gtk::{
|
||||||
gio::Cancellable,
|
gio::Cancellable,
|
||||||
glib::{Uri, UriFlags},
|
glib::{Uri, UriFlags},
|
||||||
prelude::{CancellableExt, EditableExt, EntryExt, WidgetExt},
|
prelude::{CancellableExt, EditableExt, EntryExt},
|
||||||
};
|
};
|
||||||
use plurify::ns as plural;
|
|
||||||
use std::{cell::Cell, rc::Rc};
|
use std::{cell::Cell, rc::Rc};
|
||||||
use subject::Subject;
|
use subject::Subject;
|
||||||
|
|
||||||
|
|
@ -86,26 +84,7 @@ impl Client {
|
||||||
match result {
|
match result {
|
||||||
// route by scheme
|
// route by scheme
|
||||||
Ok(uri) => match uri.scheme().as_str() {
|
Ok(uri) => match uri.scheme().as_str() {
|
||||||
"gemini" => driver.gemini.handle(uri, feature, cancellable),
|
"gemini" | "titan" => driver.gemini.handle(uri, feature, cancellable),
|
||||||
"titan" => subject.page.input.set_new_titan(|data, label| {
|
|
||||||
// init data to send
|
|
||||||
const CHUNK: usize = 0x400;
|
|
||||||
let bytes_sent = 0;
|
|
||||||
let bytes_total = data.len();
|
|
||||||
|
|
||||||
// send by chunks for large content size
|
|
||||||
if bytes_total > CHUNK {
|
|
||||||
label.set_label(&format!(
|
|
||||||
"sent {}/{} {}",
|
|
||||||
format_bytes(bytes_sent),
|
|
||||||
format_bytes(bytes_total),
|
|
||||||
plural(bytes_sent, &["byte", "bytes", "bytes"])
|
|
||||||
));
|
|
||||||
} else {
|
|
||||||
label.set_visible(false);
|
|
||||||
}
|
|
||||||
todo!()
|
|
||||||
}),
|
|
||||||
scheme => {
|
scheme => {
|
||||||
// no scheme match driver, complete with failure message
|
// no scheme match driver, complete with failure message
|
||||||
let status = subject.page.content.to_status_failure();
|
let status = subject.page.content.to_status_failure();
|
||||||
|
|
@ -155,7 +134,7 @@ impl Client {
|
||||||
fn lookup(
|
fn lookup(
|
||||||
query: &str,
|
query: &str,
|
||||||
cancellable: Cancellable,
|
cancellable: Cancellable,
|
||||||
callback: impl FnOnce(Feature, Cancellable, Result<Uri, Uri>) + 'static,
|
callback: impl FnOnce(Rc<Feature>, Cancellable, Result<Uri, Uri>) + 'static,
|
||||||
) {
|
) {
|
||||||
use gtk::{
|
use gtk::{
|
||||||
gio::{NetworkAddress, Resolver},
|
gio::{NetworkAddress, Resolver},
|
||||||
|
|
@ -167,6 +146,7 @@ fn lookup(
|
||||||
const TIMEOUT: u32 = 250; // ms
|
const TIMEOUT: u32 = 250; // ms
|
||||||
|
|
||||||
let (feature, query) = Feature::parse(query.trim());
|
let (feature, query) = Feature::parse(query.trim());
|
||||||
|
let feature = Rc::new(feature);
|
||||||
|
|
||||||
match Uri::parse(query, UriFlags::NONE) {
|
match Uri::parse(query, UriFlags::NONE) {
|
||||||
Ok(uri) => callback(feature, cancellable, Ok(uri)),
|
Ok(uri) => callback(feature, cancellable, Ok(uri)),
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,9 @@
|
||||||
use super::{Feature, Subject};
|
use super::{Feature, Subject};
|
||||||
use ggemini::client::{
|
use ggemini::client::{
|
||||||
connection::response::{data::Text, meta::Status},
|
connection::response::{data::Text, meta::Status},
|
||||||
Request,
|
Client, Request,
|
||||||
};
|
};
|
||||||
use gtk::glib::{GString, UriFlags};
|
use gtk::glib::{GString, UriFlags};
|
||||||
use gtk::prelude::{EditableExt, FileExt};
|
|
||||||
use gtk::{
|
use gtk::{
|
||||||
gdk::Texture,
|
gdk::Texture,
|
||||||
gdk_pixbuf::Pixbuf,
|
gdk_pixbuf::Pixbuf,
|
||||||
|
|
@ -12,12 +11,16 @@ use gtk::{
|
||||||
glib::{Priority, Uri},
|
glib::{Priority, Uri},
|
||||||
prelude::{EntryExt, SocketClientExt},
|
prelude::{EntryExt, SocketClientExt},
|
||||||
};
|
};
|
||||||
|
use gtk::{
|
||||||
|
glib::Bytes,
|
||||||
|
prelude::{EditableExt, FileExt},
|
||||||
|
};
|
||||||
use std::{cell::Cell, path::MAIN_SEPARATOR, rc::Rc, time::Duration};
|
use std::{cell::Cell, path::MAIN_SEPARATOR, rc::Rc, time::Duration};
|
||||||
|
|
||||||
/// Multi-protocol client API for `Page` object
|
/// Multi-protocol client API for `Page` object
|
||||||
pub struct Gemini {
|
pub struct Gemini {
|
||||||
/// Should be initiated once
|
/// Should be initiated once
|
||||||
client: Rc<ggemini::Client>,
|
client: Rc<Client>,
|
||||||
/// Validate redirection count by Gemini protocol specification
|
/// Validate redirection count by Gemini protocol specification
|
||||||
redirects: Rc<Cell<usize>>,
|
redirects: Rc<Cell<usize>>,
|
||||||
/// Handle target
|
/// Handle target
|
||||||
|
|
@ -72,338 +75,402 @@ impl Gemini {
|
||||||
|
|
||||||
// Actions
|
// Actions
|
||||||
|
|
||||||
pub fn handle(&self, uri: Uri, feature: Feature, cancellable: Cancellable) {
|
pub fn handle(&self, uri: Uri, feature: Rc<Feature>, cancellable: Cancellable) {
|
||||||
self.client.request_async(
|
use ggemini::client::connection::request::*;
|
||||||
Request::gemini(uri.clone()),
|
|
||||||
Priority::DEFAULT,
|
match uri.scheme().as_str() {
|
||||||
cancellable.clone(),
|
"gemini" => handle(
|
||||||
// Search for user certificate match request
|
Request::Gemini(Gemini { uri }),
|
||||||
// * @TODO this feature does not support multi-protocol yet
|
self.client.clone(),
|
||||||
match self
|
self.subject.clone(),
|
||||||
.subject.page
|
self.redirects.clone(),
|
||||||
.profile
|
feature,
|
||||||
.identity
|
cancellable,
|
||||||
.gemini
|
),
|
||||||
.match_scope(&uri.to_string())
|
"titan" => self.subject.page.input.set_new_titan({
|
||||||
{
|
let client = self.client.clone();
|
||||||
Some(identity) => match identity.to_tls_certificate() {
|
|
||||||
Ok(certificate) => Some(certificate),
|
|
||||||
Err(_) => panic!(), // unexpected
|
|
||||||
},
|
|
||||||
None => None,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
let uri = uri.clone();
|
|
||||||
let subject = self.subject.clone();
|
let subject = self.subject.clone();
|
||||||
let redirects = self.redirects.clone();
|
let redirects = self.redirects.clone();
|
||||||
move |result| match result {
|
move |data, _label| {
|
||||||
Ok(response) => {
|
handle(
|
||||||
match response.meta.status {
|
Request::Titan(Titan {
|
||||||
// https://geminiprotocol.net/docs/protocol-specification.gmi#input-expected
|
uri: uri.clone(),
|
||||||
Status::Input | Status::SensitiveInput => {
|
data: Bytes::from(data),
|
||||||
let title = match response.meta.data {
|
mime: None, // @TODO
|
||||||
Some(data) => data.to_string(),
|
token: None, // @TODO
|
||||||
None => Status::Input.to_string(),
|
}),
|
||||||
};
|
client.clone(),
|
||||||
if matches!(response.meta.status, Status::SensitiveInput) {
|
subject.clone(),
|
||||||
subject.page.input.set_new_sensitive(
|
redirects.clone(),
|
||||||
subject.page.tab_action.clone(),
|
feature.clone(),
|
||||||
uri,
|
cancellable.clone(),
|
||||||
Some(&title),
|
)
|
||||||
Some(1024),
|
// init data to send
|
||||||
);
|
/* @TODO
|
||||||
} else {
|
use crate::tool::format_bytes;
|
||||||
subject.page.input.set_new_response(
|
use plurify::ns as plural;
|
||||||
subject.page.tab_action.clone(),
|
|
||||||
uri,
|
const CHUNK: usize = 0x400;
|
||||||
Some(&title),
|
let bytes_sent = 0;
|
||||||
Some(1024),
|
let bytes_total = data.len();
|
||||||
);
|
|
||||||
}
|
// send by chunks for large content size
|
||||||
subject.page.title.replace(title.into());
|
if bytes_total > CHUNK {
|
||||||
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
label.set_label(&format!(
|
||||||
subject.tab_page.set_loading(false);
|
"sent {}/{} {}",
|
||||||
|
format_bytes(bytes_sent),
|
||||||
|
format_bytes(bytes_total),
|
||||||
|
plural(bytes_sent, &["byte", "bytes", "bytes"])
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
label.set_visible(false);
|
||||||
|
}
|
||||||
|
todo!()*/
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
_ => panic!(), // unexpected
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle(
|
||||||
|
request: Request,
|
||||||
|
client: Rc<Client>,
|
||||||
|
subject: Rc<Subject>,
|
||||||
|
redirects: Rc<Cell<usize>>,
|
||||||
|
feature: Rc<Feature>,
|
||||||
|
cancellable: Cancellable,
|
||||||
|
) {
|
||||||
|
let uri = request.uri().clone();
|
||||||
|
client.request_async(
|
||||||
|
request,
|
||||||
|
Priority::DEFAULT,
|
||||||
|
cancellable.clone(),
|
||||||
|
// Search for user certificate match request
|
||||||
|
// * @TODO this feature does not support multi-protocol yet
|
||||||
|
match subject
|
||||||
|
.page
|
||||||
|
.profile
|
||||||
|
.identity
|
||||||
|
.gemini
|
||||||
|
.match_scope(&uri.to_string())
|
||||||
|
{
|
||||||
|
Some(identity) => match identity.to_tls_certificate() {
|
||||||
|
Ok(certificate) => Some(certificate),
|
||||||
|
Err(_) => panic!(), // unexpected
|
||||||
|
},
|
||||||
|
None => None,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
let subject = subject.clone();
|
||||||
|
let redirects = redirects.clone();
|
||||||
|
move |result| match result {
|
||||||
|
Ok(response) => {
|
||||||
|
match response.meta.status {
|
||||||
|
// https://geminiprotocol.net/docs/protocol-specification.gmi#input-expected
|
||||||
|
Status::Input | Status::SensitiveInput => {
|
||||||
|
let title = match response.meta.data {
|
||||||
|
Some(data) => data.to_string(),
|
||||||
|
None => Status::Input.to_string(),
|
||||||
|
};
|
||||||
|
if matches!(response.meta.status, Status::SensitiveInput) {
|
||||||
|
subject.page.input.set_new_sensitive(
|
||||||
|
subject.page.tab_action.clone(),
|
||||||
|
uri,
|
||||||
|
Some(&title),
|
||||||
|
Some(1024),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
subject.page.input.set_new_response(
|
||||||
|
subject.page.tab_action.clone(),
|
||||||
|
uri,
|
||||||
|
Some(&title),
|
||||||
|
Some(1024),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
// https://geminiprotocol.net/docs/protocol-specification.gmi#status-20
|
subject.page.title.replace(title.into());
|
||||||
Status::Success => match feature {
|
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
||||||
Feature::Download => {
|
subject.tab_page.set_loading(false);
|
||||||
// Init download widget
|
}
|
||||||
let status = subject.page.content.to_status_download(
|
// https://geminiprotocol.net/docs/protocol-specification.gmi#status-20
|
||||||
uri_to_title(&uri).trim_matches(MAIN_SEPARATOR), // grab default filename from base URI,
|
Status::Success => match *feature {
|
||||||
// format FS entities
|
Feature::Download => {
|
||||||
&cancellable,
|
// Init download widget
|
||||||
{
|
let status = subject.page.content.to_status_download(
|
||||||
let cancellable = cancellable.clone();
|
uri_to_title(&uri).trim_matches(MAIN_SEPARATOR), // grab default filename from base URI,
|
||||||
let stream = response.connection.stream();
|
// format FS entities
|
||||||
move |file, action| {
|
&cancellable,
|
||||||
match file.replace(
|
{
|
||||||
None,
|
let cancellable = cancellable.clone();
|
||||||
false,
|
let stream = response.connection.stream();
|
||||||
gtk::gio::FileCreateFlags::NONE,
|
move |file, action| {
|
||||||
Some(&cancellable),
|
match file.replace(
|
||||||
) {
|
None,
|
||||||
Ok(file_output_stream) => {
|
false,
|
||||||
// Asynchronously read [IOStream](https://docs.gtk.org/gio/class.IOStream.html)
|
gtk::gio::FileCreateFlags::NONE,
|
||||||
// to local [MemoryInputStream](https://docs.gtk.org/gio/class.MemoryInputStream.html)
|
Some(&cancellable),
|
||||||
// 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
|
Ok(file_output_stream) => {
|
||||||
ggemini::gio::file_output_stream::move_all_from_stream_async(
|
// Asynchronously read [IOStream](https://docs.gtk.org/gio/class.IOStream.html)
|
||||||
stream.clone(),
|
// to local [MemoryInputStream](https://docs.gtk.org/gio/class.MemoryInputStream.html)
|
||||||
file_output_stream,
|
// show bytes count in loading widget, validate max size for incoming data
|
||||||
cancellable.clone(),
|
// * no dependency of Gemini library here, feel free to use any other `IOStream` processor
|
||||||
Priority::DEFAULT,
|
ggemini::gio::file_output_stream::move_all_from_stream_async(
|
||||||
(
|
stream.clone(),
|
||||||
0x100000, // 1M bytes per chunk
|
file_output_stream,
|
||||||
None, // unlimited
|
cancellable.clone(),
|
||||||
0, // initial totals
|
Priority::DEFAULT,
|
||||||
),
|
(
|
||||||
(
|
0x100000, // 1M bytes per chunk
|
||||||
// on chunk
|
None, // unlimited
|
||||||
{
|
0, // initial totals
|
||||||
let action = action.clone();
|
),
|
||||||
move |_, total| {
|
(
|
||||||
action.update.activate(&format!(
|
// on chunk
|
||||||
"Received {}...",
|
{
|
||||||
crate::tool::format_bytes(total)
|
let action = action.clone();
|
||||||
|
move |_, total| {
|
||||||
|
action.update.activate(&format!(
|
||||||
|
"Received {}...",
|
||||||
|
crate::tool::format_bytes(total)
|
||||||
|
))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
// on complete
|
||||||
|
{
|
||||||
|
let action = action.clone();
|
||||||
|
move |result| match result {
|
||||||
|
Ok((_, total)) => {
|
||||||
|
action.complete.activate(&format!(
|
||||||
|
"Saved to {} ({total} bytes total)",
|
||||||
|
file.parse_name()
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
},
|
Err(e) => {
|
||||||
// on complete
|
action.cancel.activate(&e.to_string())
|
||||||
{
|
|
||||||
let action = action.clone();
|
|
||||||
move |result| match result {
|
|
||||||
Ok((_, total)) => {
|
|
||||||
action.complete.activate(&format!(
|
|
||||||
"Saved to {} ({total} bytes total)",
|
|
||||||
file.parse_name()
|
|
||||||
))
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
action.cancel.activate(&e.to_string())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
),
|
},
|
||||||
)
|
),
|
||||||
}
|
)
|
||||||
Err(e) => action.cancel.activate(&e.to_string()),
|
|
||||||
}
|
}
|
||||||
|
Err(e) => action.cancel.activate(&e.to_string()),
|
||||||
}
|
}
|
||||||
},
|
|
||||||
);
|
|
||||||
subject.page.title.replace(status.title());
|
|
||||||
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
|
||||||
subject.tab_page.set_loading(false);
|
|
||||||
},
|
|
||||||
_ => match response.meta.mime {
|
|
||||||
Some(mime) => match mime.as_str() {
|
|
||||||
"text/gemini" => Text::from_stream_async(
|
|
||||||
response.connection.stream(),
|
|
||||||
Priority::DEFAULT,
|
|
||||||
cancellable.clone(),
|
|
||||||
move |result| match result {
|
|
||||||
Ok(text) => {
|
|
||||||
let widget = if matches!(feature, Feature::Source) {
|
|
||||||
subject.page.content.to_text_source(&text.to_string())
|
|
||||||
} else {
|
|
||||||
subject.page.content.to_text_gemini(&uri, &text.to_string())
|
|
||||||
};
|
|
||||||
|
|
||||||
// Connect `TextView` widget, update `search` model
|
|
||||||
subject.page.search.set(Some(widget.text_view));
|
|
||||||
|
|
||||||
// Update page meta
|
|
||||||
subject.page.title.replace(match widget.meta.title {
|
|
||||||
Some(title) => title.into(), // @TODO
|
|
||||||
None => uri_to_title(&uri),
|
|
||||||
});
|
|
||||||
|
|
||||||
// Deactivate loading indication
|
|
||||||
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
|
||||||
subject.tab_page.set_loading(false);
|
|
||||||
|
|
||||||
// Update window components
|
|
||||||
subject.page.window_action
|
|
||||||
.find
|
|
||||||
.simple_action
|
|
||||||
.set_enabled(true);
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
let status = subject.page.content.to_status_failure();
|
|
||||||
status.set_description(Some(&e.to_string()));
|
|
||||||
subject.page.title.replace(status.title());
|
|
||||||
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
|
||||||
subject.tab_page.set_loading(false);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"image/png" | "image/gif" | "image/jpeg" | "image/webp" => {
|
|
||||||
// Final image size unknown, show loading widget
|
|
||||||
let status = subject.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 subject = subject.clone();
|
|
||||||
move |result| match result {
|
|
||||||
Ok((memory_input_stream, _)) => {
|
|
||||||
Pixbuf::from_stream_async(
|
|
||||||
&memory_input_stream,
|
|
||||||
Some(&cancellable),
|
|
||||||
move |result| {
|
|
||||||
// Process buffer data
|
|
||||||
match result {
|
|
||||||
Ok(buffer) => {
|
|
||||||
subject.page.title.replace(uri_to_title(&uri));
|
|
||||||
subject.page.content
|
|
||||||
.to_image(&Texture::for_pixbuf(&buffer));
|
|
||||||
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
|
||||||
subject.tab_page.set_loading(false);
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
let status = subject.page.content.to_status_failure();
|
|
||||||
status.set_description(Some(e.message()));
|
|
||||||
subject.page.title.replace(status.title());
|
|
||||||
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
|
||||||
subject.tab_page.set_loading(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
let status = subject.page.content.to_status_failure();
|
|
||||||
status.set_description(Some(&e.to_string()));
|
|
||||||
|
|
||||||
subject.page.title.replace(status.title());
|
|
||||||
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
|
||||||
subject.tab_page.set_loading(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
mime => {
|
|
||||||
let status = subject.page
|
|
||||||
.content
|
|
||||||
.to_status_mime(mime, Some((&subject.page.tab_action, &uri)));
|
|
||||||
status.set_description(Some(&format!("Content type `{mime}` yet not supported")));
|
|
||||||
subject.page.title.replace(status.title());
|
|
||||||
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
|
||||||
subject.tab_page.set_loading(false);
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
None => {
|
);
|
||||||
let status = subject.page.content.to_status_failure();
|
subject.page.title.replace(status.title());
|
||||||
status.set_description(Some("MIME type not found"));
|
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
||||||
subject.page.title.replace(status.title());
|
subject.tab_page.set_loading(false);
|
||||||
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
|
||||||
subject.tab_page.set_loading(false);
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
// https://geminiprotocol.net/docs/protocol-specification.gmi#status-30-temporary-redirection
|
_ => match response.meta.mime {
|
||||||
// https://geminiprotocol.net/docs/protocol-specification.gmi#status-31-permanent-redirection
|
Some(mime) => match mime.as_str() {
|
||||||
Status::PermanentRedirect | Status::Redirect => {
|
"text/gemini" => Text::from_stream_async(
|
||||||
// Expected target URL in response meta
|
response.connection.stream(),
|
||||||
match response.meta.data {
|
Priority::DEFAULT,
|
||||||
Some(data) => {
|
cancellable.clone(),
|
||||||
match uri.parse_relative(data.as_str(), UriFlags::NONE) {
|
move |result| match result {
|
||||||
Ok(target) => {
|
Ok(text) => {
|
||||||
let total = redirects.take() + 1;
|
let widget = if matches!(*feature, Feature::Source) {
|
||||||
|
subject.page.content.to_text_source(&text.to_string())
|
||||||
// Validate total redirects by protocol specification
|
|
||||||
if total > 5 {
|
|
||||||
let status = subject.page.content.to_status_failure();
|
|
||||||
status.set_description(Some("Redirection limit reached"));
|
|
||||||
subject.page.title.replace(status.title());
|
|
||||||
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
|
||||||
subject.tab_page.set_loading(false);
|
|
||||||
redirects.replace(0); // reset
|
|
||||||
|
|
||||||
// Disallow external redirection
|
|
||||||
} else if uri.scheme() != target.scheme()
|
|
||||||
|| uri.port() != target.port()
|
|
||||||
|| uri.host() != target.host() {
|
|
||||||
let status = subject.page.content.to_status_failure();
|
|
||||||
status.set_description(Some("External redirects not allowed by protocol specification"));
|
|
||||||
subject.page.title.replace(status.title());
|
|
||||||
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
|
||||||
subject.tab_page.set_loading(false);
|
|
||||||
redirects.replace(0); // reset
|
|
||||||
// Valid
|
|
||||||
} else {
|
} else {
|
||||||
if matches!(response.meta.status, Status::PermanentRedirect) {
|
subject.page.content.to_text_gemini(&uri, &text.to_string())
|
||||||
subject.page.navigation
|
};
|
||||||
.request
|
|
||||||
.widget
|
// Connect `TextView` widget, update `search` model
|
||||||
.entry
|
subject.page.search.set(Some(widget.text_view));
|
||||||
.set_text(&uri.to_string());
|
|
||||||
}
|
// Update page meta
|
||||||
redirects.replace(total);
|
subject.page.title.replace(match widget.meta.title {
|
||||||
subject.page.tab_action.load.activate(Some(&target.to_string()), false);
|
Some(title) => title.into(), // @TODO
|
||||||
}
|
None => uri_to_title(&uri),
|
||||||
|
});
|
||||||
|
|
||||||
|
// Deactivate loading indication
|
||||||
|
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
||||||
|
subject.tab_page.set_loading(false);
|
||||||
|
|
||||||
|
// Update window components
|
||||||
|
subject.page.window_action
|
||||||
|
.find
|
||||||
|
.simple_action
|
||||||
|
.set_enabled(true);
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
let status = subject.page.content.to_status_failure();
|
let status = subject.page.content.to_status_failure();
|
||||||
status.set_description(Some(&e.to_string()));
|
status.set_description(Some(&e.to_string()));
|
||||||
subject.page.title.replace(status.title());
|
subject.page.title.replace(status.title());
|
||||||
|
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
||||||
|
subject.tab_page.set_loading(false);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
),
|
||||||
|
"image/png" | "image/gif" | "image/jpeg" | "image/webp" => {
|
||||||
|
// Final image size unknown, show loading widget
|
||||||
|
let status = subject.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 subject = subject.clone();
|
||||||
|
move |result| match result {
|
||||||
|
Ok((memory_input_stream, _)) => {
|
||||||
|
Pixbuf::from_stream_async(
|
||||||
|
&memory_input_stream,
|
||||||
|
Some(&cancellable),
|
||||||
|
move |result| {
|
||||||
|
// Process buffer data
|
||||||
|
match result {
|
||||||
|
Ok(buffer) => {
|
||||||
|
subject.page.title.replace(uri_to_title(&uri));
|
||||||
|
subject.page.content
|
||||||
|
.to_image(&Texture::for_pixbuf(&buffer));
|
||||||
|
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
||||||
|
subject.tab_page.set_loading(false);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
let status = subject.page.content.to_status_failure();
|
||||||
|
status.set_description(Some(e.message()));
|
||||||
|
subject.page.title.replace(status.title());
|
||||||
|
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
||||||
|
subject.tab_page.set_loading(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
let status = subject.page.content.to_status_failure();
|
||||||
|
status.set_description(Some(&e.to_string()));
|
||||||
|
|
||||||
|
subject.page.title.replace(status.title());
|
||||||
|
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
||||||
|
subject.tab_page.set_loading(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
mime => {
|
||||||
|
let status = subject.page
|
||||||
|
.content
|
||||||
|
.to_status_mime(mime, Some((&subject.page.tab_action, &uri)));
|
||||||
|
status.set_description(Some(&format!("Content type `{mime}` yet not supported")));
|
||||||
|
subject.page.title.replace(status.title());
|
||||||
|
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
||||||
|
subject.tab_page.set_loading(false);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
let status = subject.page.content.to_status_failure();
|
||||||
|
status.set_description(Some("MIME type not found"));
|
||||||
|
subject.page.title.replace(status.title());
|
||||||
|
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
||||||
|
subject.tab_page.set_loading(false);
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
// 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 uri.parse_relative(data.as_str(), UriFlags::NONE) {
|
||||||
|
Ok(target) => {
|
||||||
|
let total = redirects.take() + 1;
|
||||||
|
|
||||||
|
// Validate total redirects by protocol specification
|
||||||
|
if total > 5 {
|
||||||
|
let status = subject.page.content.to_status_failure();
|
||||||
|
status.set_description(Some("Redirection limit reached"));
|
||||||
|
subject.page.title.replace(status.title());
|
||||||
|
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
||||||
|
subject.tab_page.set_loading(false);
|
||||||
|
redirects.replace(0); // reset
|
||||||
|
|
||||||
|
// Disallow external redirection
|
||||||
|
} else if uri.scheme() != target.scheme()
|
||||||
|
|| uri.port() != target.port()
|
||||||
|
|| uri.host() != target.host() {
|
||||||
|
let status = subject.page.content.to_status_failure();
|
||||||
|
status.set_description(Some("External redirects not allowed by protocol specification"));
|
||||||
|
subject.page.title.replace(status.title());
|
||||||
|
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
||||||
|
subject.tab_page.set_loading(false);
|
||||||
|
redirects.replace(0); // reset
|
||||||
|
// Valid
|
||||||
|
} else {
|
||||||
|
if matches!(response.meta.status, Status::PermanentRedirect) {
|
||||||
|
subject.page.navigation
|
||||||
|
.request
|
||||||
|
.widget
|
||||||
|
.entry
|
||||||
|
.set_text(&uri.to_string());
|
||||||
|
}
|
||||||
|
redirects.replace(total);
|
||||||
|
subject.page.tab_action.load.activate(Some(&target.to_string()), false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
Err(e) => {
|
||||||
None => {
|
let status = subject.page.content.to_status_failure();
|
||||||
let status = subject.page.content.to_status_failure();
|
status.set_description(Some(&e.to_string()));
|
||||||
status.set_description(Some("Redirection target not found"));
|
subject.page.title.replace(status.title());
|
||||||
subject.page.title.replace(status.title());
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
None => {
|
||||||
// https://geminiprotocol.net/docs/protocol-specification.gmi#status-60
|
let status = subject.page.content.to_status_failure();
|
||||||
Status::CertificateRequest |
|
status.set_description(Some("Redirection target not found"));
|
||||||
// https://geminiprotocol.net/docs/protocol-specification.gmi#status-61-certificate-not-authorized
|
subject.page.title.replace(status.title());
|
||||||
Status::CertificateUnauthorized |
|
}
|
||||||
// https://geminiprotocol.net/docs/protocol-specification.gmi#status-62-certificate-not-valid
|
|
||||||
Status::CertificateInvalid => {
|
|
||||||
let status = subject.page.content.to_status_identity();
|
|
||||||
status.set_description(Some(&match response.meta.data {
|
|
||||||
Some(data) => data.to_string(),
|
|
||||||
None => response.meta.status.to_string(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
subject.page.title.replace(status.title());
|
|
||||||
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
|
||||||
subject.tab_page.set_loading(false);
|
|
||||||
}
|
}
|
||||||
status => {
|
},
|
||||||
let _status = subject.page.content.to_status_failure();
|
// https://geminiprotocol.net/docs/protocol-specification.gmi#status-60
|
||||||
_status.set_description(Some(&format!("Undefined status code `{status}`")));
|
Status::CertificateRequest |
|
||||||
subject.page.title.replace(_status.title());
|
// https://geminiprotocol.net/docs/protocol-specification.gmi#status-61-certificate-not-authorized
|
||||||
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
Status::CertificateUnauthorized |
|
||||||
subject.tab_page.set_loading(false);
|
// https://geminiprotocol.net/docs/protocol-specification.gmi#status-62-certificate-not-valid
|
||||||
},
|
Status::CertificateInvalid => {
|
||||||
|
let status = subject.page.content.to_status_identity();
|
||||||
|
status.set_description(Some(&match response.meta.data {
|
||||||
|
Some(data) => data.to_string(),
|
||||||
|
None => response.meta.status.to_string(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
subject.page.title.replace(status.title());
|
||||||
|
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
||||||
|
subject.tab_page.set_loading(false);
|
||||||
}
|
}
|
||||||
|
status => {
|
||||||
|
let _status = subject.page.content.to_status_failure();
|
||||||
|
_status.set_description(Some(&format!("Undefined status code `{status}`")));
|
||||||
|
subject.page.title.replace(_status.title());
|
||||||
|
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
||||||
|
subject.tab_page.set_loading(false);
|
||||||
|
},
|
||||||
}
|
}
|
||||||
Err(e) => {
|
|
||||||
let status = subject.page.content.to_status_failure();
|
|
||||||
status.set_description(Some(&e.to_string()));
|
|
||||||
subject.page.title.replace(status.title());
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
},
|
Err(e) => {
|
||||||
)
|
let status = subject.page.content.to_status_failure();
|
||||||
}
|
status.set_description(Some(&e.to_string()));
|
||||||
|
subject.page.title.replace(status.title());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helper function, extract readable title from [Uri](https://docs.gtk.org/glib/struct.Uri.html)
|
/// Helper function, extract readable title from [Uri](https://docs.gtk.org/glib/struct.Uri.html)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue