xmlutils.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Radicale Server - Calendar Server
  4. # Copyright © 2008 Nicolas Kandel
  5. # Copyright © 2008 Pascal Halter
  6. # Copyright © 2008-2015 Guillaume Ayoub
  7. #
  8. # This library is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This library is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  20. """
  21. XML and iCal requests manager.
  22. Note that all these functions need to receive unicode objects for full
  23. iCal requests (PUT) and string objects with charset correctly defined
  24. in them for XML requests (all but PUT).
  25. """
  26. try:
  27. from collections import OrderedDict
  28. except ImportError:
  29. # Python 2.6 has no OrderedDict, use a dict instead
  30. OrderedDict = dict # pylint: disable=C0103
  31. # Manage Python2/3 different modules
  32. # pylint: disable=F0401,E0611
  33. try:
  34. from urllib.parse import unquote, urlparse
  35. except ImportError:
  36. from urllib import unquote
  37. from urlparse import urlparse
  38. # pylint: enable=F0401,E0611
  39. import re
  40. import xml.etree.ElementTree as ET
  41. from . import client, config, ical
  42. NAMESPACES = {
  43. "C": "urn:ietf:params:xml:ns:caldav",
  44. "CR": "urn:ietf:params:xml:ns:carddav",
  45. "D": "DAV:",
  46. "CS": "http://calendarserver.org/ns/",
  47. "ICAL": "http://apple.com/ns/ical/",
  48. "ME": "http://me.com/_namespace/"}
  49. NAMESPACES_REV = {}
  50. for short, url in NAMESPACES.items():
  51. NAMESPACES_REV[url] = short
  52. if hasattr(ET, "register_namespace"):
  53. # Register namespaces cleanly with Python 2.7+ and 3.2+ ...
  54. ET.register_namespace("" if short == "D" else short, url)
  55. else:
  56. # ... and badly with Python 2.6 and 3.1
  57. ET._namespace_map[url] = short # pylint: disable=W0212
  58. CLARK_TAG_REGEX = re.compile(r"""
  59. { # {
  60. (?P<namespace>[^}]*) # namespace URL
  61. } # }
  62. (?P<tag>.*) # short tag name
  63. """, re.VERBOSE)
  64. def _pretty_xml(element, level=0):
  65. """Indent an ElementTree ``element`` and its children."""
  66. i = "\n" + level * " "
  67. if len(element):
  68. if not element.text or not element.text.strip():
  69. element.text = i + " "
  70. if not element.tail or not element.tail.strip():
  71. element.tail = i
  72. for sub_element in element:
  73. _pretty_xml(sub_element, level + 1)
  74. # ``sub_element`` is always defined as len(element) > 0
  75. # pylint: disable=W0631
  76. if not sub_element.tail or not sub_element.tail.strip():
  77. sub_element.tail = i
  78. # pylint: enable=W0631
  79. else:
  80. if level and (not element.tail or not element.tail.strip()):
  81. element.tail = i
  82. if not level:
  83. output_encoding = config.get("encoding", "request")
  84. return ('<?xml version="1.0"?>\n' + ET.tostring(
  85. element, "utf-8").decode("utf-8")).encode(output_encoding)
  86. def _tag(short_name, local):
  87. """Get XML Clark notation {uri(``short_name``)}``local``."""
  88. return "{%s}%s" % (NAMESPACES[short_name], local)
  89. def _tag_from_clark(name):
  90. """Get a human-readable variant of the XML Clark notation tag ``name``.
  91. For a given name using the XML Clark notation, return a human-readable
  92. variant of the tag name for known namespaces. Otherwise, return the name as
  93. is.
  94. """
  95. match = CLARK_TAG_REGEX.match(name)
  96. if match and match.group("namespace") in NAMESPACES_REV:
  97. args = {
  98. "ns": NAMESPACES_REV[match.group("namespace")],
  99. "tag": match.group("tag")}
  100. return "%(ns)s:%(tag)s" % args
  101. return name
  102. def _response(code):
  103. """Return full W3C names from HTTP status codes."""
  104. return "HTTP/1.1 %i %s" % (code, client.responses[code])
  105. def _href(href):
  106. """Return prefixed href."""
  107. return "%s%s" % (config.get("server", "base_prefix"), href.lstrip("/"))
  108. def name_from_path(path, collection):
  109. """Return Radicale item name from ``path``."""
  110. collection_parts = collection.path.strip("/").split("/")
  111. path_parts = path.strip("/").split("/")
  112. if (len(path_parts) - len(collection_parts)):
  113. return path_parts[-1]
  114. def props_from_request(root, actions=("set", "remove")):
  115. """Return a list of properties as a dictionary."""
  116. result = OrderedDict()
  117. if not hasattr(root, "tag"):
  118. root = ET.fromstring(root.encode("utf8"))
  119. for action in actions:
  120. action_element = root.find(_tag("D", action))
  121. if action_element is not None:
  122. break
  123. else:
  124. action_element = root
  125. prop_element = action_element.find(_tag("D", "prop"))
  126. if prop_element is not None:
  127. for prop in prop_element:
  128. if prop.tag == _tag("D", "resourcetype"):
  129. for resource_type in prop:
  130. if resource_type.tag == _tag("C", "calendar"):
  131. result["tag"] = "VCALENDAR"
  132. break
  133. elif resource_type.tag == _tag("CR", "addressbook"):
  134. result["tag"] = "VADDRESSBOOK"
  135. break
  136. elif prop.tag == _tag("C", "supported-calendar-component-set"):
  137. result[_tag_from_clark(prop.tag)] = ",".join(
  138. supported_comp.attrib["name"]
  139. for supported_comp in prop
  140. if supported_comp.tag == _tag("C", "comp"))
  141. else:
  142. result[_tag_from_clark(prop.tag)] = prop.text
  143. return result
  144. def delete(path, collection):
  145. """Read and answer DELETE requests.
  146. Read rfc4918-9.6 for info.
  147. """
  148. # Reading request
  149. if collection.path == path.strip("/"):
  150. # Delete the whole collection
  151. collection.delete()
  152. else:
  153. # Remove an item from the collection
  154. collection.remove(name_from_path(path, collection))
  155. # Writing answer
  156. multistatus = ET.Element(_tag("D", "multistatus"))
  157. response = ET.Element(_tag("D", "response"))
  158. multistatus.append(response)
  159. href = ET.Element(_tag("D", "href"))
  160. href.text = _href(path)
  161. response.append(href)
  162. status = ET.Element(_tag("D", "status"))
  163. status.text = _response(200)
  164. response.append(status)
  165. return _pretty_xml(multistatus)
  166. def propfind(path, xml_request, collections, user=None):
  167. """Read and answer PROPFIND requests.
  168. Read rfc4918-9.1 for info.
  169. The collections parameter is a list of collections that are
  170. to be included in the output. Rights checking has to be done
  171. by the caller.
  172. """
  173. # Reading request
  174. if xml_request:
  175. root = ET.fromstring(xml_request.encode("utf8"))
  176. props = [prop.tag for prop in root.find(_tag("D", "prop"))]
  177. else:
  178. props = [_tag("D", "getcontenttype"),
  179. _tag("D", "resourcetype"),
  180. _tag("D", "displayname"),
  181. _tag("D", "owner"),
  182. _tag("D", "getetag"),
  183. _tag("ICAL", "calendar-color"),
  184. _tag("CS", "getctag")]
  185. # Writing answer
  186. multistatus = ET.Element(_tag("D", "multistatus"))
  187. for collection in collections:
  188. response = _propfind_response(path, collection, props, user)
  189. multistatus.append(response)
  190. return _pretty_xml(multistatus)
  191. def _propfind_response(path, item, props, user):
  192. """Build and return a PROPFIND response."""
  193. is_collection = isinstance(item, ical.Collection)
  194. if is_collection:
  195. with item.props as properties:
  196. collection_props = properties
  197. response = ET.Element(_tag("D", "response"))
  198. href = ET.Element(_tag("D", "href"))
  199. uri = item.url if is_collection else "%s/%s" % (path, item.name)
  200. href.text = _href(uri.replace("//", "/"))
  201. response.append(href)
  202. propstat404 = ET.Element(_tag("D", "propstat"))
  203. propstat200 = ET.Element(_tag("D", "propstat"))
  204. response.append(propstat200)
  205. prop200 = ET.Element(_tag("D", "prop"))
  206. propstat200.append(prop200)
  207. prop404 = ET.Element(_tag("D", "prop"))
  208. propstat404.append(prop404)
  209. for tag in props:
  210. element = ET.Element(tag)
  211. is404 = False
  212. if tag == _tag("D", "getetag"):
  213. element.text = item.etag
  214. elif tag == _tag("D", "principal-URL"):
  215. tag = ET.Element(_tag("D", "href"))
  216. tag.text = _href(path)
  217. element.append(tag)
  218. elif tag in (_tag("D", "principal-collection-set"),
  219. _tag("C", "calendar-user-address-set"),
  220. _tag("CR", "addressbook-home-set"),
  221. _tag("C", "calendar-home-set")):
  222. tag = ET.Element(_tag("D", "href"))
  223. tag.text = _href(path)
  224. element.append(tag)
  225. elif tag == _tag("C", "supported-calendar-component-set"):
  226. # This is not a Todo
  227. # pylint: disable=W0511
  228. human_tag = _tag_from_clark(tag)
  229. if is_collection and human_tag in collection_props:
  230. # TODO: what do we have to do if it's not a collection?
  231. components = collection_props[human_tag].split(",")
  232. else:
  233. components = ("VTODO", "VEVENT", "VJOURNAL")
  234. for component in components:
  235. comp = ET.Element(_tag("C", "comp"))
  236. comp.set("name", component)
  237. element.append(comp)
  238. # pylint: enable=W0511
  239. elif tag == _tag("D", "current-user-principal") and user:
  240. tag = ET.Element(_tag("D", "href"))
  241. tag.text = _href("/%s/" % user)
  242. element.append(tag)
  243. elif tag == _tag("D", "current-user-privilege-set"):
  244. privilege = ET.Element(_tag("D", "privilege"))
  245. privilege.append(ET.Element(_tag("D", "all")))
  246. privilege.append(ET.Element(_tag("D", "read")))
  247. privilege.append(ET.Element(_tag("D", "write")))
  248. privilege.append(ET.Element(_tag("D", "write-properties")))
  249. privilege.append(ET.Element(_tag("D", "write-content")))
  250. element.append(privilege)
  251. elif tag == _tag("D", "supported-report-set"):
  252. for report_name in (
  253. "principal-property-search", "sync-collection",
  254. "expand-property", "principal-search-property-set"):
  255. supported = ET.Element(_tag("D", "supported-report"))
  256. report_tag = ET.Element(_tag("D", "report"))
  257. report_tag.text = report_name
  258. supported.append(report_tag)
  259. element.append(supported)
  260. elif is_collection:
  261. if tag == _tag("D", "getcontenttype"):
  262. element.text = item.mimetype
  263. elif tag == _tag("D", "resourcetype"):
  264. if item.is_principal:
  265. tag = ET.Element(_tag("D", "principal"))
  266. element.append(tag)
  267. if item.is_leaf(item.path) or (
  268. not item.exists and item.resource_type):
  269. # 2nd case happens when the collection is not stored yet,
  270. # but the resource type is guessed
  271. if item.resource_type == "addressbook":
  272. tag = ET.Element(_tag("CR", item.resource_type))
  273. else:
  274. tag = ET.Element(_tag("C", item.resource_type))
  275. element.append(tag)
  276. tag = ET.Element(_tag("D", "collection"))
  277. element.append(tag)
  278. elif tag == _tag("D", "owner") and item.owner_url:
  279. element.text = item.owner_url
  280. elif tag == _tag("CS", "getctag"):
  281. element.text = item.etag
  282. elif tag == _tag("C", "calendar-timezone"):
  283. element.text = ical.serialize(
  284. item.tag, item.headers, item.timezones)
  285. elif tag == _tag("D", "displayname"):
  286. element.text = item.name
  287. elif tag == _tag("ICAL", "calendar-color"):
  288. element.text = item.color
  289. else:
  290. human_tag = _tag_from_clark(tag)
  291. if human_tag in collection_props:
  292. element.text = collection_props[human_tag]
  293. else:
  294. is404 = True
  295. # Not for collections
  296. elif tag == _tag("D", "getcontenttype"):
  297. element.text = "%s; component=%s" % (
  298. item.mimetype, item.tag.lower())
  299. elif tag == _tag("D", "resourcetype"):
  300. # resourcetype must be returned empty for non-collection elements
  301. pass
  302. else:
  303. is404 = True
  304. if is404:
  305. prop404.append(element)
  306. else:
  307. prop200.append(element)
  308. status200 = ET.Element(_tag("D", "status"))
  309. status200.text = _response(200)
  310. propstat200.append(status200)
  311. status404 = ET.Element(_tag("D", "status"))
  312. status404.text = _response(404)
  313. propstat404.append(status404)
  314. if len(prop404):
  315. response.append(propstat404)
  316. return response
  317. def _add_propstat_to(element, tag, status_number):
  318. """Add a PROPSTAT response structure to an element.
  319. The PROPSTAT answer structure is defined in rfc4918-9.1. It is added to the
  320. given ``element``, for the following ``tag`` with the given
  321. ``status_number``.
  322. """
  323. propstat = ET.Element(_tag("D", "propstat"))
  324. element.append(propstat)
  325. prop = ET.Element(_tag("D", "prop"))
  326. propstat.append(prop)
  327. if "{" in tag:
  328. clark_tag = tag
  329. else:
  330. clark_tag = _tag(*tag.split(":", 1))
  331. prop_tag = ET.Element(clark_tag)
  332. prop.append(prop_tag)
  333. status = ET.Element(_tag("D", "status"))
  334. status.text = _response(status_number)
  335. propstat.append(status)
  336. def proppatch(path, xml_request, collection):
  337. """Read and answer PROPPATCH requests.
  338. Read rfc4918-9.2 for info.
  339. """
  340. # Reading request
  341. root = ET.fromstring(xml_request.encode("utf8"))
  342. props_to_set = props_from_request(root, actions=("set",))
  343. props_to_remove = props_from_request(root, actions=("remove",))
  344. # Writing answer
  345. multistatus = ET.Element(_tag("D", "multistatus"))
  346. response = ET.Element(_tag("D", "response"))
  347. multistatus.append(response)
  348. href = ET.Element(_tag("D", "href"))
  349. href.text = _href(path)
  350. response.append(href)
  351. with collection.props as collection_props:
  352. for short_name, value in props_to_set.items():
  353. if short_name.split(":")[-1] == "calendar-timezone":
  354. collection.replace(None, value)
  355. collection_props[short_name] = value
  356. _add_propstat_to(response, short_name, 200)
  357. for short_name in props_to_remove:
  358. try:
  359. del collection_props[short_name]
  360. except KeyError:
  361. _add_propstat_to(response, short_name, 412)
  362. else:
  363. _add_propstat_to(response, short_name, 200)
  364. return _pretty_xml(multistatus)
  365. def put(path, ical_request, collection):
  366. """Read PUT requests."""
  367. name = name_from_path(path, collection)
  368. if name in collection.items:
  369. # PUT is modifying an existing item
  370. collection.replace(name, ical_request)
  371. elif name:
  372. # PUT is adding a new item
  373. collection.append(name, ical_request)
  374. else:
  375. # PUT is replacing the whole collection
  376. collection.save(ical_request)
  377. def report(path, xml_request, collection):
  378. """Read and answer REPORT requests.
  379. Read rfc3253-3.6 for info.
  380. """
  381. # Reading request
  382. root = ET.fromstring(xml_request.encode("utf8"))
  383. prop_element = root.find(_tag("D", "prop"))
  384. props = (
  385. [prop.tag for prop in prop_element]
  386. if prop_element is not None else [])
  387. if collection:
  388. if root.tag in (_tag("C", "calendar-multiget"),
  389. _tag("CR", "addressbook-multiget")):
  390. # Read rfc4791-7.9 for info
  391. base_prefix = config.get("server", "base_prefix")
  392. hreferences = set()
  393. for href_element in root.findall(_tag("D", "href")):
  394. href_path = unquote(urlparse(href_element.text).path)
  395. if href_path.startswith(base_prefix):
  396. hreferences.add(href_path[len(base_prefix):])
  397. else:
  398. hreferences = (path,)
  399. # TODO: handle other filters
  400. # TODO: handle the nested comp-filters correctly
  401. # Read rfc4791-9.7.1 for info
  402. tag_filters = set(
  403. element.get("name") for element
  404. in root.findall(".//%s" % _tag("C", "comp-filter")))
  405. tag_filters.discard('VCALENDAR')
  406. else:
  407. hreferences = ()
  408. tag_filters = None
  409. # Writing answer
  410. multistatus = ET.Element(_tag("D", "multistatus"))
  411. collection_tag = collection.tag
  412. collection_headers = collection.headers
  413. collection_timezones = collection.timezones
  414. for hreference in hreferences:
  415. # Check if the reference is an item or a collection
  416. name = name_from_path(hreference, collection)
  417. if name:
  418. # Reference is an item
  419. path = "/".join(hreference.split("/")[:-1]) + "/"
  420. try:
  421. items = [collection.items[name]]
  422. except KeyError:
  423. multistatus.append(
  424. _item_response(hreference, found_item=False))
  425. continue
  426. else:
  427. # Reference is a collection
  428. path = hreference
  429. items = collection.components
  430. for item in items:
  431. href = _href("%s/%s" % (path.rstrip("/"), item.name))
  432. if tag_filters and item.tag not in tag_filters:
  433. continue
  434. found_props = []
  435. not_found_props = []
  436. for tag in props:
  437. element = ET.Element(tag)
  438. if tag == _tag("D", "getetag"):
  439. element.text = item.etag
  440. found_props.append(element)
  441. elif tag == _tag("D", "getcontenttype"):
  442. element.text = "%s; component=%s" % (
  443. item.mimetype, item.tag.lower())
  444. found_props.append(element)
  445. elif tag in (_tag("C", "calendar-data"),
  446. _tag("CR", "address-data")):
  447. if isinstance(item, ical.Component):
  448. element.text = ical.serialize(
  449. collection_tag, collection_headers,
  450. collection_timezones + [item])
  451. found_props.append(element)
  452. else:
  453. not_found_props.append(element)
  454. multistatus.append(_item_response(
  455. href, found_props=found_props, not_found_props=not_found_props,
  456. found_item=True))
  457. return _pretty_xml(multistatus)
  458. def _item_response(href, found_props=(), not_found_props=(), found_item=True):
  459. response = ET.Element(_tag("D", "response"))
  460. href_tag = ET.Element(_tag("D", "href"))
  461. href_tag.text = href
  462. response.append(href_tag)
  463. if found_item:
  464. if found_props:
  465. propstat = ET.Element(_tag("D", "propstat"))
  466. status = ET.Element(_tag("D", "status"))
  467. status.text = _response(200)
  468. prop = ET.Element(_tag("D", "prop"))
  469. for p in found_props:
  470. prop.append(p)
  471. propstat.append(prop)
  472. propstat.append(status)
  473. response.append(propstat)
  474. if not_found_props:
  475. propstat = ET.Element(_tag("D", "propstat"))
  476. status = ET.Element(_tag("D", "status"))
  477. status.text = _response(404)
  478. prop = ET.Element(_tag("D", "prop"))
  479. for p in not_found_props:
  480. prop.append(p)
  481. propstat.append(prop)
  482. propstat.append(status)
  483. response.append(propstat)
  484. else:
  485. status = ET.Element(_tag("D", "status"))
  486. status.text = _response(404)
  487. response.append(status)
  488. return response