mkcol.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 posixpath
  20. import socket
  21. from http import client
  22. from radicale import httputils
  23. from radicale import item as radicale_item
  24. from radicale import pathutils, rights, storage, xmlutils
  25. from radicale.log import logger
  26. class ApplicationMkcolMixin:
  27. def do_MKCOL(self, environ, base_prefix, path, user):
  28. """Manage MKCOL request."""
  29. permissions = self._rights.authorization(user, path)
  30. if not rights.intersect(permissions, "Ww"):
  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:
  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._storage.acquire_lock("w", user):
  53. item = next(self._storage.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._storage.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._storage.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