mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 10:32:19 +00:00
Checkpoint: Migration to axum
This commit is contained in:
@@ -2,77 +2,15 @@ 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::extract::{Path, State};
|
||||
use axum_extra::TypedHeader;
|
||||
use headers::Header;
|
||||
use headers::{HeaderValue, IfMatch, IfNoneMatch};
|
||||
#[cfg(feature = "axum")]
|
||||
use headers::{IfMatch, IfNoneMatch};
|
||||
use http::HeaderMap;
|
||||
use itertools::Itertools;
|
||||
#[cfg(feature = "axum")]
|
||||
use std::sync::Arc;
|
||||
use tracing::instrument;
|
||||
|
||||
#[cfg(feature = "actix")]
|
||||
#[instrument(parent = root_span.id(), skip(path, req, root_span, resource_service))]
|
||||
pub async fn actix_route_delete<R: ResourceService>(
|
||||
path: actix_web::web::Path<R::PathComponents>,
|
||||
req: actix_web::HttpRequest,
|
||||
principal: R::Principal,
|
||||
resource_service: actix_web::web::Data<R>,
|
||||
root_span: tracing_actix_web::RootSpan,
|
||||
) -> Result<actix_web::HttpResponse, R::Error> {
|
||||
let no_trash = req
|
||||
.headers()
|
||||
.get("X-No-Trashbin")
|
||||
.map(|val| matches!(val.to_str(), Ok("1")))
|
||||
.unwrap_or(false);
|
||||
|
||||
// This weird conversion stuff is because we want to use the headers library (to be
|
||||
// framework-agnostic in the future) which uses http==1.0,
|
||||
// while actix-web still uses http==0.2
|
||||
let if_match = req
|
||||
.headers()
|
||||
.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_02::header::IF_NONE_MATCH)
|
||||
.map(|val_02| HeaderValue::from_bytes(val_02.as_bytes()).unwrap())
|
||||
.collect_vec();
|
||||
|
||||
let if_match = if if_match.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(IfMatch::decode(&mut if_match.iter()).unwrap())
|
||||
};
|
||||
let if_none_match = if if_none_match.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(IfNoneMatch::decode(&mut if_none_match.iter()).unwrap())
|
||||
};
|
||||
|
||||
route_delete(
|
||||
&path.into_inner(),
|
||||
&principal,
|
||||
resource_service.as_ref(),
|
||||
no_trash,
|
||||
if_match,
|
||||
if_none_match,
|
||||
)
|
||||
.await?;
|
||||
|
||||
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>,
|
||||
State(resource_service): State<R>,
|
||||
principal: R::Principal,
|
||||
if_match: Option<TypedHeader<IfMatch>>,
|
||||
if_none_match: Option<TypedHeader<IfNoneMatch>>,
|
||||
header_map: HeaderMap,
|
||||
@@ -84,7 +22,7 @@ pub(crate) async fn axum_route_delete<R: ResourceService>(
|
||||
route_delete(
|
||||
&path,
|
||||
&principal,
|
||||
resource_service.as_ref(),
|
||||
&resource_service,
|
||||
no_trash,
|
||||
if_match.map(|hdr| hdr.0),
|
||||
if_none_match.map(|hdr| hdr.0),
|
||||
|
||||
@@ -2,17 +2,6 @@ mod delete;
|
||||
mod propfind;
|
||||
mod 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;
|
||||
|
||||
@@ -7,47 +7,15 @@ 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")]
|
||||
#[instrument(parent = root_span.id(), skip(path, req, root_span, resource_service, puri))]
|
||||
#[allow(clippy::type_complexity)]
|
||||
pub(crate) async fn actix_route_propfind<R: ResourceService>(
|
||||
path: ::actix_web::web::Path<R::PathComponents>,
|
||||
body: String,
|
||||
req: ::actix_web::HttpRequest,
|
||||
user: R::Principal,
|
||||
depth: Depth,
|
||||
root_span: tracing_actix_web::RootSpan,
|
||||
resource_service: ::actix_web::web::Data<R>,
|
||||
puri: ::actix_web::web::Data<R::PrincipalUri>,
|
||||
) -> Result<
|
||||
MultistatusElement<<R::Resource as Resource>::Prop, <R::MemberType as Resource>::Prop>,
|
||||
R::Error,
|
||||
> {
|
||||
route_propfind(
|
||||
&path.into_inner(),
|
||||
req.path(),
|
||||
&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>>,
|
||||
State(resource_service): State<R>,
|
||||
depth: Depth,
|
||||
Extension(principal): Extension<R::Principal>,
|
||||
principal: R::Principal,
|
||||
uri: OriginalUri,
|
||||
Extension(puri): Extension<R::PrincipalUri>,
|
||||
body: String,
|
||||
@@ -61,7 +29,7 @@ pub(crate) async fn axum_route_propfind<R: ResourceService>(
|
||||
&body,
|
||||
&principal,
|
||||
&depth,
|
||||
resource_service.as_ref(),
|
||||
&resource_service,
|
||||
&puri,
|
||||
)
|
||||
.await
|
||||
@@ -115,7 +83,7 @@ pub(crate) async fn route_propfind<R: ResourceService>(
|
||||
}
|
||||
}
|
||||
|
||||
let response = resource.propfind_typed(path, &propfind_self.prop, puri, &principal)?;
|
||||
let response = resource.propfind_typed(path, &propfind_self.prop, puri, principal)?;
|
||||
|
||||
Ok(MultistatusElement {
|
||||
responses: vec![response],
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
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};
|
||||
use axum::extract::{OriginalUri, Path, State};
|
||||
use http::StatusCode;
|
||||
use quick_xml::name::Namespace;
|
||||
use rustical_xml::NamespaceOwned;
|
||||
@@ -17,7 +15,6 @@ use rustical_xml::XmlDeserialize;
|
||||
use rustical_xml::XmlDocument;
|
||||
use rustical_xml::XmlRootTag;
|
||||
use std::str::FromStr;
|
||||
use tracing::instrument;
|
||||
|
||||
#[derive(XmlDeserialize, Clone, Debug)]
|
||||
#[xml(untagged)]
|
||||
@@ -64,46 +61,14 @@ enum Operation<T: XmlDeserialize> {
|
||||
#[xml(ns = "crate::namespace::NS_DAV")]
|
||||
struct PropertyupdateElement<T: XmlDeserialize>(#[xml(ty = "untagged", flatten)] Vec<Operation<T>>);
|
||||
|
||||
#[cfg(feature = "actix")]
|
||||
#[instrument(parent = root_span.id(), skip(path, req, root_span, resource_service))]
|
||||
pub(crate) async fn actix_route_proppatch<R: ResourceService>(
|
||||
path: actix_web::web::Path<R::PathComponents>,
|
||||
body: String,
|
||||
req: actix_web::HttpRequest,
|
||||
principal: R::Principal,
|
||||
root_span: tracing_actix_web::RootSpan,
|
||||
resource_service: actix_web::web::Data<R>,
|
||||
) -> Result<MultistatusElement<String, String>, R::Error> {
|
||||
route_proppatch(
|
||||
&path.into_inner(),
|
||||
req.path(),
|
||||
&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>,
|
||||
State(resource_service): State<R>,
|
||||
principal: R::Principal,
|
||||
uri: OriginalUri,
|
||||
body: String,
|
||||
) -> Result<
|
||||
MultistatusElement<String, String>,
|
||||
R::Error,
|
||||
> {
|
||||
route_proppatch(
|
||||
&path,
|
||||
uri.path(),
|
||||
&body,
|
||||
&principal,
|
||||
resource_service.as_ref(),
|
||||
)
|
||||
.await
|
||||
) -> Result<MultistatusElement<String, String>, R::Error> {
|
||||
route_proppatch(&path, uri.path(), &body, &principal, &resource_service).await
|
||||
}
|
||||
|
||||
pub(crate) async fn route_proppatch<R: ResourceService>(
|
||||
@@ -118,10 +83,10 @@ pub(crate) async fn route_proppatch<R: ResourceService>(
|
||||
// Extract operations
|
||||
let PropertyupdateElement::<SetPropertyPropWrapperWrapper<<R::Resource as Resource>::Prop>>(
|
||||
operations,
|
||||
) = XmlDocument::parse_str(&body).map_err(Error::XmlError)?;
|
||||
) = XmlDocument::parse_str(body).map_err(Error::XmlError)?;
|
||||
|
||||
let mut 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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user