From 2bda8ef081d9c8f47081845bb4545a12b6ae8a18 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Thu, 18 Apr 2024 13:55:57 +0200 Subject: split dav module in multiple files --- aero-proto/src/dav/codec.rs | 80 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 aero-proto/src/dav/codec.rs (limited to 'aero-proto/src/dav/codec.rs') diff --git a/aero-proto/src/dav/codec.rs b/aero-proto/src/dav/codec.rs new file mode 100644 index 0000000..08af2fe --- /dev/null +++ b/aero-proto/src/dav/codec.rs @@ -0,0 +1,80 @@ +use anyhow::Result; +use hyper::{Request, Response, body::Bytes}; +use hyper::body::Incoming; +use http_body_util::Full; +use futures::stream::StreamExt; +use futures::stream::TryStreamExt; +use http_body_util::BodyStream; +use http_body_util::StreamBody; +use http_body_util::combinators::BoxBody; +use hyper::body::Frame; +use tokio_util::sync::PollSender; +use std::io::{Error, ErrorKind}; +use futures::sink::SinkExt; +use tokio_util::io::{SinkWriter, CopyToBytes}; +use http_body_util::BodyExt; + +use aero_dav::types as dav; +use aero_dav::xml as dxml; + +pub(crate) fn depth(req: &Request) -> dav::Depth { + match req.headers().get("Depth").map(hyper::header::HeaderValue::to_str) { + Some(Ok("0")) => dav::Depth::Zero, + Some(Ok("1")) => dav::Depth::One, + Some(Ok("Infinity")) => dav::Depth::Infinity, + _ => dav::Depth::Zero, + } +} + +pub(crate) fn text_body(txt: &'static str) -> BoxBody { + BoxBody::new(Full::new(Bytes::from(txt)).map_err(|e| match e {})) +} + +pub(crate) fn serialize(status_ok: hyper::StatusCode, elem: T) -> Result>> { + let (tx, rx) = tokio::sync::mpsc::channel::(1); + + // Build the writer + tokio::task::spawn(async move { + let sink = PollSender::new(tx).sink_map_err(|_| Error::from(ErrorKind::BrokenPipe)); + let mut writer = SinkWriter::new(CopyToBytes::new(sink)); + let q = quick_xml::writer::Writer::new_with_indent(&mut writer, b' ', 4); + let ns_to_apply = vec![ ("xmlns:D".into(), "DAV:".into()), ("xmlns:C".into(), "urn:ietf:params:xml:ns:caldav".into()) ]; + let mut qwriter = dxml::Writer { q, ns_to_apply }; + let decl = quick_xml::events::BytesDecl::from_start(quick_xml::events::BytesStart::from_content("xml version=\"1.0\" encoding=\"utf-8\"", 0)); + match qwriter.q.write_event_async(quick_xml::events::Event::Decl(decl)).await { + Ok(_) => (), + Err(e) => tracing::error!(err=?e, "unable to write XML declaration "), + } + match elem.qwrite(&mut qwriter).await { + Ok(_) => tracing::debug!("fully serialized object"), + Err(e) => tracing::error!(err=?e, "failed to serialize object"), + } + }); + + + // Build the reader + let recv = tokio_stream::wrappers::ReceiverStream::new(rx); + let stream = StreamBody::new(recv.map(|v| Ok(Frame::data(v)))); + let boxed_body = BoxBody::new(stream); + + let response = Response::builder() + .status(status_ok) + .header("content-type", "application/xml; charset=\"utf-8\"") + .body(boxed_body)?; + + Ok(response) +} + + +/// Deserialize a request body to an XML request +pub(crate) async fn deserialize>(req: Request) -> Result { + let stream_of_frames = BodyStream::new(req.into_body()); + let stream_of_bytes = stream_of_frames + .try_filter_map(|frame| async move { Ok(frame.into_data().ok()) }) + .map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err)); + let async_read = tokio_util::io::StreamReader::new(stream_of_bytes); + let async_read = std::pin::pin!(async_read); + let mut rdr = dxml::Reader::new(quick_xml::reader::NsReader::from_reader(async_read)).await?; + let parsed = rdr.find::().await?; + Ok(parsed) +} -- cgit v1.2.3 From 936f851fdb120dd0b46c4effeabe0dbb508d4d3d Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Sun, 21 Apr 2024 13:47:45 +0200 Subject: Do not silently drop an invalid frame --- aero-proto/src/dav/codec.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'aero-proto/src/dav/codec.rs') diff --git a/aero-proto/src/dav/codec.rs b/aero-proto/src/dav/codec.rs index 08af2fe..e317a03 100644 --- a/aero-proto/src/dav/codec.rs +++ b/aero-proto/src/dav/codec.rs @@ -70,8 +70,12 @@ pub(crate) fn serialize(status_ok: hyper::Stat pub(crate) async fn deserialize>(req: Request) -> Result { let stream_of_frames = BodyStream::new(req.into_body()); let stream_of_bytes = stream_of_frames - .try_filter_map(|frame| async move { Ok(frame.into_data().ok()) }) - .map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err)); + .map_ok(|frame| frame.into_data()) + .map(|obj| match obj { + Ok(Ok(v)) => Ok(v), + Ok(Err(_)) => Err(std::io::Error::new(std::io::ErrorKind::Other, "conversion error")), + Err(err) => Err(std::io::Error::new(std::io::ErrorKind::Other, err)), + }); let async_read = tokio_util::io::StreamReader::new(stream_of_bytes); let async_read = std::pin::pin!(async_read); let mut rdr = dxml::Reader::new(quick_xml::reader::NsReader::from_reader(async_read)).await?; -- cgit v1.2.3 From 50ce8621c2eaf91c46be0a2a9c2b82b19e66880b Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Tue, 23 Apr 2024 15:20:29 +0200 Subject: GET implementation --- aero-proto/src/dav/codec.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'aero-proto/src/dav/codec.rs') diff --git a/aero-proto/src/dav/codec.rs b/aero-proto/src/dav/codec.rs index e317a03..9082d0a 100644 --- a/aero-proto/src/dav/codec.rs +++ b/aero-proto/src/dav/codec.rs @@ -6,7 +6,7 @@ use futures::stream::StreamExt; use futures::stream::TryStreamExt; use http_body_util::BodyStream; use http_body_util::StreamBody; -use http_body_util::combinators::BoxBody; +use http_body_util::combinators::UnsyncBoxBody; use hyper::body::Frame; use tokio_util::sync::PollSender; use std::io::{Error, ErrorKind}; @@ -16,6 +16,7 @@ use http_body_util::BodyExt; use aero_dav::types as dav; use aero_dav::xml as dxml; +use super::controller::HttpResponse; pub(crate) fn depth(req: &Request) -> dav::Depth { match req.headers().get("Depth").map(hyper::header::HeaderValue::to_str) { @@ -26,11 +27,11 @@ pub(crate) fn depth(req: &Request) -> dav::Depth { } } -pub(crate) fn text_body(txt: &'static str) -> BoxBody { - BoxBody::new(Full::new(Bytes::from(txt)).map_err(|e| match e {})) +pub(crate) fn text_body(txt: &'static str) -> UnsyncBoxBody { + UnsyncBoxBody::new(Full::new(Bytes::from(txt)).map_err(|e| match e {})) } -pub(crate) fn serialize(status_ok: hyper::StatusCode, elem: T) -> Result>> { +pub(crate) fn serialize(status_ok: hyper::StatusCode, elem: T) -> Result { let (tx, rx) = tokio::sync::mpsc::channel::(1); // Build the writer @@ -55,7 +56,7 @@ pub(crate) fn serialize(status_ok: hyper::Stat // Build the reader let recv = tokio_stream::wrappers::ReceiverStream::new(rx); let stream = StreamBody::new(recv.map(|v| Ok(Frame::data(v)))); - let boxed_body = BoxBody::new(stream); + let boxed_body = UnsyncBoxBody::new(stream); let response = Response::builder() .status(status_ok) -- cgit v1.2.3 From 52d767edae38cc0d3effd216152ff2dcf6d19239 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Wed, 24 Apr 2024 11:43:57 +0200 Subject: Parse If-{None-}Match headers --- aero-proto/src/dav/codec.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'aero-proto/src/dav/codec.rs') 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) -> dav::Depth { match req.headers().get("Depth").map(hyper::header::HeaderValue::to_str) { @@ -27,6 +28,28 @@ pub(crate) fn depth(req: &Request) -> dav::Depth { } } +pub(crate) fn put_policy(req: &Request) -> Result { + 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 { UnsyncBoxBody::new(Full::new(Bytes::from(txt)).map_err(|e| match e {})) } -- cgit v1.2.3 From 32dfd25f570b7a55bf43752684d286be0f6b2dc2 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Thu, 16 May 2024 17:38:34 +0200 Subject: format + WIP calendar-query --- aero-proto/src/dav/codec.rs | 71 +++++++++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 22 deletions(-) (limited to 'aero-proto/src/dav/codec.rs') diff --git a/aero-proto/src/dav/codec.rs b/aero-proto/src/dav/codec.rs index 57c3808..a441e7e 100644 --- a/aero-proto/src/dav/codec.rs +++ b/aero-proto/src/dav/codec.rs @@ -1,26 +1,30 @@ use anyhow::{bail, Result}; -use hyper::{Request, Response, body::Bytes}; -use hyper::body::Incoming; -use http_body_util::Full; +use futures::sink::SinkExt; use futures::stream::StreamExt; use futures::stream::TryStreamExt; +use http_body_util::combinators::UnsyncBoxBody; +use http_body_util::BodyExt; use http_body_util::BodyStream; +use http_body_util::Full; use http_body_util::StreamBody; -use http_body_util::combinators::UnsyncBoxBody; use hyper::body::Frame; -use tokio_util::sync::PollSender; +use hyper::body::Incoming; +use hyper::{body::Bytes, Request, Response}; use std::io::{Error, ErrorKind}; -use futures::sink::SinkExt; -use tokio_util::io::{SinkWriter, CopyToBytes}; -use http_body_util::BodyExt; +use tokio_util::io::{CopyToBytes, SinkWriter}; +use tokio_util::sync::PollSender; -use aero_dav::types as dav; -use aero_dav::xml as dxml; use super::controller::HttpResponse; use super::node::PutPolicy; +use aero_dav::types as dav; +use aero_dav::xml as dxml; pub(crate) fn depth(req: &Request) -> dav::Depth { - match req.headers().get("Depth").map(hyper::header::HeaderValue::to_str) { + match req + .headers() + .get("Depth") + .map(hyper::header::HeaderValue::to_str) + { Some(Ok("0")) => dav::Depth::Zero, Some(Ok("1")) => dav::Depth::One, Some(Ok("Infinity")) => dav::Depth::Infinity, @@ -29,20 +33,28 @@ pub(crate) fn depth(req: &Request) -> dav::Depth { } pub(crate) fn put_policy(req: &Request) -> Result { - if let Some(maybe_txt_etag) = req.headers().get("If-Match").map(hyper::header::HeaderValue::to_str) { + 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())) + return Ok(PutPolicy::ReplaceEtag(etag.into())); } - if let Some(maybe_txt_etag) = req.headers().get("If-None-Match").map(hyper::header::HeaderValue::to_str) { + 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) + return Ok(PutPolicy::CreateOnly); } bail!("Either If-None-Match value is invalid or it's not supported (only asterisk is supported)") } @@ -54,7 +66,10 @@ pub(crate) fn text_body(txt: &'static str) -> UnsyncBoxBody(status_ok: hyper::StatusCode, elem: T) -> Result { +pub(crate) fn serialize( + status_ok: hyper::StatusCode, + elem: T, +) -> Result { let (tx, rx) = tokio::sync::mpsc::channel::(1); // Build the writer @@ -62,10 +77,21 @@ pub(crate) fn serialize(status_ok: hyper::Stat let sink = PollSender::new(tx).sink_map_err(|_| Error::from(ErrorKind::BrokenPipe)); let mut writer = SinkWriter::new(CopyToBytes::new(sink)); let q = quick_xml::writer::Writer::new_with_indent(&mut writer, b' ', 4); - let ns_to_apply = vec![ ("xmlns:D".into(), "DAV:".into()), ("xmlns:C".into(), "urn:ietf:params:xml:ns:caldav".into()) ]; + let ns_to_apply = vec![ + ("xmlns:D".into(), "DAV:".into()), + ("xmlns:C".into(), "urn:ietf:params:xml:ns:caldav".into()), + ]; let mut qwriter = dxml::Writer { q, ns_to_apply }; - let decl = quick_xml::events::BytesDecl::from_start(quick_xml::events::BytesStart::from_content("xml version=\"1.0\" encoding=\"utf-8\"", 0)); - match qwriter.q.write_event_async(quick_xml::events::Event::Decl(decl)).await { + let decl = + quick_xml::events::BytesDecl::from_start(quick_xml::events::BytesStart::from_content( + "xml version=\"1.0\" encoding=\"utf-8\"", + 0, + )); + match qwriter + .q + .write_event_async(quick_xml::events::Event::Decl(decl)) + .await + { Ok(_) => (), Err(e) => tracing::error!(err=?e, "unable to write XML declaration "), } @@ -75,7 +101,6 @@ pub(crate) fn serialize(status_ok: hyper::Stat } }); - // Build the reader let recv = tokio_stream::wrappers::ReceiverStream::new(rx); let stream = StreamBody::new(recv.map(|v| Ok(Frame::data(v)))); @@ -89,7 +114,6 @@ pub(crate) fn serialize(status_ok: hyper::Stat Ok(response) } - /// Deserialize a request body to an XML request pub(crate) async fn deserialize>(req: Request) -> Result { let stream_of_frames = BodyStream::new(req.into_body()); @@ -97,7 +121,10 @@ pub(crate) async fn deserialize>(req: Request) -> Res .map_ok(|frame| frame.into_data()) .map(|obj| match obj { Ok(Ok(v)) => Ok(v), - Ok(Err(_)) => Err(std::io::Error::new(std::io::ErrorKind::Other, "conversion error")), + Ok(Err(_)) => Err(std::io::Error::new( + std::io::ErrorKind::Other, + "conversion error", + )), Err(err) => Err(std::io::Error::new(std::io::ErrorKind::Other, err)), }); let async_read = tokio_util::io::StreamReader::new(stream_of_bytes); -- cgit v1.2.3