From a5f82a1bca8586d264f2570d312a03536a45c430 Mon Sep 17 00:00:00 2001 From: Julien Blanchard Date: Mon, 12 Aug 2019 17:04:53 +0200 Subject: [PATCH] Handle errors ans statuses Gone, Certificate issues, Timeout, Redirections --- src/main.rs | 106 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 62 insertions(+), 44 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5c0ca56..fbf4dc5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,8 +24,7 @@ use link::Link; mod content; mod history; -const HELP: &str = " -Welcome to Asuka Gemini browser! +const HELP: &str = "Welcome to Asuka Gemini browser! Press g to visit an URL Press h to show/hide history @@ -70,9 +69,7 @@ fn prompt_for_url(s: &mut Cursive) { .title("Enter URL") // Padding is (left, right, top, bottom) .padding((1, 1, 1, 0)) - .content(EditView::new() - .on_submit(goto_url) - .fixed_width(20)) + .content(EditView::new().on_submit(goto_url).fixed_width(20)) .with_id("url_popup"), ); } @@ -91,15 +88,15 @@ fn show_history(s: &mut Cursive) { // Hide popup when pressing h on an opened popup if s.find_id::("history_popup").is_some() { s.pop_layer(); - return + return; } let mut select = SelectView::new(); - for url in history::content() { + for url in history::content() { let url_s = url.as_str(); select.add_item_str(url_s); - }; + } select.set_on_submit(|s, link| { s.pop_layer(); @@ -139,48 +136,69 @@ fn draw_content(s: &mut Cursive, url: Url, content: String) { let mut main_view = s.find_id::("main").unwrap(); let mut container = s.find_id::("container").unwrap(); + // handle response status + if let Some(status_line) = content.lines().next() { + if let Ok(status) = Status::from_str(status_line) { + match status { + Status::Success(_meta) => {} + Status::Gone(_meta) => { + s.add_layer(Dialog::info("Sorry page is gone.")); + return; + } + Status::RedirectTemporary(new_url) | Status::RedirectPermanent(new_url) => { + follow_link(s, &new_url) + } + Status::TransientCertificateRequired(_meta) + | Status::AuthorisedCertificatedRequired(_meta) => { + s.add_layer(Dialog::info( + "You need a valid certificate to access this page.", + )); + return; + } + other_status => { + s.add_layer(Dialog::info(format!("ERROR: {:?}", other_status))); + return; + } + } + } + } + + // set title and clear old content container.set_title(url.as_str()); main_view.clear(); - // handle statuses here - match Status::from_str(content.lines().next().unwrap()) { - Ok(_status) => {} - Err(_) => {} - } - + // draw new content lines for line in content.lines().skip(1) { match Link::from_str(line) { - Ok(link) => { - match link { - Link::Http(_url, label) => { - let mut formatted = StyledString::new(); - let www_label = format!("[WWW] {}", label); - formatted.append(StyledString::styled(www_label, Effect::Italic)); - - main_view.add_item(formatted, String::from("0")) - }, - Link::Gopher(_url, label) => { - let mut formatted = StyledString::new(); - let gopher_label = format!("[Gopher] {}", label); - formatted.append(StyledString::styled(gopher_label, Effect::Italic)); - - main_view.add_item(formatted, String::from("0")) - }, - Link::Gemini(url, label) => { - let mut formatted = StyledString::new(); - formatted.append(StyledString::styled(label, Effect::Underline)); - - main_view.add_item(formatted, url.to_string()) - }, - Link::Relative(url, label) => { - let mut formatted = StyledString::new(); - formatted.append(StyledString::styled(label, Effect::Underline)); - - main_view.add_item(formatted, url.to_string()) - }, - _ => () + Ok(link) => match link { + Link::Http(_url, label) => { + let mut formatted = StyledString::new(); + let www_label = format!("[WWW] {}", label); + formatted.append(StyledString::styled(www_label, Effect::Italic)); + + main_view.add_item(formatted, String::from("0")) } - } + Link::Gopher(_url, label) => { + let mut formatted = StyledString::new(); + let gopher_label = format!("[Gopher] {}", label); + formatted.append(StyledString::styled(gopher_label, Effect::Italic)); + + main_view.add_item(formatted, String::from("0")) + } + Link::Gemini(url, label) => { + let mut formatted = StyledString::new(); + formatted.append(StyledString::styled(label, Effect::Underline)); + + main_view.add_item(formatted, url.to_string()) + } + Link::Relative(url, label) => { + let mut formatted = StyledString::new(); + formatted.append(StyledString::styled(label, Effect::Underline)); + + main_view.add_item(formatted, url.to_string()) + } + Link::Unknown(_, _) => (), + }, Err(_) => main_view.add_item(str::replace(line, "\t", " "), String::from("0")), } }