xmlutils.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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. break
  106. else:
  107. return False
  108. if filter_length == 0:
  109. # Point #1 of rfc4791-9.7.1
  110. return filter_.get("name") == tag
  111. else:
  112. if filter_length == 1:
  113. if filter_[0].tag == _tag("C", "is-not-defined"):
  114. # Point #2 of rfc4791-9.7.1
  115. return filter_.get("name") != tag
  116. if filter_[0].tag == _tag("C", "time-range"):
  117. # Point #3 of rfc4791-9.7.1
  118. if not _time_range_match(item, filter_):
  119. return False
  120. filter_.remove(filter_[0])
  121. # Point #4 of rfc4791-9.7.1
  122. return all(
  123. _prop_match(item, child) if child.tag == _tag("C", "prop-filter")
  124. else _comp_match(item, child, scope="component")
  125. for child in filter_)
  126. def _prop_match(item, filter_):
  127. """Check whether the ``item`` matches the prop ``filter_``.
  128. See rfc4791-9.7.2 and rfc6352-10.5.1.
  129. """
  130. filter_length = len(filter_)
  131. if item.collection.get_meta("tag") == "VCALENDAR":
  132. for component in item.components():
  133. if component.name in ("VTODO", "VEVENT", "VJOURNAL"):
  134. vobject_item = component
  135. else:
  136. vobject_item = item.item
  137. if filter_length == 0:
  138. # Point #1 of rfc4791-9.7.2
  139. return filter_.get("name").lower() in vobject_item.contents
  140. else:
  141. if filter_length == 1:
  142. if filter_[0].tag == _tag("C", "is-not-defined"):
  143. # Point #2 of rfc4791-9.7.2
  144. return filter_.get("name").lower() not in vobject_item.contents
  145. if filter_[0].tag == _tag("C", "time-range"):
  146. # Point #3 of rfc4791-9.7.2
  147. if not _time_range_match(item, filter_[0]):
  148. return False
  149. filter_.remove(filter_[0])
  150. elif filter_[0].tag == _tag("C", "text-match"):
  151. # Point #4 of rfc4791-9.7.2
  152. # TODO: collations are not supported, but the default ones needed
  153. # for DAV servers are actually pretty useless. Texts are lowered to
  154. # be case-insensitive, almost as the "i;ascii-casemap" value.
  155. match = next(filter_[0].itertext()).lower()
  156. value = vobject_item.getChildValue(filter_.get("name").lower())
  157. if value is None:
  158. return False
  159. value = value.lower()
  160. if filter_[0].get("negate-condition") == "yes":
  161. if match in value:
  162. return False
  163. elif match not in value:
  164. return False
  165. filter_.remove(filter_[0])
  166. return all(
  167. _param_filter_match(item, param_filter)
  168. for param_filter in filter_)
  169. def _time_range_match(item, filter_):
  170. """Check whether the ``item`` matches the time-range ``filter_``.
  171. See rfc4791-9.9.
  172. """
  173. # TODO: implement this
  174. return True
  175. def _param_filter_match(item, filter_):
  176. """Check whether the ``item`` matches the param-filter ``filter_``.
  177. See rfc4791-9.7.3.
  178. """
  179. # TODO: implement this
  180. return True
  181. def name_from_path(path, collection):
  182. """Return Radicale item name from ``path``."""
  183. collection_parts = collection.path.strip("/").split("/")
  184. path_parts = path.strip("/").split("/")
  185. if (len(path_parts) - len(collection_parts)):
  186. return path_parts[-1]
  187. def props_from_request(root, actions=("set", "remove")):
  188. """Return a list of properties as a dictionary."""
  189. result = OrderedDict()
  190. if root:
  191. if not hasattr(root, "tag"):
  192. root = ET.fromstring(root.encode("utf8"))
  193. else:
  194. return result
  195. for action in actions:
  196. action_element = root.find(_tag("D", action))
  197. if action_element is not None:
  198. break
  199. else:
  200. action_element = root
  201. prop_element = action_element.find(_tag("D", "prop"))
  202. if prop_element is not None:
  203. for prop in prop_element:
  204. if prop.tag == _tag("D", "resourcetype"):
  205. for resource_type in prop:
  206. if resource_type.tag == _tag("C", "calendar"):
  207. result["tag"] = "VCALENDAR"
  208. break
  209. elif resource_type.tag == _tag("CR", "addressbook"):
  210. result["tag"] = "VADDRESSBOOK"
  211. break
  212. elif prop.tag == _tag("C", "supported-calendar-component-set"):
  213. result[_tag_from_clark(prop.tag)] = ",".join(
  214. supported_comp.attrib["name"]
  215. for supported_comp in prop
  216. if supported_comp.tag == _tag("C", "comp"))
  217. else:
  218. result[_tag_from_clark(prop.tag)] = prop.text
  219. return result
  220. def delete(path, collection):
  221. """Read and answer DELETE requests.
  222. Read rfc4918-9.6 for info.
  223. """
  224. # Reading request
  225. if collection.path == path.strip("/"):
  226. # Delete the whole collection
  227. collection.delete()
  228. else:
  229. # Remove an item from the collection
  230. collection.delete(name_from_path(path, collection))
  231. # Writing answer
  232. multistatus = ET.Element(_tag("D", "multistatus"))
  233. response = ET.Element(_tag("D", "response"))
  234. multistatus.append(response)
  235. href = ET.Element(_tag("D", "href"))
  236. href.text = _href(collection, path)
  237. response.append(href)
  238. status = ET.Element(_tag("D", "status"))
  239. status.text = _response(200)
  240. response.append(status)
  241. return _pretty_xml(multistatus)
  242. def propfind(path, xml_request, read_collections, write_collections,
  243. user=None):
  244. """Read and answer PROPFIND requests.
  245. Read rfc4918-9.1 for info.
  246. The collections parameter is a list of collections that are to be included
  247. in the output.
  248. """
  249. # Reading request
  250. if xml_request:
  251. root = ET.fromstring(xml_request.encode("utf8"))
  252. props = [prop.tag for prop in root.find(_tag("D", "prop"))]
  253. else:
  254. props = [_tag("D", "getcontenttype"),
  255. _tag("D", "resourcetype"),
  256. _tag("D", "displayname"),
  257. _tag("D", "owner"),
  258. _tag("D", "getetag"),
  259. _tag("ICAL", "calendar-color"),
  260. _tag("CS", "getctag")]
  261. # Writing answer
  262. multistatus = ET.Element(_tag("D", "multistatus"))
  263. collections = []
  264. for collection in write_collections:
  265. collections.append(collection)
  266. response = _propfind_response(
  267. path, collection, props, user, write=True)
  268. multistatus.append(response)
  269. for collection in read_collections:
  270. if collection in collections:
  271. continue
  272. response = _propfind_response(
  273. path, collection, props, user, write=False)
  274. multistatus.append(response)
  275. return _pretty_xml(multistatus)
  276. def _propfind_response(path, item, props, user, write=False):
  277. """Build and return a PROPFIND response."""
  278. # TODO: fix this
  279. is_collection = hasattr(item, "list")
  280. if is_collection:
  281. is_leaf = bool(item.list())
  282. collection = item
  283. else:
  284. collection = item.collection
  285. response = ET.Element(_tag("D", "response"))
  286. href = ET.Element(_tag("D", "href"))
  287. if is_collection:
  288. uri = item.path
  289. else:
  290. # TODO: fix this
  291. if path.split("/")[-1] == item.href:
  292. # Happening when depth is 0
  293. uri = path
  294. else:
  295. # Happening when depth is 1
  296. uri = "/".join((path, item.href))
  297. # TODO: fix this
  298. href.text = _href(collection, uri.replace("//", "/"))
  299. response.append(href)
  300. propstat404 = ET.Element(_tag("D", "propstat"))
  301. propstat200 = ET.Element(_tag("D", "propstat"))
  302. response.append(propstat200)
  303. prop200 = ET.Element(_tag("D", "prop"))
  304. propstat200.append(prop200)
  305. prop404 = ET.Element(_tag("D", "prop"))
  306. propstat404.append(prop404)
  307. for tag in props:
  308. element = ET.Element(tag)
  309. is404 = False
  310. if tag == _tag("D", "getetag"):
  311. element.text = item.etag
  312. elif tag == _tag("D", "principal-URL"):
  313. tag = ET.Element(_tag("D", "href"))
  314. tag.text = _href(collection, path)
  315. element.append(tag)
  316. elif tag == _tag("D", "getlastmodified"):
  317. element.text = item.last_modified
  318. elif tag in (_tag("D", "principal-collection-set"),
  319. _tag("C", "calendar-user-address-set"),
  320. _tag("CR", "addressbook-home-set"),
  321. _tag("C", "calendar-home-set")):
  322. tag = ET.Element(_tag("D", "href"))
  323. tag.text = _href(collection, path)
  324. element.append(tag)
  325. elif tag == _tag("C", "supported-calendar-component-set"):
  326. # This is not a Todo
  327. # pylint: disable=W0511
  328. human_tag = _tag_from_clark(tag)
  329. if is_collection and is_leaf:
  330. meta = item.get_meta(human_tag)
  331. if meta:
  332. components = meta.split(",")
  333. else:
  334. components = ("VTODO", "VEVENT", "VJOURNAL")
  335. for component in components:
  336. comp = ET.Element(_tag("C", "comp"))
  337. comp.set("name", component)
  338. element.append(comp)
  339. else:
  340. is404 = True
  341. # pylint: enable=W0511
  342. elif tag == _tag("D", "current-user-principal") and user:
  343. tag = ET.Element(_tag("D", "href"))
  344. tag.text = _href(collection, "/%s/" % user)
  345. element.append(tag)
  346. elif tag == _tag("D", "current-user-privilege-set"):
  347. privilege = ET.Element(_tag("D", "privilege"))
  348. if write:
  349. privilege.append(ET.Element(_tag("D", "all")))
  350. privilege.append(ET.Element(_tag("D", "write")))
  351. privilege.append(ET.Element(_tag("D", "write-properties")))
  352. privilege.append(ET.Element(_tag("D", "write-content")))
  353. privilege.append(ET.Element(_tag("D", "read")))
  354. element.append(privilege)
  355. elif tag == _tag("D", "supported-report-set"):
  356. for report_name in (
  357. "principal-property-search", "sync-collection",
  358. "expand-property", "principal-search-property-set"):
  359. supported = ET.Element(_tag("D", "supported-report"))
  360. report_tag = ET.Element(_tag("D", "report"))
  361. report_tag.text = report_name
  362. supported.append(report_tag)
  363. element.append(supported)
  364. elif is_collection:
  365. if tag == _tag("D", "getcontenttype"):
  366. item_tag = item.get_meta("tag")
  367. if item_tag:
  368. element.text = storage.MIMETYPES[item_tag]
  369. else:
  370. is404 = True
  371. elif tag == _tag("D", "resourcetype"):
  372. if item.is_principal:
  373. tag = ET.Element(_tag("D", "principal"))
  374. element.append(tag)
  375. item_tag = item.get_meta("tag")
  376. if is_leaf or item_tag:
  377. # 2nd case happens when the collection is not stored yet,
  378. # but the resource type is guessed
  379. if item.get_meta("tag") == "VADDRESSBOOK":
  380. tag = ET.Element(_tag("CR", "addressbook"))
  381. element.append(tag)
  382. elif item.get_meta("tag") == "VCALENDAR":
  383. tag = ET.Element(_tag("C", "calendar"))
  384. element.append(tag)
  385. tag = ET.Element(_tag("D", "collection"))
  386. element.append(tag)
  387. elif is_leaf:
  388. if tag == _tag("D", "owner") and item.owner:
  389. element.text = "/%s/" % item.owner
  390. elif tag == _tag("CS", "getctag"):
  391. element.text = item.etag
  392. elif tag == _tag("C", "calendar-timezone"):
  393. timezones = set()
  394. for href, _ in item.list():
  395. event = item.get(href)
  396. if "vtimezone" in event.contents:
  397. for timezone in event.vtimezone_list:
  398. timezones.add(timezone)
  399. collection = vobject.iCalendar()
  400. for timezone in timezones:
  401. collection.add(timezone)
  402. element.text = collection.serialize()
  403. elif tag == _tag("D", "displayname"):
  404. element.text = item.get_meta("D:displayname") or item.path
  405. elif tag == _tag("ICAL", "calendar-color"):
  406. element.text = item.get_meta("ICAL:calendar-color")
  407. else:
  408. human_tag = _tag_from_clark(tag)
  409. meta = item.get_meta(human_tag)
  410. if meta:
  411. element.text = meta
  412. else:
  413. is404 = True
  414. else:
  415. is404 = True
  416. # Not for collections
  417. elif tag == _tag("D", "getcontenttype"):
  418. name = item.name.lower()
  419. mimetype = "text/vcard" if name == "vcard" else "text/calendar"
  420. element.text = "%s; component=%s" % (mimetype, name)
  421. elif tag == _tag("D", "resourcetype"):
  422. # resourcetype must be returned empty for non-collection elements
  423. pass
  424. elif tag == _tag("D", "getcontentlength"):
  425. encoding = collection.configuration.get("encoding", "request")
  426. element.text = str(len(item.serialize().encode(encoding)))
  427. else:
  428. is404 = True
  429. if is404:
  430. prop404.append(element)
  431. else:
  432. prop200.append(element)
  433. status200 = ET.Element(_tag("D", "status"))
  434. status200.text = _response(200)
  435. propstat200.append(status200)
  436. status404 = ET.Element(_tag("D", "status"))
  437. status404.text = _response(404)
  438. propstat404.append(status404)
  439. if len(prop404):
  440. response.append(propstat404)
  441. return response
  442. def _add_propstat_to(element, tag, status_number):
  443. """Add a PROPSTAT response structure to an element.
  444. The PROPSTAT answer structure is defined in rfc4918-9.1. It is added to the
  445. given ``element``, for the following ``tag`` with the given
  446. ``status_number``.
  447. """
  448. propstat = ET.Element(_tag("D", "propstat"))
  449. element.append(propstat)
  450. prop = ET.Element(_tag("D", "prop"))
  451. propstat.append(prop)
  452. if "{" in tag:
  453. clark_tag = tag
  454. else:
  455. clark_tag = _tag(*tag.split(":", 1))
  456. prop_tag = ET.Element(clark_tag)
  457. prop.append(prop_tag)
  458. status = ET.Element(_tag("D", "status"))
  459. status.text = _response(status_number)
  460. propstat.append(status)
  461. def proppatch(path, xml_request, collection):
  462. """Read and answer PROPPATCH requests.
  463. Read rfc4918-9.2 for info.
  464. """
  465. # Reading request
  466. root = ET.fromstring(xml_request.encode("utf8"))
  467. props_to_set = props_from_request(root, actions=("set",))
  468. props_to_remove = props_from_request(root, actions=("remove",))
  469. # Writing answer
  470. multistatus = ET.Element(_tag("D", "multistatus"))
  471. response = ET.Element(_tag("D", "response"))
  472. multistatus.append(response)
  473. href = ET.Element(_tag("D", "href"))
  474. href.text = _href(collection, path)
  475. response.append(href)
  476. for short_name, value in props_to_set.items():
  477. collection.set_meta(short_name, value)
  478. _add_propstat_to(response, short_name, 200)
  479. for short_name in props_to_remove:
  480. collection.set_meta(short_name, '')
  481. _add_propstat_to(response, short_name, 200)
  482. return _pretty_xml(multistatus)
  483. def report(path, xml_request, collection):
  484. """Read and answer REPORT requests.
  485. Read rfc3253-3.6 for info.
  486. """
  487. # Reading request
  488. root = ET.fromstring(xml_request.encode("utf8"))
  489. prop_element = root.find(_tag("D", "prop"))
  490. props = (
  491. [prop.tag for prop in prop_element]
  492. if prop_element is not None else [])
  493. if collection:
  494. if root.tag in (_tag("C", "calendar-multiget"),
  495. _tag("CR", "addressbook-multiget")):
  496. # Read rfc4791-7.9 for info
  497. base_prefix = collection.configuration.get("server", "base_prefix")
  498. hreferences = set()
  499. for href_element in root.findall(_tag("D", "href")):
  500. href_path = unquote(urlparse(href_element.text).path)
  501. if href_path.startswith(base_prefix):
  502. hreferences.add(href_path[len(base_prefix) - 1:])
  503. else:
  504. hreferences = (path,)
  505. filters = (
  506. root.findall(".//%s" % _tag("C", "filter")) +
  507. root.findall(".//%s" % _tag("CR", "filter")))
  508. else:
  509. hreferences = filters = ()
  510. # Writing answer
  511. multistatus = ET.Element(_tag("D", "multistatus"))
  512. for hreference in hreferences:
  513. # Check if the reference is an item or a collection
  514. name = name_from_path(hreference, collection)
  515. if name:
  516. # Reference is an item
  517. path = "/".join(hreference.split("/")[:-1]) + "/"
  518. item = collection.get(name)
  519. if item is None:
  520. multistatus.append(
  521. _item_response(hreference, found_item=False))
  522. continue
  523. items = [item]
  524. else:
  525. # Reference is a collection
  526. path = hreference
  527. items = [collection.get(href) for href, etag in collection.list()]
  528. for item in items:
  529. if filters:
  530. match = (
  531. _comp_match if collection.get_meta("tag") == "VCALENDAR"
  532. else _prop_match)
  533. if not all(match(item, filter_[0]) for filter_ in filters):
  534. continue
  535. found_props = []
  536. not_found_props = []
  537. for tag in props:
  538. element = ET.Element(tag)
  539. if tag == _tag("D", "getetag"):
  540. element.text = item.etag
  541. found_props.append(element)
  542. elif tag == _tag("D", "getcontenttype"):
  543. name = item.name.lower()
  544. mimetype = (
  545. "text/vcard" if name == "vcard" else "text/calendar")
  546. element.text = "%s; component=%s" % (mimetype, name)
  547. found_props.append(element)
  548. elif tag in (_tag("C", "calendar-data"),
  549. _tag("CR", "address-data")):
  550. element.text = item.serialize()
  551. found_props.append(element)
  552. else:
  553. not_found_props.append(element)
  554. # TODO: fix this
  555. if hreference.split("/")[-1] == item.href:
  556. # Happening when depth is 0
  557. uri = hreference
  558. else:
  559. # Happening when depth is 1
  560. uri = posixpath.join(hreference, item.href)
  561. multistatus.append(_item_response(
  562. uri, found_props=found_props,
  563. not_found_props=not_found_props, found_item=True))
  564. return _pretty_xml(multistatus)
  565. def _item_response(href, found_props=(), not_found_props=(), found_item=True):
  566. response = ET.Element(_tag("D", "response"))
  567. href_tag = ET.Element(_tag("D", "href"))
  568. href_tag.text = href
  569. response.append(href_tag)
  570. if found_item:
  571. if found_props:
  572. propstat = ET.Element(_tag("D", "propstat"))
  573. status = ET.Element(_tag("D", "status"))
  574. status.text = _response(200)
  575. prop = ET.Element(_tag("D", "prop"))
  576. for p in found_props:
  577. prop.append(p)
  578. propstat.append(prop)
  579. propstat.append(status)
  580. response.append(propstat)
  581. if not_found_props:
  582. propstat = ET.Element(_tag("D", "propstat"))
  583. status = ET.Element(_tag("D", "status"))
  584. status.text = _response(404)
  585. prop = ET.Element(_tag("D", "prop"))
  586. for p in not_found_props:
  587. prop.append(p)
  588. propstat.append(prop)
  589. propstat.append(status)
  590. response.append(propstat)
  591. else:
  592. status = ET.Element(_tag("D", "status"))
  593. status.text = _response(404)
  594. response.append(status)
  595. return response