xmlutils.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Radicale Server - Calendar Server
  4. # Copyright © 2008-2011 Guillaume Ayoub
  5. # Copyright © 2008 Nicolas Kandel
  6. # Copyright © 2008 Pascal Halter
  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. import xml.etree.ElementTree as ET
  27. from radicale import client, config, ical
  28. NAMESPACES = {
  29. "C": "urn:ietf:params:xml:ns:caldav",
  30. "D": "DAV:",
  31. "CS": "http://calendarserver.org/ns/"}
  32. def _tag(short_name, local):
  33. """Get XML Clark notation {uri(``short_name``)}``local``."""
  34. return "{%s}%s" % (NAMESPACES[short_name], local)
  35. def _response(code):
  36. """Return full W3C names from HTTP status codes."""
  37. return "HTTP/1.1 %i %s" % (code, client.responses[code])
  38. def name_from_path(path, calendar):
  39. """Return Radicale item name from ``path``."""
  40. calendar_parts = calendar.local_path.strip("/").split("/")
  41. path_parts = path.strip("/").split("/")
  42. return path_parts[-1] if (len(path_parts) - len(calendar_parts)) else None
  43. def delete(path, calendar):
  44. """Read and answer DELETE requests.
  45. Read rfc4918-9.6 for info.
  46. """
  47. # Reading request
  48. calendar.remove(name_from_path(path, calendar))
  49. # Writing answer
  50. multistatus = ET.Element(_tag("D", "multistatus"))
  51. response = ET.Element(_tag("D", "response"))
  52. multistatus.append(response)
  53. href = ET.Element(_tag("D", "href"))
  54. href.text = path
  55. response.append(href)
  56. status = ET.Element(_tag("D", "status"))
  57. status.text = _response(200)
  58. response.append(status)
  59. return ET.tostring(multistatus, config.get("encoding", "request"))
  60. def propfind(path, xml_request, calendar, depth):
  61. """Read and answer PROPFIND requests.
  62. Read rfc4918-9.1 for info.
  63. """
  64. # Reading request
  65. root = ET.fromstring(xml_request)
  66. prop_element = root.find(_tag("D", "prop"))
  67. prop_list = prop_element.getchildren()
  68. props = [prop.tag for prop in prop_list]
  69. # Writing answer
  70. multistatus = ET.Element(_tag("D", "multistatus"))
  71. if calendar:
  72. if depth == "0":
  73. items = [calendar]
  74. else:
  75. # depth is 1, infinity or not specified
  76. # we limit ourselves to depth == 1
  77. items = [calendar] + calendar.components
  78. else:
  79. items = []
  80. for item in items:
  81. is_calendar = isinstance(item, ical.Calendar)
  82. response = ET.Element(_tag("D", "response"))
  83. multistatus.append(response)
  84. href = ET.Element(_tag("D", "href"))
  85. href.text = path if is_calendar else path + item.name
  86. response.append(href)
  87. propstat = ET.Element(_tag("D", "propstat"))
  88. response.append(propstat)
  89. prop = ET.Element(_tag("D", "prop"))
  90. propstat.append(prop)
  91. for tag in props:
  92. element = ET.Element(tag)
  93. if tag == _tag("D", "resourcetype") and is_calendar:
  94. tag = ET.Element(_tag("C", "calendar"))
  95. element.append(tag)
  96. tag = ET.Element(_tag("D", "collection"))
  97. element.append(tag)
  98. elif tag == _tag("D", "owner"):
  99. if calendar.owner:
  100. element.text = calendar.owner
  101. elif tag == _tag("D", "getcontenttype"):
  102. element.text = "text/calendar"
  103. elif tag == _tag("CS", "getctag") and is_calendar:
  104. element.text = item.etag
  105. elif tag == _tag("D", "getetag"):
  106. element.text = item.etag
  107. elif tag == _tag("D", "displayname") and is_calendar:
  108. element.text = calendar.name
  109. elif tag == _tag("D", "principal-URL"):
  110. # TODO: use a real principal URL, read rfc3744-4.2 for info
  111. tag = ET.Element(_tag("D", "href"))
  112. tag.text = path
  113. element.append(tag)
  114. elif tag in (
  115. _tag("D", "principal-collection-set"),
  116. _tag("C", "calendar-user-address-set"),
  117. _tag("C", "calendar-home-set")):
  118. tag = ET.Element(_tag("D", "href"))
  119. tag.text = path
  120. element.append(tag)
  121. elif tag == _tag("C", "supported-calendar-component-set"):
  122. # This is not a Todo
  123. # pylint: disable=W0511
  124. for component in ("VTODO", "VEVENT", "VJOURNAL"):
  125. comp = ET.Element(_tag("C", "comp"))
  126. comp.set("name", component)
  127. element.append(comp)
  128. # pylint: enable=W0511
  129. elif tag == _tag("D", "current-user-privilege-set"):
  130. privilege = ET.Element(_tag("D", "privilege"))
  131. privilege.append(ET.Element(_tag("D", "all")))
  132. element.append(privilege)
  133. elif tag == _tag("D", "supported-report-set"):
  134. for report_name in (
  135. "principal-property-search", "sync-collection"
  136. "expand-property", "principal-search-property-set"):
  137. supported = ET.Element(_tag("D", "supported-report"))
  138. report_tag = ET.Element(_tag("D", "report"))
  139. report_tag.text = report_name
  140. supported.append(report_tag)
  141. element.append(supported)
  142. prop.append(element)
  143. status = ET.Element(_tag("D", "status"))
  144. status.text = _response(200)
  145. propstat.append(status)
  146. return ET.tostring(multistatus, config.get("encoding", "request"))
  147. def proppatch(path, xml_request, calendar):
  148. """Read and answer PROPPATCH requests.
  149. Read rfc4918-9.2 for info.
  150. """
  151. # Reading request
  152. root = ET.fromstring(xml_request)
  153. props = []
  154. for action in ("set", "remove"):
  155. action_element = root.find(_tag("D", action))
  156. if action_element is not None:
  157. prop_element = action_element.find(_tag("D", "prop"))
  158. prop_list = prop_element.getchildren()
  159. props.extend(prop.tag for prop in prop_list)
  160. # Writing answer
  161. multistatus = ET.Element(_tag("D", "multistatus"))
  162. response = ET.Element(_tag("D", "response"))
  163. multistatus.append(response)
  164. href = ET.Element(_tag("D", "href"))
  165. href.text = path
  166. response.append(href)
  167. propstat = ET.Element(_tag("D", "propstat"))
  168. response.append(propstat)
  169. prop = ET.Element(_tag("D", "prop"))
  170. propstat.append(prop)
  171. for tag in props:
  172. element = ET.Element(tag)
  173. prop.append(element)
  174. status = ET.Element(_tag("D", "status"))
  175. status.text = _response(200)
  176. propstat.append(status)
  177. return ET.tostring(multistatus, config.get("encoding", "request"))
  178. def put(path, ical_request, calendar):
  179. """Read PUT requests."""
  180. name = name_from_path(path, calendar)
  181. if name in (item.name for item in calendar.items):
  182. # PUT is modifying an existing item
  183. calendar.replace(name, ical_request)
  184. else:
  185. # PUT is adding a new item
  186. calendar.append(name, ical_request)
  187. def report(path, xml_request, calendar):
  188. """Read and answer REPORT requests.
  189. Read rfc3253-3.6 for info.
  190. """
  191. # Reading request
  192. root = ET.fromstring(xml_request)
  193. prop_element = root.find(_tag("D", "prop"))
  194. prop_list = prop_element.getchildren()
  195. props = [prop.tag for prop in prop_list]
  196. if calendar:
  197. if root.tag == _tag("C", "calendar-multiget"):
  198. # Read rfc4791-7.9 for info
  199. hreferences = set((href_element.text for href_element
  200. in root.findall(_tag("D", "href"))))
  201. else:
  202. hreferences = (path,)
  203. else:
  204. hreferences = ()
  205. # Writing answer
  206. multistatus = ET.Element(_tag("D", "multistatus"))
  207. for hreference in hreferences:
  208. # Check if the reference is an item or a calendar
  209. name = name_from_path(hreference, calendar)
  210. if name:
  211. # Reference is an item
  212. path = "/".join(hreference.split("/")[:-1]) + "/"
  213. items = (item for item in calendar.items if item.name == name)
  214. else:
  215. # Reference is a calendar
  216. path = hreference
  217. items = calendar.components
  218. for item in items:
  219. response = ET.Element(_tag("D", "response"))
  220. multistatus.append(response)
  221. href = ET.Element(_tag("D", "href"))
  222. href.text = path + item.name
  223. response.append(href)
  224. propstat = ET.Element(_tag("D", "propstat"))
  225. response.append(propstat)
  226. prop = ET.Element(_tag("D", "prop"))
  227. propstat.append(prop)
  228. for tag in props:
  229. element = ET.Element(tag)
  230. if tag == _tag("D", "getetag"):
  231. element.text = item.etag
  232. elif tag == _tag("C", "calendar-data"):
  233. if isinstance(item, (ical.Event, ical.Todo, ical.Journal)):
  234. element.text = ical.serialize(
  235. calendar.headers, calendar.timezones + [item])
  236. prop.append(element)
  237. status = ET.Element(_tag("D", "status"))
  238. status.text = _response(200)
  239. propstat.append(status)
  240. return ET.tostring(multistatus, config.get("encoding", "request"))