move.py 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 app, 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. access = app.Access(self._rights, user, path)
  34. if not access.check("w"):
  35. return httputils.NOT_ALLOWED
  36. to_path = pathutils.sanitize_path(to_url.path)
  37. if not (to_path + "/").startswith(base_prefix + "/"):
  38. logger.warning("Destination %r from MOVE request on %r doesn't "
  39. "start with base prefix", to_path, path)
  40. return httputils.NOT_ALLOWED
  41. to_path = to_path[len(base_prefix):]
  42. to_access = app.Access(self._rights, user, to_path)
  43. if not to_access.check("w"):
  44. return httputils.NOT_ALLOWED
  45. with self._storage.acquire_lock("w", user):
  46. item = next(self._storage.discover(path), None)
  47. if not item:
  48. return httputils.NOT_FOUND
  49. if (not access.check("w", item) or
  50. not to_access.check("w", item)):
  51. return httputils.NOT_ALLOWED
  52. if isinstance(item, storage.BaseCollection):
  53. # TODO: support moving collections
  54. return httputils.METHOD_NOT_ALLOWED
  55. to_item = next(self._storage.discover(to_path), None)
  56. if isinstance(to_item, storage.BaseCollection):
  57. return httputils.FORBIDDEN
  58. to_parent_path = pathutils.unstrip_path(
  59. posixpath.dirname(pathutils.strip_path(to_path)), True)
  60. to_collection = next(
  61. self._storage.discover(to_parent_path), None)
  62. if not to_collection:
  63. return httputils.CONFLICT
  64. tag = item.collection.get_meta("tag")
  65. if not tag or tag != to_collection.get_meta("tag"):
  66. return httputils.FORBIDDEN
  67. if to_item and environ.get("HTTP_OVERWRITE", "F") != "T":
  68. return httputils.PRECONDITION_FAILED
  69. if (to_item and item.uid != to_item.uid or
  70. not to_item and
  71. to_collection.path != item.collection.path and
  72. to_collection.has_uid(item.uid)):
  73. return self._webdav_error_response(
  74. client.CONFLICT, "%s:no-uid-conflict" % (
  75. "C" if tag == "VCALENDAR" else "CR"))
  76. to_href = posixpath.basename(pathutils.strip_path(to_path))
  77. try:
  78. self._storage.move(item, to_collection, to_href)
  79. except ValueError as e:
  80. logger.warning(
  81. "Bad MOVE request on %r: %s", path, e, exc_info=True)
  82. return httputils.BAD_REQUEST
  83. return client.NO_CONTENT if to_item else client.CREATED, {}, None