proppatch.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 contextlib
  20. import socket
  21. from http import client
  22. from xml.etree import ElementTree as ET
  23. from radicale import app, httputils
  24. from radicale import item as radicale_item
  25. from radicale import storage, xmlutils
  26. from radicale.log import logger
  27. def xml_proppatch(base_prefix, path, xml_request, collection):
  28. """Read and answer PROPPATCH requests.
  29. Read rfc4918-9.2 for info.
  30. """
  31. multistatus = ET.Element(xmlutils.make_clark("D:multistatus"))
  32. response = ET.Element(xmlutils.make_clark("D:response"))
  33. multistatus.append(response)
  34. href = ET.Element(xmlutils.make_clark("D:href"))
  35. href.text = xmlutils.make_href(base_prefix, path)
  36. response.append(href)
  37. # Create D:propstat element for props with status 200 OK
  38. propstat = ET.Element(xmlutils.make_clark("D:propstat"))
  39. status = ET.Element(xmlutils.make_clark("D:status"))
  40. status.text = xmlutils.make_response(200)
  41. props_ok = ET.Element(xmlutils.make_clark("D:prop"))
  42. propstat.append(props_ok)
  43. propstat.append(status)
  44. response.append(propstat)
  45. new_props = collection.get_meta()
  46. for short_name, value in xmlutils.props_from_request(xml_request).items():
  47. if value is None:
  48. with contextlib.suppress(KeyError):
  49. del new_props[short_name]
  50. else:
  51. new_props[short_name] = value
  52. props_ok.append(ET.Element(xmlutils.make_clark(short_name)))
  53. radicale_item.check_and_sanitize_props(new_props)
  54. collection.set_meta(new_props)
  55. return multistatus
  56. class ApplicationProppatchMixin:
  57. def do_PROPPATCH(self, environ, base_prefix, path, user):
  58. """Manage PROPPATCH request."""
  59. access = app.Access(self._rights, user, path)
  60. if not access.check("w"):
  61. return httputils.NOT_ALLOWED
  62. try:
  63. xml_content = self._read_xml_request_body(environ)
  64. except RuntimeError as e:
  65. logger.warning(
  66. "Bad PROPPATCH request on %r: %s", path, e, exc_info=True)
  67. return httputils.BAD_REQUEST
  68. except socket.timeout:
  69. logger.debug("client timed out", exc_info=True)
  70. return httputils.REQUEST_TIMEOUT
  71. with self._storage.acquire_lock("w", user):
  72. item = next(self._storage.discover(path), None)
  73. if not item:
  74. return httputils.NOT_FOUND
  75. if not access.check("w", item):
  76. return httputils.NOT_ALLOWED
  77. if not isinstance(item, storage.BaseCollection):
  78. return httputils.FORBIDDEN
  79. headers = {"DAV": httputils.DAV_HEADERS,
  80. "Content-Type": "text/xml; charset=%s" % self._encoding}
  81. try:
  82. xml_answer = xml_proppatch(base_prefix, path, xml_content,
  83. item)
  84. except ValueError as e:
  85. logger.warning(
  86. "Bad PROPPATCH request on %r: %s", path, e, exc_info=True)
  87. return httputils.BAD_REQUEST
  88. return client.MULTI_STATUS, headers, self._xml_response(xml_answer)