mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-16 21:59:37 +00:00
Move route_propfind to its own file
This commit is contained in:
@@ -1,19 +1,14 @@
|
|||||||
use actix_web::http::header::ContentType;
|
|
||||||
use actix_web::http::Method;
|
use actix_web::http::Method;
|
||||||
use actix_web::web::{self, Data, Path};
|
use actix_web::web::{self, Data};
|
||||||
use actix_web::{guard, HttpRequest, HttpResponse, Responder};
|
use actix_web::{guard, HttpResponse, Responder};
|
||||||
use depth_extractor::Depth;
|
|
||||||
use error::Error;
|
use error::Error;
|
||||||
use namespace::Namespace;
|
|
||||||
use propfind::{generate_multistatus, parse_propfind};
|
|
||||||
use quick_xml::events::BytesText;
|
|
||||||
use resource::{HandlePropfind, Resource};
|
|
||||||
use resources::calendar::CalendarResource;
|
use resources::calendar::CalendarResource;
|
||||||
use resources::event::EventResource;
|
use resources::event::EventResource;
|
||||||
use resources::principal::PrincipalCalendarsResource;
|
use resources::principal::PrincipalCalendarsResource;
|
||||||
use resources::root::RootResource;
|
use resources::root::RootResource;
|
||||||
|
use routes::propfind::route_propfind;
|
||||||
use routes::{calendar, event};
|
use routes::{calendar, event};
|
||||||
use rustical_auth::{AuthInfoExtractor, CheckAuthentication};
|
use rustical_auth::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;
|
||||||
@@ -60,96 +55,30 @@ pub fn configure_dav<A: CheckAuthentication, C: CalendarStore>(
|
|||||||
.to(options_handler),
|
.to(options_handler),
|
||||||
)
|
)
|
||||||
.service(
|
.service(
|
||||||
web::resource("").route(web::method(propfind_method()).to(route_new_propfind::<
|
web::resource("").route(web::method(propfind_method()).to(route_propfind::<
|
||||||
A,
|
A,
|
||||||
RootResource,
|
RootResource,
|
||||||
C,
|
C,
|
||||||
>)),
|
>)),
|
||||||
)
|
)
|
||||||
.service(
|
.service(web::resource("/{principal}").route(
|
||||||
web::resource("/{principal}").route(
|
web::method(propfind_method()).to(route_propfind::<A, PrincipalCalendarsResource<C>, C>),
|
||||||
web::method(propfind_method()).to(route_new_propfind::<
|
))
|
||||||
A,
|
|
||||||
PrincipalCalendarsResource<C>,
|
|
||||||
C,
|
|
||||||
>),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.service(
|
.service(
|
||||||
web::resource("/{principal}/{calendar}")
|
web::resource("/{principal}/{calendar}")
|
||||||
.route(web::method(report_method()).to(calendar::route_report_calendar::<A, C>))
|
.route(web::method(report_method()).to(calendar::route_report_calendar::<A, C>))
|
||||||
.route(
|
.route(web::method(propfind_method()).to(route_propfind::<A, CalendarResource<C>, C>))
|
||||||
web::method(propfind_method()).to(route_new_propfind::<A, CalendarResource<C>, C>),
|
|
||||||
)
|
|
||||||
.route(web::method(mkcol_method()).to(calendar::route_mkcol_calendar::<A, C>)),
|
.route(web::method(mkcol_method()).to(calendar::route_mkcol_calendar::<A, C>)),
|
||||||
)
|
)
|
||||||
.service(
|
.service(
|
||||||
web::resource("/{principal}/{calendar}/{event}")
|
web::resource("/{principal}/{calendar}/{event}")
|
||||||
.route(web::method(propfind_method()).to(route_new_propfind::<A, EventResource, C>))
|
.route(web::method(propfind_method()).to(route_propfind::<A, EventResource<C>, C>))
|
||||||
.route(web::method(propfind_method()).to(route_new_propfind::<A, EventResource<C>, C>))
|
|
||||||
.route(web::method(Method::DELETE).to(event::delete_event::<A, 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::GET).to(event::get_event::<A, C>))
|
||||||
.route(web::method(Method::PUT).to(event::put_event::<A, C>)),
|
.route(web::method(Method::PUT).to(event::put_event::<A, C>)),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn route_new_propfind<A: CheckAuthentication, R: Resource, C: CalendarStore>(
|
|
||||||
path: Path<R::UriComponents>,
|
|
||||||
body: String,
|
|
||||||
req: HttpRequest,
|
|
||||||
context: Data<CalDavContext<C>>,
|
|
||||||
auth: AuthInfoExtractor<A>,
|
|
||||||
depth: Depth,
|
|
||||||
) -> Result<HttpResponse, Error> {
|
|
||||||
let props = parse_propfind(&body).map_err(|_e| Error::BadRequest)?;
|
|
||||||
let req_path = req.path().to_string();
|
|
||||||
let auth_info = auth.inner;
|
|
||||||
|
|
||||||
let resource = R::acquire_from_request(
|
|
||||||
req,
|
|
||||||
auth_info,
|
|
||||||
path.into_inner(),
|
|
||||||
context.prefix.to_string(),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.map_err(|_e| Error::InternalError)?;
|
|
||||||
|
|
||||||
let mut responses = vec![resource
|
|
||||||
.propfind(props.clone())
|
|
||||||
.map_err(|_e| Error::InternalError)?];
|
|
||||||
|
|
||||||
if depth != Depth::Zero {
|
|
||||||
for member in resource
|
|
||||||
.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((
|
||||||
|
|||||||
@@ -1,41 +1,12 @@
|
|||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
use actix_web::http::StatusCode;
|
use actix_web::http::StatusCode;
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::Result;
|
||||||
use quick_xml::{
|
use quick_xml::{
|
||||||
events::{attributes::Attribute, BytesText},
|
events::{attributes::Attribute, BytesText},
|
||||||
Writer,
|
Writer,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn parse_propfind(body: &str) -> Result<Vec<&str>> {
|
|
||||||
if body.is_empty() {
|
|
||||||
// if body is empty, allprops must be returned (RFC 4918)
|
|
||||||
return Ok(vec!["allprops"]);
|
|
||||||
}
|
|
||||||
let doc = roxmltree::Document::parse(body)?;
|
|
||||||
|
|
||||||
let propfind_node = doc.root_element();
|
|
||||||
if propfind_node.tag_name().name() != "propfind" {
|
|
||||||
return Err(anyhow!("invalid tag"));
|
|
||||||
}
|
|
||||||
|
|
||||||
let prop_node = if let Some(el) = propfind_node.first_element_child() {
|
|
||||||
el
|
|
||||||
} else {
|
|
||||||
return Ok(Vec::new());
|
|
||||||
};
|
|
||||||
|
|
||||||
let props = match prop_node.tag_name().name() {
|
|
||||||
"prop" => Ok(prop_node
|
|
||||||
.children()
|
|
||||||
.map(|node| node.tag_name().name())
|
|
||||||
.collect()),
|
|
||||||
_ => Err(anyhow!("invalid prop tag")),
|
|
||||||
};
|
|
||||||
dbg!(body, &props);
|
|
||||||
props
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write_resourcetype<W: Write>(
|
pub fn write_resourcetype<W: Write>(
|
||||||
writer: &mut Writer<W>,
|
writer: &mut Writer<W>,
|
||||||
types: Vec<&str>,
|
types: Vec<&str>,
|
||||||
|
|||||||
@@ -1,2 +1,3 @@
|
|||||||
pub mod calendar;
|
pub mod calendar;
|
||||||
pub mod event;
|
pub mod event;
|
||||||
|
pub mod propfind;
|
||||||
|
|||||||
99
crates/dav/src/routes/propfind.rs
Normal file
99
crates/dav/src/routes/propfind.rs
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
use crate::depth_extractor::Depth;
|
||||||
|
use crate::error::Error;
|
||||||
|
use crate::namespace::Namespace;
|
||||||
|
use crate::propfind::generate_multistatus;
|
||||||
|
use crate::resource::{HandlePropfind, Resource};
|
||||||
|
use crate::CalDavContext;
|
||||||
|
use actix_web::http::header::ContentType;
|
||||||
|
use actix_web::web::{Data, Path};
|
||||||
|
use actix_web::{HttpRequest, HttpResponse};
|
||||||
|
use anyhow::{anyhow, Result};
|
||||||
|
use quick_xml::events::BytesText;
|
||||||
|
use rustical_auth::{AuthInfoExtractor, CheckAuthentication};
|
||||||
|
use rustical_store::calendar::CalendarStore;
|
||||||
|
|
||||||
|
fn parse_propfind(body: &str) -> Result<Vec<&str>> {
|
||||||
|
if body.is_empty() {
|
||||||
|
// if body is empty, allprops must be returned (RFC 4918)
|
||||||
|
return Ok(vec!["allprops"]);
|
||||||
|
}
|
||||||
|
let doc = roxmltree::Document::parse(body)?;
|
||||||
|
|
||||||
|
let propfind_node = doc.root_element();
|
||||||
|
if propfind_node.tag_name().name() != "propfind" {
|
||||||
|
return Err(anyhow!("invalid tag"));
|
||||||
|
}
|
||||||
|
|
||||||
|
let prop_node = if let Some(el) = propfind_node.first_element_child() {
|
||||||
|
el
|
||||||
|
} else {
|
||||||
|
return Ok(Vec::new());
|
||||||
|
};
|
||||||
|
|
||||||
|
let props = match prop_node.tag_name().name() {
|
||||||
|
"prop" => Ok(prop_node
|
||||||
|
.children()
|
||||||
|
.map(|node| node.tag_name().name())
|
||||||
|
.collect()),
|
||||||
|
_ => Err(anyhow!("invalid prop tag")),
|
||||||
|
};
|
||||||
|
dbg!(body, &props);
|
||||||
|
props
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn route_propfind<A: CheckAuthentication, R: Resource, C: CalendarStore>(
|
||||||
|
path: Path<R::UriComponents>,
|
||||||
|
body: String,
|
||||||
|
req: HttpRequest,
|
||||||
|
context: Data<CalDavContext<C>>,
|
||||||
|
auth: AuthInfoExtractor<A>,
|
||||||
|
depth: Depth,
|
||||||
|
) -> Result<HttpResponse, Error> {
|
||||||
|
let props = parse_propfind(&body).map_err(|_e| Error::BadRequest)?;
|
||||||
|
// let req_path = req.path().to_string();
|
||||||
|
let auth_info = auth.inner;
|
||||||
|
|
||||||
|
let resource = R::acquire_from_request(
|
||||||
|
req,
|
||||||
|
auth_info,
|
||||||
|
path.into_inner(),
|
||||||
|
context.prefix.to_string(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.map_err(|_e| Error::InternalError)?;
|
||||||
|
|
||||||
|
let mut responses = vec![resource
|
||||||
|
.propfind(props.clone())
|
||||||
|
.map_err(|_e| Error::InternalError)?];
|
||||||
|
|
||||||
|
if depth != Depth::Zero {
|
||||||
|
for member in resource
|
||||||
|
.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))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user