diff options
| author | Fuwn <[email protected]> | 2023-05-22 06:34:28 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-05-22 06:34:28 +0000 |
| commit | a78c5700084a6de66ff67d9aa9c96cb65ce7e712 (patch) | |
| tree | 4a8b3287bfa6220aefe5eaa933f768815e2e0eaa /src | |
| parent | refactor(src): rename files (diff) | |
| download | september-a78c5700084a6de66ff67d9aa9c96cb65ce7e712.tar.xz september-a78c5700084a6de66ff67d9aa9c96cb65ce7e712.zip | |
deps(gmi): finally drop gmi
Diffstat (limited to 'src')
| -rw-r--r-- | src/html.rs | 46 | ||||
| -rw-r--r-- | src/response.rs | 26 | ||||
| -rw-r--r-- | src/url.rs | 4 |
3 files changed, 43 insertions, 33 deletions
diff --git a/src/html.rs b/src/html.rs index 2026272..0c88b2a 100644 --- a/src/html.rs +++ b/src/html.rs @@ -18,12 +18,12 @@ use std::env::var; use germ::ast::Node; -use gmi::url::Url; +use url::Url; -fn link_from_host_href(url: &Url, href: &str) -> String { - format!( +fn link_from_host_href(url: &Url, href: &str) -> Option<String> { + Some(format!( "gemini://{}{}{}", - url.authority.host, + url.domain()?, { if href.starts_with('/') { "" @@ -32,17 +32,17 @@ fn link_from_host_href(url: &Url, href: &str) -> String { } }, href - ) + )) } #[allow(clippy::too_many_lines)] pub fn from_gemini( - response: &gmi::protocol::Response, + response: &germ::request::Response, url: &Url, is_proxy: bool, -) -> (String, String) { +) -> Option<(String, String)> { let ast_tree = - germ::ast::Ast::from_string(String::from_utf8_lossy(&response.data)); + germ::ast::Ast::from_string(response.content().clone().unwrap_or_default()); let ast = ast_tree.inner(); let mut html = String::new(); let mut title = String::new(); @@ -59,7 +59,7 @@ pub fn from_gemini( } else if !href.starts_with("gemini://") && !href.starts_with('/') { href = format!("./{href}"); } else if href.starts_with('/') || !href.contains("://") { - href = link_from_host_href(url, &href); + href = link_from_host_href(url, &href)?; } if var("PROXY_BY_DEFAULT") @@ -77,13 +77,18 @@ pub fn from_gemini( .collect::<Vec<_>>() .first() .unwrap() - != &url.authority.host.as_str() + != &url.host().unwrap().to_string().as_str() { href = format!("/proxy/{}", href.trim_start_matches("gemini://")); } else { - href = href - .trim_start_matches("gemini://") - .replace(&url.authority.host, ""); + href = href.trim_start_matches("gemini://").replace( + &if let Some(host) = url.host() { + host.to_string() + } else { + return None; + }, + "", + ); } } @@ -91,7 +96,7 @@ pub fn from_gemini( let mut keeps = keeps.split(','); if (href.starts_with('/') || !href.contains("://")) && !surface { - let temporary_href = link_from_host_href(url, &href); + let temporary_href = link_from_host_href(url, &href)?; if keeps.any(|k| k == &*temporary_href) { href = temporary_href; @@ -100,12 +105,17 @@ pub fn from_gemini( } if let Ok(keeps) = var("KEEP_GEMINI_DOMAIN") { + let host = if let Some(host) = url.host() { + host.to_string() + } else { + return None; + }; + if (href.starts_with('/') - || !href.contains("://") - && keeps.split(',').any(|k| k == &*url.authority.host)) + || !href.contains("://") && keeps.split(',').any(|k| k == &*host)) && !surface { - href = link_from_host_href(url, &href); + href = link_from_host_href(url, &href)?; } } @@ -149,5 +159,5 @@ pub fn from_gemini( } } - (title, html) + Some((title, html)) } diff --git a/src/response.rs b/src/response.rs index 83bdc1e..7c71f55 100644 --- a/src/response.rs +++ b/src/response.rs @@ -68,15 +68,15 @@ For example: to proxy "gemini://fuwn.me/uptime", visit "/proxy/fuwn.me/uptime".< }; // Make a request to get Gemini content and time it. let mut timer = Instant::now(); - let mut response = match gmi::request::make_request(&url) { + let mut response = match germ::request::request(&url) { Ok(response) => response, Err(e) => { return Ok(HttpResponse::Ok().body(e.to_string())); } }; - if response.data.is_empty() { - response = match gmi::request::make_request(&match make_url( + if response.content().is_some() { + response = match germ::request::request(&match make_url( req.path(), true, &mut is_proxy, @@ -100,7 +100,7 @@ For example: to proxy "gemini://fuwn.me/uptime", visit "/proxy/fuwn.me/uptime".< } let response_time_taken = timer.elapsed(); - let meta = germ::meta::Meta::from_string(response.meta.clone()); + let meta = germ::meta::Meta::from_string(response.meta().to_string()); let charset = meta .parameters() .get("charset") @@ -126,12 +126,13 @@ For example: to proxy "gemini://fuwn.me/uptime", visit "/proxy/fuwn.me/uptime".< } ) }; - let gemini_html = crate::html::from_gemini(&response, &url, is_proxy); + let gemini_html = + crate::html::from_gemini(&response, &url, is_proxy).unwrap(); let gemini_title = gemini_html.0; let convert_time_taken = timer.elapsed(); if is_raw { - html_context.push_str(&String::from_utf8_lossy(&response.data)); + html_context.push_str(&response.content().clone().unwrap_or_default()); return Ok( HttpResponse::Ok() @@ -187,11 +188,11 @@ For example: to proxy "gemini://fuwn.me/uptime", visit "/proxy/fuwn.me/uptime".< html_context.push_str(&format!("<title>{gemini_title}</title>")); html_context.push_str("</head><body>"); - match response.status { - gmi::protocol::StatusCode::Success(_) => { + match response.status() { + germ::request::Status::Success => { html_context.push_str(&gemini_html.1); } - _ => html_context.push_str(&format!("<p>{}</p>", response.meta)), + _ => html_context.push_str(&format!("<p>{}</p>", response.meta())), } // Add proxy information to footer of HTML response @@ -211,8 +212,8 @@ For example: to proxy "gemini://fuwn.me/uptime", visit "/proxy/fuwn.me/uptime".< <a href=\"https://github.com/gemrest/september{}\">September ({})</a>.</p> </details></body></html>", url, - response.status, - response.meta, + response.status(), + response.meta(), response_time_taken.as_nanos() as f64 / 1_000_000.0, convert_time_taken.as_nanos() as f64 / 1_000_000.0, format_args!("/tree/{}", env!("VERGEN_GIT_SHA")), @@ -222,8 +223,7 @@ For example: to proxy "gemini://fuwn.me/uptime", visit "/proxy/fuwn.me/uptime".< if let Ok(plain_texts) = var("PLAIN_TEXT_ROUTE") { if plain_texts.split(',').any(|r| r == req.path()) { return Ok( - HttpResponse::Ok() - .body(String::from_utf8_lossy(&response.data).to_string()), + HttpResponse::Ok().body(response.content().clone().unwrap_or_default()), ); } } @@ -15,7 +15,7 @@ // Copyright (C) 2022-2023 Fuwn <[email protected]> // SPDX-License-Identifier: GPL-3.0-only -use gmi::url::Url; +use url::Url; pub fn make( path: &str, @@ -23,7 +23,7 @@ pub fn make( is_proxy: &mut bool, is_raw: &mut bool, is_nocss: &mut bool, -) -> Result<Url, gmi::url::UrlParseError> { +) -> Result<Url, url::ParseError> { Ok( match Url::try_from(&*if path.starts_with("/proxy") { *is_proxy = true; |