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"
log = "0.4.11"
webpki = "0.21.0"
lazy_static = "1.4.0"

View File

@ -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<RwLock<HashMap<CertBytes, String>>>, 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<RwLock<HashMap<CertBytes, String>>>, 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

View File

@ -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<PrivateKey> {
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> {
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;
}
}

View File

@ -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<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 {