Isolate directory serving methods behind feature, incl mime_guess. Remove itertools
This commit is contained in:
parent
bbf034cf47
commit
753ecf708d
|
@ -8,6 +8,10 @@ description = "Gemini server implementation"
|
|||
repository = "https://github.com/panicbit/northstar"
|
||||
documentation = "https://docs.rs/northstar"
|
||||
|
||||
[features]
|
||||
default = ["serve_dir"]
|
||||
serve_dir = ["mime_guess"]
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.33"
|
||||
rustls = { version = "0.18.1", features = ["dangerous_configuration"] }
|
||||
|
@ -17,11 +21,10 @@ mime = "0.3.16"
|
|||
uriparse = "0.6.3"
|
||||
percent-encoding = "2.1.0"
|
||||
futures = "0.3.7"
|
||||
itertools = "0.9.0"
|
||||
log = "0.4.11"
|
||||
webpki = "0.21.0"
|
||||
lazy_static = "1.4.0"
|
||||
mime_guess = "2.0.3"
|
||||
mime_guess = { version = "2.0.3", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
env_logger = "0.8.1"
|
||||
|
|
|
@ -39,7 +39,6 @@
|
|||
use std::convert::TryInto;
|
||||
use std::fmt;
|
||||
|
||||
use itertools::Itertools;
|
||||
use crate::types::URIReference;
|
||||
use crate::util::Cowy;
|
||||
|
||||
|
@ -550,5 +549,6 @@ fn strip_newlines(text: impl Cowy<str>) -> String {
|
|||
text.as_ref()
|
||||
.lines()
|
||||
.filter(|part| !part.is_empty())
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ")
|
||||
}
|
||||
|
|
16
src/util.rs
16
src/util.rs
|
@ -1,13 +1,18 @@
|
|||
use std::path::Path;
|
||||
#[cfg(feature="serve_dir")]
|
||||
use std::path::{Path, PathBuf};
|
||||
#[cfg(feature="serve_dir")]
|
||||
use mime::Mime;
|
||||
#[cfg(feature="serve_dir")]
|
||||
use anyhow::*;
|
||||
#[cfg(feature="serve_dir")]
|
||||
use tokio::{
|
||||
fs::{self, File},
|
||||
io,
|
||||
};
|
||||
#[cfg(feature="serve_dir")]
|
||||
use crate::types::{Response, Document, document::HeadingLevel::*};
|
||||
use itertools::Itertools;
|
||||
|
||||
#[cfg(feature="serve_dir")]
|
||||
pub async fn serve_file<P: AsRef<Path>>(path: P, mime: &Mime) -> Result<Response> {
|
||||
let path = path.as_ref();
|
||||
|
||||
|
@ -22,6 +27,7 @@ pub async fn serve_file<P: AsRef<Path>>(path: P, mime: &Mime) -> Result<Response
|
|||
Ok(Response::success_with_body(mime, file))
|
||||
}
|
||||
|
||||
#[cfg(feature="serve_dir")]
|
||||
pub async fn serve_dir<D: AsRef<Path>, P: AsRef<Path>>(dir: D, virtual_path: &[P]) -> Result<Response> {
|
||||
debug!("Dir: {}", dir.as_ref().display());
|
||||
let dir = dir.as_ref().canonicalize()
|
||||
|
@ -47,6 +53,7 @@ pub async fn serve_dir<D: AsRef<Path>, P: AsRef<Path>>(dir: D, virtual_path: &[P
|
|||
serve_dir_listing(path, virtual_path).await
|
||||
}
|
||||
|
||||
#[cfg(feature="serve_dir")]
|
||||
async fn serve_dir_listing<P: AsRef<Path>, B: AsRef<Path>>(path: P, virtual_path: &[B]) -> Result<Response> {
|
||||
let mut dir = match fs::read_dir(path).await {
|
||||
Ok(dir) => dir,
|
||||
|
@ -56,10 +63,10 @@ async fn serve_dir_listing<P: AsRef<Path>, B: AsRef<Path>>(path: P, virtual_path
|
|||
}
|
||||
};
|
||||
|
||||
let breadcrumbs = virtual_path.iter().map(|segment| segment.as_ref().display()).join("/");
|
||||
let breadcrumbs: PathBuf = virtual_path.iter().collect();
|
||||
let mut document = Document::new();
|
||||
|
||||
document.add_heading(H1, format!("Index of /{}", breadcrumbs));
|
||||
document.add_heading(H1, format!("Index of /{}", breadcrumbs.display()));
|
||||
document.add_blank_line();
|
||||
|
||||
if virtual_path.get(0).map(<_>::as_ref) != Some(Path::new("")) {
|
||||
|
@ -85,6 +92,7 @@ async fn serve_dir_listing<P: AsRef<Path>, B: AsRef<Path>>(path: P, virtual_path
|
|||
Ok(Response::document(document))
|
||||
}
|
||||
|
||||
#[cfg(feature="serve_dir")]
|
||||
pub fn guess_mime_from_path<P: AsRef<Path>>(path: P) -> Mime {
|
||||
let path = path.as_ref();
|
||||
let extension = path.extension().and_then(|s| s.to_str());
|
||||
|
|
Loading…
Reference in a new issue