mirror of
https://github.com/lennart-k/rustical.git
synced 2026-01-30 22:28:22 +00:00
WIP: Complete work of propfind parsing
This commit is contained in:
@@ -5,9 +5,9 @@ use crate::resource::PrincipalUri;
|
||||
use crate::resource::Resource;
|
||||
use crate::resource::ResourceService;
|
||||
use crate::xml::MultistatusElement;
|
||||
use crate::xml::PropElement;
|
||||
use crate::xml::PropfindElement;
|
||||
use crate::xml::PropfindType;
|
||||
use rustical_xml::PropName;
|
||||
use rustical_xml::XmlDocument;
|
||||
use tracing::instrument;
|
||||
|
||||
@@ -58,37 +58,36 @@ pub(crate) async fn route_propfind<R: ResourceService>(
|
||||
}
|
||||
|
||||
// A request body is optional. If empty we MUST return all props
|
||||
let propfind: PropfindElement = if !body.is_empty() {
|
||||
PropfindElement::parse_str(&body).map_err(Error::XmlError)?
|
||||
} else {
|
||||
PropfindElement {
|
||||
prop: PropfindType::Allprop,
|
||||
}
|
||||
};
|
||||
|
||||
// TODO: respect namespaces?
|
||||
let props = match &propfind.prop {
|
||||
PropfindType::Allprop => vec!["allprop"],
|
||||
PropfindType::Propname => vec!["propname"],
|
||||
PropfindType::Prop(PropElement(prop_tags)) => prop_tags
|
||||
.iter()
|
||||
.map(|propname| propname.name.as_str())
|
||||
.collect(),
|
||||
};
|
||||
let propfind_self: PropfindElement<<<R::Resource as Resource>::Prop as PropName>::Names> =
|
||||
if !body.is_empty() {
|
||||
PropfindElement::parse_str(&body).map_err(Error::XmlError)?
|
||||
} else {
|
||||
PropfindElement {
|
||||
prop: PropfindType::Allprop,
|
||||
}
|
||||
};
|
||||
let propfind_member: PropfindElement<<<R::MemberType as Resource>::Prop as PropName>::Names> =
|
||||
if !body.is_empty() {
|
||||
PropfindElement::parse_str(&body).map_err(Error::XmlError)?
|
||||
} else {
|
||||
PropfindElement {
|
||||
prop: PropfindType::Allprop,
|
||||
}
|
||||
};
|
||||
|
||||
let mut member_responses = Vec::new();
|
||||
if depth != Depth::Zero {
|
||||
for (subpath, member) in resource_service.get_members(path_components).await? {
|
||||
member_responses.push(member.propfind(
|
||||
member_responses.push(member.propfind_typed(
|
||||
&format!("{}/{}", path.trim_end_matches('/'), subpath),
|
||||
&props,
|
||||
&propfind_member.prop,
|
||||
puri,
|
||||
&user,
|
||||
)?);
|
||||
}
|
||||
}
|
||||
|
||||
let response = resource.propfind(path, &props, puri, &user)?;
|
||||
let response = resource.propfind_typed(path, &propfind_self.prop, puri, &user)?;
|
||||
|
||||
Ok(MultistatusElement {
|
||||
responses: vec![response],
|
||||
|
||||
@@ -7,7 +7,8 @@ use crate::xml::TagList;
|
||||
use crate::xml::multistatus::{PropstatElement, PropstatWrapper, ResponseElement};
|
||||
use http::StatusCode;
|
||||
use quick_xml::name::Namespace;
|
||||
use rustical_xml::EnumUnitVariants;
|
||||
use rustical_xml::NamespaceOwned;
|
||||
use rustical_xml::PropName;
|
||||
use rustical_xml::Unparsed;
|
||||
use rustical_xml::XmlDeserialize;
|
||||
use rustical_xml::XmlDocument;
|
||||
@@ -111,13 +112,15 @@ pub(crate) async fn route_proppatch<R: ResourceService>(
|
||||
}) => {
|
||||
match property {
|
||||
SetPropertyPropWrapper::Valid(prop) => {
|
||||
let propname: <<R::Resource as Resource>::Prop as EnumUnitVariants>::UnitVariants = prop.clone().into();
|
||||
let propname: <<R::Resource as Resource>::Prop as PropName>::Names =
|
||||
prop.clone().into();
|
||||
let (ns, propname): (Option<Namespace>, &str) = propname.into();
|
||||
match resource.set_prop(prop) {
|
||||
Ok(()) => props_ok.push((ns, propname.to_owned())),
|
||||
Err(Error::PropReadOnly) => {
|
||||
props_conflict.push((ns, propname.to_owned()))
|
||||
Ok(()) => {
|
||||
props_ok.push((ns.map(NamespaceOwned::from), propname.to_owned()))
|
||||
}
|
||||
Err(Error::PropReadOnly) => props_conflict
|
||||
.push((ns.map(NamespaceOwned::from), propname.to_owned())),
|
||||
Err(err) => return Err(err.into()),
|
||||
};
|
||||
}
|
||||
@@ -128,7 +131,7 @@ pub(crate) async fn route_proppatch<R: ResourceService>(
|
||||
.into_iter()
|
||||
.find_map(|(ns, tag)| {
|
||||
if tag == propname.as_str() {
|
||||
Some((ns, tag.to_owned()))
|
||||
Some((ns.map(NamespaceOwned::from), tag.to_owned()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@@ -146,14 +149,12 @@ pub(crate) async fn route_proppatch<R: ResourceService>(
|
||||
}
|
||||
Operation::Remove(remove_el) => {
|
||||
let propname = remove_el.prop.0.0;
|
||||
match <<R::Resource as Resource>::Prop as EnumUnitVariants>::UnitVariants::from_str(
|
||||
&propname,
|
||||
) {
|
||||
match <<R::Resource as Resource>::Prop as PropName>::Names::from_str(&propname) {
|
||||
Ok(prop) => match resource.remove_prop(&prop) {
|
||||
Ok(()) => props_ok.push((None, propname)),
|
||||
Err(Error::PropReadOnly) => props_conflict.push({
|
||||
let (ns, tag) = prop.into();
|
||||
(ns, tag.to_owned())
|
||||
(ns.map(NamespaceOwned::from), tag.to_owned())
|
||||
}),
|
||||
Err(err) => return Err(err.into()),
|
||||
},
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
use crate::Principal;
|
||||
use crate::privileges::UserPrivilegeSet;
|
||||
use crate::xml::Resourcetype;
|
||||
use crate::xml::multistatus::{PropTagWrapper, PropstatElement, PropstatWrapper};
|
||||
use crate::xml::{PropElement, PropfindType, Resourcetype};
|
||||
use crate::xml::{TagList, multistatus::ResponseElement};
|
||||
use crate::{Error, Principal};
|
||||
use headers::{ETag, IfMatch, IfNoneMatch};
|
||||
use http::StatusCode;
|
||||
use itertools::Itertools;
|
||||
use quick_xml::name::Namespace;
|
||||
pub use resource_service::ResourceService;
|
||||
use rustical_xml::{EnumUnitVariants, EnumVariants, XmlDeserialize, XmlSerialize};
|
||||
use rustical_xml::{EnumVariants, NamespaceOwned, PropName, XmlDeserialize, XmlSerialize};
|
||||
use std::collections::HashSet;
|
||||
use std::str::FromStr;
|
||||
|
||||
@@ -26,7 +26,7 @@ pub trait ResourcePropName: FromStr {}
|
||||
impl<T: FromStr> ResourcePropName for T {}
|
||||
|
||||
pub trait Resource: Clone + 'static {
|
||||
type Prop: ResourceProp + PartialEq + Clone + EnumVariants + EnumUnitVariants;
|
||||
type Prop: ResourceProp + PartialEq + Clone + EnumVariants + PropName;
|
||||
type Error: From<crate::Error>;
|
||||
type Principal: Principal;
|
||||
|
||||
@@ -40,17 +40,14 @@ pub trait Resource: Clone + 'static {
|
||||
&self,
|
||||
principal_uri: &impl PrincipalUri,
|
||||
principal: &Self::Principal,
|
||||
prop: &<Self::Prop as EnumUnitVariants>::UnitVariants,
|
||||
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 EnumUnitVariants>::UnitVariants,
|
||||
) -> Result<(), crate::Error> {
|
||||
fn remove_prop(&mut self, _prop: &<Self::Prop as PropName>::Names) -> Result<(), crate::Error> {
|
||||
Err(crate::Error::PropReadOnly)
|
||||
}
|
||||
|
||||
@@ -91,62 +88,45 @@ pub trait Resource: Clone + 'static {
|
||||
principal: &Self::Principal,
|
||||
) -> Result<UserPrivilegeSet, Self::Error>;
|
||||
|
||||
fn propfind(
|
||||
fn propfind_typed(
|
||||
&self,
|
||||
path: &str,
|
||||
props: &[&str],
|
||||
prop: &PropfindType<<Self::Prop as PropName>::Names>,
|
||||
principal_uri: &impl PrincipalUri,
|
||||
principal: &Self::Principal,
|
||||
) -> Result<ResponseElement<Self::Prop>, Self::Error> {
|
||||
let mut props: HashSet<&str> = props.iter().cloned().collect();
|
||||
// TODO: Support include element
|
||||
let (props, invalid_props): (HashSet<<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();
|
||||
|
||||
if props.contains(&"propname") {
|
||||
if props.len() != 1 {
|
||||
// propname MUST be the only queried prop per spec
|
||||
return Err(
|
||||
Error::BadRequest("propname MUST be the only queried prop".to_owned()).into(),
|
||||
);
|
||||
return Ok(ResponseElement {
|
||||
href: path.to_owned(),
|
||||
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().cloned().collect(),
|
||||
invalid_tags.to_owned(),
|
||||
),
|
||||
};
|
||||
|
||||
let props = Self::list_props()
|
||||
.into_iter()
|
||||
.map(|(ns, tag)| (ns.to_owned(), tag.to_string()))
|
||||
.collect_vec();
|
||||
|
||||
return Ok(ResponseElement {
|
||||
href: path.to_owned(),
|
||||
propstat: vec![PropstatWrapper::TagList(PropstatElement {
|
||||
prop: TagList::from(props),
|
||||
status: StatusCode::OK,
|
||||
})],
|
||||
..Default::default()
|
||||
});
|
||||
}
|
||||
|
||||
if props.contains(&"allprop") {
|
||||
if props.len() != 1 {
|
||||
// allprop MUST be the only queried prop per spec
|
||||
return Err(
|
||||
Error::BadRequest("allprop MUST be the only queried prop".to_owned()).into(),
|
||||
);
|
||||
}
|
||||
props = Self::list_props()
|
||||
.into_iter()
|
||||
.map(|(_ns, tag)| tag)
|
||||
.collect();
|
||||
}
|
||||
|
||||
let mut valid_props = vec![];
|
||||
let mut invalid_props = vec![];
|
||||
for prop in props {
|
||||
if let Ok(valid_prop) = <Self::Prop as EnumUnitVariants>::UnitVariants::from_str(prop) {
|
||||
valid_props.push(valid_prop);
|
||||
} else {
|
||||
invalid_props.push(prop.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
let prop_responses = valid_props
|
||||
let prop_responses = props
|
||||
.into_iter()
|
||||
.map(|prop| self.get_prop(principal_uri, principal, &prop))
|
||||
.collect::<Result<Vec<_>, Self::Error>>()?;
|
||||
@@ -158,11 +138,7 @@ pub trait Resource: Clone + 'static {
|
||||
if !invalid_props.is_empty() {
|
||||
propstats.push(PropstatWrapper::TagList(PropstatElement {
|
||||
status: StatusCode::NOT_FOUND,
|
||||
prop: invalid_props
|
||||
.into_iter()
|
||||
.map(|tag| (None, tag))
|
||||
.collect_vec()
|
||||
.into(),
|
||||
prop: invalid_props.into(),
|
||||
}));
|
||||
}
|
||||
Ok(ResponseElement {
|
||||
|
||||
Reference in New Issue
Block a user