proppatch.py 4.1 KB

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