xmlutils.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Radicale Server - Calendar Server
  4. # Copyright © 2008-2010 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. # TODO: Manage depth and calendars/collections
  27. import xml.etree.ElementTree as ET
  28. from radicale import client, config, ical
  29. NAMESPACES = {
  30. "C": "urn:ietf:params:xml:ns:caldav",
  31. "D": "DAV:",
  32. "CS": "http://calendarserver.org/ns/"}
  33. def _tag(short_name, local):
  34. """Get XML Clark notation {uri(``short_name``)}``local``."""
  35. return "{%s}%s" % (NAMESPACES[short_name], local)
  36. def _response(code):
  37. """Return full W3C names from HTTP status codes."""
  38. return "HTTP/1.1 %i %s" % (code, client.responses[code])
  39. def name_from_path(path):
  40. """Return Radicale item name from ``path``."""
  41. path_parts = path.strip("/").split("/")
  42. return path_parts[-1] if len(path_parts) > 2 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))
  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, request):
  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 depth == "0":
  72. items = [calendar]
  73. elif depth == "1":
  74. items = [calendar] + calendar.events + calendar.todos
  75. else:
  76. # depth is infinity or not specified
  77. # we limit ourselves to depth == 1
  78. items = [calendar] + calendar.events + calendar.todos
  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 "%s/%s" % (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"):
  93. if 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. else:
  99. tag = ET.Element(_tag("C", "comp"))
  100. tag.set("name", element.tag)
  101. element.append(tag)
  102. elif tag == _tag("D", "owner"):
  103. element.text = calendar.owner
  104. elif tag == _tag("D", "getcontenttype"):
  105. element.text = "text/calendar"
  106. elif tag == _tag("D", "getetag"):
  107. element.text = item.etag
  108. elif tag == _tag("D", "displayname"):
  109. element.text = calendar.name
  110. elif tag == _tag("D", "supported-report-set"):
  111. supported_report = ET.Element(_tag("D", "supported-report"))
  112. report_set = ET.Element(_tag("D", "report"))
  113. report_set.append(ET.Element(_tag("C", "calendar-multiget")))
  114. supported_report.append(report_set)
  115. element.append(supported_report)
  116. elif tag == _tag("D", "principal-URL"):
  117. # TODO: use a real principal URL, read rfc3744-4.2 for info
  118. element.text = "%s://%s%s" % (
  119. request.server.PROTOCOL, request.headers["Host"],
  120. request.path)
  121. prop.append(element)
  122. status = ET.Element(_tag("D", "status"))
  123. status.text = _response(200)
  124. propstat.append(status)
  125. return ET.tostring(multistatus, config.get("encoding", "request"))
  126. def put(path, ical_request, calendar):
  127. """Read PUT requests."""
  128. name = name_from_path(path)
  129. if name in (item.name for item in calendar.items):
  130. # PUT is modifying an existing item
  131. calendar.replace(name, ical_request)
  132. else:
  133. # PUT is adding a new item
  134. calendar.append(name, ical_request)
  135. def report(path, xml_request, calendar):
  136. """Read and answer REPORT requests.
  137. Read rfc3253-3.6 for info.
  138. """
  139. # Reading request
  140. root = ET.fromstring(xml_request)
  141. prop_element = root.find(_tag("D", "prop"))
  142. prop_list = prop_element.getchildren()
  143. props = [prop.tag for prop in prop_list]
  144. if root.tag == _tag("C", "calendar-multiget"):
  145. # Read rfc4791-7.9 for info
  146. hreferences = set((href_element.text for href_element
  147. in root.findall(_tag("D", "href"))))
  148. else:
  149. hreferences = (path,)
  150. # Writing answer
  151. multistatus = ET.Element(_tag("D", "multistatus"))
  152. for hreference in hreferences:
  153. # Check if the reference is an item or a calendar
  154. name = name_from_path(hreference)
  155. if name:
  156. # Reference is an item
  157. path = "/".join(hreference.split("/")[:-1]) + "/"
  158. items = (item for item in calendar.items if item.name == name)
  159. else:
  160. # Reference is a calendar
  161. path = hreference
  162. items = calendar.events + calendar.todos
  163. for item in items:
  164. response = ET.Element(_tag("D", "response"))
  165. multistatus.append(response)
  166. href = ET.Element(_tag("D", "href"))
  167. href.text = path + item.name
  168. response.append(href)
  169. propstat = ET.Element(_tag("D", "propstat"))
  170. response.append(propstat)
  171. prop = ET.Element(_tag("D", "prop"))
  172. propstat.append(prop)
  173. if _tag("D", "getetag") in props:
  174. element = ET.Element(_tag("D", "getetag"))
  175. element.text = item.etag
  176. prop.append(element)
  177. if _tag("C", "calendar-data") in props:
  178. element = ET.Element(_tag("C", "calendar-data"))
  179. if isinstance(item, ical.Event):
  180. element.text = ical.serialize(
  181. calendar.headers, calendar.timezones + [item])
  182. elif isinstance(item, ical.Todo):
  183. element.text = ical.serialize(
  184. calendar.headers, calendar.timezones + [item])
  185. prop.append(element)
  186. status = ET.Element(_tag("D", "status"))
  187. status.text = _response(200)
  188. propstat.append(status)
  189. return ET.tostring(multistatus, config.get("encoding", "request"))