@@ -76,61 +76,53 @@ def __init__(
7676 ## TODO: this if/else/elif could possibly be refactored, or we should
7777 ## consider to do streaming into the xmltree library as originally
7878 ## intended. It only makes sense for really huge payloads though.
79- if self .headers .get ("Content-Type" , "" ).startswith (
79+ content_type = self .headers .get ("Content-Type" , "" )
80+ expect_xml = content_type .startswith (
8081 "text/xml"
81- ) or self .headers .get ("Content-Type" , "" ).startswith ("application/xml" ):
82- try :
83- content_length = int (self .headers ["Content-Length" ])
84- except :
85- content_length = - 1
86- if content_length == 0 or not self ._raw :
87- self ._raw = ""
88- self .tree = None
89- log .debug ("No content delivered" )
90- else :
91- ## With response.raw we could be streaming the content, but it does not work because
92- ## the stream often is compressed. We could add uncompression on the fly, but not
93- ## considered worth the effort as for now.
94- # self.tree = etree.parse(response.raw, parser=etree.XMLParser(remove_blank_text=True))
95- try :
96- self .tree = etree .XML (
97- self ._raw ,
98- parser = etree .XMLParser (
99- remove_blank_text = True , huge_tree = self .huge_tree
100- ),
101- )
102- except :
103- logging .critical (
104- "Expected some valid XML from the server, but got this: \n "
105- + str (self ._raw ),
106- exc_info = True ,
107- )
108- raise
109- if log .level <= logging .DEBUG :
110- log .debug (etree .tostring (self .tree , pretty_print = True ))
111- elif self .headers .get ("Content-Type" , "" ).startswith (
82+ ) or content_type .startswith ("application/xml" )
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 (
11287 "text/calendar"
113- ) or self .headers .get ("Content-Type" , "" ).startswith ("text/plain" ):
114- ## text/plain is typically for errors, we shouldn't see it on 200/207 responses.
115- ## TODO: may want to log an error if it's text/plain and 200/207.
116- ## Logic here was moved when refactoring
117- pass
88+ ) or content_type .startswith ("text/plain" )
89+ try :
90+ content_length = int (self .headers ["Content-Length" ])
91+ except :
92+ content_length = - 1
93+ if content_length == 0 or not self ._raw :
94+ self ._raw = ""
95+ self .tree = None
96+ log .debug ("No content delivered" )
11897 else :
119- ## Probably no content type given (iCloud). Some servers
120- ## give text/html as the default when no content is
121- ## delivered or on errors (ref
122- ## https://github.com/python-caldav/caldav/issues/142).
123- ## TODO: maybe just remove all of the code above in this if/else and let all
124- ## data be parsed through this code.
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.
101+ # self.tree = etree.parse(response.raw, parser=etree.XMLParser(remove_blank_text=True))
125102 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.
126108 self .tree = etree .XML (
127109 self ._raw ,
128110 parser = etree .XMLParser (
129111 remove_blank_text = True , huge_tree = self .huge_tree
130112 ),
131113 )
132114 except :
133- pass
115+ if not expect_error :
116+ logging .critical (
117+ "Expected some valid XML from the server, but got this: \n "
118+ + str (self ._raw ),
119+ exc_info = True ,
120+ )
121+ if expect_xml :
122+ raise
123+ else :
124+ if log .level <= logging .DEBUG :
125+ log .debug (etree .tostring (self .tree , pretty_print = True ))
134126
135127 ## this if will always be true as for now, see other comments on streaming.
136128 if hasattr (self , "_raw" ):
0 commit comments