on master help sign in
Merge pull request #971 from kvch/image-proxy-compatibility
6ebfdf0Adam Tauber
searx/utils.py+9
@@ -1,4 +1,6 @@
1import csv
1import csv
2+import hashlib
3+import hmac
2import os
4import os
3import re
5import re
4
6
@@ -321,3 +323,10 @@ def load_module(filename, module_dir):
321 module = load_source(modname, filepath)
323 module = load_source(modname, filepath)
322 module.name = modname
324 module.name = modname
323 return module
325 return module
326+
327+
328+def new_hmac(secret_key, url):
329+ if sys.version_info[0] == 2:
330+ return hmac.new(bytes(secret_key), url, hashlib.sha256).hexdigest()
331+ else:
332+ return hmac.new(bytes(secret_key, 'utf-8'), url, hashlib.sha256).hexdigest()
searx/webapp.py+4−3
@@ -69,6 +69,7 @@ from searx.plugins import plugins
69from searx.preferences import Preferences, ValidationException
69from searx.preferences import Preferences, ValidationException
70from searx.answerers import answerers
70from searx.answerers import answerers
71from searx.url_utils import urlencode, urlparse, urljoin
71from searx.url_utils import urlencode, urlparse, urljoin
72+from searx.utils import new_hmac
72
73
73# check if the pyopenssl package is installed.
74# check if the pyopenssl package is installed.
74# It is needed for SSL connection without trouble, see #298
75# It is needed for SSL connection without trouble, see #298
@@ -290,7 +291,7 @@ def image_proxify(url):
290 if settings.get('result_proxy'):
291 if settings.get('result_proxy'):
291 return proxify(url)
292 return proxify(url)
292
293
293− h = hmac.new(settings['server']['secret_key'], url.encode('utf-8'), hashlib.sha256).hexdigest()
294+ h = new_hmac(settings['server']['secret_key'], url.encode('utf-8'))
294
295
295 return '{0}?{1}'.format(url_for('image_proxy'),
296 return '{0}?{1}'.format(url_for('image_proxy'),
296 urlencode(dict(url=url.encode('utf-8'), h=h)))
297 urlencode(dict(url=url.encode('utf-8'), h=h)))
@@ -704,7 +705,7 @@ def image_proxy():
704 if not url:
705 if not url:
705 return '', 400
706 return '', 400
706
707
707− h = hmac.new(settings['server']['secret_key'], url, hashlib.sha256).hexdigest()
708+ h = new_hmac(settings['server']['secret_key'], url)
708
709
709 if h != request.args.get('h'):
710 if h != request.args.get('h'):
710 return '', 400
711 return '', 400
@@ -731,7 +732,7 @@ def image_proxy():
731 logger.debug('image-proxy: wrong content-type: {0}'.format(resp.headers.get('content-type')))
732 logger.debug('image-proxy: wrong content-type: {0}'.format(resp.headers.get('content-type')))
732 return '', 400
733 return '', 400
733
734
734− img = ''
735+ img = b''
735 chunk_counter = 0
736 chunk_counter = 0
736
737
737 for chunk in resp.iter_content(1024 * 1024):
738 for chunk in resp.iter_content(1024 * 1024):