From 96a27d7b223d97675ba9388a6dea9514939ff502 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Wed, 6 Mar 2024 16:09:20 +0100 Subject: Implement lockinfo --- src/dav/decoder.rs | 275 ++++++++++++++++++++++++++++++++++++++++++++++------- src/dav/encoder.rs | 1 + src/dav/error.rs | 1 + src/dav/types.rs | 1 + src/dav/xml.rs | 10 ++ 5 files changed, 256 insertions(+), 32 deletions(-) (limited to 'src/dav') diff --git a/src/dav/decoder.rs b/src/dav/decoder.rs index 43e5c49..042a608 100644 --- a/src/dav/decoder.rs +++ b/src/dav/decoder.rs @@ -11,6 +11,15 @@ use super::types::*; use super::error::ParsingError; use super::xml::{QRead, Reader, IRead, DAV_URN, CAL_URN}; +//@TODO (1) Rewrite all objects as Href, +// where we return Ok(None) instead of trying to find the object at any cost. +// Add a xml.find() -> Result, ParsingError> or similar for the cases we +// really need the object +// (2) Rewrite QRead and replace Result, _> with Result<_, _>, not found being a possible +// error. +// (3) Rewrite vectors with xml.collect() -> Result, _> +// (4) Something for alternatives would be great but no idea yet + // ---- ROOT ---- /// Propfind request @@ -74,11 +83,59 @@ impl QRead> for PropertyUpdate { } } +/// Generic response //@TODO Multistatus -//@TODO LockInfo +// LOCK REQUEST +impl QRead for LockInfo { + async fn qread(xml: &mut Reader) -> Result, ParsingError> { + xml.tag_start(DAV_URN, "lockinfo").await?; + let (mut m_scope, mut m_type, mut owner) = (None, None, None); + loop { + if let Some(v) = LockScope::qread(xml).await? { + m_scope = Some(v); + } else if let Some(v) = LockType::qread(xml).await? { + m_type = Some(v); + } else if let Some(v) = Owner::qread(xml).await? { + owner = Some(v); + } else { + match xml.peek() { + Event::End(_) => break, + _ => xml.skip().await?, + }; + } + } + xml.tag_stop(DAV_URN, "lockinfo").await?; + match (m_scope, m_type) { + (Some(lockscope), Some(locktype)) => Ok(Some(LockInfo { lockscope, locktype, owner })), + _ => Err(ParsingError::MissingChild), + } + } +} + +// LOCK RESPONSE +impl QRead> for PropValue { + async fn qread(xml: &mut Reader) -> Result, ParsingError> { + xml.tag_start(DAV_URN, "prop").await?; + let mut acc = Vec::new(); + loop { + // Found a property + if let Some(prop) = Property::qread(xml).await? { + acc.push(prop); + continue; + } + + // Otherwise skip or escape + match xml.peek() { + Event::End(_) => break, + _ => { xml.skip().await?; }, + } + } + xml.tag_stop(DAV_URN, "prop").await?; + Ok(Some(PropValue(acc))) + } +} -//@TODO PropValue /// Error response impl QRead> for Error { @@ -326,29 +383,6 @@ impl QRead> for PropertyRequest { } } - -impl QRead> for PropValue { - async fn qread(xml: &mut Reader) -> Result, ParsingError> { - xml.tag_start(DAV_URN, "prop").await?; - let mut acc = Vec::new(); - loop { - // Found a property - if let Some(prop) = Property::qread(xml).await? { - acc.push(prop); - continue; - } - - // Otherwise skip or escape - match xml.peek() { - Event::End(_) => break, - _ => { xml.skip().await?; }, - } - } - xml.tag_stop(DAV_URN, "prop").await?; - Ok(Some(PropValue(acc))) - } -} - impl QRead> for Property { async fn qread(xml: &mut Reader) -> Result, ParsingError> { use chrono::{DateTime, FixedOffset, TimeZone}; @@ -472,7 +506,126 @@ impl QRead> for Property { impl QRead for ActiveLock { async fn qread(xml: &mut Reader) -> Result, ParsingError> { - unimplemented!(); + xml.tag_start(DAV_URN, "activelock").await?; + let (mut m_scope, mut m_type, mut m_depth, mut owner, mut timeout, mut locktoken, mut m_root) = + (None, None, None, None, None, None, None); + + loop { + if let Some(v) = LockScope::qread(xml).await? { + m_scope = Some(v); + } else if let Some(v) = LockType::qread(xml).await? { + m_type = Some(v); + } else if let Some(v) = Depth::qread(xml).await? { + m_depth = Some(v); + } else if let Some(v) = Owner::qread(xml).await? { + owner = Some(v); + } else if let Some(v) = Timeout::qread(xml).await? { + timeout = Some(v); + } else if let Some(v) = LockToken::qread(xml).await? { + locktoken = Some(v); + } else if let Some(v) = LockRoot::qread(xml).await? { + m_root = Some(v); + } else { + match xml.peek() { + Event::End(_) => break, + _ => { xml.skip().await?; }, + } + } + } + + xml.tag_stop(DAV_URN, "activelock").await?; + match (m_scope, m_type, m_depth, m_root) { + (Some(lockscope), Some(locktype), Some(depth), Some(lockroot)) => + Ok(Some(ActiveLock { lockscope, locktype, depth, owner, timeout, locktoken, lockroot })), + _ => Err(ParsingError::MissingChild), + } + } +} + +impl QRead for Depth { + async fn qread(xml: &mut Reader) -> Result, ParsingError> { + xml.tag_start(DAV_URN, "depth").await?; + let depth_str = xml.tag_string().await?; + xml.tag_stop(DAV_URN, "depth").await?; + match depth_str.as_str() { + "0" => Ok(Some(Depth::Zero)), + "1" => Ok(Some(Depth::One)), + "infinity" => Ok(Some(Depth::Infinity)), + _ => Err(ParsingError::WrongToken), + } + } +} + +impl QRead for Owner { + async fn qread(xml: &mut Reader) -> Result, ParsingError> { + if xml.maybe_tag_start(DAV_URN, "owner").await?.is_none() { + return Ok(None) + } + + let mut owner = Owner::Unknown; + loop { + match xml.peek() { + Event::Text(_) | Event::CData(_) => { + let txt = xml.tag_string().await?; + if matches!(owner, Owner::Unknown) { + owner = Owner::Txt(txt); + } + } + Event::Start(_) | Event::Empty(_) => { + if let Some(href) = Href::qread(xml).await? { + owner = Owner::Href(href) + } + xml.skip().await?; + } + Event::End(_) => break, + _ => { xml.skip().await?; }, + } + }; + xml.tag_stop(DAV_URN, "owner").await?; + Ok(Some(owner)) + } +} + +impl QRead for Timeout { + async fn qread(xml: &mut Reader) -> Result, ParsingError> { + const SEC_PFX: &str = "SEC_PFX"; + + match xml.peek() { + Event::Start(b) if xml.is_tag(DAV_URN, "timeout") => xml.next().await?, + _ => return Ok(None), + }; + + let timeout = match xml.tag_string().await?.as_str() { + "Infinite" => Timeout::Infinite, + seconds => match seconds.strip_prefix(SEC_PFX) { + Some(secs) => Timeout::Seconds(secs.parse::()?), + None => return Err(ParsingError::InvalidValue), + }, + }; + + xml.tag_stop(DAV_URN, "timeout").await?; + Ok(Some(timeout)) + } +} + +impl QRead for LockToken { + async fn qread(xml: &mut Reader) -> Result, ParsingError> { + match xml.peek() { + Event::Start(b) if xml.is_tag(DAV_URN, "locktoken") => xml.next().await?, + _ => return Ok(None), + }; + let href = Href::qread(xml).await?.ok_or(ParsingError::MissingChild)?; + xml.tag_stop(DAV_URN, "locktoken").await?; + Ok(Some(LockToken(href))) + } +} + +impl QRead for LockRoot { + async fn qread(xml: &mut Reader) -> Result, ParsingError> { + xml.tag_start(DAV_URN, "lockroot").await?; + let href = Href::qread(xml).await?.ok_or(ParsingError::MissingChild)?; + xml.tag_stop(DAV_URN, "lockroot").await?; + Ok(Some(LockRoot(href))) } } @@ -495,10 +648,10 @@ impl QRead for LockEntry { loop { match xml.peek() { - Event::Start(b) if xml.is_tag(DAV_URN, "lockscope") => { + Event::Start(_) if xml.is_tag(DAV_URN, "lockscope") => { maybe_scope = LockScope::qread(xml).await?; }, - Event::Start(b) if xml.is_tag(DAV_URN, "lockentry") => { + Event::Start(_) if xml.is_tag(DAV_URN, "lockentry") => { maybe_type = LockType::qread(xml).await?; } Event::End(_) => break, @@ -518,14 +671,18 @@ impl QRead for LockEntry { impl QRead for LockScope { async fn qread(xml: &mut Reader) -> Result, ParsingError> { - xml.tag_start(DAV_URN, "lockscope").await?; + if xml.maybe_tag_start(DAV_URN, "lockscope").await?.is_none() { + return Ok(None) + } + let lockscope = loop { + println!("lockscope tag: {:?}", xml.peek()); match xml.peek() { - Event::Empty(b) if xml.is_tag(DAV_URN, "exclusive") => { + Event::Empty(_) if xml.is_tag(DAV_URN, "exclusive") => { xml.next().await?; break LockScope::Exclusive }, - Event::Empty(b) if xml.is_tag(DAV_URN, "shared") => { + Event::Empty(_) if xml.is_tag(DAV_URN, "shared") => { xml.next().await?; break LockScope::Shared } @@ -540,7 +697,10 @@ impl QRead for LockScope { impl QRead for LockType { async fn qread(xml: &mut Reader) -> Result, ParsingError> { - xml.tag_start(DAV_URN, "locktype").await?; + if xml.maybe_tag_start(DAV_URN, "locktype").await?.is_none() { + return Ok(None) + } + let locktype = loop { match xml.peek() { Event::Empty(b) if xml.is_tag(DAV_URN, "write") => { @@ -639,4 +799,55 @@ mod tests { ]) ])); } + + + #[tokio::test] + async fn rfc_propertyupdate() { + let src = r#" + + + + + Jim Whitehead + Roy Fielding + + + + + + + "#; + + let mut rdr = Reader::new(NsReader::from_reader(src.as_bytes())).await.unwrap(); + let got = PropertyUpdate::::qread(&mut rdr).await.unwrap().unwrap(); + + assert_eq!(got, PropertyUpdate(vec![ + PropertyUpdateItem::Set(Set(PropValue(vec![]))), + PropertyUpdateItem::Remove(Remove(PropName(vec![]))), + ])); + } + + #[tokio::test] + async fn rfc_lockinfo1() { + let src = r#" + + + + + + http://example.org/~ejw/contact.html + + +"#; + + let mut rdr = Reader::new(NsReader::from_reader(src.as_bytes())).await.unwrap(); + let got = LockInfo::qread(&mut rdr).await.unwrap().unwrap(); + assert_eq!(got, LockInfo { + lockscope: LockScope::Exclusive, + locktype: LockType::Write, + owner: Some(Owner::Href(Href("http://example.org/~ejw/contact.html".into()))), + }); + } + } diff --git a/src/dav/encoder.rs b/src/dav/encoder.rs index f3a1860..ec937c6 100644 --- a/src/dav/encoder.rs +++ b/src/dav/encoder.rs @@ -527,6 +527,7 @@ impl QWrite for Owner { match self { Self::Txt(txt) => xml.q.write_event_async(Event::Text(BytesText::new(&txt))).await?, Self::Href(href) => href.qwrite(xml).await?, + Self::Unknown => (), } xml.q.write_event_async(Event::End(end)).await } diff --git a/src/dav/error.rs b/src/dav/error.rs index 8cb60ba..88a5e60 100644 --- a/src/dav/error.rs +++ b/src/dav/error.rs @@ -6,6 +6,7 @@ pub enum ParsingError { NamespacePrefixAlreadyUsed, WrongToken, TagNotFound, + InvalidValue, Utf8Error(std::str::Utf8Error), QuickXml(quick_xml::Error), Chrono(chrono::format::ParseError), diff --git a/src/dav/types.rs b/src/dav/types.rs index f2eae3a..08c0bc6 100644 --- a/src/dav/types.rs +++ b/src/dav/types.rs @@ -366,6 +366,7 @@ pub struct Multistatus { pub enum Owner { Txt(String), Href(Href), + Unknown, } /// 14.18. prop XML Element diff --git a/src/dav/xml.rs b/src/dav/xml.rs index bf02721..ff121f4 100644 --- a/src/dav/xml.rs +++ b/src/dav/xml.rs @@ -106,6 +106,16 @@ impl Reader { } } + /// maybe find start tag + pub async fn maybe_tag_start(&mut self, ns: &[u8], key: &str) -> Result>, ParsingError> { + println!("maybe start tag {}", key); + let peek = self.peek(); + match peek { + Event::Start(_) | Event::Empty(_) if self.is_tag(ns, key) => Ok(Some(self.next().await?)), + _ => Ok(None), + } + } + /// find start tag pub async fn tag_start(&mut self, ns: &[u8], key: &str) -> Result, ParsingError> { println!("search start tag {}", key); -- cgit v1.2.3