mkcol.py 3.2 KB

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