Merge pull request #5 from panicbit/4_gemini_mime_convenience

4 gemini mime convenience
This commit is contained in:
panicbit 2020-11-14 01:13:48 +01:00 committed by GitHub
commit 5f96cbc5e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 9 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;
@ -201,11 +202,17 @@ fn load_key() -> Result<PrivateKey> {
Ok(key)
}
const GEMINI_MIME: &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");
}
#[deprecated(note = "Use `GEMINI_MIME` instead", since = "0.3.0")]
pub fn gemini_mime() -> Result<Mime> {
let mime = GEMINI_MIME.parse()?;
Ok(mime)
Ok(GEMINI_MIME.clone())
}
/// A client cert verifier that accepts all connections
@ -243,3 +250,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, 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 {
@ -90,7 +90,7 @@ pub fn guess_mime_from_path<P: AsRef<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",