Work on axum support

This commit is contained in:
Lennart
2025-06-07 20:17:50 +02:00
parent 57832116aa
commit 790c657b08
38 changed files with 582 additions and 64 deletions

View File

@@ -2,9 +2,17 @@ use crate::Error;
use crate::privileges::UserPrivilege;
use crate::resource::Resource;
use crate::resource::ResourceService;
#[cfg(feature = "axum")]
use axum::extract::{Extension, Path, State};
#[cfg(feature = "axum")]
use axum_extra::TypedHeader;
use headers::Header;
use headers::{HeaderValue, IfMatch, IfNoneMatch};
#[cfg(feature = "axum")]
use http::HeaderMap;
use itertools::Itertools;
#[cfg(feature = "axum")]
use std::sync::Arc;
use tracing::instrument;
#[cfg(feature = "actix")]
@@ -27,12 +35,12 @@ pub async fn actix_route_delete<R: ResourceService>(
// while actix-web still uses http==0.2
let if_match = req
.headers()
.get_all(http::header::IF_MATCH)
.get_all(http_02::header::IF_MATCH)
.map(|val_02| HeaderValue::from_bytes(val_02.as_bytes()).unwrap())
.collect_vec();
let if_none_match = req
.headers()
.get_all(http::header::IF_NONE_MATCH)
.get_all(http_02::header::IF_NONE_MATCH)
.map(|val_02| HeaderValue::from_bytes(val_02.as_bytes()).unwrap())
.collect_vec();
@@ -49,7 +57,7 @@ pub async fn actix_route_delete<R: ResourceService>(
route_delete(
&path.into_inner(),
principal,
&principal,
resource_service.as_ref(),
no_trash,
if_match,
@@ -60,9 +68,33 @@ pub async fn actix_route_delete<R: ResourceService>(
Ok(actix_web::HttpResponse::Ok().body(""))
}
#[cfg(feature = "axum")]
pub(crate) async fn axum_route_delete<R: ResourceService>(
Path(path): Path<R::PathComponents>,
State(resource_service): State<Arc<R>>,
Extension(principal): Extension<R::Principal>,
if_match: Option<TypedHeader<IfMatch>>,
if_none_match: Option<TypedHeader<IfNoneMatch>>,
header_map: HeaderMap,
) -> Result<(), R::Error> {
let no_trash = header_map
.get("X-No-Trashbin")
.map(|val| matches!(val.to_str(), Ok("1")))
.unwrap_or(false);
route_delete(
&path,
&principal,
resource_service.as_ref(),
no_trash,
if_match.map(|hdr| hdr.0),
if_none_match.map(|hdr| hdr.0),
)
.await
}
pub async fn route_delete<R: ResourceService>(
path_components: &R::PathComponents,
principal: R::Principal,
principal: &R::Principal,
resource_service: &R,
no_trash: bool,
if_match: Option<IfMatch>,
@@ -70,7 +102,7 @@ pub async fn route_delete<R: ResourceService>(
) -> Result<(), R::Error> {
let resource = resource_service.get_resource(path_components).await?;
let privileges = resource.get_user_privileges(&principal)?;
let privileges = resource.get_user_privileges(principal)?;
if !privileges.has(&UserPrivilege::Write) {
return Err(Error::Unauthorized.into());
}

View File

@@ -2,15 +2,17 @@ mod delete;
mod propfind;
mod proppatch;
pub(crate) use delete::route_delete;
pub(crate) use propfind::route_propfind;
pub(crate) use proppatch::route_proppatch;
#[cfg(feature = "actix")]
pub(crate) use delete::actix_route_delete;
#[cfg(feature = "axum")]
pub(crate) use delete::axum_route_delete;
#[cfg(feature = "actix")]
pub(crate) use propfind::actix_route_propfind;
#[cfg(feature = "axum")]
pub(crate) use propfind::axum_route_propfind;
#[cfg(feature = "actix")]
pub(crate) use proppatch::actix_route_proppatch;
#[cfg(feature = "axum")]
pub(crate) use proppatch::axum_route_proppatch;

View File

@@ -7,8 +7,11 @@ use crate::resource::ResourceService;
use crate::xml::MultistatusElement;
use crate::xml::PropfindElement;
use crate::xml::PropfindType;
#[cfg(feature = "axum")]
use axum::extract::{Extension, OriginalUri, Path, State};
use rustical_xml::PropName;
use rustical_xml::XmlDocument;
use std::sync::Arc;
use tracing::instrument;
#[cfg(feature = "actix")]
@@ -30,21 +33,46 @@ pub(crate) async fn actix_route_propfind<R: ResourceService>(
route_propfind(
&path.into_inner(),
req.path(),
body,
user,
depth,
&body,
&user,
&depth,
resource_service.as_ref(),
puri.as_ref(),
)
.await
}
#[cfg(feature = "axum")]
pub(crate) async fn axum_route_propfind<R: ResourceService>(
Path(path): Path<R::PathComponents>,
State(resource_service): State<Arc<R>>,
depth: Depth,
Extension(principal): Extension<R::Principal>,
uri: OriginalUri,
Extension(puri): Extension<R::PrincipalUri>,
body: String,
) -> Result<
MultistatusElement<<R::Resource as Resource>::Prop, <R::MemberType as Resource>::Prop>,
R::Error,
> {
route_propfind::<R>(
&path,
uri.path(),
&body,
&principal,
&depth,
resource_service.as_ref(),
&puri,
)
.await
}
pub(crate) async fn route_propfind<R: ResourceService>(
path_components: &R::PathComponents,
path: &str,
body: String,
user: R::Principal,
depth: Depth,
body: &str,
principal: &R::Principal,
depth: &Depth,
resource_service: &R,
puri: &impl PrincipalUri,
) -> Result<
@@ -52,7 +80,7 @@ pub(crate) async fn route_propfind<R: ResourceService>(
R::Error,
> {
let resource = resource_service.get_resource(path_components).await?;
let privileges = resource.get_user_privileges(&user)?;
let privileges = resource.get_user_privileges(principal)?;
if !privileges.has(&UserPrivilege::Read) {
return Err(Error::Unauthorized.into());
}
@@ -60,7 +88,7 @@ pub(crate) async fn route_propfind<R: ResourceService>(
// A request body is optional. If empty we MUST return all props
let propfind_self: PropfindElement<<<R::Resource as Resource>::Prop as PropName>::Names> =
if !body.is_empty() {
PropfindElement::parse_str(&body).map_err(Error::XmlError)?
PropfindElement::parse_str(body).map_err(Error::XmlError)?
} else {
PropfindElement {
prop: PropfindType::Allprop,
@@ -68,7 +96,7 @@ pub(crate) async fn route_propfind<R: ResourceService>(
};
let propfind_member: PropfindElement<<<R::MemberType as Resource>::Prop as PropName>::Names> =
if !body.is_empty() {
PropfindElement::parse_str(&body).map_err(Error::XmlError)?
PropfindElement::parse_str(body).map_err(Error::XmlError)?
} else {
PropfindElement {
prop: PropfindType::Allprop,
@@ -76,18 +104,18 @@ pub(crate) async fn route_propfind<R: ResourceService>(
};
let mut member_responses = Vec::new();
if depth != Depth::Zero {
if depth != &Depth::Zero {
for (subpath, member) in resource_service.get_members(path_components).await? {
member_responses.push(member.propfind_typed(
&format!("{}/{}", path.trim_end_matches('/'), subpath),
&propfind_member.prop,
puri,
&user,
principal,
)?);
}
}
let response = resource.propfind_typed(path, &propfind_self.prop, puri, &user)?;
let response = resource.propfind_typed(path, &propfind_self.prop, puri, &principal)?;
Ok(MultistatusElement {
responses: vec![response],

View File

@@ -1,7 +1,10 @@
use crate::Error;
use crate::privileges::UserPrivilege;
use std::sync::Arc;
use crate::resource::Resource;
use crate::resource::ResourceService;
#[cfg(feature = "axum")]
use axum::extract::{Extension, OriginalUri, Path, State};
use crate::xml::MultistatusElement;
use crate::xml::TagList;
use crate::xml::multistatus::{PropstatElement, PropstatWrapper, ResponseElement};
@@ -74,8 +77,30 @@ pub(crate) async fn actix_route_proppatch<R: ResourceService>(
route_proppatch(
&path.into_inner(),
req.path(),
body,
principal,
&body,
&principal,
resource_service.as_ref(),
)
.await
}
#[cfg(feature = "axum")]
pub(crate) async fn axum_route_proppatch<R: ResourceService>(
Path(path): Path<R::PathComponents>,
State(resource_service): State<Arc<R>>,
Extension(principal): Extension<R::Principal>,
uri: OriginalUri,
body: String,
) -> Result<
MultistatusElement<String, String>,
R::Error,
> {
route_proppatch(
&path,
uri.path(),
&body,
&principal,
resource_service.as_ref(),
)
.await
@@ -84,8 +109,8 @@ pub(crate) async fn actix_route_proppatch<R: ResourceService>(
pub(crate) async fn route_proppatch<R: ResourceService>(
path_components: &R::PathComponents,
path: &str,
body: String,
principal: R::Principal,
body: &str,
principal: &R::Principal,
resource_service: &R,
) -> Result<MultistatusElement<String, String>, R::Error> {
let href = path.to_owned();