mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-02 17:45:28 +00:00
hide input forms on handle begin
This commit is contained in:
parent
0023f6f299
commit
9ba99417ae
1 changed files with 318 additions and 311 deletions
|
|
@ -175,331 +175,338 @@ fn handle(
|
||||||
{
|
{
|
||||||
let subject = subject.clone();
|
let subject = subject.clone();
|
||||||
let redirects = redirects.clone();
|
let redirects = redirects.clone();
|
||||||
move |result| match result {
|
move |result| {
|
||||||
Ok(response) => {
|
// Remove input forms when redirection expected has been not applied (e.g. failure status)
|
||||||
match response.meta.status {
|
// @TODO implement input data recovery on error (it's also available before unset, but reference lost at this point)
|
||||||
// https://geminiprotocol.net/docs/protocol-specification.gmi#input-expected
|
subject.page.input.unset();
|
||||||
Status::Input | Status::SensitiveInput => {
|
|
||||||
let title = match response.meta.data {
|
// Begin result handle
|
||||||
Some(data) => data.to_string(),
|
match result {
|
||||||
None => Status::Input.to_string(),
|
Ok(response) => {
|
||||||
};
|
match response.meta.status {
|
||||||
if matches!(response.meta.status, Status::SensitiveInput) {
|
// https://geminiprotocol.net/docs/protocol-specification.gmi#input-expected
|
||||||
subject.page.input.set_new_sensitive(
|
Status::Input | Status::SensitiveInput => {
|
||||||
subject.page.item_action.clone(),
|
let title = match response.meta.data {
|
||||||
uri,
|
Some(data) => data.to_string(),
|
||||||
Some(&title),
|
None => Status::Input.to_string(),
|
||||||
Some(1024),
|
};
|
||||||
);
|
if matches!(response.meta.status, Status::SensitiveInput) {
|
||||||
} else {
|
subject.page.input.set_new_sensitive(
|
||||||
subject.page.input.set_new_response(
|
subject.page.item_action.clone(),
|
||||||
subject.page.item_action.clone(),
|
uri,
|
||||||
uri,
|
Some(&title),
|
||||||
Some(&title),
|
Some(1024),
|
||||||
Some(1024),
|
);
|
||||||
);
|
} else {
|
||||||
|
subject.page.input.set_new_response(
|
||||||
|
subject.page.item_action.clone(),
|
||||||
|
uri,
|
||||||
|
Some(&title),
|
||||||
|
Some(1024),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
||||||
|
subject.tab_page.set_loading(false);
|
||||||
|
subject.tab_page.set_title(&title);
|
||||||
|
redirects.replace(0); // reset
|
||||||
}
|
}
|
||||||
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
// https://geminiprotocol.net/docs/protocol-specification.gmi#status-20
|
||||||
subject.tab_page.set_loading(false);
|
Status::Success => match *feature {
|
||||||
subject.tab_page.set_title(&title);
|
Feature::Download => {
|
||||||
redirects.replace(0); // reset
|
// 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) => {
|
},
|
||||||
action.cancel.activate(&e.to_string())
|
// 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) => {
|
||||||
|
action.cancel.activate(&e.to_string())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
},
|
),
|
||||||
),
|
)
|
||||||
)
|
}
|
||||||
|
Err(e) => action.cancel.activate(&e.to_string()),
|
||||||
}
|
}
|
||||||
Err(e) => action.cancel.activate(&e.to_string()),
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
||||||
|
subject.tab_page.set_loading(false);
|
||||||
|
subject.tab_page.set_title(&status.title());
|
||||||
|
redirects.replace(0); // reset
|
||||||
|
},
|
||||||
|
_ => 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())
|
||||||
|
};
|
||||||
|
subject.page.search.set(Some(widget.text_view));
|
||||||
|
subject.tab_page.set_title(&match widget.meta.title {
|
||||||
|
Some(title) => title.into(), // @TODO
|
||||||
|
None => uri_to_title(&uri),
|
||||||
|
});
|
||||||
|
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
||||||
|
subject.tab_page.set_loading(false);
|
||||||
|
subject.page.window_action
|
||||||
|
.find
|
||||||
|
.simple_action
|
||||||
|
.set_enabled(true);
|
||||||
|
redirects.replace(0); // reset
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
let status = subject.page.content.to_status_failure();
|
||||||
|
status.set_description(Some(&e.to_string()));
|
||||||
|
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
||||||
|
subject.tab_page.set_loading(false);
|
||||||
|
subject.tab_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 = 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| {
|
||||||
|
match result {
|
||||||
|
Ok(buffer) => {
|
||||||
|
subject.tab_page.set_title(&uri_to_title(&uri));
|
||||||
|
subject.page.content.to_image(&Texture::for_pixbuf(&buffer));
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
let status = subject.page.content.to_status_failure();
|
||||||
|
status.set_description(Some(e.message()));
|
||||||
|
subject.tab_page.set_title(&status.title());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
||||||
|
subject.tab_page.set_loading(false);
|
||||||
|
redirects.replace(0); // reset
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
let status = subject.page.content.to_status_failure();
|
||||||
|
status.set_description(Some(&e.to_string()));
|
||||||
|
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
||||||
|
subject.tab_page.set_loading(false);
|
||||||
|
subject.tab_page.set_title(&status.title());
|
||||||
|
redirects.replace(0); // reset
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
mime => {
|
||||||
|
let status = subject.page
|
||||||
|
.content
|
||||||
|
.to_status_mime(mime, Some((&subject.page.item_action, &uri)));
|
||||||
|
status.set_description(Some(&format!("Content type `{mime}` yet not supported")));
|
||||||
|
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
||||||
|
subject.tab_page.set_loading(false);
|
||||||
|
subject.tab_page.set_title(&status.title());
|
||||||
|
redirects.replace(0); // reset
|
||||||
|
},
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
let status = subject.page.content.to_status_failure();
|
||||||
|
status.set_description(Some("MIME type not found"));
|
||||||
|
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
||||||
|
subject.tab_page.set_loading(false);
|
||||||
|
subject.tab_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 uri.parse_relative(data.as_str(), UriFlags::NONE) {
|
||||||
|
Ok(absolute) => {
|
||||||
|
// Base donor scheme could be `titan`, rewrite new relative links resolved with `gemini`
|
||||||
|
// otherwise, keep original scheme to handle external redirect rules properly
|
||||||
|
// * in this case, `titan` scheme redirects unexpected
|
||||||
|
let scheme = absolute.scheme();
|
||||||
|
let target = Uri::build(
|
||||||
|
UriFlags::NONE,
|
||||||
|
&if "titan" == scheme {
|
||||||
|
scheme.replace("titan", "gemini")
|
||||||
|
} else {
|
||||||
|
scheme.to_string()
|
||||||
|
},
|
||||||
|
absolute.userinfo().as_deref(),
|
||||||
|
absolute.host().as_deref(),
|
||||||
|
absolute.port(),
|
||||||
|
absolute.path().as_str(),
|
||||||
|
absolute.query().as_deref(),
|
||||||
|
absolute.fragment().as_deref(),
|
||||||
|
);
|
||||||
|
// Increase client redirection counter
|
||||||
|
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.navigation.request.widget.entry.set_progress_fraction(0.0);
|
||||||
|
subject.tab_page.set_loading(false);
|
||||||
|
subject.tab_page.set_title(&status.title());
|
||||||
|
redirects.replace(0); // reset
|
||||||
|
|
||||||
|
// Disallow external redirection by protocol restrictions
|
||||||
|
} else if "gemini" != 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.navigation.request.widget.entry.set_progress_fraction(0.0);
|
||||||
|
subject.tab_page.set_loading(false);
|
||||||
|
subject.tab_page.set_title(&status.title());
|
||||||
|
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.item_action.load.activate(Some(&target.to_string()), false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
Err(e) => {
|
||||||
);
|
let status = subject.page.content.to_status_failure();
|
||||||
|
status.set_description(Some(&e.to_string()));
|
||||||
|
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
||||||
|
subject.tab_page.set_loading(false);
|
||||||
|
subject.tab_page.set_title(&status.title());
|
||||||
|
redirects.replace(0); // reset
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
let status = subject.page.content.to_status_failure();
|
||||||
|
status.set_description(Some("Redirection target not found"));
|
||||||
|
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
||||||
|
subject.tab_page.set_loading(false);
|
||||||
|
subject.tab_page.set_title(&status.title());
|
||||||
|
redirects.replace(0); // reset
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
// https://geminiprotocol.net/docs/protocol-specification.gmi#status-60
|
||||||
|
Status::CertificateRequest |
|
||||||
|
// https://geminiprotocol.net/docs/protocol-specification.gmi#status-61-certificate-not-authorized
|
||||||
|
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.navigation.request.widget.entry.set_progress_fraction(0.0);
|
||||||
|
subject.tab_page.set_loading(false);
|
||||||
|
subject.tab_page.set_title(&status.title());
|
||||||
|
redirects.replace(0); // reset
|
||||||
|
}
|
||||||
|
error => {
|
||||||
|
let status = subject.page.content.to_status_failure();
|
||||||
|
status.set_description(Some(&error.to_string()));
|
||||||
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
||||||
subject.tab_page.set_loading(false);
|
subject.tab_page.set_loading(false);
|
||||||
subject.tab_page.set_title(&status.title());
|
subject.tab_page.set_title(&status.title());
|
||||||
redirects.replace(0); // reset
|
redirects.replace(0); // reset
|
||||||
},
|
},
|
||||||
_ => 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())
|
|
||||||
};
|
|
||||||
subject.page.search.set(Some(widget.text_view));
|
|
||||||
subject.tab_page.set_title(&match widget.meta.title {
|
|
||||||
Some(title) => title.into(), // @TODO
|
|
||||||
None => uri_to_title(&uri),
|
|
||||||
});
|
|
||||||
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
|
||||||
subject.tab_page.set_loading(false);
|
|
||||||
subject.page.window_action
|
|
||||||
.find
|
|
||||||
.simple_action
|
|
||||||
.set_enabled(true);
|
|
||||||
redirects.replace(0); // reset
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
let status = subject.page.content.to_status_failure();
|
|
||||||
status.set_description(Some(&e.to_string()));
|
|
||||||
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
|
||||||
subject.tab_page.set_loading(false);
|
|
||||||
subject.tab_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 = 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| {
|
|
||||||
match result {
|
|
||||||
Ok(buffer) => {
|
|
||||||
subject.tab_page.set_title(&uri_to_title(&uri));
|
|
||||||
subject.page.content.to_image(&Texture::for_pixbuf(&buffer));
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
let status = subject.page.content.to_status_failure();
|
|
||||||
status.set_description(Some(e.message()));
|
|
||||||
subject.tab_page.set_title(&status.title());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
|
||||||
subject.tab_page.set_loading(false);
|
|
||||||
redirects.replace(0); // reset
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
let status = subject.page.content.to_status_failure();
|
|
||||||
status.set_description(Some(&e.to_string()));
|
|
||||||
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
|
||||||
subject.tab_page.set_loading(false);
|
|
||||||
subject.tab_page.set_title(&status.title());
|
|
||||||
redirects.replace(0); // reset
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
mime => {
|
|
||||||
let status = subject.page
|
|
||||||
.content
|
|
||||||
.to_status_mime(mime, Some((&subject.page.item_action, &uri)));
|
|
||||||
status.set_description(Some(&format!("Content type `{mime}` yet not supported")));
|
|
||||||
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
|
||||||
subject.tab_page.set_loading(false);
|
|
||||||
subject.tab_page.set_title(&status.title());
|
|
||||||
redirects.replace(0); // reset
|
|
||||||
},
|
|
||||||
},
|
|
||||||
None => {
|
|
||||||
let status = subject.page.content.to_status_failure();
|
|
||||||
status.set_description(Some("MIME type not found"));
|
|
||||||
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
|
||||||
subject.tab_page.set_loading(false);
|
|
||||||
subject.tab_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 uri.parse_relative(data.as_str(), UriFlags::NONE) {
|
|
||||||
Ok(absolute) => {
|
|
||||||
// Base donor scheme could be `titan`, rewrite new relative links resolved with `gemini`
|
|
||||||
// otherwise, keep original scheme to handle external redirect rules properly
|
|
||||||
// * in this case, `titan` scheme redirects unexpected
|
|
||||||
let scheme = absolute.scheme();
|
|
||||||
let target = Uri::build(
|
|
||||||
UriFlags::NONE,
|
|
||||||
&if "titan" == scheme {
|
|
||||||
scheme.replace("titan", "gemini")
|
|
||||||
} else {
|
|
||||||
scheme.to_string()
|
|
||||||
},
|
|
||||||
absolute.userinfo().as_deref(),
|
|
||||||
absolute.host().as_deref(),
|
|
||||||
absolute.port(),
|
|
||||||
absolute.path().as_str(),
|
|
||||||
absolute.query().as_deref(),
|
|
||||||
absolute.fragment().as_deref(),
|
|
||||||
);
|
|
||||||
// Increase client redirection counter
|
|
||||||
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.navigation.request.widget.entry.set_progress_fraction(0.0);
|
|
||||||
subject.tab_page.set_loading(false);
|
|
||||||
subject.tab_page.set_title(&status.title());
|
|
||||||
redirects.replace(0); // reset
|
|
||||||
|
|
||||||
// Disallow external redirection by protocol restrictions
|
|
||||||
} else if "gemini" != 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.navigation.request.widget.entry.set_progress_fraction(0.0);
|
|
||||||
subject.tab_page.set_loading(false);
|
|
||||||
subject.tab_page.set_title(&status.title());
|
|
||||||
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.item_action.load.activate(Some(&target.to_string()), false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
let status = subject.page.content.to_status_failure();
|
|
||||||
status.set_description(Some(&e.to_string()));
|
|
||||||
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
|
||||||
subject.tab_page.set_loading(false);
|
|
||||||
subject.tab_page.set_title(&status.title());
|
|
||||||
redirects.replace(0); // reset
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
let status = subject.page.content.to_status_failure();
|
|
||||||
status.set_description(Some("Redirection target not found"));
|
|
||||||
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
|
||||||
subject.tab_page.set_loading(false);
|
|
||||||
subject.tab_page.set_title(&status.title());
|
|
||||||
redirects.replace(0); // reset
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
// https://geminiprotocol.net/docs/protocol-specification.gmi#status-60
|
|
||||||
Status::CertificateRequest |
|
|
||||||
// https://geminiprotocol.net/docs/protocol-specification.gmi#status-61-certificate-not-authorized
|
|
||||||
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.navigation.request.widget.entry.set_progress_fraction(0.0);
|
|
||||||
subject.tab_page.set_loading(false);
|
|
||||||
subject.tab_page.set_title(&status.title());
|
|
||||||
redirects.replace(0); // reset
|
|
||||||
}
|
}
|
||||||
error => {
|
|
||||||
let status = subject.page.content.to_status_failure();
|
|
||||||
status.set_description(Some(&error.to_string()));
|
|
||||||
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
|
||||||
subject.tab_page.set_loading(false);
|
|
||||||
subject.tab_page.set_title(&status.title());
|
|
||||||
redirects.replace(0); // reset
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
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.navigation.request.widget.entry.set_progress_fraction(0.0);
|
||||||
subject.page.navigation.request.widget.entry.set_progress_fraction(0.0);
|
subject.tab_page.set_loading(false);
|
||||||
subject.tab_page.set_loading(false);
|
subject.tab_page.set_title(&status.title());
|
||||||
subject.tab_page.set_title(&status.title());
|
redirects.replace(0); // reset
|
||||||
redirects.replace(0); // reset
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue