python-caldav / caldav

Apache License 2.0
326 stars 98 forks source link

XMLSyntaxError when deleting events #213

Closed devmount closed 2 years ago

devmount commented 2 years ago

First: Thanks for this awesome lib. I get the following error when trying to delete events from a CalDAV calendar:

Traceback (most recent call last):
  File "/home/devmount/test/test.py", line 12, in <module>
    e.delete()
  File "/home/devmount/.local/lib/python3.10/site-packages/caldav/objects.py", line 286, in delete
    r = self.client.delete(self.url)
  File "/home/devmount/.local/lib/python3.10/site-packages/caldav/davclient.py", line 496, in delete
    return self.request(url, "DELETE")
  File "/home/devmount/.local/lib/python3.10/site-packages/caldav/davclient.py", line 528, in request
    response = DAVResponse(r)
  File "/home/devmount/.local/lib/python3.10/site-packages/caldav/davclient.py", line 60, in __init__
    self.tree = etree.XML(self._raw, parser=etree.XMLParser(remove_blank_text=True))
  File "src/lxml/etree.pyx", line 3233, in lxml.etree.XML
  File "src/lxml/parser.pxi", line 1913, in lxml.etree._parseMemoryDocument
  File "src/lxml/parser.pxi", line 1800, in lxml.etree._parseDoc
  File "src/lxml/parser.pxi", line 1141, in lxml.etree._BaseParser._parseDoc
  File "src/lxml/parser.pxi", line 615, in lxml.etree._ParserContext._handleParseResultDoc
  File "src/lxml/parser.pxi", line 725, in lxml.etree._handleParseResult
  File "src/lxml/parser.pxi", line 654, in lxml.etree._raiseParseError
  File "<string>", line 1
lxml.etree.XMLSyntaxError: Document is empty, line 1, column 1

Here is a minimal reproducible example:

import caldav

URL  = "https://your-caldav-test-url"
USER = "user"
PASS = "password"

client = caldav.DAVClient(url=URL, username=USER, password=PASS)
calendar = client.calendar(url=URL)
result = calendar.events()
count = 0
for e in result:
  e.delete()
  count += 1
print(count)

Am I doing something wrong here? How to fix this? Maybe this is caused by wrong character encoding like suggested here?

tobixen commented 2 years ago

Did the event get deleted?

What calendar server are you using?

Is it possible for me to get a test account so I can test those things myself?

Except for that, I would suggest to throw in some debug logging before line 60 in davclient.py to see what self._raw is set to.

tobixen commented 2 years ago

(it does seem to me that the client expects some XML in return, while the server returns something else - as a minimum I should throw in a try/except here and give a better error message)

devmount commented 2 years ago

Thanks for the quick reply!

I used this public platform for testing: https://calendar.robur.coop/ (you can simply create an account to test calendars)

The event was not deleted.

(it does seem to me that the client expects some XML in return, while the server returns something else - as a minimum I should throw in a try/except here and give a better error message)

Yes, that would be great.

devmount commented 2 years ago

When adding a print(self._raw) before line 60, I get this when no events are there (so this is correct):

b'<?xml version="1.0" encoding="utf-8" ?>\n<D:multistatus xmlns:D="DAV:"></D:multistatus>'

And I must correct myself: The events actually DO get deleted.

tobixen commented 2 years ago

Looks like valid XML to me.

I should look more into this, but I don't have time right now.

devmount commented 2 years ago

And I get this with 2 example events in the calendar:

b'<?xml version="1.0" encoding="utf-8" ?>\n<D:multistatus xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav"><D:response><D:href>/calendars/mozilla/727cfac8f7acafab91abea264a2c96b552e5dc8e6293980b0d83ec19ef87c3d1.ics</D:href><D:propstat><D:prop><C:calendar-data>BEGIN:VCALENDAR\nVERSION:2.0\nCALSCALE:GREGORIAN\nPRODID:-//Inf-IT//CalDavZAP 0.13.1//EN\nBEGIN:VEVENT\nUID:dub72emh-v8iy-jyo3-tiwg-8usntqac8gt8\nDTSTAMP:20220922T141039Z\nDTSTART;VALUE=DATE:20220930\nDTEND;VALUE=DATE:20221001\nCREATED:20220922T141039Z\nLAST-MODIFIED:20220922T141039Z\nSUMMARY:test\nTRANSP:OPAQUE\nCLASS:PUBLIC\nEND:VEVENT\nEND:VCALENDAR\n</C:calendar-data></D:prop><D:status>HTTP/1.1 200 OK</D:status></D:propstat></D:response><D:response><D:href>/calendars/mozilla/faed7286c5425a820e36b1b11a121d7d04dc3476232fd21ad5ce5b3275645e16.ics</D:href><D:propstat><D:prop><C:calendar-data>BEGIN:VCALENDAR\nVERSION:2.0\nCALSCALE:GREGORIAN\nPRODID:-//Inf-IT//CalDavZAP 0.13.1//EN\nBEGIN:VEVENT\nUID:vv90891v-en29-dhsg-dpvp-8r03ryi4yj5o\nDTSTAMP:20220922T141048Z\nDTSTART;VALUE=DATE:20220929\nDTEND;VALUE=DATE:20220930\nCREATED:20220922T141048Z\nLAST-MODIFIED:20220922T141048Z\nSUMMARY:test\nTRANSP:OPAQUE\nCLASS:PUBLIC\nEND:VEVENT\nEND:VCALENDAR\n</C:calendar-data></D:prop><D:status>HTTP/1.1 200 OK</D:status></D:propstat></D:response></D:multistatus>'
b''
Traceback (most recent call last):
  File "/home/devmount/test/test.py", line 12, in <module>
    e.delete()
  File "/home/devmount/.local/lib/python3.10/site-packages/caldav/objects.py", line 286, in delete
    r = self.client.delete(self.url)
  File "/home/devmount/.local/lib/python3.10/site-packages/caldav/davclient.py", line 498, in delete
    return self.request(url, "DELETE")
  File "/home/devmount/.local/lib/python3.10/site-packages/caldav/davclient.py", line 530, in request
    response = DAVResponse(r)
  File "/home/devmount/.local/lib/python3.10/site-packages/caldav/davclient.py", line 62, in __init__
    self.tree = etree.XML(self._raw, parser=etree.XMLParser(remove_blank_text=True))
  File "src/lxml/etree.pyx", line 3233, in lxml.etree.XML
  File "src/lxml/parser.pxi", line 1913, in lxml.etree._parseMemoryDocument
  File "src/lxml/parser.pxi", line 1800, in lxml.etree._parseDoc
  File "src/lxml/parser.pxi", line 1141, in lxml.etree._BaseParser._parseDoc
  File "src/lxml/parser.pxi", line 615, in lxml.etree._ParserContext._handleParseResultDoc
  File "src/lxml/parser.pxi", line 725, in lxml.etree._handleParseResult
  File "src/lxml/parser.pxi", line 654, in lxml.etree._raiseParseError
  File "<string>", line 1
lxml.etree.XMLSyntaxError: Document is empty, line 1, column 1
devmount commented 2 years ago

I should look more into this, but I don't have time right now.

Thank you very much and please take your time.

tobixen commented 2 years ago

Obviously it's the b'' that is the problem, that's an empty string and not valid XML - but the server claims (in Content-Type) that it should be valid XML. Bug at the server side, I would say - but I will make a workaround.

tobixen commented 2 years ago

Could you try the fix/213 branch and see if that solves the problem?

devmount commented 2 years ago

Yes, seems like the server returns XML and an additional empty string.

Could you try the fix/213 branch and see if that solves the problem?

YES! This fixes it 🙌🏻 💪🏻 Thank you so much for your time 🙏🏻

tobixen commented 2 years ago

I'll leave this issue open, as it's needed to add some test code as well.

Also, I should check out that calendaring service and run the test suite towards it.

tobixen commented 2 years ago

Thanks for making me aware of the calendar.robur.coop service. I've been running through the test suite and summarized my findings in issue #215

devmount commented 2 years ago

You're very welcome. Thank you for testing and improving this service!