1
0

xmlutils.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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 not hasattr(root, "tag"):
  102. root = ET.fromstring(root.encode("utf8"))
  103. for action in actions:
  104. action_element = root.find(_tag("D", action))
  105. if action_element is not None:
  106. break
  107. else:
  108. action_element = root
  109. prop_element = action_element.find(_tag("D", "prop"))
  110. if prop_element is not None:
  111. for prop in prop_element:
  112. if prop.tag == _tag("D", "resourcetype"):
  113. for resource_type in prop:
  114. if resource_type.tag == _tag("C", "calendar"):
  115. result["tag"] = "VCALENDAR"
  116. break
  117. elif resource_type.tag == _tag("CR", "addressbook"):
  118. result["tag"] = "VADDRESSBOOK"
  119. break
  120. elif prop.tag == _tag("C", "supported-calendar-component-set"):
  121. result[_tag_from_clark(prop.tag)] = ",".join(
  122. supported_comp.attrib["name"]
  123. for supported_comp in prop
  124. if supported_comp.tag == _tag("C", "comp"))
  125. else:
  126. result[_tag_from_clark(prop.tag)] = prop.text
  127. return result
  128. def delete(path, collection):
  129. """Read and answer DELETE requests.
  130. Read rfc4918-9.6 for info.
  131. """
  132. # Reading request
  133. if collection.path == path.strip("/"):
  134. # Delete the whole collection
  135. collection.delete()
  136. else:
  137. # Remove an item from the collection
  138. collection.delete(name_from_path(path, collection))
  139. # Writing answer
  140. multistatus = ET.Element(_tag("D", "multistatus"))
  141. response = ET.Element(_tag("D", "response"))
  142. multistatus.append(response)
  143. href = ET.Element(_tag("D", "href"))
  144. href.text = _href(path)
  145. response.append(href)
  146. status = ET.Element(_tag("D", "status"))
  147. status.text = _response(200)
  148. response.append(status)
  149. return _pretty_xml(multistatus)
  150. def propfind(path, xml_request, read_collections, write_collections, user=None):
  151. """Read and answer PROPFIND requests.
  152. Read rfc4918-9.1 for info.
  153. The collections parameter is a list of collections that are to be included
  154. in the output.
  155. """
  156. # Reading request
  157. if xml_request:
  158. root = ET.fromstring(xml_request.encode("utf8"))
  159. props = [prop.tag for prop in root.find(_tag("D", "prop"))]
  160. else:
  161. props = [_tag("D", "getcontenttype"),
  162. _tag("D", "resourcetype"),
  163. _tag("D", "displayname"),
  164. _tag("D", "owner"),
  165. _tag("D", "getetag"),
  166. _tag("ICAL", "calendar-color"),
  167. _tag("CS", "getctag")]
  168. # Writing answer
  169. multistatus = ET.Element(_tag("D", "multistatus"))
  170. collections = []
  171. for collection in write_collections:
  172. collections.append(collection)
  173. response = _propfind_response(path, collection, props, user, write=True)
  174. multistatus.append(response)
  175. for collection in read_collections:
  176. if collection in collections:
  177. continue
  178. response = _propfind_response(path, collection, props, user, write=False)
  179. multistatus.append(response)
  180. return _pretty_xml(multistatus)
  181. def _propfind_response(path, item, props, user, write=False):
  182. """Build and return a PROPFIND response."""
  183. is_collection = isinstance(item, storage.Collection)
  184. if is_collection:
  185. # TODO: fix this
  186. is_leaf = bool(item.list())
  187. response = ET.Element(_tag("D", "response"))
  188. href = ET.Element(_tag("D", "href"))
  189. if is_collection:
  190. uri = item.path
  191. else:
  192. # TODO: fix this
  193. if path.split("/")[-1] == item.href:
  194. # Happening when depth is 0
  195. uri = path
  196. else:
  197. # Happening when depth is 1
  198. uri = "/".join((path, item.href))
  199. # TODO: fix this
  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 = storage.get_etag(item.serialize())
  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 is_leaf:
  230. meta = item.get_meta(human_tag)
  231. if meta:
  232. components = meta.split(",")
  233. else:
  234. components = ("VTODO", "VEVENT", "VJOURNAL")
  235. for component in components:
  236. comp = ET.Element(_tag("C", "comp"))
  237. comp.set("name", component)
  238. element.append(comp)
  239. else:
  240. is404 = True
  241. # pylint: enable=W0511
  242. elif tag == _tag("D", "current-user-principal") and user:
  243. tag = ET.Element(_tag("D", "href"))
  244. tag.text = _href("/%s/" % user)
  245. element.append(tag)
  246. elif tag == _tag("D", "current-user-privilege-set"):
  247. privilege = ET.Element(_tag("D", "privilege"))
  248. if write:
  249. privilege.append(ET.Element(_tag("D", "all")))
  250. privilege.append(ET.Element(_tag("D", "write")))
  251. privilege.append(ET.Element(_tag("D", "write-properties")))
  252. privilege.append(ET.Element(_tag("D", "write-content")))
  253. privilege.append(ET.Element(_tag("D", "read")))
  254. element.append(privilege)
  255. elif tag == _tag("D", "supported-report-set"):
  256. for report_name in (
  257. "principal-property-search", "sync-collection",
  258. "expand-property", "principal-search-property-set"):
  259. supported = ET.Element(_tag("D", "supported-report"))
  260. report_tag = ET.Element(_tag("D", "report"))
  261. report_tag.text = report_name
  262. supported.append(report_tag)
  263. element.append(supported)
  264. elif is_collection:
  265. if tag == _tag("D", "getcontenttype"):
  266. element.text = storage.MIMETYPES[item.get_meta("tag")]
  267. elif tag == _tag("D", "resourcetype"):
  268. if item.is_principal:
  269. tag = ET.Element(_tag("D", "principal"))
  270. element.append(tag)
  271. item_tag = item.get_meta("tag")
  272. if is_leaf or item_tag:
  273. # 2nd case happens when the collection is not stored yet,
  274. # but the resource type is guessed
  275. if item.get_meta("tag") == "VADDRESSBOOK":
  276. tag = ET.Element(_tag("CR", "addressbook"))
  277. element.append(tag)
  278. elif item.get_meta("tag") == "VCALENDAR":
  279. tag = ET.Element(_tag("C", "calendar"))
  280. element.append(tag)
  281. tag = ET.Element(_tag("D", "collection"))
  282. element.append(tag)
  283. elif is_leaf:
  284. if tag == _tag("D", "owner") and item.owner:
  285. element.text = "/%s/" % item.owner
  286. elif tag == _tag("CS", "getctag"):
  287. element.text = item.etag
  288. elif tag == _tag("C", "calendar-timezone"):
  289. timezones = {}
  290. for event in item.list():
  291. if "vtimezone" in event.content:
  292. for timezone in event.vtimezone_list:
  293. timezones.add(timezone)
  294. collection = vobject.iCalendar()
  295. for timezone in timezones:
  296. collection.add(timezone)
  297. element.text = collection.serialize()
  298. elif tag == _tag("D", "displayname"):
  299. element.text = item.get_meta("D:displayname") or item.path
  300. elif tag == _tag("ICAL", "calendar-color"):
  301. element.text = item.get_meta("ICAL:calendar-color")
  302. else:
  303. human_tag = _tag_from_clark(tag)
  304. meta = item.get_meta(human_tag)
  305. if meta:
  306. element.text = meta
  307. else:
  308. is404 = True
  309. else:
  310. is404 = True
  311. # Not for collections
  312. elif tag == _tag("D", "getcontenttype"):
  313. name = item.name.lower()
  314. mimetype = "text/vcard" if name == "vcard" else "text/calendar"
  315. element.text = "%s; component=%s" % (mimetype, name)
  316. elif tag == _tag("D", "resourcetype"):
  317. # resourcetype must be returned empty for non-collection elements
  318. pass
  319. else:
  320. is404 = True
  321. if is404:
  322. prop404.append(element)
  323. else:
  324. prop200.append(element)
  325. status200 = ET.Element(_tag("D", "status"))
  326. status200.text = _response(200)
  327. propstat200.append(status200)
  328. status404 = ET.Element(_tag("D", "status"))
  329. status404.text = _response(404)
  330. propstat404.append(status404)
  331. if len(prop404):
  332. response.append(propstat404)
  333. return response
  334. def _add_propstat_to(element, tag, status_number):
  335. """Add a PROPSTAT response structure to an element.
  336. The PROPSTAT answer structure is defined in rfc4918-9.1. It is added to the
  337. given ``element``, for the following ``tag`` with the given
  338. ``status_number``.
  339. """
  340. propstat = ET.Element(_tag("D", "propstat"))
  341. element.append(propstat)
  342. prop = ET.Element(_tag("D", "prop"))
  343. propstat.append(prop)
  344. if "{" in tag:
  345. clark_tag = tag
  346. else:
  347. clark_tag = _tag(*tag.split(":", 1))
  348. prop_tag = ET.Element(clark_tag)
  349. prop.append(prop_tag)
  350. status = ET.Element(_tag("D", "status"))
  351. status.text = _response(status_number)
  352. propstat.append(status)
  353. def proppatch(path, xml_request, collection):
  354. """Read and answer PROPPATCH requests.
  355. Read rfc4918-9.2 for info.
  356. """
  357. # Reading request
  358. root = ET.fromstring(xml_request.encode("utf8"))
  359. props_to_set = props_from_request(root, actions=("set",))
  360. props_to_remove = props_from_request(root, actions=("remove",))
  361. # Writing answer
  362. multistatus = ET.Element(_tag("D", "multistatus"))
  363. response = ET.Element(_tag("D", "response"))
  364. multistatus.append(response)
  365. href = ET.Element(_tag("D", "href"))
  366. href.text = _href(path)
  367. response.append(href)
  368. with collection.props as collection_props:
  369. for short_name, value in props_to_set.items():
  370. if short_name.split(":")[-1] == "calendar-timezone":
  371. collection.replace(None, value)
  372. collection_props[short_name] = value
  373. _add_propstat_to(response, short_name, 200)
  374. for short_name in props_to_remove:
  375. try:
  376. del collection_props[short_name]
  377. except KeyError:
  378. _add_propstat_to(response, short_name, 412)
  379. else:
  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):])
  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. try:
  437. items = [collection.get(name)]
  438. except KeyError:
  439. multistatus.append(
  440. _item_response(hreference, found_item=False))
  441. continue
  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