From 55402bdc5076c43da352cbcaa5833485733661c7 Mon Sep 17 00:00:00 2001 From: Yann Leboulanger Date: Thu, 20 Aug 2015 21:53:59 +0200 Subject: [PATCH] [tmolitor] ability to download bigger files with higher timeouts. Fixes #8130 --- src/common/helpers.py | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/common/helpers.py b/src/common/helpers.py index f2ba6e090..4999addc0 100644 --- a/src/common/helpers.py +++ b/src/common/helpers.py @@ -1433,8 +1433,11 @@ def _get_img_direct(attrs): """ mem = b'' alt = '' - # Wait maximum 5s for connection - socket.setdefaulttimeout(5) + max_size = 2*1024*1024 + if 'max_size' in attrs: + max_size = attrs['max_size'] + # Wait maximum 10s for connection + socket.setdefaulttimeout(10) try: req = urllib.request.Request(attrs['src']) req.add_header('User-Agent', 'Gajim ' + gajim.version) @@ -1444,13 +1447,13 @@ def _get_img_direct(attrs): pixbuf = None alt = attrs.get('alt', 'Broken image') else: - # Wait 0.5s between each byte + # Wait 2s between each byte try: - f.fp._sock.fp._sock.settimeout(0.5) + f.fp._sock.fp._sock.settimeout(2) except Exception: pass - # Max image size = 2 MB (to try to prevent DoS) - deadline = time.time() + 3 + # On a slow internet connection with ~1000kbps you need ~10 seconds for 1 MB + deadline = time.time() + (10 * (max_size / 1048576)) while True: if time.time() > deadline: log.debug('Timeout loading image %s ' % attrs['src']) @@ -1473,7 +1476,7 @@ def _get_img_direct(attrs): mem += temp else: break - if len(mem) > 2*1024*1024: + if len(mem) > max_size: alt = attrs.get('alt', '') if alt: alt += '\n' @@ -1489,15 +1492,19 @@ def _get_img_proxy(attrs, proxy): """ if not gajim.HAVE_PYCURL: return '', _('PyCURL is not installed') - mem, alt = '', '' + mem, alt, max_size = '', '', 2*1024*1024 + if 'max_size' in attrs: + max_size = attrs['max_size'] try: b = StringIO() c = pycurl.Curl() c.setopt(pycurl.URL, attrs['src'].encode('utf-8')) c.setopt(pycurl.FOLLOWLOCATION, 1) - c.setopt(pycurl.CONNECTTIMEOUT, 5) - c.setopt(pycurl.TIMEOUT, 10) - c.setopt(pycurl.MAXFILESIZE, 2000000) + # Wait maximum 10s for connection + c.setopt(pycurl.CONNECTTIMEOUT, 10) + # On a slow internet connection with ~1000kbps you need ~10 seconds for 1 MB + c.setopt(pycurl.TIMEOUT, 10 * (max_size / 1048576)) + c.setopt(pycurl.MAXFILESIZE, max_size) c.setopt(pycurl.WRITEFUNCTION, b.write) c.setopt(pycurl.USERAGENT, 'Gajim ' + gajim.version) # set proxy