2024-03-04 12:36:41 +00:00
|
|
|
use std::borrow::Cow;
|
2024-03-04 21:27:37 +00:00
|
|
|
use std::future::Future;
|
2024-03-04 12:36:41 +00:00
|
|
|
|
2024-03-04 16:55:48 +00:00
|
|
|
use quick_xml::events::{Event, BytesStart, BytesDecl, BytesText};
|
2024-03-04 12:36:41 +00:00
|
|
|
use quick_xml::events::attributes::AttrError;
|
2024-03-04 16:55:48 +00:00
|
|
|
use quick_xml::name::{Namespace, QName, PrefixDeclaration, ResolveResult, ResolveResult::*};
|
|
|
|
use quick_xml::reader::NsReader;
|
2024-03-04 12:36:41 +00:00
|
|
|
use tokio::io::AsyncBufRead;
|
|
|
|
|
|
|
|
use super::types::*;
|
|
|
|
|
2024-03-04 16:55:48 +00:00
|
|
|
#[derive(Debug)]
|
2024-03-04 12:36:41 +00:00
|
|
|
pub enum ParsingError {
|
|
|
|
NamespacePrefixAlreadyUsed,
|
|
|
|
WrongToken,
|
2024-03-04 16:55:48 +00:00
|
|
|
TagNotFound,
|
2024-03-04 21:27:37 +00:00
|
|
|
QuickXml(quick_xml::Error),
|
|
|
|
Eof
|
2024-03-04 12:36:41 +00:00
|
|
|
}
|
|
|
|
impl From<AttrError> for ParsingError {
|
|
|
|
fn from(value: AttrError) -> Self {
|
|
|
|
Self::QuickXml(value.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl From<quick_xml::Error> for ParsingError {
|
|
|
|
fn from(value: quick_xml::Error) -> Self {
|
|
|
|
Self::QuickXml(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const DAV_URN: &[u8] = b"DAV:";
|
|
|
|
const CALDAV_URN: &[u8] = b"urn:ietf:params:xml:ns:caldav";
|
|
|
|
const CARDDAV_URN: &[u8] = b"urn:ietf:params:xml:ns:carddav";
|
2024-03-04 21:27:37 +00:00
|
|
|
//const XML_URN: &[u8] = b"xml";
|
2024-03-04 12:36:41 +00:00
|
|
|
|
2024-03-04 21:27:37 +00:00
|
|
|
trait Reader = AsyncBufRead+Unpin+'static;
|
|
|
|
|
|
|
|
pub struct PeekRead<T: Reader> {
|
2024-03-04 16:55:48 +00:00
|
|
|
evt: Event<'static>,
|
|
|
|
rdr: NsReader<T>,
|
|
|
|
buf: Vec<u8>,
|
2024-03-04 12:36:41 +00:00
|
|
|
}
|
2024-03-04 21:27:37 +00:00
|
|
|
impl<T: Reader> PeekRead<T> {
|
2024-03-04 16:55:48 +00:00
|
|
|
async fn new(mut rdr: NsReader<T>) -> Result<Self, ParsingError> {
|
|
|
|
let mut buf: Vec<u8> = vec![];
|
|
|
|
let evt = rdr.read_event_into_async(&mut buf).await?.into_owned();
|
|
|
|
buf.clear();
|
|
|
|
Ok(Self { evt, rdr, buf })
|
2024-03-04 12:36:41 +00:00
|
|
|
}
|
|
|
|
|
2024-03-04 16:55:48 +00:00
|
|
|
fn peek(&self) -> &Event<'static> {
|
|
|
|
&self.evt
|
2024-03-04 12:36:41 +00:00
|
|
|
}
|
2024-03-04 21:27:37 +00:00
|
|
|
|
|
|
|
/// skip tag. Can't skip end, can't skip eof.
|
|
|
|
async fn skip(&mut self) -> Result<Event<'static>, ParsingError> {
|
2024-03-04 16:55:48 +00:00
|
|
|
match &self.evt {
|
|
|
|
Event::Start(b) => {
|
|
|
|
let _span = self.rdr.read_to_end_into_async(b.to_end().name(), &mut self.buf).await?;
|
|
|
|
self.next().await
|
|
|
|
},
|
2024-03-04 21:27:37 +00:00
|
|
|
Event::End(_) => Err(ParsingError::WrongToken),
|
|
|
|
Event::Eof => Err(ParsingError::Eof),
|
|
|
|
_ => self.next().await,
|
2024-03-04 12:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-04 21:27:37 +00:00
|
|
|
/// read one more tag
|
|
|
|
async fn next(&mut self) -> Result<Event<'static>, ParsingError> {
|
2024-03-04 16:55:48 +00:00
|
|
|
let evt = self.rdr.read_event_into_async(&mut self.buf).await?.into_owned();
|
|
|
|
self.buf.clear();
|
2024-03-04 21:27:37 +00:00
|
|
|
let old_evt = std::mem::replace(&mut self.evt, evt);
|
|
|
|
Ok(old_evt)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// check if this is the desired tag
|
|
|
|
fn is_tag(&self, ns: &[u8], key: &str) -> bool {
|
|
|
|
let qname = match self.peek() {
|
|
|
|
Event::Start(bs) | Event::Empty(bs) => bs.name(),
|
|
|
|
Event::End(be) => be.name(),
|
|
|
|
_ => return false,
|
|
|
|
};
|
|
|
|
|
|
|
|
let (extr_ns, local) = self.rdr.resolve_element(qname);
|
|
|
|
|
|
|
|
if local.into_inner() != key.as_bytes() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
match extr_ns {
|
|
|
|
ResolveResult::Bound(v) => v.into_inner() == ns,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// find start tag
|
|
|
|
async fn tag_start(&mut self, ns: &[u8], key: &str) -> Result<Event<'static>, ParsingError> {
|
|
|
|
loop {
|
|
|
|
match self.peek() {
|
|
|
|
Event::Start(b) if self.is_tag(ns, key) => break,
|
|
|
|
_ => { self.skip().await?; },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.next().await
|
|
|
|
}
|
|
|
|
|
|
|
|
// find stop tag
|
|
|
|
async fn tag_stop(&mut self, ns: &[u8], key: &str) -> Result<Event<'static>, ParsingError> {
|
|
|
|
loop {
|
|
|
|
match self.peek() {
|
|
|
|
Event::End(b) if self.is_tag(ns, key) => break,
|
|
|
|
_ => { self.skip().await?; },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.next().await
|
2024-03-04 12:36:41 +00:00
|
|
|
}
|
2024-03-04 16:55:48 +00:00
|
|
|
}
|
|
|
|
|
2024-03-04 21:27:37 +00:00
|
|
|
pub trait QReadable<T: Reader>: Sized {
|
2024-03-04 16:55:48 +00:00
|
|
|
async fn read(xml: &mut PeekRead<T>) -> Result<Self, ParsingError>;
|
|
|
|
}
|
2024-03-04 12:36:41 +00:00
|
|
|
|
2024-03-04 21:27:37 +00:00
|
|
|
impl<E: Extension, T: Reader> QReadable<T> for PropFind<E> {
|
2024-03-04 16:55:48 +00:00
|
|
|
async fn read(xml: &mut PeekRead<T>) -> Result<PropFind<E>, ParsingError> {
|
|
|
|
// Find propfind
|
2024-03-04 21:27:37 +00:00
|
|
|
xml.tag_start(DAV_URN, "propfind").await?;
|
2024-03-04 16:55:48 +00:00
|
|
|
|
|
|
|
// Find any tag
|
2024-03-04 21:27:37 +00:00
|
|
|
let propfind: PropFind<E> = loop {
|
2024-03-04 16:55:48 +00:00
|
|
|
match xml.peek() {
|
2024-03-04 21:27:37 +00:00
|
|
|
Event::Start(_) if xml.is_tag(DAV_URN, "allprop") => {
|
|
|
|
xml.tag_start(DAV_URN, "allprop").await?;
|
|
|
|
let r = PropFind::AllProp(Some(Include::read(xml).await?));
|
|
|
|
xml.tag_stop(DAV_URN, "allprop").await?;
|
|
|
|
break r
|
2024-03-04 16:55:48 +00:00
|
|
|
},
|
2024-03-04 21:27:37 +00:00
|
|
|
Event::Start(_) if xml.is_tag(DAV_URN, "prop") => {
|
|
|
|
xml.tag_start(DAV_URN, "prop").await?;
|
|
|
|
let r = PropFind::Prop(PropName::read(xml).await?);
|
|
|
|
xml.tag_stop(DAV_URN, "prop").await?;
|
|
|
|
break r
|
2024-03-04 16:55:48 +00:00
|
|
|
},
|
2024-03-04 21:27:37 +00:00
|
|
|
Event::Empty(_) if xml.is_tag(DAV_URN, "allprop") => {
|
|
|
|
xml.next().await?;
|
|
|
|
break PropFind::AllProp(None)
|
|
|
|
},
|
|
|
|
Event::Empty(_) if xml.is_tag(DAV_URN, "propname") => {
|
|
|
|
xml.next().await?;
|
|
|
|
break PropFind::PropName
|
|
|
|
},
|
|
|
|
_ => { xml.skip().await?; },
|
2024-03-04 16:55:48 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Close tag
|
2024-03-04 21:27:37 +00:00
|
|
|
xml.tag_stop(DAV_URN, "propfind").await?;
|
|
|
|
|
|
|
|
Ok(propfind)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<E: Extension, T: Reader> QReadable<T> for Include<E> {
|
|
|
|
async fn read(xml: &mut PeekRead<T>) -> Result<Include<E>, ParsingError> {
|
|
|
|
xml.tag_start(DAV_URN, "include").await?;
|
|
|
|
let mut acc: Vec<PropertyRequest<E>> = Vec::new();
|
2024-03-04 16:55:48 +00:00
|
|
|
loop {
|
|
|
|
match xml.peek() {
|
2024-03-04 21:27:37 +00:00
|
|
|
Event::Start(_) => acc.push(PropertyRequest::read(xml).await?),
|
|
|
|
Event::End(_) if xml.is_tag(DAV_URN, "include") => break,
|
|
|
|
_ => { xml.skip().await?; },
|
2024-03-04 16:55:48 +00:00
|
|
|
}
|
2024-03-04 12:36:41 +00:00
|
|
|
}
|
2024-03-04 21:27:37 +00:00
|
|
|
xml.tag_stop(DAV_URN, "include").await?;
|
|
|
|
Ok(Include(acc))
|
|
|
|
}
|
|
|
|
}
|
2024-03-04 16:55:48 +00:00
|
|
|
|
2024-03-04 21:27:37 +00:00
|
|
|
impl<E: Extension, T: Reader> QReadable<T> for PropName<E> {
|
|
|
|
async fn read(xml: &mut PeekRead<T>) -> Result<PropName<E>, ParsingError> {
|
|
|
|
xml.tag_start(DAV_URN, "prop").await?;
|
|
|
|
let mut acc: Vec<PropertyRequest<E>> = Vec::new();
|
|
|
|
loop {
|
|
|
|
match xml.peek() {
|
|
|
|
Event::Start(_) => acc.push(PropertyRequest::read(xml).await?),
|
|
|
|
Event::End(_) if xml.is_tag(DAV_URN, "prop") => break,
|
|
|
|
_ => { xml.skip().await?; },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
xml.tag_stop(DAV_URN, "prop").await?;
|
|
|
|
Ok(PropName(acc))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<E: Extension, T: Reader> QReadable<T> for PropertyRequest<E> {
|
|
|
|
async fn read(xml: &mut PeekRead<T>) -> Result<PropertyRequest<E>, ParsingError> {
|
|
|
|
/*match xml.peek() {
|
|
|
|
|
|
|
|
}*/
|
|
|
|
unimplemented!();
|
2024-03-04 12:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-04 16:55:48 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2024-03-04 12:36:41 +00:00
|
|
|
|
2024-03-04 16:55:48 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn basic_propfind() {
|
2024-03-04 21:27:37 +00:00
|
|
|
let src = r#"<?xml version="1.0" encoding="utf-8" ?>
|
|
|
|
<rando/>
|
|
|
|
<garbage><old/></garbage>
|
|
|
|
<D:propfind xmlns:D="DAV:">
|
|
|
|
<D:propname/>
|
|
|
|
</D:propfind>
|
|
|
|
"#;
|
2024-03-04 12:36:41 +00:00
|
|
|
|
2024-03-04 16:55:48 +00:00
|
|
|
let mut rdr = PeekRead::new(NsReader::from_reader(src.as_bytes())).await.unwrap();
|
|
|
|
let got = PropFind::<NoExtension>::read(&mut rdr).await.unwrap();
|
|
|
|
assert!(matches!(got, PropFind::PropName));
|
2024-03-04 12:36:41 +00:00
|
|
|
}
|
|
|
|
}
|