xmlutils.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2008 Nicolas Kandel
  3. # Copyright © 2008 Pascal Halter
  4. # Copyright © 2008-2015 Guillaume Ayoub
  5. #
  6. # This library is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This library is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. XML and iCal requests manager.
  20. Note that all these functions need to receive unicode objects for full
  21. iCal requests (PUT) and string objects with charset correctly defined
  22. in them for XML requests (all but PUT).
  23. """
  24. import posixpath
  25. import re
  26. import xml.etree.ElementTree as ET
  27. from collections import OrderedDict
  28. from urllib.parse import unquote, urlparse
  29. import vobject
  30. from . import client, storage
  31. NAMESPACES = {
  32. "C": "urn:ietf:params:xml:ns:caldav",
  33. "CR": "urn:ietf:params:xml:ns:carddav",
  34. "D": "DAV:",
  35. "CS": "http://calendarserver.org/ns/",
  36. "ICAL": "http://apple.com/ns/ical/",
  37. "ME": "http://me.com/_namespace/"}
  38. NAMESPACES_REV = {}
  39. for short, url in NAMESPACES.items():
  40. NAMESPACES_REV[url] = short
  41. ET.register_namespace("" if short == "D" else short, url)
  42. CLARK_TAG_REGEX = re.compile(r"""
  43. { # {
  44. (?P<namespace>[^}]*) # namespace URL
  45. } # }
  46. (?P<tag>.*) # short tag name
  47. """, re.VERBOSE)
  48. def _pretty_xml(element, level=0):
  49. """Indent an ElementTree ``element`` and its children."""
  50. i = "\n" + level * " "
  51. if len(element):
  52. if not element.text or not element.text.strip():
  53. element.text = i + " "
  54. if not element.tail or not element.tail.strip():
  55. element.tail = i
  56. for sub_element in element:
  57. _pretty_xml(sub_element, level + 1)
  58. # ``sub_element`` is always defined as len(element) > 0
  59. # pylint: disable=W0631
  60. if not sub_element.tail or not sub_element.tail.strip():
  61. sub_element.tail = i
  62. # pylint: enable=W0631
  63. else:
  64. if level and (not element.tail or not element.tail.strip()):
  65. element.tail = i
  66. if not level:
  67. return '<?xml version="1.0"?>\n%s' % ET.tostring(element, "unicode")
  68. def _tag(short_name, local):
  69. """Get XML Clark notation {uri(``short_name``)}``local``."""
  70. return "{%s}%s" % (NAMESPACES[short_name], local)
  71. def _tag_from_clark(name):
  72. """Get a human-readable variant of the XML Clark notation tag ``name``.
  73. For a given name using the XML Clark notation, return a human-readable
  74. variant of the tag name for known namespaces. Otherwise, return the name as
  75. is.
  76. """
  77. match = CLARK_TAG_REGEX.match(name)
  78. if match and match.group("namespace") in NAMESPACES_REV:
  79. args = {
  80. "ns": NAMESPACES_REV[match.group("namespace")],
  81. "tag": match.group("tag")}
  82. return "%(ns)s:%(tag)s" % args
  83. return name
  84. def _response(code):
  85. """Return full W3C names from HTTP status codes."""
  86. return "HTTP/1.1 %i %s" % (code, client.responses[code])
  87. def _href(collection, href):
  88. """Return prefixed href."""
  89. return "%s%s" % (
  90. collection.configuration.get("server", "base_prefix"),
  91. href.lstrip("/"))
  92. def _comp_match(item, filter_, scope="collection"):
  93. """Check whether the ``item`` matches the comp ``filter_``.
  94. If ``scope`` is ``"collection"``, the filter is applied on the
  95. item's collection. Otherwise, it's applied on the item.
  96. See rfc4791-9.7.1.
  97. """
  98. filter_length = len(filter_)
  99. if scope == "collection":
  100. tag = item.collection.get_meta("tag")
  101. else:
  102. for component in item.components():
  103. if component.name in ("VTODO", "VEVENT", "VJOURNAL"):
  104. tag = component.name
  105. if filter_length == 0:
  106. # Point #1 of rfc4791-9.7.1
  107. return filter_.get("name") == tag
  108. else:
  109. if filter_length == 1:
  110. if filter_[0].tag == _tag("C", "is-not-defined"):
  111. # Point #2 of rfc4791-9.7.1
  112. return filter_.get("name") != tag
  113. if filter_[0].tag == _tag("C", "time-range"):
  114. # Point #3 of rfc4791-9.7.1
  115. if not _time_range_match(item, filter_[0]):
  116. return False
  117. filter_.remove(filter_[0])
  118. # Point #4 of rfc4791-9.7.1
  119. return all(
  120. _prop_match(item, child) if child.tag == _tag("C", "prop-filter")
  121. else _comp_match(item, child, scope="component")
  122. for child in filter_)
  123. def _prop_match(item, filter_):
  124. """Check whether the ``item`` matches the prop ``filter_``.
  125. See rfc4791-9.7.2 and rfc6352-10.5.1.
  126. """
  127. filter_length = len(filter_)
  128. if item.collection.get_meta("tag") == "VCALENDAR":
  129. for component in item.components():
  130. if component.name in ("VTODO", "VEVENT", "VJOURNAL"):
  131. vobject_item = component
  132. else:
  133. vobject_item = item.item
  134. if filter_length == 0:
  135. # Point #1 of rfc4791-9.7.2
  136. return filter_.get("name").lower() in vobject_item.contents
  137. else:
  138. if filter_length == 1:
  139. if filter_[0].tag == _tag("C", "is-not-defined"):
  140. # Point #2 of rfc4791-9.7.2
  141. return filter_.get("name").lower() not in vobject_item.contents
  142. if filter_[0].tag == _tag("C", "time-range"):
  143. # Point #3 of rfc4791-9.7.2
  144. if not _time_range_match(item, filter_[0]):
  145. return False
  146. filter_.remove(filter_[0])
  147. elif filter_[0].tag == _tag("C", "text-match"):
  148. # Point #4 of rfc4791-9.7.2
  149. if not _text_match(item, filter_[0]):
  150. return False
  151. filter_.remove(filter_[0])
  152. return all(
  153. _param_filter_match(item, param_filter)
  154. for param_filter in filter_)
  155. def _time_range_match(item, _filter):
  156. """Check whether the ``item`` matches the time-range ``filter_``.
  157. See rfc4791-9.9.
  158. """
  159. # TODO: implement this
  160. return True
  161. def _text_match(item, _filter):
  162. """Check whether the ``item`` matches the text-match ``filter_``.
  163. See rfc4791-9.7.3.
  164. """
  165. # TODO: implement this
  166. return True
  167. def _param_filter_match(item, _filter):
  168. """Check whether the ``item`` matches the param-filter ``filter_``.
  169. See rfc4791-9.7.3.
  170. """
  171. # TODO: implement this
  172. return True
  173. def name_from_path(path, collection):
  174. """Return Radicale item name from ``path``."""
  175. collection_parts = collection.path.strip("/").split("/")
  176. path_parts = path.strip("/").split("/")
  177. if (len(path_parts) - len(collection_parts)):
  178. return path_parts[-1]
  179. def props_from_request(root, actions=("set", "remove")):
  180. """Return a list of properties as a dictionary."""
  181. result = OrderedDict()
  182. if root:
  183. if not hasattr(root, "tag"):
  184. root = ET.fromstring(root.encode("utf8"))
  185. else:
  186. return result
  187. for action in actions:
  188. action_element = root.find(_tag("D", action))
  189. if action_element is not None:
  190. break
  191. else:
  192. action_element = root
  193. prop_element = action_element.find(_tag("D", "prop"))
  194. if prop_element is not None:
  195. for prop in prop_element:
  196. if prop.tag == _tag("D", "resourcetype"):
  197. for resource_type in prop:
  198. if resource_type.tag == _tag("C", "calendar"):
  199. result["tag"] = "VCALENDAR"
  200. break
  201. elif resource_type.tag == _tag("CR", "addressbook"):
  202. result["tag"] = "VADDRESSBOOK"
  203. break
  204. elif prop.tag == _tag("C", "supported-calendar-component-set"):
  205. result[_tag_from_clark(prop.tag)] = ",".join(
  206. supported_comp.attrib["name"]
  207. for supported_comp in prop
  208. if supported_comp.tag == _tag("C", "comp"))
  209. else:
  210. result[_tag_from_clark(prop.tag)] = prop.text
  211. return result
  212. def delete(path, collection):
  213. """Read and answer DELETE requests.
  214. Read rfc4918-9.6 for info.
  215. """
  216. # Reading request
  217. if collection.path == path.strip("/"):
  218. # Delete the whole collection
  219. collection.delete()
  220. else:
  221. # Remove an item from the collection
  222. collection.delete(name_from_path(path, collection))
  223. # Writing answer
  224. multistatus = ET.Element(_tag("D", "multistatus"))
  225. response = ET.Element(_tag("D", "response"))
  226. multistatus.append(response)
  227. href = ET.Element(_tag("D", "href"))
  228. href.text = _href(collection, path)
  229. response.append(href)
  230. status = ET.Element(_tag("D", "status"))
  231. status.text = _response(200)
  232. response.append(status)
  233. return _pretty_xml(multistatus)
  234. def propfind(path, xml_request, read_collections, write_collections, user=None):
  235. """Read and answer PROPFIND requests.
  236. Read rfc4918-9.1 for info.
  237. The collections parameter is a list of collections that are to be included
  238. in the output.
  239. """
  240. # Reading request
  241. if xml_request:
  242. root = ET.fromstring(xml_request.encode("utf8"))
  243. props = [prop.tag for prop in root.find(_tag("D", "prop"))]
  244. else:
  245. props = [_tag("D", "getcontenttype"),
  246. _tag("D", "resourcetype"),
  247. _tag("D", "displayname"),
  248. _tag("D", "owner"),
  249. _tag("D", "getetag"),
  250. _tag("ICAL", "calendar-color"),
  251. _tag("CS", "getctag")]
  252. # Writing answer
  253. multistatus = ET.Element(_tag("D", "multistatus"))
  254. collections = []
  255. for collection in write_collections:
  256. collections.append(collection)
  257. response = _propfind_response(path, collection, props, user, write=True)
  258. multistatus.append(response)
  259. for collection in read_collections:
  260. if collection in collections:
  261. continue
  262. response = _propfind_response(path, collection, props, user, write=False)
  263. multistatus.append(response)
  264. return _pretty_xml(multistatus)
  265. def _propfind_response(path, item, props, user, write=False):
  266. """Build and return a PROPFIND response."""
  267. # TODO: fix this
  268. is_collection = hasattr(item, "list")
  269. if is_collection:
  270. is_leaf = bool(item.list())
  271. collection = item
  272. else:
  273. collection = item.collection
  274. response = ET.Element(_tag("D", "response"))
  275. href = ET.Element(_tag("D", "href"))
  276. if is_collection:
  277. uri = item.path
  278. else:
  279. # TODO: fix this
  280. if path.split("/")[-1] == item.href:
  281. # Happening when depth is 0
  282. uri = path
  283. else:
  284. # Happening when depth is 1
  285. uri = "/".join((path, item.href))
  286. # TODO: fix this
  287. href.text = _href(collection, uri.replace("//", "/"))
  288. response.append(href)
  289. propstat404 = ET.Element(_tag("D", "propstat"))
  290. propstat200 = ET.Element(_tag("D", "propstat"))
  291. response.append(propstat200)
  292. prop200 = ET.Element(_tag("D", "prop"))
  293. propstat200.append(prop200)
  294. prop404 = ET.Element(_tag("D", "prop"))
  295. propstat404.append(prop404)
  296. for tag in props:
  297. element = ET.Element(tag)
  298. is404 = False
  299. if tag == _tag("D", "getetag"):
  300. element.text = item.etag
  301. elif tag == _tag("D", "principal-URL"):
  302. tag = ET.Element(_tag("D", "href"))
  303. tag.text = _href(collection, path)
  304. element.append(tag)
  305. elif tag == _tag("D", "getlastmodified"):
  306. element.text = item.last_modified
  307. elif tag in (_tag("D", "principal-collection-set"),
  308. _tag("C", "calendar-user-address-set"),
  309. _tag("CR", "addressbook-home-set"),
  310. _tag("C", "calendar-home-set")):
  311. tag = ET.Element(_tag("D", "href"))
  312. tag.text = _href(collection, path)
  313. element.append(tag)
  314. elif tag == _tag("C", "supported-calendar-component-set"):
  315. # This is not a Todo
  316. # pylint: disable=W0511
  317. human_tag = _tag_from_clark(tag)
  318. if is_collection and is_leaf:
  319. meta = item.get_meta(human_tag)
  320. if meta:
  321. components = meta.split(",")
  322. else:
  323. components = ("VTODO", "VEVENT", "VJOURNAL")
  324. for component in components:
  325. comp = ET.Element(_tag("C", "comp"))
  326. comp.set("name", component)
  327. element.append(comp)
  328. else:
  329. is404 = True
  330. # pylint: enable=W0511
  331. elif tag == _tag("D", "current-user-principal") and user:
  332. tag = ET.Element(_tag("D", "href"))
  333. tag.text = _href(collection, "/%s/" % user)
  334. element.append(tag)
  335. elif tag == _tag("D", "current-user-privilege-set"):
  336. privilege = ET.Element(_tag("D", "privilege"))
  337. if write:
  338. privilege.append(ET.Element(_tag("D", "all")))
  339. privilege.append(ET.Element(_tag("D", "write")))
  340. privilege.append(ET.Element(_tag("D", "write-properties")))
  341. privilege.append(ET.Element(_tag("D", "write-content")))
  342. privilege.append(ET.Element(_tag("D", "read")))
  343. element.append(privilege)
  344. elif tag == _tag("D", "supported-report-set"):
  345. for report_name in (
  346. "principal-property-search", "sync-collection",
  347. "expand-property", "principal-search-property-set"):
  348. supported = ET.Element(_tag("D", "supported-report"))
  349. report_tag = ET.Element(_tag("D", "report"))
  350. report_tag.text = report_name
  351. supported.append(report_tag)
  352. element.append(supported)
  353. elif is_collection:
  354. if tag == _tag("D", "getcontenttype"):
  355. element.text = storage.MIMETYPES[item.get_meta("tag")]
  356. elif tag == _tag("D", "resourcetype"):
  357. if item.is_principal:
  358. tag = ET.Element(_tag("D", "principal"))
  359. element.append(tag)
  360. item_tag = item.get_meta("tag")
  361. if is_leaf or item_tag:
  362. # 2nd case happens when the collection is not stored yet,
  363. # but the resource type is guessed
  364. if item.get_meta("tag") == "VADDRESSBOOK":
  365. tag = ET.Element(_tag("CR", "addressbook"))
  366. element.append(tag)
  367. elif item.get_meta("tag") == "VCALENDAR":
  368. tag = ET.Element(_tag("C", "calendar"))
  369. element.append(tag)
  370. tag = ET.Element(_tag("D", "collection"))
  371. element.append(tag)
  372. elif is_leaf:
  373. if tag == _tag("D", "owner") and item.owner:
  374. element.text = "/%s/" % item.owner
  375. elif tag == _tag("CS", "getctag"):
  376. element.text = item.etag
  377. elif tag == _tag("C", "calendar-timezone"):
  378. timezones = set()
  379. for href, _ in item.list():
  380. event = item.get(href)
  381. if "vtimezone" in event.contents:
  382. for timezone in event.vtimezone_list:
  383. timezones.add(timezone)
  384. collection = vobject.iCalendar()
  385. for timezone in timezones:
  386. collection.add(timezone)
  387. element.text = collection.serialize()
  388. elif tag == _tag("D", "displayname"):
  389. element.text = item.get_meta("D:displayname") or item.path
  390. elif tag == _tag("ICAL", "calendar-color"):
  391. element.text = item.get_meta("ICAL:calendar-color")
  392. else:
  393. human_tag = _tag_from_clark(tag)
  394. meta = item.get_meta(human_tag)
  395. if meta:
  396. element.text = meta
  397. else:
  398. is404 = True
  399. else:
  400. is404 = True
  401. # Not for collections
  402. elif tag == _tag("D", "getcontenttype"):
  403. name = item.name.lower()
  404. mimetype = "text/vcard" if name == "vcard" else "text/calendar"
  405. element.text = "%s; component=%s" % (mimetype, name)
  406. elif tag == _tag("D", "resourcetype"):
  407. # resourcetype must be returned empty for non-collection elements
  408. pass
  409. elif tag == _tag("D", "getcontentlength"):
  410. encoding = collection.configuration.get("encoding", "request")
  411. element.text = str(len(item.serialize().encode(encoding)))
  412. else:
  413. is404 = True
  414. if is404:
  415. prop404.append(element)
  416. else:
  417. prop200.append(element)
  418. status200 = ET.Element(_tag("D", "status"))
  419. status200.text = _response(200)
  420. propstat200.append(status200)
  421. status404 = ET.Element(_tag("D", "status"))
  422. status404.text = _response(404)
  423. propstat404.append(status404)
  424. if len(prop404):
  425. response.append(propstat404)
  426. return response
  427. def _add_propstat_to(element, tag, status_number):
  428. """Add a PROPSTAT response structure to an element.
  429. The PROPSTAT answer structure is defined in rfc4918-9.1. It is added to the
  430. given ``element``, for the following ``tag`` with the given
  431. ``status_number``.
  432. """
  433. propstat = ET.Element(_tag("D", "propstat"))
  434. element.append(propstat)
  435. prop = ET.Element(_tag("D", "prop"))
  436. propstat.append(prop)
  437. if "{" in tag:
  438. clark_tag = tag
  439. else:
  440. clark_tag = _tag(*tag.split(":", 1))
  441. prop_tag = ET.Element(clark_tag)
  442. prop.append(prop_tag)
  443. status = ET.Element(_tag("D", "status"))
  444. status.text = _response(status_number)
  445. propstat.append(status)
  446. def proppatch(path, xml_request, collection):
  447. """Read and answer PROPPATCH requests.
  448. Read rfc4918-9.2 for info.
  449. """
  450. # Reading request
  451. root = ET.fromstring(xml_request.encode("utf8"))
  452. props_to_set = props_from_request(root, actions=("set",))
  453. props_to_remove = props_from_request(root, actions=("remove",))
  454. # Writing answer
  455. multistatus = ET.Element(_tag("D", "multistatus"))
  456. response = ET.Element(_tag("D", "response"))
  457. multistatus.append(response)
  458. href = ET.Element(_tag("D", "href"))
  459. href.text = _href(collection, path)
  460. response.append(href)
  461. for short_name, value in props_to_set.items():
  462. collection.set_meta(short_name, value)
  463. _add_propstat_to(response, short_name, 200)
  464. for short_name in props_to_remove:
  465. collection.set_meta(short_name, '')
  466. _add_propstat_to(response, short_name, 200)
  467. return _pretty_xml(multistatus)
  468. def report(path, xml_request, collection):
  469. """Read and answer REPORT requests.
  470. Read rfc3253-3.6 for info.
  471. """
  472. # Reading request
  473. root = ET.fromstring(xml_request.encode("utf8"))
  474. prop_element = root.find(_tag("D", "prop"))
  475. props = (
  476. [prop.tag for prop in prop_element]
  477. if prop_element is not None else [])
  478. if collection:
  479. if root.tag in (_tag("C", "calendar-multiget"),
  480. _tag("CR", "addressbook-multiget")):
  481. # Read rfc4791-7.9 for info
  482. base_prefix = collection.configuration.get("server", "base_prefix")
  483. hreferences = set()
  484. for href_element in root.findall(_tag("D", "href")):
  485. href_path = unquote(urlparse(href_element.text).path)
  486. if href_path.startswith(base_prefix):
  487. hreferences.add(href_path[len(base_prefix) - 1:])
  488. else:
  489. hreferences = (path,)
  490. filters = (
  491. root.findall(".//%s" % _tag("C", "filter")) +
  492. root.findall(".//%s" % _tag("CR", "filter")))
  493. else:
  494. hreferences = filters = ()
  495. # Writing answer
  496. multistatus = ET.Element(_tag("D", "multistatus"))
  497. for hreference in hreferences:
  498. # Check if the reference is an item or a collection
  499. name = name_from_path(hreference, collection)
  500. if name:
  501. # Reference is an item
  502. path = "/".join(hreference.split("/")[:-1]) + "/"
  503. item = collection.get(name)
  504. if item is None:
  505. multistatus.append(
  506. _item_response(hreference, found_item=False))
  507. continue
  508. items = [item]
  509. else:
  510. # Reference is a collection
  511. path = hreference
  512. items = [collection.get(href) for href, etag in collection.list()]
  513. for item in items:
  514. if filters:
  515. match = (
  516. _comp_match if collection.get_meta("tag") == "VCALENDAR"
  517. else _prop_match)
  518. if not all(match(item, filter_[0]) for filter_ in filters):
  519. continue
  520. found_props = []
  521. not_found_props = []
  522. for tag in props:
  523. element = ET.Element(tag)
  524. if tag == _tag("D", "getetag"):
  525. element.text = item.etag
  526. found_props.append(element)
  527. elif tag == _tag("D", "getcontenttype"):
  528. name = item.name.lower()
  529. mimetype = (
  530. "text/vcard" if name == "vcard" else "text/calendar")
  531. element.text = "%s; component=%s" % (mimetype, name)
  532. found_props.append(element)
  533. elif tag in (_tag("C", "calendar-data"),
  534. _tag("CR", "address-data")):
  535. element.text = item.serialize()
  536. found_props.append(element)
  537. else:
  538. not_found_props.append(element)
  539. # TODO: fix this
  540. if hreference.split("/")[-1] == item.href:
  541. # Happening when depth is 0
  542. uri = hreference
  543. else:
  544. # Happening when depth is 1
  545. uri = posixpath.join(hreference, item.href)
  546. multistatus.append(_item_response(
  547. uri, found_props=found_props,
  548. not_found_props=not_found_props, found_item=True))
  549. return _pretty_xml(multistatus)
  550. def _item_response(href, found_props=(), not_found_props=(), found_item=True):
  551. response = ET.Element(_tag("D", "response"))
  552. href_tag = ET.Element(_tag("D", "href"))
  553. href_tag.text = href
  554. response.append(href_tag)
  555. if found_item:
  556. if found_props:
  557. propstat = ET.Element(_tag("D", "propstat"))
  558. status = ET.Element(_tag("D", "status"))
  559. status.text = _response(200)
  560. prop = ET.Element(_tag("D", "prop"))
  561. for p in found_props:
  562. prop.append(p)
  563. propstat.append(prop)
  564. propstat.append(status)
  565. response.append(propstat)
  566. if not_found_props:
  567. propstat = ET.Element(_tag("D", "propstat"))
  568. status = ET.Element(_tag("D", "status"))
  569. status.text = _response(404)
  570. prop = ET.Element(_tag("D", "prop"))
  571. for p in not_found_props:
  572. prop.append(p)
  573. propstat.append(prop)
  574. propstat.append(status)
  575. response.append(propstat)
  576. else:
  577. status = ET.Element(_tag("D", "status"))
  578. status.text = _response(404)
  579. response.append(status)
  580. return response