move.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. from http import client
  21. from urllib.parse import urlparse
  22. from radicale import httputils, pathutils, storage
  23. from radicale.log import logger
  24. class ApplicationMoveMixin:
  25. def do_MOVE(self, environ, base_prefix, path, user):
  26. """Manage MOVE request."""
  27. raw_dest = environ.get("HTTP_DESTINATION", "")
  28. to_url = urlparse(raw_dest)
  29. if to_url.netloc != environ["HTTP_HOST"]:
  30. logger.info("Unsupported destination address: %r", raw_dest)
  31. # Remote destination server, not supported
  32. return httputils.REMOTE_DESTINATION
  33. if not self.access(user, path, "w"):
  34. return httputils.NOT_ALLOWED
  35. to_path = pathutils.sanitize_path(to_url.path)
  36. if not (to_path + "/").startswith(base_prefix + "/"):
  37. logger.warning("Destination %r from MOVE request on %r doesn't "
  38. "start with base prefix", to_path, path)
  39. return httputils.NOT_ALLOWED
  40. to_path = to_path[len(base_prefix):]
  41. if not self.access(user, to_path, "w"):
  42. return httputils.NOT_ALLOWED
  43. with self.storage.acquire_lock("w", user):
  44. item = next(self.storage.discover(path), None)
  45. if not item:
  46. return httputils.NOT_FOUND
  47. if (not self.access(user, path, "w", item) or
  48. not self.access(user, to_path, "w", item)):
  49. return httputils.NOT_ALLOWED
  50. if isinstance(item, storage.BaseCollection):
  51. # TODO: support moving collections
  52. return httputils.METHOD_NOT_ALLOWED
  53. to_item = next(self.storage.discover(to_path), None)
  54. if isinstance(to_item, storage.BaseCollection):
  55. return httputils.FORBIDDEN
  56. to_parent_path = pathutils.unstrip_path(
  57. posixpath.dirname(pathutils.strip_path(to_path)), True)
  58. to_collection = next(
  59. self.storage.discover(to_parent_path), None)
  60. if not to_collection:
  61. return httputils.CONFLICT
  62. tag = item.collection.get_meta("tag")
  63. if not tag or tag != to_collection.get_meta("tag"):
  64. return httputils.FORBIDDEN
  65. if to_item and environ.get("HTTP_OVERWRITE", "F") != "T":
  66. return httputils.PRECONDITION_FAILED
  67. if (to_item and item.uid != to_item.uid or
  68. not to_item and
  69. to_collection.path != item.collection.path and
  70. to_collection.has_uid(item.uid)):
  71. return self.webdav_error_response(
  72. "C" if tag == "VCALENDAR" else "CR", "no-uid-conflict")
  73. to_href = posixpath.basename(pathutils.strip_path(to_path))
  74. try:
  75. self.storage.move(item, to_collection, to_href)
  76. except ValueError as e:
  77. logger.warning(
  78. "Bad MOVE request on %r: %s", path, e, exc_info=True)
  79. return httputils.BAD_REQUEST
  80. return client.NO_CONTENT if to_item else client.CREATED, {}, None