@@ -73,19 +73,11 @@ def __init__(
7373 if davclient :
7474 self .huge_tree = davclient .huge_tree
7575
76- ## TODO: this if/else/elif could possibly be refactored, or we should
77- ## consider to do streaming into the xmltree library as originally
78- ## intended. It only makes sense for really huge payloads though.
7976 content_type = self .headers .get ("Content-Type" , "" )
80- expect_xml = content_type .startswith ("text/xml" ) or content_type .startswith (
81- "application/xml"
82- )
83- ## text/plain is typically for errors, we shouldn't see it on 200/207 responses.
84- ## TODO: may want to log an error if it's text/plain and 200/207.
85- ## Logic here was moved when refactoring
86- expect_error = content_type .startswith (
87- "text/calendar"
88- ) or content_type .startswith ("text/plain" )
77+ xml = ["text/xml" , "application/xml" ]
78+ no_xml = ["text/plain" , "text/calendar" ]
79+ expect_xml = any ((content_type .startswith (x ) for x in xml ))
80+ expect_no_xml = any ((content_type .startswith (x ) for x in no_xml ))
8981 try :
9082 content_length = int (self .headers ["Content-Length" ])
9183 except :
@@ -95,25 +87,37 @@ def __init__(
9587 self .tree = None
9688 log .debug ("No content delivered" )
9789 else :
98- ## With response.raw we could be streaming the content, but it does not work because
99- ## the stream often is compressed. We could add uncompression on the fly, but not
100- ## considered worth the effort as for now.
90+ ## For really huge objects we should pass the object as a stream to the
91+ ## XML parser, like this:
10192 # self.tree = etree.parse(response.raw, parser=etree.XMLParser(remove_blank_text=True))
93+ ## However, we would also need to decompress on the fly. I won't bother now.
10294 try :
103- ## Sometimes no content type given (iCloud). Some servers
104- ## give text/html as the default when no content is
105- ## delivered or on errors (ref
106- ## https://github.com/python-caldav/caldav/issues/142).
107- ## DONE: let all data be parsed through this code.
95+ ## https://github.com/python-caldav/caldav/issues/142
96+ ## We cannot trust the content=type (iCloud, OX and others).
97+ ## We'll try to parse the content as XML no matter
98+ ## the content type given.
10899 self .tree = etree .XML (
109100 self ._raw ,
110101 parser = etree .XMLParser (
111102 remove_blank_text = True , huge_tree = self .huge_tree
112103 ),
113104 )
114105 except :
115- if not expect_error :
116- logging .critical (
106+ ## Content wasn't XML. What does the content-type say?
107+ ## expect_no_xml means text/plain or text/calendar
108+ ## expect_no_xml -> ok, pass on, with debug logging
109+ ## expect_xml means text/xml or application/xml
110+ ## expect_xml -> raise an error
111+ ## anything else (text/plain, text/html, ''),
112+ ## log an error and continue
113+ if not expect_no_xml or log .level <= logging .DEBUG :
114+ if not expect_no_xml :
115+ _log = logging .critical
116+ else :
117+ _log = logging .debug
118+ ## The statement below may not be true.
119+ ## We may be expecting something else
120+ _log (
117121 "Expected some valid XML from the server, but got this: \n "
118122 + str (self ._raw ),
119123 exc_info = True ,
0 commit comments