xmlutils.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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, config, 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. output_encoding = config.get("encoding", "request")
  68. return ('<?xml version="1.0"?>\n' + ET.tostring(
  69. element, "utf-8").decode("utf-8")).encode(output_encoding)
  70. def _tag(short_name, local):
  71. """Get XML Clark notation {uri(``short_name``)}``local``."""
  72. return "{%s}%s" % (NAMESPACES[short_name], local)
  73. def _tag_from_clark(name):
  74. """Get a human-readable variant of the XML Clark notation tag ``name``.
  75. For a given name using the XML Clark notation, return a human-readable
  76. variant of the tag name for known namespaces. Otherwise, return the name as
  77. is.
  78. """
  79. match = CLARK_TAG_REGEX.match(name)
  80. if match and match.group("namespace") in NAMESPACES_REV:
  81. args = {
  82. "ns": NAMESPACES_REV[match.group("namespace")],
  83. "tag": match.group("tag")}
  84. return "%(ns)s:%(tag)s" % args
  85. return name
  86. def _response(code):
  87. """Return full W3C names from HTTP status codes."""
  88. return "HTTP/1.1 %i %s" % (code, client.responses[code])
  89. def _href(href):
  90. """Return prefixed href."""
  91. return "%s%s" % (config.get("server", "base_prefix"), 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(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. is_collection = isinstance(item, storage.Collection)
  187. if is_collection:
  188. # TODO: fix this
  189. is_leaf = bool(item.list())
  190. response = ET.Element(_tag("D", "response"))
  191. href = ET.Element(_tag("D", "href"))
  192. if is_collection:
  193. uri = item.path
  194. else:
  195. # TODO: fix this
  196. if path.split("/")[-1] == item.href:
  197. # Happening when depth is 0
  198. uri = path
  199. else:
  200. # Happening when depth is 1
  201. uri = "/".join((path, item.href))
  202. # TODO: fix this
  203. href.text = _href(uri.replace("//", "/"))
  204. response.append(href)
  205. propstat404 = ET.Element(_tag("D", "propstat"))
  206. propstat200 = ET.Element(_tag("D", "propstat"))
  207. response.append(propstat200)
  208. prop200 = ET.Element(_tag("D", "prop"))
  209. propstat200.append(prop200)
  210. prop404 = ET.Element(_tag("D", "prop"))
  211. propstat404.append(prop404)
  212. for tag in props:
  213. element = ET.Element(tag)
  214. is404 = False
  215. if tag == _tag("D", "getetag"):
  216. element.text = item.etag
  217. elif tag == _tag("D", "principal-URL"):
  218. tag = ET.Element(_tag("D", "href"))
  219. tag.text = _href(path)
  220. element.append(tag)
  221. elif tag == _tag("D", "getlastmodified"):
  222. element.text = item.last_modified
  223. elif tag in (_tag("D", "principal-collection-set"),
  224. _tag("C", "calendar-user-address-set"),
  225. _tag("CR", "addressbook-home-set"),
  226. _tag("C", "calendar-home-set")):
  227. tag = ET.Element(_tag("D", "href"))
  228. tag.text = _href(path)
  229. element.append(tag)
  230. elif tag == _tag("C", "supported-calendar-component-set"):
  231. # This is not a Todo
  232. # pylint: disable=W0511
  233. human_tag = _tag_from_clark(tag)
  234. if is_collection and is_leaf:
  235. meta = item.get_meta(human_tag)
  236. if meta:
  237. components = meta.split(",")
  238. else:
  239. components = ("VTODO", "VEVENT", "VJOURNAL")
  240. for component in components:
  241. comp = ET.Element(_tag("C", "comp"))
  242. comp.set("name", component)
  243. element.append(comp)
  244. else:
  245. is404 = True
  246. # pylint: enable=W0511
  247. elif tag == _tag("D", "current-user-principal") and user:
  248. tag = ET.Element(_tag("D", "href"))
  249. tag.text = _href("/%s/" % user)
  250. element.append(tag)
  251. elif tag == _tag("D", "current-user-privilege-set"):
  252. privilege = ET.Element(_tag("D", "privilege"))
  253. if write:
  254. privilege.append(ET.Element(_tag("D", "all")))
  255. privilege.append(ET.Element(_tag("D", "write")))
  256. privilege.append(ET.Element(_tag("D", "write-properties")))
  257. privilege.append(ET.Element(_tag("D", "write-content")))
  258. privilege.append(ET.Element(_tag("D", "read")))
  259. element.append(privilege)
  260. elif tag == _tag("D", "supported-report-set"):
  261. for report_name in (
  262. "principal-property-search", "sync-collection",
  263. "expand-property", "principal-search-property-set"):
  264. supported = ET.Element(_tag("D", "supported-report"))
  265. report_tag = ET.Element(_tag("D", "report"))
  266. report_tag.text = report_name
  267. supported.append(report_tag)
  268. element.append(supported)
  269. elif is_collection:
  270. if tag == _tag("D", "getcontenttype"):
  271. element.text = storage.MIMETYPES[item.get_meta("tag")]
  272. elif tag == _tag("D", "resourcetype"):
  273. if item.is_principal:
  274. tag = ET.Element(_tag("D", "principal"))
  275. element.append(tag)
  276. item_tag = item.get_meta("tag")
  277. if is_leaf or item_tag:
  278. # 2nd case happens when the collection is not stored yet,
  279. # but the resource type is guessed
  280. if item.get_meta("tag") == "VADDRESSBOOK":
  281. tag = ET.Element(_tag("CR", "addressbook"))
  282. element.append(tag)
  283. elif item.get_meta("tag") == "VCALENDAR":
  284. tag = ET.Element(_tag("C", "calendar"))
  285. element.append(tag)
  286. tag = ET.Element(_tag("D", "collection"))
  287. element.append(tag)
  288. elif is_leaf:
  289. if tag == _tag("D", "owner") and item.owner:
  290. element.text = "/%s/" % item.owner
  291. elif tag == _tag("CS", "getctag"):
  292. element.text = item.etag
  293. elif tag == _tag("C", "calendar-timezone"):
  294. timezones = {}
  295. for event in item.list():
  296. if "vtimezone" in event.content:
  297. for timezone in event.vtimezone_list:
  298. timezones.add(timezone)
  299. collection = vobject.iCalendar()
  300. for timezone in timezones:
  301. collection.add(timezone)
  302. element.text = collection.serialize()
  303. elif tag == _tag("D", "displayname"):
  304. element.text = item.get_meta("D:displayname") or item.path
  305. elif tag == _tag("ICAL", "calendar-color"):
  306. element.text = item.get_meta("ICAL:calendar-color")
  307. else:
  308. human_tag = _tag_from_clark(tag)
  309. meta = item.get_meta(human_tag)
  310. if meta:
  311. element.text = meta
  312. else:
  313. is404 = True
  314. else:
  315. is404 = True
  316. # Not for collections
  317. elif tag == _tag("D", "getcontenttype"):
  318. name = item.name.lower()
  319. mimetype = "text/vcard" if name == "vcard" else "text/calendar"
  320. element.text = "%s; component=%s" % (mimetype, name)
  321. elif tag == _tag("D", "resourcetype"):
  322. # resourcetype must be returned empty for non-collection elements
  323. pass
  324. elif tag == _tag("D", "getcontentlength"):
  325. element.text = str(item.content_length)
  326. else:
  327. is404 = True
  328. if is404:
  329. prop404.append(element)
  330. else:
  331. prop200.append(element)
  332. status200 = ET.Element(_tag("D", "status"))
  333. status200.text = _response(200)
  334. propstat200.append(status200)
  335. status404 = ET.Element(_tag("D", "status"))
  336. status404.text = _response(404)
  337. propstat404.append(status404)
  338. if len(prop404):
  339. response.append(propstat404)
  340. return response
  341. def _add_propstat_to(element, tag, status_number):
  342. """Add a PROPSTAT response structure to an element.
  343. The PROPSTAT answer structure is defined in rfc4918-9.1. It is added to the
  344. given ``element``, for the following ``tag`` with the given
  345. ``status_number``.
  346. """
  347. propstat = ET.Element(_tag("D", "propstat"))
  348. element.append(propstat)
  349. prop = ET.Element(_tag("D", "prop"))
  350. propstat.append(prop)
  351. if "{" in tag:
  352. clark_tag = tag
  353. else:
  354. clark_tag = _tag(*tag.split(":", 1))
  355. prop_tag = ET.Element(clark_tag)
  356. prop.append(prop_tag)
  357. status = ET.Element(_tag("D", "status"))
  358. status.text = _response(status_number)
  359. propstat.append(status)
  360. def proppatch(path, xml_request, collection):
  361. """Read and answer PROPPATCH requests.
  362. Read rfc4918-9.2 for info.
  363. """
  364. # Reading request
  365. root = ET.fromstring(xml_request.encode("utf8"))
  366. props_to_set = props_from_request(root, actions=("set",))
  367. props_to_remove = props_from_request(root, actions=("remove",))
  368. # Writing answer
  369. multistatus = ET.Element(_tag("D", "multistatus"))
  370. response = ET.Element(_tag("D", "response"))
  371. multistatus.append(response)
  372. href = ET.Element(_tag("D", "href"))
  373. href.text = _href(path)
  374. response.append(href)
  375. for short_name, value in props_to_set.items():
  376. collection.set_meta(short_name, value)
  377. _add_propstat_to(response, short_name, 200)
  378. for short_name in props_to_remove:
  379. collection.set_meta(short_name, '')
  380. _add_propstat_to(response, short_name, 200)
  381. return _pretty_xml(multistatus)
  382. def put(path, ical_request, collection):
  383. """Read PUT requests."""
  384. name = name_from_path(path, collection)
  385. items = list(vobject.readComponents(ical_request))
  386. if items:
  387. if collection.has(name):
  388. # PUT is modifying an existing item
  389. return collection.update(name, items[0])
  390. elif name:
  391. # PUT is adding a new item
  392. return collection.upload(name, items[0])
  393. else:
  394. # PUT is replacing the whole collection
  395. collection.delete()
  396. return storage.Collection.create_collection(path, items)
  397. def report(path, xml_request, collection):
  398. """Read and answer REPORT requests.
  399. Read rfc3253-3.6 for info.
  400. """
  401. # Reading request
  402. root = ET.fromstring(xml_request.encode("utf8"))
  403. prop_element = root.find(_tag("D", "prop"))
  404. props = (
  405. [prop.tag for prop in prop_element]
  406. if prop_element is not None else [])
  407. if collection:
  408. if root.tag in (_tag("C", "calendar-multiget"),
  409. _tag("CR", "addressbook-multiget")):
  410. # Read rfc4791-7.9 for info
  411. base_prefix = config.get("server", "base_prefix")
  412. hreferences = set()
  413. for href_element in root.findall(_tag("D", "href")):
  414. href_path = unquote(urlparse(href_element.text).path)
  415. if href_path.startswith(base_prefix):
  416. hreferences.add(href_path[len(base_prefix) - 1:])
  417. else:
  418. hreferences = (path,)
  419. # TODO: handle other filters
  420. # TODO: handle the nested comp-filters correctly
  421. # Read rfc4791-9.7.1 for info
  422. tag_filters = set(
  423. element.get("name") for element
  424. in root.findall(".//%s" % _tag("C", "comp-filter")))
  425. else:
  426. hreferences = ()
  427. tag_filters = None
  428. # Writing answer
  429. multistatus = ET.Element(_tag("D", "multistatus"))
  430. for hreference in hreferences:
  431. # Check if the reference is an item or a collection
  432. name = name_from_path(hreference, collection)
  433. if name:
  434. # Reference is an item
  435. path = "/".join(hreference.split("/")[:-1]) + "/"
  436. item = collection.get(name)
  437. if item is None:
  438. multistatus.append(
  439. _item_response(hreference, found_item=False))
  440. continue
  441. items = [item]
  442. else:
  443. # Reference is a collection
  444. path = hreference
  445. items = [collection.get(href) for href, etag in collection.list()]
  446. for item in items:
  447. if tag_filters and item.name not in tag_filters:
  448. continue
  449. found_props = []
  450. not_found_props = []
  451. for tag in props:
  452. element = ET.Element(tag)
  453. if tag == _tag("D", "getetag"):
  454. element.text = item.etag
  455. found_props.append(element)
  456. elif tag == _tag("D", "getcontenttype"):
  457. name = item.name.lower()
  458. mimetype = (
  459. "text/vcard" if name == "vcard" else "text/calendar")
  460. element.text = "%s; component=%s" % (mimetype, name)
  461. found_props.append(element)
  462. elif tag in (_tag("C", "calendar-data"),
  463. _tag("CR", "address-data")):
  464. if isinstance(item, (storage.Item, storage.Collection)):
  465. element.text = item.serialize()
  466. found_props.append(element)
  467. else:
  468. not_found_props.append(element)
  469. # TODO: fix this
  470. if hreference.split("/")[-1] == item.href:
  471. # Happening when depth is 0
  472. uri = hreference
  473. else:
  474. # Happening when depth is 1
  475. uri = posixpath.join(hreference, item.href)
  476. multistatus.append(_item_response(
  477. uri, found_props=found_props,
  478. not_found_props=not_found_props, found_item=True))
  479. return _pretty_xml(multistatus)
  480. def _item_response(href, found_props=(), not_found_props=(), found_item=True):
  481. response = ET.Element(_tag("D", "response"))
  482. href_tag = ET.Element(_tag("D", "href"))
  483. href_tag.text = href
  484. response.append(href_tag)
  485. if found_item:
  486. if found_props:
  487. propstat = ET.Element(_tag("D", "propstat"))
  488. status = ET.Element(_tag("D", "status"))
  489. status.text = _response(200)
  490. prop = ET.Element(_tag("D", "prop"))
  491. for p in found_props:
  492. prop.append(p)
  493. propstat.append(prop)
  494. propstat.append(status)
  495. response.append(propstat)
  496. if not_found_props:
  497. propstat = ET.Element(_tag("D", "propstat"))
  498. status = ET.Element(_tag("D", "status"))
  499. status.text = _response(404)
  500. prop = ET.Element(_tag("D", "prop"))
  501. for p in not_found_props:
  502. prop.append(p)
  503. propstat.append(prop)
  504. propstat.append(status)
  505. response.append(propstat)
  506. else:
  507. status = ET.Element(_tag("D", "status"))
  508. status.text = _response(404)
  509. response.append(status)
  510. return response