From 4f1d1ec869cbc5d7f9ed3729339cddf0e39332d5 Mon Sep 17 00:00:00 2001 From: Ildar Gafurov Date: Mon, 7 May 2018 22:45:16 +0300 Subject: [PATCH] webdav3/client.py: Client.list method fix --- webdav3/client.py | 16 +++++----------- webdav3/urn.py | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/webdav3/client.py b/webdav3/client.py index 07a54a4..a72dc36 100644 --- a/webdav3/client.py +++ b/webdav3/client.py @@ -246,8 +246,8 @@ class Client(object): response = self.execute_request(action='list', path=directory_urn.quote()) urns = WebDavXmlUtils.parse_get_list_response(response.content) - path = self.get_full_path(directory_urn) - return [urn.filename() for urn in urns if urn.path() != path and urn.path() != path[:-1]] + path = Urn.normalize_path(self.get_full_path(directory_urn)) + return [urn.filename() for urn in urns if Urn.compare_path(path, urn.path()) is False] @wrap_connection_error def free(self): @@ -814,7 +814,7 @@ class WebDavXmlUtils: """ try: tree = etree.fromstring(content) - hrees = [unquote(hree.text) for hree in tree.findall(".//{DAV:}href")] + hrees = [Urn.separate + unquote(urlsplit(hree.text).path) for hree in tree.findall(".//{DAV:}href")] return [Urn(hree) for hree in hrees] except etree.XMLSyntaxError: return list() @@ -963,18 +963,12 @@ class WebDavXmlUtils: tree = etree.fromstring(content) responses = tree.findall("{DAV:}response") - def normalization_fn(p): - result = (sub, '/{2,}', '/') - return result if result[-1] != Urn.separate else result[:-1] - - normalized_path = normalization_fn(path) + n_path = Urn.normalize_path(path) for resp in responses: href = resp.findtext("{DAV:}href") - urn = unquote(urlsplit(href).path) - - if normalized_path == normalization_fn(urn): + if Urn.compare_path(n_path, href) is True: return resp raise RemoteResourceNotFound(path) except etree.XMLSyntaxError: diff --git a/webdav3/urn.py b/webdav3/urn.py index 4d6b869..6279de2 100644 --- a/webdav3/urn.py +++ b/webdav3/urn.py @@ -1,7 +1,8 @@ try: - from urllib.parse import unquote, quote + from urllib.parse import unquote, quote, urlsplit except ImportError: from urllib import unquote, quote + from urlparse import urlsplit from re import sub @@ -53,4 +54,14 @@ class Urn(object): return self._path.count(Urn.separate, 0, -1) def is_dir(self): - return self._path[-1] == Urn.separate \ No newline at end of file + return self._path[-1] == Urn.separate + + @staticmethod + def normalize_path(path): + result = sub('/{2,}', '/', path) + return result if len(result) < 1 or result[-1] != Urn.separate else result[:-1] + + @staticmethod + def compare_path(path_a, href): + unqouted_path = Urn.separate + unquote(urlsplit(href).path) + return Urn.normalize_path(path_a) == Urn.normalize_path(unqouted_path)