mkcol.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # This file is part of Radicale - CalDAV and CardDAV 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. import radicale.item as radicale_item
  23. from radicale import httputils, pathutils, rights, storage, types, xmlutils
  24. from radicale.app.base import ApplicationBase
  25. from radicale.log import logger
  26. class ApplicationPartMkcol(ApplicationBase):
  27. def do_MKCOL(self, environ: types.WSGIEnviron, base_prefix: str,
  28. path: str, user: str) -> types.WSGIResponse:
  29. """Manage MKCOL request."""
  30. permissions = self._rights.authorization(user, path)
  31. if not rights.intersect(permissions, "Ww"):
  32. return httputils.NOT_ALLOWED
  33. try:
  34. xml_content = self._read_xml_request_body(environ)
  35. except RuntimeError as e:
  36. logger.warning(
  37. "Bad MKCOL request on %r: %s", path, e, exc_info=True)
  38. return httputils.BAD_REQUEST
  39. except socket.timeout:
  40. logger.debug("Client timed out", exc_info=True)
  41. return httputils.REQUEST_TIMEOUT
  42. # Prepare before locking
  43. props_with_remove = xmlutils.props_from_request(xml_content)
  44. try:
  45. props = radicale_item.check_and_sanitize_props(props_with_remove)
  46. except ValueError as e:
  47. logger.warning(
  48. "Bad MKCOL request on %r: %s", path, e, exc_info=True)
  49. return httputils.BAD_REQUEST
  50. if (props.get("tag") and "w" not in permissions or
  51. not props.get("tag") and "W" not in permissions):
  52. return httputils.NOT_ALLOWED
  53. with self._storage.acquire_lock("w", user):
  54. item = next(iter(self._storage.discover(path)), None)
  55. if item:
  56. return httputils.METHOD_NOT_ALLOWED
  57. parent_path = pathutils.unstrip_path(
  58. posixpath.dirname(pathutils.strip_path(path)), True)
  59. parent_item = next(iter(self._storage.discover(parent_path)), None)
  60. if not parent_item:
  61. return httputils.CONFLICT
  62. if (not isinstance(parent_item, storage.BaseCollection) or
  63. parent_item.tag):
  64. return httputils.FORBIDDEN
  65. try:
  66. self._storage.create_collection(path, props=props)
  67. except ValueError as e:
  68. logger.warning(
  69. "Bad MKCOL request on %r: %s", path, e, exc_info=True)
  70. return httputils.BAD_REQUEST
  71. return client.CREATED, {}, None