mkcalendar.py 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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-2021 Unrud <unrud@outlook.com>
  6. # Copyright © 2024-2025 Peter Bieringer <pb@bieringer.de>
  7. #
  8. # This library is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This library is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  20. import errno
  21. import posixpath
  22. import re
  23. import socket
  24. from http import client
  25. import radicale.item as radicale_item
  26. from radicale import httputils, pathutils, storage, types, xmlutils
  27. from radicale.app.base import ApplicationBase
  28. from radicale.log import logger
  29. class ApplicationPartMkcalendar(ApplicationBase):
  30. def do_MKCALENDAR(self, environ: types.WSGIEnviron, base_prefix: str,
  31. path: str, user: str) -> types.WSGIResponse:
  32. """Manage MKCALENDAR request."""
  33. if "w" not in self._rights.authorization(user, path):
  34. return httputils.NOT_ALLOWED
  35. try:
  36. xml_content = self._read_xml_request_body(environ)
  37. except RuntimeError as e:
  38. logger.warning(
  39. "Bad MKCALENDAR request on %r: %s", path, e, exc_info=True)
  40. return httputils.BAD_REQUEST
  41. except socket.timeout:
  42. logger.debug("Client timed out", exc_info=True)
  43. return httputils.REQUEST_TIMEOUT
  44. # Prepare before locking
  45. props_with_remove = xmlutils.props_from_request(xml_content)
  46. props_with_remove["tag"] = "VCALENDAR"
  47. try:
  48. props = radicale_item.check_and_sanitize_props(props_with_remove)
  49. except ValueError as e:
  50. logger.warning(
  51. "Bad MKCALENDAR request on %r: %s", path, e, exc_info=True)
  52. return httputils.BAD_REQUEST
  53. # TODO: use this?
  54. # timezone = props.get("C:calendar-timezone")
  55. with self._storage.acquire_lock("w", user, path=path, request="MKCALENDAR"):
  56. item = next(iter(self._storage.discover(path)), None)
  57. if item:
  58. return self._webdav_error_response(
  59. client.CONFLICT, "D:resource-must-be-null")
  60. parent_path = pathutils.unstrip_path(
  61. posixpath.dirname(pathutils.strip_path(path)), True)
  62. parent_item = next(iter(self._storage.discover(parent_path)), None)
  63. if not parent_item:
  64. return httputils.CONFLICT
  65. if (not isinstance(parent_item, storage.BaseCollection) or
  66. parent_item.tag):
  67. return httputils.FORBIDDEN
  68. try:
  69. self._storage.create_collection(path, props=props)
  70. except ValueError as e:
  71. # return better matching HTTP result in case errno is provided and catched
  72. errno_match = re.search("\\[Errno ([0-9]+)\\]", str(e))
  73. if errno_match:
  74. logger.error(
  75. "Failed MKCALENDAR request on %r: %s", path, e, exc_info=True)
  76. errno_e = int(errno_match.group(1))
  77. if errno_e == errno.ENOSPC:
  78. return httputils.INSUFFICIENT_STORAGE
  79. elif errno_e in [errno.EPERM, errno.EACCES]:
  80. return httputils.FORBIDDEN
  81. else:
  82. return httputils.INTERNAL_SERVER_ERROR
  83. else:
  84. logger.warning(
  85. "Bad MKCALENDAR request on %r: %s", path, e, exc_info=True)
  86. return httputils.BAD_REQUEST
  87. return client.CREATED, {}, None