Skip to content

Commit cdbc269

Browse files
author
Carlos M. Esterlizi
committed
improved performance
check function using HEAD instead using PROPFIND and listing
1 parent 66072fc commit cdbc269

File tree

1 file changed

+11
-37
lines changed

1 file changed

+11
-37
lines changed

webdav/client.py

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
except ImportError:
1717
from urllib import unquote
1818

19-
__version__ = "1.0.7"
19+
__version__ = "1.0.8"
2020

2121

2222
def listdir(directory):
@@ -71,7 +71,7 @@ class Client(object):
7171
'move': ["Accept: */*"],
7272
'mkdir': ["Accept: */*", "Connection: Keep-Alive"],
7373
'clean': ["Accept: */*", "Connection: Keep-Alive"],
74-
'check': ["Accept: */*", "Depth: 0"],
74+
'check': ["Accept: */*"],
7575
'info': ["Accept: */*", "Depth: 1"],
7676
'get_metadata': ["Accept: */*", "Depth: 1", "Content-Type: application/x-www-form-urlencoded"],
7777
'set_metadata': ["Accept: */*", "Depth: 1", "Content-Type: application/x-www-form-urlencoded"]
@@ -98,7 +98,7 @@ def get_header(self, method):
9898
'move': "MOVE",
9999
'mkdir': "MKCOL",
100100
'clean': "DELETE",
101-
'check': "PROPFIND",
101+
'check': "HEAD",
102102
'list': "PROPFIND",
103103
'free': "PROPFIND",
104104
'info': "PROPFIND",
@@ -274,53 +274,30 @@ def data():
274274
raise NotConnection(self.webdav.hostname)
275275

276276
def check(self, remote_path=root):
277-
278-
def parse(response, path):
279-
280-
try:
281-
response_str = response.getvalue()
282-
tree = etree.fromstring(response_str)
283-
284-
resps = tree.findall("{DAV:}response")
285-
286-
for resp in resps:
287-
href = resp.findtext("{DAV:}href")
288-
urn = unquote(href)
289-
290-
if path.endswith(Urn.separate):
291-
if path == urn or path[:-1] == urn:
292-
return True
293-
else:
294-
if path == urn:
295-
return True
296-
297-
return False
298-
299-
except etree.XMLSyntaxError:
300-
return False
301277

302278
try:
303279
urn = Urn(remote_path)
304-
parent_urn = Urn(urn.parent())
305280
response = BytesIO()
306281

307-
url = {'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': parent_urn.quote()}
282+
url = {'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': urn.quote()}
308283
options = {
309284
'URL': "{hostname}{root}{path}".format(**url),
310-
'CUSTOMREQUEST': Client.requests['info'],
311-
'HTTPHEADER': self.get_header('info'),
285+
'CUSTOMREQUEST': Client.requests['check'],
286+
'HTTPHEADER': self.get_header('check'),
312287
'WRITEDATA': response,
313288
'NOBODY': 0
314289
}
315290

316291
request = self.Request(options=options)
317292

318293
request.perform()
294+
code = request.getinfo(pycurl.HTTP_CODE)
319295
request.close()
320296

321-
path = "{root}{path}".format(root=self.webdav.root, path=urn.path())
322-
323-
return parse(response, path)
297+
if int(code) == 200:
298+
return True
299+
300+
return False
324301

325302
except pycurl.error:
326303
raise NotConnection(self.webdav.hostname)
@@ -523,9 +500,6 @@ def upload_file(self, remote_path, local_path, progress=None):
523500
if os.path.isdir(local_path):
524501
raise OptionNotValid(name="local_path", value=local_path)
525502

526-
if not os.path.exists(local_path):
527-
raise LocalResourceNotFound(local_path)
528-
529503
if not self.check(urn.parent()):
530504
raise RemoteParentNotFound(urn.path())
531505

0 commit comments

Comments
 (0)