mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 22:52:22 +00:00
dav: Migrate WebDav routes into a resource formalism
This commit is contained in:
@@ -21,3 +21,4 @@ rustical_auth = { path = "../auth/" }
|
|||||||
serde = { version = "1.0.188", features = ["serde_derive", "derive"] }
|
serde = { version = "1.0.188", features = ["serde_derive", "derive"] }
|
||||||
serde_json = "1.0.105"
|
serde_json = "1.0.105"
|
||||||
tokio = { version = "1.32.0", features = ["sync", "full"] }
|
tokio = { version = "1.32.0", features = ["sync", "full"] }
|
||||||
|
async-trait = "0.1.73"
|
||||||
|
|||||||
@@ -1,9 +1,19 @@
|
|||||||
|
use actix_web::http::header::ContentType;
|
||||||
use actix_web::http::Method;
|
use actix_web::http::Method;
|
||||||
use actix_web::web::{self, Data};
|
use actix_web::web::{self, Data, Path};
|
||||||
use actix_web::{guard, HttpResponse, Responder};
|
use actix_web::{guard, HttpRequest, HttpResponse, Responder};
|
||||||
|
use depth_extractor::Depth;
|
||||||
use error::Error;
|
use error::Error;
|
||||||
use routes::{calendar, event, principal, root};
|
use namespace::Namespace;
|
||||||
use rustical_auth::CheckAuthentication;
|
use propfind::{generate_multistatus, parse_propfind};
|
||||||
|
use quick_xml::events::BytesText;
|
||||||
|
use resource::{HandlePropfind, Resource};
|
||||||
|
use resources::calendar::CalendarResource;
|
||||||
|
use resources::event::EventResource;
|
||||||
|
use resources::principal::PrincipalCalendarsResource;
|
||||||
|
use resources::root::RootResource;
|
||||||
|
use routes::{calendar, event};
|
||||||
|
use rustical_auth::{AuthInfoExtractor, CheckAuthentication};
|
||||||
use rustical_store::calendar::CalendarStore;
|
use rustical_store::calendar::CalendarStore;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
@@ -13,6 +23,9 @@ pub mod depth_extractor;
|
|||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod namespace;
|
pub mod namespace;
|
||||||
mod propfind;
|
mod propfind;
|
||||||
|
pub mod proptypes;
|
||||||
|
pub mod resource;
|
||||||
|
pub mod resources;
|
||||||
pub mod routes;
|
pub mod routes;
|
||||||
|
|
||||||
pub struct CalDavContext<C: CalendarStore> {
|
pub struct CalDavContext<C: CalendarStore> {
|
||||||
@@ -32,44 +45,115 @@ pub fn configure_dav<A: CheckAuthentication, C: CalendarStore>(
|
|||||||
) {
|
) {
|
||||||
let propfind_method = || Method::from_str("PROPFIND").unwrap();
|
let propfind_method = || Method::from_str("PROPFIND").unwrap();
|
||||||
let report_method = || Method::from_str("REPORT").unwrap();
|
let report_method = || Method::from_str("REPORT").unwrap();
|
||||||
|
let mkcol_method = || Method::from_str("MKCOL").unwrap();
|
||||||
|
|
||||||
|
cfg.app_data(Data::new(CalDavContext {
|
||||||
|
prefix,
|
||||||
|
store: store.clone(),
|
||||||
|
}))
|
||||||
|
.app_data(Data::from(store.clone()))
|
||||||
|
.app_data(Data::from(auth))
|
||||||
|
.service(
|
||||||
|
web::resource("{path:.*}")
|
||||||
|
// Without the guard this service would handle all requests
|
||||||
|
.guard(guard::Method(Method::OPTIONS))
|
||||||
|
.to(options_handler),
|
||||||
|
)
|
||||||
|
.service(
|
||||||
|
web::resource("").route(web::method(propfind_method()).to(route_new_propfind::<
|
||||||
|
A,
|
||||||
|
RootResource,
|
||||||
|
C,
|
||||||
|
>)),
|
||||||
|
)
|
||||||
|
.service(
|
||||||
|
web::resource("/{principal}").route(
|
||||||
|
web::method(propfind_method()).to(route_new_propfind::<
|
||||||
|
A,
|
||||||
|
PrincipalCalendarsResource<C>,
|
||||||
|
C,
|
||||||
|
>),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.service(
|
||||||
|
web::resource("/{principal}/{calendar}")
|
||||||
|
.route(web::method(report_method()).to(calendar::route_report_calendar::<A, C>))
|
||||||
|
.route(
|
||||||
|
web::method(propfind_method()).to(route_new_propfind::<A, CalendarResource<C>, C>),
|
||||||
|
)
|
||||||
|
.route(web::method(mkcol_method()).to(calendar::route_mkcol_calendar::<A, C>)),
|
||||||
|
)
|
||||||
|
.service(
|
||||||
|
web::resource("/{principal}/{calendar}/{event}")
|
||||||
|
.route(web::method(propfind_method()).to(route_new_propfind::<A, EventResource, C>))
|
||||||
|
.route(web::method(Method::DELETE).to(event::delete_event::<A, C>))
|
||||||
|
.route(web::method(Method::GET).to(event::get_event::<A, C>))
|
||||||
|
.route(web::method(Method::PUT).to(event::put_event::<A, C>)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
cfg.app_data(Data::new(CalDavContext { prefix, store }))
|
async fn route_new_propfind<A: CheckAuthentication, R: Resource, C: CalendarStore>(
|
||||||
.app_data(Data::from(auth))
|
path: Path<R::UriComponents>,
|
||||||
.service(
|
body: String,
|
||||||
web::resource("{path:.*}")
|
req: HttpRequest,
|
||||||
// Without the guard this service would handle all requests
|
context: Data<CalDavContext<C>>,
|
||||||
.guard(guard::Method(Method::OPTIONS))
|
auth: AuthInfoExtractor<A>,
|
||||||
.to(options_handler),
|
depth: Depth,
|
||||||
)
|
) -> Result<HttpResponse, Error> {
|
||||||
.service(
|
let props = parse_propfind(&body).map_err(|_e| Error::BadRequest)?;
|
||||||
web::resource("")
|
let req_path = req.path().to_string();
|
||||||
.route(web::method(propfind_method()).to(root::route_propfind_root::<A, C>)),
|
let auth_info = auth.inner;
|
||||||
)
|
|
||||||
.service(
|
let resource = R::acquire_from_request(
|
||||||
web::resource("/{principal}").route(
|
req,
|
||||||
web::method(propfind_method()).to(principal::route_propfind_principal::<A, C>),
|
auth_info,
|
||||||
),
|
path.into_inner(),
|
||||||
)
|
context.prefix.to_string(),
|
||||||
.service(
|
)
|
||||||
web::resource("/{principal}/{calendar}")
|
.await
|
||||||
.route(web::method(report_method()).to(calendar::route_report_calendar::<A, C>))
|
.map_err(|_e| Error::InternalError)?;
|
||||||
.route(web::method(propfind_method()).to(calendar::route_propfind_calendar::<A, C>))
|
|
||||||
.route(web::method(mkcol_method()).to(calendar::route_mkcol_calendar::<A, C>)),
|
let mut responses = vec![resource
|
||||||
)
|
.propfind(props.clone())
|
||||||
.service(
|
.map_err(|_e| Error::InternalError)?];
|
||||||
web::resource("/{principal}/{calendar}/{event}")
|
|
||||||
.route(web::method(Method::DELETE).to(event::delete_event::<A, C>))
|
if depth != Depth::Zero {
|
||||||
.route(web::method(Method::GET).to(event::get_event::<A, C>))
|
for member in resource
|
||||||
.route(web::method(Method::PUT).to(event::put_event::<A, C>)),
|
.get_members()
|
||||||
);
|
.await
|
||||||
|
.map_err(|_e| Error::InternalError)?
|
||||||
|
{
|
||||||
|
responses.push(
|
||||||
|
member
|
||||||
|
.propfind(props.clone())
|
||||||
|
.map_err(|_e| Error::InternalError)?,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let output = generate_multistatus(
|
||||||
|
vec![Namespace::Dav, Namespace::CalDAV, Namespace::ICal],
|
||||||
|
|writer| {
|
||||||
|
for response in responses {
|
||||||
|
writer.write_event(quick_xml::events::Event::Text(BytesText::from_escaped(
|
||||||
|
response,
|
||||||
|
)))?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.map_err(|_e| Error::InternalError)?;
|
||||||
|
|
||||||
|
Ok(HttpResponse::MultiStatus()
|
||||||
|
.content_type(ContentType::xml())
|
||||||
|
.body(output))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn options_handler() -> impl Responder {
|
async fn options_handler() -> impl Responder {
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
.insert_header((
|
.insert_header((
|
||||||
"Allow",
|
"Allow",
|
||||||
"OPTIONS, GET, HEAD, POST, PUT, REPORT, PROPFIND, PROPPATCH",
|
"OPTIONS, GET, HEAD, POST, PUT, REPORT, PROPFIND, PROPPATCH, MKCOL",
|
||||||
))
|
))
|
||||||
.insert_header((
|
.insert_header((
|
||||||
"DAV",
|
"DAV",
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
use std::io::Write;
|
||||||
|
|
||||||
use actix_web::http::StatusCode;
|
use actix_web::http::StatusCode;
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use quick_xml::{
|
use quick_xml::{
|
||||||
@@ -23,17 +25,19 @@ pub fn parse_propfind(body: &str) -> Result<Vec<&str>> {
|
|||||||
return Ok(Vec::new());
|
return Ok(Vec::new());
|
||||||
};
|
};
|
||||||
|
|
||||||
match prop_node.tag_name().name() {
|
let props = match prop_node.tag_name().name() {
|
||||||
"prop" => Ok(prop_node
|
"prop" => Ok(prop_node
|
||||||
.children()
|
.children()
|
||||||
.map(|node| node.tag_name().name())
|
.map(|node| node.tag_name().name())
|
||||||
.collect()),
|
.collect()),
|
||||||
_ => Err(anyhow!("invalid prop tag")),
|
_ => Err(anyhow!("invalid prop tag")),
|
||||||
}
|
};
|
||||||
|
dbg!(body, &props);
|
||||||
|
props
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_resourcetype(
|
pub fn write_resourcetype<W: Write>(
|
||||||
writer: &mut Writer<&mut Vec<u8>>,
|
writer: &mut Writer<W>,
|
||||||
types: Vec<&str>,
|
types: Vec<&str>,
|
||||||
) -> Result<(), quick_xml::Error> {
|
) -> Result<(), quick_xml::Error> {
|
||||||
writer
|
writer
|
||||||
@@ -47,8 +51,8 @@ pub fn write_resourcetype(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_invalid_props_response(
|
pub fn write_invalid_props_response<W: Write>(
|
||||||
writer: &mut Writer<&mut Vec<u8>>,
|
writer: &mut Writer<W>,
|
||||||
href: &str,
|
href: &str,
|
||||||
invalid_props: Vec<&str>,
|
invalid_props: Vec<&str>,
|
||||||
) -> Result<(), quick_xml::Error> {
|
) -> Result<(), quick_xml::Error> {
|
||||||
@@ -68,14 +72,14 @@ pub fn write_invalid_props_response(
|
|||||||
|
|
||||||
// Writes a propstat response into a multistatus
|
// Writes a propstat response into a multistatus
|
||||||
// closure hooks into the <prop> element
|
// closure hooks into the <prop> element
|
||||||
pub fn write_propstat_response<F>(
|
pub fn write_propstat_response<F, W: Write>(
|
||||||
writer: &mut Writer<&mut Vec<u8>>,
|
writer: &mut Writer<W>,
|
||||||
href: &str,
|
href: &str,
|
||||||
status: StatusCode,
|
status: StatusCode,
|
||||||
prop_closure: F,
|
prop_closure: F,
|
||||||
) -> Result<(), quick_xml::Error>
|
) -> Result<(), quick_xml::Error>
|
||||||
where
|
where
|
||||||
F: FnOnce(&mut Writer<&mut Vec<u8>>) -> Result<(), quick_xml::Error>,
|
F: FnOnce(&mut Writer<W>) -> Result<(), quick_xml::Error>,
|
||||||
{
|
{
|
||||||
writer
|
writer
|
||||||
.create_element("response")
|
.create_element("response")
|
||||||
|
|||||||
39
crates/dav/src/proptypes.rs
Normal file
39
crates/dav/src/proptypes.rs
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
use std::io::Write;
|
||||||
|
|
||||||
|
use quick_xml::{events::BytesText, Writer};
|
||||||
|
|
||||||
|
pub fn write_string_prop<'a, W: Write>(
|
||||||
|
writer: &'a mut Writer<W>,
|
||||||
|
propname: &'a str,
|
||||||
|
value: &str,
|
||||||
|
) -> Result<&'a mut Writer<W>, quick_xml::Error> {
|
||||||
|
let el = writer.create_element(propname);
|
||||||
|
if value.is_empty() {
|
||||||
|
el.write_empty()
|
||||||
|
} else {
|
||||||
|
el.write_text_content(BytesText::new(value))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn write_href_prop<'a, W: Write>(
|
||||||
|
writer: &'a mut Writer<W>,
|
||||||
|
propname: &'a str,
|
||||||
|
href: &str,
|
||||||
|
) -> Result<&'a mut Writer<W>, quick_xml::Error> {
|
||||||
|
write_hrefs_prop(writer, propname, vec![href])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn write_hrefs_prop<'a, W: Write>(
|
||||||
|
writer: &'a mut Writer<W>,
|
||||||
|
propname: &'a str,
|
||||||
|
hrefs: Vec<&str>,
|
||||||
|
) -> Result<&'a mut Writer<W>, quick_xml::Error> {
|
||||||
|
writer
|
||||||
|
.create_element(propname)
|
||||||
|
.write_inner_content(|writer| {
|
||||||
|
for href in hrefs {
|
||||||
|
write_string_prop(writer, "href", href)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
|
}
|
||||||
70
crates/dav/src/resource.rs
Normal file
70
crates/dav/src/resource.rs
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
use std::io::Write;
|
||||||
|
|
||||||
|
use actix_web::{http::StatusCode, HttpRequest};
|
||||||
|
use anyhow::{anyhow, Result};
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use quick_xml::Writer;
|
||||||
|
use rustical_auth::AuthInfo;
|
||||||
|
|
||||||
|
use crate::propfind::{write_invalid_props_response, write_propstat_response};
|
||||||
|
|
||||||
|
// A resource is identified by a URI and has properties
|
||||||
|
// A resource can also be a collection
|
||||||
|
// A resource cannot be none, only Methods like PROPFIND, GET, REPORT, etc. can be exposed
|
||||||
|
// A resource exists
|
||||||
|
#[async_trait(?Send)]
|
||||||
|
pub trait Resource: Sized {
|
||||||
|
type MemberType: Resource;
|
||||||
|
type UriComponents: Sized; // defines how the resource URI maps to parameters, i.e. /{principal}/{calendar} -> (String, String)
|
||||||
|
|
||||||
|
async fn acquire_from_request(
|
||||||
|
req: HttpRequest,
|
||||||
|
auth_info: AuthInfo,
|
||||||
|
uri_components: Self::UriComponents,
|
||||||
|
prefix: String,
|
||||||
|
) -> Result<Self>;
|
||||||
|
|
||||||
|
fn get_path(&self) -> &str;
|
||||||
|
async fn get_members(&self) -> Result<Vec<Self::MemberType>>;
|
||||||
|
|
||||||
|
fn list_dead_props() -> Vec<&'static str>;
|
||||||
|
fn write_prop<W: Write>(&self, writer: &mut Writer<W>, prop: &str) -> Result<()>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait HandlePropfind {
|
||||||
|
fn propfind(&self, props: Vec<&str>) -> Result<String>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<R: Resource> HandlePropfind for R {
|
||||||
|
fn propfind(&self, props: Vec<&str>) -> Result<String> {
|
||||||
|
let mut props = props;
|
||||||
|
if props.contains(&"allprops") {
|
||||||
|
if props.len() != 1 {
|
||||||
|
// allprops MUST be the only queried prop per spec
|
||||||
|
return Err(anyhow!("allprops MUST be the only quereid prop"));
|
||||||
|
}
|
||||||
|
props = R::list_dead_props();
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut invalid_props = Vec::<&str>::new();
|
||||||
|
|
||||||
|
let mut output_buffer = Vec::new();
|
||||||
|
let mut writer = Writer::new_with_indent(&mut output_buffer, b' ', 2);
|
||||||
|
|
||||||
|
write_propstat_response(&mut writer, self.get_path(), StatusCode::OK, |writer| {
|
||||||
|
for prop in props {
|
||||||
|
// TODO: Fix error types
|
||||||
|
match self
|
||||||
|
.write_prop(writer, prop)
|
||||||
|
.map_err(|_e| quick_xml::Error::TextNotFound)
|
||||||
|
{
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(_) => invalid_props.push(prop),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
})?;
|
||||||
|
write_invalid_props_response(&mut writer, self.get_path(), invalid_props)?;
|
||||||
|
Ok(std::str::from_utf8(&output_buffer)?.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
175
crates/dav/src/resources/calendar.rs
Normal file
175
crates/dav/src/resources/calendar.rs
Normal file
@@ -0,0 +1,175 @@
|
|||||||
|
use std::{io::Write, sync::Arc};
|
||||||
|
|
||||||
|
use actix_web::{web::Data, HttpRequest};
|
||||||
|
use anyhow::{anyhow, Result};
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use quick_xml::{events::BytesText, Writer};
|
||||||
|
use rustical_auth::AuthInfo;
|
||||||
|
use rustical_store::calendar::{Calendar, CalendarStore};
|
||||||
|
use tokio::sync::RwLock;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
propfind::write_resourcetype,
|
||||||
|
proptypes::{write_href_prop, write_string_prop},
|
||||||
|
resource::Resource,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct CalendarResource<C: CalendarStore> {
|
||||||
|
pub cal_store: Arc<RwLock<C>>,
|
||||||
|
pub calendar: Calendar,
|
||||||
|
pub path: String,
|
||||||
|
pub prefix: String,
|
||||||
|
pub principal: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait(?Send)]
|
||||||
|
impl<C: CalendarStore> Resource for CalendarResource<C> {
|
||||||
|
type MemberType = Self;
|
||||||
|
type UriComponents = (String, String); // principal, calendar_id
|
||||||
|
|
||||||
|
async fn acquire_from_request(
|
||||||
|
req: HttpRequest,
|
||||||
|
_auth_info: AuthInfo,
|
||||||
|
uri_components: Self::UriComponents,
|
||||||
|
prefix: String,
|
||||||
|
) -> Result<Self> {
|
||||||
|
let cal_store = req
|
||||||
|
.app_data::<Data<RwLock<C>>>()
|
||||||
|
.ok_or(anyhow!("no calendar store in app_data!"))?
|
||||||
|
.clone()
|
||||||
|
.into_inner();
|
||||||
|
|
||||||
|
let (principal, cid) = uri_components;
|
||||||
|
let calendar = cal_store.read().await.get_calendar(&cid).await?;
|
||||||
|
Ok(Self {
|
||||||
|
cal_store,
|
||||||
|
calendar,
|
||||||
|
path: req.path().to_string(),
|
||||||
|
prefix,
|
||||||
|
principal,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_path(&self) -> &str {
|
||||||
|
&self.path
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_members(&self) -> Result<Vec<Self::MemberType>> {
|
||||||
|
// As of now the calendar resource has no members
|
||||||
|
Ok(vec![])
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn list_dead_props() -> Vec<&'static str> {
|
||||||
|
vec![
|
||||||
|
"resourcetype",
|
||||||
|
"current-user-principal",
|
||||||
|
"displayname",
|
||||||
|
"supported-calendar-component-set",
|
||||||
|
"supported-calendar-data",
|
||||||
|
"getcontenttype",
|
||||||
|
"calendar-description",
|
||||||
|
"owner",
|
||||||
|
"calendar-color",
|
||||||
|
"current-user-privilege-set",
|
||||||
|
"max-resource-size",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
fn write_prop<W: Write>(&self, writer: &mut Writer<W>, prop: &str) -> Result<()> {
|
||||||
|
match prop {
|
||||||
|
"resourcetype" => write_resourcetype(writer, vec!["C:calendar", "collection"])?,
|
||||||
|
"current-user-principal" | "owner" => {
|
||||||
|
write_href_prop(
|
||||||
|
writer,
|
||||||
|
prop,
|
||||||
|
&format!("{}/{}/", self.prefix, self.principal),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
"displayname" => {
|
||||||
|
let el = writer.create_element("displayname");
|
||||||
|
if let Some(name) = self.calendar.clone().name {
|
||||||
|
el.write_text_content(BytesText::new(&name))?;
|
||||||
|
} else {
|
||||||
|
el.write_empty()?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"calendar-color" => {
|
||||||
|
let el = writer.create_element("IC:calendar-color");
|
||||||
|
if let Some(color) = self.calendar.clone().color {
|
||||||
|
el.write_text_content(BytesText::new(&color))?;
|
||||||
|
} else {
|
||||||
|
el.write_empty()?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"calendar-description" => {
|
||||||
|
let el = writer.create_element("C:calendar-description");
|
||||||
|
if let Some(description) = self.calendar.clone().description {
|
||||||
|
el.write_text_content(BytesText::new(&description))?;
|
||||||
|
} else {
|
||||||
|
el.write_empty()?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"supported-calendar-component-set" => {
|
||||||
|
writer
|
||||||
|
.create_element("C:supported-calendar-component-set")
|
||||||
|
.write_inner_content(|writer| {
|
||||||
|
writer
|
||||||
|
.create_element("C:comp")
|
||||||
|
.with_attribute(("name", "VEVENT"))
|
||||||
|
.write_empty()?;
|
||||||
|
Ok(())
|
||||||
|
})?;
|
||||||
|
}
|
||||||
|
"supported-calendar-data" => {
|
||||||
|
writer
|
||||||
|
.create_element("C:supported-calendar-data")
|
||||||
|
.write_inner_content(|writer| {
|
||||||
|
// <cal:calendar-data content-type="text/calendar" version="2.0" />
|
||||||
|
writer
|
||||||
|
.create_element("C:calendar-data")
|
||||||
|
.with_attributes(vec![
|
||||||
|
("content-type", "text/calendar"),
|
||||||
|
("version", "2.0"),
|
||||||
|
])
|
||||||
|
.write_empty()?;
|
||||||
|
Ok(())
|
||||||
|
})?;
|
||||||
|
}
|
||||||
|
"getcontenttype" => {
|
||||||
|
write_string_prop(writer, "getcontenttype", "text/calendar")?;
|
||||||
|
}
|
||||||
|
"max-resource-size" => {
|
||||||
|
write_string_prop(writer, "max-resource-size", "10000000")?;
|
||||||
|
}
|
||||||
|
"current-user-privilege-set" => {
|
||||||
|
writer
|
||||||
|
.create_element("current-user-privilege-set")
|
||||||
|
// These are just hard-coded for now and will possibly change in the future
|
||||||
|
.write_inner_content(|writer| {
|
||||||
|
for privilege in [
|
||||||
|
"read",
|
||||||
|
"read-acl",
|
||||||
|
"write",
|
||||||
|
"write-acl",
|
||||||
|
"write-content",
|
||||||
|
"read-current-user-privilege-set",
|
||||||
|
"bind",
|
||||||
|
"unbind",
|
||||||
|
] {
|
||||||
|
writer
|
||||||
|
.create_element("privilege")
|
||||||
|
.write_inner_content(|writer| {
|
||||||
|
writer.create_element(privilege).write_empty()?;
|
||||||
|
Ok(())
|
||||||
|
})?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
})?;
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
return Err(anyhow!("invalid prop"));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
46
crates/dav/src/resources/event.rs
Normal file
46
crates/dav/src/resources/event.rs
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
use crate::resource::Resource;
|
||||||
|
use actix_web::HttpRequest;
|
||||||
|
use anyhow::{anyhow, Result};
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use rustical_auth::AuthInfo;
|
||||||
|
|
||||||
|
pub struct EventResource {
|
||||||
|
path: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait(?Send)]
|
||||||
|
impl Resource for EventResource {
|
||||||
|
type UriComponents = (String, String, String); // principal, calendar, event
|
||||||
|
type MemberType = Self;
|
||||||
|
|
||||||
|
fn get_path(&self) -> &str {
|
||||||
|
&self.path
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_members(&self) -> Result<Vec<Self::MemberType>> {
|
||||||
|
Ok(vec![])
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn acquire_from_request(
|
||||||
|
req: HttpRequest,
|
||||||
|
_auth_info: AuthInfo,
|
||||||
|
_uri_components: Self::UriComponents,
|
||||||
|
_prefix: String,
|
||||||
|
) -> Result<Self> {
|
||||||
|
Ok(Self {
|
||||||
|
path: req.path().to_string(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_prop<W: std::io::Write>(
|
||||||
|
&self,
|
||||||
|
_writer: &mut quick_xml::Writer<W>,
|
||||||
|
_prop: &str,
|
||||||
|
) -> Result<()> {
|
||||||
|
Err(anyhow!("invalid prop!"))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn list_dead_props() -> Vec<&'static str> {
|
||||||
|
vec![]
|
||||||
|
}
|
||||||
|
}
|
||||||
4
crates/dav/src/resources/mod.rs
Normal file
4
crates/dav/src/resources/mod.rs
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
pub mod calendar;
|
||||||
|
pub mod event;
|
||||||
|
pub mod principal;
|
||||||
|
pub mod root;
|
||||||
114
crates/dav/src/resources/principal.rs
Normal file
114
crates/dav/src/resources/principal.rs
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use crate::{propfind::write_resourcetype, proptypes::write_href_prop, resource::Resource};
|
||||||
|
use actix_web::{web::Data, HttpRequest};
|
||||||
|
use anyhow::{anyhow, Result};
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use quick_xml::events::BytesText;
|
||||||
|
use rustical_auth::AuthInfo;
|
||||||
|
use rustical_store::calendar::CalendarStore;
|
||||||
|
use tokio::sync::RwLock;
|
||||||
|
|
||||||
|
use super::calendar::CalendarResource;
|
||||||
|
|
||||||
|
pub struct PrincipalCalendarsResource<C: CalendarStore> {
|
||||||
|
prefix: String,
|
||||||
|
principal: String,
|
||||||
|
path: String,
|
||||||
|
cal_store: Arc<RwLock<C>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait(?Send)]
|
||||||
|
impl<C: CalendarStore> Resource for PrincipalCalendarsResource<C> {
|
||||||
|
type UriComponents = ();
|
||||||
|
type MemberType = CalendarResource<C>;
|
||||||
|
|
||||||
|
fn get_path(&self) -> &str {
|
||||||
|
&self.path
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_members(&self) -> Result<Vec<Self::MemberType>> {
|
||||||
|
let calendars = self
|
||||||
|
.cal_store
|
||||||
|
.read()
|
||||||
|
.await
|
||||||
|
.get_calendars(&self.principal)
|
||||||
|
.await?;
|
||||||
|
let mut out = Vec::new();
|
||||||
|
for calendar in calendars {
|
||||||
|
let path = format!("{}/{}", &self.path, &calendar.id);
|
||||||
|
out.push(CalendarResource {
|
||||||
|
cal_store: self.cal_store.clone(),
|
||||||
|
calendar,
|
||||||
|
path,
|
||||||
|
prefix: self.prefix.clone(),
|
||||||
|
principal: self.principal.clone(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Ok(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn acquire_from_request(
|
||||||
|
req: HttpRequest,
|
||||||
|
auth_info: AuthInfo,
|
||||||
|
_uri_components: Self::UriComponents,
|
||||||
|
prefix: String,
|
||||||
|
) -> Result<Self> {
|
||||||
|
let cal_store = req
|
||||||
|
.app_data::<Data<RwLock<C>>>()
|
||||||
|
.ok_or(anyhow!("no calendar store in app_data!"))?
|
||||||
|
.clone()
|
||||||
|
.into_inner();
|
||||||
|
Ok(Self {
|
||||||
|
cal_store,
|
||||||
|
prefix,
|
||||||
|
principal: auth_info.user_id,
|
||||||
|
path: req.path().to_string(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_prop<W: std::io::Write>(
|
||||||
|
&self,
|
||||||
|
writer: &mut quick_xml::Writer<W>,
|
||||||
|
prop: &str,
|
||||||
|
) -> Result<()> {
|
||||||
|
match prop {
|
||||||
|
"resourcetype" => write_resourcetype(writer, vec!["principal", "collection"])?,
|
||||||
|
"current-user-principal" | "principal-URL" => {
|
||||||
|
write_href_prop(
|
||||||
|
writer,
|
||||||
|
prop,
|
||||||
|
&format!("{}/{}/", self.prefix, self.principal),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
"calendar-home-set" | "calendar-user-address-set" => {
|
||||||
|
writer
|
||||||
|
.create_element(&format!("C:{prop}"))
|
||||||
|
.write_inner_content(|writer| {
|
||||||
|
writer
|
||||||
|
.create_element("href")
|
||||||
|
.write_text_content(BytesText::new(&format!(
|
||||||
|
"{}/{}/",
|
||||||
|
self.prefix, self.principal
|
||||||
|
)))?;
|
||||||
|
Ok(())
|
||||||
|
})?;
|
||||||
|
}
|
||||||
|
"allprops" => {}
|
||||||
|
_ => {
|
||||||
|
return Err(anyhow!("invalid prop"));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn list_dead_props() -> Vec<&'static str> {
|
||||||
|
vec![
|
||||||
|
"resourcetype",
|
||||||
|
"current-user-principal",
|
||||||
|
"principal-URL",
|
||||||
|
"calendar-home-set",
|
||||||
|
"calendar-user-address-set",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
68
crates/dav/src/resources/root.rs
Normal file
68
crates/dav/src/resources/root.rs
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
use crate::{propfind::write_resourcetype, resource::Resource};
|
||||||
|
use actix_web::HttpRequest;
|
||||||
|
use anyhow::{anyhow, Result};
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use quick_xml::events::BytesText;
|
||||||
|
use rustical_auth::AuthInfo;
|
||||||
|
|
||||||
|
pub struct RootResource {
|
||||||
|
prefix: String,
|
||||||
|
principal: String,
|
||||||
|
path: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait(?Send)]
|
||||||
|
impl Resource for RootResource {
|
||||||
|
type UriComponents = ();
|
||||||
|
type MemberType = Self;
|
||||||
|
|
||||||
|
fn get_path(&self) -> &str {
|
||||||
|
&self.path
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_members(&self) -> Result<Vec<Self::MemberType>> {
|
||||||
|
Ok(vec![])
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn acquire_from_request(
|
||||||
|
req: HttpRequest,
|
||||||
|
auth_info: AuthInfo,
|
||||||
|
_uri_components: Self::UriComponents,
|
||||||
|
prefix: String,
|
||||||
|
) -> Result<Self> {
|
||||||
|
Ok(Self {
|
||||||
|
prefix,
|
||||||
|
principal: auth_info.user_id,
|
||||||
|
path: req.path().to_string(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_prop<W: std::io::Write>(
|
||||||
|
&self,
|
||||||
|
writer: &mut quick_xml::Writer<W>,
|
||||||
|
prop: &str,
|
||||||
|
) -> Result<()> {
|
||||||
|
match prop {
|
||||||
|
"resourcetype" => write_resourcetype(writer, vec!["collection"])?,
|
||||||
|
"current-user-principal" => {
|
||||||
|
writer
|
||||||
|
.create_element("current-user-principal")
|
||||||
|
.write_inner_content(|writer| {
|
||||||
|
writer
|
||||||
|
.create_element("href")
|
||||||
|
.write_text_content(BytesText::new(&format!(
|
||||||
|
"{}/{}",
|
||||||
|
self.prefix, self.principal
|
||||||
|
)))?;
|
||||||
|
Ok(())
|
||||||
|
})?;
|
||||||
|
}
|
||||||
|
_ => return Err(anyhow!("invalid prop!")),
|
||||||
|
};
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn list_dead_props() -> Vec<&'static str> {
|
||||||
|
vec!["resourcetype", "current-user-principal"]
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,22 +1,38 @@
|
|||||||
use rustical_auth::{AuthInfoExtractor, CheckAuthentication};
|
|
||||||
use crate::namespace::Namespace;
|
use crate::namespace::Namespace;
|
||||||
use crate::propfind::{
|
use crate::propfind::{generate_multistatus, write_propstat_response};
|
||||||
generate_multistatus, parse_propfind, write_invalid_props_response, write_propstat_response,
|
use crate::proptypes::write_string_prop;
|
||||||
write_resourcetype,
|
|
||||||
};
|
|
||||||
use crate::{CalDavContext, Error};
|
use crate::{CalDavContext, Error};
|
||||||
use actix_web::http::header::ContentType;
|
use actix_web::http::header::ContentType;
|
||||||
use actix_web::http::StatusCode;
|
use actix_web::http::StatusCode;
|
||||||
use actix_web::web::{Data, Path};
|
use actix_web::web::{Data, Path};
|
||||||
use actix_web::{HttpRequest, HttpResponse};
|
use actix_web::{HttpRequest, HttpResponse};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use quick_xml::events::BytesText;
|
|
||||||
use quick_xml::Writer;
|
|
||||||
use roxmltree::{Node, NodeType};
|
use roxmltree::{Node, NodeType};
|
||||||
|
use rustical_auth::{AuthInfoExtractor, CheckAuthentication};
|
||||||
use rustical_store::calendar::{Calendar, CalendarStore, Event};
|
use rustical_store::calendar::{Calendar, CalendarStore, Event};
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use tokio::sync::RwLock;
|
||||||
|
|
||||||
|
async fn _parse_filter(filter_node: &Node<'_, '_>) {
|
||||||
|
for comp_filter_node in filter_node.children() {
|
||||||
|
if comp_filter_node.tag_name().name() != "comp-filter" {
|
||||||
|
dbg!("wtf", comp_filter_node.tag_name().name());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
for filter in filter_node.children() {
|
||||||
|
match filter.tag_name().name() {
|
||||||
|
// <time-range start=\"20230804T125257Z\" end=\"20231013T125257Z\"/
|
||||||
|
"time-range" => {}
|
||||||
|
_ => {
|
||||||
|
dbg!("unknown filter", filter.tag_name());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async fn handle_report_calendar_query(
|
async fn handle_report_calendar_query(
|
||||||
query_node: Node<'_, '_>,
|
query_node: Node<'_, '_>,
|
||||||
@@ -44,19 +60,17 @@ async fn handle_report_calendar_query(
|
|||||||
for prop in props.deref() {
|
for prop in props.deref() {
|
||||||
match *prop {
|
match *prop {
|
||||||
"getetag" => {
|
"getetag" => {
|
||||||
writer
|
write_string_prop(writer, "getetag", &event.get_etag())?;
|
||||||
.create_element("getetag")
|
|
||||||
.write_text_content(BytesText::new(&event.get_etag()))?;
|
|
||||||
}
|
}
|
||||||
"calendar-data" => {
|
"calendar-data" => {
|
||||||
writer
|
write_string_prop(writer, "C:calendar-data", event.to_ics())?;
|
||||||
.create_element("C:calendar-data")
|
|
||||||
.write_text_content(BytesText::new(event.to_ics()))?;
|
|
||||||
}
|
}
|
||||||
"getcontenttype" => {
|
"getcontenttype" => {
|
||||||
writer
|
write_string_prop(
|
||||||
.create_element("getcontenttype")
|
writer,
|
||||||
.write_text_content(BytesText::new("text/calendar"))?;
|
"getcontenttype",
|
||||||
|
"text/calendar;charset=utf-8",
|
||||||
|
)?;
|
||||||
}
|
}
|
||||||
prop => {
|
prop => {
|
||||||
dbg!(prop);
|
dbg!(prop);
|
||||||
@@ -86,10 +100,12 @@ pub async fn route_report_calendar<A: CheckAuthentication, C: CalendarStore>(
|
|||||||
// TODO: Check authorization
|
// TODO: Check authorization
|
||||||
let (_principal, cid) = path.into_inner();
|
let (_principal, cid) = path.into_inner();
|
||||||
|
|
||||||
let doc = roxmltree::Document::parse(&body).map_err(|_e| Error::InternalError)?;
|
let doc = roxmltree::Document::parse(&body).map_err(|_e| Error::BadRequest)?;
|
||||||
let query_node = doc.root_element();
|
let query_node = doc.root_element();
|
||||||
let events = context.store.read().await.get_events(&cid).await.unwrap();
|
let events = context.store.read().await.get_events(&cid).await.unwrap();
|
||||||
|
|
||||||
|
dbg!(&body);
|
||||||
|
|
||||||
// TODO: implement filtering
|
// TODO: implement filtering
|
||||||
match query_node.tag_name().name() {
|
match query_node.tag_name().name() {
|
||||||
"calendar-query" => {}
|
"calendar-query" => {}
|
||||||
@@ -99,187 +115,76 @@ pub async fn route_report_calendar<A: CheckAuthentication, C: CalendarStore>(
|
|||||||
handle_report_calendar_query(query_node, request, events).await
|
handle_report_calendar_query(query_node, request, events).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn generate_propfind_calendar_response(
|
pub async fn handle_mkcol_calendar_set<C: CalendarStore>(
|
||||||
props: Vec<&str>,
|
store: &RwLock<C>,
|
||||||
principal: &str,
|
prop_node: Node<'_, '_>,
|
||||||
path: &str,
|
cid: String,
|
||||||
prefix: &str,
|
owner: String,
|
||||||
calendar: &Calendar,
|
) -> Result<()> {
|
||||||
) -> Result<String> {
|
let mut cal = Calendar {
|
||||||
let mut props = props;
|
owner,
|
||||||
if props.contains(&"allprops") {
|
id: cid.clone(),
|
||||||
props.extend(
|
..Default::default()
|
||||||
[
|
};
|
||||||
"resourcetype",
|
for prop in prop_node.children() {
|
||||||
"current-user-principal",
|
match prop.tag_name().name() {
|
||||||
"displayname",
|
"displayname" => {
|
||||||
"supported-calendar-component-set",
|
cal.name = prop.text().map(|s| s.to_string());
|
||||||
"supported-calendar-data",
|
}
|
||||||
"getcontenttype",
|
"timezone" => {
|
||||||
"calendar-description",
|
cal.timezone = prop.text().map(|s| s.to_string());
|
||||||
"owner",
|
}
|
||||||
"calendar-color",
|
"calendar-color" => {
|
||||||
"current-user-privilege-set",
|
cal.color = prop.text().map(|s| s.to_string());
|
||||||
"max-resource-size",
|
}
|
||||||
]
|
"calendar-description" => {
|
||||||
.iter(),
|
cal.description = prop.text().map(|s| s.to_string());
|
||||||
);
|
}
|
||||||
|
"calendar-timezone" => {
|
||||||
|
cal.timezone = prop.text().map(|s| s.to_string());
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
println!("unsupported mkcol tag: {}", prop.tag_name().name())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut invalid_props = Vec::<&str>::new();
|
store.write().await.insert_calendar(cid, cal).await?;
|
||||||
let mut output_buffer = Vec::new();
|
Ok(())
|
||||||
let mut writer = Writer::new_with_indent(&mut output_buffer, b' ', 2);
|
|
||||||
|
|
||||||
write_propstat_response(&mut writer, path, StatusCode::OK, |writer| {
|
|
||||||
for prop in props {
|
|
||||||
match prop {
|
|
||||||
"resourcetype" => write_resourcetype(writer, vec!["C:calendar", "collection"])?,
|
|
||||||
"current-user-principal" | "owner" => {
|
|
||||||
writer.create_element(prop).write_inner_content(|writer| {
|
|
||||||
writer
|
|
||||||
.create_element("href")
|
|
||||||
.write_text_content(BytesText::new(&format!(
|
|
||||||
"{prefix}/{principal}/",
|
|
||||||
)))?;
|
|
||||||
Ok(())
|
|
||||||
})?;
|
|
||||||
}
|
|
||||||
"displayname" => {
|
|
||||||
let el = writer.create_element("displayname");
|
|
||||||
if let Some(name) = calendar.clone().name {
|
|
||||||
el.write_text_content(BytesText::new(&name))?;
|
|
||||||
} else {
|
|
||||||
el.write_empty()?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"calendar-color" => {
|
|
||||||
let el = writer.create_element("IC:calendar-color");
|
|
||||||
if let Some(color) = calendar.clone().color {
|
|
||||||
el.write_text_content(BytesText::new(&color))?;
|
|
||||||
} else {
|
|
||||||
el.write_empty()?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"calendar-description" => {
|
|
||||||
let el = writer.create_element("C:calendar-description");
|
|
||||||
if let Some(description) = calendar.clone().description {
|
|
||||||
el.write_text_content(BytesText::new(&description))?;
|
|
||||||
} else {
|
|
||||||
el.write_empty()?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"supported-calendar-component-set" => {
|
|
||||||
writer
|
|
||||||
.create_element("C:supported-calendar-component-set")
|
|
||||||
.write_inner_content(|writer| {
|
|
||||||
writer
|
|
||||||
.create_element("C:comp")
|
|
||||||
.with_attribute(("name", "VEVENT"))
|
|
||||||
.write_empty()?;
|
|
||||||
Ok(())
|
|
||||||
})?;
|
|
||||||
}
|
|
||||||
"supported-calendar-data" => {
|
|
||||||
writer
|
|
||||||
.create_element("C:supported-calendar-data")
|
|
||||||
.write_inner_content(|writer| {
|
|
||||||
// <cal:calendar-data content-type="text/calendar" version="2.0" />
|
|
||||||
writer
|
|
||||||
.create_element("C:calendar-data")
|
|
||||||
.with_attributes(vec![
|
|
||||||
("content-type", "text/calendar"),
|
|
||||||
("version", "2.0"),
|
|
||||||
])
|
|
||||||
.write_empty()?;
|
|
||||||
Ok(())
|
|
||||||
})?;
|
|
||||||
}
|
|
||||||
"getcontenttype" => {
|
|
||||||
writer
|
|
||||||
.create_element("getcontenttype")
|
|
||||||
.write_text_content(BytesText::new("text/calendar"))?;
|
|
||||||
}
|
|
||||||
"max-resource-size" => {
|
|
||||||
writer
|
|
||||||
.create_element("max-resource-size")
|
|
||||||
.write_text_content(BytesText::new("10000000"))?;
|
|
||||||
}
|
|
||||||
"current-user-privilege-set" => {
|
|
||||||
writer
|
|
||||||
.create_element("current-user-privilege-set")
|
|
||||||
// These are just hard-coded for now and will possibly change in the future
|
|
||||||
.write_inner_content(|writer| {
|
|
||||||
for privilege in [
|
|
||||||
"read",
|
|
||||||
"read-acl",
|
|
||||||
"write",
|
|
||||||
"write-acl",
|
|
||||||
"write-content",
|
|
||||||
"read-current-user-privilege-set",
|
|
||||||
"bind",
|
|
||||||
"unbind",
|
|
||||||
] {
|
|
||||||
writer.create_element("privilege").write_inner_content(
|
|
||||||
|writer| {
|
|
||||||
writer.create_element(privilege).write_empty()?;
|
|
||||||
Ok(())
|
|
||||||
},
|
|
||||||
)?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
})?;
|
|
||||||
}
|
|
||||||
"allprops" => {}
|
|
||||||
_ => invalid_props.push(prop),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
})?;
|
|
||||||
|
|
||||||
write_invalid_props_response(&mut writer, path, invalid_props)?;
|
|
||||||
Ok(std::str::from_utf8(&output_buffer)?.to_string())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn route_propfind_calendar<A: CheckAuthentication, C: CalendarStore>(
|
pub async fn route_mkcol_calendar<A: CheckAuthentication, C: CalendarStore>(
|
||||||
path: Path<(String, String)>,
|
path: Path<(String, String)>,
|
||||||
body: String,
|
body: String,
|
||||||
request: HttpRequest,
|
|
||||||
context: Data<CalDavContext<C>>,
|
|
||||||
auth: AuthInfoExtractor<A>,
|
auth: AuthInfoExtractor<A>,
|
||||||
|
context: Data<CalDavContext<C>>,
|
||||||
) -> Result<HttpResponse, Error> {
|
) -> Result<HttpResponse, Error> {
|
||||||
// TODO: Check authorization
|
|
||||||
let (_principal, cid) = path.into_inner();
|
let (_principal, cid) = path.into_inner();
|
||||||
let calendar = context
|
let doc = roxmltree::Document::parse(&body).map_err(|_e| Error::BadRequest)?;
|
||||||
.store
|
let mkcol_node = doc.root_element();
|
||||||
.read()
|
match mkcol_node.tag_name().name() {
|
||||||
.await
|
"mkcol" => {}
|
||||||
.get_calendar(&cid)
|
_ => return Err(Error::BadRequest),
|
||||||
|
}
|
||||||
|
|
||||||
|
for set_node in mkcol_node.children() {
|
||||||
|
if set_node.tag_name().name() != "set" {
|
||||||
|
return Err(Error::BadRequest);
|
||||||
|
}
|
||||||
|
let prop_node = set_node.first_element_child().ok_or(Error::BadRequest)?;
|
||||||
|
if prop_node.tag_name().name() != "prop" {
|
||||||
|
return Err(Error::BadRequest);
|
||||||
|
}
|
||||||
|
handle_mkcol_calendar_set(
|
||||||
|
&context.store,
|
||||||
|
prop_node,
|
||||||
|
cid.clone(),
|
||||||
|
auth.inner.user_id.clone(),
|
||||||
|
)
|
||||||
.await
|
.await
|
||||||
.map_err(|_e| Error::InternalError)?;
|
.map_err(|_e| Error::InternalError)?;
|
||||||
|
}
|
||||||
|
|
||||||
let props = parse_propfind(&body).map_err(|_e| Error::BadRequest)?;
|
println!("{body}");
|
||||||
|
Err(Error::InternalError)
|
||||||
let responses_string = generate_propfind_calendar_response(
|
|
||||||
props.clone(),
|
|
||||||
&auth.inner.user_id,
|
|
||||||
request.path(),
|
|
||||||
&context.prefix,
|
|
||||||
&calendar,
|
|
||||||
)
|
|
||||||
.map_err(|_e| Error::InternalError)?;
|
|
||||||
|
|
||||||
let output = generate_multistatus(
|
|
||||||
vec![Namespace::Dav, Namespace::CalDAV, Namespace::ICal],
|
|
||||||
|writer| {
|
|
||||||
writer.write_event(quick_xml::events::Event::Text(BytesText::from_escaped(
|
|
||||||
responses_string,
|
|
||||||
)))?;
|
|
||||||
Ok(())
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.map_err(|_e| Error::InternalError)?;
|
|
||||||
|
|
||||||
Ok(HttpResponse::MultiStatus()
|
|
||||||
.content_type(ContentType::xml())
|
|
||||||
.body(output))
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,2 @@
|
|||||||
pub mod calendar;
|
pub mod calendar;
|
||||||
pub mod event;
|
pub mod event;
|
||||||
pub mod principal;
|
|
||||||
pub mod root;
|
|
||||||
|
|||||||
@@ -1,146 +0,0 @@
|
|||||||
use crate::{
|
|
||||||
calendar::generate_propfind_calendar_response,
|
|
||||||
depth_extractor::Depth,
|
|
||||||
namespace::Namespace,
|
|
||||||
propfind::{
|
|
||||||
generate_multistatus, parse_propfind, write_invalid_props_response,
|
|
||||||
write_propstat_response, write_resourcetype,
|
|
||||||
},
|
|
||||||
CalDavContext, Error,
|
|
||||||
};
|
|
||||||
use actix_web::{
|
|
||||||
http::{header::ContentType, StatusCode},
|
|
||||||
web::Data,
|
|
||||||
HttpRequest, HttpResponse,
|
|
||||||
};
|
|
||||||
use anyhow::Result;
|
|
||||||
use quick_xml::{
|
|
||||||
events::{BytesText, Event},
|
|
||||||
Writer,
|
|
||||||
};
|
|
||||||
use rustical_auth::{AuthInfoExtractor, CheckAuthentication};
|
|
||||||
use rustical_store::calendar::CalendarStore;
|
|
||||||
|
|
||||||
// Executes the PROPFIND request and returns a XML string to be written into a <mulstistatus> object.
|
|
||||||
pub async fn generate_propfind_principal_response(
|
|
||||||
props: Vec<&str>,
|
|
||||||
principal: &str,
|
|
||||||
path: &str,
|
|
||||||
prefix: &str,
|
|
||||||
) -> Result<String, quick_xml::Error> {
|
|
||||||
let mut props = props;
|
|
||||||
if props.contains(&"allprops") {
|
|
||||||
props.extend(
|
|
||||||
[
|
|
||||||
"resourcetype",
|
|
||||||
"current-user-principal",
|
|
||||||
"principal-URL",
|
|
||||||
"calendar-home-set",
|
|
||||||
"calendar-user-address-set",
|
|
||||||
]
|
|
||||||
.iter(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut invalid_props = Vec::<&str>::new();
|
|
||||||
|
|
||||||
let mut output_buffer = Vec::new();
|
|
||||||
let mut writer = Writer::new_with_indent(&mut output_buffer, b' ', 2);
|
|
||||||
|
|
||||||
write_propstat_response(&mut writer, path, StatusCode::OK, |writer| {
|
|
||||||
let writer = writer;
|
|
||||||
for prop in props {
|
|
||||||
match prop {
|
|
||||||
"resourcetype" => write_resourcetype(writer, vec!["principal", "collection"])?,
|
|
||||||
"current-user-principal" | "principal-URL" => {
|
|
||||||
writer.create_element(prop).write_inner_content(|writer| {
|
|
||||||
writer
|
|
||||||
.create_element("href")
|
|
||||||
.write_text_content(BytesText::new(&format!(
|
|
||||||
"{prefix}/{principal}/",
|
|
||||||
)))?;
|
|
||||||
Ok(())
|
|
||||||
})?;
|
|
||||||
}
|
|
||||||
"calendar-home-set" | "calendar-user-address-set" => {
|
|
||||||
writer
|
|
||||||
.create_element(&format!("C:{prop}"))
|
|
||||||
.write_inner_content(|writer| {
|
|
||||||
writer
|
|
||||||
.create_element("href")
|
|
||||||
.write_text_content(BytesText::new(&format!(
|
|
||||||
"{prefix}/{principal}/"
|
|
||||||
)))?;
|
|
||||||
Ok(())
|
|
||||||
})?;
|
|
||||||
}
|
|
||||||
"allprops" => {}
|
|
||||||
_ => invalid_props.push(prop),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
})?;
|
|
||||||
|
|
||||||
dbg!(&invalid_props);
|
|
||||||
write_invalid_props_response(&mut writer, path, invalid_props)?;
|
|
||||||
Ok(std::str::from_utf8(&output_buffer)?.to_string())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn route_propfind_principal<A: CheckAuthentication, C: CalendarStore>(
|
|
||||||
body: String,
|
|
||||||
request: HttpRequest,
|
|
||||||
auth: AuthInfoExtractor<A>,
|
|
||||||
context: Data<CalDavContext<C>>,
|
|
||||||
depth: Depth,
|
|
||||||
) -> Result<HttpResponse, Error> {
|
|
||||||
let user = &auth.inner.user_id;
|
|
||||||
let props = parse_propfind(&body).map_err(|_e| Error::BadRequest)?;
|
|
||||||
|
|
||||||
let mut responses = Vec::new();
|
|
||||||
// also get calendars:
|
|
||||||
if depth != Depth::Zero {
|
|
||||||
let cals = context
|
|
||||||
.store
|
|
||||||
.read()
|
|
||||||
.await
|
|
||||||
.get_calendars(user)
|
|
||||||
.await
|
|
||||||
.map_err(|_e| Error::InternalError)?;
|
|
||||||
|
|
||||||
for cal in cals {
|
|
||||||
responses.push(
|
|
||||||
generate_propfind_calendar_response(
|
|
||||||
props.clone(),
|
|
||||||
&auth.inner.user_id,
|
|
||||||
&format!("{}/{}", request.path(), cal.id),
|
|
||||||
&context.prefix,
|
|
||||||
&cal,
|
|
||||||
)
|
|
||||||
.map_err(|_e| Error::InternalError)?,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
responses.push(
|
|
||||||
generate_propfind_principal_response(props.clone(), user, request.path(), &context.prefix)
|
|
||||||
.await
|
|
||||||
.map_err(|_e| Error::InternalError)?,
|
|
||||||
);
|
|
||||||
|
|
||||||
let output = generate_multistatus(
|
|
||||||
vec![Namespace::Dav, Namespace::CalDAV, Namespace::ICal],
|
|
||||||
|writer| {
|
|
||||||
for response in responses {
|
|
||||||
writer.write_event(Event::Text(BytesText::from_escaped(response)))?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.map_err(|_e| Error::InternalError)?;
|
|
||||||
|
|
||||||
println!("{}", &output);
|
|
||||||
|
|
||||||
Ok(HttpResponse::MultiStatus()
|
|
||||||
.content_type(ContentType::xml())
|
|
||||||
.body(output))
|
|
||||||
}
|
|
||||||
@@ -1,88 +0,0 @@
|
|||||||
use crate::{
|
|
||||||
namespace::Namespace,
|
|
||||||
propfind::{
|
|
||||||
generate_multistatus, parse_propfind, write_invalid_props_response,
|
|
||||||
write_propstat_response, write_resourcetype,
|
|
||||||
},
|
|
||||||
CalDavContext, Error,
|
|
||||||
};
|
|
||||||
use actix_web::{
|
|
||||||
http::{header::ContentType, StatusCode},
|
|
||||||
web::Data,
|
|
||||||
HttpRequest, HttpResponse,
|
|
||||||
};
|
|
||||||
use quick_xml::{
|
|
||||||
events::{BytesText, Event},
|
|
||||||
Writer,
|
|
||||||
};
|
|
||||||
use rustical_auth::{AuthInfoExtractor, CheckAuthentication};
|
|
||||||
use rustical_store::calendar::CalendarStore;
|
|
||||||
|
|
||||||
// Executes the PROPFIND request and returns a XML string to be written into a <mulstistatus> object.
|
|
||||||
pub async fn generate_propfind_root_response(
|
|
||||||
props: Vec<&str>,
|
|
||||||
principal: &str,
|
|
||||||
path: &str,
|
|
||||||
prefix: &str,
|
|
||||||
) -> Result<String, quick_xml::Error> {
|
|
||||||
let mut props = props;
|
|
||||||
if props.contains(&"allprops") {
|
|
||||||
props.extend(["resourcetype", "current-user-principal"].iter());
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut invalid_props = Vec::<&str>::new();
|
|
||||||
|
|
||||||
let mut output_buffer = Vec::new();
|
|
||||||
let mut writer = Writer::new_with_indent(&mut output_buffer, b' ', 2);
|
|
||||||
|
|
||||||
write_propstat_response(&mut writer, path, StatusCode::OK, |writer| {
|
|
||||||
for prop in props {
|
|
||||||
match prop {
|
|
||||||
"resourcetype" => write_resourcetype(writer, vec!["collection"])?,
|
|
||||||
"current-user-principal" => {
|
|
||||||
writer
|
|
||||||
.create_element("current-user-principal")
|
|
||||||
.write_inner_content(|writer| {
|
|
||||||
writer
|
|
||||||
.create_element("href")
|
|
||||||
.write_text_content(BytesText::new(&format!(
|
|
||||||
"{prefix}/{principal}"
|
|
||||||
)))?;
|
|
||||||
Ok(())
|
|
||||||
})?;
|
|
||||||
}
|
|
||||||
"allprops" => {}
|
|
||||||
_ => invalid_props.push(prop),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
})?;
|
|
||||||
dbg!(&invalid_props);
|
|
||||||
write_invalid_props_response(&mut writer, path, invalid_props)?;
|
|
||||||
Ok(std::str::from_utf8(&output_buffer)?.to_string())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn route_propfind_root<A: CheckAuthentication, C: CalendarStore>(
|
|
||||||
body: String,
|
|
||||||
request: HttpRequest,
|
|
||||||
context: Data<CalDavContext<C>>,
|
|
||||||
auth: AuthInfoExtractor<A>,
|
|
||||||
) -> Result<HttpResponse, Error> {
|
|
||||||
let principal = &auth.inner.user_id;
|
|
||||||
let props = parse_propfind(&body).map_err(|_e| Error::BadRequest)?;
|
|
||||||
|
|
||||||
let responses_string =
|
|
||||||
generate_propfind_root_response(props.clone(), principal, request.path(), &context.prefix)
|
|
||||||
.await
|
|
||||||
.map_err(|_e| Error::InternalError)?;
|
|
||||||
|
|
||||||
let output = generate_multistatus(vec![Namespace::Dav, Namespace::CalDAV], |writer| {
|
|
||||||
writer.write_event(Event::Text(BytesText::from_escaped(responses_string)))?;
|
|
||||||
Ok(())
|
|
||||||
})
|
|
||||||
.map_err(|_e| Error::InternalError)?;
|
|
||||||
|
|
||||||
Ok(HttpResponse::MultiStatus()
|
|
||||||
.content_type(ContentType::xml())
|
|
||||||
.body(output))
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user