pub mod configuration; use { crate::url::from_path as url_from_path, actix_web::{Error, HttpResponse}, std::{env::var, fmt::Write, time::Instant}, }; const CSS: &str = include_str!("../default.css"); #[allow(clippy::future_not_send, clippy::too_many_lines)] pub async fn default( http_request: actix_web::HttpRequest, ) -> Result { if ["/proxy", "/proxy/", "/x", "/x/", "/raw", "/raw/", "/nocss", "/nocss/"] .contains(&http_request.path()) { return Ok(HttpResponse::Ok() .content_type("text/html") .body(r"

September

This is a proxy path. Specify a Gemini URL without the protocol (gemini://) to proxy it.

To proxy gemini://fuwn.me/uptime, visit https://fuwn.me/proxy/fuwn.me/uptime.

Additionally, you may visit /raw to view the raw Gemini content, or /nocss to view the content without CSS.

")); } let mut configuration = configuration::Configuration::new(); let url = match url_from_path( &format!("{}{}", http_request.path(), { if !http_request.query_string().is_empty() || http_request.uri().to_string().ends_with('?') { format!("?{}", http_request.query_string()) } else { String::new() } }), false, &mut configuration, ) { Ok(url) => url, Err(e) => { return Ok( HttpResponse::BadRequest() .content_type("text/plain") .body(format!("{e}")), ); } }; let mut timer = Instant::now(); let mut response = match germ::request::request(&url).await { Ok(response) => response, Err(e) => { return Ok(HttpResponse::Ok().body(e.to_string())); } }; let mut redirect_response_status = None; let mut redirect_url = None; if *response.status() == germ::request::Status::PermanentRedirect || *response.status() == germ::request::Status::TemporaryRedirect { redirect_response_status = Some(*response.status()); redirect_url = Some( url::Url::parse(&if response.meta().starts_with('/') { format!( "gemini://{}{}", url.domain().unwrap_or_default(), response.meta() ) } else { response.meta().to_string() }) .unwrap(), ); response = match germ::request::request(&redirect_url.clone().unwrap()).await { Ok(response) => response, Err(e) => { return Ok(HttpResponse::Ok().body(e.to_string())); } } } let response_time_taken = timer.elapsed(); let meta = germ::meta::Meta::from_string(response.meta().to_string()); let charset = meta .parameters() .get("charset") .map_or_else(|| "utf-8".to_string(), ToString::to_string); let language = meta.parameters().get("lang").map_or_else(String::new, ToString::to_string); timer = Instant::now(); let mut html_context = if configuration.is_raw() { String::new() } else { format!( r#""#, if language.is_empty() { String::new() } else { format!(" lang=\"{language}\"") } ) }; let gemini_html = crate::html::from_gemini(&response, &url, &configuration).unwrap(); let gemini_title = gemini_html.0; let convert_time_taken = timer.elapsed(); if configuration.is_raw() { html_context.push_str( &response.content().as_ref().map_or_else(String::default, String::clone), ); return Ok( HttpResponse::Ok() .content_type(format!("{}; charset={charset}", meta.mime())) .body(html_context), ); } if configuration.is_no_css() { html_context.push_str(&gemini_html.1); return Ok( HttpResponse::Ok() .content_type(format!("text/html; charset={}", meta.mime())) .body(html_context), ); } if let Ok(css) = var("CSS_EXTERNAL") { let stylesheets = css.split(',').filter(|s| !s.is_empty()).collect::>(); for stylesheet in stylesheets { let _ = write!( &mut html_context, "", ); } } else if !configuration.is_no_css() { let _ = write!( &mut html_context, r#""# ); if let Ok(primary) = var("PRIMARY_COLOUR") { let _ = write!( &mut html_context, "" ); } else { let _ = write!( &mut html_context, "" ); } } if let Ok(favicon) = var("FAVICON_EXTERNAL") { let _ = write!( &mut html_context, "", ); } if var("MATHJAX").unwrap_or_else(|_| "true".to_string()).to_lowercase() == "true" { html_context.push_str( r#""#, ); } if let Ok(head) = var("HEAD") { html_context.push_str(&head); } let _ = write!(&mut html_context, "{gemini_title}"); let _ = write!(&mut html_context, ""); if !http_request.path().starts_with("/proxy") { if let Ok(header) = var("HEADER") { let _ = write!( &mut html_context, "
{header}
" ); } } match response.status() { germ::request::Status::Success => { if let (Some(status), Some(url)) = (redirect_response_status, redirect_url) { let _ = write!( &mut html_context, "
This page {} redirects to {}.
", if status == germ::request::Status::PermanentRedirect { "permanently" } else { "temporarily" }, url, url ); } html_context.push_str(&gemini_html.1); } _ => { let _ = write!(&mut html_context, "

{}

", response.meta()); } } let _ = write!( &mut html_context, "
\nProxy Information
Original URL
{0}
Status Code
{} ({})
Meta
{}
\
Capsule Response Time
{} milliseconds
Gemini-to-HTML \ Time
{} milliseconds

This content has been proxied \ by September \ ({}).

", url, response.status(), i32::from(*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")), env!("VERGEN_GIT_SHA").get(0..5).unwrap_or("UNKNOWN"), ); if let Ok(plain_texts) = var("PLAIN_TEXT_ROUTE") { if plain_texts.split(',').any(|r| { path_matches_pattern(r, http_request.path()) || path_matches_pattern(r, http_request.path().trim_end_matches('/')) }) { return Ok(HttpResponse::Ok().body( response.content().as_ref().map_or_else(String::default, String::clone), )); } } Ok( HttpResponse::Ok() .content_type(format!("text/html; charset={charset}")) .body(html_context), ) } fn path_matches_pattern(pattern: &str, path: &str) -> bool { if !pattern.contains('*') { return path == pattern; } let parts: Vec<&str> = pattern.split('*').collect(); let mut position = if pattern.starts_with('*') { 0 } else { let first = parts.first().unwrap(); if !path.starts_with(first) { return false; } first.len() }; let mid_end = parts.len().saturating_sub(1); for part in &parts[1..mid_end] { if part.is_empty() { continue; } if let Some(found) = path[position..].find(part) { position += found + part.len(); } else { return false; } } if !pattern.ends_with('*') { let last = parts.last().unwrap(); if !path[position..].ends_with(last) { return false; } } true }