proppatch.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 app, 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_clark("D:propstat"))
  33. element.append(propstat)
  34. prop = ET.Element(xmlutils.make_clark("D:prop"))
  35. propstat.append(prop)
  36. clark_tag = xmlutils.make_clark(tag)
  37. prop_tag = ET.Element(clark_tag)
  38. prop.append(prop_tag)
  39. status = ET.Element(xmlutils.make_clark("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_clark("D:multistatus"))
  50. response = ET.Element(xmlutils.make_clark("D:response"))
  51. multistatus.append(response)
  52. href = ET.Element(xmlutils.make_clark("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. access = app.Access(self._rights, user, path)
  72. if not access.check("w"):
  73. return httputils.NOT_ALLOWED
  74. try:
  75. xml_content = self._read_xml_content(environ)
  76. except RuntimeError as e:
  77. logger.warning(
  78. "Bad PROPPATCH request on %r: %s", path, e, exc_info=True)
  79. return httputils.BAD_REQUEST
  80. except socket.timeout:
  81. logger.debug("client timed out", exc_info=True)
  82. return httputils.REQUEST_TIMEOUT
  83. with self._storage.acquire_lock("w", user):
  84. item = next(self._storage.discover(path), None)
  85. if not item:
  86. return httputils.NOT_FOUND
  87. if not access.check("w", item):
  88. return httputils.NOT_ALLOWED
  89. if not isinstance(item, storage.BaseCollection):
  90. return httputils.FORBIDDEN
  91. headers = {"DAV": httputils.DAV_HEADERS,
  92. "Content-Type": "text/xml; charset=%s" % self._encoding}
  93. try:
  94. xml_answer = xml_proppatch(base_prefix, path, xml_content,
  95. item)
  96. except ValueError as e:
  97. logger.warning(
  98. "Bad PROPPATCH request on %r: %s", path, e, exc_info=True)
  99. return httputils.BAD_REQUEST
  100. return (client.MULTI_STATUS, headers,
  101. self._write_xml_content(xml_answer))