2008-12-30 16:25:42 +00:00
|
|
|
# This file is part of Radicale Server - Calendar Server
|
2009-07-27 15:04:54 +00:00
|
|
|
# Copyright © 2008 Nicolas Kandel
|
|
|
|
# Copyright © 2008 Pascal Halter
|
2015-02-07 17:26:20 +01:00
|
|
|
# Copyright © 2008-2015 Guillaume Ayoub
|
2018-09-04 03:33:47 +02:00
|
|
|
# Copyright © 2017-2018 Unrud<unrud@outlook.com>
|
2008-12-30 16:25:42 +00:00
|
|
|
#
|
|
|
|
# This library is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Radicale. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""
|
2009-07-27 15:04:54 +00:00
|
|
|
XML and iCal requests manager.
|
2008-12-30 16:25:42 +00:00
|
|
|
|
|
|
|
Note that all these functions need to receive unicode objects for full
|
|
|
|
iCal requests (PUT) and string objects with charset correctly defined
|
|
|
|
in them for XML requests (all but PUT).
|
2010-02-10 18:57:21 +01:00
|
|
|
|
2008-12-30 16:25:42 +00:00
|
|
|
"""
|
|
|
|
|
2017-05-07 08:17:39 +02:00
|
|
|
import copy
|
2011-05-24 16:12:35 +02:00
|
|
|
import re
|
2008-12-30 16:25:42 +00:00
|
|
|
import xml.etree.ElementTree as ET
|
2016-03-31 19:57:40 +02:00
|
|
|
from collections import OrderedDict
|
2016-08-05 02:14:49 +02:00
|
|
|
from http import client
|
2018-08-28 16:19:36 +02:00
|
|
|
from urllib.parse import quote
|
2016-08-13 04:51:42 +02:00
|
|
|
|
2018-08-28 16:19:50 +02:00
|
|
|
from radicale import pathutils
|
|
|
|
|
2016-08-05 02:14:49 +02:00
|
|
|
MIMETYPES = {
|
|
|
|
"VADDRESSBOOK": "text/vcard",
|
|
|
|
"VCALENDAR": "text/calendar"}
|
2008-12-30 16:25:42 +00:00
|
|
|
|
2017-08-29 20:08:25 +02:00
|
|
|
OBJECT_MIMETYPES = {
|
|
|
|
"VCARD": "text/vcard",
|
|
|
|
"VLIST": "text/x-vlist",
|
|
|
|
"VCALENDAR": "text/calendar"}
|
|
|
|
|
2010-04-10 16:26:22 +02:00
|
|
|
NAMESPACES = {
|
2010-02-10 18:57:21 +01:00
|
|
|
"C": "urn:ietf:params:xml:ns:caldav",
|
2011-12-31 13:31:22 +01:00
|
|
|
"CR": "urn:ietf:params:xml:ns:carddav",
|
2010-02-10 18:57:21 +01:00
|
|
|
"D": "DAV:",
|
2011-05-11 16:24:20 +02:00
|
|
|
"CS": "http://calendarserver.org/ns/",
|
2011-06-01 12:43:49 +02:00
|
|
|
"ICAL": "http://apple.com/ns/ical/",
|
2017-09-17 14:03:50 +02:00
|
|
|
"ME": "http://me.com/_namespace/",
|
|
|
|
"RADICALE": "http://radicale.org/ns/"}
|
2008-12-30 16:25:42 +00:00
|
|
|
|
2011-05-24 16:12:35 +02:00
|
|
|
NAMESPACES_REV = {}
|
2011-05-10 19:16:03 +02:00
|
|
|
for short, url in NAMESPACES.items():
|
2011-05-24 16:12:35 +02:00
|
|
|
NAMESPACES_REV[url] = short
|
2016-03-31 19:57:40 +02:00
|
|
|
ET.register_namespace("" if short == "D" else short, url)
|
2011-05-09 17:02:31 +02:00
|
|
|
|
2017-02-26 15:33:34 +01:00
|
|
|
CLARK_TAG_REGEX = re.compile(r"{(?P<namespace>[^}]*)}(?P<tag>.*)", re.VERBOSE)
|
2017-09-17 14:03:53 +02:00
|
|
|
HUMAN_REGEX = re.compile(r"(?P<namespace>[^:{}]*):(?P<tag>.*)", re.VERBOSE)
|
2011-05-24 16:12:35 +02:00
|
|
|
|
|
|
|
|
2017-05-07 08:17:39 +02:00
|
|
|
def pretty_xml(element, level=0):
|
2011-05-11 06:19:05 +02:00
|
|
|
"""Indent an ElementTree ``element`` and its children."""
|
2017-05-07 08:17:39 +02:00
|
|
|
if not level:
|
|
|
|
element = copy.deepcopy(element)
|
2011-05-09 16:51:58 +02:00
|
|
|
i = "\n" + level * " "
|
2011-05-11 06:19:05 +02:00
|
|
|
if len(element):
|
|
|
|
if not element.text or not element.text.strip():
|
|
|
|
element.text = i + " "
|
|
|
|
if not element.tail or not element.tail.strip():
|
|
|
|
element.tail = i
|
|
|
|
for sub_element in element:
|
2017-05-07 08:17:39 +02:00
|
|
|
pretty_xml(sub_element, level + 1)
|
2011-05-11 06:19:05 +02:00
|
|
|
if not sub_element.tail or not sub_element.tail.strip():
|
|
|
|
sub_element.tail = i
|
2011-05-09 16:51:58 +02:00
|
|
|
else:
|
2011-05-11 06:19:05 +02:00
|
|
|
if level and (not element.tail or not element.tail.strip()):
|
|
|
|
element.tail = i
|
2011-05-11 11:20:39 +02:00
|
|
|
if not level:
|
2016-04-22 11:37:02 +09:00
|
|
|
return '<?xml version="1.0"?>\n%s' % ET.tostring(element, "unicode")
|
2011-05-09 16:51:58 +02:00
|
|
|
|
|
|
|
|
2018-08-28 16:19:36 +02:00
|
|
|
def make_tag(short_name, local):
|
2009-07-27 15:04:54 +00:00
|
|
|
"""Get XML Clark notation {uri(``short_name``)}``local``."""
|
2010-04-10 16:26:22 +02:00
|
|
|
return "{%s}%s" % (NAMESPACES[short_name], local)
|
2010-02-10 18:57:21 +01:00
|
|
|
|
2010-01-16 13:33:50 +01:00
|
|
|
|
2018-08-28 16:19:36 +02:00
|
|
|
def tag_from_clark(name):
|
2011-06-02 20:15:07 +02:00
|
|
|
"""Get a human-readable variant of the XML Clark notation tag ``name``.
|
|
|
|
|
|
|
|
For a given name using the XML Clark notation, return a human-readable
|
|
|
|
variant of the tag name for known namespaces. Otherwise, return the name as
|
|
|
|
is.
|
2011-05-24 17:33:57 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
match = CLARK_TAG_REGEX.match(name)
|
2012-03-01 10:40:15 +01:00
|
|
|
if match and match.group("namespace") in NAMESPACES_REV:
|
2011-05-24 17:33:57 +02:00
|
|
|
args = {
|
2012-03-01 10:40:15 +01:00
|
|
|
"ns": NAMESPACES_REV[match.group("namespace")],
|
|
|
|
"tag": match.group("tag")}
|
|
|
|
return "%(ns)s:%(tag)s" % args
|
2011-06-01 12:43:49 +02:00
|
|
|
return name
|
2011-05-24 17:33:57 +02:00
|
|
|
|
|
|
|
|
2018-08-28 16:19:36 +02:00
|
|
|
def tag_from_human(name):
|
2014-08-09 22:44:36 +02:00
|
|
|
"""Get an XML Clark notation tag from human-readable variant ``name``."""
|
|
|
|
match = HUMAN_REGEX.match(name)
|
2017-02-26 15:33:34 +01:00
|
|
|
if match and match.group("namespace") in NAMESPACES:
|
2018-08-28 16:19:36 +02:00
|
|
|
return make_tag(match.group("namespace"), match.group("tag"))
|
2014-08-09 22:44:36 +02:00
|
|
|
return name
|
|
|
|
|
2017-02-26 15:33:34 +01:00
|
|
|
|
2018-08-28 16:19:36 +02:00
|
|
|
def make_response(code):
|
2010-01-16 13:33:50 +01:00
|
|
|
"""Return full W3C names from HTTP status codes."""
|
|
|
|
return "HTTP/1.1 %i %s" % (code, client.responses[code])
|
2008-12-30 16:25:42 +00:00
|
|
|
|
2010-02-10 18:57:21 +01:00
|
|
|
|
2018-08-28 16:19:36 +02:00
|
|
|
def make_href(base_prefix, href):
|
2013-04-30 14:02:17 +02:00
|
|
|
"""Return prefixed href."""
|
2018-08-28 16:19:50 +02:00
|
|
|
assert href == pathutils.sanitize_path(href)
|
2016-09-04 22:01:27 +02:00
|
|
|
return quote("%s%s" % (base_prefix, href))
|
2013-03-18 18:14:53 +01:00
|
|
|
|
|
|
|
|
2018-04-30 19:34:29 +02:00
|
|
|
def webdav_error(namespace, name):
|
2017-06-07 14:16:18 +02:00
|
|
|
"""Generate XML error message."""
|
2018-08-28 16:19:36 +02:00
|
|
|
root = ET.Element(make_tag("D", "error"))
|
|
|
|
root.append(ET.Element(make_tag(namespace, name)))
|
2017-06-07 14:16:18 +02:00
|
|
|
return root
|
|
|
|
|
|
|
|
|
2017-08-29 20:08:26 +02:00
|
|
|
def get_content_type(item):
|
|
|
|
"""Get the content-type of an item with charset and component parameters.
|
|
|
|
"""
|
|
|
|
mimetype = OBJECT_MIMETYPES[item.name]
|
|
|
|
encoding = item.collection.configuration.get("encoding", "request")
|
2017-08-29 20:08:28 +02:00
|
|
|
tag = item.component_name
|
2017-08-29 20:08:26 +02:00
|
|
|
content_type = "%s;charset=%s" % (mimetype, encoding)
|
|
|
|
if tag:
|
|
|
|
content_type += ";component=%s" % tag
|
|
|
|
return content_type
|
|
|
|
|
|
|
|
|
2017-05-07 08:17:39 +02:00
|
|
|
def props_from_request(xml_request, actions=("set", "remove")):
|
2011-06-02 20:15:07 +02:00
|
|
|
"""Return a list of properties as a dictionary."""
|
2011-05-24 16:12:35 +02:00
|
|
|
result = OrderedDict()
|
2017-05-07 08:17:39 +02:00
|
|
|
if xml_request is None:
|
2016-04-17 13:58:56 +09:00
|
|
|
return result
|
2011-05-24 16:12:35 +02:00
|
|
|
|
2011-05-24 17:33:57 +02:00
|
|
|
for action in actions:
|
2018-08-28 16:19:36 +02:00
|
|
|
action_element = xml_request.find(make_tag("D", action))
|
2011-05-24 17:33:57 +02:00
|
|
|
if action_element is not None:
|
|
|
|
break
|
|
|
|
else:
|
2017-05-07 08:17:39 +02:00
|
|
|
action_element = xml_request
|
2011-05-24 16:12:35 +02:00
|
|
|
|
2018-08-28 16:19:36 +02:00
|
|
|
prop_element = action_element.find(make_tag("D", "prop"))
|
2011-05-24 17:33:57 +02:00
|
|
|
if prop_element is not None:
|
2011-05-24 16:12:35 +02:00
|
|
|
for prop in prop_element:
|
2018-08-28 16:19:36 +02:00
|
|
|
if prop.tag == make_tag("D", "resourcetype"):
|
2011-12-31 13:31:22 +01:00
|
|
|
for resource_type in prop:
|
2018-08-28 16:19:36 +02:00
|
|
|
if resource_type.tag == make_tag("C", "calendar"):
|
2013-05-13 23:19:22 +02:00
|
|
|
result["tag"] = "VCALENDAR"
|
2011-12-31 13:31:22 +01:00
|
|
|
break
|
2018-08-28 16:19:36 +02:00
|
|
|
elif resource_type.tag == make_tag("CR", "addressbook"):
|
2013-05-13 23:19:22 +02:00
|
|
|
result["tag"] = "VADDRESSBOOK"
|
|
|
|
break
|
2018-08-28 16:19:36 +02:00
|
|
|
elif prop.tag == make_tag("C", "supported-calendar-component-set"):
|
|
|
|
result[tag_from_clark(prop.tag)] = ",".join(
|
2013-05-10 14:56:17 +02:00
|
|
|
supported_comp.attrib["name"]
|
|
|
|
for supported_comp in prop
|
2018-08-28 16:19:36 +02:00
|
|
|
if supported_comp.tag == make_tag("C", "comp"))
|
2013-05-13 23:19:22 +02:00
|
|
|
else:
|
2018-08-28 16:19:36 +02:00
|
|
|
result[tag_from_clark(prop.tag)] = prop.text
|
2011-06-02 20:15:07 +02:00
|
|
|
|
2011-05-24 16:12:35 +02:00
|
|
|
return result
|