proppatch.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2008 Nicolas Kandel
  3. # Copyright © 2008 Pascal Halter
  4. # Copyright © 2008-2017 Guillaume Ayoub
  5. # Copyright © 2017-2018 Unrud<unrud@outlook.com>
  6. #
  7. # This library is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  19. """
  20. Radicale WSGI application.
  21. Can be used with an external WSGI server or the built-in server.
  22. """
  23. import socket
  24. from http import client
  25. from xml.etree import ElementTree as ET
  26. from radicale import httputils
  27. from radicale import item as radicale_item
  28. from radicale import storage, xmlutils
  29. from radicale.log import logger
  30. def xml_add_propstat_to(element, tag, status_number):
  31. """Add a PROPSTAT response structure to an element.
  32. The PROPSTAT answer structure is defined in rfc4918-9.1. It is added to the
  33. given ``element``, for the following ``tag`` with the given
  34. ``status_number``.
  35. """
  36. propstat = ET.Element(xmlutils.make_tag("D", "propstat"))
  37. element.append(propstat)
  38. prop = ET.Element(xmlutils.make_tag("D", "prop"))
  39. propstat.append(prop)
  40. clark_tag = tag if "{" in tag else xmlutils.make_tag(*tag.split(":", 1))
  41. prop_tag = ET.Element(clark_tag)
  42. prop.append(prop_tag)
  43. status = ET.Element(xmlutils.make_tag("D", "status"))
  44. status.text = xmlutils.make_response(status_number)
  45. propstat.append(status)
  46. def xml_proppatch(base_prefix, path, xml_request, collection):
  47. """Read and answer PROPPATCH requests.
  48. Read rfc4918-9.2 for info.
  49. """
  50. props_to_set = xmlutils.props_from_request(xml_request, actions=("set",))
  51. props_to_remove = xmlutils.props_from_request(xml_request,
  52. actions=("remove",))
  53. multistatus = ET.Element(xmlutils.make_tag("D", "multistatus"))
  54. response = ET.Element(xmlutils.make_tag("D", "response"))
  55. multistatus.append(response)
  56. href = ET.Element(xmlutils.make_tag("D", "href"))
  57. href.text = xmlutils.make_href(base_prefix, path)
  58. response.append(href)
  59. new_props = collection.get_meta()
  60. for short_name, value in props_to_set.items():
  61. new_props[short_name] = value
  62. xml_add_propstat_to(response, short_name, 200)
  63. for short_name in props_to_remove:
  64. try:
  65. del new_props[short_name]
  66. except KeyError:
  67. pass
  68. xml_add_propstat_to(response, short_name, 200)
  69. radicale_item.check_and_sanitize_props(new_props)
  70. collection.set_meta(new_props)
  71. return multistatus
  72. class ApplicationProppatchMixin:
  73. def do_PROPPATCH(self, environ, base_prefix, path, user):
  74. """Manage PROPPATCH request."""
  75. if not self.access(user, path, "w"):
  76. return httputils.NOT_ALLOWED
  77. try:
  78. xml_content = self.read_xml_content(environ)
  79. except RuntimeError as e:
  80. logger.warning(
  81. "Bad PROPPATCH request on %r: %s", path, e, exc_info=True)
  82. return httputils.BAD_REQUEST
  83. except socket.timeout as e:
  84. logger.debug("client timed out", exc_info=True)
  85. return httputils.REQUEST_TIMEOUT
  86. with self.Collection.acquire_lock("w", user):
  87. item = next(self.Collection.discover(path), None)
  88. if not item:
  89. return httputils.NOT_FOUND
  90. if not self.access(user, path, "w", item):
  91. return httputils.NOT_ALLOWED
  92. if not isinstance(item, storage.BaseCollection):
  93. return httputils.FORBIDDEN
  94. headers = {"DAV": httputils.DAV_HEADERS,
  95. "Content-Type": "text/xml; charset=%s" % self.encoding}
  96. try:
  97. xml_answer = xml_proppatch(base_prefix, path, xml_content,
  98. item)
  99. except ValueError as e:
  100. logger.warning(
  101. "Bad PROPPATCH request on %r: %s", path, e, exc_info=True)
  102. return httputils.BAD_REQUEST
  103. return (client.MULTI_STATUS, headers,
  104. self.write_xml_content(xml_answer))