proppatch.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. import socket
  20. from http import client
  21. from xml.etree import ElementTree as ET
  22. from radicale import httputils
  23. from radicale import item as radicale_item
  24. from radicale import storage, xmlutils
  25. from radicale.log import logger
  26. def xml_add_propstat_to(element, tag, status_number):
  27. """Add a PROPSTAT response structure to an element.
  28. The PROPSTAT answer structure is defined in rfc4918-9.1. It is added to the
  29. given ``element``, for the following ``tag`` with the given
  30. ``status_number``.
  31. """
  32. propstat = ET.Element(xmlutils.make_tag("D", "propstat"))
  33. element.append(propstat)
  34. prop = ET.Element(xmlutils.make_tag("D", "prop"))
  35. propstat.append(prop)
  36. clark_tag = tag if "{" in tag else xmlutils.make_tag(*tag.split(":", 1))
  37. prop_tag = ET.Element(clark_tag)
  38. prop.append(prop_tag)
  39. status = ET.Element(xmlutils.make_tag("D", "status"))
  40. status.text = xmlutils.make_response(status_number)
  41. propstat.append(status)
  42. def xml_proppatch(base_prefix, path, xml_request, collection):
  43. """Read and answer PROPPATCH requests.
  44. Read rfc4918-9.2 for info.
  45. """
  46. props_to_set = xmlutils.props_from_request(xml_request, actions=("set",))
  47. props_to_remove = xmlutils.props_from_request(xml_request,
  48. actions=("remove",))
  49. multistatus = ET.Element(xmlutils.make_tag("D", "multistatus"))
  50. response = ET.Element(xmlutils.make_tag("D", "response"))
  51. multistatus.append(response)
  52. href = ET.Element(xmlutils.make_tag("D", "href"))
  53. href.text = xmlutils.make_href(base_prefix, path)
  54. response.append(href)
  55. new_props = collection.get_meta()
  56. for short_name, value in props_to_set.items():
  57. new_props[short_name] = value
  58. xml_add_propstat_to(response, short_name, 200)
  59. for short_name in props_to_remove:
  60. try:
  61. del new_props[short_name]
  62. except KeyError:
  63. pass
  64. xml_add_propstat_to(response, short_name, 200)
  65. radicale_item.check_and_sanitize_props(new_props)
  66. collection.set_meta(new_props)
  67. return multistatus
  68. class ApplicationProppatchMixin:
  69. def do_PROPPATCH(self, environ, base_prefix, path, user):
  70. """Manage PROPPATCH request."""
  71. if not self.access(user, path, "w"):
  72. return httputils.NOT_ALLOWED
  73. try:
  74. xml_content = self.read_xml_content(environ)
  75. except RuntimeError as e:
  76. logger.warning(
  77. "Bad PROPPATCH request on %r: %s", path, e, exc_info=True)
  78. return httputils.BAD_REQUEST
  79. except socket.timeout:
  80. logger.debug("client timed out", exc_info=True)
  81. return httputils.REQUEST_TIMEOUT
  82. with self.Collection.acquire_lock("w", user):
  83. item = next(self.Collection.discover(path), None)
  84. if not item:
  85. return httputils.NOT_FOUND
  86. if not self.access(user, path, "w", item):
  87. return httputils.NOT_ALLOWED
  88. if not isinstance(item, storage.BaseCollection):
  89. return httputils.FORBIDDEN
  90. headers = {"DAV": httputils.DAV_HEADERS,
  91. "Content-Type": "text/xml; charset=%s" % self.encoding}
  92. try:
  93. xml_answer = xml_proppatch(base_prefix, path, xml_content,
  94. item)
  95. except ValueError as e:
  96. logger.warning(
  97. "Bad PROPPATCH request on %r: %s", path, e, exc_info=True)
  98. return httputils.BAD_REQUEST
  99. return (client.MULTI_STATUS, headers,
  100. self.write_xml_content(xml_answer))