mod delete;
mod head;
mod options;
mod patch;
mod post;
use std::sync::Arc;
use bytes::Bytes;
use http::{HeaderMap, HeaderValue, Method, StatusCode, Uri};
use tokio::sync::broadcast;
use crate::{
config::Config,
error::TusError,
hooks::{HookEvent, HookSender},
lock::SendLocker,
proto::{HDR_CACHE_CONTROL, HDR_TUS_RESUMABLE, TUS_VERSION},
store::SendDataStore,
util::static_header,
};
pub struct TusRequest {
pub method: Method,
pub uri: Uri,
pub upload_id: Option<String>,
pub headers: HeaderMap,
pub body: Option<Box<dyn tokio::io::AsyncRead + Send + Sync + Unpin>>,
}
pub struct TusResponse {
pub status: StatusCode,
pub headers: HeaderMap,
pub body: Bytes,
}
pub struct TusHandler<S, L = NoLocker> {
pub(crate) store: S,
pub(crate) locker: Option<L>,
pub(crate) config: Arc<Config>,
pub(crate) hook_tx: Option<HookSender>,
}
impl<S, L> TusHandler<S, L>
where
S: SendDataStore + Send + Sync + 'static,
L: SendLocker + Send + Sync + 'static,
{
pub fn new(store: S, locker: Option<L>, config: Config) -> Self {
let hook_tx = if config.hooks.channel_capacity > 0 {
let (tx, _) = broadcast::channel(config.hooks.channel_capacity);
Some(tx)
} else {
None
};
Self {
store,
locker,
config: Arc::new(config),
hook_tx,
}
}
pub fn hook_receiver(&self) -> Option<broadcast::Receiver<HookEvent>> {
self.hook_tx.as_ref().map(|tx| tx.subscribe())
}
pub async fn handle(&self, req: TusRequest) -> TusResponse {
let result = match req.method {
Method::OPTIONS => options::handle(self, &req).await,
Method::HEAD => head::handle(self, &req).await,
Method::POST => post::handle(self, req).await,
Method::PATCH => patch::handle(self, req).await,
Method::DELETE => delete::handle(self, &req).await,
_ => Err(TusError::MethodNotAllowed),
};
match result {
Ok(resp) => resp,
Err(err) => self.error_response(err),
}
}
pub(crate) fn response(
&self,
status: StatusCode,
extra_headers: HeaderMap,
body: Bytes,
) -> TusResponse {
let mut headers = self.base_headers();
headers.extend(extra_headers);
TusResponse { status, headers, body }
}
pub(crate) fn error_response(&self, err: TusError) -> TusResponse {
let status = err.status_code();
let body = Bytes::from(err.to_string());
let mut headers = self.base_headers();
headers.insert(
http::header::CONTENT_TYPE,
HeaderValue::from_static("text/plain; charset=utf-8"),
);
TusResponse { status, headers, body }
}
fn base_headers(&self) -> HeaderMap {
let mut h = HeaderMap::new();
h.insert(HDR_TUS_RESUMABLE, static_header(TUS_VERSION));
h.insert(HDR_CACHE_CONTROL, static_header("no-store"));
if self.config.enable_cors {
h.insert(
crate::proto::HDR_ACCESS_CONTROL_ALLOW_ORIGIN,
static_header("*"),
);
h.insert(
crate::proto::HDR_ACCESS_CONTROL_EXPOSE_HEADERS,
static_header(
"Upload-Offset,Upload-Length,Upload-Metadata,Upload-Expires,\
Upload-Defer-Length,Location,Tus-Resumable,Tus-Version,Tus-Extension,\
Tus-Max-Size,Tus-Checksum-Algorithm",
),
);
}
h
}
pub(crate) fn emit(&self, event: HookEvent) {
if let Some(tx) = &self.hook_tx {
let _ = tx.send(event);
}
}
}
pub struct NoLocker;
impl crate::lock::SendLock for NoLocker {
async fn release(self) -> Result<(), TusError> {
Ok(())
}
}
impl crate::lock::SendLocker for NoLocker {
type LockType = NoLocker;
async fn acquire(&self, _id: &crate::info::UploadId) -> Result<NoLocker, TusError> {
Ok(NoLocker)
}
}