1
0
Fork 0
mirror of https://github.com/Kozea/Radicale.git synced 2025-06-26 16:45:52 +00:00
Radicale/radicale/app/mkcalendar.py

93 lines
4 KiB
Python
Raw Permalink Normal View History

2021-12-08 21:45:42 +01:00
# This file is part of Radicale - CalDAV and CardDAV server
2018-08-28 16:19:36 +02:00
# Copyright © 2008 Nicolas Kandel
# Copyright © 2008 Pascal Halter
# Copyright © 2008-2017 Guillaume Ayoub
2025-02-11 16:26:57 +01:00
# Copyright © 2017-2021 Unrud <unrud@outlook.com>
# Copyright © 2024-2025 Peter Bieringer <pb@bieringer.de>
2018-08-28 16:19:36 +02:00
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Radicale. If not, see <http://www.gnu.org/licenses/>.
2025-02-11 16:29:56 +01:00
import errno
2019-06-15 09:01:55 +02:00
import posixpath
2025-02-11 16:29:56 +01:00
import re
2018-08-28 16:19:36 +02:00
import socket
from http import client
2021-07-26 20:56:46 +02:00
import radicale.item as radicale_item
from radicale import httputils, pathutils, storage, types, xmlutils
from radicale.app.base import ApplicationBase
2018-08-28 16:19:36 +02:00
from radicale.log import logger
2021-07-26 20:56:46 +02:00
class ApplicationPartMkcalendar(ApplicationBase):
def do_MKCALENDAR(self, environ: types.WSGIEnviron, base_prefix: str,
path: str, user: str) -> types.WSGIResponse:
2018-08-28 16:19:36 +02:00
"""Manage MKCALENDAR request."""
2020-04-09 22:02:03 +02:00
if "w" not in self._rights.authorization(user, path):
2018-08-28 16:19:36 +02:00
return httputils.NOT_ALLOWED
try:
2020-09-14 21:19:48 +02:00
xml_content = self._read_xml_request_body(environ)
2018-08-28 16:19:36 +02:00
except RuntimeError as e:
logger.warning(
"Bad MKCALENDAR request on %r: %s", path, e, exc_info=True)
return httputils.BAD_REQUEST
2018-11-04 18:54:11 +00:00
except socket.timeout:
logger.debug("Client timed out", exc_info=True)
2018-08-28 16:19:36 +02:00
return httputils.REQUEST_TIMEOUT
# Prepare before locking
2021-07-26 20:56:46 +02:00
props_with_remove = xmlutils.props_from_request(xml_content)
props_with_remove["tag"] = "VCALENDAR"
2018-08-28 16:19:36 +02:00
try:
2021-07-26 20:56:46 +02:00
props = radicale_item.check_and_sanitize_props(props_with_remove)
2018-08-28 16:19:36 +02:00
except ValueError as e:
logger.warning(
"Bad MKCALENDAR request on %r: %s", path, e, exc_info=True)
2020-10-11 19:13:10 +02:00
return httputils.BAD_REQUEST
2021-07-26 20:56:46 +02:00
# TODO: use this?
# timezone = props.get("C:calendar-timezone")
with self._storage.acquire_lock("w", user):
2021-07-26 20:56:46 +02:00
item = next(iter(self._storage.discover(path)), None)
2018-08-28 16:19:36 +02:00
if item:
2020-05-24 13:19:30 +02:00
return self._webdav_error_response(
client.CONFLICT, "D:resource-must-be-null")
2018-08-28 16:19:50 +02:00
parent_path = pathutils.unstrip_path(
posixpath.dirname(pathutils.strip_path(path)), True)
2021-07-26 20:56:46 +02:00
parent_item = next(iter(self._storage.discover(parent_path)), None)
2018-08-28 16:19:36 +02:00
if not parent_item:
return httputils.CONFLICT
if (not isinstance(parent_item, storage.BaseCollection) or
2021-07-26 20:56:46 +02:00
parent_item.tag):
2018-08-28 16:19:36 +02:00
return httputils.FORBIDDEN
try:
self._storage.create_collection(path, props=props)
2018-08-28 16:19:36 +02:00
except ValueError as e:
2025-02-11 16:29:56 +01:00
# return better matching HTTP result in case errno is provided and catched
errno_match = re.search("\\[Errno ([0-9]+)\\]", str(e))
if errno_match:
logger.error(
"Failed MKCALENDAR request on %r: %s", path, e, exc_info=True)
errno_e = int(errno_match.group(1))
if errno_e == errno.ENOSPC:
return httputils.INSUFFICIENT_STORAGE
elif errno_e in [errno.EPERM, errno.EACCES]:
return httputils.FORBIDDEN
else:
return httputils.INTERNAL_SERVER_ERROR
else:
logger.warning(
"Bad MKCALENDAR request on %r: %s", path, e, exc_info=True)
return httputils.BAD_REQUEST
2018-08-28 16:19:36 +02:00
return client.CREATED, {}, None