mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 22:52:22 +00:00
Add explicit error type to propfind resources
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
use crate::Error;
|
||||
use actix_web::{web::Data, HttpRequest};
|
||||
use anyhow::{anyhow, Result};
|
||||
use anyhow::anyhow;
|
||||
use async_trait::async_trait;
|
||||
use rustical_auth::AuthInfo;
|
||||
use rustical_dav::error::Error;
|
||||
use rustical_dav::resource::{Resource, ResourceService};
|
||||
use rustical_dav::xml_snippets::{HrefElement, TextNode};
|
||||
use rustical_store::calendar::Calendar;
|
||||
@@ -166,8 +166,13 @@ pub struct CalendarFile {
|
||||
impl Resource for CalendarFile {
|
||||
type PropType = CalendarProp;
|
||||
type PropResponse = CalendarPropResponse;
|
||||
type Error = Error;
|
||||
|
||||
fn get_prop(&self, prefix: &str, prop: Self::PropType) -> Result<Self::PropResponse> {
|
||||
fn get_prop(
|
||||
&self,
|
||||
prefix: &str,
|
||||
prop: Self::PropType,
|
||||
) -> Result<Self::PropResponse, Self::Error> {
|
||||
match prop {
|
||||
CalendarProp::Resourcetype => {
|
||||
Ok(CalendarPropResponse::Resourcetype(Resourcetype::default()))
|
||||
@@ -220,8 +225,9 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarResource<C> {
|
||||
type MemberType = CalendarFile;
|
||||
type PathComponents = (String, String); // principal, calendar_id
|
||||
type File = CalendarFile;
|
||||
type Error = Error;
|
||||
|
||||
async fn get_file(&self) -> Result<Self::File> {
|
||||
async fn get_file(&self) -> Result<Self::File, Error> {
|
||||
let calendar = self
|
||||
.cal_store
|
||||
.read()
|
||||
@@ -236,7 +242,10 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarResource<C> {
|
||||
})
|
||||
}
|
||||
|
||||
async fn get_members(&self, _auth_info: AuthInfo) -> Result<Vec<Self::MemberType>> {
|
||||
async fn get_members(
|
||||
&self,
|
||||
_auth_info: AuthInfo,
|
||||
) -> Result<Vec<Self::MemberType>, Self::Error> {
|
||||
// As of now the calendar resource has no members since events are shown with REPORT
|
||||
Ok(vec![])
|
||||
}
|
||||
@@ -245,7 +254,7 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarResource<C> {
|
||||
req: HttpRequest,
|
||||
auth_info: AuthInfo,
|
||||
path_components: Self::PathComponents,
|
||||
) -> Result<Self, rustical_dav::error::Error> {
|
||||
) -> Result<Self, Self::Error> {
|
||||
let cal_store = req
|
||||
.app_data::<Data<RwLock<C>>>()
|
||||
.ok_or(anyhow!("no calendar store in app_data!"))?
|
||||
|
||||
@@ -5,6 +5,9 @@ pub enum Error {
|
||||
#[error("Unauthorized")]
|
||||
Unauthorized,
|
||||
|
||||
#[error("Not Found")]
|
||||
NotFound,
|
||||
|
||||
#[error("Not implemented")]
|
||||
NotImplemented,
|
||||
|
||||
@@ -33,6 +36,7 @@ impl actix_web::ResponseError for Error {
|
||||
Error::XmlDecodeError(_) => StatusCode::BAD_REQUEST,
|
||||
Error::NotImplemented => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Error::Other(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Error::NotFound => StatusCode::NOT_FOUND,
|
||||
}
|
||||
}
|
||||
fn error_response(&self) -> actix_web::HttpResponse<actix_web::body::BoxBody> {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use crate::Error;
|
||||
use actix_web::{web::Data, HttpRequest};
|
||||
use anyhow::{anyhow, Result};
|
||||
use anyhow::anyhow;
|
||||
use async_trait::async_trait;
|
||||
use rustical_auth::AuthInfo;
|
||||
use rustical_dav::error::Error;
|
||||
use rustical_dav::resource::{Resource, ResourceService};
|
||||
use rustical_dav::xml_snippets::TextNode;
|
||||
use rustical_store::event::Event;
|
||||
@@ -43,12 +43,17 @@ pub struct EventFile {
|
||||
impl Resource for EventFile {
|
||||
type PropType = EventProp;
|
||||
type PropResponse = PrincipalPropResponse;
|
||||
type Error = Error;
|
||||
|
||||
fn get_path(&self) -> &str {
|
||||
&self.path
|
||||
}
|
||||
|
||||
fn get_prop(&self, _prefix: &str, prop: Self::PropType) -> Result<Self::PropResponse> {
|
||||
fn get_prop(
|
||||
&self,
|
||||
_prefix: &str,
|
||||
prop: Self::PropType,
|
||||
) -> Result<Self::PropResponse, Self::Error> {
|
||||
match prop {
|
||||
EventProp::Getetag => Ok(PrincipalPropResponse::Getetag(TextNode(Some(
|
||||
self.event.get_etag(),
|
||||
@@ -68,8 +73,12 @@ impl<C: CalendarStore + ?Sized> ResourceService for EventResource<C> {
|
||||
type PathComponents = (String, String, String); // principal, calendar, event
|
||||
type File = EventFile;
|
||||
type MemberType = EventFile;
|
||||
type Error = Error;
|
||||
|
||||
async fn get_members(&self, _auth_info: AuthInfo) -> Result<Vec<Self::MemberType>> {
|
||||
async fn get_members(
|
||||
&self,
|
||||
_auth_info: AuthInfo,
|
||||
) -> Result<Vec<Self::MemberType>, Self::Error> {
|
||||
Ok(vec![])
|
||||
}
|
||||
|
||||
@@ -77,7 +86,7 @@ impl<C: CalendarStore + ?Sized> ResourceService for EventResource<C> {
|
||||
req: HttpRequest,
|
||||
_auth_info: AuthInfo,
|
||||
path_components: Self::PathComponents,
|
||||
) -> Result<Self, Error> {
|
||||
) -> Result<Self, Self::Error> {
|
||||
let (_principal, cid, uid) = path_components;
|
||||
|
||||
let cal_store = req
|
||||
@@ -94,7 +103,7 @@ impl<C: CalendarStore + ?Sized> ResourceService for EventResource<C> {
|
||||
})
|
||||
}
|
||||
|
||||
async fn get_file(&self) -> Result<Self::File> {
|
||||
async fn get_file(&self) -> Result<Self::File, Self::Error> {
|
||||
let event = self
|
||||
.cal_store
|
||||
.read()
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::Error;
|
||||
use actix_web::web::Data;
|
||||
use actix_web::HttpRequest;
|
||||
use anyhow::{anyhow, Result};
|
||||
use anyhow::anyhow;
|
||||
use async_trait::async_trait;
|
||||
use rustical_auth::AuthInfo;
|
||||
use rustical_dav::resource::{Resource, ResourceService};
|
||||
use rustical_dav::xml_snippets::HrefElement;
|
||||
use rustical_store::CalendarStore;
|
||||
use serde::Serialize;
|
||||
use std::sync::Arc;
|
||||
use strum::{EnumString, IntoStaticStr, VariantNames};
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
@@ -60,8 +60,13 @@ pub enum PrincipalProp {
|
||||
impl Resource for PrincipalFile {
|
||||
type PropType = PrincipalProp;
|
||||
type PropResponse = PrincipalPropResponse;
|
||||
type Error = Error;
|
||||
|
||||
fn get_prop(&self, prefix: &str, prop: Self::PropType) -> Result<Self::PropResponse> {
|
||||
fn get_prop(
|
||||
&self,
|
||||
prefix: &str,
|
||||
prop: Self::PropType,
|
||||
) -> Result<Self::PropResponse, Self::Error> {
|
||||
match prop {
|
||||
PrincipalProp::Resourcetype => {
|
||||
Ok(PrincipalPropResponse::Resourcetype(Resourcetype::default()))
|
||||
@@ -93,14 +98,15 @@ impl<C: CalendarStore + ?Sized> ResourceService for PrincipalResource<C> {
|
||||
type PathComponents = (String,);
|
||||
type MemberType = CalendarFile;
|
||||
type File = PrincipalFile;
|
||||
type Error = Error;
|
||||
|
||||
async fn new(
|
||||
req: HttpRequest,
|
||||
auth_info: AuthInfo,
|
||||
(principal,): Self::PathComponents,
|
||||
) -> Result<Self, rustical_dav::error::Error> {
|
||||
) -> Result<Self, Self::Error> {
|
||||
if auth_info.user_id != principal {
|
||||
return Err(rustical_dav::error::Error::Unauthorized);
|
||||
return Err(Error::Unauthorized);
|
||||
}
|
||||
let cal_store = req
|
||||
.app_data::<Data<RwLock<C>>>()
|
||||
@@ -115,14 +121,17 @@ impl<C: CalendarStore + ?Sized> ResourceService for PrincipalResource<C> {
|
||||
})
|
||||
}
|
||||
|
||||
async fn get_file(&self) -> Result<Self::File> {
|
||||
async fn get_file(&self) -> Result<Self::File, Self::Error> {
|
||||
Ok(PrincipalFile {
|
||||
principal: self.principal.to_owned(),
|
||||
path: self.path.to_owned(),
|
||||
})
|
||||
}
|
||||
|
||||
async fn get_members(&self, _auth_info: AuthInfo) -> Result<Vec<Self::MemberType>> {
|
||||
async fn get_members(
|
||||
&self,
|
||||
_auth_info: AuthInfo,
|
||||
) -> Result<Vec<Self::MemberType>, Self::Error> {
|
||||
let calendars = self
|
||||
.cal_store
|
||||
.read()
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use crate::Error;
|
||||
use actix_web::HttpRequest;
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use rustical_auth::AuthInfo;
|
||||
use rustical_dav::error::Error;
|
||||
use rustical_dav::resource::{Resource, ResourceService};
|
||||
use rustical_dav::xml_snippets::HrefElement;
|
||||
use serde::Serialize;
|
||||
@@ -41,8 +41,13 @@ pub struct RootFile {
|
||||
impl Resource for RootFile {
|
||||
type PropType = RootProp;
|
||||
type PropResponse = RootPropResponse;
|
||||
type Error = Error;
|
||||
|
||||
fn get_prop(&self, prefix: &str, prop: Self::PropType) -> Result<Self::PropResponse> {
|
||||
fn get_prop(
|
||||
&self,
|
||||
prefix: &str,
|
||||
prop: Self::PropType,
|
||||
) -> Result<Self::PropResponse, Self::Error> {
|
||||
match prop {
|
||||
RootProp::Resourcetype => Ok(RootPropResponse::Resourcetype(Resourcetype::default())),
|
||||
RootProp::CurrentUserPrincipal => Ok(RootPropResponse::CurrentUserPrincipal(
|
||||
@@ -61,8 +66,12 @@ impl ResourceService for RootResource {
|
||||
type PathComponents = ();
|
||||
type MemberType = RootFile;
|
||||
type File = RootFile;
|
||||
type Error = Error;
|
||||
|
||||
async fn get_members(&self, _auth_info: AuthInfo) -> Result<Vec<Self::MemberType>> {
|
||||
async fn get_members(
|
||||
&self,
|
||||
_auth_info: AuthInfo,
|
||||
) -> Result<Vec<Self::MemberType>, Self::Error> {
|
||||
Ok(vec![])
|
||||
}
|
||||
|
||||
@@ -70,14 +79,14 @@ impl ResourceService for RootResource {
|
||||
req: HttpRequest,
|
||||
auth_info: AuthInfo,
|
||||
_path_components: Self::PathComponents,
|
||||
) -> Result<Self, Error> {
|
||||
) -> Result<Self, Self::Error> {
|
||||
Ok(Self {
|
||||
principal: auth_info.user_id,
|
||||
path: req.path().to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
async fn get_file(&self) -> Result<Self::File> {
|
||||
async fn get_file(&self) -> Result<Self::File, Self::Error> {
|
||||
Ok(RootFile {
|
||||
path: self.path.to_owned(),
|
||||
principal: self.principal.to_owned(),
|
||||
|
||||
@@ -13,6 +13,8 @@ pub enum Error {
|
||||
Unauthorized,
|
||||
#[error("Internal server error :(")]
|
||||
InternalError,
|
||||
#[error(transparent)]
|
||||
XmlDecodeError(#[from] quick_xml::DeError),
|
||||
#[error("Internal server error")]
|
||||
Other(#[from] anyhow::Error),
|
||||
}
|
||||
@@ -25,6 +27,7 @@ impl actix_web::error::ResponseError for Error {
|
||||
Self::NotFound => StatusCode::NOT_FOUND,
|
||||
Self::BadRequest => StatusCode::BAD_REQUEST,
|
||||
Self::Unauthorized => StatusCode::UNAUTHORIZED,
|
||||
Self::XmlDecodeError(_) => StatusCode::BAD_REQUEST,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ use crate::namespace::Namespace;
|
||||
use crate::resource::HandlePropfind;
|
||||
use crate::resource::ResourceService;
|
||||
use crate::xml::tag_list::TagList;
|
||||
use crate::Error;
|
||||
use actix_web::http::header::ContentType;
|
||||
use actix_web::web::{Data, Path};
|
||||
use actix_web::{HttpRequest, HttpResponse};
|
||||
@@ -58,21 +59,22 @@ pub async fn route_propfind<A: CheckAuthentication, R: ResourceService + ?Sized>
|
||||
prefix: Data<ServicePrefix>,
|
||||
auth: AuthInfoExtractor<A>,
|
||||
depth: Depth,
|
||||
) -> Result<HttpResponse, crate::error::Error> {
|
||||
) -> Result<HttpResponse, R::Error> {
|
||||
let auth_info = auth.inner;
|
||||
let prefix = prefix.0.to_owned();
|
||||
let path_components = path.into_inner();
|
||||
|
||||
let resource_service = R::new(req, auth_info.clone(), path_components.clone()).await?;
|
||||
|
||||
let propfind: PropfindElement = quick_xml::de::from_str(&body).unwrap();
|
||||
let propfind: PropfindElement =
|
||||
quick_xml::de::from_str(&body).map_err(Error::XmlDecodeError)?;
|
||||
let props = match propfind.prop {
|
||||
PropfindType::Allprop => {
|
||||
vec!["allprop".to_owned()]
|
||||
}
|
||||
PropfindType::Propname => {
|
||||
// TODO: Implement
|
||||
return Err(crate::error::Error::InternalError);
|
||||
return Err(Error::InternalError.into());
|
||||
}
|
||||
PropfindType::Prop(PropElement { prop: prop_tags }) => prop_tags.into(),
|
||||
};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::{error::Error, xml::tag_list::TagList};
|
||||
use actix_web::{http::StatusCode, HttpRequest};
|
||||
use anyhow::{anyhow, Result};
|
||||
use crate::xml::tag_list::TagList;
|
||||
use actix_web::{http::StatusCode, HttpRequest, ResponseError};
|
||||
use anyhow::anyhow;
|
||||
use async_trait::async_trait;
|
||||
use itertools::Itertools;
|
||||
use rustical_auth::AuthInfo;
|
||||
@@ -12,12 +12,17 @@ use strum::VariantNames;
|
||||
pub trait Resource {
|
||||
type PropType: FromStr + VariantNames + Clone;
|
||||
type PropResponse: Serialize;
|
||||
type Error: ResponseError + From<crate::Error> + From<anyhow::Error>;
|
||||
|
||||
fn list_dead_props() -> &'static [&'static str] {
|
||||
Self::PropType::VARIANTS
|
||||
}
|
||||
|
||||
fn get_prop(&self, prefix: &str, prop: Self::PropType) -> Result<Self::PropResponse>;
|
||||
fn get_prop(
|
||||
&self,
|
||||
prefix: &str,
|
||||
prop: Self::PropType,
|
||||
) -> Result<Self::PropResponse, Self::Error>;
|
||||
|
||||
fn get_path(&self) -> &str;
|
||||
}
|
||||
@@ -28,19 +33,20 @@ pub trait Resource {
|
||||
// A resource exists
|
||||
#[async_trait(?Send)]
|
||||
pub trait ResourceService: Sized {
|
||||
type MemberType: Resource;
|
||||
type MemberType: Resource<Error = Self::Error>;
|
||||
type PathComponents: Sized + Clone; // defines how the resource URI maps to parameters, i.e. /{principal}/{calendar} -> (String, String)
|
||||
type File: Resource;
|
||||
type File: Resource<Error = Self::Error>;
|
||||
type Error: ResponseError + From<crate::Error> + From<anyhow::Error>;
|
||||
|
||||
async fn new(
|
||||
req: HttpRequest,
|
||||
auth_info: AuthInfo,
|
||||
path_components: Self::PathComponents,
|
||||
) -> Result<Self, Error>;
|
||||
) -> Result<Self, Self::Error>;
|
||||
|
||||
async fn get_file(&self) -> Result<Self::File>;
|
||||
async fn get_file(&self) -> Result<Self::File, Self::Error>;
|
||||
|
||||
async fn get_members(&self, auth_info: AuthInfo) -> Result<Vec<Self::MemberType>>;
|
||||
async fn get_members(&self, auth_info: AuthInfo) -> Result<Vec<Self::MemberType>, Self::Error>;
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
@@ -72,21 +78,26 @@ enum PropstatType<T1: Serialize, T2: Serialize> {
|
||||
|
||||
#[async_trait(?Send)]
|
||||
pub trait HandlePropfind {
|
||||
async fn propfind(&self, prefix: &str, props: Vec<&str>) -> Result<impl Serialize>;
|
||||
type Error: ResponseError + From<crate::Error> + From<anyhow::Error>;
|
||||
|
||||
async fn propfind(&self, prefix: &str, props: Vec<&str>)
|
||||
-> Result<impl Serialize, Self::Error>;
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl<R: Resource> HandlePropfind for R {
|
||||
type Error = R::Error;
|
||||
|
||||
async fn propfind(
|
||||
&self,
|
||||
prefix: &str,
|
||||
props: Vec<&str>,
|
||||
) -> Result<PropstatResponseElement<PropWrapper<Vec<R::PropResponse>>, TagList>> {
|
||||
) -> Result<PropstatResponseElement<PropWrapper<Vec<R::PropResponse>>, TagList>, R::Error> {
|
||||
let mut props = props;
|
||||
if props.contains(&"propname") {
|
||||
if props.len() != 1 {
|
||||
// propname MUST be the only queried prop per spec
|
||||
return Err(anyhow!("propname MUST be the only queried prop"));
|
||||
return Err(anyhow!("propname MUST be the only queried prop").into());
|
||||
}
|
||||
// TODO: implement propname
|
||||
props = R::list_dead_props().into();
|
||||
@@ -94,7 +105,7 @@ impl<R: Resource> HandlePropfind for R {
|
||||
if props.contains(&"allprop") {
|
||||
if props.len() != 1 {
|
||||
// allprop MUST be the only queried prop per spec
|
||||
return Err(anyhow!("allprop MUST be the only queried prop"));
|
||||
return Err(anyhow!("allprop MUST be the only queried prop").into());
|
||||
}
|
||||
props = R::list_dead_props().into();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user