move.py 4.0 KB

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