aboutsummaryrefslogtreecommitdiff
path: root/aero-proto/src/dav/codec.rs
diff options
context:
space:
mode:
authorQuentin Dufour <quentin@deuxfleurs.fr>2024-04-24 11:43:57 +0200
committerQuentin Dufour <quentin@deuxfleurs.fr>2024-04-24 11:43:57 +0200
commit52d767edae38cc0d3effd216152ff2dcf6d19239 (patch)
tree15b32a7bdcb6f12b4c9f1dc875090a5ac7d614d7 /aero-proto/src/dav/codec.rs
parent5d85fd16f2625b6efb7ed70254a275237dfab1eb (diff)
downloadaerogramme-52d767edae38cc0d3effd216152ff2dcf6d19239.tar.gz
aerogramme-52d767edae38cc0d3effd216152ff2dcf6d19239.zip
Parse If-{None-}Match headers
Diffstat (limited to 'aero-proto/src/dav/codec.rs')
-rw-r--r--aero-proto/src/dav/codec.rs25
1 files changed, 24 insertions, 1 deletions
diff --git a/aero-proto/src/dav/codec.rs b/aero-proto/src/dav/codec.rs
index 9082d0a..57c3808 100644
--- a/aero-proto/src/dav/codec.rs
+++ b/aero-proto/src/dav/codec.rs
@@ -1,4 +1,4 @@
-use anyhow::Result;
+use anyhow::{bail, Result};
use hyper::{Request, Response, body::Bytes};
use hyper::body::Incoming;
use http_body_util::Full;
@@ -17,6 +17,7 @@ use http_body_util::BodyExt;
use aero_dav::types as dav;
use aero_dav::xml as dxml;
use super::controller::HttpResponse;
+use super::node::PutPolicy;
pub(crate) fn depth(req: &Request<impl hyper::body::Body>) -> dav::Depth {
match req.headers().get("Depth").map(hyper::header::HeaderValue::to_str) {
@@ -27,6 +28,28 @@ pub(crate) fn depth(req: &Request<impl hyper::body::Body>) -> dav::Depth {
}
}
+pub(crate) fn put_policy(req: &Request<impl hyper::body::Body>) -> Result<PutPolicy> {
+ if let Some(maybe_txt_etag) = req.headers().get("If-Match").map(hyper::header::HeaderValue::to_str) {
+ let etag = maybe_txt_etag?;
+ let dquote_count = etag.chars().filter(|c| *c == '"').count();
+ if dquote_count != 2 {
+ bail!("Either If-Match value is invalid or it's not supported (only single etag is supported)");
+ }
+
+ return Ok(PutPolicy::ReplaceEtag(etag.into()))
+ }
+
+ if let Some(maybe_txt_etag) = req.headers().get("If-None-Match").map(hyper::header::HeaderValue::to_str) {
+ let etag = maybe_txt_etag?;
+ if etag == "*" {
+ return Ok(PutPolicy::CreateOnly)
+ }
+ bail!("Either If-None-Match value is invalid or it's not supported (only asterisk is supported)")
+ }
+
+ Ok(PutPolicy::OverwriteAll)
+}
+
pub(crate) fn text_body(txt: &'static str) -> UnsyncBoxBody<Bytes, std::io::Error> {
UnsyncBoxBody::new(Full::new(Bytes::from(txt)).map_err(|e| match e {}))
}