How to use the xandikos.webdav.UnsupportedMediaType function in xandikos

To help you get started, we’ve selected a few xandikos examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jelmer / xandikos / xandikos / webdav.py View on Github external
async def _readXmlBody(
        request, expected_tag: Optional[str] = None, strict: bool = True):
    content_type = request.content_type
    base_content_type, params = parse_type(content_type)
    if strict and base_content_type not in ('text/xml', 'application/xml'):
        raise UnsupportedMediaType(content_type)
    body = b''.join(await _readBody(request))
    if os.environ.get('XANDIKOS_DUMP_DAV_XML'):
        print("IN: " + body.decode('utf-8'))
    try:
        et = xmlparse(body)
    except ET.ParseError:
        raise BadRequestError('Unable to parse body.')
    if expected_tag is not None and et.tag != expected_tag:
        raise BadRequestError('Expected %s tag, got %s' %
                              (expected_tag, et.tag))
    return et
github jelmer / xandikos / xandikos / caldav.py View on Github external
async def handle(self, request, environ, app):
        content_type = request.content_type
        base_content_type, params = webdav.parse_type(content_type)
        if base_content_type not in (
            'text/xml', 'application/xml', None, 'text/plain',
            'application/octet-stream',
        ):
            raise webdav.UnsupportedMediaType(content_type)
        href, path, resource = app._get_resource_from_environ(request, environ)
        if resource is not None:
            return webdav._send_simple_dav_error(
                request,
                '403 Forbidden',
                error=ET.Element('{DAV:}resource-must-be-null'),
                description=('Something already exists at %r' % path))
        try:
            resource = app.backend.create_collection(path)
        except FileNotFoundError:
            return webdav.Response(status='409 Conflict')
        el = ET.Element('{DAV:}resourcetype')
        await app.properties['{DAV:}resourcetype'].get_value(
            href, resource, el, environ)
        ET.SubElement(el, '{urn:ietf:params:xml:ns:caldav}calendar')
        app.properties['{DAV:}resourcetype'].set_value(href, resource, el)
github jelmer / xandikos / xandikos / webdav.py View on Github external
async def handle(self, request, environ, app):
        content_type = request.content_type
        base_content_type, params = parse_type(content_type)
        if base_content_type not in (
            'text/plain', 'text/xml', 'application/xml', None,
            'application/octet-stream',
        ):
            raise UnsupportedMediaType(base_content_type)
        href, path, resource = app._get_resource_from_environ(request, environ)
        if resource is not None:
            return _send_method_not_allowed(
                app._get_allowed_methods(request))
        try:
            resource = app.backend.create_collection(path)
        except FileNotFoundError:
            return Response(status=409, reason='Conflict')
        if base_content_type in ('text/xml', 'application/xml'):
            # Extended MKCOL (RFC5689)
            et = await _readXmlBody(request, '{DAV:}mkcol', strict=app.strict)
            propstat = []
            for el in et:
                if el.tag != '{DAV:}set':
                    raise BadRequestError('Unknown tag %s in mkcol' % el.tag)
                propstat.extend(apply_modify_prop(el, href, resource,
github jelmer / xandikos / xandikos / webdav.py View on Github external
return Response(status='417 Expectation Failed')
        try:
            do = self.methods[request.method]
        except KeyError:
            return _send_method_not_allowed(self._get_allowed_methods(request))
        try:
            return await do.handle(request, environ, self)
        except BadRequestError as e:
            return Response(
                status='400 Bad Request',
                body=[e.message.encode(DEFAULT_ENCODING)])
        except NotAcceptableError as e:
            return Response(
                status='406 Not Acceptable',
                body=[str(e).encode(DEFAULT_ENCODING)])
        except UnsupportedMediaType as e:
            return Response(
                status='415 Unsupported Media Type',
                body=[('Unsupported media type %r' % e.content_type)
                      .encode(DEFAULT_ENCODING)])
        except UnauthorizedError:
            return Response(
                status='401 Unauthorized',
                body=[('Please login.'.encode(DEFAULT_ENCODING))])
github jelmer / xandikos / xandikos / webdav.py View on Github external
def __init__(self, content_type):
        super(UnsupportedMediaType, self).__init__(
            "Unsupported media type: %r" % (content_type, ))
        self.content_type = content_type