xmlutils.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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-2013 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. HUMAN_REGEX = re.compile(r"""
  65. (?P<namespaceabbrev>[^:{}]*) # namespace abbreviation
  66. : # :
  67. (?P<tag>.*) # short tag name
  68. """, re.VERBOSE)
  69. def _pretty_xml(element, level=0):
  70. """Indent an ElementTree ``element`` and its children."""
  71. i = "\n" + level * " "
  72. if len(element):
  73. if not element.text or not element.text.strip():
  74. element.text = i + " "
  75. if not element.tail or not element.tail.strip():
  76. element.tail = i
  77. for sub_element in element:
  78. _pretty_xml(sub_element, level + 1)
  79. # ``sub_element`` is always defined as len(element) > 0
  80. # pylint: disable=W0631
  81. if not sub_element.tail or not sub_element.tail.strip():
  82. sub_element.tail = i
  83. # pylint: enable=W0631
  84. else:
  85. if level and (not element.tail or not element.tail.strip()):
  86. element.tail = i
  87. if not level:
  88. output_encoding = config.get("encoding", "request")
  89. return ('<?xml version="1.0"?>\n' + ET.tostring(
  90. element, "utf-8").decode("utf-8")).encode(output_encoding)
  91. def _tag(short_name, local):
  92. """Get XML Clark notation {uri(``short_name``)}``local``."""
  93. return "{%s}%s" % (NAMESPACES[short_name], local)
  94. def _tag_from_clark(name):
  95. """Get a human-readable variant of the XML Clark notation tag ``name``.
  96. For a given name using the XML Clark notation, return a human-readable
  97. variant of the tag name for known namespaces. Otherwise, return the name as
  98. is.
  99. """
  100. match = CLARK_TAG_REGEX.match(name)
  101. if match and match.group("namespace") in NAMESPACES_REV:
  102. args = {
  103. "ns": NAMESPACES_REV[match.group("namespace")],
  104. "tag": match.group("tag")}
  105. return "%(ns)s:%(tag)s" % args
  106. return name
  107. def _tag_from_human(name):
  108. """Get an XML Clark notation tag from human-readable variant ``name``."""
  109. match = HUMAN_REGEX.match(name)
  110. if match and match.group("namespaceabbrev") in NAMESPACES:
  111. return _tag(match.group("namespaceabbrev"), match.group("tag"))
  112. return name
  113. def _response(code):
  114. """Return full W3C names from HTTP status codes."""
  115. return "HTTP/1.1 %i %s" % (code, client.responses[code])
  116. def _href(href):
  117. """Return prefixed href."""
  118. return "%s%s" % (config.get("server", "base_prefix"), href.lstrip("/"))
  119. def name_from_path(path, collection):
  120. """Return Radicale item name from ``path``."""
  121. collection_parts = collection.path.strip("/").split("/")
  122. path_parts = path.strip("/").split("/")
  123. if (len(path_parts) - len(collection_parts)):
  124. return path_parts[-1]
  125. def props_from_request(root, actions=("set", "remove")):
  126. """Return a list of properties as a dictionary."""
  127. result = OrderedDict()
  128. if not hasattr(root, "tag"):
  129. root = ET.fromstring(root.encode("utf8"))
  130. for action in actions:
  131. action_element = root.find(_tag("D", action))
  132. if action_element is not None:
  133. break
  134. else:
  135. action_element = root
  136. prop_element = action_element.find(_tag("D", "prop"))
  137. if prop_element is not None:
  138. for prop in prop_element:
  139. if prop.tag == _tag("D", "resourcetype"):
  140. for resource_type in prop:
  141. if resource_type.tag == _tag("C", "calendar"):
  142. result["tag"] = "VCALENDAR"
  143. break
  144. elif resource_type.tag == _tag("CR", "addressbook"):
  145. result["tag"] = "VADDRESSBOOK"
  146. break
  147. elif prop.tag == _tag("C", "supported-calendar-component-set"):
  148. result[_tag_from_clark(prop.tag)] = ",".join(
  149. supported_comp.attrib["name"]
  150. for supported_comp in prop
  151. if supported_comp.tag == _tag("C", "comp"))
  152. else:
  153. result[_tag_from_clark(prop.tag)] = prop.text
  154. return result
  155. def delete(path, collection):
  156. """Read and answer DELETE requests.
  157. Read rfc4918-9.6 for info.
  158. """
  159. # Reading request
  160. if collection.path == path.strip("/"):
  161. # Delete the whole collection
  162. collection.delete()
  163. else:
  164. # Remove an item from the collection
  165. collection.remove(name_from_path(path, collection))
  166. # Writing answer
  167. multistatus = ET.Element(_tag("D", "multistatus"))
  168. response = ET.Element(_tag("D", "response"))
  169. multistatus.append(response)
  170. href = ET.Element(_tag("D", "href"))
  171. href.text = _href(path)
  172. response.append(href)
  173. status = ET.Element(_tag("D", "status"))
  174. status.text = _response(200)
  175. response.append(status)
  176. return _pretty_xml(multistatus)
  177. def propfind(path, xml_request, collections, user=None):
  178. """Read and answer PROPFIND requests.
  179. Read rfc4918-9.1 for info.
  180. The collections parameter is a list of collections that are
  181. to be included in the output. Rights checking has to be done
  182. by the caller.
  183. """
  184. # Reading request
  185. root = ET.fromstring(xml_request.encode("utf8")) if xml_request else None
  186. # > A client may choose not to submit a request body. An empty PROPFIND
  187. # > request body MUST be treated as if it were an 'allprop' request.
  188. top_tag = root[0] if root is not None else ET.Element(_tag("D", "allprop"))
  189. props = ()
  190. if top_tag.tag == _tag("D", "allprop"):
  191. props = [_tag("D", "getcontenttype"),
  192. _tag("D", "resourcetype"),
  193. _tag("D", "displayname"),
  194. _tag("D", "owner"),
  195. _tag("D", "getetag"),
  196. _tag("ICAL", "calendar-color"),
  197. _tag("CS", "getctag"),
  198. _tag("C", "supported-calendar-component-set"),
  199. _tag("D", "supported-report-set"),
  200. ]
  201. elif top_tag.tag == _tag("D", "prop"):
  202. props = [prop.tag for prop in top_tag]
  203. # Writing answer
  204. multistatus = ET.Element(_tag("D", "multistatus"))
  205. for collection in collections:
  206. if top_tag.tag == _tag("D", "propname"):
  207. response = _propfind_response(path, collection, (), user, propnames=True)
  208. else:
  209. response = _propfind_response(path, collection, props, user)
  210. multistatus.append(response)
  211. return _pretty_xml(multistatus)
  212. def _propfind_response(path, item, props, user, propnames=False):
  213. """Build and return a PROPFIND response."""
  214. is_collection = isinstance(item, ical.Collection)
  215. if is_collection:
  216. with item.props as properties:
  217. collection_props = properties
  218. response = ET.Element(_tag("D", "response"))
  219. href = ET.Element(_tag("D", "href"))
  220. uri = item.url if is_collection else "%s/%s" % (path, item.name)
  221. href.text = _href(uri.replace("//", "/"))
  222. response.append(href)
  223. propstat404 = ET.Element(_tag("D", "propstat"))
  224. propstat200 = ET.Element(_tag("D", "propstat"))
  225. response.append(propstat200)
  226. prop200 = ET.Element(_tag("D", "prop"))
  227. propstat200.append(prop200)
  228. prop404 = ET.Element(_tag("D", "prop"))
  229. propstat404.append(prop404)
  230. if propnames:
  231. # Should list all properties that can be retrieved by the code below
  232. prop200.append(ET.Element(_tag("D", "getetag" )))
  233. prop200.append(ET.Element(_tag("D", "principal-URL" )))
  234. prop200.append(ET.Element(_tag("D", "principal-collection-set" )))
  235. prop200.append(ET.Element(_tag("C", "calendar-user-address-set" )))
  236. prop200.append(ET.Element(_tag("CR", "addressbook-home-set" )))
  237. prop200.append(ET.Element(_tag("C", "calendar-home-set" )))
  238. prop200.append(ET.Element(_tag("C", "supported-calendar-component-set")))
  239. prop200.append(ET.Element(_tag("D", "current-user-privilege-set" )))
  240. prop200.append(ET.Element(_tag("D", "supported-report-set" )))
  241. prop200.append(ET.Element(_tag("D", "getcontenttype" )))
  242. prop200.append(ET.Element(_tag("D", "resourcetype" )))
  243. if is_collection:
  244. prop200.append(ET.Element(_tag("CS", "getctag" )))
  245. prop200.append(ET.Element(_tag("C", "calendar-timezone" )))
  246. prop200.append(ET.Element(_tag("D", "displayname" )))
  247. prop200.append(ET.Element(_tag("ICAL", "calendar-color" )))
  248. if item.owner_url:
  249. prop200.append(ET.Element(_tag("D", "owner")))
  250. for tag in collection_props:
  251. clark_tag = _tag_from_human(tag)
  252. if prop200.find(clark_tag) is None:
  253. prop200.append(ET.Element(clark_tag))
  254. for tag in props:
  255. element = ET.Element(tag)
  256. is404 = False
  257. if tag == _tag("D", "getetag"):
  258. element.text = item.etag
  259. elif tag == _tag("D", "principal-URL"):
  260. tag = ET.Element(_tag("D", "href"))
  261. tag.text = _href(path)
  262. element.append(tag)
  263. elif tag in (_tag("D", "principal-collection-set"),
  264. _tag("C", "calendar-user-address-set"),
  265. _tag("CR", "addressbook-home-set"),
  266. _tag("C", "calendar-home-set")):
  267. tag = ET.Element(_tag("D", "href"))
  268. tag.text = _href(path)
  269. element.append(tag)
  270. elif tag == _tag("C", "supported-calendar-component-set"):
  271. # This is not a Todo
  272. # pylint: disable=W0511
  273. human_tag = _tag_from_clark(tag)
  274. if is_collection and human_tag in collection_props:
  275. # TODO: what do we have to do if it's not a collection?
  276. components = collection_props[human_tag].split(",")
  277. else:
  278. components = ("VTODO", "VEVENT", "VJOURNAL")
  279. for component in components:
  280. comp = ET.Element(_tag("C", "comp"))
  281. comp.set("name", component)
  282. element.append(comp)
  283. # pylint: enable=W0511
  284. elif tag == _tag("D", "current-user-principal") and user:
  285. tag = ET.Element(_tag("D", "href"))
  286. tag.text = _href("/%s/" % user)
  287. element.append(tag)
  288. elif tag == _tag("D", "current-user-privilege-set"):
  289. privilege = ET.Element(_tag("D", "privilege"))
  290. privilege.append(ET.Element(_tag("D", "all")))
  291. privilege.append(ET.Element(_tag("D", "read")))
  292. privilege.append(ET.Element(_tag("D", "write")))
  293. privilege.append(ET.Element(_tag("D", "write-properties")))
  294. privilege.append(ET.Element(_tag("D", "write-content")))
  295. element.append(privilege)
  296. elif tag == _tag("D", "supported-report-set"):
  297. for report_name in (
  298. "principal-property-search", "sync-collection",
  299. "expand-property", "principal-search-property-set"):
  300. supported = ET.Element(_tag("D", "supported-report"))
  301. report_tag = ET.Element(_tag("D", "report"))
  302. report_tag.text = report_name
  303. supported.append(report_tag)
  304. element.append(supported)
  305. elif is_collection:
  306. if tag == _tag("D", "getcontenttype"):
  307. element.text = item.mimetype
  308. elif tag == _tag("D", "resourcetype"):
  309. if item.is_principal:
  310. tag = ET.Element(_tag("D", "principal"))
  311. element.append(tag)
  312. if item.is_leaf(item.path) or (
  313. not item.exists and item.resource_type):
  314. # 2nd case happens when the collection is not stored yet,
  315. # but the resource type is guessed
  316. if item.resource_type == "addressbook":
  317. tag = ET.Element(_tag("CR", item.resource_type))
  318. else:
  319. tag = ET.Element(_tag("C", item.resource_type))
  320. element.append(tag)
  321. tag = ET.Element(_tag("D", "collection"))
  322. element.append(tag)
  323. elif tag == _tag("D", "owner") and item.owner_url:
  324. element.text = item.owner_url
  325. elif tag == _tag("CS", "getctag"):
  326. element.text = item.etag
  327. elif tag == _tag("C", "calendar-timezone"):
  328. element.text = ical.serialize(
  329. item.tag, item.headers, item.timezones)
  330. elif tag == _tag("D", "displayname"):
  331. element.text = item.name
  332. elif tag == _tag("ICAL", "calendar-color"):
  333. element.text = item.color
  334. else:
  335. human_tag = _tag_from_clark(tag)
  336. if human_tag in collection_props:
  337. element.text = collection_props[human_tag]
  338. else:
  339. is404 = True
  340. # Not for collections
  341. elif tag == _tag("D", "getcontenttype"):
  342. element.text = "%s; component=%s" % (
  343. item.mimetype, item.tag.lower())
  344. elif tag == _tag("D", "resourcetype"):
  345. # resourcetype must be returned empty for non-collection elements
  346. pass
  347. else:
  348. is404 = True
  349. if is404:
  350. prop404.append(element)
  351. else:
  352. prop200.append(element)
  353. status200 = ET.Element(_tag("D", "status"))
  354. status200.text = _response(200)
  355. propstat200.append(status200)
  356. status404 = ET.Element(_tag("D", "status"))
  357. status404.text = _response(404)
  358. propstat404.append(status404)
  359. if len(prop404):
  360. response.append(propstat404)
  361. return response
  362. def _add_propstat_to(element, tag, status_number):
  363. """Add a PROPSTAT response structure to an element.
  364. The PROPSTAT answer structure is defined in rfc4918-9.1. It is added to the
  365. given ``element``, for the following ``tag`` with the given
  366. ``status_number``.
  367. """
  368. propstat = ET.Element(_tag("D", "propstat"))
  369. element.append(propstat)
  370. prop = ET.Element(_tag("D", "prop"))
  371. propstat.append(prop)
  372. if "{" in tag:
  373. clark_tag = tag
  374. else:
  375. clark_tag = _tag(*tag.split(":", 1))
  376. prop_tag = ET.Element(clark_tag)
  377. prop.append(prop_tag)
  378. status = ET.Element(_tag("D", "status"))
  379. status.text = _response(status_number)
  380. propstat.append(status)
  381. def proppatch(path, xml_request, collection):
  382. """Read and answer PROPPATCH requests.
  383. Read rfc4918-9.2 for info.
  384. """
  385. # Reading request
  386. root = ET.fromstring(xml_request.encode("utf8"))
  387. props_to_set = props_from_request(root, actions=("set",))
  388. props_to_remove = props_from_request(root, actions=("remove",))
  389. # Writing answer
  390. multistatus = ET.Element(_tag("D", "multistatus"))
  391. response = ET.Element(_tag("D", "response"))
  392. multistatus.append(response)
  393. href = ET.Element(_tag("D", "href"))
  394. href.text = _href(path)
  395. response.append(href)
  396. with collection.props as collection_props:
  397. for short_name, value in props_to_set.items():
  398. if short_name.split(":")[-1] == "calendar-timezone":
  399. collection.replace(None, value)
  400. collection_props[short_name] = value
  401. _add_propstat_to(response, short_name, 200)
  402. for short_name in props_to_remove:
  403. try:
  404. del collection_props[short_name]
  405. except KeyError:
  406. _add_propstat_to(response, short_name, 412)
  407. else:
  408. _add_propstat_to(response, short_name, 200)
  409. return _pretty_xml(multistatus)
  410. def put(path, ical_request, collection):
  411. """Read PUT requests."""
  412. name = name_from_path(path, collection)
  413. if name in (item.name for item in collection.items):
  414. # PUT is modifying an existing item
  415. collection.replace(name, ical_request)
  416. elif name:
  417. # PUT is adding a new item
  418. collection.append(name, ical_request)
  419. else:
  420. # PUT is replacing the whole collection
  421. collection.save(ical_request)
  422. def report(path, xml_request, collection):
  423. """Read and answer REPORT requests.
  424. Read rfc3253-3.6 for info.
  425. """
  426. # Reading request
  427. root = ET.fromstring(xml_request.encode("utf8"))
  428. prop_element = root.find(_tag("D", "prop"))
  429. props = [prop.tag for prop in prop_element] if prop_element else []
  430. if collection:
  431. if root.tag in (_tag("C", "calendar-multiget"),
  432. _tag("CR", "addressbook-multiget")):
  433. # Read rfc4791-7.9 for info
  434. base_prefix = config.get("server", "base_prefix")
  435. hreferences = set()
  436. for href_element in root.findall(_tag("D", "href")):
  437. href_path = unquote(urlparse(href_element.text).path)
  438. if href_path.startswith(base_prefix):
  439. hreferences.add(href_path[len(base_prefix):])
  440. else:
  441. hreferences = (path,)
  442. # TODO: handle other filters
  443. # TODO: handle the nested comp-filters correctly
  444. # Read rfc4791-9.7.1 for info
  445. tag_filters = set(
  446. element.get("name") for element
  447. in root.findall(".//%s" % _tag("C", "comp-filter")))
  448. tag_filters.discard('VCALENDAR')
  449. else:
  450. hreferences = ()
  451. tag_filters = None
  452. # Writing answer
  453. multistatus = ET.Element(_tag("D", "multistatus"))
  454. collection_tag = collection.tag
  455. collection_items = collection.items
  456. collection_headers = collection.headers
  457. collection_timezones = collection.timezones
  458. for hreference in hreferences:
  459. # Check if the reference is an item or a collection
  460. name = name_from_path(hreference, collection)
  461. if name:
  462. # Reference is an item
  463. path = "/".join(hreference.split("/")[:-1]) + "/"
  464. items = (item for item in collection_items if item.name == name)
  465. else:
  466. # Reference is a collection
  467. path = hreference
  468. items = collection.components
  469. for item in items:
  470. if tag_filters and item.tag not in tag_filters:
  471. continue
  472. response = ET.Element(_tag("D", "response"))
  473. multistatus.append(response)
  474. href = ET.Element(_tag("D", "href"))
  475. href.text = _href("%s/%s" % (path.rstrip("/"), item.name))
  476. response.append(href)
  477. propstat = ET.Element(_tag("D", "propstat"))
  478. response.append(propstat)
  479. prop = ET.Element(_tag("D", "prop"))
  480. propstat.append(prop)
  481. for tag in props:
  482. element = ET.Element(tag)
  483. if tag == _tag("D", "getetag"):
  484. element.text = item.etag
  485. elif tag == _tag("D", "getcontenttype"):
  486. element.text = "%s; component=%s" % (
  487. item.mimetype, item.tag.lower())
  488. elif tag in (_tag("C", "calendar-data"),
  489. _tag("CR", "address-data")):
  490. if isinstance(item, ical.Component):
  491. element.text = ical.serialize(
  492. collection_tag, collection_headers,
  493. collection_timezones + [item])
  494. prop.append(element)
  495. status = ET.Element(_tag("D", "status"))
  496. status.text = _response(200)
  497. propstat.append(status)
  498. return _pretty_xml(multistatus)