xmlutils.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. # This file is part of Radicale - CalDAV and CardDAV server
  2. # Copyright © 2008 Nicolas Kandel
  3. # Copyright © 2008 Pascal Halter
  4. # Copyright © 2008-2015 Guillaume Ayoub
  5. # Copyright © 2017-2018 Unrud <unrud@outlook.com>
  6. #
  7. # This library is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  19. """
  20. Helper functions for XML.
  21. """
  22. import copy
  23. import xml.etree.ElementTree as ET
  24. from collections import OrderedDict
  25. from http import client
  26. from typing import Dict, Mapping, Optional
  27. from urllib.parse import quote
  28. from radicale import item, pathutils
  29. MIMETYPES: Mapping[str, str] = {
  30. "VADDRESSBOOK": "text/vcard",
  31. "VCALENDAR": "text/calendar"}
  32. OBJECT_MIMETYPES: Mapping[str, str] = {
  33. "VCARD": "text/vcard",
  34. "VLIST": "text/x-vlist",
  35. "VCALENDAR": "text/calendar"}
  36. NAMESPACES: Mapping[str, str] = {
  37. "C": "urn:ietf:params:xml:ns:caldav",
  38. "CR": "urn:ietf:params:xml:ns:carddav",
  39. "D": "DAV:",
  40. "CS": "http://calendarserver.org/ns/",
  41. "ICAL": "http://apple.com/ns/ical/",
  42. "ME": "http://me.com/_namespace/",
  43. "RADICALE": "http://radicale.org/ns/"}
  44. NAMESPACES_REV: Mapping[str, str] = {v: k for k, v in NAMESPACES.items()}
  45. for short, url in NAMESPACES.items():
  46. ET.register_namespace("" if short == "D" else short, url)
  47. def pretty_xml(element: ET.Element) -> str:
  48. """Indent an ElementTree ``element`` and its children."""
  49. def pretty_xml_recursive(element: ET.Element, level: int) -> None:
  50. indent = "\n" + level * " "
  51. if len(element) > 0:
  52. if not (element.text or "").strip():
  53. element.text = indent + " "
  54. if not (element.tail or "").strip():
  55. element.tail = indent
  56. for sub_element in element:
  57. pretty_xml_recursive(sub_element, level + 1)
  58. if not (sub_element.tail or "").strip():
  59. sub_element.tail = indent
  60. elif level > 0 and not (element.tail or "").strip():
  61. element.tail = indent
  62. element = copy.deepcopy(element)
  63. pretty_xml_recursive(element, 0)
  64. return '<?xml version="1.0"?>\n%s' % ET.tostring(element, "unicode")
  65. def make_clark(human_tag: str) -> str:
  66. """Get XML Clark notation from human tag ``human_tag``.
  67. If ``human_tag`` is already in XML Clark notation it is returned as-is.
  68. """
  69. if human_tag.startswith("{"):
  70. ns, tag = human_tag[len("{"):].split("}", maxsplit=1)
  71. if not ns or not tag:
  72. raise ValueError("Invalid XML tag: %r" % human_tag)
  73. return human_tag
  74. ns_prefix, tag = human_tag.split(":", maxsplit=1)
  75. if not ns_prefix or not tag:
  76. raise ValueError("Invalid XML tag: %r" % human_tag)
  77. ns = NAMESPACES.get(ns_prefix, "")
  78. if not ns:
  79. raise ValueError("Unknown XML namespace prefix: %r" % human_tag)
  80. return "{%s}%s" % (ns, tag)
  81. def make_human_tag(clark_tag: str) -> str:
  82. """Replace known namespaces in XML Clark notation ``clark_tag`` with
  83. prefix.
  84. If the namespace is not in ``NAMESPACES`` the tag is returned as-is.
  85. """
  86. if not clark_tag.startswith("{"):
  87. ns_prefix, tag = clark_tag.split(":", maxsplit=1)
  88. if not ns_prefix or not tag:
  89. raise ValueError("Invalid XML tag: %r" % clark_tag)
  90. if ns_prefix not in NAMESPACES:
  91. raise ValueError("Unknown XML namespace prefix: %r" % clark_tag)
  92. return clark_tag
  93. ns, tag = clark_tag[len("{"):].split("}", maxsplit=1)
  94. if not ns or not tag:
  95. raise ValueError("Invalid XML tag: %r" % clark_tag)
  96. ns_prefix = NAMESPACES_REV.get(ns, "")
  97. if ns_prefix:
  98. return "%s:%s" % (ns_prefix, tag)
  99. return clark_tag
  100. def make_response(code: int) -> str:
  101. """Return full W3C names from HTTP status codes."""
  102. return "HTTP/1.1 %i %s" % (code, client.responses[code])
  103. def make_href(base_prefix: str, href: str) -> str:
  104. """Return prefixed href."""
  105. assert href == pathutils.sanitize_path(href)
  106. return quote("%s%s" % (base_prefix, href))
  107. def webdav_error(human_tag: str) -> ET.Element:
  108. """Generate XML error message."""
  109. root = ET.Element(make_clark("D:error"))
  110. root.append(ET.Element(make_clark(human_tag)))
  111. return root
  112. def get_content_type(item: "item.Item", encoding: str) -> str:
  113. """Get the content-type of an item with charset and component parameters.
  114. """
  115. mimetype = OBJECT_MIMETYPES[item.name]
  116. tag = item.component_name
  117. content_type = "%s;charset=%s" % (mimetype, encoding)
  118. if tag:
  119. content_type += ";component=%s" % tag
  120. return content_type
  121. def props_from_request(xml_request: Optional[ET.Element]
  122. ) -> Dict[str, Optional[str]]:
  123. """Return a list of properties as a dictionary.
  124. Properties that should be removed are set to `None`.
  125. """
  126. result: OrderedDict = OrderedDict()
  127. if xml_request is None:
  128. return result
  129. # Requests can contain multipe <D:set> and <D:remove> elements.
  130. # Each of these elements must contain exactly one <D:prop> element which
  131. # can contain multpile properties.
  132. # The order of the elements in the document must be respected.
  133. props = []
  134. for element in xml_request:
  135. if element.tag in (make_clark("D:set"), make_clark("D:remove")):
  136. for prop in element.findall("./%s/*" % make_clark("D:prop")):
  137. props.append((element.tag == make_clark("D:set"), prop))
  138. for is_set, prop in props:
  139. key = make_human_tag(prop.tag)
  140. value = None
  141. if prop.tag == make_clark("D:resourcetype"):
  142. key = "tag"
  143. if is_set:
  144. for resource_type in prop:
  145. if resource_type.tag == make_clark("C:calendar"):
  146. value = "VCALENDAR"
  147. break
  148. if resource_type.tag == make_clark("CR:addressbook"):
  149. value = "VADDRESSBOOK"
  150. break
  151. elif prop.tag == make_clark("C:supported-calendar-component-set"):
  152. if is_set:
  153. value = ",".join(
  154. supported_comp.attrib["name"] for supported_comp in prop
  155. if supported_comp.tag == make_clark("C:comp"))
  156. elif is_set:
  157. value = prop.text or ""
  158. result[key] = value
  159. result.move_to_end(key)
  160. return result