From b97c7a6f12782d9eb667630985d0f1b4f5318ad9 Mon Sep 17 00:00:00 2001 From: panicbit Date: Sat, 14 Nov 2020 00:27:41 +0100 Subject: [PATCH 1/3] rename GEMINI_MIME to GEMINI_MIME_STR --- src/lib.rs | 4 ++-- src/util.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3206a27..b5e59f6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -201,10 +201,10 @@ fn load_key() -> Result { Ok(key) } -const GEMINI_MIME: &str = "text/gemini"; +const GEMINI_MIME_STR: &str = "text/gemini"; pub fn gemini_mime() -> Result { - let mime = GEMINI_MIME.parse()?; + let mime = GEMINI_MIME_STR.parse()?; Ok(mime) } diff --git a/src/util.rs b/src/util.rs index dcd7b09..e7a6cf0 100644 --- a/src/util.rs +++ b/src/util.rs @@ -6,7 +6,7 @@ use tokio::{ fs::{self, File}, io, }; -use crate::{GEMINI_MIME, Response, gemini_mime}; +use crate::{GEMINI_MIME_STR, Response, gemini_mime}; use itertools::Itertools; pub async fn serve_file>(path: P, mime: &Mime) -> Result { @@ -90,7 +90,7 @@ pub fn guess_mime_from_path>(path: P) -> Mime { let extension = path.extension().and_then(|s| s.to_str()); let mime = match extension { Some(extension) => match extension { - "gemini" => GEMINI_MIME, + "gemini" => GEMINI_MIME_STR, "txt" => "text/plain", "jpeg" | "jpg" | "jpe" => "image/jpeg", "png" => "image/png", From 9b9955827d4f785c912087ccf6dd56942551f610 Mon Sep 17 00:00:00 2001 From: panicbit Date: Sat, 14 Nov 2020 00:48:50 +0100 Subject: [PATCH 2/3] introduce GEMINI_MIME static --- Cargo.toml | 1 + examples/certificates.rs | 6 +++--- src/lib.rs | 19 +++++++++++++++++-- src/util.rs | 4 ++-- 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a519135..6c4874c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,3 +18,4 @@ futures = "0.3.7" itertools = "0.9.0" log = "0.4.11" webpki = "0.21.0" +lazy_static = "1.4.0" diff --git a/examples/certificates.rs b/examples/certificates.rs index 095f65f..103aa7f 100644 --- a/examples/certificates.rs +++ b/examples/certificates.rs @@ -1,7 +1,7 @@ use anyhow::*; use futures::{future::BoxFuture, FutureExt}; use tokio::sync::RwLock; -use northstar::{Server, Request, Response, GEMINI_PORT, Certificate, gemini_mime}; +use northstar::{Certificate, GEMINI_MIME, GEMINI_PORT, Request, Response, Server}; use std::collections::HashMap; use std::sync::Arc; @@ -32,7 +32,7 @@ fn handle_request(users: Arc>>, request: Reque if let Some(user) = users_read.get(cert_bytes) { // The user has already registered Ok( - Response::success(&gemini_mime()?)? + Response::success(&GEMINI_MIME)? .with_body(format!("Welcome {}!", user)) ) } else { @@ -44,7 +44,7 @@ fn handle_request(users: Arc>>, request: Reque let mut users_write = users.write().await; users_write.insert(cert_bytes.clone(), username.to_owned()); Ok( - Response::success(&gemini_mime()?)? + Response::success(&GEMINI_MIME)? .with_body(format!( "Your account has been created {}! Welcome!", username diff --git a/src/lib.rs b/src/lib.rs index b5e59f6..0cc7d6b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,6 +14,7 @@ use tokio_rustls::{rustls, TlsAcceptor}; use rustls::*; use anyhow::*; use uri::URIReference; +use lazy_static::lazy_static; pub mod types; pub mod util; @@ -203,9 +204,13 @@ fn load_key() -> Result { const GEMINI_MIME_STR: &str = "text/gemini"; +lazy_static! { + pub static ref GEMINI_MIME: Mime = GEMINI_MIME_STR.parse().expect("northstar BUG"); +} + +#[deprecated(note = "Use `GEMINI_MIME` instead", since = "0.3.0")] pub fn gemini_mime() -> Result { - let mime = GEMINI_MIME_STR.parse()?; - Ok(mime) + Ok(GEMINI_MIME.clone()) } /// A client cert verifier that accepts all connections @@ -243,3 +248,13 @@ impl ClientCertVerifier for AllowAnonOrSelfsignedClient { Ok(ClientCertVerified::assertion()) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn gemini_mime_parses() { + let _: &Mime = &GEMINI_MIME; + } +} diff --git a/src/util.rs b/src/util.rs index e7a6cf0..8b388fc 100644 --- a/src/util.rs +++ b/src/util.rs @@ -6,7 +6,7 @@ use tokio::{ fs::{self, File}, io, }; -use crate::{GEMINI_MIME_STR, Response, gemini_mime}; +use crate::{GEMINI_MIME, GEMINI_MIME_STR, Response}; use itertools::Itertools; pub async fn serve_file>(path: P, mime: &Mime) -> Result { @@ -82,7 +82,7 @@ async fn serve_dir_listing, B: AsRef>(path: P, virtual_path )?; } - Ok(Response::success(&gemini_mime()?)?.with_body(listing)) + Ok(Response::success(&GEMINI_MIME)?.with_body(listing)) } pub fn guess_mime_from_path>(path: P) -> Mime { From 015ea4284043e0d5c1f8231760e26b1497561117 Mon Sep 17 00:00:00 2001 From: panicbit Date: Sat, 14 Nov 2020 00:53:54 +0100 Subject: [PATCH 3/3] make GEMINI_MIME_STR public --- src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 0cc7d6b..2a58256 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -202,9 +202,11 @@ fn load_key() -> Result { Ok(key) } -const GEMINI_MIME_STR: &str = "text/gemini"; +/// Mime for Gemini documents +pub const GEMINI_MIME_STR: &str = "text/gemini"; lazy_static! { + /// Mime for Gemini documents ("text/gemini") pub static ref GEMINI_MIME: Mime = GEMINI_MIME_STR.parse().expect("northstar BUG"); }