diff --git a/Cargo.toml b/Cargo.toml index ab91deb..6a7127b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ include = ["src/**", "Cargo.*", "CHANGELOG.md", "LICENSE*", "README.md"] [features] default = ["certgen"] -user_management = ["sled", "bincode", "serde/derive", "crc32fast"] +user_management = ["sled", "bincode", "serde/derive", "crc32fast", "lazy_static"] user_management_advanced = ["rust-argon2", "ring", "user_management"] user_management_routes = ["user_management"] serve_dir = ["mime_guess", "tokio/fs"] @@ -25,12 +25,11 @@ anyhow = "1.0.33" rustls = { version = "0.18.1", features = ["dangerous_configuration"] } tokio-rustls = "0.20.0" tokio = { version = "0.3.1", features = ["io-util","net","time", "rt"] } -mime = "0.3.16" uriparse = "0.6.3" percent-encoding = "2.1.0" log = "0.4.11" webpki = "0.21.0" -lazy_static = "1.4.0" +lazy_static = { version = "1.4.0", optional = true } mime_guess = { version = "2.0.3", optional = true } dashmap = { version = "3.11.10", optional = true } sled = { version = "0.34.6", optional = true } diff --git a/src/lib.rs b/src/lib.rs index 9cdf1cf..83ae391 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,7 +23,6 @@ use rustls::internal::msgs::handshake::DigitallySignedStruct; use tokio_rustls::{rustls, TlsAcceptor}; use rustls::*; use anyhow::*; -use lazy_static::lazy_static; use crate::util::opt_timeout; use routing::RoutingNode; #[cfg(feature = "ratelimiting")] @@ -45,7 +44,6 @@ use user_management::UserManager; #[cfg(feature = "certgen")] use gencert::CertGenMode; -pub use mime; pub use uriparse as uri; pub use types::*; @@ -561,14 +559,6 @@ fn load_key(key_path: &PathBuf) -> Result { Ok(key) } -/// 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().unwrap(); -} - /// A client cert verifier that accepts all connections /// /// Unfortunately, rustls doesn't provide a ClientCertVerifier that accepts self-signed @@ -625,15 +615,5 @@ impl ClientCertVerifier for AllowAnonOrSelfsignedClient { } } -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn gemini_mime_parses() { - let _: &Mime = &GEMINI_MIME; - } -} - #[cfg(feature = "ratelimiting")] enum Never {} diff --git a/src/types.rs b/src/types.rs index b376427..0ad442d 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,4 +1,3 @@ -pub use ::mime::Mime; pub use rustls::Certificate; pub use uriparse::URIReference; diff --git a/src/types/meta.rs b/src/types/meta.rs index 8123d46..80477fc 100644 --- a/src/types/meta.rs +++ b/src/types/meta.rs @@ -1,5 +1,4 @@ use anyhow::*; -use crate::Mime; use crate::util::Cowy; @@ -46,12 +45,6 @@ impl Meta { pub fn as_str(&self) -> &str { &self.0 } - - pub fn to_mime(&self) -> Result { - let mime = self.as_str().parse::() - .context("Meta is not a valid MIME")?; - Ok(mime) - } } #[cfg(test)] diff --git a/src/types/response.rs b/src/types/response.rs index 39c7cdb..1baf59e 100644 --- a/src/types/response.rs +++ b/src/types/response.rs @@ -3,9 +3,8 @@ use std::borrow::Borrow; use anyhow::*; use uriparse::URIReference; -use crate::types::{ResponseHeader, Body, Mime, Document}; +use crate::types::{ResponseHeader, Body, Document}; use crate::util::Cowy; -use crate::GEMINI_MIME; pub struct Response { header: ResponseHeader, @@ -44,7 +43,7 @@ impl Response { } /// Create a successful response with a given body and MIME - pub fn success(mime: &Mime, body: impl Into) -> Self { + pub fn success(mime: impl ToString, body: impl Into) -> Self { Self { header: ResponseHeader::success(mime), body: Some(body.into()), @@ -53,12 +52,12 @@ impl Response { /// Create a successful response with a `text/gemini` MIME pub fn success_gemini(body: impl Into) -> Self { - Self::success(&GEMINI_MIME, body) + Self::success("text/gemini", body) } /// Create a successful response with a `text/plain` MIME pub fn success_plain(body: impl Into) -> Self { - Self::success(&mime::TEXT_PLAIN, body) + Self::success("text/plain", body) } pub fn server_error(reason: impl Cowy) -> Result { diff --git a/src/types/response_header.rs b/src/types/response_header.rs index b2b5e20..3e214d1 100644 --- a/src/types/response_header.rs +++ b/src/types/response_header.rs @@ -2,7 +2,6 @@ use std::convert::TryInto; use anyhow::*; use uriparse::URIReference; -use crate::Mime; use crate::util::Cowy; use crate::types::{Status, Meta}; @@ -27,7 +26,7 @@ impl ResponseHeader { } } - pub fn success(mime: &Mime) -> Self { + pub fn success(mime: impl ToString) -> Self { Self { status: Status::SUCCESS, meta: Meta::new_lossy(mime.to_string()), diff --git a/src/util.rs b/src/util.rs index 97df128..32d521d 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,7 +1,5 @@ #[cfg(feature="serve_dir")] use std::path::{Path, PathBuf}; -#[cfg(feature="serve_dir")] -use mime::Mime; use anyhow::*; #[cfg(feature="serve_dir")] use tokio::{ @@ -16,7 +14,7 @@ use std::future::Future; use tokio::time; #[cfg(feature="serve_dir")] -pub async fn serve_file>(path: P, mime: &Mime) -> Result { +pub async fn serve_file>(path: P, mime: &str) -> Result { let path = path.as_ref(); let file = match File::open(path).await { @@ -81,7 +79,7 @@ pub async fn serve_dir, P: AsRef>(dir: D, virtual_path: &[P if !path.is_dir() { let mime = guess_mime_from_path(&path); - return serve_file(path, &mime).await; + return serve_file(path, mime).await; } serve_dir_listing(path, virtual_path).await @@ -131,19 +129,19 @@ async fn serve_dir_listing, B: AsRef>(path: P, virtual_path } #[cfg(feature="serve_dir")] -pub fn guess_mime_from_path>(path: P) -> Mime { +pub fn guess_mime_from_path>(path: P) -> &'static str { let path = path.as_ref(); let extension = path.extension().and_then(|s| s.to_str()); let extension = match extension { Some(extension) => extension, - None => return mime::APPLICATION_OCTET_STREAM, + None => return "application/octet-stream" }; if let "gemini" | "gmi" = extension { - return crate::GEMINI_MIME.clone(); + return "text/gemini"; } - mime_guess::from_ext(extension).first_or_octet_stream() + mime_guess::from_ext(extension).first_raw().unwrap_or("application/octet-stream") } #[cfg(feature="serve_dir")]