proppatch.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # This file is part of Radicale - CalDAV and CardDAV 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. import xml.etree.ElementTree as ET
  21. from http import client
  22. from typing import Dict, Optional, cast
  23. import defusedxml.ElementTree as DefusedET
  24. import radicale.item as radicale_item
  25. from radicale import httputils, storage, types, xmlutils
  26. from radicale.app.base import Access, ApplicationBase
  27. from radicale.hook import HookNotificationItem, HookNotificationItemTypes
  28. from radicale.log import logger
  29. def xml_proppatch(base_prefix: str, path: str,
  30. xml_request: Optional[ET.Element],
  31. collection: storage.BaseCollection) -> ET.Element:
  32. """Read and answer PROPPATCH requests.
  33. Read rfc4918-9.2 for info.
  34. """
  35. multistatus = ET.Element(xmlutils.make_clark("D:multistatus"))
  36. response = ET.Element(xmlutils.make_clark("D:response"))
  37. multistatus.append(response)
  38. href = ET.Element(xmlutils.make_clark("D:href"))
  39. href.text = xmlutils.make_href(base_prefix, path)
  40. response.append(href)
  41. # Create D:propstat element for props with status 200 OK
  42. propstat = ET.Element(xmlutils.make_clark("D:propstat"))
  43. status = ET.Element(xmlutils.make_clark("D:status"))
  44. status.text = xmlutils.make_response(200)
  45. props_ok = ET.Element(xmlutils.make_clark("D:prop"))
  46. propstat.append(props_ok)
  47. propstat.append(status)
  48. response.append(propstat)
  49. props_with_remove = xmlutils.props_from_request(xml_request)
  50. all_props_with_remove = cast(Dict[str, Optional[str]],
  51. dict(collection.get_meta()))
  52. all_props_with_remove.update(props_with_remove)
  53. all_props = radicale_item.check_and_sanitize_props(all_props_with_remove)
  54. collection.set_meta(all_props)
  55. for short_name in props_with_remove:
  56. props_ok.append(ET.Element(xmlutils.make_clark(short_name)))
  57. return multistatus
  58. class ApplicationPartProppatch(ApplicationBase):
  59. def do_PROPPATCH(self, environ: types.WSGIEnviron, base_prefix: str,
  60. path: str, user: str) -> types.WSGIResponse:
  61. """Manage PROPPATCH request."""
  62. access = Access(self._rights, user, path)
  63. if not access.check("w"):
  64. return httputils.NOT_ALLOWED
  65. try:
  66. xml_content = self._read_xml_request_body(environ)
  67. except RuntimeError as e:
  68. logger.warning(
  69. "Bad PROPPATCH request on %r: %s", path, e, exc_info=True)
  70. return httputils.BAD_REQUEST
  71. except socket.timeout:
  72. logger.debug("Client timed out", exc_info=True)
  73. return httputils.REQUEST_TIMEOUT
  74. with self._storage.acquire_lock("w", user):
  75. item = next(iter(self._storage.discover(path)), None)
  76. if not item:
  77. return httputils.NOT_FOUND
  78. if not access.check("w", item):
  79. return httputils.NOT_ALLOWED
  80. if not isinstance(item, storage.BaseCollection):
  81. return httputils.FORBIDDEN
  82. headers = {"DAV": httputils.DAV_HEADERS,
  83. "Content-Type": "text/xml; charset=%s" % self._encoding}
  84. try:
  85. xml_answer = xml_proppatch(base_prefix, path, xml_content,
  86. item)
  87. if xml_content is not None:
  88. hook_notification_item = HookNotificationItem(
  89. HookNotificationItemTypes.CPATCH,
  90. access.path,
  91. DefusedET.tostring(
  92. xml_content,
  93. encoding=self._encoding
  94. ).decode(encoding=self._encoding)
  95. )
  96. self._hook.notify(hook_notification_item)
  97. except ValueError as e:
  98. logger.warning(
  99. "Bad PROPPATCH request on %r: %s", path, e, exc_info=True)
  100. return httputils.BAD_REQUEST
  101. return client.MULTI_STATUS, headers, self._xml_response(xml_answer)