xmlutils.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. # TODO: This is a well-known and accepted hack for ET to avoid ET from renaming
  30. # namespaces, which is accepted in XML norm but often not in XML
  31. # readers. Is there another clean solution to force namespaces?
  32. PROTECTED_NAMESPACES = {
  33. "C": "urn:ietf:params:xml:ns:caldav",
  34. "D": "DAV:",
  35. "CS": "http://calendarserver.org/ns/"}
  36. for key, value in PROTECTED_NAMESPACES.items():
  37. ET._namespace_map[value] = key
  38. def _tag(short_name, local):
  39. """Get XML Clark notation {uri(``short_name``)}``local``."""
  40. return "{%s}%s" % (PROTECTED_NAMESPACES[short_name], local)
  41. def _response(code):
  42. """Return full W3C names from HTTP status codes."""
  43. return "HTTP/1.1 %i %s" % (code, client.responses[code])
  44. def delete(obj, calendar, url):
  45. """Read and answer DELETE requests.
  46. Read rfc4918-9.6 for info.
  47. """
  48. # Reading request
  49. calendar.remove(obj)
  50. # Writing answer
  51. multistatus = ET.Element(_tag("D", "multistatus"))
  52. response = ET.Element(_tag("D", "response"))
  53. multistatus.append(response)
  54. href = ET.Element(_tag("D", "href"))
  55. href.text = url
  56. response.append(href)
  57. status = ET.Element(_tag("D", "status"))
  58. status.text = _response(200)
  59. response.append(status)
  60. return ET.tostring(multistatus, config.get("encoding", "request"))
  61. def propfind(xml_request, calendar, url):
  62. """Read and answer PROPFIND requests.
  63. Read rfc4918-9.1 for info.
  64. """
  65. # Reading request
  66. root = ET.fromstring(xml_request)
  67. prop_element = root.find(_tag("D", "prop"))
  68. prop_list = prop_element.getchildren()
  69. props = [prop.tag for prop in prop_list]
  70. # Writing answer
  71. multistatus = ET.Element(_tag("D", "multistatus"))
  72. response = ET.Element(_tag("D", "response"))
  73. multistatus.append(response)
  74. href = ET.Element(_tag("D", "href"))
  75. href.text = url
  76. response.append(href)
  77. propstat = ET.Element(_tag("D", "propstat"))
  78. response.append(propstat)
  79. prop = ET.Element(_tag("D", "prop"))
  80. propstat.append(prop)
  81. if _tag("D", "resourcetype") in props:
  82. element = ET.Element(_tag("D", "resourcetype"))
  83. element.append(ET.Element(_tag("C", "calendar")))
  84. prop.append(element)
  85. if _tag("D", "owner") in props:
  86. element = ET.Element(_tag("D", "owner"))
  87. element.text = calendar.owner
  88. prop.append(element)
  89. if _tag("D", "getcontenttype") in props:
  90. element = ET.Element(_tag("D", "getcontenttype"))
  91. element.text = "text/calendar"
  92. prop.append(element)
  93. if _tag("D", "getetag") in props:
  94. element = ET.Element(_tag("D", "getetag"))
  95. element.text = calendar.etag
  96. prop.append(element)
  97. if _tag("CS", "getctag") in props:
  98. element = ET.Element(_tag("CS", "getctag"))
  99. element.text = calendar.ctag
  100. prop.append(element)
  101. status = ET.Element(_tag("D", "status"))
  102. status.text = _response(200)
  103. propstat.append(status)
  104. return ET.tostring(multistatus, config.get("encoding", "request"))
  105. def put(ical_request, calendar, url, obj):
  106. """Read PUT requests."""
  107. if obj:
  108. # PUT is modifying obj
  109. calendar.replace(obj, ical_request)
  110. else:
  111. # PUT is adding a new object
  112. calendar.append(ical_request)
  113. def report(xml_request, calendar, url):
  114. """Read and answer REPORT requests.
  115. Read rfc3253-3.6 for info.
  116. """
  117. # Reading request
  118. root = ET.fromstring(xml_request)
  119. prop_element = root.find(_tag("D", "prop"))
  120. prop_list = prop_element.getchildren()
  121. props = [prop.tag for prop in prop_list]
  122. if root.tag == _tag("C", "calendar-multiget"):
  123. # Read rfc4791-7.9 for info
  124. hreferences = set([href_element.text for href_element
  125. in root.findall(_tag("D", "href"))])
  126. else:
  127. hreferences = [url]
  128. # Writing answer
  129. multistatus = ET.Element(_tag("D", "multistatus"))
  130. # TODO: WTF, sunbird needs one response by object,
  131. # is that really what is needed?
  132. # Read rfc4791-9.[6|10] for info
  133. for hreference in hreferences:
  134. headers = ical.headers(calendar.vcalendar)
  135. timezones = ical.timezones(calendar.vcalendar)
  136. objects = \
  137. ical.events(calendar.vcalendar) + ical.todos(calendar.vcalendar)
  138. if not objects:
  139. # TODO: Read rfc4791-9.[6|10] to find a right answer
  140. response = ET.Element(_tag("D", "response"))
  141. multistatus.append(response)
  142. href = ET.Element(_tag("D", "href"))
  143. href.text = url
  144. response.append(href)
  145. status = ET.Element(_tag("D", "status"))
  146. status.text = _response(204)
  147. response.append(status)
  148. for obj in objects:
  149. # TODO: Use the hreference to read data and create href.text
  150. # We assume here that hreference is url
  151. response = ET.Element(_tag("D", "response"))
  152. multistatus.append(response)
  153. href = ET.Element(_tag("D", "href"))
  154. href.text = url
  155. response.append(href)
  156. propstat = ET.Element(_tag("D", "propstat"))
  157. response.append(propstat)
  158. prop = ET.Element(_tag("D", "prop"))
  159. propstat.append(prop)
  160. if _tag("D", "getetag") in props:
  161. element = ET.Element(_tag("D", "getetag"))
  162. element.text = obj.etag
  163. prop.append(element)
  164. if _tag("C", "calendar-data") in props:
  165. element = ET.Element(_tag("C", "calendar-data"))
  166. # TODO: Maybe assume that events and todos are not the same
  167. element.text = ical.write_calendar(headers, timezones, [obj])
  168. prop.append(element)
  169. status = ET.Element(_tag("D", "status"))
  170. status.text = _response(200)
  171. propstat.append(status)
  172. return ET.tostring(multistatus, config.get("encoding", "request"))