mkcol.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. from http import client
  21. from radicale import httputils
  22. from radicale import item as radicale_item
  23. from radicale import pathutils, storage, xmlutils
  24. from radicale.log import logger
  25. import posixpath # isort:skip
  26. class ApplicationMkcolMixin:
  27. def do_MKCOL(self, environ, base_prefix, path, user):
  28. """Manage MKCOL request."""
  29. permissions = self.Rights.authorized(user, path, "Ww")
  30. if not permissions:
  31. return httputils.NOT_ALLOWED
  32. try:
  33. xml_content = self.read_xml_content(environ)
  34. except RuntimeError as e:
  35. logger.warning(
  36. "Bad MKCOL request on %r: %s", path, e, exc_info=True)
  37. return httputils.BAD_REQUEST
  38. except socket.timeout as e:
  39. logger.debug("client timed out", exc_info=True)
  40. return httputils.REQUEST_TIMEOUT
  41. # Prepare before locking
  42. props = xmlutils.props_from_request(xml_content)
  43. try:
  44. radicale_item.check_and_sanitize_props(props)
  45. except ValueError as e:
  46. logger.warning(
  47. "Bad MKCOL request on %r: %s", path, e, exc_info=True)
  48. return httputils.BAD_REQUEST
  49. if (props.get("tag") and "w" not in permissions or
  50. not props.get("tag") and "W" not in permissions):
  51. return httputils.NOT_ALLOWED
  52. with self.Collection.acquire_lock("w", user):
  53. item = next(self.Collection.discover(path), None)
  54. if item:
  55. return httputils.METHOD_NOT_ALLOWED
  56. parent_path = pathutils.unstrip_path(
  57. posixpath.dirname(pathutils.strip_path(path)), True)
  58. parent_item = next(self.Collection.discover(parent_path), None)
  59. if not parent_item:
  60. return httputils.CONFLICT
  61. if (not isinstance(parent_item, storage.BaseCollection) or
  62. parent_item.get_meta("tag")):
  63. return httputils.FORBIDDEN
  64. try:
  65. self.Collection.create_collection(path, props=props)
  66. except ValueError as e:
  67. logger.warning(
  68. "Bad MKCOL request on %r: %s", path, e, exc_info=True)
  69. return httputils.BAD_REQUEST
  70. return client.CREATED, {}, None