1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
mod driver;
mod feature;
use super::{Page, Profile};
use driver::Driver;
use feature::Feature;
use gtk::{
gio::Cancellable,
glib::{Uri, UriFlags},
prelude::CancellableExt,
};
use std::{cell::Cell, rc::Rc};
/// Multi-protocol client API for tab `Item`
pub struct Client {
cancellable: Cell<Cancellable>,
driver: Rc<Driver>,
page: Rc<Page>,
profile: Rc<Profile>,
}
impl Client {
// Constructors
/// Create new `Self`
pub fn init(profile: &Rc<Profile>, page: &Rc<Page>) -> Self {
Self {
cancellable: Cell::new(Cancellable::new()),
driver: Rc::new(Driver::build(page)),
page: page.clone(),
profile: profile.clone(),
}
}
// Actions
/// Route tab item `request` to protocol driver
/// * or `navigation` entry if the value not provided
pub fn handle(&self, request: &str, is_snap_history: bool, is_redirect: bool) {
self.page.escape();
// Initially disable find action
self.page
.window_action
.find
.simple_action
.set_enabled(false);
// Reset widgets
self.page.input.unset();
self.page.search.unset();
self.page.set_title("Loading..");
self.page.set_progress(0.1);
self.page
.navigation
.request
.info
.borrow_mut()
.reset(!is_redirect)
.add_event("New request".to_string());
// run async resolver to detect Uri, scheme-less host, or search query
lookup(&self.profile, request, self.cancellable(), {
let driver = self.driver.clone();
let page = self.page.clone();
move |feature, cancellable, result| {
match result {
// route by scheme
Ok(uri) => match uri.scheme().as_str() {
"file" => driver
.file
.handle(uri, feature, cancellable, is_snap_history),
"gemini" | "titan" => {
driver
.gemini
.handle(uri, feature, cancellable, is_snap_history)
}
"nex" => driver
.nex
.handle(uri, feature, cancellable, is_snap_history),
scheme => {
// no scheme match driver, complete with failure message
let status = page.content.to_status_failure();
status.set_description(Some(&format!(
"Scheme `{scheme}` yet not supported"
)));
page.set_title(&status.title());
page.set_progress(0.0);
}
},
// begin redirection to new address suggested
Err(query) => {
page.item_action
.load
.activate(Some(&query), is_snap_history, true)
}
}
}
})
}
/// Get new [Cancellable](https://docs.gtk.org/gio/class.Cancellable.html) by cancel previous one
fn cancellable(&self) -> Cancellable {
// Init new Cancellable
let cancellable = Cancellable::new();
// Replace by cancel previous operations
let previous = self.cancellable.replace(cancellable.clone());
if !previous.is_cancelled() {
previous.cancel();
}
// Done
cancellable
}
}
/// Create request using async DNS resolver (slow method)
/// * return suggestion [Uri](https://docs.gtk.org/glib/struct.Uri.html) on failure (to handle as redirect)
fn lookup(
profile: &Rc<Profile>,
query: &str,
cancellable: Cancellable,
callback: impl FnOnce(Rc<Feature>, Cancellable, Result<Uri, String>) + 'static,
) {
use gtk::{
gio::{NetworkAddress, Resolver},
prelude::{NetworkAddressExt, ResolverExt},
};
const DEFAULT_SCHEME: &str = "gemini";
const DEFAULT_PORT: u16 = 1965;
const TIMEOUT: u32 = 1000; // ms @TODO optional
let (feature, query) = Feature::parse(query.trim());
let feature = Rc::new(feature);
match Uri::parse(query, UriFlags::NONE) {
Ok(uri) => callback(feature, cancellable, Ok(uri)),
Err(_) => {
// try default scheme suggestion
let suggestion = format!(
"{DEFAULT_SCHEME}://{}",
query
.strip_prefix(&format!("{DEFAULT_SCHEME}://"))
.unwrap_or(query)
);
let resolver = Resolver::default();
resolver.set_timeout(TIMEOUT);
match NetworkAddress::parse_uri(&suggestion, DEFAULT_PORT) {
Ok(connectable) => resolver.lookup_by_name_async(
&connectable.hostname(),
Some(&cancellable.clone()),
{
let profile = profile.clone();
let query = query.to_owned();
move |resolve| {
callback(
feature,
cancellable,
if resolve.is_ok() {
Err(match Uri::parse(&suggestion, UriFlags::NONE) {
Ok(uri) => uri,
Err(_) => search(&profile, &query),
}
.to_string())
} else {
const FILE_SCHEME: &str = "file://";
Err(
// try autocomplete scheme if the request match local filename
if !query.starts_with(FILE_SCHEME)
&& std::path::Path::new(&query).exists()
{
format!("{FILE_SCHEME}{query}")
} else {
search(&profile, &query).to_string()
},
)
},
)
}
},
),
Err(_) => callback(
feature,
cancellable,
Err(search(profile, query).to_string()),
),
}
}
}
}
/// Convert `query` to default search provider [Uri](https://docs.gtk.org/glib/struct.Uri.html)
fn search(profile: &Profile, query: &str) -> Uri {
Uri::parse(
&format!(
"{}?{}",
profile.search.default().unwrap().query, // @TODO handle
Uri::escape_string(query, None, false)
),
UriFlags::NONE,
)
.unwrap() // @TODO handle or skip extra URI parse by String return
}