Allow multiple <D:set> and <D:remove> elements and consider order

This commit is contained in:
Unrud 2020-09-26 22:08:22 +02:00
parent 1fe011020b
commit 10dafde32d
4 changed files with 40 additions and 35 deletions

View File

@ -43,6 +43,7 @@ class ApplicationMkcalendarMixin:
return httputils.REQUEST_TIMEOUT return httputils.REQUEST_TIMEOUT
# Prepare before locking # Prepare before locking
props = xmlutils.props_from_request(xml_content) props = xmlutils.props_from_request(xml_content)
props = {k: v for k, v in props.items() if v is not None}
props["tag"] = "VCALENDAR" props["tag"] = "VCALENDAR"
# TODO: use this? # TODO: use this?
# timezone = props.get("C:calendar-timezone") # timezone = props.get("C:calendar-timezone")

View File

@ -44,6 +44,7 @@ class ApplicationMkcolMixin:
return httputils.REQUEST_TIMEOUT return httputils.REQUEST_TIMEOUT
# Prepare before locking # Prepare before locking
props = xmlutils.props_from_request(xml_content) props = xmlutils.props_from_request(xml_content)
props = {k: v for k, v in props.items() if v is not None}
try: try:
radicale_item.check_and_sanitize_props(props) radicale_item.check_and_sanitize_props(props)
except ValueError as e: except ValueError as e:

View File

@ -17,6 +17,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with Radicale. If not, see <http://www.gnu.org/licenses/>. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
import contextlib
import socket import socket
from http import client from http import client
from xml.etree import ElementTree as ET from xml.etree import ElementTree as ET
@ -33,18 +34,12 @@ def xml_proppatch(base_prefix, path, xml_request, collection):
Read rfc4918-9.2 for info. Read rfc4918-9.2 for info.
""" """
props_to_set = xmlutils.props_from_request(xml_request, actions=("set",))
props_to_remove = xmlutils.props_from_request(xml_request,
actions=("remove",))
multistatus = ET.Element(xmlutils.make_clark("D:multistatus")) multistatus = ET.Element(xmlutils.make_clark("D:multistatus"))
response = ET.Element(xmlutils.make_clark("D:response")) response = ET.Element(xmlutils.make_clark("D:response"))
multistatus.append(response) multistatus.append(response)
href = ET.Element(xmlutils.make_clark("D:href")) href = ET.Element(xmlutils.make_clark("D:href"))
href.text = xmlutils.make_href(base_prefix, path) href.text = xmlutils.make_href(base_prefix, path)
response.append(href) response.append(href)
# Create D:propstat element for props with status 200 OK # Create D:propstat element for props with status 200 OK
propstat = ET.Element(xmlutils.make_clark("D:propstat")) propstat = ET.Element(xmlutils.make_clark("D:propstat"))
status = ET.Element(xmlutils.make_clark("D:status")) status = ET.Element(xmlutils.make_clark("D:status"))
@ -55,14 +50,12 @@ def xml_proppatch(base_prefix, path, xml_request, collection):
response.append(propstat) response.append(propstat)
new_props = collection.get_meta() new_props = collection.get_meta()
for short_name, value in props_to_set.items(): for short_name, value in xmlutils.props_from_request(xml_request).items():
new_props[short_name] = value if value is None:
props_ok.append(ET.Element(xmlutils.make_clark(short_name))) with contextlib.suppress(KeyError):
for short_name in props_to_remove: del new_props[short_name]
try: else:
del new_props[short_name] new_props[short_name] = value
except KeyError:
pass
props_ok.append(ET.Element(xmlutils.make_clark(short_name))) props_ok.append(ET.Element(xmlutils.make_clark(short_name)))
radicale_item.check_and_sanitize_props(new_props) radicale_item.check_and_sanitize_props(new_props)
collection.set_meta(new_props) collection.set_meta(new_props)

View File

@ -146,36 +146,46 @@ def get_content_type(item, encoding):
return content_type return content_type
def props_from_request(xml_request, actions=("set", "remove")): def props_from_request(xml_request):
"""Return a list of properties as a dictionary.""" """Return a list of properties as a dictionary.
Properties that should be removed are set to `None`.
"""
result = OrderedDict() result = OrderedDict()
if xml_request is None: if xml_request is None:
return result return result
for action in actions: # Requests can contain multipe <D:set> and <D:remove> elements.
action_element = xml_request.find(make_clark("D:%s" % action)) # Each of these elements must contain exactly one <D:prop> element which
if action_element is not None: # can contain multpile properties.
break # The order of the elements in the document must be respected.
else: props = []
action_element = xml_request for element in xml_request:
if element.tag in (make_clark("D:set"), make_clark("D:remove")):
prop_element = action_element.find(make_clark("D:prop")) for prop in element.findall("./%s/*" % make_clark("D:prop")):
if prop_element is not None: props.append((element.tag == make_clark("D:set"), prop))
for prop in prop_element: for is_set, prop in props:
if prop.tag == make_clark("D:resourcetype"): key = make_human_tag(prop.tag)
value = None
if prop.tag == make_clark("D:resourcetype"):
key = "tag"
if is_set:
for resource_type in prop: for resource_type in prop:
if resource_type.tag == make_clark("C:calendar"): if resource_type.tag == make_clark("C:calendar"):
result["tag"] = "VCALENDAR" value = "VCALENDAR"
break break
if resource_type.tag == make_clark("CR:addressbook"): if resource_type.tag == make_clark("CR:addressbook"):
result["tag"] = "VADDRESSBOOK" value = "VADDRESSBOOK"
break break
elif prop.tag == make_clark("C:supported-calendar-component-set"): elif prop.tag == make_clark("C:supported-calendar-component-set"):
result[make_human_tag(prop.tag)] = ",".join( if is_set:
supported_comp.attrib["name"] value = ",".join(
for supported_comp in prop supported_comp.attrib["name"] for supported_comp in prop
if supported_comp.tag == make_clark("C:comp")) if supported_comp.tag == make_clark("C:comp"))
else: elif is_set:
result[make_human_tag(prop.tag)] = prop.text value = prop.text or ""
result[key] = value
result.move_to_end(key)
return result return result