mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 22:52:22 +00:00
Refactoring that will hopefully make life easier
This commit is contained in:
@@ -0,0 +1,75 @@
|
|||||||
|
use crate::{
|
||||||
|
event::resource::{EventFile, EventProp},
|
||||||
|
Error,
|
||||||
|
};
|
||||||
|
use actix_web::HttpRequest;
|
||||||
|
use rustical_dav::{
|
||||||
|
methods::propfind::{PropElement, PropfindType},
|
||||||
|
namespace::Namespace,
|
||||||
|
resource::HandlePropfind,
|
||||||
|
xml::{multistatus::PropstatWrapper, MultistatusElement},
|
||||||
|
};
|
||||||
|
use rustical_store::{event::Event, CalendarStore};
|
||||||
|
use serde::Deserialize;
|
||||||
|
use tokio::sync::RwLock;
|
||||||
|
|
||||||
|
#[derive(Deserialize, Clone, Debug)]
|
||||||
|
#[serde(rename_all = "kebab-case")]
|
||||||
|
#[allow(dead_code)]
|
||||||
|
// <!ELEMENT calendar-query ((DAV:allprop | DAV:propname | DAV:prop)?, href+)>
|
||||||
|
pub struct CalendarMultigetRequest {
|
||||||
|
#[serde(flatten)]
|
||||||
|
prop: PropfindType,
|
||||||
|
href: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_events_calendar_multiget<C: CalendarStore + ?Sized>(
|
||||||
|
_cal_query: &CalendarMultigetRequest,
|
||||||
|
principal: &str,
|
||||||
|
cid: &str,
|
||||||
|
store: &RwLock<C>,
|
||||||
|
) -> Result<Vec<Event>, Error> {
|
||||||
|
// TODO: proper implementation
|
||||||
|
Ok(store.read().await.get_events(principal, cid).await?)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn handle_calendar_multiget<C: CalendarStore + ?Sized>(
|
||||||
|
cal_multiget: CalendarMultigetRequest,
|
||||||
|
req: HttpRequest,
|
||||||
|
prefix: &str,
|
||||||
|
principal: &str,
|
||||||
|
cid: &str,
|
||||||
|
cal_store: &RwLock<C>,
|
||||||
|
) -> Result<MultistatusElement<PropstatWrapper<EventProp>, String>, Error> {
|
||||||
|
let events = get_events_calendar_multiget(&cal_multiget, principal, cid, cal_store).await?;
|
||||||
|
|
||||||
|
let props = match cal_multiget.prop {
|
||||||
|
PropfindType::Allprop => {
|
||||||
|
vec!["allprop".to_owned()]
|
||||||
|
}
|
||||||
|
PropfindType::Propname => {
|
||||||
|
// TODO: Implement
|
||||||
|
return Err(Error::NotImplemented);
|
||||||
|
}
|
||||||
|
PropfindType::Prop(PropElement { prop: prop_tags }) => prop_tags.into(),
|
||||||
|
};
|
||||||
|
let props: Vec<&str> = props.iter().map(String::as_str).collect();
|
||||||
|
|
||||||
|
let mut responses = Vec::new();
|
||||||
|
for event in events {
|
||||||
|
let path = format!("{}/{}", req.path(), event.get_uid());
|
||||||
|
responses.push(
|
||||||
|
EventFile { event }
|
||||||
|
.propfind(prefix, path, props.clone())
|
||||||
|
.await?,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(MultistatusElement {
|
||||||
|
responses,
|
||||||
|
member_responses: vec![],
|
||||||
|
ns_dav: Namespace::Dav.as_str(),
|
||||||
|
ns_caldav: Namespace::CalDAV.as_str(),
|
||||||
|
ns_ical: Namespace::ICal.as_str(),
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -1,40 +1,21 @@
|
|||||||
use crate::{event::resource::EventFile, Error};
|
use actix_web::HttpRequest;
|
||||||
use actix_web::{
|
|
||||||
web::{Data, Path},
|
|
||||||
HttpRequest, Responder,
|
|
||||||
};
|
|
||||||
use rustical_auth::{AuthInfoExtractor, CheckAuthentication};
|
|
||||||
use rustical_dav::{
|
use rustical_dav::{
|
||||||
methods::propfind::{PropElement, PropfindType, ServicePrefix},
|
methods::propfind::{PropElement, PropfindType},
|
||||||
namespace::Namespace,
|
namespace::Namespace,
|
||||||
resource::HandlePropfind,
|
resource::HandlePropfind,
|
||||||
xml::MultistatusElement,
|
xml::{multistatus::PropstatWrapper, MultistatusElement},
|
||||||
};
|
};
|
||||||
use rustical_store::event::Event;
|
use rustical_store::{event::Event, CalendarStore};
|
||||||
use rustical_store::CalendarStore;
|
use serde::Deserialize;
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, Clone, Debug)]
|
use crate::{
|
||||||
#[serde(rename_all = "kebab-case")]
|
event::resource::{EventFile, EventProp},
|
||||||
pub enum PropQuery {
|
Error,
|
||||||
Allprop,
|
};
|
||||||
Prop,
|
|
||||||
Propname,
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Implement all the other filters
|
// TODO: Implement all the other filters
|
||||||
|
|
||||||
#[derive(Deserialize, Clone, Debug)]
|
|
||||||
#[serde(rename_all = "kebab-case")]
|
|
||||||
#[allow(dead_code)]
|
|
||||||
// <!ELEMENT calendar-query ((DAV:allprop | DAV:propname | DAV:prop)?, href+)>
|
|
||||||
pub struct CalendarMultigetRequest {
|
|
||||||
#[serde(flatten)]
|
|
||||||
prop: PropfindType,
|
|
||||||
href: Vec<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize, Clone, Debug)]
|
#[derive(Deserialize, Clone, Debug)]
|
||||||
#[serde(rename_all = "kebab-case")]
|
#[serde(rename_all = "kebab-case")]
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
@@ -102,24 +83,16 @@ struct FilterElement {
|
|||||||
#[derive(Deserialize, Clone, Debug)]
|
#[derive(Deserialize, Clone, Debug)]
|
||||||
#[serde(rename_all = "kebab-case")]
|
#[serde(rename_all = "kebab-case")]
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
// #[serde(rename = "calendar-query")]
|
|
||||||
// <!ELEMENT calendar-query ((DAV:allprop | DAV:propname | DAV:prop)?, filter, timezone?)>
|
// <!ELEMENT calendar-query ((DAV:allprop | DAV:propname | DAV:prop)?, filter, timezone?)>
|
||||||
pub struct CalendarQueryRequest {
|
pub struct CalendarQueryRequest {
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
prop: PropfindType,
|
pub prop: PropfindType,
|
||||||
filter: Option<FilterElement>,
|
filter: Option<FilterElement>,
|
||||||
timezone: Option<String>,
|
timezone: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Clone, Debug)]
|
pub async fn get_events_calendar_query<C: CalendarStore + ?Sized>(
|
||||||
#[serde(rename_all = "kebab-case")]
|
_cal_query: &CalendarQueryRequest,
|
||||||
pub enum ReportRequest {
|
|
||||||
CalendarMultiget(CalendarMultigetRequest),
|
|
||||||
CalendarQuery(CalendarQueryRequest),
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn get_events_calendar_query<C: CalendarStore + ?Sized>(
|
|
||||||
_cal_query: CalendarQueryRequest,
|
|
||||||
principal: &str,
|
principal: &str,
|
||||||
cid: &str,
|
cid: &str,
|
||||||
store: &RwLock<C>,
|
store: &RwLock<C>,
|
||||||
@@ -128,45 +101,17 @@ async fn get_events_calendar_query<C: CalendarStore + ?Sized>(
|
|||||||
Ok(store.read().await.get_events(principal, cid).await?)
|
Ok(store.read().await.get_events(principal, cid).await?)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_events_calendar_multiget<C: CalendarStore + ?Sized>(
|
pub async fn handle_calendar_query<C: CalendarStore + ?Sized>(
|
||||||
_cal_query: CalendarMultigetRequest,
|
cal_query: CalendarQueryRequest,
|
||||||
|
req: HttpRequest,
|
||||||
|
prefix: &str,
|
||||||
principal: &str,
|
principal: &str,
|
||||||
cid: &str,
|
cid: &str,
|
||||||
store: &RwLock<C>,
|
cal_store: &RwLock<C>,
|
||||||
) -> Result<Vec<Event>, Error> {
|
) -> Result<MultistatusElement<PropstatWrapper<EventProp>, String>, Error> {
|
||||||
// TODO: proper implementation
|
let events = get_events_calendar_query(&cal_query, principal, cid, cal_store).await?;
|
||||||
Ok(store.read().await.get_events(principal, cid).await?)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn route_report_calendar<A: CheckAuthentication, C: CalendarStore + ?Sized>(
|
let props = match cal_query.prop {
|
||||||
path: Path<(String, String)>,
|
|
||||||
body: String,
|
|
||||||
auth: AuthInfoExtractor<A>,
|
|
||||||
req: HttpRequest,
|
|
||||||
cal_store: Data<RwLock<C>>,
|
|
||||||
prefix: Data<ServicePrefix>,
|
|
||||||
) -> Result<impl Responder, Error> {
|
|
||||||
let (principal, cid) = path.into_inner();
|
|
||||||
if principal != auth.inner.user_id {
|
|
||||||
return Err(Error::Unauthorized);
|
|
||||||
}
|
|
||||||
|
|
||||||
let request: ReportRequest = quick_xml::de::from_str(&body)?;
|
|
||||||
let events = match request.clone() {
|
|
||||||
ReportRequest::CalendarQuery(cal_query) => {
|
|
||||||
get_events_calendar_query(cal_query, &principal, &cid, &cal_store).await?
|
|
||||||
}
|
|
||||||
ReportRequest::CalendarMultiget(cal_multiget) => {
|
|
||||||
get_events_calendar_multiget(cal_multiget, &principal, &cid, &cal_store).await?
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// TODO: Change this
|
|
||||||
let proptag = match request {
|
|
||||||
ReportRequest::CalendarQuery(CalendarQueryRequest { prop, .. }) => prop.clone(),
|
|
||||||
ReportRequest::CalendarMultiget(CalendarMultigetRequest { prop, .. }) => prop.clone(),
|
|
||||||
};
|
|
||||||
let props = match proptag {
|
|
||||||
PropfindType::Allprop => {
|
PropfindType::Allprop => {
|
||||||
vec!["allprop".to_owned()]
|
vec!["allprop".to_owned()]
|
||||||
}
|
}
|
||||||
@@ -183,14 +128,14 @@ pub async fn route_report_calendar<A: CheckAuthentication, C: CalendarStore + ?S
|
|||||||
let path = format!("{}/{}", req.path(), event.get_uid());
|
let path = format!("{}/{}", req.path(), event.get_uid());
|
||||||
responses.push(
|
responses.push(
|
||||||
EventFile { event }
|
EventFile { event }
|
||||||
.propfind(&prefix.0, path, props.clone())
|
.propfind(prefix, path, props.clone())
|
||||||
.await?,
|
.await?,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(MultistatusElement {
|
Ok(MultistatusElement {
|
||||||
responses,
|
responses,
|
||||||
member_responses: Vec::<String>::new(),
|
member_responses: vec![],
|
||||||
ns_dav: Namespace::Dav.as_str(),
|
ns_dav: Namespace::Dav.as_str(),
|
||||||
ns_caldav: Namespace::CalDAV.as_str(),
|
ns_caldav: Namespace::CalDAV.as_str(),
|
||||||
ns_ical: Namespace::ICal.as_str(),
|
ns_ical: Namespace::ICal.as_str(),
|
||||||
72
crates/caldav/src/calendar/methods/report/mod.rs
Normal file
72
crates/caldav/src/calendar/methods/report/mod.rs
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
use crate::Error;
|
||||||
|
use actix_web::{
|
||||||
|
web::{Data, Path},
|
||||||
|
HttpRequest, Responder,
|
||||||
|
};
|
||||||
|
use calendar_multiget::{handle_calendar_multiget, CalendarMultigetRequest};
|
||||||
|
use calendar_query::{handle_calendar_query, CalendarQueryRequest};
|
||||||
|
use rustical_auth::{AuthInfoExtractor, CheckAuthentication};
|
||||||
|
use rustical_dav::methods::propfind::ServicePrefix;
|
||||||
|
use rustical_store::CalendarStore;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use sync_collection::{handle_sync_collection, SyncCollectionRequest};
|
||||||
|
use tokio::sync::RwLock;
|
||||||
|
|
||||||
|
mod calendar_multiget;
|
||||||
|
mod calendar_query;
|
||||||
|
mod sync_collection;
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize, Clone, Debug)]
|
||||||
|
#[serde(rename_all = "kebab-case")]
|
||||||
|
pub enum PropQuery {
|
||||||
|
Allprop,
|
||||||
|
Prop,
|
||||||
|
Propname,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Clone, Debug)]
|
||||||
|
#[serde(rename_all = "kebab-case")]
|
||||||
|
pub enum ReportRequest {
|
||||||
|
CalendarMultiget(CalendarMultigetRequest),
|
||||||
|
CalendarQuery(CalendarQueryRequest),
|
||||||
|
SyncCollection(SyncCollectionRequest),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn route_report_calendar<A: CheckAuthentication, C: CalendarStore + ?Sized>(
|
||||||
|
path: Path<(String, String)>,
|
||||||
|
body: String,
|
||||||
|
auth: AuthInfoExtractor<A>,
|
||||||
|
req: HttpRequest,
|
||||||
|
cal_store: Data<RwLock<C>>,
|
||||||
|
prefix: Data<ServicePrefix>,
|
||||||
|
) -> Result<impl Responder, Error> {
|
||||||
|
let (principal, cid) = path.into_inner();
|
||||||
|
if principal != auth.inner.user_id {
|
||||||
|
return Err(Error::Unauthorized);
|
||||||
|
}
|
||||||
|
|
||||||
|
dbg!("REPORT request:", &body);
|
||||||
|
dbg!(req.headers().get("If"));
|
||||||
|
let request: ReportRequest = quick_xml::de::from_str(&body)?;
|
||||||
|
|
||||||
|
Ok(match request.clone() {
|
||||||
|
ReportRequest::CalendarQuery(cal_query) => {
|
||||||
|
handle_calendar_query(cal_query, req, &prefix.0, &principal, &cid, &cal_store).await?
|
||||||
|
}
|
||||||
|
ReportRequest::CalendarMultiget(cal_multiget) => {
|
||||||
|
handle_calendar_multiget(cal_multiget, req, &prefix.0, &principal, &cid, &cal_store)
|
||||||
|
.await?
|
||||||
|
}
|
||||||
|
ReportRequest::SyncCollection(sync_collection) => {
|
||||||
|
handle_sync_collection(
|
||||||
|
sync_collection,
|
||||||
|
req,
|
||||||
|
&prefix.0,
|
||||||
|
&principal,
|
||||||
|
&cid,
|
||||||
|
&cal_store,
|
||||||
|
)
|
||||||
|
.await?
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
89
crates/caldav/src/calendar/methods/report/sync_collection.rs
Normal file
89
crates/caldav/src/calendar/methods/report/sync_collection.rs
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
use actix_web::HttpRequest;
|
||||||
|
use rustical_dav::{
|
||||||
|
methods::propfind::{PropElement, PropfindType},
|
||||||
|
namespace::Namespace,
|
||||||
|
resource::HandlePropfind,
|
||||||
|
xml::{multistatus::PropstatWrapper, MultistatusElement},
|
||||||
|
};
|
||||||
|
use rustical_store::{event::Event, CalendarStore};
|
||||||
|
use serde::Deserialize;
|
||||||
|
use tokio::sync::RwLock;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
event::resource::{EventFile, EventProp},
|
||||||
|
Error,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Deserialize, Clone, Debug)]
|
||||||
|
#[serde(rename_all = "kebab-case")]
|
||||||
|
enum SyncLevel {
|
||||||
|
#[serde(rename = "1")]
|
||||||
|
One,
|
||||||
|
Infinity,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Clone, Debug)]
|
||||||
|
#[serde(rename_all = "kebab-case")]
|
||||||
|
#[allow(dead_code)]
|
||||||
|
// <!ELEMENT sync-collection (sync-token, sync-level, limit?, prop)>
|
||||||
|
// <!-- DAV:limit defined in RFC 5323, Section 5.17 -->
|
||||||
|
// <!-- DAV:prop defined in RFC 4918, Section 14.18 -->
|
||||||
|
pub struct SyncCollectionRequest {
|
||||||
|
sync_token: String,
|
||||||
|
sync_level: SyncLevel,
|
||||||
|
timezone: Option<String>,
|
||||||
|
#[serde(flatten)]
|
||||||
|
pub prop: PropfindType,
|
||||||
|
limit: Option<u64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_events_sync_collection<C: CalendarStore + ?Sized>(
|
||||||
|
_sync_collection: &SyncCollectionRequest,
|
||||||
|
principal: &str,
|
||||||
|
cid: &str,
|
||||||
|
store: &RwLock<C>,
|
||||||
|
) -> Result<Vec<Event>, Error> {
|
||||||
|
// TODO: proper implementation
|
||||||
|
Ok(store.read().await.get_events(principal, cid).await?)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn handle_sync_collection<C: CalendarStore + ?Sized>(
|
||||||
|
sync_collection: SyncCollectionRequest,
|
||||||
|
req: HttpRequest,
|
||||||
|
prefix: &str,
|
||||||
|
principal: &str,
|
||||||
|
cid: &str,
|
||||||
|
cal_store: &RwLock<C>,
|
||||||
|
) -> Result<MultistatusElement<PropstatWrapper<EventProp>, String>, Error> {
|
||||||
|
let events = get_events_sync_collection(&sync_collection, principal, cid, cal_store).await?;
|
||||||
|
|
||||||
|
let props = match sync_collection.prop {
|
||||||
|
PropfindType::Allprop => {
|
||||||
|
vec!["allprop".to_owned()]
|
||||||
|
}
|
||||||
|
PropfindType::Propname => {
|
||||||
|
// TODO: Implement
|
||||||
|
return Err(Error::NotImplemented);
|
||||||
|
}
|
||||||
|
PropfindType::Prop(PropElement { prop: prop_tags }) => prop_tags.into(),
|
||||||
|
};
|
||||||
|
let props: Vec<&str> = props.iter().map(String::as_str).collect();
|
||||||
|
|
||||||
|
let mut responses = Vec::new();
|
||||||
|
for event in events {
|
||||||
|
let path = format!("{}/{}", req.path(), event.get_uid());
|
||||||
|
responses.push(
|
||||||
|
EventFile { event }
|
||||||
|
.propfind(prefix, path, props.clone())
|
||||||
|
.await?,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(MultistatusElement {
|
||||||
|
responses,
|
||||||
|
member_responses: vec![],
|
||||||
|
ns_dav: Namespace::Dav.as_str(),
|
||||||
|
ns_caldav: Namespace::CalDAV.as_str(),
|
||||||
|
ns_ical: Namespace::ICal.as_str(),
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -101,25 +101,34 @@ impl Default for UserPrivilegeSet {
|
|||||||
pub enum ReportMethod {
|
pub enum ReportMethod {
|
||||||
CalendarQuery,
|
CalendarQuery,
|
||||||
CalendarMultiget,
|
CalendarMultiget,
|
||||||
// SyncCollection,
|
SyncCollection,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
#[serde(rename_all = "kebab-case")]
|
#[serde(rename_all = "kebab-case")]
|
||||||
pub struct SupportedReport {
|
pub struct ReportWrapper {
|
||||||
|
#[serde(rename = "$value")]
|
||||||
report: ReportMethod,
|
report: ReportMethod,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<ReportMethod> for SupportedReport {
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
|
#[serde(rename_all = "kebab-case")]
|
||||||
|
pub struct SupportedReportWrapper {
|
||||||
|
report: ReportWrapper,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<ReportMethod> for SupportedReportWrapper {
|
||||||
fn from(value: ReportMethod) -> Self {
|
fn from(value: ReportMethod) -> Self {
|
||||||
Self { report: value }
|
Self {
|
||||||
|
report: ReportWrapper { report: value },
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
#[serde(rename_all = "kebab-case")]
|
#[serde(rename_all = "kebab-case")]
|
||||||
pub struct SupportedReportSet {
|
pub struct SupportedReportSet {
|
||||||
supported_report: Vec<SupportedReport>,
|
supported_report: Vec<SupportedReportWrapper>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for SupportedReportSet {
|
impl Default for SupportedReportSet {
|
||||||
@@ -128,7 +137,7 @@ impl Default for SupportedReportSet {
|
|||||||
supported_report: vec![
|
supported_report: vec![
|
||||||
ReportMethod::CalendarQuery.into(),
|
ReportMethod::CalendarQuery.into(),
|
||||||
ReportMethod::CalendarMultiget.into(),
|
ReportMethod::CalendarMultiget.into(),
|
||||||
// ReportMethod::SyncCollection.into(),
|
ReportMethod::SyncCollection.into(),
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,13 +4,12 @@ use crate::namespace::Namespace;
|
|||||||
use crate::resource::InvalidProperty;
|
use crate::resource::InvalidProperty;
|
||||||
use crate::resource::Resource;
|
use crate::resource::Resource;
|
||||||
use crate::resource::ResourceService;
|
use crate::resource::ResourceService;
|
||||||
use crate::resource::{PropstatElement, PropstatResponseElement, PropstatType};
|
use crate::xml::multistatus::{PropstatElement, PropstatWrapper, ResponseElement};
|
||||||
use crate::xml::MultistatusElement;
|
use crate::xml::MultistatusElement;
|
||||||
use crate::xml::TagList;
|
use crate::xml::TagList;
|
||||||
use crate::xml::TagName;
|
use crate::xml::TagName;
|
||||||
use crate::Error;
|
use crate::Error;
|
||||||
use actix_web::http::StatusCode;
|
use actix_web::http::StatusCode;
|
||||||
use actix_web::Responder;
|
|
||||||
use actix_web::{web::Path, HttpRequest};
|
use actix_web::{web::Path, HttpRequest};
|
||||||
use log::debug;
|
use log::debug;
|
||||||
use rustical_auth::{AuthInfoExtractor, CheckAuthentication};
|
use rustical_auth::{AuthInfoExtractor, CheckAuthentication};
|
||||||
@@ -55,7 +54,7 @@ pub async fn route_proppatch<A: CheckAuthentication, R: ResourceService + ?Sized
|
|||||||
body: String,
|
body: String,
|
||||||
req: HttpRequest,
|
req: HttpRequest,
|
||||||
auth: AuthInfoExtractor<A>,
|
auth: AuthInfoExtractor<A>,
|
||||||
) -> Result<impl Responder, R::Error> {
|
) -> Result<MultistatusElement<PropstatWrapper<String>, PropstatWrapper<String>>, R::Error> {
|
||||||
let auth_info = auth.inner;
|
let auth_info = auth.inner;
|
||||||
let path_components = path.into_inner();
|
let path_components = path.into_inner();
|
||||||
let href = req.path().to_owned();
|
let href = req.path().to_owned();
|
||||||
@@ -138,25 +137,25 @@ pub async fn route_proppatch<A: CheckAuthentication, R: ResourceService + ?Sized
|
|||||||
}
|
}
|
||||||
|
|
||||||
Ok(MultistatusElement {
|
Ok(MultistatusElement {
|
||||||
responses: vec![PropstatResponseElement {
|
responses: vec![ResponseElement {
|
||||||
href,
|
href,
|
||||||
propstat: vec![
|
propstat: vec![
|
||||||
PropstatType::Normal(PropstatElement {
|
PropstatWrapper::TagList(PropstatElement {
|
||||||
prop: TagList::from(props_ok),
|
prop: TagList::from(props_ok),
|
||||||
status: format!("HTTP/1.1 {}", StatusCode::OK),
|
status: format!("HTTP/1.1 {}", StatusCode::OK),
|
||||||
}),
|
}),
|
||||||
PropstatType::NotFound(PropstatElement {
|
PropstatWrapper::TagList(PropstatElement {
|
||||||
prop: TagList::from(props_not_found),
|
prop: TagList::from(props_not_found),
|
||||||
status: format!("HTTP/1.1 {}", StatusCode::NOT_FOUND),
|
status: format!("HTTP/1.1 {}", StatusCode::NOT_FOUND),
|
||||||
}),
|
}),
|
||||||
PropstatType::Conflict(PropstatElement {
|
PropstatWrapper::TagList(PropstatElement {
|
||||||
prop: TagList::from(props_conflict),
|
prop: TagList::from(props_conflict),
|
||||||
status: format!("HTTP/1.1 {}", StatusCode::CONFLICT),
|
status: format!("HTTP/1.1 {}", StatusCode::CONFLICT),
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
}],
|
}],
|
||||||
// Dummy just for typing
|
// Dummy just for typing
|
||||||
member_responses: Vec::<String>::new(),
|
member_responses: vec![],
|
||||||
ns_dav: Namespace::Dav.as_str(),
|
ns_dav: Namespace::Dav.as_str(),
|
||||||
ns_caldav: Namespace::CalDAV.as_str(),
|
ns_caldav: Namespace::CalDAV.as_str(),
|
||||||
ns_ical: Namespace::ICal.as_str(),
|
ns_ical: Namespace::ICal.as_str(),
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
use crate::xml::TagList;
|
use crate::xml::multistatus::{PropTagWrapper, PropstatElement, PropstatWrapper};
|
||||||
|
use crate::xml::{multistatus::ResponseElement, TagList};
|
||||||
use crate::Error;
|
use crate::Error;
|
||||||
use actix_web::{http::StatusCode, HttpRequest, ResponseError};
|
use actix_web::{http::StatusCode, HttpRequest, ResponseError};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
@@ -65,41 +66,6 @@ pub trait ResourceService: Sized {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
|
||||||
pub struct PropTagWrapper<T: Serialize> {
|
|
||||||
#[serde(rename = "$value")]
|
|
||||||
prop: T,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
|
||||||
#[serde(untagged)]
|
|
||||||
pub enum PropWrapper<T: Serialize> {
|
|
||||||
Prop(PropTagWrapper<T>),
|
|
||||||
TagList(TagList),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
|
||||||
#[serde(rename_all = "kebab-case")]
|
|
||||||
pub struct PropstatElement<T: Serialize> {
|
|
||||||
pub prop: T,
|
|
||||||
pub status: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
|
||||||
#[serde(rename_all = "kebab-case")]
|
|
||||||
pub struct PropstatResponseElement<T1: Serialize, T2: Serialize> {
|
|
||||||
pub href: String,
|
|
||||||
pub propstat: Vec<PropstatType<T1, T2>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
|
||||||
#[serde(untagged)]
|
|
||||||
pub enum PropstatType<T1: Serialize, T2: Serialize> {
|
|
||||||
Normal(PropstatElement<T1>),
|
|
||||||
NotFound(PropstatElement<T2>),
|
|
||||||
Conflict(PropstatElement<T2>),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[async_trait(?Send)]
|
#[async_trait(?Send)]
|
||||||
pub trait HandlePropfind {
|
pub trait HandlePropfind {
|
||||||
type Error: ResponseError + From<crate::Error> + From<anyhow::Error>;
|
type Error: ResponseError + From<crate::Error> + From<anyhow::Error>;
|
||||||
@@ -121,7 +87,7 @@ impl<R: Resource> HandlePropfind for R {
|
|||||||
prefix: &str,
|
prefix: &str,
|
||||||
path: String,
|
path: String,
|
||||||
props: Vec<&str>,
|
props: Vec<&str>,
|
||||||
) -> Result<PropstatResponseElement<PropWrapper<Vec<R::Prop>>, TagList>, R::Error> {
|
) -> Result<ResponseElement<PropstatWrapper<R::Prop>>, R::Error> {
|
||||||
let mut props = props;
|
let mut props = props;
|
||||||
if props.contains(&"propname") {
|
if props.contains(&"propname") {
|
||||||
if props.len() != 1 {
|
if props.len() != 1 {
|
||||||
@@ -134,10 +100,10 @@ impl<R: Resource> HandlePropfind for R {
|
|||||||
.iter()
|
.iter()
|
||||||
.map(|&prop| prop.to_string())
|
.map(|&prop| prop.to_string())
|
||||||
.collect();
|
.collect();
|
||||||
return Ok(PropstatResponseElement {
|
return Ok(ResponseElement {
|
||||||
href: path,
|
href: path,
|
||||||
propstat: vec![PropstatType::Normal(PropstatElement {
|
propstat: vec![PropstatWrapper::TagList(PropstatElement {
|
||||||
prop: PropWrapper::TagList(TagList::from(props)),
|
prop: TagList::from(props),
|
||||||
status: format!("HTTP/1.1 {}", StatusCode::OK),
|
status: format!("HTTP/1.1 {}", StatusCode::OK),
|
||||||
})],
|
})],
|
||||||
});
|
});
|
||||||
@@ -170,14 +136,14 @@ impl<R: Resource> HandlePropfind for R {
|
|||||||
.map(|prop| self.get_prop(prefix, prop))
|
.map(|prop| self.get_prop(prefix, prop))
|
||||||
.collect::<Result<Vec<R::Prop>, R::Error>>()?;
|
.collect::<Result<Vec<R::Prop>, R::Error>>()?;
|
||||||
|
|
||||||
let mut propstats = vec![PropstatType::Normal(PropstatElement {
|
let mut propstats = vec![PropstatWrapper::Normal(PropstatElement {
|
||||||
status: format!("HTTP/1.1 {}", StatusCode::OK),
|
status: format!("HTTP/1.1 {}", StatusCode::OK),
|
||||||
prop: PropWrapper::Prop(PropTagWrapper {
|
prop: PropTagWrapper {
|
||||||
prop: prop_responses,
|
prop: prop_responses,
|
||||||
}),
|
},
|
||||||
})];
|
})];
|
||||||
if !invalid_props.is_empty() {
|
if !invalid_props.is_empty() {
|
||||||
propstats.push(PropstatType::NotFound(PropstatElement {
|
propstats.push(PropstatWrapper::TagList(PropstatElement {
|
||||||
status: format!("HTTP/1.1 {}", StatusCode::NOT_FOUND),
|
status: format!("HTTP/1.1 {}", StatusCode::NOT_FOUND),
|
||||||
prop: invalid_props
|
prop: invalid_props
|
||||||
.into_iter()
|
.into_iter()
|
||||||
@@ -186,7 +152,7 @@ impl<R: Resource> HandlePropfind for R {
|
|||||||
.into(),
|
.into(),
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
Ok(PropstatResponseElement {
|
Ok(ResponseElement {
|
||||||
href: path,
|
href: path,
|
||||||
propstat: propstats,
|
propstat: propstats,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,16 +1,60 @@
|
|||||||
|
use crate::xml::TagList;
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
body::BoxBody, http::header::ContentType, HttpRequest, HttpResponse, Responder, ResponseError,
|
body::BoxBody, http::header::ContentType, HttpRequest, HttpResponse, Responder, ResponseError,
|
||||||
};
|
};
|
||||||
use log::debug;
|
use log::debug;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
||||||
|
// Intermediate struct because of a serde limitation, see following article:
|
||||||
|
// https://stackoverflow.com/questions/78444158/unsupportedcannot-serialize-enum-newtype-variant-exampledata
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
#[serde(rename = "multistatus")]
|
pub struct PropTagWrapper<T: Serialize> {
|
||||||
|
#[serde(rename = "$value")]
|
||||||
|
pub prop: Vec<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
// #[derive(Serialize)]
|
||||||
|
// #[serde(untagged)]
|
||||||
|
// pub enum PropWrapper<T: Serialize> {
|
||||||
|
// Prop(Vec<T>),
|
||||||
|
// TagList(TagList),
|
||||||
|
// }
|
||||||
|
|
||||||
|
// RFC 2518
|
||||||
|
// <!ELEMENT propstat (prop, status, responsedescription?) >
|
||||||
|
#[derive(Serialize)]
|
||||||
|
#[serde(rename_all = "kebab-case")]
|
||||||
|
pub struct PropstatElement<PropType: Serialize> {
|
||||||
|
pub prop: PropType,
|
||||||
|
pub status: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
#[serde(untagged)]
|
||||||
|
pub enum PropstatWrapper<T: Serialize> {
|
||||||
|
Normal(PropstatElement<PropTagWrapper<T>>),
|
||||||
|
TagList(PropstatElement<TagList>),
|
||||||
|
}
|
||||||
|
|
||||||
|
// RFC 2518
|
||||||
|
// <!ELEMENT response (href, ((href*, status)|(propstat+)),
|
||||||
|
// responsedescription?) >
|
||||||
|
#[derive(Serialize)]
|
||||||
|
#[serde(rename_all = "kebab-case")]
|
||||||
|
pub struct ResponseElement<PropstatType: Serialize> {
|
||||||
|
pub href: String,
|
||||||
|
pub propstat: Vec<PropstatType>,
|
||||||
|
}
|
||||||
|
|
||||||
|
// RFC 2518
|
||||||
|
// <!ELEMENT multistatus (response+, responsedescription?) >
|
||||||
|
#[derive(Serialize)]
|
||||||
|
#[serde(rename = "multistatus", rename_all = "kebab-case")]
|
||||||
pub struct MultistatusElement<T1: Serialize, T2: Serialize> {
|
pub struct MultistatusElement<T1: Serialize, T2: Serialize> {
|
||||||
#[serde(rename = "response")]
|
#[serde(rename = "response")]
|
||||||
pub responses: Vec<T1>,
|
pub responses: Vec<ResponseElement<T1>>,
|
||||||
#[serde(rename = "response")]
|
#[serde(rename = "response")]
|
||||||
pub member_responses: Vec<T2>,
|
pub member_responses: Vec<ResponseElement<T2>>,
|
||||||
#[serde(rename = "@xmlns")]
|
#[serde(rename = "@xmlns")]
|
||||||
pub ns_dav: &'static str,
|
pub ns_dav: &'static str,
|
||||||
#[serde(rename = "@xmlns:C")]
|
#[serde(rename = "@xmlns:C")]
|
||||||
|
|||||||
Reference in New Issue
Block a user