Remove dependancy on `mime` when `serve_dir` is off

This commit is contained in:
Emii Tatsuo 2020-11-30 21:18:37 -05:00
parent bc6d0b89bc
commit 86ed240761
Signed by: Emi
GPG Key ID: 68FAB2E2E6DFC98B
7 changed files with 13 additions and 46 deletions

View File

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

View File

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

View File

@ -1,4 +1,3 @@
pub use ::mime::Mime;
pub use rustls::Certificate;
pub use uriparse::URIReference;

View File

@ -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<Mime> {
let mime = self.as_str().parse::<Mime>()
.context("Meta is not a valid MIME")?;
Ok(mime)
}
}
#[cfg(test)]

View File

@ -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<Body>) -> Self {
pub fn success(mime: impl ToString, body: impl Into<Body>) -> 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<Body>) -> 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<Body>) -> Self {
Self::success(&mime::TEXT_PLAIN, body)
Self::success("text/plain", body)
}
pub fn server_error(reason: impl Cowy<str>) -> Result<Self> {

View File

@ -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()),

View File

@ -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<P: AsRef<Path>>(path: P, mime: &Mime) -> Result<Response> {
pub async fn serve_file<P: AsRef<Path>>(path: P, mime: &str) -> Result<Response> {
let path = path.as_ref();
let file = match File::open(path).await {
@ -81,7 +79,7 @@ pub async fn serve_dir<D: AsRef<Path>, P: AsRef<Path>>(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<P: AsRef<Path>, B: AsRef<Path>>(path: P, virtual_path
}
#[cfg(feature="serve_dir")]
pub fn guess_mime_from_path<P: AsRef<Path>>(path: P) -> Mime {
pub fn guess_mime_from_path<P: AsRef<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")]