2023-06-01 13:52:05 +03:00
|
|
|
#![allow(clippy::too_many_arguments)]
|
|
|
|
|
2023-11-03 15:36:23 +03:00
|
|
|
use crate::auth::{www_authenticate, AccessPaths, AccessPerm};
|
2023-12-21 09:24:20 +03:00
|
|
|
use crate::http_utils::{body_full, IncomingStream, LengthLimitedStream};
|
2023-03-31 18:48:23 +03:00
|
|
|
use crate::utils::{
|
2023-12-21 10:46:55 +03:00
|
|
|
decode_uri, encode_uri, get_file_mtime_and_mode, get_file_name, glob, parse_range,
|
|
|
|
try_get_file_name,
|
2023-03-31 18:48:23 +03:00
|
|
|
};
|
2023-02-21 12:23:24 +03:00
|
|
|
use crate::Args;
|
2022-05-26 11:17:55 +03:00
|
|
|
|
2023-12-21 09:24:20 +03:00
|
|
|
use anyhow::{anyhow, Result};
|
|
|
|
use async_zip::{tokio::write::ZipFileWriter, Compression, ZipDateTime, ZipEntryBuilder};
|
|
|
|
use bytes::Bytes;
|
2023-02-21 12:23:24 +03:00
|
|
|
use chrono::{LocalResult, TimeZone, Utc};
|
2023-12-21 09:24:20 +03:00
|
|
|
use futures_util::{pin_mut, TryStreamExt};
|
2022-05-30 06:22:28 +03:00
|
|
|
use headers::{
|
2023-11-15 14:44:44 +03:00
|
|
|
AcceptRanges, AccessControlAllowCredentials, AccessControlAllowOrigin, CacheControl,
|
|
|
|
ContentLength, ContentType, ETag, HeaderMap, HeaderMapExt, IfModifiedSince, IfNoneMatch,
|
|
|
|
IfRange, LastModified, Range,
|
2022-05-30 06:22:28 +03:00
|
|
|
};
|
2023-12-21 09:24:20 +03:00
|
|
|
use http_body_util::{combinators::BoxBody, BodyExt, StreamBody};
|
|
|
|
use hyper::body::Frame;
|
|
|
|
use hyper::{
|
|
|
|
body::Incoming,
|
|
|
|
header::{
|
|
|
|
HeaderValue, AUTHORIZATION, CONTENT_DISPOSITION, CONTENT_LENGTH, CONTENT_RANGE,
|
|
|
|
CONTENT_TYPE, RANGE,
|
|
|
|
},
|
|
|
|
Method, StatusCode, Uri,
|
2022-05-31 09:39:07 +03:00
|
|
|
};
|
2022-05-26 11:17:55 +03:00
|
|
|
use serde::Serialize;
|
2022-09-05 05:30:45 +03:00
|
|
|
use std::borrow::Cow;
|
2023-07-04 05:10:48 +03:00
|
|
|
use std::cmp::Ordering;
|
2022-08-23 09:24:42 +03:00
|
|
|
use std::collections::HashMap;
|
2022-05-31 05:58:32 +03:00
|
|
|
use std::fs::Metadata;
|
2022-06-16 05:24:32 +03:00
|
|
|
use std::io::SeekFrom;
|
2022-06-15 14:33:51 +03:00
|
|
|
use std::net::SocketAddr;
|
2022-05-26 11:17:55 +03:00
|
|
|
use std::path::{Path, PathBuf};
|
2023-07-04 05:10:48 +03:00
|
|
|
use std::sync::atomic::{self, AtomicBool};
|
2022-05-26 11:17:55 +03:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::time::SystemTime;
|
2022-05-28 13:58:43 +03:00
|
|
|
use tokio::fs::File;
|
2023-02-20 17:50:24 +03:00
|
|
|
use tokio::io::{AsyncReadExt, AsyncSeekExt, AsyncWrite};
|
2022-05-26 11:17:55 +03:00
|
|
|
use tokio::{fs, io};
|
2023-06-01 17:05:44 +03:00
|
|
|
use tokio_util::compat::FuturesAsyncWriteCompatExt;
|
2023-12-21 09:24:20 +03:00
|
|
|
use tokio_util::io::{ReaderStream, StreamReader};
|
2022-06-04 19:09:21 +03:00
|
|
|
use uuid::Uuid;
|
2023-12-21 09:24:20 +03:00
|
|
|
use walkdir::WalkDir;
|
|
|
|
use xml::escape::escape_str_pcdata;
|
2022-05-26 11:17:55 +03:00
|
|
|
|
2023-12-21 09:24:20 +03:00
|
|
|
pub type Request = hyper::Request<Incoming>;
|
|
|
|
pub type Response = hyper::Response<BoxBody<Bytes, anyhow::Error>>;
|
2022-05-26 11:17:55 +03:00
|
|
|
|
2022-06-01 15:26:03 +03:00
|
|
|
const INDEX_HTML: &str = include_str!("../assets/index.html");
|
|
|
|
const INDEX_CSS: &str = include_str!("../assets/index.css");
|
|
|
|
const INDEX_JS: &str = include_str!("../assets/index.js");
|
2022-06-07 03:59:44 +03:00
|
|
|
const FAVICON_ICO: &[u8] = include_bytes!("../assets/favicon.ico");
|
2022-06-01 17:49:55 +03:00
|
|
|
const INDEX_NAME: &str = "index.html";
|
2022-06-16 05:24:32 +03:00
|
|
|
const BUF_SIZE: usize = 65536;
|
2023-02-20 17:50:24 +03:00
|
|
|
const TEXT_MAX_SIZE: u64 = 4194304; // 4M
|
2022-05-26 11:17:55 +03:00
|
|
|
|
2022-06-15 14:33:51 +03:00
|
|
|
pub struct Server {
|
2023-12-21 09:24:20 +03:00
|
|
|
args: Args,
|
2022-06-21 18:01:00 +03:00
|
|
|
assets_prefix: String,
|
2022-09-05 05:30:45 +03:00
|
|
|
html: Cow<'static, str>,
|
2022-07-08 14:30:05 +03:00
|
|
|
single_file_req_paths: Vec<String>,
|
2022-07-02 17:55:22 +03:00
|
|
|
running: Arc<AtomicBool>,
|
2022-05-26 11:17:55 +03:00
|
|
|
}
|
|
|
|
|
2022-06-15 14:33:51 +03:00
|
|
|
impl Server {
|
2023-12-21 09:24:20 +03:00
|
|
|
pub fn init(args: Args, running: Arc<AtomicBool>) -> Result<Self> {
|
2022-06-21 18:01:00 +03:00
|
|
|
let assets_prefix = format!("{}__dufs_v{}_", args.uri_prefix, env!("CARGO_PKG_VERSION"));
|
2022-07-08 14:30:05 +03:00
|
|
|
let single_file_req_paths = if args.path_is_file {
|
|
|
|
vec![
|
|
|
|
args.uri_prefix.to_string(),
|
|
|
|
args.uri_prefix[0..args.uri_prefix.len() - 1].to_string(),
|
|
|
|
encode_uri(&format!(
|
|
|
|
"{}{}",
|
|
|
|
&args.uri_prefix,
|
2023-11-04 11:58:19 +03:00
|
|
|
get_file_name(&args.serve_path)
|
2022-07-08 14:30:05 +03:00
|
|
|
)),
|
|
|
|
]
|
|
|
|
} else {
|
|
|
|
vec![]
|
|
|
|
};
|
2023-11-04 11:58:19 +03:00
|
|
|
let html = match args.assets.as_ref() {
|
2023-02-21 12:23:24 +03:00
|
|
|
Some(path) => Cow::Owned(std::fs::read_to_string(path.join("index.html"))?),
|
2022-09-05 05:30:45 +03:00
|
|
|
None => Cow::Borrowed(INDEX_HTML),
|
|
|
|
};
|
2023-02-21 12:23:24 +03:00
|
|
|
Ok(Self {
|
2022-06-21 18:01:00 +03:00
|
|
|
args,
|
2022-07-02 17:55:22 +03:00
|
|
|
running,
|
2022-07-08 14:30:05 +03:00
|
|
|
single_file_req_paths,
|
2022-06-21 18:01:00 +03:00
|
|
|
assets_prefix,
|
2022-09-05 05:30:45 +03:00
|
|
|
html,
|
2023-02-21 12:23:24 +03:00
|
|
|
})
|
2022-05-26 11:17:55 +03:00
|
|
|
}
|
|
|
|
|
2022-06-15 14:33:51 +03:00
|
|
|
pub async fn call(
|
|
|
|
self: Arc<Self>,
|
|
|
|
req: Request,
|
2022-11-11 03:57:44 +03:00
|
|
|
addr: Option<SocketAddr>,
|
2022-06-15 14:33:51 +03:00
|
|
|
) -> Result<Response, hyper::Error> {
|
2022-05-27 04:01:16 +03:00
|
|
|
let uri = req.uri().clone();
|
2023-10-05 03:50:24 +03:00
|
|
|
let assets_prefix = &self.assets_prefix;
|
2022-06-19 12:27:09 +03:00
|
|
|
let enable_cors = self.args.enable_cors;
|
2023-11-04 12:10:38 +03:00
|
|
|
let mut http_log_data = self.args.http_logger.data(&req);
|
2022-11-11 03:57:44 +03:00
|
|
|
if let Some(addr) = addr {
|
|
|
|
http_log_data.insert("remote_addr".to_string(), addr.ip().to_string());
|
|
|
|
}
|
2022-05-31 00:49:42 +03:00
|
|
|
|
2022-07-31 03:27:09 +03:00
|
|
|
let mut res = match self.clone().handle(req).await {
|
2022-05-31 03:38:30 +03:00
|
|
|
Ok(res) => {
|
2022-07-31 03:27:09 +03:00
|
|
|
http_log_data.insert("status".to_string(), res.status().as_u16().to_string());
|
2023-10-05 03:50:24 +03:00
|
|
|
if !uri.path().starts_with(assets_prefix) {
|
2023-11-04 12:10:38 +03:00
|
|
|
self.args.http_logger.log(&http_log_data, None);
|
2022-06-21 18:01:00 +03:00
|
|
|
}
|
2022-05-31 03:38:30 +03:00
|
|
|
res
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
let mut res = Response::default();
|
2022-06-02 06:06:41 +03:00
|
|
|
let status = StatusCode::INTERNAL_SERVER_ERROR;
|
2022-06-15 14:57:28 +03:00
|
|
|
*res.status_mut() = status;
|
2022-07-31 03:27:09 +03:00
|
|
|
http_log_data.insert("status".to_string(), status.as_u16().to_string());
|
|
|
|
self.args
|
2023-11-04 12:10:38 +03:00
|
|
|
.http_logger
|
2022-07-31 03:27:09 +03:00
|
|
|
.log(&http_log_data, Some(err.to_string()));
|
2022-05-31 03:38:30 +03:00
|
|
|
res
|
|
|
|
}
|
|
|
|
};
|
2022-05-31 00:49:42 +03:00
|
|
|
|
2022-06-19 12:27:09 +03:00
|
|
|
if enable_cors {
|
2022-05-29 12:33:21 +03:00
|
|
|
add_cors(&mut res);
|
|
|
|
}
|
2022-05-27 04:01:16 +03:00
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
2023-02-21 12:23:24 +03:00
|
|
|
pub async fn handle(self: Arc<Self>, req: Request) -> Result<Response> {
|
2022-05-31 00:49:42 +03:00
|
|
|
let mut res = Response::default();
|
|
|
|
|
2022-06-01 17:49:55 +03:00
|
|
|
let req_path = req.uri().path();
|
2022-06-12 03:43:50 +03:00
|
|
|
let headers = req.headers();
|
|
|
|
let method = req.method().clone();
|
|
|
|
|
2022-09-05 05:30:45 +03:00
|
|
|
if method == Method::GET && self.handle_assets(req_path, headers, &mut res).await? {
|
2022-06-19 06:26:03 +03:00
|
|
|
return Ok(res);
|
|
|
|
}
|
|
|
|
|
2022-06-19 09:23:10 +03:00
|
|
|
let authorization = headers.get(AUTHORIZATION);
|
2023-06-01 13:52:05 +03:00
|
|
|
let relative_path = match self.resolve_path(req_path) {
|
|
|
|
Some(v) => v,
|
|
|
|
None => {
|
|
|
|
status_forbid(&mut res);
|
|
|
|
return Ok(res);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-11-03 15:36:23 +03:00
|
|
|
let guard = self.args.auth.guard(&relative_path, &method, authorization);
|
2023-06-01 13:52:05 +03:00
|
|
|
|
|
|
|
let (user, access_paths) = match guard {
|
|
|
|
(None, None) => {
|
|
|
|
self.auth_reject(&mut res)?;
|
|
|
|
return Ok(res);
|
|
|
|
}
|
|
|
|
(Some(_), None) => {
|
|
|
|
status_forbid(&mut res);
|
|
|
|
return Ok(res);
|
|
|
|
}
|
|
|
|
(x, Some(y)) => (x, y),
|
|
|
|
};
|
2022-05-31 00:49:42 +03:00
|
|
|
|
2023-02-21 07:42:40 +03:00
|
|
|
let query = req.uri().query().unwrap_or_default();
|
|
|
|
let query_params: HashMap<String, String> = form_urlencoded::parse(query.as_bytes())
|
|
|
|
.map(|(k, v)| (k.to_string(), v.to_string()))
|
|
|
|
.collect();
|
|
|
|
|
2023-02-21 11:10:51 +03:00
|
|
|
if method.as_str() == "WRITEABLE" {
|
2023-02-21 07:42:40 +03:00
|
|
|
return Ok(res);
|
|
|
|
}
|
|
|
|
|
2022-06-19 09:23:10 +03:00
|
|
|
let head_only = method == Method::HEAD;
|
|
|
|
|
|
|
|
if self.args.path_is_file {
|
2022-07-08 14:30:05 +03:00
|
|
|
if self
|
|
|
|
.single_file_req_paths
|
|
|
|
.iter()
|
|
|
|
.any(|v| v.as_str() == req_path)
|
|
|
|
{
|
2023-11-04 11:58:19 +03:00
|
|
|
self.handle_send_file(&self.args.serve_path, headers, head_only, &mut res)
|
2022-07-08 14:30:05 +03:00
|
|
|
.await?;
|
|
|
|
} else {
|
|
|
|
status_not_found(&mut res);
|
|
|
|
}
|
2022-06-19 09:23:10 +03:00
|
|
|
return Ok(res);
|
|
|
|
}
|
2023-06-01 13:52:05 +03:00
|
|
|
let path = match self.join_path(&relative_path) {
|
2022-05-31 00:49:42 +03:00
|
|
|
Some(v) => v,
|
|
|
|
None => {
|
2022-06-15 14:57:28 +03:00
|
|
|
status_forbid(&mut res);
|
2022-05-31 00:49:42 +03:00
|
|
|
return Ok(res);
|
|
|
|
}
|
2022-05-26 11:17:55 +03:00
|
|
|
};
|
2022-06-19 06:26:03 +03:00
|
|
|
|
2022-06-01 17:49:55 +03:00
|
|
|
let path = path.as_path();
|
2022-05-31 00:49:42 +03:00
|
|
|
|
2022-06-04 14:08:18 +03:00
|
|
|
let (is_miss, is_dir, is_file, size) = match fs::metadata(path).await.ok() {
|
|
|
|
Some(meta) => (false, meta.is_dir(), meta.is_file(), meta.len()),
|
|
|
|
None => (true, false, false, 0),
|
|
|
|
};
|
2022-05-31 00:49:42 +03:00
|
|
|
|
2022-05-31 03:38:30 +03:00
|
|
|
let allow_upload = self.args.allow_upload;
|
|
|
|
let allow_delete = self.args.allow_delete;
|
2022-06-21 02:23:20 +03:00
|
|
|
let allow_search = self.args.allow_search;
|
2022-12-10 06:09:42 +03:00
|
|
|
let allow_archive = self.args.allow_archive;
|
2022-06-01 17:49:55 +03:00
|
|
|
let render_index = self.args.render_index;
|
|
|
|
let render_spa = self.args.render_spa;
|
2022-06-17 14:02:13 +03:00
|
|
|
let render_try_index = self.args.render_try_index;
|
2022-05-31 00:49:42 +03:00
|
|
|
|
2022-06-01 17:49:55 +03:00
|
|
|
if !self.args.allow_symlink && !is_miss && !self.is_root_contained(path).await {
|
2022-06-15 14:57:28 +03:00
|
|
|
status_not_found(&mut res);
|
2022-05-31 15:53:14 +03:00
|
|
|
return Ok(res);
|
|
|
|
}
|
2022-06-04 14:08:18 +03:00
|
|
|
|
2022-06-11 05:18:07 +03:00
|
|
|
match method {
|
|
|
|
Method::GET | Method::HEAD => {
|
2022-06-01 17:49:55 +03:00
|
|
|
if is_dir {
|
2022-07-02 18:25:57 +03:00
|
|
|
if render_try_index {
|
2022-12-10 06:09:42 +03:00
|
|
|
if allow_archive && query_params.contains_key("zip") {
|
|
|
|
if !allow_archive {
|
|
|
|
status_not_found(&mut res);
|
|
|
|
return Ok(res);
|
|
|
|
}
|
2023-06-01 13:52:05 +03:00
|
|
|
self.handle_zip_dir(path, head_only, access_paths, &mut res)
|
2022-07-02 18:25:57 +03:00
|
|
|
.await?;
|
2023-06-01 13:52:05 +03:00
|
|
|
} else if allow_search && query_params.contains_key("q") {
|
|
|
|
self.handle_search_dir(
|
|
|
|
path,
|
|
|
|
&query_params,
|
|
|
|
head_only,
|
|
|
|
user,
|
|
|
|
access_paths,
|
|
|
|
&mut res,
|
|
|
|
)
|
|
|
|
.await?;
|
2022-07-02 18:25:57 +03:00
|
|
|
} else {
|
2022-08-23 09:24:42 +03:00
|
|
|
self.handle_render_index(
|
|
|
|
path,
|
|
|
|
&query_params,
|
|
|
|
headers,
|
|
|
|
head_only,
|
2023-02-21 07:42:40 +03:00
|
|
|
user,
|
2023-06-01 13:52:05 +03:00
|
|
|
access_paths,
|
2022-08-23 09:24:42 +03:00
|
|
|
&mut res,
|
|
|
|
)
|
|
|
|
.await?;
|
2022-07-02 18:25:57 +03:00
|
|
|
}
|
|
|
|
} else if render_index || render_spa {
|
2023-02-21 07:42:40 +03:00
|
|
|
self.handle_render_index(
|
|
|
|
path,
|
|
|
|
&query_params,
|
|
|
|
headers,
|
|
|
|
head_only,
|
|
|
|
user,
|
2023-06-01 13:52:05 +03:00
|
|
|
access_paths,
|
2023-02-21 07:42:40 +03:00
|
|
|
&mut res,
|
|
|
|
)
|
|
|
|
.await?;
|
2022-08-23 09:24:42 +03:00
|
|
|
} else if query_params.contains_key("zip") {
|
2022-12-10 06:09:42 +03:00
|
|
|
if !allow_archive {
|
|
|
|
status_not_found(&mut res);
|
|
|
|
return Ok(res);
|
|
|
|
}
|
2023-06-01 13:52:05 +03:00
|
|
|
self.handle_zip_dir(path, head_only, access_paths, &mut res)
|
2022-06-25 03:15:16 +03:00
|
|
|
.await?;
|
2023-06-01 13:52:05 +03:00
|
|
|
} else if allow_search && query_params.contains_key("q") {
|
|
|
|
self.handle_search_dir(
|
|
|
|
path,
|
|
|
|
&query_params,
|
|
|
|
head_only,
|
|
|
|
user,
|
|
|
|
access_paths,
|
|
|
|
&mut res,
|
|
|
|
)
|
|
|
|
.await?;
|
2022-06-01 17:49:55 +03:00
|
|
|
} else {
|
2023-06-01 13:52:05 +03:00
|
|
|
self.handle_ls_dir(
|
|
|
|
path,
|
|
|
|
true,
|
|
|
|
&query_params,
|
|
|
|
head_only,
|
|
|
|
user,
|
|
|
|
access_paths,
|
|
|
|
&mut res,
|
|
|
|
)
|
|
|
|
.await?;
|
2022-06-01 17:49:55 +03:00
|
|
|
}
|
|
|
|
} else if is_file {
|
2023-02-20 17:50:24 +03:00
|
|
|
if query_params.contains_key("edit") {
|
2023-11-28 02:14:53 +03:00
|
|
|
self.handle_deal_file(path, DataKind::Edit, head_only, user, &mut res)
|
|
|
|
.await?;
|
|
|
|
} else if query_params.contains_key("view") {
|
|
|
|
self.handle_deal_file(path, DataKind::View, head_only, user, &mut res)
|
2023-02-21 07:42:40 +03:00
|
|
|
.await?;
|
2023-02-20 17:50:24 +03:00
|
|
|
} else {
|
|
|
|
self.handle_send_file(path, headers, head_only, &mut res)
|
|
|
|
.await?;
|
|
|
|
}
|
2022-06-01 17:49:55 +03:00
|
|
|
} else if render_spa {
|
2022-06-11 05:18:07 +03:00
|
|
|
self.handle_render_spa(path, headers, head_only, &mut res)
|
|
|
|
.await?;
|
2022-06-01 17:49:55 +03:00
|
|
|
} else if allow_upload && req_path.ends_with('/') {
|
2023-06-01 13:52:05 +03:00
|
|
|
self.handle_ls_dir(
|
|
|
|
path,
|
|
|
|
false,
|
|
|
|
&query_params,
|
|
|
|
head_only,
|
|
|
|
user,
|
|
|
|
access_paths,
|
|
|
|
&mut res,
|
|
|
|
)
|
|
|
|
.await?;
|
2022-06-01 17:49:55 +03:00
|
|
|
} else {
|
2022-06-15 14:57:28 +03:00
|
|
|
status_not_found(&mut res);
|
2022-06-01 17:49:55 +03:00
|
|
|
}
|
2022-05-31 00:49:42 +03:00
|
|
|
}
|
2022-06-11 05:18:07 +03:00
|
|
|
Method::OPTIONS => {
|
2022-06-15 14:57:28 +03:00
|
|
|
set_webdav_headers(&mut res);
|
2022-05-31 03:38:30 +03:00
|
|
|
}
|
2022-06-11 05:18:07 +03:00
|
|
|
Method::PUT => {
|
2022-06-04 14:08:18 +03:00
|
|
|
if !allow_upload || (!allow_delete && is_file && size > 0) {
|
2022-06-15 14:57:28 +03:00
|
|
|
status_forbid(&mut res);
|
2022-06-01 17:49:55 +03:00
|
|
|
} else {
|
|
|
|
self.handle_upload(path, req, &mut res).await?;
|
|
|
|
}
|
2022-05-31 03:38:30 +03:00
|
|
|
}
|
2022-06-11 05:18:07 +03:00
|
|
|
Method::DELETE => {
|
2022-06-01 17:49:55 +03:00
|
|
|
if !allow_delete {
|
2022-06-15 14:57:28 +03:00
|
|
|
status_forbid(&mut res);
|
2022-06-01 17:49:55 +03:00
|
|
|
} else if !is_miss {
|
2022-06-04 07:51:56 +03:00
|
|
|
self.handle_delete(path, is_dir, &mut res).await?
|
2022-06-01 17:49:55 +03:00
|
|
|
} else {
|
2022-06-15 14:57:28 +03:00
|
|
|
status_not_found(&mut res);
|
2022-06-01 17:49:55 +03:00
|
|
|
}
|
2022-05-31 03:38:30 +03:00
|
|
|
}
|
2022-06-04 07:51:56 +03:00
|
|
|
method => match method.as_str() {
|
|
|
|
"PROPFIND" => {
|
|
|
|
if is_dir {
|
2023-06-05 06:40:31 +03:00
|
|
|
let access_paths = if access_paths.perm().indexonly() {
|
|
|
|
// see https://github.com/sigoden/dufs/issues/229
|
|
|
|
AccessPaths::new(AccessPerm::ReadOnly)
|
|
|
|
} else {
|
|
|
|
access_paths
|
|
|
|
};
|
2023-06-01 13:52:05 +03:00
|
|
|
self.handle_propfind_dir(path, headers, access_paths, &mut res)
|
|
|
|
.await?;
|
2022-06-04 07:51:56 +03:00
|
|
|
} else if is_file {
|
|
|
|
self.handle_propfind_file(path, &mut res).await?;
|
|
|
|
} else {
|
2022-06-15 14:57:28 +03:00
|
|
|
status_not_found(&mut res);
|
2022-06-04 07:51:56 +03:00
|
|
|
}
|
|
|
|
}
|
2022-06-04 14:08:18 +03:00
|
|
|
"PROPPATCH" => {
|
|
|
|
if is_file {
|
2022-06-05 02:35:05 +03:00
|
|
|
self.handle_proppatch(req_path, &mut res).await?;
|
2022-06-04 14:08:18 +03:00
|
|
|
} else {
|
2022-06-15 14:57:28 +03:00
|
|
|
status_not_found(&mut res);
|
2022-06-04 14:08:18 +03:00
|
|
|
}
|
|
|
|
}
|
2022-06-12 03:43:50 +03:00
|
|
|
"MKCOL" => {
|
2022-11-10 13:41:10 +03:00
|
|
|
if !allow_upload {
|
2022-06-15 14:57:28 +03:00
|
|
|
status_forbid(&mut res);
|
2022-11-10 13:41:10 +03:00
|
|
|
} else if !is_miss {
|
|
|
|
*res.status_mut() = StatusCode::METHOD_NOT_ALLOWED;
|
2023-12-21 09:24:20 +03:00
|
|
|
*res.body_mut() = body_full("Already exists");
|
2022-06-12 03:43:50 +03:00
|
|
|
} else {
|
|
|
|
self.handle_mkcol(path, &mut res).await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"COPY" => {
|
|
|
|
if !allow_upload {
|
2022-06-15 14:57:28 +03:00
|
|
|
status_forbid(&mut res);
|
2022-06-12 03:43:50 +03:00
|
|
|
} else if is_miss {
|
2022-06-15 14:57:28 +03:00
|
|
|
status_not_found(&mut res);
|
2022-06-12 03:43:50 +03:00
|
|
|
} else {
|
2022-07-04 18:25:05 +03:00
|
|
|
self.handle_copy(path, &req, &mut res).await?
|
2022-06-12 03:43:50 +03:00
|
|
|
}
|
2022-06-04 07:51:56 +03:00
|
|
|
}
|
2022-06-12 03:43:50 +03:00
|
|
|
"MOVE" => {
|
|
|
|
if !allow_upload || !allow_delete {
|
2022-06-15 14:57:28 +03:00
|
|
|
status_forbid(&mut res);
|
2022-06-12 03:43:50 +03:00
|
|
|
} else if is_miss {
|
2022-06-15 14:57:28 +03:00
|
|
|
status_not_found(&mut res);
|
2022-06-12 03:43:50 +03:00
|
|
|
} else {
|
2022-07-04 18:25:05 +03:00
|
|
|
self.handle_move(path, &req, &mut res).await?
|
2022-06-12 03:43:50 +03:00
|
|
|
}
|
2022-06-04 14:08:18 +03:00
|
|
|
}
|
|
|
|
"LOCK" => {
|
|
|
|
// Fake lock
|
|
|
|
if is_file {
|
2022-06-19 06:26:03 +03:00
|
|
|
let has_auth = authorization.is_some();
|
|
|
|
self.handle_lock(req_path, has_auth, &mut res).await?;
|
2022-06-04 14:08:18 +03:00
|
|
|
} else {
|
2022-06-15 14:57:28 +03:00
|
|
|
status_not_found(&mut res);
|
2022-06-04 14:08:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"UNLOCK" => {
|
|
|
|
// Fake unlock
|
|
|
|
if is_miss {
|
2022-06-15 14:57:28 +03:00
|
|
|
status_not_found(&mut res);
|
2022-06-04 14:08:18 +03:00
|
|
|
}
|
2022-06-04 07:51:56 +03:00
|
|
|
}
|
|
|
|
_ => {
|
2022-06-15 14:57:28 +03:00
|
|
|
*res.status_mut() = StatusCode::METHOD_NOT_ALLOWED;
|
2022-06-04 07:51:56 +03:00
|
|
|
}
|
|
|
|
},
|
2022-05-26 11:17:55 +03:00
|
|
|
}
|
2022-05-31 00:49:42 +03:00
|
|
|
Ok(res)
|
|
|
|
}
|
2022-05-26 11:17:55 +03:00
|
|
|
|
2023-12-21 09:24:20 +03:00
|
|
|
async fn handle_upload(&self, path: &Path, req: Request, res: &mut Response) -> Result<()> {
|
2022-06-04 07:51:56 +03:00
|
|
|
ensure_path_parent(path).await?;
|
2022-05-26 11:17:55 +03:00
|
|
|
|
2022-06-12 03:43:50 +03:00
|
|
|
let mut file = match fs::File::create(&path).await {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(_) => {
|
2022-06-15 14:57:28 +03:00
|
|
|
status_forbid(res);
|
2022-06-12 03:43:50 +03:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
};
|
2022-05-26 11:17:55 +03:00
|
|
|
|
2023-12-21 09:24:20 +03:00
|
|
|
let stream = IncomingStream::new(req.into_body());
|
2022-05-26 11:17:55 +03:00
|
|
|
|
2023-12-21 09:24:20 +03:00
|
|
|
let body_with_io_error = stream.map_err(|err| io::Error::new(io::ErrorKind::Other, err));
|
2022-05-26 11:17:55 +03:00
|
|
|
let body_reader = StreamReader::new(body_with_io_error);
|
|
|
|
|
2023-12-21 09:24:20 +03:00
|
|
|
pin_mut!(body_reader);
|
2022-05-26 11:17:55 +03:00
|
|
|
|
2023-11-03 15:58:53 +03:00
|
|
|
let ret = io::copy(&mut body_reader, &mut file).await;
|
|
|
|
if ret.is_err() {
|
|
|
|
tokio::fs::remove_file(&path).await?;
|
|
|
|
|
|
|
|
ret?;
|
|
|
|
}
|
2022-05-26 11:17:55 +03:00
|
|
|
|
2022-06-15 14:57:28 +03:00
|
|
|
*res.status_mut() = StatusCode::CREATED;
|
2022-05-31 00:49:42 +03:00
|
|
|
Ok(())
|
2022-05-26 11:17:55 +03:00
|
|
|
}
|
|
|
|
|
2023-02-21 12:23:24 +03:00
|
|
|
async fn handle_delete(&self, path: &Path, is_dir: bool, res: &mut Response) -> Result<()> {
|
2022-05-31 00:49:42 +03:00
|
|
|
match is_dir {
|
|
|
|
true => fs::remove_dir_all(path).await?,
|
|
|
|
false => fs::remove_file(path).await?,
|
2022-05-26 14:52:54 +03:00
|
|
|
}
|
2022-06-04 07:51:56 +03:00
|
|
|
|
2022-06-15 14:57:28 +03:00
|
|
|
status_no_content(res);
|
2022-05-31 00:49:42 +03:00
|
|
|
Ok(())
|
2022-05-26 14:52:54 +03:00
|
|
|
}
|
|
|
|
|
2022-06-11 05:18:07 +03:00
|
|
|
async fn handle_ls_dir(
|
|
|
|
&self,
|
|
|
|
path: &Path,
|
|
|
|
exist: bool,
|
2022-08-23 09:24:42 +03:00
|
|
|
query_params: &HashMap<String, String>,
|
2022-06-11 05:18:07 +03:00
|
|
|
head_only: bool,
|
2023-02-21 07:42:40 +03:00
|
|
|
user: Option<String>,
|
2023-06-01 13:52:05 +03:00
|
|
|
access_paths: AccessPaths,
|
2022-06-11 05:18:07 +03:00
|
|
|
res: &mut Response,
|
2023-02-21 12:23:24 +03:00
|
|
|
) -> Result<()> {
|
2022-06-04 07:51:56 +03:00
|
|
|
let mut paths = vec![];
|
2022-05-29 05:53:19 +03:00
|
|
|
if exist {
|
2023-08-24 13:32:34 +03:00
|
|
|
paths = match self.list_dir(path, path, access_paths.clone()).await {
|
2022-06-04 07:51:56 +03:00
|
|
|
Ok(paths) => paths,
|
2022-06-01 14:59:35 +03:00
|
|
|
Err(_) => {
|
2022-06-15 14:57:28 +03:00
|
|
|
status_forbid(res);
|
2022-06-01 14:59:35 +03:00
|
|
|
return Ok(());
|
|
|
|
}
|
2022-05-26 15:42:33 +03:00
|
|
|
}
|
2022-06-04 07:51:56 +03:00
|
|
|
};
|
2023-08-24 13:32:34 +03:00
|
|
|
self.send_index(
|
|
|
|
path,
|
|
|
|
paths,
|
|
|
|
exist,
|
|
|
|
query_params,
|
|
|
|
head_only,
|
|
|
|
user,
|
|
|
|
access_paths,
|
|
|
|
res,
|
|
|
|
)
|
2022-05-29 07:43:40 +03:00
|
|
|
}
|
2022-05-26 11:17:55 +03:00
|
|
|
|
2022-06-25 03:15:16 +03:00
|
|
|
async fn handle_search_dir(
|
2022-05-31 00:49:42 +03:00
|
|
|
&self,
|
|
|
|
path: &Path,
|
2022-08-23 09:24:42 +03:00
|
|
|
query_params: &HashMap<String, String>,
|
2022-06-11 05:18:07 +03:00
|
|
|
head_only: bool,
|
2023-02-21 07:42:40 +03:00
|
|
|
user: Option<String>,
|
2023-06-01 13:52:05 +03:00
|
|
|
access_paths: AccessPaths,
|
2022-05-31 00:49:42 +03:00
|
|
|
res: &mut Response,
|
2023-02-21 12:23:24 +03:00
|
|
|
) -> Result<()> {
|
2022-05-29 07:43:40 +03:00
|
|
|
let mut paths: Vec<PathItem> = vec![];
|
2023-02-21 12:23:24 +03:00
|
|
|
let search = query_params
|
|
|
|
.get("q")
|
|
|
|
.ok_or_else(|| anyhow!("invalid q"))?
|
|
|
|
.to_lowercase();
|
2023-12-07 01:55:17 +03:00
|
|
|
if search.is_empty() {
|
|
|
|
return self
|
|
|
|
.handle_ls_dir(path, true, query_params, head_only, user, access_paths, res)
|
|
|
|
.await;
|
|
|
|
} else {
|
2022-11-10 13:02:55 +03:00
|
|
|
let path_buf = path.to_path_buf();
|
|
|
|
let hidden = Arc::new(self.args.hidden.to_vec());
|
|
|
|
let hidden = hidden.clone();
|
|
|
|
let running = self.running.clone();
|
2023-08-24 13:32:34 +03:00
|
|
|
let access_paths = access_paths.clone();
|
2022-11-10 13:02:55 +03:00
|
|
|
let search_paths = tokio::task::spawn_blocking(move || {
|
|
|
|
let mut paths: Vec<PathBuf> = vec![];
|
2023-12-21 12:28:13 +03:00
|
|
|
for dir in access_paths.child_paths(&path_buf) {
|
2023-06-01 13:52:05 +03:00
|
|
|
let mut it = WalkDir::new(&dir).into_iter();
|
2023-06-15 03:28:21 +03:00
|
|
|
it.next();
|
2023-06-01 13:52:05 +03:00
|
|
|
while let Some(Ok(entry)) = it.next() {
|
2023-07-04 05:10:48 +03:00
|
|
|
if !running.load(atomic::Ordering::SeqCst) {
|
2023-06-01 13:52:05 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
let entry_path = entry.path();
|
|
|
|
let base_name = get_file_name(entry_path);
|
|
|
|
let file_type = entry.file_type();
|
|
|
|
let mut is_dir_type: bool = file_type.is_dir();
|
|
|
|
if file_type.is_symlink() {
|
|
|
|
match std::fs::symlink_metadata(entry_path) {
|
|
|
|
Ok(meta) => {
|
|
|
|
is_dir_type = meta.is_dir();
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
continue;
|
|
|
|
}
|
2023-02-19 17:03:59 +03:00
|
|
|
}
|
2023-06-01 13:52:05 +03:00
|
|
|
}
|
|
|
|
if is_hidden(&hidden, base_name, is_dir_type) {
|
|
|
|
if file_type.is_dir() {
|
|
|
|
it.skip_current_dir();
|
2023-02-19 17:03:59 +03:00
|
|
|
}
|
2023-06-01 13:52:05 +03:00
|
|
|
continue;
|
2023-02-19 17:03:59 +03:00
|
|
|
}
|
2023-06-01 13:52:05 +03:00
|
|
|
if !base_name.to_lowercase().contains(&search) {
|
|
|
|
continue;
|
2022-11-10 13:02:55 +03:00
|
|
|
}
|
2023-06-01 13:52:05 +03:00
|
|
|
paths.push(entry_path.to_path_buf());
|
2022-11-10 13:02:55 +03:00
|
|
|
}
|
2022-05-29 07:43:40 +03:00
|
|
|
}
|
2022-11-10 13:02:55 +03:00
|
|
|
paths
|
|
|
|
})
|
|
|
|
.await?;
|
|
|
|
for search_path in search_paths.into_iter() {
|
|
|
|
if let Ok(Some(item)) = self.to_pathitem(search_path, path.to_path_buf()).await {
|
|
|
|
paths.push(item);
|
2022-05-29 07:43:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-24 13:32:34 +03:00
|
|
|
self.send_index(
|
|
|
|
path,
|
|
|
|
paths,
|
|
|
|
true,
|
|
|
|
query_params,
|
|
|
|
head_only,
|
|
|
|
user,
|
|
|
|
access_paths,
|
|
|
|
res,
|
|
|
|
)
|
2022-05-26 11:17:55 +03:00
|
|
|
}
|
|
|
|
|
2023-06-01 13:52:05 +03:00
|
|
|
async fn handle_zip_dir(
|
|
|
|
&self,
|
|
|
|
path: &Path,
|
|
|
|
head_only: bool,
|
|
|
|
access_paths: AccessPaths,
|
|
|
|
res: &mut Response,
|
|
|
|
) -> Result<()> {
|
2022-05-29 01:57:16 +03:00
|
|
|
let (mut writer, reader) = tokio::io::duplex(BUF_SIZE);
|
2022-06-25 03:15:16 +03:00
|
|
|
let filename = try_get_file_name(path)?;
|
2023-03-31 17:52:07 +03:00
|
|
|
set_content_diposition(res, false, &format!("{}.zip", filename))?;
|
2022-06-11 05:18:07 +03:00
|
|
|
res.headers_mut()
|
2022-06-15 14:57:28 +03:00
|
|
|
.insert("content-type", HeaderValue::from_static("application/zip"));
|
2022-06-11 05:18:07 +03:00
|
|
|
if head_only {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
let path = path.to_owned();
|
2022-06-25 03:15:16 +03:00
|
|
|
let hidden = self.args.hidden.clone();
|
2022-07-02 17:55:22 +03:00
|
|
|
let running = self.running.clone();
|
2023-12-14 13:59:28 +03:00
|
|
|
let compression = self.args.compress.to_compression();
|
2022-06-11 05:18:07 +03:00
|
|
|
tokio::spawn(async move {
|
2023-12-14 13:59:28 +03:00
|
|
|
if let Err(e) = zip_dir(
|
|
|
|
&mut writer,
|
|
|
|
&path,
|
|
|
|
access_paths,
|
|
|
|
&hidden,
|
|
|
|
compression,
|
|
|
|
running,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
{
|
2022-06-11 05:18:07 +03:00
|
|
|
error!("Failed to zip {}, {}", path.display(), e);
|
|
|
|
}
|
|
|
|
});
|
2023-12-21 09:24:20 +03:00
|
|
|
let reader_stream = ReaderStream::new(reader);
|
|
|
|
let stream_body = StreamBody::new(
|
|
|
|
reader_stream
|
|
|
|
.map_ok(Frame::data)
|
|
|
|
.map_err(|err| anyhow!("{err}")),
|
|
|
|
);
|
|
|
|
let boxed_body = stream_body.boxed();
|
|
|
|
*res.body_mut() = boxed_body;
|
2022-05-31 00:49:42 +03:00
|
|
|
Ok(())
|
2022-05-28 13:58:43 +03:00
|
|
|
}
|
|
|
|
|
2022-06-01 17:49:55 +03:00
|
|
|
async fn handle_render_index(
|
|
|
|
&self,
|
|
|
|
path: &Path,
|
2022-08-23 09:24:42 +03:00
|
|
|
query_params: &HashMap<String, String>,
|
2022-06-01 17:49:55 +03:00
|
|
|
headers: &HeaderMap<HeaderValue>,
|
2022-06-11 05:18:07 +03:00
|
|
|
head_only: bool,
|
2023-02-21 07:42:40 +03:00
|
|
|
user: Option<String>,
|
2023-06-01 13:52:05 +03:00
|
|
|
access_paths: AccessPaths,
|
2022-06-01 17:49:55 +03:00
|
|
|
res: &mut Response,
|
2023-02-21 12:23:24 +03:00
|
|
|
) -> Result<()> {
|
2022-06-17 03:41:01 +03:00
|
|
|
let index_path = path.join(INDEX_NAME);
|
|
|
|
if fs::metadata(&index_path)
|
2022-06-01 17:49:55 +03:00
|
|
|
.await
|
|
|
|
.ok()
|
|
|
|
.map(|v| v.is_file())
|
|
|
|
.unwrap_or_default()
|
|
|
|
{
|
2022-06-17 03:41:01 +03:00
|
|
|
self.handle_send_file(&index_path, headers, head_only, res)
|
2022-06-11 05:18:07 +03:00
|
|
|
.await?;
|
2022-06-17 14:02:13 +03:00
|
|
|
} else if self.args.render_try_index {
|
2023-06-01 13:52:05 +03:00
|
|
|
self.handle_ls_dir(path, true, query_params, head_only, user, access_paths, res)
|
2022-08-23 09:24:42 +03:00
|
|
|
.await?;
|
2022-06-01 17:49:55 +03:00
|
|
|
} else {
|
2022-06-15 14:57:28 +03:00
|
|
|
status_not_found(res)
|
2022-06-01 17:49:55 +03:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn handle_render_spa(
|
|
|
|
&self,
|
|
|
|
path: &Path,
|
|
|
|
headers: &HeaderMap<HeaderValue>,
|
2022-06-11 05:18:07 +03:00
|
|
|
head_only: bool,
|
2022-06-01 17:49:55 +03:00
|
|
|
res: &mut Response,
|
2023-02-21 12:23:24 +03:00
|
|
|
) -> Result<()> {
|
2022-06-01 17:49:55 +03:00
|
|
|
if path.extension().is_none() {
|
2023-11-04 11:58:19 +03:00
|
|
|
let path = self.args.serve_path.join(INDEX_NAME);
|
2022-06-11 05:18:07 +03:00
|
|
|
self.handle_send_file(&path, headers, head_only, res)
|
|
|
|
.await?;
|
2022-06-01 17:49:55 +03:00
|
|
|
} else {
|
2022-06-15 14:57:28 +03:00
|
|
|
status_not_found(res)
|
2022-06-01 17:49:55 +03:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-09-05 05:30:45 +03:00
|
|
|
async fn handle_assets(
|
|
|
|
&self,
|
|
|
|
req_path: &str,
|
|
|
|
headers: &HeaderMap<HeaderValue>,
|
|
|
|
res: &mut Response,
|
2023-02-21 12:23:24 +03:00
|
|
|
) -> Result<bool> {
|
2022-06-21 18:01:00 +03:00
|
|
|
if let Some(name) = req_path.strip_prefix(&self.assets_prefix) {
|
2023-11-04 11:58:19 +03:00
|
|
|
match self.args.assets.as_ref() {
|
2022-09-05 05:30:45 +03:00
|
|
|
Some(assets_path) => {
|
|
|
|
let path = assets_path.join(name);
|
|
|
|
self.handle_send_file(&path, headers, false, res).await?;
|
2022-06-21 18:01:00 +03:00
|
|
|
}
|
2022-09-05 05:30:45 +03:00
|
|
|
None => match name {
|
|
|
|
"index.js" => {
|
2023-12-21 09:24:20 +03:00
|
|
|
*res.body_mut() = body_full(INDEX_JS);
|
2022-09-05 05:30:45 +03:00
|
|
|
res.headers_mut().insert(
|
|
|
|
"content-type",
|
2023-11-15 14:44:44 +03:00
|
|
|
HeaderValue::from_static("application/javascript; charset=UTF-8"),
|
2022-09-05 05:30:45 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
"index.css" => {
|
2023-12-21 09:24:20 +03:00
|
|
|
*res.body_mut() = body_full(INDEX_CSS);
|
2023-11-15 14:44:44 +03:00
|
|
|
res.headers_mut().insert(
|
|
|
|
"content-type",
|
|
|
|
HeaderValue::from_static("text/css; charset=UTF-8"),
|
|
|
|
);
|
2022-09-05 05:30:45 +03:00
|
|
|
}
|
|
|
|
"favicon.ico" => {
|
2023-12-21 09:24:20 +03:00
|
|
|
*res.body_mut() = body_full(FAVICON_ICO);
|
2022-09-05 05:30:45 +03:00
|
|
|
res.headers_mut()
|
|
|
|
.insert("content-type", HeaderValue::from_static("image/x-icon"));
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
status_not_found(res);
|
|
|
|
}
|
|
|
|
},
|
2022-06-21 18:01:00 +03:00
|
|
|
}
|
|
|
|
res.headers_mut().insert(
|
|
|
|
"cache-control",
|
2023-11-15 14:44:44 +03:00
|
|
|
HeaderValue::from_static("public, max-age=31536000, immutable"),
|
|
|
|
);
|
|
|
|
res.headers_mut().insert(
|
|
|
|
"x-content-type-options",
|
|
|
|
HeaderValue::from_static("nosniff"),
|
2022-06-21 18:01:00 +03:00
|
|
|
);
|
|
|
|
Ok(true)
|
2022-06-12 03:43:50 +03:00
|
|
|
} else {
|
2022-06-21 18:01:00 +03:00
|
|
|
Ok(false)
|
2022-06-12 03:43:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 00:49:42 +03:00
|
|
|
async fn handle_send_file(
|
|
|
|
&self,
|
|
|
|
path: &Path,
|
|
|
|
headers: &HeaderMap<HeaderValue>,
|
2022-06-11 05:18:07 +03:00
|
|
|
head_only: bool,
|
2022-05-31 00:49:42 +03:00
|
|
|
res: &mut Response,
|
2023-02-21 12:23:24 +03:00
|
|
|
) -> Result<()> {
|
2022-05-30 06:22:28 +03:00
|
|
|
let (file, meta) = tokio::join!(fs::File::open(path), fs::metadata(path),);
|
2022-05-31 05:58:32 +03:00
|
|
|
let (mut file, meta) = (file?, meta?);
|
2023-12-21 10:46:55 +03:00
|
|
|
let size = meta.len();
|
2022-06-16 05:24:32 +03:00
|
|
|
let mut use_range = true;
|
2022-05-31 05:58:32 +03:00
|
|
|
if let Some((etag, last_modified)) = extract_cache_headers(&meta) {
|
|
|
|
let cached = {
|
2022-05-31 00:49:42 +03:00
|
|
|
if let Some(if_none_match) = headers.typed_get::<IfNoneMatch>() {
|
2022-05-30 06:22:28 +03:00
|
|
|
!if_none_match.precondition_passes(&etag)
|
2022-05-31 00:49:42 +03:00
|
|
|
} else if let Some(if_modified_since) = headers.typed_get::<IfModifiedSince>() {
|
2022-05-31 05:58:32 +03:00
|
|
|
!if_modified_since.is_modified(last_modified.into())
|
2022-05-30 06:22:28 +03:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
};
|
2022-05-31 05:58:32 +03:00
|
|
|
if cached {
|
2022-06-15 14:57:28 +03:00
|
|
|
*res.status_mut() = StatusCode::NOT_MODIFIED;
|
2022-05-31 00:49:42 +03:00
|
|
|
return Ok(());
|
2022-05-30 06:22:28 +03:00
|
|
|
}
|
2022-06-16 05:24:32 +03:00
|
|
|
|
|
|
|
res.headers_mut().typed_insert(last_modified);
|
|
|
|
res.headers_mut().typed_insert(etag.clone());
|
|
|
|
|
2022-05-31 05:58:32 +03:00
|
|
|
if headers.typed_get::<Range>().is_some() {
|
2022-06-16 05:24:32 +03:00
|
|
|
use_range = headers
|
2022-05-31 05:58:32 +03:00
|
|
|
.typed_get::<IfRange>()
|
|
|
|
.map(|if_range| !if_range.is_modified(Some(&etag), Some(&last_modified)))
|
|
|
|
// Always be fresh if there is no validators
|
|
|
|
.unwrap_or(true);
|
|
|
|
} else {
|
2022-06-16 05:24:32 +03:00
|
|
|
use_range = false;
|
2022-05-31 05:58:32 +03:00
|
|
|
}
|
2022-05-30 06:22:28 +03:00
|
|
|
}
|
2022-06-16 05:24:32 +03:00
|
|
|
|
|
|
|
let range = if use_range {
|
2023-12-21 10:46:55 +03:00
|
|
|
headers.get(RANGE).map(|range| {
|
|
|
|
range
|
|
|
|
.to_str()
|
|
|
|
.ok()
|
|
|
|
.and_then(|range| parse_range(range, size))
|
|
|
|
})
|
2022-05-31 05:58:32 +03:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2022-06-16 05:24:32 +03:00
|
|
|
|
2023-03-01 04:36:59 +03:00
|
|
|
res.headers_mut().insert(
|
|
|
|
CONTENT_TYPE,
|
|
|
|
HeaderValue::from_str(&get_content_type(path).await?)?,
|
|
|
|
);
|
2022-06-16 05:24:32 +03:00
|
|
|
|
2022-06-25 03:15:16 +03:00
|
|
|
let filename = try_get_file_name(path)?;
|
2023-03-31 17:52:07 +03:00
|
|
|
set_content_diposition(res, true, filename)?;
|
2022-06-19 09:23:10 +03:00
|
|
|
|
2022-06-11 05:18:07 +03:00
|
|
|
res.headers_mut().typed_insert(AcceptRanges::bytes());
|
|
|
|
|
2022-06-16 05:24:32 +03:00
|
|
|
if let Some(range) = range {
|
2023-12-21 10:46:55 +03:00
|
|
|
if let Some((start, end)) = range {
|
|
|
|
file.seek(SeekFrom::Start(start)).await?;
|
|
|
|
let range_size = end - start + 1;
|
2022-06-16 05:24:32 +03:00
|
|
|
*res.status_mut() = StatusCode::PARTIAL_CONTENT;
|
2023-12-21 10:46:55 +03:00
|
|
|
let content_range = format!("bytes {}-{}/{}", start, end, size);
|
2022-06-16 05:24:32 +03:00
|
|
|
res.headers_mut()
|
2023-02-21 12:23:24 +03:00
|
|
|
.insert(CONTENT_RANGE, content_range.parse()?);
|
2022-06-16 05:24:32 +03:00
|
|
|
res.headers_mut()
|
2023-12-21 09:24:20 +03:00
|
|
|
.insert(CONTENT_LENGTH, format!("{range_size}").parse()?);
|
2022-06-16 05:24:32 +03:00
|
|
|
if head_only {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2023-12-21 09:24:20 +03:00
|
|
|
|
|
|
|
let stream_body = StreamBody::new(
|
|
|
|
LengthLimitedStream::new(file, range_size as usize)
|
|
|
|
.map_ok(Frame::data)
|
|
|
|
.map_err(|err| anyhow!("{err}")),
|
|
|
|
);
|
|
|
|
let boxed_body = stream_body.boxed();
|
|
|
|
*res.body_mut() = boxed_body;
|
2022-06-16 05:24:32 +03:00
|
|
|
} else {
|
|
|
|
*res.status_mut() = StatusCode::RANGE_NOT_SATISFIABLE;
|
|
|
|
res.headers_mut()
|
2023-02-21 12:23:24 +03:00
|
|
|
.insert(CONTENT_RANGE, format!("bytes */{size}").parse()?);
|
2022-06-16 05:24:32 +03:00
|
|
|
}
|
2022-05-31 05:58:32 +03:00
|
|
|
} else {
|
2022-06-16 05:24:32 +03:00
|
|
|
res.headers_mut()
|
2023-02-21 12:23:24 +03:00
|
|
|
.insert(CONTENT_LENGTH, format!("{size}").parse()?);
|
2022-06-16 05:24:32 +03:00
|
|
|
if head_only {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2023-12-21 09:24:20 +03:00
|
|
|
|
|
|
|
let reader_stream = ReaderStream::new(file);
|
|
|
|
let stream_body = StreamBody::new(
|
|
|
|
reader_stream
|
|
|
|
.map_ok(Frame::data)
|
|
|
|
.map_err(|err| anyhow!("{err}")),
|
|
|
|
);
|
|
|
|
let boxed_body = stream_body.boxed();
|
|
|
|
*res.body_mut() = boxed_body;
|
2022-06-16 05:24:32 +03:00
|
|
|
}
|
2022-05-31 00:49:42 +03:00
|
|
|
Ok(())
|
2022-05-26 11:17:55 +03:00
|
|
|
}
|
|
|
|
|
2023-11-28 02:14:53 +03:00
|
|
|
async fn handle_deal_file(
|
2023-02-20 17:50:24 +03:00
|
|
|
&self,
|
|
|
|
path: &Path,
|
2023-11-28 02:14:53 +03:00
|
|
|
kind: DataKind,
|
2023-02-20 17:50:24 +03:00
|
|
|
head_only: bool,
|
2023-02-21 07:42:40 +03:00
|
|
|
user: Option<String>,
|
2023-02-20 17:50:24 +03:00
|
|
|
res: &mut Response,
|
2023-02-21 12:23:24 +03:00
|
|
|
) -> Result<()> {
|
2023-02-20 17:50:24 +03:00
|
|
|
let (file, meta) = tokio::join!(fs::File::open(path), fs::metadata(path),);
|
|
|
|
let (file, meta) = (file?, meta?);
|
2023-11-04 11:58:19 +03:00
|
|
|
let href = format!(
|
|
|
|
"/{}",
|
|
|
|
normalize_path(path.strip_prefix(&self.args.serve_path)?)
|
|
|
|
);
|
2023-02-20 17:50:24 +03:00
|
|
|
let mut buffer: Vec<u8> = vec![];
|
|
|
|
file.take(1024).read_to_end(&mut buffer).await?;
|
|
|
|
let editable = meta.len() <= TEXT_MAX_SIZE && content_inspector::inspect(&buffer).is_text();
|
|
|
|
let data = EditData {
|
|
|
|
href,
|
2023-11-28 02:14:53 +03:00
|
|
|
kind,
|
2023-02-20 17:50:24 +03:00
|
|
|
uri_prefix: self.args.uri_prefix.clone(),
|
|
|
|
allow_upload: self.args.allow_upload,
|
|
|
|
allow_delete: self.args.allow_delete,
|
2023-06-05 06:40:31 +03:00
|
|
|
auth: self.args.auth.exist(),
|
2023-02-21 07:42:40 +03:00
|
|
|
user,
|
2023-02-20 17:50:24 +03:00
|
|
|
editable,
|
|
|
|
};
|
|
|
|
res.headers_mut()
|
|
|
|
.typed_insert(ContentType::from(mime_guess::mime::TEXT_HTML_UTF_8));
|
|
|
|
let output = self
|
|
|
|
.html
|
2023-08-13 10:05:45 +03:00
|
|
|
.replace("__ASSETS_PREFIX__", &self.assets_prefix)
|
2023-02-21 12:23:24 +03:00
|
|
|
.replace("__INDEX_DATA__", &serde_json::to_string(&data)?);
|
2023-02-20 17:50:24 +03:00
|
|
|
res.headers_mut()
|
|
|
|
.typed_insert(ContentLength(output.as_bytes().len() as u64));
|
|
|
|
if head_only {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2023-12-21 09:24:20 +03:00
|
|
|
*res.body_mut() = body_full(output);
|
2023-02-20 17:50:24 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-06-04 14:08:18 +03:00
|
|
|
async fn handle_propfind_dir(
|
|
|
|
&self,
|
|
|
|
path: &Path,
|
|
|
|
headers: &HeaderMap<HeaderValue>,
|
2023-06-01 13:52:05 +03:00
|
|
|
access_paths: AccessPaths,
|
2022-06-04 14:08:18 +03:00
|
|
|
res: &mut Response,
|
2023-02-21 12:23:24 +03:00
|
|
|
) -> Result<()> {
|
2022-06-04 14:08:18 +03:00
|
|
|
let depth: u32 = match headers.get("depth") {
|
|
|
|
Some(v) => match v.to_str().ok().and_then(|v| v.parse().ok()) {
|
|
|
|
Some(v) => v,
|
|
|
|
None => {
|
2022-06-15 14:57:28 +03:00
|
|
|
*res.status_mut() = StatusCode::BAD_REQUEST;
|
2022-06-04 14:08:18 +03:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
},
|
2022-06-12 03:43:50 +03:00
|
|
|
None => 1,
|
2022-06-04 07:51:56 +03:00
|
|
|
};
|
2023-11-04 11:58:19 +03:00
|
|
|
let mut paths = match self.to_pathitem(path, &self.args.serve_path).await? {
|
2023-02-21 12:23:24 +03:00
|
|
|
Some(v) => vec![v],
|
|
|
|
None => vec![],
|
|
|
|
};
|
2022-06-12 03:43:50 +03:00
|
|
|
if depth != 0 {
|
2023-11-04 11:58:19 +03:00
|
|
|
match self
|
|
|
|
.list_dir(path, &self.args.serve_path, access_paths)
|
|
|
|
.await
|
|
|
|
{
|
2022-06-04 14:08:18 +03:00
|
|
|
Ok(child) => paths.extend(child),
|
|
|
|
Err(_) => {
|
2022-06-15 14:57:28 +03:00
|
|
|
status_forbid(res);
|
2022-06-04 14:08:18 +03:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-04 07:51:56 +03:00
|
|
|
let output = paths
|
|
|
|
.iter()
|
2022-06-04 14:08:18 +03:00
|
|
|
.map(|v| v.to_dav_xml(self.args.uri_prefix.as_str()))
|
2022-06-04 07:51:56 +03:00
|
|
|
.fold(String::new(), |mut acc, v| {
|
|
|
|
acc.push_str(&v);
|
|
|
|
acc
|
|
|
|
});
|
2022-06-05 02:35:05 +03:00
|
|
|
res_multistatus(res, &output);
|
2022-06-04 07:51:56 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-02-21 12:23:24 +03:00
|
|
|
async fn handle_propfind_file(&self, path: &Path, res: &mut Response) -> Result<()> {
|
2023-11-04 11:58:19 +03:00
|
|
|
if let Some(pathitem) = self.to_pathitem(path, &self.args.serve_path).await? {
|
2022-06-05 02:35:05 +03:00
|
|
|
res_multistatus(res, &pathitem.to_dav_xml(self.args.uri_prefix.as_str()));
|
2022-06-04 07:51:56 +03:00
|
|
|
} else {
|
2022-06-15 14:57:28 +03:00
|
|
|
status_not_found(res);
|
2022-06-04 07:51:56 +03:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-02-21 12:23:24 +03:00
|
|
|
async fn handle_mkcol(&self, path: &Path, res: &mut Response) -> Result<()> {
|
2022-06-04 07:51:56 +03:00
|
|
|
fs::create_dir_all(path).await?;
|
2022-06-15 14:57:28 +03:00
|
|
|
*res.status_mut() = StatusCode::CREATED;
|
2022-06-04 07:51:56 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-02-21 12:23:24 +03:00
|
|
|
async fn handle_copy(&self, path: &Path, req: &Request, res: &mut Response) -> Result<()> {
|
2022-07-04 18:25:05 +03:00
|
|
|
let dest = match self.extract_dest(req, res) {
|
2022-06-04 07:51:56 +03:00
|
|
|
Some(dest) => dest,
|
|
|
|
None => {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let meta = fs::symlink_metadata(path).await?;
|
|
|
|
if meta.is_dir() {
|
2022-06-15 14:57:28 +03:00
|
|
|
status_forbid(res);
|
2022-06-04 07:51:56 +03:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
ensure_path_parent(&dest).await?;
|
|
|
|
|
|
|
|
fs::copy(path, &dest).await?;
|
|
|
|
|
2022-06-15 14:57:28 +03:00
|
|
|
status_no_content(res);
|
2022-06-04 07:51:56 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-02-21 12:23:24 +03:00
|
|
|
async fn handle_move(&self, path: &Path, req: &Request, res: &mut Response) -> Result<()> {
|
2022-07-04 18:25:05 +03:00
|
|
|
let dest = match self.extract_dest(req, res) {
|
2022-06-04 07:51:56 +03:00
|
|
|
Some(dest) => dest,
|
|
|
|
None => {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ensure_path_parent(&dest).await?;
|
|
|
|
|
|
|
|
fs::rename(path, &dest).await?;
|
|
|
|
|
2022-06-15 14:57:28 +03:00
|
|
|
status_no_content(res);
|
2022-06-04 07:51:56 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-02-21 12:23:24 +03:00
|
|
|
async fn handle_lock(&self, req_path: &str, auth: bool, res: &mut Response) -> Result<()> {
|
2022-06-19 06:26:03 +03:00
|
|
|
let token = if auth {
|
2022-06-04 19:09:21 +03:00
|
|
|
format!("opaquelocktoken:{}", Uuid::new_v4())
|
2022-06-19 06:26:03 +03:00
|
|
|
} else {
|
|
|
|
Utc::now().timestamp().to_string()
|
2022-06-04 19:09:21 +03:00
|
|
|
};
|
|
|
|
|
2022-06-04 14:08:18 +03:00
|
|
|
res.headers_mut().insert(
|
|
|
|
"content-type",
|
2022-06-15 14:57:28 +03:00
|
|
|
HeaderValue::from_static("application/xml; charset=utf-8"),
|
2022-06-04 14:08:18 +03:00
|
|
|
);
|
|
|
|
res.headers_mut()
|
2023-02-21 12:23:24 +03:00
|
|
|
.insert("lock-token", format!("<{token}>").parse()?);
|
2022-06-04 19:09:21 +03:00
|
|
|
|
2023-12-21 09:24:20 +03:00
|
|
|
*res.body_mut() = body_full(format!(
|
2022-06-04 14:08:18 +03:00
|
|
|
r#"<?xml version="1.0" encoding="utf-8"?>
|
|
|
|
<D:prop xmlns:D="DAV:"><D:lockdiscovery><D:activelock>
|
2023-02-19 07:24:42 +03:00
|
|
|
<D:locktoken><D:href>{token}</D:href></D:locktoken>
|
|
|
|
<D:lockroot><D:href>{req_path}</D:href></D:lockroot>
|
|
|
|
</D:activelock></D:lockdiscovery></D:prop>"#
|
2022-06-04 14:08:18 +03:00
|
|
|
));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-02-21 12:23:24 +03:00
|
|
|
async fn handle_proppatch(&self, req_path: &str, res: &mut Response) -> Result<()> {
|
2022-06-05 02:35:05 +03:00
|
|
|
let output = format!(
|
|
|
|
r#"<D:response>
|
2023-02-19 07:24:42 +03:00
|
|
|
<D:href>{req_path}</D:href>
|
2022-06-05 02:35:05 +03:00
|
|
|
<D:propstat>
|
|
|
|
<D:prop>
|
|
|
|
</D:prop>
|
|
|
|
<D:status>HTTP/1.1 403 Forbidden</D:status>
|
|
|
|
</D:propstat>
|
2023-02-19 07:24:42 +03:00
|
|
|
</D:response>"#
|
2022-06-05 02:35:05 +03:00
|
|
|
);
|
|
|
|
res_multistatus(res, &output);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-02-21 07:42:40 +03:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2022-05-31 00:49:42 +03:00
|
|
|
fn send_index(
|
|
|
|
&self,
|
|
|
|
path: &Path,
|
|
|
|
mut paths: Vec<PathItem>,
|
2022-06-10 02:41:09 +03:00
|
|
|
exist: bool,
|
2022-08-23 09:24:42 +03:00
|
|
|
query_params: &HashMap<String, String>,
|
2022-06-11 05:18:07 +03:00
|
|
|
head_only: bool,
|
2023-02-21 07:42:40 +03:00
|
|
|
user: Option<String>,
|
2023-08-24 13:32:34 +03:00
|
|
|
access_paths: AccessPaths,
|
2022-06-11 05:18:07 +03:00
|
|
|
res: &mut Response,
|
2023-02-21 12:23:24 +03:00
|
|
|
) -> Result<()> {
|
2022-08-23 09:24:42 +03:00
|
|
|
if let Some(sort) = query_params.get("sort") {
|
|
|
|
if sort == "name" {
|
2023-09-06 18:25:04 +03:00
|
|
|
paths.sort_by(|v1, v2| v1.sort_by_name(v2))
|
2022-08-23 09:24:42 +03:00
|
|
|
} else if sort == "mtime" {
|
2023-09-06 18:25:04 +03:00
|
|
|
paths.sort_by(|v1, v2| v1.sort_by_mtime(v2))
|
2022-08-23 09:24:42 +03:00
|
|
|
} else if sort == "size" {
|
2023-09-06 18:25:04 +03:00
|
|
|
paths.sort_by(|v1, v2| v1.sort_by_size(v2))
|
2022-08-23 09:24:42 +03:00
|
|
|
}
|
|
|
|
if query_params
|
|
|
|
.get("order")
|
|
|
|
.map(|v| v == "desc")
|
|
|
|
.unwrap_or_default()
|
|
|
|
{
|
|
|
|
paths.reverse()
|
|
|
|
}
|
|
|
|
} else {
|
2023-09-06 18:25:04 +03:00
|
|
|
paths.sort_by(|v1, v2| v1.sort_by_name(v2))
|
2022-08-23 09:24:42 +03:00
|
|
|
}
|
2023-02-20 06:05:53 +03:00
|
|
|
if query_params.contains_key("simple") {
|
|
|
|
let output = paths
|
|
|
|
.into_iter()
|
|
|
|
.map(|v| {
|
|
|
|
if v.is_dir() {
|
|
|
|
format!("{}/\n", v.name)
|
|
|
|
} else {
|
|
|
|
format!("{}\n", v.name)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join("");
|
|
|
|
res.headers_mut()
|
|
|
|
.typed_insert(ContentType::from(mime_guess::mime::TEXT_HTML_UTF_8));
|
|
|
|
res.headers_mut()
|
|
|
|
.typed_insert(ContentLength(output.as_bytes().len() as u64));
|
2023-12-21 09:24:20 +03:00
|
|
|
*res.body_mut() = body_full(output);
|
2023-02-20 06:05:53 +03:00
|
|
|
if head_only {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
return Ok(());
|
|
|
|
}
|
2023-11-04 11:58:19 +03:00
|
|
|
let href = format!(
|
|
|
|
"/{}",
|
|
|
|
normalize_path(path.strip_prefix(&self.args.serve_path)?)
|
|
|
|
);
|
2023-08-24 13:32:34 +03:00
|
|
|
let readwrite = access_paths.perm().readwrite();
|
2022-05-29 07:43:40 +03:00
|
|
|
let data = IndexData {
|
2023-02-20 17:50:24 +03:00
|
|
|
kind: DataKind::Index,
|
2022-06-21 14:23:34 +03:00
|
|
|
href,
|
2022-06-19 16:23:19 +03:00
|
|
|
uri_prefix: self.args.uri_prefix.clone(),
|
2023-08-24 13:32:34 +03:00
|
|
|
allow_upload: self.args.allow_upload && readwrite,
|
|
|
|
allow_delete: self.args.allow_delete && readwrite,
|
2022-06-21 02:23:20 +03:00
|
|
|
allow_search: self.args.allow_search,
|
2022-12-10 06:09:42 +03:00
|
|
|
allow_archive: self.args.allow_archive,
|
2022-06-10 02:41:09 +03:00
|
|
|
dir_exists: exist,
|
2023-06-05 06:40:31 +03:00
|
|
|
auth: self.args.auth.exist(),
|
2023-02-21 07:42:40 +03:00
|
|
|
user,
|
2023-02-20 06:05:53 +03:00
|
|
|
paths,
|
|
|
|
};
|
|
|
|
let output = if query_params.contains_key("json") {
|
|
|
|
res.headers_mut()
|
|
|
|
.typed_insert(ContentType::from(mime_guess::mime::APPLICATION_JSON));
|
2023-02-21 12:23:24 +03:00
|
|
|
serde_json::to_string_pretty(&data)?
|
2023-02-20 06:05:53 +03:00
|
|
|
} else {
|
|
|
|
res.headers_mut()
|
|
|
|
.typed_insert(ContentType::from(mime_guess::mime::TEXT_HTML_UTF_8));
|
|
|
|
self.html
|
2023-08-13 10:05:45 +03:00
|
|
|
.replace("__ASSETS_PREFIX__", &self.assets_prefix)
|
2023-02-21 12:23:24 +03:00
|
|
|
.replace("__INDEX_DATA__", &serde_json::to_string(&data)?)
|
2022-05-29 07:43:40 +03:00
|
|
|
};
|
2022-06-11 05:18:07 +03:00
|
|
|
res.headers_mut()
|
|
|
|
.typed_insert(ContentLength(output.as_bytes().len() as u64));
|
2023-11-15 14:44:44 +03:00
|
|
|
res.headers_mut()
|
|
|
|
.typed_insert(CacheControl::new().with_no_cache());
|
|
|
|
res.headers_mut().insert(
|
|
|
|
"x-content-type-options",
|
|
|
|
HeaderValue::from_static("nosniff"),
|
|
|
|
);
|
2022-06-11 05:18:07 +03:00
|
|
|
if head_only {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2023-12-21 09:24:20 +03:00
|
|
|
*res.body_mut() = body_full(output);
|
2022-05-31 00:49:42 +03:00
|
|
|
Ok(())
|
2022-05-29 07:43:40 +03:00
|
|
|
}
|
|
|
|
|
2023-02-21 12:23:24 +03:00
|
|
|
fn auth_reject(&self, res: &mut Response) -> Result<()> {
|
2022-06-19 06:26:03 +03:00
|
|
|
set_webdav_headers(res);
|
2023-12-07 10:04:14 +03:00
|
|
|
|
|
|
|
www_authenticate(res, &self.args)?;
|
2022-06-19 06:26:03 +03:00
|
|
|
*res.status_mut() = StatusCode::UNAUTHORIZED;
|
2023-02-21 12:23:24 +03:00
|
|
|
Ok(())
|
2022-05-26 13:06:52 +03:00
|
|
|
}
|
|
|
|
|
2022-05-31 15:53:14 +03:00
|
|
|
async fn is_root_contained(&self, path: &Path) -> bool {
|
|
|
|
fs::canonicalize(path)
|
|
|
|
.await
|
|
|
|
.ok()
|
2023-11-04 11:58:19 +03:00
|
|
|
.map(|v| v.starts_with(&self.args.serve_path))
|
2022-05-31 15:53:14 +03:00
|
|
|
.unwrap_or_default()
|
|
|
|
}
|
|
|
|
|
2022-07-04 18:25:05 +03:00
|
|
|
fn extract_dest(&self, req: &Request, res: &mut Response) -> Option<PathBuf> {
|
|
|
|
let headers = req.headers();
|
|
|
|
let dest_path = match self.extract_destination_header(headers) {
|
|
|
|
Some(dest) => dest,
|
|
|
|
None => {
|
|
|
|
*res.status_mut() = StatusCode::BAD_REQUEST;
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
2023-06-01 13:52:05 +03:00
|
|
|
|
|
|
|
let relative_path = match self.resolve_path(&dest_path) {
|
|
|
|
Some(v) => v,
|
|
|
|
None => {
|
|
|
|
*res.status_mut() = StatusCode::BAD_REQUEST;
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-07-04 18:25:05 +03:00
|
|
|
let authorization = headers.get(AUTHORIZATION);
|
2023-11-03 15:36:23 +03:00
|
|
|
let guard = self
|
|
|
|
.args
|
|
|
|
.auth
|
|
|
|
.guard(&relative_path, req.method(), authorization);
|
2022-07-04 18:25:05 +03:00
|
|
|
|
2023-06-01 13:52:05 +03:00
|
|
|
match guard {
|
|
|
|
(_, Some(_)) => {}
|
|
|
|
_ => {
|
|
|
|
status_forbid(res);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let dest = match self.join_path(&relative_path) {
|
2022-07-04 18:25:05 +03:00
|
|
|
Some(dest) => dest,
|
|
|
|
None => {
|
|
|
|
*res.status_mut() = StatusCode::BAD_REQUEST;
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(dest)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extract_destination_header(&self, headers: &HeaderMap<HeaderValue>) -> Option<String> {
|
2022-06-04 07:51:56 +03:00
|
|
|
let dest = headers.get("Destination")?.to_str().ok()?;
|
|
|
|
let uri: Uri = dest.parse().ok()?;
|
2022-07-04 18:25:05 +03:00
|
|
|
Some(uri.path().to_string())
|
2022-06-04 07:51:56 +03:00
|
|
|
}
|
|
|
|
|
2023-06-01 13:52:05 +03:00
|
|
|
fn resolve_path(&self, path: &str) -> Option<String> {
|
|
|
|
let path = path.trim_matches('/');
|
|
|
|
let path = decode_uri(path)?;
|
|
|
|
let prefix = self.args.path_prefix.as_str();
|
|
|
|
if prefix == "/" {
|
|
|
|
return Some(path.to_string());
|
|
|
|
}
|
|
|
|
path.strip_prefix(prefix.trim_start_matches('/'))
|
|
|
|
.map(|v| v.trim_matches('/').to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn join_path(&self, path: &str) -> Option<PathBuf> {
|
|
|
|
if path.is_empty() {
|
2023-11-04 11:58:19 +03:00
|
|
|
return Some(self.args.serve_path.clone());
|
2022-11-10 14:28:01 +03:00
|
|
|
}
|
2023-06-01 13:52:05 +03:00
|
|
|
let path = if cfg!(windows) {
|
|
|
|
path.replace('/', "\\")
|
2022-05-26 11:17:55 +03:00
|
|
|
} else {
|
2023-06-01 13:52:05 +03:00
|
|
|
path.to_string()
|
2022-05-26 11:17:55 +03:00
|
|
|
};
|
2023-11-04 11:58:19 +03:00
|
|
|
Some(self.args.serve_path.join(path))
|
2022-06-02 03:32:31 +03:00
|
|
|
}
|
|
|
|
|
2023-06-01 13:52:05 +03:00
|
|
|
async fn list_dir(
|
|
|
|
&self,
|
|
|
|
entry_path: &Path,
|
|
|
|
base_path: &Path,
|
|
|
|
access_paths: AccessPaths,
|
|
|
|
) -> Result<Vec<PathItem>> {
|
|
|
|
let mut paths: Vec<PathItem> = vec![];
|
|
|
|
if access_paths.perm().indexonly() {
|
2023-12-21 12:28:13 +03:00
|
|
|
for name in access_paths.child_names() {
|
2023-06-01 13:52:05 +03:00
|
|
|
let entry_path = entry_path.join(name);
|
|
|
|
self.add_pathitem(&mut paths, base_path, &entry_path).await;
|
|
|
|
}
|
2022-06-04 14:08:18 +03:00
|
|
|
} else {
|
2023-06-01 13:52:05 +03:00
|
|
|
let mut rd = fs::read_dir(entry_path).await?;
|
|
|
|
while let Ok(Some(entry)) = rd.next_entry().await {
|
|
|
|
let entry_path = entry.path();
|
|
|
|
self.add_pathitem(&mut paths, base_path, &entry_path).await;
|
|
|
|
}
|
2022-06-02 03:32:31 +03:00
|
|
|
}
|
2023-06-01 13:52:05 +03:00
|
|
|
Ok(paths)
|
2022-05-31 15:53:14 +03:00
|
|
|
}
|
|
|
|
|
2023-06-01 13:52:05 +03:00
|
|
|
async fn add_pathitem(&self, paths: &mut Vec<PathItem>, base_path: &Path, entry_path: &Path) {
|
|
|
|
let base_name = get_file_name(entry_path);
|
|
|
|
if let Ok(Some(item)) = self.to_pathitem(entry_path, base_path).await {
|
|
|
|
if is_hidden(&self.args.hidden, base_name, item.is_dir()) {
|
|
|
|
return;
|
2022-06-04 07:51:56 +03:00
|
|
|
}
|
2023-06-01 13:52:05 +03:00
|
|
|
paths.push(item);
|
2022-06-04 07:51:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-21 12:23:24 +03:00
|
|
|
async fn to_pathitem<P: AsRef<Path>>(&self, path: P, base_path: P) -> Result<Option<PathItem>> {
|
2022-05-31 15:53:14 +03:00
|
|
|
let path = path.as_ref();
|
|
|
|
let (meta, meta2) = tokio::join!(fs::metadata(&path), fs::symlink_metadata(&path));
|
|
|
|
let (meta, meta2) = (meta?, meta2?);
|
|
|
|
let is_symlink = meta2.is_symlink();
|
|
|
|
if !self.args.allow_symlink && is_symlink && !self.is_root_contained(path).await {
|
|
|
|
return Ok(None);
|
2022-05-26 11:17:55 +03:00
|
|
|
}
|
2022-05-31 15:53:14 +03:00
|
|
|
let is_dir = meta.is_dir();
|
|
|
|
let path_type = match (is_symlink, is_dir) {
|
|
|
|
(true, true) => PathType::SymlinkDir,
|
|
|
|
(false, true) => PathType::Dir,
|
|
|
|
(true, false) => PathType::SymlinkFile,
|
|
|
|
(false, false) => PathType::File,
|
|
|
|
};
|
|
|
|
let mtime = to_timestamp(&meta.modified()?);
|
|
|
|
let size = match path_type {
|
|
|
|
PathType::Dir | PathType::SymlinkDir => None,
|
|
|
|
PathType::File | PathType::SymlinkFile => Some(meta.len()),
|
|
|
|
};
|
2023-02-21 12:23:24 +03:00
|
|
|
let rel_path = path.strip_prefix(base_path)?;
|
2022-05-31 15:53:14 +03:00
|
|
|
let name = normalize_path(rel_path);
|
|
|
|
Ok(Some(PathItem {
|
|
|
|
path_type,
|
|
|
|
name,
|
|
|
|
mtime,
|
|
|
|
size,
|
|
|
|
}))
|
2022-05-26 11:17:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-28 02:14:53 +03:00
|
|
|
#[derive(Debug, Serialize, PartialEq)]
|
2023-02-20 17:50:24 +03:00
|
|
|
enum DataKind {
|
|
|
|
Index,
|
|
|
|
Edit,
|
2023-11-28 02:14:53 +03:00
|
|
|
View,
|
2023-02-20 17:50:24 +03:00
|
|
|
}
|
|
|
|
|
2022-06-04 07:51:56 +03:00
|
|
|
#[derive(Debug, Serialize)]
|
2022-05-29 07:43:40 +03:00
|
|
|
struct IndexData {
|
2022-06-19 16:23:19 +03:00
|
|
|
href: String,
|
2023-02-20 17:50:24 +03:00
|
|
|
kind: DataKind,
|
2022-06-19 16:23:19 +03:00
|
|
|
uri_prefix: String,
|
2022-05-31 03:38:30 +03:00
|
|
|
allow_upload: bool,
|
|
|
|
allow_delete: bool,
|
2022-06-21 02:23:20 +03:00
|
|
|
allow_search: bool,
|
2022-12-10 06:09:42 +03:00
|
|
|
allow_archive: bool,
|
2022-06-10 02:41:09 +03:00
|
|
|
dir_exists: bool,
|
2023-02-21 07:42:40 +03:00
|
|
|
auth: bool,
|
|
|
|
user: Option<String>,
|
2023-02-20 06:05:53 +03:00
|
|
|
paths: Vec<PathItem>,
|
2022-05-26 11:17:55 +03:00
|
|
|
}
|
|
|
|
|
2023-02-20 17:50:24 +03:00
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
struct EditData {
|
|
|
|
href: String,
|
|
|
|
kind: DataKind,
|
|
|
|
uri_prefix: String,
|
|
|
|
allow_upload: bool,
|
|
|
|
allow_delete: bool,
|
2023-02-21 07:42:40 +03:00
|
|
|
auth: bool,
|
|
|
|
user: Option<String>,
|
2023-02-20 17:50:24 +03:00
|
|
|
editable: bool,
|
|
|
|
}
|
|
|
|
|
2022-05-26 11:17:55 +03:00
|
|
|
#[derive(Debug, Serialize, Eq, PartialEq, Ord, PartialOrd)]
|
|
|
|
struct PathItem {
|
|
|
|
path_type: PathType,
|
|
|
|
name: String,
|
2022-05-30 06:22:28 +03:00
|
|
|
mtime: u64,
|
2022-05-26 11:17:55 +03:00
|
|
|
size: Option<u64>,
|
|
|
|
}
|
|
|
|
|
2022-06-04 07:51:56 +03:00
|
|
|
impl PathItem {
|
2022-06-15 15:24:53 +03:00
|
|
|
pub fn is_dir(&self) -> bool {
|
|
|
|
self.path_type == PathType::Dir || self.path_type == PathType::SymlinkDir
|
|
|
|
}
|
|
|
|
|
2022-06-04 14:08:18 +03:00
|
|
|
pub fn to_dav_xml(&self, prefix: &str) -> String {
|
2023-02-21 12:23:24 +03:00
|
|
|
let mtime = match Utc.timestamp_millis_opt(self.mtime as i64) {
|
|
|
|
LocalResult::Single(v) => v.to_rfc2822(),
|
|
|
|
_ => String::new(),
|
|
|
|
};
|
2022-06-15 15:24:53 +03:00
|
|
|
let mut href = encode_uri(&format!("{}{}", prefix, &self.name));
|
|
|
|
if self.is_dir() && !href.ends_with('/') {
|
|
|
|
href.push('/');
|
|
|
|
}
|
2022-06-12 03:43:50 +03:00
|
|
|
let displayname = escape_str_pcdata(self.base_name());
|
2022-06-04 07:51:56 +03:00
|
|
|
match self.path_type {
|
|
|
|
PathType::Dir | PathType::SymlinkDir => format!(
|
|
|
|
r#"<D:response>
|
2023-02-19 07:24:42 +03:00
|
|
|
<D:href>{href}</D:href>
|
2022-06-04 07:51:56 +03:00
|
|
|
<D:propstat>
|
|
|
|
<D:prop>
|
2023-02-19 07:24:42 +03:00
|
|
|
<D:displayname>{displayname}</D:displayname>
|
|
|
|
<D:getlastmodified>{mtime}</D:getlastmodified>
|
2022-06-04 07:51:56 +03:00
|
|
|
<D:resourcetype><D:collection/></D:resourcetype>
|
|
|
|
</D:prop>
|
|
|
|
<D:status>HTTP/1.1 200 OK</D:status>
|
|
|
|
</D:propstat>
|
2023-02-19 07:24:42 +03:00
|
|
|
</D:response>"#
|
2022-06-04 07:51:56 +03:00
|
|
|
),
|
|
|
|
PathType::File | PathType::SymlinkFile => format!(
|
|
|
|
r#"<D:response>
|
2022-06-09 16:35:52 +03:00
|
|
|
<D:href>{}</D:href>
|
2022-06-04 07:51:56 +03:00
|
|
|
<D:propstat>
|
|
|
|
<D:prop>
|
|
|
|
<D:displayname>{}</D:displayname>
|
|
|
|
<D:getcontentlength>{}</D:getcontentlength>
|
|
|
|
<D:getlastmodified>{}</D:getlastmodified>
|
|
|
|
<D:resourcetype></D:resourcetype>
|
|
|
|
</D:prop>
|
|
|
|
<D:status>HTTP/1.1 200 OK</D:status>
|
|
|
|
</D:propstat>
|
|
|
|
</D:response>"#,
|
2022-06-09 16:35:52 +03:00
|
|
|
href,
|
|
|
|
displayname,
|
2022-06-04 07:51:56 +03:00
|
|
|
self.size.unwrap_or_default(),
|
|
|
|
mtime
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
2023-09-06 18:25:04 +03:00
|
|
|
|
2022-06-25 03:15:16 +03:00
|
|
|
pub fn base_name(&self) -> &str {
|
|
|
|
self.name.split('/').last().unwrap_or_default()
|
2022-06-12 03:43:50 +03:00
|
|
|
}
|
2023-09-06 18:25:04 +03:00
|
|
|
|
|
|
|
pub fn sort_by_name(&self, other: &Self) -> Ordering {
|
|
|
|
match self.path_type.cmp(&other.path_type) {
|
|
|
|
Ordering::Equal => {
|
|
|
|
alphanumeric_sort::compare_str(self.name.to_lowercase(), other.name.to_lowercase())
|
|
|
|
}
|
|
|
|
v => v,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn sort_by_mtime(&self, other: &Self) -> Ordering {
|
|
|
|
match self.path_type.cmp(&other.path_type) {
|
|
|
|
Ordering::Equal => self.mtime.cmp(&other.mtime),
|
|
|
|
v => v,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn sort_by_size(&self, other: &Self) -> Ordering {
|
|
|
|
match self.path_type.cmp(&other.path_type) {
|
|
|
|
Ordering::Equal => {
|
|
|
|
if self.is_dir() {
|
|
|
|
alphanumeric_sort::compare_str(
|
|
|
|
self.name.to_lowercase(),
|
|
|
|
other.name.to_lowercase(),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
self.size.unwrap_or(0).cmp(&other.size.unwrap_or(0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
v => v,
|
|
|
|
}
|
|
|
|
}
|
2022-06-04 07:51:56 +03:00
|
|
|
}
|
|
|
|
|
2023-07-04 05:10:48 +03:00
|
|
|
#[derive(Debug, Serialize, Eq, PartialEq)]
|
2022-05-26 11:17:55 +03:00
|
|
|
enum PathType {
|
|
|
|
Dir,
|
|
|
|
SymlinkDir,
|
|
|
|
File,
|
|
|
|
SymlinkFile,
|
|
|
|
}
|
|
|
|
|
2023-07-04 05:10:48 +03:00
|
|
|
impl Ord for PathType {
|
|
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
|
|
|
let to_value = |t: &Self| -> u8 {
|
|
|
|
if matches!(t, Self::Dir | Self::SymlinkDir) {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
};
|
|
|
|
to_value(self).cmp(&to_value(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl PartialOrd for PathType {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 00:49:42 +03:00
|
|
|
fn to_timestamp(time: &SystemTime) -> u64 {
|
2022-05-30 06:22:28 +03:00
|
|
|
time.duration_since(SystemTime::UNIX_EPOCH)
|
2023-02-21 12:23:24 +03:00
|
|
|
.unwrap_or_default()
|
2022-05-30 06:22:28 +03:00
|
|
|
.as_millis() as u64
|
|
|
|
}
|
|
|
|
|
2022-05-26 11:17:55 +03:00
|
|
|
fn normalize_path<P: AsRef<Path>>(path: P) -> String {
|
|
|
|
let path = path.as_ref().to_str().unwrap_or_default();
|
|
|
|
if cfg!(windows) {
|
|
|
|
path.replace('\\', "/")
|
|
|
|
} else {
|
|
|
|
path.to_string()
|
|
|
|
}
|
|
|
|
}
|
2022-05-28 13:58:43 +03:00
|
|
|
|
2023-02-21 12:23:24 +03:00
|
|
|
async fn ensure_path_parent(path: &Path) -> Result<()> {
|
2022-06-04 07:51:56 +03:00
|
|
|
if let Some(parent) = path.parent() {
|
|
|
|
if fs::symlink_metadata(parent).await.is_err() {
|
|
|
|
fs::create_dir_all(&parent).await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-05-29 12:33:21 +03:00
|
|
|
fn add_cors(res: &mut Response) {
|
|
|
|
res.headers_mut()
|
|
|
|
.typed_insert(AccessControlAllowOrigin::ANY);
|
2022-06-15 14:57:28 +03:00
|
|
|
res.headers_mut()
|
|
|
|
.typed_insert(AccessControlAllowCredentials);
|
2022-07-08 11:18:10 +03:00
|
|
|
res.headers_mut().insert(
|
|
|
|
"Access-Control-Allow-Methods",
|
2023-06-02 14:07:43 +03:00
|
|
|
HeaderValue::from_static("*"),
|
2022-07-08 11:18:10 +03:00
|
|
|
);
|
|
|
|
res.headers_mut().insert(
|
|
|
|
"Access-Control-Allow-Headers",
|
2023-06-02 14:07:43 +03:00
|
|
|
HeaderValue::from_static("Authorization,*"),
|
2022-07-08 11:18:10 +03:00
|
|
|
);
|
|
|
|
res.headers_mut().insert(
|
|
|
|
"Access-Control-Expose-Headers",
|
2023-06-02 14:07:43 +03:00
|
|
|
HeaderValue::from_static("Authorization,*"),
|
2022-05-29 12:33:21 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-06-05 02:35:05 +03:00
|
|
|
fn res_multistatus(res: &mut Response, content: &str) {
|
2022-06-04 07:51:56 +03:00
|
|
|
*res.status_mut() = StatusCode::MULTI_STATUS;
|
2022-06-04 14:08:18 +03:00
|
|
|
res.headers_mut().insert(
|
|
|
|
"content-type",
|
2022-06-15 14:57:28 +03:00
|
|
|
HeaderValue::from_static("application/xml; charset=utf-8"),
|
2022-06-04 14:08:18 +03:00
|
|
|
);
|
2023-12-21 09:24:20 +03:00
|
|
|
*res.body_mut() = body_full(format!(
|
2022-06-04 07:51:56 +03:00
|
|
|
r#"<?xml version="1.0" encoding="utf-8" ?>
|
|
|
|
<D:multistatus xmlns:D="DAV:">
|
2023-02-19 07:24:42 +03:00
|
|
|
{content}
|
2022-06-04 07:51:56 +03:00
|
|
|
</D:multistatus>"#,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2022-07-02 17:55:22 +03:00
|
|
|
async fn zip_dir<W: AsyncWrite + Unpin>(
|
|
|
|
writer: &mut W,
|
|
|
|
dir: &Path,
|
2023-06-01 13:52:05 +03:00
|
|
|
access_paths: AccessPaths,
|
2022-07-19 15:37:14 +03:00
|
|
|
hidden: &[String],
|
2023-12-14 13:59:28 +03:00
|
|
|
compression: Compression,
|
2022-07-02 17:55:22 +03:00
|
|
|
running: Arc<AtomicBool>,
|
2023-02-21 12:23:24 +03:00
|
|
|
) -> Result<()> {
|
2023-06-01 17:05:44 +03:00
|
|
|
let mut writer = ZipFileWriter::with_tokio(writer);
|
2022-07-19 15:37:14 +03:00
|
|
|
let hidden = Arc::new(hidden.to_vec());
|
|
|
|
let hidden = hidden.clone();
|
2023-06-01 13:52:05 +03:00
|
|
|
let dir_clone = dir.to_path_buf();
|
2022-07-02 17:55:22 +03:00
|
|
|
let zip_paths = tokio::task::spawn_blocking(move || {
|
|
|
|
let mut paths: Vec<PathBuf> = vec![];
|
2023-12-21 12:28:13 +03:00
|
|
|
for dir in access_paths.child_paths(&dir_clone) {
|
2023-06-01 13:52:05 +03:00
|
|
|
let mut it = WalkDir::new(&dir).into_iter();
|
2023-06-15 03:28:21 +03:00
|
|
|
it.next();
|
2023-06-01 13:52:05 +03:00
|
|
|
while let Some(Ok(entry)) = it.next() {
|
2023-07-04 05:10:48 +03:00
|
|
|
if !running.load(atomic::Ordering::SeqCst) {
|
2023-06-01 13:52:05 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
let entry_path = entry.path();
|
|
|
|
let base_name = get_file_name(entry_path);
|
|
|
|
let file_type = entry.file_type();
|
|
|
|
let mut is_dir_type: bool = file_type.is_dir();
|
|
|
|
if file_type.is_symlink() {
|
|
|
|
match std::fs::symlink_metadata(entry_path) {
|
|
|
|
Ok(meta) => {
|
|
|
|
is_dir_type = meta.is_dir();
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
continue;
|
|
|
|
}
|
2023-02-19 17:03:59 +03:00
|
|
|
}
|
2023-06-01 13:52:05 +03:00
|
|
|
}
|
|
|
|
if is_hidden(&hidden, base_name, is_dir_type) {
|
|
|
|
if file_type.is_dir() {
|
|
|
|
it.skip_current_dir();
|
2023-02-19 17:03:59 +03:00
|
|
|
}
|
2023-06-01 13:52:05 +03:00
|
|
|
continue;
|
2023-02-19 17:03:59 +03:00
|
|
|
}
|
2023-06-01 13:52:05 +03:00
|
|
|
if entry.path().symlink_metadata().is_err() {
|
|
|
|
continue;
|
2022-07-02 17:55:22 +03:00
|
|
|
}
|
2023-06-01 13:52:05 +03:00
|
|
|
if !file_type.is_file() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
paths.push(entry_path.to_path_buf());
|
2022-07-02 17:55:22 +03:00
|
|
|
}
|
2022-05-28 13:58:43 +03:00
|
|
|
}
|
2022-07-02 17:55:22 +03:00
|
|
|
paths
|
|
|
|
})
|
|
|
|
.await?;
|
|
|
|
for zip_path in zip_paths.into_iter() {
|
|
|
|
let filename = match zip_path.strip_prefix(dir).ok().and_then(|v| v.to_str()) {
|
|
|
|
Some(v) => v,
|
|
|
|
None => continue,
|
|
|
|
};
|
2023-03-31 18:48:23 +03:00
|
|
|
let (datetime, mode) = get_file_mtime_and_mode(&zip_path).await?;
|
2023-12-14 13:59:28 +03:00
|
|
|
let builder = ZipEntryBuilder::new(filename.into(), compression)
|
2023-03-31 18:48:23 +03:00
|
|
|
.unix_permissions(mode)
|
|
|
|
.last_modification_date(ZipDateTime::from_chrono(&datetime));
|
2022-07-02 17:55:22 +03:00
|
|
|
let mut file = File::open(&zip_path).await?;
|
2023-06-01 17:05:44 +03:00
|
|
|
let mut file_writer = writer.write_entry_stream(builder).await?.compat_write();
|
2022-07-02 17:55:22 +03:00
|
|
|
io::copy(&mut file, &mut file_writer).await?;
|
2023-06-01 17:05:44 +03:00
|
|
|
file_writer.into_inner().close().await?;
|
2022-05-28 13:58:43 +03:00
|
|
|
}
|
|
|
|
writer.close().await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-05-31 05:58:32 +03:00
|
|
|
|
|
|
|
fn extract_cache_headers(meta: &Metadata) -> Option<(ETag, LastModified)> {
|
|
|
|
let mtime = meta.modified().ok()?;
|
|
|
|
let timestamp = to_timestamp(&mtime);
|
|
|
|
let size = meta.len();
|
2023-02-21 12:23:24 +03:00
|
|
|
let etag = format!(r#""{timestamp}-{size}""#).parse::<ETag>().ok()?;
|
2022-05-31 05:58:32 +03:00
|
|
|
let last_modified = LastModified::from(mtime);
|
|
|
|
Some((etag, last_modified))
|
|
|
|
}
|
|
|
|
|
2022-06-15 14:57:28 +03:00
|
|
|
fn status_forbid(res: &mut Response) {
|
|
|
|
*res.status_mut() = StatusCode::FORBIDDEN;
|
2023-12-21 09:24:20 +03:00
|
|
|
*res.body_mut() = body_full("Forbidden");
|
2022-06-15 14:57:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn status_not_found(res: &mut Response) {
|
|
|
|
*res.status_mut() = StatusCode::NOT_FOUND;
|
2023-12-21 09:24:20 +03:00
|
|
|
*res.body_mut() = body_full("Not Found");
|
2022-06-15 14:57:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn status_no_content(res: &mut Response) {
|
|
|
|
*res.status_mut() = StatusCode::NO_CONTENT;
|
|
|
|
}
|
|
|
|
|
2023-03-31 17:52:07 +03:00
|
|
|
fn set_content_diposition(res: &mut Response, inline: bool, filename: &str) -> Result<()> {
|
|
|
|
let kind = if inline { "inline" } else { "attachment" };
|
2023-12-23 10:40:41 +03:00
|
|
|
let filename: String = filename
|
|
|
|
.chars()
|
|
|
|
.map(|ch| {
|
|
|
|
if ch.is_ascii_control() && ch != '\t' {
|
|
|
|
' '
|
|
|
|
} else {
|
|
|
|
ch
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
2023-03-31 17:52:07 +03:00
|
|
|
let value = if filename.is_ascii() {
|
|
|
|
HeaderValue::from_str(&format!("{kind}; filename=\"{}\"", filename,))?
|
|
|
|
} else {
|
|
|
|
HeaderValue::from_str(&format!(
|
|
|
|
"{kind}; filename=\"{}\"; filename*=UTF-8''{}",
|
|
|
|
filename,
|
2023-12-23 10:40:41 +03:00
|
|
|
encode_uri(&filename),
|
2023-03-31 17:52:07 +03:00
|
|
|
))?
|
|
|
|
};
|
|
|
|
res.headers_mut().insert(CONTENT_DISPOSITION, value);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-02-19 17:03:59 +03:00
|
|
|
fn is_hidden(hidden: &[String], file_name: &str, is_dir_type: bool) -> bool {
|
|
|
|
hidden.iter().any(|v| {
|
|
|
|
if is_dir_type {
|
|
|
|
if let Some(x) = v.strip_suffix('/') {
|
|
|
|
return glob(x, file_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
glob(v, file_name)
|
|
|
|
})
|
2022-06-19 09:23:10 +03:00
|
|
|
}
|
|
|
|
|
2022-06-15 14:57:28 +03:00
|
|
|
fn set_webdav_headers(res: &mut Response) {
|
|
|
|
res.headers_mut().insert(
|
|
|
|
"Allow",
|
|
|
|
HeaderValue::from_static("GET,HEAD,PUT,OPTIONS,DELETE,PROPFIND,COPY,MOVE"),
|
|
|
|
);
|
|
|
|
res.headers_mut()
|
|
|
|
.insert("DAV", HeaderValue::from_static("1,2"));
|
|
|
|
}
|
2023-03-01 04:36:59 +03:00
|
|
|
|
|
|
|
async fn get_content_type(path: &Path) -> Result<String> {
|
|
|
|
let mut buffer: Vec<u8> = vec![];
|
|
|
|
fs::File::open(path)
|
|
|
|
.await?
|
|
|
|
.take(1024)
|
|
|
|
.read_to_end(&mut buffer)
|
|
|
|
.await?;
|
|
|
|
let mime = mime_guess::from_path(path).first();
|
|
|
|
let is_text = content_inspector::inspect(&buffer).is_text();
|
|
|
|
let content_type = if is_text {
|
|
|
|
let mut detector = chardetng::EncodingDetector::new();
|
|
|
|
detector.feed(&buffer, buffer.len() < 1024);
|
|
|
|
let (enc, confident) = detector.guess_assess(None, true);
|
|
|
|
let charset = if confident {
|
|
|
|
format!("; charset={}", enc.name())
|
|
|
|
} else {
|
|
|
|
"".into()
|
|
|
|
};
|
|
|
|
match mime {
|
|
|
|
Some(m) => format!("{m}{charset}"),
|
|
|
|
None => format!("text/plain{charset}"),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match mime {
|
|
|
|
Some(m) => m.to_string(),
|
|
|
|
None => "application/octet-stream".into(),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(content_type)
|
|
|
|
}
|