From e95ffa55ef689d6667d4b55ce07d345c57ff5a1e Mon Sep 17 00:00:00 2001
From: Denis Fomin
Date: Tue, 14 Dec 2010 19:30:00 +0300
Subject: [PATCH] support data:URI scheme in XHTML-IM message. Fixed #6106
---
src/htmltextview.py | 109 ++++++++++++++++++++++++++------------------
1 file changed, 64 insertions(+), 45 deletions(-)
diff --git a/src/htmltextview.py b/src/htmltextview.py
index 3467fdc2f..3afd7d82b 100644
--- a/src/htmltextview.py
+++ b/src/htmltextview.py
@@ -489,54 +489,60 @@ class HtmlHandler(xml.sax.handler.ContentHandler):
'''
mem = ''
try:
- # Wait maximum 1s for connection
- socket.setdefaulttimeout(1)
- try:
- req = urllib2.Request(attrs['src'])
- req.add_header('User-Agent', 'Gajim ' + gajim.version)
- f = urllib2.urlopen(req)
- except Exception, ex:
- log.debug('Error loading image %s ' % attrs['src'] + str(ex))
- pixbuf = None
- alt = attrs.get('alt', 'Broken image')
+ if attrs['src'].startswith('data:image/'):
+ # The "data" URL scheme http://tools.ietf.org/html/rfc2397
+ import base64
+ img = attrs['src'].split(',')[1]
+ mem = base64.standard_b64decode(urllib2.unquote(img))
else:
- # Wait 0.1s between each byte
+ # Wait maximum 1s for connection
+ socket.setdefaulttimeout(1)
try:
- f.fp._sock.fp._sock.settimeout(0.5)
- except Exception:
- pass
- # Max image size = 2 MB (to try to prevent DoS)
- deadline = time.time() + 3
- while True:
- if time.time() > deadline:
- log.debug(str('Timeout loading image %s ' % \
- attrs['src'] + ex))
- mem = ''
- alt = attrs.get('alt', '')
- if alt:
- alt += '\n'
- alt += _('Timeout loading image')
- break
+ req = urllib2.Request(attrs['src'])
+ req.add_header('User-Agent', 'Gajim ' + gajim.version)
+ f = urllib2.urlopen(req)
+ except Exception, ex:
+ log.debug('Error loading image %s ' % attrs['src'] + str(ex))
+ pixbuf = None
+ alt = attrs.get('alt', 'Broken image')
+ else:
+ # Wait 0.1s between each byte
try:
- temp = f.read(100)
- except socket.timeout, ex:
- log.debug('Timeout loading image %s ' % \
- attrs['src'] + str(ex))
- alt = attrs.get('alt', '')
- if alt:
- alt += '\n'
- alt += _('Timeout loading image')
- break
- if temp:
- mem += temp
- else:
- break
- if len(mem) > 2*1024*1024:
- alt = attrs.get('alt', '')
- if alt:
- alt += '\n'
- alt += _('Image is too big')
- break
+ f.fp._sock.fp._sock.settimeout(0.5)
+ except Exception:
+ pass
+ # Max image size = 2 MB (to try to prevent DoS)
+ deadline = time.time() + 3
+ while True:
+ if time.time() > deadline:
+ log.debug(str('Timeout loading image %s ' % \
+ attrs['src'] + ex))
+ mem = ''
+ alt = attrs.get('alt', '')
+ if alt:
+ alt += '\n'
+ alt += _('Timeout loading image')
+ break
+ try:
+ temp = f.read(100)
+ except socket.timeout, ex:
+ log.debug('Timeout loading image %s ' % \
+ attrs['src'] + str(ex))
+ alt = attrs.get('alt', '')
+ if alt:
+ alt += '\n'
+ alt += _('Timeout loading image')
+ break
+ if temp:
+ mem += temp
+ else:
+ break
+ if len(mem) > 2*1024*1024:
+ alt = attrs.get('alt', '')
+ if alt:
+ alt += '\n'
+ alt += _('Image is too big')
+ break
pixbuf = None
if mem:
# Caveat: GdkPixbuf is known not to be safe to load
@@ -1090,6 +1096,19 @@ return faciter(n,1)
#232/1
+
+
+