use pycurl to download image in XHTML when a proxy is configured. Fixes #7037
This commit is contained in:
parent
084f0ed46d
commit
a07829c0a9
|
@ -191,6 +191,12 @@ try:
|
|||
except ImportError:
|
||||
HAVE_UPNP_IGD = False
|
||||
|
||||
HAVE_PYCURL = True
|
||||
try:
|
||||
__import__('pycurl')
|
||||
except ImportError:
|
||||
HAVE_PYCURL = False
|
||||
|
||||
|
||||
gajim_identity = {'type': 'pc', 'category': 'client', 'name': 'Gajim'}
|
||||
gajim_common_features = [xmpp.NS_BYTESTREAM, xmpp.NS_SI, xmpp.NS_FILE,
|
||||
|
|
|
@ -54,6 +54,10 @@ if __name__ == '__main__':
|
|||
import gtkgui_helpers
|
||||
from common import gajim
|
||||
from gtkgui_helpers import get_icon_pixmap
|
||||
from common import helpers
|
||||
|
||||
if gajim.HAVE_PYCURL:
|
||||
import pycurl
|
||||
|
||||
import tooltips
|
||||
import logging
|
||||
|
@ -490,9 +494,10 @@ class HtmlHandler(xml.sax.handler.ContentHandler):
|
|||
tag.title = title
|
||||
return tag
|
||||
|
||||
def _get_img(self, attrs):
|
||||
'''Download an image. This function is launched in a separate thread.
|
||||
'''
|
||||
def _get_img_direct(self, attrs):
|
||||
"""
|
||||
Download an image. This function is launched in a separate thread.
|
||||
"""
|
||||
mem, alt = '', ''
|
||||
# Wait maximum 5s for connection
|
||||
socket.setdefaulttimeout(5)
|
||||
|
@ -544,6 +549,61 @@ class HtmlHandler(xml.sax.handler.ContentHandler):
|
|||
break
|
||||
return (mem, alt)
|
||||
|
||||
def _get_img_proxy(self, attrs, proxy):
|
||||
"""
|
||||
Download an image through a proxy. This function is launched in a
|
||||
separate thread.
|
||||
"""
|
||||
if not gajim.HAVE_PYCURL:
|
||||
return '', _('PyCURL is not installed')
|
||||
mem, alt = '', ''
|
||||
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)
|
||||
c.setopt(pycurl.WRITEFUNCTION, b.write)
|
||||
c.setopt(pycurl.USERAGENT, 'Gajim ' + gajim.version)
|
||||
# set proxy
|
||||
c.setopt(pycurl.PROXY, proxy['host'].encode('utf-8'))
|
||||
c.setopt(pycurl.PROXYPORT, proxy['port'])
|
||||
if proxy['useauth']:
|
||||
c.setopt(pycurl.PROXYUSERPWD, proxy['user'].encode('utf-8')\
|
||||
+ ':' + proxy['pass'].encode('utf-8'))
|
||||
c.setopt(pycurl.PROXYAUTH, pycurl.HTTPAUTH_ANY)
|
||||
if proxy['type'] == 'http':
|
||||
c.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_HTTP)
|
||||
elif proxy['type'] == 'socks5':
|
||||
c.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_SOCKS5)
|
||||
x = c.perform()
|
||||
c.close()
|
||||
t = b.getvalue()
|
||||
return (t, attrs.get('alt', ''))
|
||||
except pycurl.error, ex:
|
||||
alt = attrs.get('alt', '')
|
||||
if alt:
|
||||
alt += '\n'
|
||||
if ex[0] == pycurl.E_FILESIZE_EXCEEDED:
|
||||
alt += _('Image is too big')
|
||||
elif ex[0] == pycurl.E_OPERATION_TIMEOUTED:
|
||||
alt += _('Timeout loading image')
|
||||
else:
|
||||
alt += _('Error loading image')
|
||||
except Exception, ex:
|
||||
log.debug('Error loading image %s ' % attrs['src'] + str(ex))
|
||||
pixbuf = None
|
||||
alt = attrs.get('alt', 'Broken image')
|
||||
return ('', alt)
|
||||
|
||||
def _get_img(self, attrs):
|
||||
proxy = helpers.get_proxy_info(self.conv_textview.account)
|
||||
if proxy and proxy['type'] in ('http', 'socks5'):
|
||||
return self._get_img_proxy(attrs, proxy)
|
||||
return self._get_img_direct(attrs)
|
||||
|
||||
def _update_img(self, (mem, alt), attrs, img_mark):
|
||||
'''Callback function called after the function _get_img above.
|
||||
'''
|
||||
|
|
Loading…
Reference in New Issue