introduce GEMINI_MIME static

This commit is contained in:
panicbit 2020-11-14 00:48:50 +01:00
parent b97c7a6f12
commit 9b9955827d
4 changed files with 23 additions and 7 deletions

View file

@ -18,3 +18,4 @@ futures = "0.3.7"
itertools = "0.9.0" itertools = "0.9.0"
log = "0.4.11" log = "0.4.11"
webpki = "0.21.0" webpki = "0.21.0"
lazy_static = "1.4.0"

View file

@ -1,7 +1,7 @@
use anyhow::*; use anyhow::*;
use futures::{future::BoxFuture, FutureExt}; use futures::{future::BoxFuture, FutureExt};
use tokio::sync::RwLock; 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::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
@ -32,7 +32,7 @@ fn handle_request(users: Arc<RwLock<HashMap<CertBytes, String>>>, request: Reque
if let Some(user) = users_read.get(cert_bytes) { if let Some(user) = users_read.get(cert_bytes) {
// The user has already registered // The user has already registered
Ok( Ok(
Response::success(&gemini_mime()?)? Response::success(&GEMINI_MIME)?
.with_body(format!("Welcome {}!", user)) .with_body(format!("Welcome {}!", user))
) )
} else { } else {
@ -44,7 +44,7 @@ fn handle_request(users: Arc<RwLock<HashMap<CertBytes, String>>>, request: Reque
let mut users_write = users.write().await; let mut users_write = users.write().await;
users_write.insert(cert_bytes.clone(), username.to_owned()); users_write.insert(cert_bytes.clone(), username.to_owned());
Ok( Ok(
Response::success(&gemini_mime()?)? Response::success(&GEMINI_MIME)?
.with_body(format!( .with_body(format!(
"Your account has been created {}! Welcome!", "Your account has been created {}! Welcome!",
username username

View file

@ -14,6 +14,7 @@ use tokio_rustls::{rustls, TlsAcceptor};
use rustls::*; use rustls::*;
use anyhow::*; use anyhow::*;
use uri::URIReference; use uri::URIReference;
use lazy_static::lazy_static;
pub mod types; pub mod types;
pub mod util; pub mod util;
@ -203,9 +204,13 @@ fn load_key() -> Result<PrivateKey> {
const GEMINI_MIME_STR: &str = "text/gemini"; 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<Mime> { pub fn gemini_mime() -> Result<Mime> {
let mime = GEMINI_MIME_STR.parse()?; Ok(GEMINI_MIME.clone())
Ok(mime)
} }
/// A client cert verifier that accepts all connections /// A client cert verifier that accepts all connections
@ -243,3 +248,13 @@ impl ClientCertVerifier for AllowAnonOrSelfsignedClient {
Ok(ClientCertVerified::assertion()) Ok(ClientCertVerified::assertion())
} }
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn gemini_mime_parses() {
let _: &Mime = &GEMINI_MIME;
}
}

View file

@ -6,7 +6,7 @@ use tokio::{
fs::{self, File}, fs::{self, File},
io, io,
}; };
use crate::{GEMINI_MIME_STR, Response, gemini_mime}; use crate::{GEMINI_MIME, GEMINI_MIME_STR, Response};
use itertools::Itertools; use itertools::Itertools;
pub async fn serve_file<P: AsRef<Path>>(path: P, mime: &Mime) -> Result<Response> { pub async fn serve_file<P: AsRef<Path>>(path: P, mime: &Mime) -> Result<Response> {
@ -82,7 +82,7 @@ async fn serve_dir_listing<P: AsRef<Path>, B: AsRef<Path>>(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<P: AsRef<Path>>(path: P) -> Mime { pub fn guess_mime_from_path<P: AsRef<Path>>(path: P) -> Mime {