Implement If-Match, If-None-Match for DELETE method

This commit is contained in:
Lennart
2025-02-06 15:17:49 +01:00
parent 6caa04a516
commit 180295ef1a
6 changed files with 61 additions and 8 deletions

View File

@@ -60,9 +60,6 @@ pub async fn put_event<C: CalendarStore>(
return Ok(HttpResponse::Unauthorized().body("")); return Ok(HttpResponse::Unauthorized().body(""));
} }
// TODO: implement If-Match
//
let overwrite = let overwrite =
Some(&HeaderValue::from_static("*")) != req.headers().get(header::IF_NONE_MATCH); Some(&HeaderValue::from_static("*")) != req.headers().get(header::IF_NONE_MATCH);

View File

@@ -90,6 +90,10 @@ impl Resource for CalendarObjectResource {
Some(&self.principal) Some(&self.principal)
} }
fn get_etag(&self) -> Option<String> {
Some(self.object.get_etag())
}
fn get_user_privileges(&self, user: &User) -> Result<UserPrivilegeSet, Self::Error> { fn get_user_privileges(&self, user: &User) -> Result<UserPrivilegeSet, Self::Error> {
Ok(UserPrivilegeSet::owner_only( Ok(UserPrivilegeSet::owner_only(
user.is_principal(&self.principal), user.is_principal(&self.principal),

View File

@@ -68,9 +68,6 @@ pub async fn put_object<AS: AddressbookStore>(
return Err(Error::Unauthorized); return Err(Error::Unauthorized);
} }
// TODO: implement If-Match
//
let overwrite = let overwrite =
Some(&HeaderValue::from_static("*")) != req.headers().get(header::IF_NONE_MATCH); Some(&HeaderValue::from_static("*")) != req.headers().get(header::IF_NONE_MATCH);

View File

@@ -86,6 +86,10 @@ impl Resource for AddressObjectResource {
Some(&self.principal) Some(&self.principal)
} }
fn get_etag(&self) -> Option<String> {
Some(self.object.get_etag())
}
fn get_user_privileges(&self, user: &User) -> Result<UserPrivilegeSet, Self::Error> { fn get_user_privileges(&self, user: &User) -> Result<UserPrivilegeSet, Self::Error> {
Ok(UserPrivilegeSet::owner_only( Ok(UserPrivilegeSet::owner_only(
user.is_principal(&self.principal), user.is_principal(&self.principal),

View File

@@ -2,6 +2,9 @@ use crate::privileges::UserPrivilege;
use crate::resource::Resource; use crate::resource::Resource;
use crate::resource::ResourceService; use crate::resource::ResourceService;
use crate::Error; use crate::Error;
use actix_web::http::header::IfMatch;
use actix_web::http::header::IfNoneMatch;
use actix_web::web;
use actix_web::web::Data; use actix_web::web::Data;
use actix_web::web::Path; use actix_web::web::Path;
use actix_web::HttpRequest; use actix_web::HttpRequest;
@@ -18,6 +21,8 @@ pub async fn route_delete<R: ResourceService>(
user: User, user: User,
resource_service: Data<R>, resource_service: Data<R>,
root_span: RootSpan, root_span: RootSpan,
if_match: web::Header<IfMatch>,
if_none_match: web::Header<IfNoneMatch>,
) -> Result<impl Responder, R::Error> { ) -> Result<impl Responder, R::Error> {
let no_trash = req let no_trash = req
.headers() .headers()
@@ -26,12 +31,21 @@ pub async fn route_delete<R: ResourceService>(
.unwrap_or(false); .unwrap_or(false);
let resource = resource_service.get_resource(&path).await?; let resource = resource_service.get_resource(&path).await?;
let privileges = resource.get_user_privileges(&user)?; let privileges = resource.get_user_privileges(&user)?;
if !privileges.has(&UserPrivilege::Write) { if !privileges.has(&UserPrivilege::Write) {
// TODO: Actually the spec wants us to look whether we have unbind access in the parent
// collection
return Err(Error::Unauthorized.into()); return Err(Error::Unauthorized.into());
} }
if !resource.satisfies_if_match(&if_match) {
// Precondition failed
return Ok(HttpResponse::PreconditionFailed().finish());
}
if resource.satisfies_if_none_match(&if_none_match) {
// Precondition failed
return Ok(HttpResponse::PreconditionFailed().finish());
}
resource_service.delete_resource(&path, !no_trash).await?; resource_service.delete_resource(&path, !no_trash).await?;
Ok(HttpResponse::Ok().body("")) Ok(HttpResponse::Ok().body(""))

View File

@@ -4,6 +4,7 @@ use crate::xml::Resourcetype;
use crate::xml::{multistatus::ResponseElement, TagList}; use crate::xml::{multistatus::ResponseElement, TagList};
use crate::Error; use crate::Error;
use actix_web::dev::ResourceMap; use actix_web::dev::ResourceMap;
use actix_web::http::header::{EntityTag, IfMatch, IfNoneMatch};
use actix_web::{http::StatusCode, ResponseError}; use actix_web::{http::StatusCode, ResponseError};
use itertools::Itertools; use itertools::Itertools;
use quick_xml::name::Namespace; use quick_xml::name::Namespace;
@@ -56,6 +57,42 @@ pub trait Resource: Clone + 'static {
None None
} }
fn get_etag(&self) -> Option<String> {
None
}
fn satisfies_if_match(&self, if_match: &IfMatch) -> bool {
match if_match {
IfMatch::Any => true,
// This is not nice but if the header doesn't exist, actix just gives us an empty
// IfMatch::Items header
IfMatch::Items(items) if items.is_empty() => true,
IfMatch::Items(items) => {
if let Some(etag) = self.get_etag() {
let etag = EntityTag::new_strong(etag.to_owned());
return items.iter().any(|item| item.strong_eq(&etag));
}
false
}
}
}
fn satisfies_if_none_match(&self, if_none_match: &IfNoneMatch) -> bool {
match if_none_match {
IfNoneMatch::Any => false,
// This is not nice but if the header doesn't exist, actix just gives us an empty
// IfNoneMatch::Items header
IfNoneMatch::Items(items) if items.is_empty() => false,
IfNoneMatch::Items(items) => {
if let Some(etag) = self.get_etag() {
let etag = EntityTag::new_strong(etag.to_owned());
return items.iter().all(|item| item.strong_ne(&etag));
}
true
}
}
}
fn get_user_privileges(&self, user: &User) -> Result<UserPrivilegeSet, Self::Error>; fn get_user_privileges(&self, user: &User) -> Result<UserPrivilegeSet, Self::Error>;
fn propfind( fn propfind(