xmlutils.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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):
  39. """Return Radicale item name from ``path``."""
  40. path_parts = path.strip("/").split("/")
  41. return path_parts[-1] if len(path_parts) > 2 else None
  42. def delete(path, calendar):
  43. """Read and answer DELETE requests.
  44. Read rfc4918-9.6 for info.
  45. """
  46. # Reading request
  47. calendar.remove(name_from_path(path))
  48. # Writing answer
  49. multistatus = ET.Element(_tag("D", "multistatus"))
  50. response = ET.Element(_tag("D", "response"))
  51. multistatus.append(response)
  52. href = ET.Element(_tag("D", "href"))
  53. href.text = path
  54. response.append(href)
  55. status = ET.Element(_tag("D", "status"))
  56. status.text = _response(200)
  57. response.append(status)
  58. return ET.tostring(multistatus, config.get("encoding", "request"))
  59. def propfind(path, xml_request, calendar, depth):
  60. """Read and answer PROPFIND requests.
  61. Read rfc4918-9.1 for info.
  62. """
  63. # Reading request
  64. root = ET.fromstring(xml_request)
  65. prop_element = root.find(_tag("D", "prop"))
  66. prop_list = prop_element.getchildren()
  67. props = [prop.tag for prop in prop_list]
  68. # Writing answer
  69. multistatus = ET.Element(_tag("D", "multistatus"))
  70. if calendar:
  71. if depth == "0":
  72. items = [calendar]
  73. else:
  74. # depth is 1, infinity or not specified
  75. # we limit ourselves to depth == 1
  76. items = [calendar] + calendar.events + calendar.todos
  77. else:
  78. items = []
  79. for item in items:
  80. is_calendar = isinstance(item, ical.Calendar)
  81. response = ET.Element(_tag("D", "response"))
  82. multistatus.append(response)
  83. href = ET.Element(_tag("D", "href"))
  84. href.text = path if is_calendar else path + item.name
  85. response.append(href)
  86. propstat = ET.Element(_tag("D", "propstat"))
  87. response.append(propstat)
  88. prop = ET.Element(_tag("D", "prop"))
  89. propstat.append(prop)
  90. for tag in props:
  91. element = ET.Element(tag)
  92. if tag == _tag("D", "resourcetype") and is_calendar:
  93. tag = ET.Element(_tag("C", "calendar"))
  94. element.append(tag)
  95. tag = ET.Element(_tag("D", "collection"))
  96. element.append(tag)
  97. elif tag == _tag("D", "owner"):
  98. element.text = calendar.owner
  99. elif tag == _tag("D", "getcontenttype"):
  100. element.text = "text/calendar"
  101. elif tag == _tag("CS", "getctag") and is_calendar:
  102. element.text = item.etag
  103. elif tag == _tag("D", "getetag"):
  104. element.text = item.etag
  105. elif tag == _tag("D", "displayname") and is_calendar:
  106. element.text = calendar.name
  107. elif tag == _tag("D", "principal-URL"):
  108. # TODO: use a real principal URL, read rfc3744-4.2 for info
  109. tag = ET.Element(_tag("D", "href"))
  110. tag.text = path
  111. element.append(tag)
  112. elif tag in (
  113. _tag("D", "principal-collection-set"),
  114. _tag("C", "calendar-user-address-set"),
  115. _tag("C", "calendar-home-set")):
  116. tag = ET.Element(_tag("D", "href"))
  117. tag.text = path
  118. element.append(tag)
  119. elif tag == _tag("C", "supported-calendar-component-set"):
  120. comp = ET.Element(_tag("C", "comp"))
  121. comp.set("name", "VTODO") # pylint: disable=W0511
  122. element.append(comp)
  123. comp = ET.Element(_tag("C", "comp"))
  124. comp.set("name", "VEVENT")
  125. element.append(comp)
  126. elif tag == _tag("D", "current-user-privilege-set"):
  127. privilege = ET.Element(_tag("D", "privilege"))
  128. privilege.append(ET.Element(_tag("D", "all")))
  129. element.append(privilege)
  130. prop.append(element)
  131. status = ET.Element(_tag("D", "status"))
  132. status.text = _response(200)
  133. propstat.append(status)
  134. return ET.tostring(multistatus, config.get("encoding", "request"))
  135. def put(path, ical_request, calendar):
  136. """Read PUT requests."""
  137. name = name_from_path(path)
  138. if name in (item.name for item in calendar.items):
  139. # PUT is modifying an existing item
  140. calendar.replace(name, ical_request)
  141. else:
  142. # PUT is adding a new item
  143. calendar.append(name, ical_request)
  144. def report(path, xml_request, calendar):
  145. """Read and answer REPORT requests.
  146. Read rfc3253-3.6 for info.
  147. """
  148. # Reading request
  149. root = ET.fromstring(xml_request)
  150. prop_element = root.find(_tag("D", "prop"))
  151. prop_list = prop_element.getchildren()
  152. props = [prop.tag for prop in prop_list]
  153. if calendar:
  154. if root.tag == _tag("C", "calendar-multiget"):
  155. # Read rfc4791-7.9 for info
  156. hreferences = set((href_element.text for href_element
  157. in root.findall(_tag("D", "href"))))
  158. else:
  159. hreferences = (path,)
  160. else:
  161. hreferences = ()
  162. # Writing answer
  163. multistatus = ET.Element(_tag("D", "multistatus"))
  164. for hreference in hreferences:
  165. # Check if the reference is an item or a calendar
  166. name = name_from_path(hreference)
  167. if name:
  168. # Reference is an item
  169. path = "/".join(hreference.split("/")[:-1]) + "/"
  170. items = (item for item in calendar.items if item.name == name)
  171. else:
  172. # Reference is a calendar
  173. path = hreference
  174. items = calendar.events + calendar.todos
  175. for item in items:
  176. response = ET.Element(_tag("D", "response"))
  177. multistatus.append(response)
  178. href = ET.Element(_tag("D", "href"))
  179. href.text = path + item.name
  180. response.append(href)
  181. propstat = ET.Element(_tag("D", "propstat"))
  182. response.append(propstat)
  183. prop = ET.Element(_tag("D", "prop"))
  184. propstat.append(prop)
  185. for tag in props:
  186. element = ET.Element(tag)
  187. if tag == _tag("D", "getetag"):
  188. element.text = item.etag
  189. elif tag == _tag("C", "calendar-data"):
  190. if isinstance(item, (ical.Event, ical.Todo)):
  191. element.text = ical.serialize(
  192. calendar.headers, calendar.timezones + [item])
  193. prop.append(element)
  194. status = ET.Element(_tag("D", "status"))
  195. status.text = _response(200)
  196. propstat.append(status)
  197. return ET.tostring(multistatus, config.get("encoding", "request"))