mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 13:32:16 +00:00
191 lines
6.1 KiB
Rust
191 lines
6.1 KiB
Rust
use crate::Principal;
|
|
use crate::privileges::UserPrivilegeSet;
|
|
use crate::xml::multistatus::{PropTagWrapper, PropstatElement, PropstatWrapper};
|
|
use crate::xml::{PropElement, PropfindElement, PropfindType, Resourcetype};
|
|
use crate::xml::{TagList, multistatus::ResponseElement};
|
|
use headers::{ETag, IfMatch, IfNoneMatch};
|
|
use http::StatusCode;
|
|
use itertools::Itertools;
|
|
use quick_xml::name::Namespace;
|
|
pub use resource_service::ResourceService;
|
|
use rustical_xml::{
|
|
EnumVariants, NamespaceOwned, PropName, XmlDeserialize, XmlDocument, XmlSerialize,
|
|
};
|
|
use std::str::FromStr;
|
|
|
|
mod axum_methods;
|
|
mod axum_service;
|
|
mod methods;
|
|
mod principal_uri;
|
|
mod resource_service;
|
|
|
|
pub use axum_methods::{AxumMethods, MethodFunction};
|
|
pub use axum_service::AxumService;
|
|
pub use principal_uri::PrincipalUri;
|
|
|
|
pub trait ResourceProp: XmlSerialize + XmlDeserialize {}
|
|
impl<T: XmlSerialize + XmlDeserialize> ResourceProp for T {}
|
|
|
|
pub trait ResourcePropName: FromStr {}
|
|
impl<T: FromStr> ResourcePropName for T {}
|
|
|
|
pub trait ResourceName {
|
|
fn get_name(&self) -> String;
|
|
}
|
|
|
|
pub trait Resource: Clone + Send + 'static {
|
|
type Prop: ResourceProp + PartialEq + Clone + EnumVariants + PropName + Send;
|
|
type Error: From<crate::Error>;
|
|
type Principal: Principal;
|
|
|
|
fn is_collection(&self) -> bool;
|
|
|
|
fn get_resourcetype(&self) -> Resourcetype;
|
|
|
|
#[must_use]
|
|
fn list_props() -> Vec<(Option<Namespace<'static>>, &'static str)> {
|
|
Self::Prop::variant_names()
|
|
}
|
|
|
|
fn get_prop(
|
|
&self,
|
|
principal_uri: &impl PrincipalUri,
|
|
principal: &Self::Principal,
|
|
prop: &<Self::Prop as PropName>::Names,
|
|
) -> Result<Self::Prop, Self::Error>;
|
|
|
|
fn set_prop(&mut self, _prop: Self::Prop) -> Result<(), crate::Error> {
|
|
Err(crate::Error::PropReadOnly)
|
|
}
|
|
|
|
fn remove_prop(&mut self, _prop: &<Self::Prop as PropName>::Names) -> Result<(), crate::Error> {
|
|
Err(crate::Error::PropReadOnly)
|
|
}
|
|
|
|
fn get_displayname(&self) -> Option<&str>;
|
|
fn set_displayname(&mut self, _name: Option<String>) -> Result<(), crate::Error> {
|
|
Err(crate::Error::PropReadOnly)
|
|
}
|
|
|
|
fn get_owner(&self) -> Option<&str> {
|
|
None
|
|
}
|
|
|
|
fn get_etag(&self) -> Option<String> {
|
|
None
|
|
}
|
|
|
|
fn satisfies_if_match(&self, if_match: &IfMatch) -> bool {
|
|
self.get_etag().map_or_else(
|
|
|| if_match.is_any(),
|
|
|etag| {
|
|
ETag::from_str(&etag).map_or_else(
|
|
|_| if_match.is_any(),
|
|
|etag| if_match.precondition_passes(&etag),
|
|
)
|
|
},
|
|
)
|
|
}
|
|
|
|
fn satisfies_if_none_match(&self, if_none_match: &IfNoneMatch) -> bool {
|
|
self.get_etag().map_or_else(
|
|
|| if_none_match != &IfNoneMatch::any(),
|
|
|etag| {
|
|
ETag::from_str(&etag).map_or_else(
|
|
|_| if_none_match != &IfNoneMatch::any(),
|
|
|etag| if_none_match.precondition_passes(&etag),
|
|
)
|
|
},
|
|
)
|
|
}
|
|
|
|
fn get_user_privileges(
|
|
&self,
|
|
principal: &Self::Principal,
|
|
) -> Result<UserPrivilegeSet, Self::Error>;
|
|
|
|
fn parse_propfind(
|
|
body: &str,
|
|
) -> Result<PropfindElement<<Self::Prop as PropName>::Names>, rustical_xml::XmlError> {
|
|
if body.is_empty() {
|
|
Ok(PropfindElement {
|
|
prop: PropfindType::Allprop,
|
|
include: None,
|
|
})
|
|
} else {
|
|
PropfindElement::parse_str(body)
|
|
}
|
|
}
|
|
|
|
fn propfind(
|
|
&self,
|
|
path: &str,
|
|
prop: &PropfindType<<Self::Prop as PropName>::Names>,
|
|
include: Option<&PropElement<<Self::Prop as PropName>::Names>>,
|
|
principal_uri: &impl PrincipalUri,
|
|
principal: &Self::Principal,
|
|
) -> Result<ResponseElement<Self::Prop>, Self::Error> {
|
|
// Collections have a trailing slash
|
|
let mut path = path.to_string();
|
|
if self.is_collection() && !path.ends_with('/') {
|
|
path.push('/');
|
|
}
|
|
|
|
let (mut props, mut invalid_props): (Vec<<Self::Prop as PropName>::Names>, Vec<_>) =
|
|
match prop {
|
|
PropfindType::Propname => {
|
|
let props = Self::list_props()
|
|
.into_iter()
|
|
.map(|(ns, tag)| (ns.map(NamespaceOwned::from), tag.to_string()))
|
|
.collect_vec();
|
|
|
|
return Ok(ResponseElement {
|
|
href: path.clone(),
|
|
propstat: vec![PropstatWrapper::TagList(PropstatElement {
|
|
prop: TagList::from(props),
|
|
status: StatusCode::OK,
|
|
})],
|
|
..Default::default()
|
|
});
|
|
}
|
|
PropfindType::Allprop => (
|
|
Self::list_props()
|
|
.iter()
|
|
.map(|(_ns, name)| <Self::Prop as PropName>::Names::from_str(name).unwrap())
|
|
.collect(),
|
|
vec![],
|
|
),
|
|
PropfindType::Prop(PropElement(valid_tags, invalid_tags)) => (
|
|
valid_tags.iter().unique().cloned().collect(),
|
|
invalid_tags.to_owned(),
|
|
),
|
|
};
|
|
|
|
if let Some(PropElement(valid_tags, invalid_tags)) = include {
|
|
props.extend(valid_tags.clone());
|
|
invalid_props.extend(invalid_tags.to_owned());
|
|
}
|
|
|
|
let prop_responses = props
|
|
.into_iter()
|
|
.map(|prop| self.get_prop(principal_uri, principal, &prop))
|
|
.collect::<Result<Vec<_>, Self::Error>>()?;
|
|
|
|
let mut propstats = vec![PropstatWrapper::Normal(PropstatElement {
|
|
status: StatusCode::OK,
|
|
prop: PropTagWrapper(prop_responses),
|
|
})];
|
|
if !invalid_props.is_empty() {
|
|
propstats.push(PropstatWrapper::TagList(PropstatElement {
|
|
status: StatusCode::NOT_FOUND,
|
|
prop: invalid_props.into(),
|
|
}));
|
|
}
|
|
Ok(ResponseElement {
|
|
href: path.clone(),
|
|
propstat: propstats,
|
|
..Default::default()
|
|
})
|
|
}
|
|
}
|