Move depth_extractor to dav crate

This commit is contained in:
Lennart
2023-09-14 14:04:48 +02:00
parent a4b2e104e7
commit 7edb041eb7
5 changed files with 4 additions and 2 deletions

View File

@@ -1,53 +0,0 @@
use actix_web::{http::StatusCode, FromRequest, HttpRequest, ResponseError};
use derive_more::Display;
use futures_util::future::{err, ok, Ready};
#[derive(Debug, Display)]
pub struct InvalidDepthHeader {}
impl ResponseError for InvalidDepthHeader {
fn status_code(&self) -> actix_web::http::StatusCode {
StatusCode::BAD_REQUEST
}
}
#[derive(Debug, PartialEq)]
pub enum Depth {
Zero,
One,
Infinity,
}
impl TryFrom<&[u8]> for Depth {
type Error = InvalidDepthHeader;
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
match value {
b"0" => Ok(Depth::Zero),
b"1" => Ok(Depth::One),
b"Infinity" | b"infinity" => Ok(Depth::Infinity),
_ => Err(InvalidDepthHeader {}),
}
}
}
impl FromRequest for Depth {
type Error = InvalidDepthHeader;
type Future = Ready<Result<Self, Self::Error>>;
fn extract(req: &HttpRequest) -> Self::Future {
if let Some(depth_header) = req.headers().get("Depth") {
match depth_header.as_bytes().try_into() {
Ok(depth) => ok(depth),
Err(e) => err(e),
}
} else {
// default depth
ok(Depth::Zero)
}
}
fn from_request(req: &HttpRequest, _payload: &mut actix_web::dev::Payload) -> Self::Future {
Self::extract(req)
}
}

View File

@@ -14,7 +14,6 @@ use std::str::FromStr;
use std::sync::Arc;
use tokio::sync::RwLock;
pub mod depth_extractor;
pub mod error;
pub mod proptypes;
pub mod resources;

View File

@@ -1,4 +1,3 @@
use crate::depth_extractor::Depth;
use crate::CalDavContext;
use actix_web::http::header::ContentType;
use actix_web::http::StatusCode;
@@ -7,6 +6,7 @@ use actix_web::{HttpRequest, HttpResponse};
use anyhow::Result;
use quick_xml::events::BytesText;
use rustical_auth::{AuthInfoExtractor, CheckAuthentication};
use rustical_dav::depth_extractor::Depth;
use rustical_dav::namespace::Namespace;
use rustical_dav::resource::{HandlePropfind, Resource};
use rustical_dav::xml_snippets::generate_multistatus;