xmlutils.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. return path.split("/")[-1]
  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, request):
  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. response = ET.Element(_tag("D", "response"))
  71. multistatus.append(response)
  72. href = ET.Element(_tag("D", "href"))
  73. href.text = path
  74. response.append(href)
  75. propstat = ET.Element(_tag("D", "propstat"))
  76. response.append(propstat)
  77. prop = ET.Element(_tag("D", "prop"))
  78. propstat.append(prop)
  79. for tag in props:
  80. element = ET.Element(tag)
  81. if tag == _tag("D", "resourcetype"):
  82. element.append(ET.Element(_tag("C", "calendar")))
  83. elif tag == _tag("D", "owner"):
  84. element.text = calendar.owner
  85. elif tag == _tag("D", "getcontenttype"):
  86. element.text = "text/calendar"
  87. elif tag == _tag("D", "getetag"):
  88. element.text = calendar.etag
  89. elif tag == _tag("D", "displayname"):
  90. element.text = calendar.name
  91. elif tag == _tag("D", "supported-report-set"):
  92. supported_report = ET.Element(_tag("D", "supported-report"))
  93. report = ET.Element(_tag("D", "report"))
  94. report.append(ET.Element(_tag("C", "calendar-multiget")))
  95. supported_report.append(report)
  96. element.append(supported_report)
  97. elif tag == _tag("D", "principal-URL"):
  98. # TODO: use a real principal URL, read rfc3744-4.2 for info
  99. element.text = "%s://%s%s" % (
  100. request.server.PROTOCOL, request.headers["Host"], request.path)
  101. prop.append(element)
  102. status = ET.Element(_tag("D", "status"))
  103. status.text = _response(200)
  104. propstat.append(status)
  105. return ET.tostring(multistatus, config.get("encoding", "request"))
  106. def put(path, ical_request, calendar):
  107. """Read PUT requests."""
  108. name = name_from_path(path)
  109. if name in (item.name for item in calendar.items):
  110. # PUT is modifying an existing item
  111. calendar.replace(name, ical_request)
  112. else:
  113. # PUT is adding a new item
  114. calendar.append(name, ical_request)
  115. def report(path, xml_request, calendar):
  116. """Read and answer REPORT requests.
  117. Read rfc3253-3.6 for info.
  118. """
  119. # Reading request
  120. root = ET.fromstring(xml_request)
  121. prop_element = root.find(_tag("D", "prop"))
  122. prop_list = prop_element.getchildren()
  123. props = [prop.tag for prop in prop_list]
  124. if root.tag == _tag("C", "calendar-multiget"):
  125. # Read rfc4791-7.9 for info
  126. hreferences = set((href_element.text for href_element
  127. in root.findall(_tag("D", "href"))))
  128. else:
  129. hreferences = (path,)
  130. # Writing answer
  131. multistatus = ET.Element(_tag("D", "multistatus"))
  132. for hreference in hreferences:
  133. # Check if the reference is an item or a calendar
  134. name = name_from_path(hreference)
  135. if name:
  136. # Reference is an item
  137. path = "/".join(hreference.split("/")[:-1]) + "/"
  138. items = (item for item in calendar.items if item.name == name)
  139. else:
  140. # Reference is a calendar
  141. path = hreference
  142. items = calendar.events + calendar.todos
  143. for item in items:
  144. response = ET.Element(_tag("D", "response"))
  145. multistatus.append(response)
  146. href = ET.Element(_tag("D", "href"))
  147. href.text = path + item.name
  148. response.append(href)
  149. propstat = ET.Element(_tag("D", "propstat"))
  150. response.append(propstat)
  151. prop = ET.Element(_tag("D", "prop"))
  152. propstat.append(prop)
  153. if _tag("D", "getetag") in props:
  154. element = ET.Element(_tag("D", "getetag"))
  155. element.text = item.etag
  156. prop.append(element)
  157. if _tag("C", "calendar-data") in props:
  158. element = ET.Element(_tag("C", "calendar-data"))
  159. if isinstance(item, ical.Event):
  160. element.text = ical.serialize(
  161. calendar.headers, calendar.timezones + [item])
  162. elif isinstance(item, ical.Todo):
  163. element.text = ical.serialize(
  164. calendar.headers, calendar.timezones + [item])
  165. prop.append(element)
  166. status = ET.Element(_tag("D", "status"))
  167. status.text = _response(200)
  168. propstat.append(status)
  169. return ET.tostring(multistatus, config.get("encoding", "request"))