xmlutils.py 21 KB

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