fix: resolve CalDAV PROPFIND returning empty property values (#153)
Root cause: XML namespace prefixes (e.g. "D", "C") were not being
resolved to their actual namespace URIs (e.g. "DAV:",
"urn:ietf:params:xml:ns:caldav") during PROPFIND parsing. This caused
all property match arms to fall through to the catch-all, producing
empty XML elements.
Changes:
- Add namespace-aware XML parsing (collect_ns_decls + resolve_name) to
WebDavAdapter, used by all DAV protocol parsers (WebDAV, CalDAV,
CardDAV)
- Add /.well-known/caldav -> /caldav/ redirect (RFC 6764)
- Add root /caldav/ PROPFIND response with current-user-principal and
calendar-home-set discovery properties
- Add /caldav/principals/{username}/ PROPFIND handler (was 500 error)
- Add /caldav/{username}/ user calendar home handler (calendar-home-set
target)
- Respect Depth header at root: depth 0 returns only root entry, depth
1+ includes calendar children
- Fix pre-existing TRANSCODE_POOL_THREADS test compilation error
- Add 8 new tests covering namespace resolution, discovery properties,
and principal responses
https://claude.ai/code/session_01T49VBJSimgo28APxbucHzq
This commit is contained in:
@@ -57,10 +57,12 @@ impl CalDavAdapter {
|
||||
let mut props = Vec::new();
|
||||
let mut hrefs = Vec::new();
|
||||
let mut sync_token = String::new();
|
||||
let mut ns_map = std::collections::HashMap::<String, String>::new();
|
||||
|
||||
loop {
|
||||
match xml_reader.read_event_into(&mut buffer) {
|
||||
Ok(Event::Start(ref e)) => {
|
||||
WebDavAdapter::collect_ns_decls(e, &mut ns_map);
|
||||
let name = e.name();
|
||||
let name_str = std::str::from_utf8(name.as_ref()).unwrap_or("");
|
||||
|
||||
@@ -102,11 +104,8 @@ impl CalDavAdapter {
|
||||
// We'll capture the text in the Text event
|
||||
}
|
||||
_ if in_prop => {
|
||||
// Add property to request
|
||||
let namespace = WebDavAdapter::extract_namespace(name_str);
|
||||
let prop_name = WebDavAdapter::extract_local_name(name_str);
|
||||
|
||||
props.push(QualifiedName::new(namespace, prop_name));
|
||||
let qname = WebDavAdapter::resolve_name(name_str, &ns_map);
|
||||
props.push(qname);
|
||||
}
|
||||
_ => { /* Ignore other elements */ }
|
||||
}
|
||||
@@ -138,15 +137,13 @@ impl CalDavAdapter {
|
||||
}
|
||||
}
|
||||
Ok(Event::Empty(ref e)) => {
|
||||
WebDavAdapter::collect_ns_decls(e, &mut ns_map);
|
||||
let name = e.name();
|
||||
let name_str = std::str::from_utf8(name.as_ref()).unwrap_or("");
|
||||
|
||||
if in_prop {
|
||||
// Add empty property element to request
|
||||
let namespace = WebDavAdapter::extract_namespace(name_str);
|
||||
let prop_name = WebDavAdapter::extract_local_name(name_str);
|
||||
|
||||
props.push(QualifiedName::new(namespace, prop_name));
|
||||
let qname = WebDavAdapter::resolve_name(name_str, &ns_map);
|
||||
props.push(qname);
|
||||
} else if name_str == "time-range" || name_str.ends_with(":time-range") {
|
||||
// Parse time-range attributes
|
||||
for attr in e.attributes().flatten() {
|
||||
@@ -199,7 +196,47 @@ impl CalDavAdapter {
|
||||
Ok(report_type)
|
||||
}
|
||||
|
||||
/// Generate a PROPFIND response for calendars
|
||||
/// Generate a PROPFIND response for the root CalDAV resource.
|
||||
/// Includes a response for /caldav/ itself with discovery properties
|
||||
/// (current-user-principal, calendar-home-set) plus each calendar.
|
||||
pub fn generate_root_propfind_response<W: Write>(
|
||||
writer: W,
|
||||
calendars: &[CalendarDto],
|
||||
request: &PropFindRequest,
|
||||
base_href: &str,
|
||||
username: &str,
|
||||
) -> Result<()> {
|
||||
let mut xml_writer = Writer::new(writer);
|
||||
|
||||
// Start multistatus response
|
||||
xml_writer.write_event(Event::Start(
|
||||
BytesStart::new("D:multistatus").with_attributes([
|
||||
("xmlns:D", "DAV:"),
|
||||
("xmlns:C", "urn:ietf:params:xml:ns:caldav"),
|
||||
("xmlns:CS", "http://calendarserver.org/ns/"),
|
||||
]),
|
||||
))?;
|
||||
|
||||
// Write the root /caldav/ response with discovery properties
|
||||
Self::write_root_response(&mut xml_writer, request, base_href, username)?;
|
||||
|
||||
// Add responses for calendars
|
||||
for calendar in calendars {
|
||||
Self::write_calendar_response(
|
||||
&mut xml_writer,
|
||||
calendar,
|
||||
request,
|
||||
&format!("{}{}/", base_href, calendar.id),
|
||||
)?;
|
||||
}
|
||||
|
||||
// End multistatus
|
||||
xml_writer.write_event(Event::End(BytesEnd::new("D:multistatus")))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Generate a PROPFIND response for calendars (without root discovery entry)
|
||||
pub fn generate_calendars_propfind_response<W: Write>(
|
||||
writer: W,
|
||||
calendars: &[CalendarDto],
|
||||
@@ -233,6 +270,301 @@ impl CalDavAdapter {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Generate a PROPFIND response for a user principal resource.
|
||||
pub fn generate_principal_propfind_response<W: Write>(
|
||||
writer: W,
|
||||
request: &PropFindRequest,
|
||||
username: &str,
|
||||
) -> Result<()> {
|
||||
let mut xml_writer = Writer::new(writer);
|
||||
|
||||
xml_writer.write_event(Event::Start(
|
||||
BytesStart::new("D:multistatus").with_attributes([
|
||||
("xmlns:D", "DAV:"),
|
||||
("xmlns:C", "urn:ietf:params:xml:ns:caldav"),
|
||||
("xmlns:CS", "http://calendarserver.org/ns/"),
|
||||
]),
|
||||
))?;
|
||||
|
||||
xml_writer.write_event(Event::Start(BytesStart::new("D:response")))?;
|
||||
|
||||
// href
|
||||
let href = format!("/caldav/principals/{}/", username);
|
||||
xml_writer.write_event(Event::Start(BytesStart::new("D:href")))?;
|
||||
xml_writer.write_event(Event::Text(BytesText::new(&href)))?;
|
||||
xml_writer.write_event(Event::End(BytesEnd::new("D:href")))?;
|
||||
|
||||
xml_writer.write_event(Event::Start(BytesStart::new("D:propstat")))?;
|
||||
xml_writer.write_event(Event::Start(BytesStart::new("D:prop")))?;
|
||||
|
||||
match &request.prop_find_type {
|
||||
PropFindType::AllProp | PropFindType::PropName => {
|
||||
Self::write_principal_props(&mut xml_writer, username)?;
|
||||
}
|
||||
PropFindType::Prop(props) => {
|
||||
Self::write_principal_requested_props(&mut xml_writer, username, props)?;
|
||||
}
|
||||
}
|
||||
|
||||
xml_writer.write_event(Event::End(BytesEnd::new("D:prop")))?;
|
||||
|
||||
xml_writer.write_event(Event::Start(BytesStart::new("D:status")))?;
|
||||
xml_writer.write_event(Event::Text(BytesText::new("HTTP/1.1 200 OK")))?;
|
||||
xml_writer.write_event(Event::End(BytesEnd::new("D:status")))?;
|
||||
|
||||
xml_writer.write_event(Event::End(BytesEnd::new("D:propstat")))?;
|
||||
xml_writer.write_event(Event::End(BytesEnd::new("D:response")))?;
|
||||
xml_writer.write_event(Event::End(BytesEnd::new("D:multistatus")))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Write the root /caldav/ response entry with discovery properties.
|
||||
fn write_root_response<W: Write>(
|
||||
xml_writer: &mut Writer<W>,
|
||||
request: &PropFindRequest,
|
||||
href: &str,
|
||||
username: &str,
|
||||
) -> Result<()> {
|
||||
xml_writer.write_event(Event::Start(BytesStart::new("D:response")))?;
|
||||
|
||||
xml_writer.write_event(Event::Start(BytesStart::new("D:href")))?;
|
||||
xml_writer.write_event(Event::Text(BytesText::new(href)))?;
|
||||
xml_writer.write_event(Event::End(BytesEnd::new("D:href")))?;
|
||||
|
||||
xml_writer.write_event(Event::Start(BytesStart::new("D:propstat")))?;
|
||||
xml_writer.write_event(Event::Start(BytesStart::new("D:prop")))?;
|
||||
|
||||
match &request.prop_find_type {
|
||||
PropFindType::AllProp => {
|
||||
// Resource type — collection
|
||||
xml_writer.write_event(Event::Start(BytesStart::new("D:resourcetype")))?;
|
||||
xml_writer.write_event(Event::Empty(BytesStart::new("D:collection")))?;
|
||||
xml_writer.write_event(Event::End(BytesEnd::new("D:resourcetype")))?;
|
||||
|
||||
// current-user-principal
|
||||
xml_writer
|
||||
.write_event(Event::Start(BytesStart::new("D:current-user-principal")))?;
|
||||
xml_writer.write_event(Event::Start(BytesStart::new("D:href")))?;
|
||||
xml_writer.write_event(Event::Text(BytesText::new(&format!(
|
||||
"/caldav/principals/{}/",
|
||||
username
|
||||
))))?;
|
||||
xml_writer.write_event(Event::End(BytesEnd::new("D:href")))?;
|
||||
xml_writer
|
||||
.write_event(Event::End(BytesEnd::new("D:current-user-principal")))?;
|
||||
|
||||
// calendar-home-set
|
||||
xml_writer
|
||||
.write_event(Event::Start(BytesStart::new("C:calendar-home-set")))?;
|
||||
xml_writer.write_event(Event::Start(BytesStart::new("D:href")))?;
|
||||
xml_writer.write_event(Event::Text(BytesText::new(&format!(
|
||||
"/caldav/{}/",
|
||||
username
|
||||
))))?;
|
||||
xml_writer.write_event(Event::End(BytesEnd::new("D:href")))?;
|
||||
xml_writer
|
||||
.write_event(Event::End(BytesEnd::new("C:calendar-home-set")))?;
|
||||
}
|
||||
PropFindType::PropName => {
|
||||
xml_writer.write_event(Event::Empty(BytesStart::new("D:resourcetype")))?;
|
||||
xml_writer
|
||||
.write_event(Event::Empty(BytesStart::new("D:current-user-principal")))?;
|
||||
xml_writer
|
||||
.write_event(Event::Empty(BytesStart::new("C:calendar-home-set")))?;
|
||||
}
|
||||
PropFindType::Prop(props) => {
|
||||
Self::write_root_requested_props(xml_writer, username, props)?;
|
||||
}
|
||||
}
|
||||
|
||||
xml_writer.write_event(Event::End(BytesEnd::new("D:prop")))?;
|
||||
|
||||
xml_writer.write_event(Event::Start(BytesStart::new("D:status")))?;
|
||||
xml_writer.write_event(Event::Text(BytesText::new("HTTP/1.1 200 OK")))?;
|
||||
xml_writer.write_event(Event::End(BytesEnd::new("D:status")))?;
|
||||
|
||||
xml_writer.write_event(Event::End(BytesEnd::new("D:propstat")))?;
|
||||
xml_writer.write_event(Event::End(BytesEnd::new("D:response")))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Write requested properties for the root /caldav/ resource.
|
||||
fn write_root_requested_props<W: Write>(
|
||||
xml_writer: &mut Writer<W>,
|
||||
username: &str,
|
||||
props: &[QualifiedName],
|
||||
) -> Result<()> {
|
||||
for prop in props {
|
||||
match (prop.namespace.as_str(), prop.name.as_str()) {
|
||||
("DAV:", "resourcetype") => {
|
||||
xml_writer.write_event(Event::Start(BytesStart::new("D:resourcetype")))?;
|
||||
xml_writer.write_event(Event::Empty(BytesStart::new("D:collection")))?;
|
||||
xml_writer.write_event(Event::End(BytesEnd::new("D:resourcetype")))?;
|
||||
}
|
||||
("DAV:", "current-user-principal") => {
|
||||
xml_writer.write_event(Event::Start(BytesStart::new(
|
||||
"D:current-user-principal",
|
||||
)))?;
|
||||
xml_writer.write_event(Event::Start(BytesStart::new("D:href")))?;
|
||||
xml_writer.write_event(Event::Text(BytesText::new(&format!(
|
||||
"/caldav/principals/{}/",
|
||||
username
|
||||
))))?;
|
||||
xml_writer.write_event(Event::End(BytesEnd::new("D:href")))?;
|
||||
xml_writer
|
||||
.write_event(Event::End(BytesEnd::new("D:current-user-principal")))?;
|
||||
}
|
||||
("urn:ietf:params:xml:ns:caldav", "calendar-home-set") => {
|
||||
xml_writer
|
||||
.write_event(Event::Start(BytesStart::new("C:calendar-home-set")))?;
|
||||
xml_writer.write_event(Event::Start(BytesStart::new("D:href")))?;
|
||||
xml_writer.write_event(Event::Text(BytesText::new(&format!(
|
||||
"/caldav/{}/",
|
||||
username
|
||||
))))?;
|
||||
xml_writer.write_event(Event::End(BytesEnd::new("D:href")))?;
|
||||
xml_writer
|
||||
.write_event(Event::End(BytesEnd::new("C:calendar-home-set")))?;
|
||||
}
|
||||
("DAV:", "displayname") => {
|
||||
xml_writer.write_event(Event::Start(BytesStart::new("D:displayname")))?;
|
||||
xml_writer.write_event(Event::Text(BytesText::new("CalDAV Root")))?;
|
||||
xml_writer.write_event(Event::End(BytesEnd::new("D:displayname")))?;
|
||||
}
|
||||
_ => {
|
||||
// Unknown property — write empty
|
||||
let prop_name = if prop.namespace == "http://calendarserver.org/ns/" {
|
||||
format!("CS:{}", prop.name)
|
||||
} else if prop.namespace == "urn:ietf:params:xml:ns:caldav" {
|
||||
format!("C:{}", prop.name)
|
||||
} else if prop.namespace == "DAV:" {
|
||||
format!("D:{}", prop.name)
|
||||
} else {
|
||||
format!("{}:{}", prop.namespace, prop.name)
|
||||
};
|
||||
xml_writer.write_event(Event::Empty(BytesStart::new(&prop_name)))?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Write standard properties for a principal resource.
|
||||
fn write_principal_props<W: Write>(
|
||||
xml_writer: &mut Writer<W>,
|
||||
username: &str,
|
||||
) -> Result<()> {
|
||||
// resourcetype — principal
|
||||
xml_writer.write_event(Event::Start(BytesStart::new("D:resourcetype")))?;
|
||||
xml_writer.write_event(Event::Empty(BytesStart::new("D:collection")))?;
|
||||
xml_writer.write_event(Event::Empty(BytesStart::new("D:principal")))?;
|
||||
xml_writer.write_event(Event::End(BytesEnd::new("D:resourcetype")))?;
|
||||
|
||||
// displayname
|
||||
xml_writer.write_event(Event::Start(BytesStart::new("D:displayname")))?;
|
||||
xml_writer.write_event(Event::Text(BytesText::new(username)))?;
|
||||
xml_writer.write_event(Event::End(BytesEnd::new("D:displayname")))?;
|
||||
|
||||
// calendar-home-set
|
||||
xml_writer.write_event(Event::Start(BytesStart::new("C:calendar-home-set")))?;
|
||||
xml_writer.write_event(Event::Start(BytesStart::new("D:href")))?;
|
||||
xml_writer.write_event(Event::Text(BytesText::new(&format!(
|
||||
"/caldav/{}/",
|
||||
username
|
||||
))))?;
|
||||
xml_writer.write_event(Event::End(BytesEnd::new("D:href")))?;
|
||||
xml_writer.write_event(Event::End(BytesEnd::new("C:calendar-home-set")))?;
|
||||
|
||||
// current-user-principal (self-reference)
|
||||
xml_writer.write_event(Event::Start(BytesStart::new("D:current-user-principal")))?;
|
||||
xml_writer.write_event(Event::Start(BytesStart::new("D:href")))?;
|
||||
xml_writer.write_event(Event::Text(BytesText::new(&format!(
|
||||
"/caldav/principals/{}/",
|
||||
username
|
||||
))))?;
|
||||
xml_writer.write_event(Event::End(BytesEnd::new("D:href")))?;
|
||||
xml_writer.write_event(Event::End(BytesEnd::new("D:current-user-principal")))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Write requested properties for a principal resource.
|
||||
fn write_principal_requested_props<W: Write>(
|
||||
xml_writer: &mut Writer<W>,
|
||||
username: &str,
|
||||
props: &[QualifiedName],
|
||||
) -> Result<()> {
|
||||
for prop in props {
|
||||
match (prop.namespace.as_str(), prop.name.as_str()) {
|
||||
("DAV:", "resourcetype") => {
|
||||
xml_writer.write_event(Event::Start(BytesStart::new("D:resourcetype")))?;
|
||||
xml_writer.write_event(Event::Empty(BytesStart::new("D:collection")))?;
|
||||
xml_writer.write_event(Event::Empty(BytesStart::new("D:principal")))?;
|
||||
xml_writer.write_event(Event::End(BytesEnd::new("D:resourcetype")))?;
|
||||
}
|
||||
("DAV:", "displayname") => {
|
||||
xml_writer.write_event(Event::Start(BytesStart::new("D:displayname")))?;
|
||||
xml_writer.write_event(Event::Text(BytesText::new(username)))?;
|
||||
xml_writer.write_event(Event::End(BytesEnd::new("D:displayname")))?;
|
||||
}
|
||||
("DAV:", "current-user-principal") => {
|
||||
xml_writer.write_event(Event::Start(BytesStart::new(
|
||||
"D:current-user-principal",
|
||||
)))?;
|
||||
xml_writer.write_event(Event::Start(BytesStart::new("D:href")))?;
|
||||
xml_writer.write_event(Event::Text(BytesText::new(&format!(
|
||||
"/caldav/principals/{}/",
|
||||
username
|
||||
))))?;
|
||||
xml_writer.write_event(Event::End(BytesEnd::new("D:href")))?;
|
||||
xml_writer
|
||||
.write_event(Event::End(BytesEnd::new("D:current-user-principal")))?;
|
||||
}
|
||||
("urn:ietf:params:xml:ns:caldav", "calendar-home-set") => {
|
||||
xml_writer
|
||||
.write_event(Event::Start(BytesStart::new("C:calendar-home-set")))?;
|
||||
xml_writer.write_event(Event::Start(BytesStart::new("D:href")))?;
|
||||
xml_writer.write_event(Event::Text(BytesText::new(&format!(
|
||||
"/caldav/{}/",
|
||||
username
|
||||
))))?;
|
||||
xml_writer.write_event(Event::End(BytesEnd::new("D:href")))?;
|
||||
xml_writer
|
||||
.write_event(Event::End(BytesEnd::new("C:calendar-home-set")))?;
|
||||
}
|
||||
("urn:ietf:params:xml:ns:caldav", "calendar-user-address-set") => {
|
||||
xml_writer.write_event(Event::Start(BytesStart::new(
|
||||
"C:calendar-user-address-set",
|
||||
)))?;
|
||||
xml_writer.write_event(Event::Start(BytesStart::new("D:href")))?;
|
||||
xml_writer.write_event(Event::Text(BytesText::new(&format!(
|
||||
"/caldav/principals/{}/",
|
||||
username
|
||||
))))?;
|
||||
xml_writer.write_event(Event::End(BytesEnd::new("D:href")))?;
|
||||
xml_writer.write_event(Event::End(BytesEnd::new(
|
||||
"C:calendar-user-address-set",
|
||||
)))?;
|
||||
}
|
||||
_ => {
|
||||
let prop_name = if prop.namespace == "http://calendarserver.org/ns/" {
|
||||
format!("CS:{}", prop.name)
|
||||
} else if prop.namespace == "urn:ietf:params:xml:ns:caldav" {
|
||||
format!("C:{}", prop.name)
|
||||
} else if prop.namespace == "DAV:" {
|
||||
format!("D:{}", prop.name)
|
||||
} else {
|
||||
format!("{}:{}", prop.namespace, prop.name)
|
||||
};
|
||||
xml_writer.write_event(Event::Empty(BytesStart::new(&prop_name)))?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Write calendar properties as a response
|
||||
fn write_calendar_response<W: Write>(
|
||||
xml_writer: &mut Writer<W>,
|
||||
|
||||
@@ -336,4 +336,284 @@ mod tests {
|
||||
"Should have multistatus even for empty"
|
||||
);
|
||||
}
|
||||
|
||||
// ==============================================
|
||||
// Namespace resolution tests (issue #153 fixes)
|
||||
// ==============================================
|
||||
|
||||
#[test]
|
||||
fn test_propfind_namespace_resolution_dav_and_caldav() {
|
||||
use crate::application::adapters::webdav_adapter::WebDavAdapter;
|
||||
|
||||
// Simulates a real CalDAV client PROPFIND request with namespace prefixes
|
||||
let xml = r#"<?xml version="1.0" encoding="utf-8"?>
|
||||
<D:propfind xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">
|
||||
<D:prop>
|
||||
<D:current-user-principal/>
|
||||
<C:calendar-home-set/>
|
||||
<D:resourcetype/>
|
||||
</D:prop>
|
||||
</D:propfind>"#;
|
||||
|
||||
let result = WebDavAdapter::parse_propfind(Cursor::new(xml));
|
||||
assert!(result.is_ok(), "Failed to parse PROPFIND: {:?}", result.err());
|
||||
|
||||
let request = result.unwrap();
|
||||
match request.prop_find_type {
|
||||
PropFindType::Prop(ref props) => {
|
||||
assert_eq!(props.len(), 3);
|
||||
|
||||
// Verify namespace URIs are resolved, not prefix names
|
||||
assert_eq!(props[0].namespace, "DAV:");
|
||||
assert_eq!(props[0].name, "current-user-principal");
|
||||
|
||||
assert_eq!(props[1].namespace, "urn:ietf:params:xml:ns:caldav");
|
||||
assert_eq!(props[1].name, "calendar-home-set");
|
||||
|
||||
assert_eq!(props[2].namespace, "DAV:");
|
||||
assert_eq!(props[2].name, "resourcetype");
|
||||
}
|
||||
other => panic!("Expected Prop, got {:?}", other),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_propfind_namespace_resolution_with_custom_prefixes() {
|
||||
use crate::application::adapters::webdav_adapter::WebDavAdapter;
|
||||
|
||||
// Some clients use different prefix names
|
||||
let xml = r#"<?xml version="1.0" encoding="utf-8"?>
|
||||
<A:propfind xmlns:A="DAV:" xmlns:B="urn:ietf:params:xml:ns:caldav">
|
||||
<A:prop>
|
||||
<A:current-user-principal/>
|
||||
<B:calendar-home-set/>
|
||||
</A:prop>
|
||||
</A:propfind>"#;
|
||||
|
||||
let result = WebDavAdapter::parse_propfind(Cursor::new(xml));
|
||||
assert!(result.is_ok());
|
||||
|
||||
let request = result.unwrap();
|
||||
match request.prop_find_type {
|
||||
PropFindType::Prop(ref props) => {
|
||||
assert_eq!(props.len(), 2);
|
||||
assert_eq!(props[0].namespace, "DAV:");
|
||||
assert_eq!(props[0].name, "current-user-principal");
|
||||
assert_eq!(props[1].namespace, "urn:ietf:params:xml:ns:caldav");
|
||||
assert_eq!(props[1].name, "calendar-home-set");
|
||||
}
|
||||
other => panic!("Expected Prop, got {:?}", other),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_root_propfind_response_has_discovery_properties() {
|
||||
// Depth 0: only root entry, no calendars (simulates initial discovery)
|
||||
let calendars: Vec<CalendarDto> = vec![];
|
||||
let request = PropFindRequest {
|
||||
prop_find_type: PropFindType::Prop(vec![
|
||||
QualifiedName {
|
||||
namespace: "DAV:".to_string(),
|
||||
name: "current-user-principal".to_string(),
|
||||
},
|
||||
QualifiedName {
|
||||
namespace: "urn:ietf:params:xml:ns:caldav".to_string(),
|
||||
name: "calendar-home-set".to_string(),
|
||||
},
|
||||
]),
|
||||
};
|
||||
|
||||
let mut output = Vec::new();
|
||||
let result = CalDavAdapter::generate_root_propfind_response(
|
||||
&mut output,
|
||||
&calendars,
|
||||
&request,
|
||||
"/caldav/",
|
||||
"testuser",
|
||||
);
|
||||
assert!(result.is_ok(), "Failed to generate root propfind: {:?}", result.err());
|
||||
|
||||
let xml_str = String::from_utf8(output).expect("Invalid UTF-8");
|
||||
|
||||
// Root response should contain populated current-user-principal
|
||||
assert!(
|
||||
xml_str.contains("/caldav/principals/testuser/"),
|
||||
"Should contain principal href, got: {}",
|
||||
xml_str
|
||||
);
|
||||
|
||||
// Root response should contain populated calendar-home-set
|
||||
assert!(
|
||||
xml_str.contains("/caldav/testuser/"),
|
||||
"Should contain calendar home href, got: {}",
|
||||
xml_str
|
||||
);
|
||||
|
||||
// Properties should NOT be empty self-closing elements
|
||||
assert!(
|
||||
!xml_str.contains("<D:current-user-principal/>"),
|
||||
"current-user-principal should not be empty"
|
||||
);
|
||||
assert!(
|
||||
!xml_str.contains("<C:calendar-home-set/>"),
|
||||
"calendar-home-set should not be empty"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_root_propfind_response_depth1_includes_calendars() {
|
||||
// Depth 1: root entry + calendars
|
||||
let calendars = vec![sample_calendar()];
|
||||
let request = PropFindRequest {
|
||||
prop_find_type: PropFindType::AllProp,
|
||||
};
|
||||
|
||||
let mut output = Vec::new();
|
||||
let result = CalDavAdapter::generate_root_propfind_response(
|
||||
&mut output,
|
||||
&calendars,
|
||||
&request,
|
||||
"/caldav/",
|
||||
"testuser",
|
||||
);
|
||||
assert!(result.is_ok());
|
||||
|
||||
let xml_str = String::from_utf8(output).expect("Invalid UTF-8");
|
||||
|
||||
// Should contain root entry with discovery properties
|
||||
assert!(xml_str.contains("/caldav/principals/testuser/"));
|
||||
// Should contain calendar entry
|
||||
assert!(xml_str.contains("cal-001"));
|
||||
assert!(xml_str.contains("Personal"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_principal_propfind_response() {
|
||||
let request = PropFindRequest {
|
||||
prop_find_type: PropFindType::Prop(vec![
|
||||
QualifiedName {
|
||||
namespace: "DAV:".to_string(),
|
||||
name: "resourcetype".to_string(),
|
||||
},
|
||||
QualifiedName {
|
||||
namespace: "DAV:".to_string(),
|
||||
name: "displayname".to_string(),
|
||||
},
|
||||
QualifiedName {
|
||||
namespace: "urn:ietf:params:xml:ns:caldav".to_string(),
|
||||
name: "calendar-home-set".to_string(),
|
||||
},
|
||||
QualifiedName {
|
||||
namespace: "DAV:".to_string(),
|
||||
name: "current-user-principal".to_string(),
|
||||
},
|
||||
]),
|
||||
};
|
||||
|
||||
let mut output = Vec::new();
|
||||
let result = CalDavAdapter::generate_principal_propfind_response(
|
||||
&mut output,
|
||||
&request,
|
||||
"testuser",
|
||||
);
|
||||
assert!(result.is_ok(), "Failed: {:?}", result.err());
|
||||
|
||||
let xml_str = String::from_utf8(output).expect("Invalid UTF-8");
|
||||
|
||||
// Should contain principal href
|
||||
assert!(xml_str.contains("/caldav/principals/testuser/"));
|
||||
// Should contain calendar-home-set
|
||||
assert!(xml_str.contains("/caldav/testuser/"));
|
||||
// Should contain principal in resourcetype
|
||||
assert!(xml_str.contains("D:principal"));
|
||||
// Should have displayname
|
||||
assert!(xml_str.contains("testuser"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_calendar_propfind_with_resolved_namespaces() {
|
||||
// Simulate what happens when a client asks for specific props
|
||||
// After namespace resolution fix, these should have proper URIs
|
||||
let calendar = sample_calendar();
|
||||
let request = PropFindRequest {
|
||||
prop_find_type: PropFindType::Prop(vec![
|
||||
QualifiedName {
|
||||
namespace: "DAV:".to_string(),
|
||||
name: "resourcetype".to_string(),
|
||||
},
|
||||
QualifiedName {
|
||||
namespace: "DAV:".to_string(),
|
||||
name: "displayname".to_string(),
|
||||
},
|
||||
QualifiedName {
|
||||
namespace: "urn:ietf:params:xml:ns:caldav".to_string(),
|
||||
name: "supported-calendar-component-set".to_string(),
|
||||
},
|
||||
]),
|
||||
};
|
||||
|
||||
let mut output = Vec::new();
|
||||
let result = CalDavAdapter::generate_calendar_collection_propfind(
|
||||
&mut output,
|
||||
&calendar,
|
||||
&[],
|
||||
&request,
|
||||
"/caldav/cal-001/",
|
||||
"0",
|
||||
);
|
||||
assert!(result.is_ok(), "Failed: {:?}", result.err());
|
||||
|
||||
let xml_str = String::from_utf8(output).expect("Invalid UTF-8");
|
||||
|
||||
// resourcetype should have collection + calendar children
|
||||
assert!(
|
||||
xml_str.contains("D:collection"),
|
||||
"Should contain collection in resourcetype"
|
||||
);
|
||||
assert!(
|
||||
xml_str.contains("C:calendar"),
|
||||
"Should contain calendar in resourcetype"
|
||||
);
|
||||
|
||||
// displayname should be populated
|
||||
assert!(
|
||||
xml_str.contains("Personal"),
|
||||
"Should contain calendar name"
|
||||
);
|
||||
|
||||
// supported-calendar-component-set should have VEVENT
|
||||
assert!(
|
||||
xml_str.contains("VEVENT"),
|
||||
"Should contain VEVENT component"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_report_namespace_resolution() {
|
||||
// Verify that REPORT parsing also resolves namespaces correctly
|
||||
let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
|
||||
<C:calendar-query xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">
|
||||
<D:prop>
|
||||
<D:getetag/>
|
||||
<C:calendar-data/>
|
||||
</D:prop>
|
||||
<C:filter>
|
||||
<C:comp-filter name="VCALENDAR"/>
|
||||
</C:filter>
|
||||
</C:calendar-query>"#;
|
||||
|
||||
let result = CalDavAdapter::parse_report(Cursor::new(xml));
|
||||
assert!(result.is_ok(), "Failed to parse: {:?}", result.err());
|
||||
|
||||
match result.unwrap() {
|
||||
CalDavReportType::CalendarQuery { props, .. } => {
|
||||
assert_eq!(props.len(), 2);
|
||||
assert_eq!(props[0].namespace, "DAV:");
|
||||
assert_eq!(props[0].name, "getetag");
|
||||
assert_eq!(props[1].namespace, "urn:ietf:params:xml:ns:caldav");
|
||||
assert_eq!(props[1].name, "calendar-data");
|
||||
}
|
||||
other => panic!("Expected CalendarQuery, got {:?}", other),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,10 +53,12 @@ impl CardDavAdapter {
|
||||
let mut sync_token = String::new();
|
||||
let mut in_href = false;
|
||||
let mut in_sync_token = false;
|
||||
let mut ns_map = std::collections::HashMap::<String, String>::new();
|
||||
|
||||
loop {
|
||||
match xml_reader.read_event_into(&mut buffer) {
|
||||
Ok(Event::Start(ref e)) => {
|
||||
WebDavAdapter::collect_ns_decls(e, &mut ns_map);
|
||||
let name = e.name();
|
||||
let name_str = std::str::from_utf8(name.as_ref()).unwrap_or("");
|
||||
|
||||
@@ -78,9 +80,8 @@ impl CardDavAdapter {
|
||||
in_sync_token = true
|
||||
}
|
||||
_ if in_prop => {
|
||||
let namespace = WebDavAdapter::extract_namespace(name_str);
|
||||
let prop_name = WebDavAdapter::extract_local_name(name_str);
|
||||
props.push(QualifiedName::new(namespace, prop_name));
|
||||
let qname = WebDavAdapter::resolve_name(name_str, &ns_map);
|
||||
props.push(qname);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
@@ -107,11 +108,11 @@ impl CardDavAdapter {
|
||||
}
|
||||
}
|
||||
Ok(Event::Empty(ref e)) if in_prop => {
|
||||
WebDavAdapter::collect_ns_decls(e, &mut ns_map);
|
||||
let name = e.name();
|
||||
let name_str = std::str::from_utf8(name.as_ref()).unwrap_or("");
|
||||
let namespace = WebDavAdapter::extract_namespace(name_str);
|
||||
let prop_name = WebDavAdapter::extract_local_name(name_str);
|
||||
props.push(QualifiedName::new(namespace, prop_name));
|
||||
let qname = WebDavAdapter::resolve_name(name_str, &ns_map);
|
||||
props.push(qname);
|
||||
}
|
||||
Ok(Event::Eof) => break,
|
||||
Err(e) => return Err(WebDavError::XmlError(e)),
|
||||
|
||||
@@ -124,6 +124,41 @@ pub enum LockType {
|
||||
pub struct WebDavAdapter;
|
||||
|
||||
impl WebDavAdapter {
|
||||
/// Collect namespace prefix → URI mappings from element attributes.
|
||||
/// E.g. `xmlns:D="DAV:"` maps prefix `"D"` to `"DAV:"`.
|
||||
pub fn collect_ns_decls(
|
||||
e: &BytesStart,
|
||||
ns_map: &mut std::collections::HashMap<String, String>,
|
||||
) {
|
||||
for attr in e.attributes().flatten() {
|
||||
let key = std::str::from_utf8(attr.key.as_ref()).unwrap_or("");
|
||||
if let Some(prefix) = key.strip_prefix("xmlns:") {
|
||||
let uri = attr.unescape_value().unwrap_or_default().to_string();
|
||||
ns_map.insert(prefix.to_string(), uri);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolve a prefixed element name (e.g. `D:resourcetype`) to a
|
||||
/// `QualifiedName` using the accumulated namespace declarations.
|
||||
pub fn resolve_name(
|
||||
name_str: &str,
|
||||
ns_map: &std::collections::HashMap<String, String>,
|
||||
) -> QualifiedName {
|
||||
if let Some(idx) = name_str.find(':') {
|
||||
let prefix = &name_str[..idx];
|
||||
let local = &name_str[idx + 1..];
|
||||
if let Some(uri) = ns_map.get(prefix) {
|
||||
return QualifiedName::new(uri.clone(), local.to_string());
|
||||
}
|
||||
}
|
||||
// Fallback: no prefix or unknown prefix → use legacy extraction
|
||||
QualifiedName::new(
|
||||
Self::extract_namespace(name_str),
|
||||
Self::extract_local_name(name_str),
|
||||
)
|
||||
}
|
||||
|
||||
/// Parse a PROPFIND XML request
|
||||
pub fn parse_propfind<R: Read>(reader: R) -> Result<PropFindRequest> {
|
||||
let mut xml_reader = Reader::from_reader(BufReader::new(reader));
|
||||
@@ -135,10 +170,12 @@ impl WebDavAdapter {
|
||||
let mut in_allprop = false;
|
||||
let mut in_propname = false;
|
||||
let mut props = Vec::new();
|
||||
let mut ns_map = std::collections::HashMap::<String, String>::new();
|
||||
|
||||
loop {
|
||||
match xml_reader.read_event_into(&mut buffer) {
|
||||
Ok(Event::Start(ref e)) => {
|
||||
Self::collect_ns_decls(e, &mut ns_map);
|
||||
let name = e.name();
|
||||
let name_str = std::str::from_utf8(name.as_ref()).unwrap_or("");
|
||||
|
||||
@@ -155,11 +192,8 @@ impl WebDavAdapter {
|
||||
{
|
||||
in_propname = true;
|
||||
} else if in_prop {
|
||||
// Add property to request
|
||||
let namespace = Self::extract_namespace(name_str);
|
||||
let prop_name = Self::extract_local_name(name_str);
|
||||
|
||||
props.push(QualifiedName::new(namespace, prop_name));
|
||||
let qname = Self::resolve_name(name_str, &ns_map);
|
||||
props.push(qname);
|
||||
}
|
||||
}
|
||||
Ok(Event::End(ref e)) => {
|
||||
@@ -177,6 +211,7 @@ impl WebDavAdapter {
|
||||
}
|
||||
}
|
||||
Ok(Event::Empty(ref e)) => {
|
||||
Self::collect_ns_decls(e, &mut ns_map);
|
||||
let name = e.name();
|
||||
let name_str = std::str::from_utf8(name.as_ref()).unwrap_or("");
|
||||
|
||||
@@ -187,11 +222,8 @@ impl WebDavAdapter {
|
||||
{
|
||||
in_propname = true;
|
||||
} else if in_prop {
|
||||
// Add property to request (empty element)
|
||||
let namespace = Self::extract_namespace(name_str);
|
||||
let prop_name = Self::extract_local_name(name_str);
|
||||
|
||||
props.push(QualifiedName::new(namespace, prop_name));
|
||||
let qname = Self::resolve_name(name_str, &ns_map);
|
||||
props.push(qname);
|
||||
}
|
||||
}
|
||||
Ok(Event::Eof) => break,
|
||||
@@ -635,10 +667,12 @@ impl WebDavAdapter {
|
||||
let mut props_to_set = Vec::new();
|
||||
let mut props_to_remove = Vec::new();
|
||||
let mut current_text = String::new();
|
||||
let mut ns_map = std::collections::HashMap::<String, String>::new();
|
||||
|
||||
loop {
|
||||
match xml_reader.read_event_into(&mut buffer) {
|
||||
Ok(Event::Start(ref e)) => {
|
||||
Self::collect_ns_decls(e, &mut ns_map);
|
||||
let name = e.name();
|
||||
let name_str = std::str::from_utf8(name.as_ref()).unwrap_or("");
|
||||
|
||||
@@ -656,11 +690,7 @@ impl WebDavAdapter {
|
||||
in_prop = true
|
||||
}
|
||||
_ if in_prop => {
|
||||
// This is a property element
|
||||
let namespace = Self::extract_namespace(name_str);
|
||||
let prop_name = Self::extract_local_name(name_str);
|
||||
|
||||
current_prop = Some(QualifiedName::new(namespace, prop_name));
|
||||
current_prop = Some(Self::resolve_name(name_str, &ns_map));
|
||||
current_text.clear();
|
||||
}
|
||||
_ => (),
|
||||
@@ -704,15 +734,12 @@ impl WebDavAdapter {
|
||||
}
|
||||
}
|
||||
Ok(Event::Empty(ref e)) => {
|
||||
Self::collect_ns_decls(e, &mut ns_map);
|
||||
let name = e.name();
|
||||
let name_str = std::str::from_utf8(name.as_ref()).unwrap_or("");
|
||||
|
||||
if in_prop {
|
||||
// Empty property element
|
||||
let namespace = Self::extract_namespace(name_str);
|
||||
let prop_name = Self::extract_local_name(name_str);
|
||||
|
||||
let qname = QualifiedName::new(namespace, prop_name);
|
||||
let qname = Self::resolve_name(name_str, &ns_map);
|
||||
|
||||
if in_set {
|
||||
props_to_set.push(PropValue {
|
||||
|
||||
Reference in New Issue
Block a user