2021-11-22 02:19:09 +01:00
|
|
|
use std::{fmt, io, ops::Deref, path::PathBuf, rc::Rc};
|
2020-09-21 00:18:25 +02:00
|
|
|
|
|
|
|
use actix_web::{
|
2021-12-28 03:37:13 +01:00
|
|
|
body::BoxBody,
|
|
|
|
dev::{Service, ServiceRequest, ServiceResponse},
|
2020-09-21 00:18:25 +02:00
|
|
|
error::Error,
|
|
|
|
guard::Guard,
|
|
|
|
http::{header, Method},
|
|
|
|
HttpResponse,
|
|
|
|
};
|
2021-04-01 16:26:13 +02:00
|
|
|
use futures_core::future::LocalBoxFuture;
|
2020-09-21 00:18:25 +02:00
|
|
|
|
|
|
|
use crate::{
|
2021-02-12 00:03:17 +01:00
|
|
|
named, Directory, DirectoryRenderer, FilesError, HttpService, MimeOverride, NamedFile,
|
2021-06-25 15:21:57 +02:00
|
|
|
PathBufWrap, PathFilter,
|
2020-09-21 00:18:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Assembled file serving service.
|
2021-11-22 02:19:09 +01:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct FilesService(pub(crate) Rc<FilesServiceInner>);
|
|
|
|
|
|
|
|
impl Deref for FilesService {
|
|
|
|
type Target = FilesServiceInner;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&*self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct FilesServiceInner {
|
2020-09-21 00:18:25 +02:00
|
|
|
pub(crate) directory: PathBuf,
|
|
|
|
pub(crate) index: Option<String>,
|
|
|
|
pub(crate) show_index: bool,
|
|
|
|
pub(crate) redirect_to_slash: bool,
|
|
|
|
pub(crate) default: Option<HttpService>,
|
|
|
|
pub(crate) renderer: Rc<DirectoryRenderer>,
|
|
|
|
pub(crate) mime_override: Option<Rc<MimeOverride>>,
|
2021-06-25 15:21:57 +02:00
|
|
|
pub(crate) path_filter: Option<Rc<PathFilter>>,
|
2020-09-21 00:18:25 +02:00
|
|
|
pub(crate) file_flags: named::Flags,
|
|
|
|
pub(crate) guards: Option<Rc<dyn Guard>>,
|
2020-12-26 04:36:15 +01:00
|
|
|
pub(crate) hidden_files: bool,
|
2020-09-21 00:18:25 +02:00
|
|
|
}
|
|
|
|
|
2021-11-22 02:19:09 +01:00
|
|
|
impl fmt::Debug for FilesServiceInner {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
f.write_str("FilesServiceInner")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-21 00:18:25 +02:00
|
|
|
impl FilesService {
|
2021-11-22 02:19:09 +01:00
|
|
|
async fn handle_err(
|
2021-04-01 16:26:13 +02:00
|
|
|
&self,
|
|
|
|
err: io::Error,
|
|
|
|
req: ServiceRequest,
|
2021-11-22 02:19:09 +01:00
|
|
|
) -> Result<ServiceResponse, Error> {
|
2021-04-01 16:26:13 +02:00
|
|
|
log::debug!("error handling {}: {}", req.path(), err);
|
2020-09-21 00:18:25 +02:00
|
|
|
|
2021-02-07 02:00:40 +01:00
|
|
|
if let Some(ref default) = self.default {
|
2021-11-22 02:19:09 +01:00
|
|
|
default.call(req).await
|
2020-09-21 00:18:25 +02:00
|
|
|
} else {
|
2021-11-22 02:19:09 +01:00
|
|
|
Ok(req.error_response(err))
|
2020-09-21 00:18:25 +02:00
|
|
|
}
|
|
|
|
}
|
2021-11-22 02:19:09 +01:00
|
|
|
|
|
|
|
fn serve_named_file(
|
|
|
|
&self,
|
|
|
|
req: ServiceRequest,
|
|
|
|
mut named_file: NamedFile,
|
|
|
|
) -> ServiceResponse {
|
|
|
|
if let Some(ref mime_override) = self.mime_override {
|
|
|
|
let new_disposition = mime_override(&named_file.content_type.type_());
|
|
|
|
named_file.content_disposition.disposition = new_disposition;
|
|
|
|
}
|
|
|
|
named_file.flags = self.file_flags;
|
|
|
|
|
|
|
|
let (req, _) = req.into_parts();
|
|
|
|
let res = named_file.into_response(&req);
|
|
|
|
ServiceResponse::new(req, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn show_index(&self, req: ServiceRequest, path: PathBuf) -> ServiceResponse {
|
|
|
|
let dir = Directory::new(self.directory.clone(), path);
|
|
|
|
|
|
|
|
let (req, _) = req.into_parts();
|
|
|
|
|
|
|
|
(self.renderer)(&dir, &req).unwrap_or_else(|e| ServiceResponse::from_err(e, req))
|
|
|
|
}
|
2020-09-21 00:18:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for FilesService {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
f.write_str("FilesService")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 00:47:04 +01:00
|
|
|
impl Service<ServiceRequest> for FilesService {
|
2021-12-28 03:37:13 +01:00
|
|
|
type Response = ServiceResponse<BoxBody>;
|
2020-09-21 00:18:25 +02:00
|
|
|
type Error = Error;
|
2021-11-22 02:19:09 +01:00
|
|
|
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
|
2020-09-21 00:18:25 +02:00
|
|
|
|
2021-02-07 02:00:40 +01:00
|
|
|
actix_service::always_ready!();
|
2020-09-21 00:18:25 +02:00
|
|
|
|
2021-02-07 02:00:40 +01:00
|
|
|
fn call(&self, req: ServiceRequest) -> Self::Future {
|
2020-09-21 00:18:25 +02:00
|
|
|
let is_method_valid = if let Some(guard) = &self.guards {
|
|
|
|
// execute user defined guards
|
2021-12-28 03:37:13 +01:00
|
|
|
(**guard).check(&req.guard_ctx())
|
2020-09-21 00:18:25 +02:00
|
|
|
} else {
|
|
|
|
// default behavior
|
|
|
|
matches!(*req.method(), Method::HEAD | Method::GET)
|
|
|
|
};
|
|
|
|
|
2021-11-22 02:19:09 +01:00
|
|
|
let this = self.clone();
|
2020-09-21 00:18:25 +02:00
|
|
|
|
2021-11-22 02:19:09 +01:00
|
|
|
Box::pin(async move {
|
|
|
|
if !is_method_valid {
|
|
|
|
return Ok(req.into_response(
|
|
|
|
actix_web::HttpResponse::MethodNotAllowed()
|
|
|
|
.insert_header(header::ContentType(mime::TEXT_PLAIN_UTF_8))
|
|
|
|
.body("Request did not meet this resource's requirements."),
|
|
|
|
));
|
2021-06-25 15:21:57 +02:00
|
|
|
}
|
|
|
|
|
2021-11-22 02:19:09 +01:00
|
|
|
let real_path =
|
|
|
|
match PathBufWrap::parse_path(req.match_info().path(), this.hidden_files) {
|
|
|
|
Ok(item) => item,
|
|
|
|
Err(e) => return Ok(req.error_response(e)),
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(filter) = &this.path_filter {
|
|
|
|
if !filter(real_path.as_ref(), req.head()) {
|
|
|
|
if let Some(ref default) = this.default {
|
|
|
|
return default.call(req).await;
|
|
|
|
} else {
|
|
|
|
return Ok(
|
|
|
|
req.into_response(actix_web::HttpResponse::NotFound().finish())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-21 00:18:25 +02:00
|
|
|
|
2021-11-22 02:19:09 +01:00
|
|
|
// full file path
|
|
|
|
let path = this.directory.join(&real_path);
|
|
|
|
if let Err(err) = path.canonicalize() {
|
|
|
|
return this.handle_err(err, req).await;
|
2021-05-26 11:42:29 +02:00
|
|
|
}
|
2020-09-21 00:18:25 +02:00
|
|
|
|
2021-11-22 02:19:09 +01:00
|
|
|
if path.is_dir() {
|
|
|
|
if this.redirect_to_slash
|
|
|
|
&& !req.path().ends_with('/')
|
|
|
|
&& (this.index.is_some() || this.show_index)
|
|
|
|
{
|
|
|
|
let redirect_to = format!("{}/", req.path());
|
|
|
|
|
|
|
|
return Ok(req.into_response(
|
|
|
|
HttpResponse::Found()
|
|
|
|
.insert_header((header::LOCATION, redirect_to))
|
|
|
|
.finish(),
|
|
|
|
));
|
2020-09-21 00:18:25 +02:00
|
|
|
}
|
|
|
|
|
2021-11-22 02:19:09 +01:00
|
|
|
match this.index {
|
|
|
|
Some(ref index) => {
|
|
|
|
let named_path = path.join(index);
|
|
|
|
match NamedFile::open_async(named_path).await {
|
|
|
|
Ok(named_file) => Ok(this.serve_named_file(req, named_file)),
|
|
|
|
Err(_) if this.show_index => Ok(this.show_index(req, path)),
|
|
|
|
Err(err) => this.handle_err(err, req).await,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None if this.show_index => Ok(this.show_index(req, path)),
|
|
|
|
_ => Ok(ServiceResponse::from_err(
|
|
|
|
FilesError::IsDirectory,
|
|
|
|
req.into_parts().0,
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match NamedFile::open_async(&path).await {
|
|
|
|
Ok(mut named_file) => {
|
|
|
|
if let Some(ref mime_override) = this.mime_override {
|
|
|
|
let new_disposition =
|
|
|
|
mime_override(&named_file.content_type.type_());
|
|
|
|
named_file.content_disposition.disposition = new_disposition;
|
|
|
|
}
|
|
|
|
named_file.flags = this.file_flags;
|
|
|
|
|
|
|
|
let (req, _) = req.into_parts();
|
|
|
|
let res = named_file.into_response(&req);
|
|
|
|
Ok(ServiceResponse::new(req, res))
|
|
|
|
}
|
|
|
|
Err(err) => this.handle_err(err, req).await,
|
2020-09-21 00:18:25 +02:00
|
|
|
}
|
|
|
|
}
|
2021-11-22 02:19:09 +01:00
|
|
|
})
|
2020-09-21 00:18:25 +02:00
|
|
|
}
|
|
|
|
}
|