proppatch.py 4.7 KB

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