logo

searx

My custom branche(s) on searx, a meta-search engine
commit: 5f758b2d39becf251a9097264d7414ad9c471d62
parent: b34124fd8a6b020136ca9656acdb01afceabe96f
Author: Adam Tauber <asciimoo@gmail.com>
Date:   Wed, 25 Oct 2017 23:56:37 +0200

[mod] compress saved preferences in url

Diffstat:

Msearx/preferences.py15+++++++++++++--
Msearx/templates/oscar/preferences.html2+-
Msearx/url_utils.py6++++--
Msearx/webapp.py14+++++++++-----
4 files changed, 27 insertions(+), 10 deletions(-)

diff --git a/searx/preferences.py b/searx/preferences.py @@ -1,6 +1,13 @@ +from base64 import urlsafe_b64encode, urlsafe_b64decode +from zlib import compress, decompress +from sys import version + from searx import settings, autocomplete from searx.languages import language_codes as languages -from searx.url_utils import urlencode +from searx.url_utils import parse_qs, urlencode + +if version[0] == '3': + unicode = str COOKIE_MAX_AGE = 60 * 60 * 24 * 365 * 5 # 5 years @@ -279,7 +286,11 @@ class Preferences(object): settings_kv['disabled_plugins'] = ','.join(self.plugins.disabled) settings_kv['enabled_plugins'] = ','.join(self.plugins.enabled) - return urlencode(settings_kv) + return urlsafe_b64encode(compress(urlencode(settings_kv).encode('utf-8'))).decode('utf-8') + + def parse_encoded_data(self, input_data): + decoded_data = decompress(urlsafe_b64decode(input_data.encode('utf-8'))) + self.parse_dict({x: y[0] for x,y in parse_qs(unicode(decoded_data)).items()}) def parse_dict(self, input_data): for user_setting_name, user_setting in input_data.items(): diff --git a/searx/templates/oscar/preferences.html b/searx/templates/oscar/preferences.html @@ -287,7 +287,7 @@ {{ _("These cookies serve your sole convenience, we don't use these cookies to track you.") }} </p> <p style="margin:20px 0;">{{ _('Search URL of the currently saved preferences') }} <small class="text-muted">({{ _('Note: specifying custom settings in the search URL can reduce privacy by leaking data to the clicked result sites.') }})</small>:<br/> - <input readonly="" class="form-control select-all-on-click cursor-text" type="url" value="{{ url_for('index', _external=True) }}?{{ preferences_url_params|e }}{% raw %}&amp;q=%s{% endraw %}"> + <input readonly="" class="form-control select-all-on-click cursor-text" type="url" value="{{ url_for('index', _external=True) }}?preferences={{ preferences_url_params|e }}{% raw %}&amp;q=%s{% endraw %}"> </p> <input type="submit" class="btn btn-primary" value="{{ _('save') }}" /> diff --git a/searx/url_utils.py b/searx/url_utils.py @@ -2,9 +2,10 @@ from sys import version_info if version_info[0] == 2: from urllib import quote, quote_plus, unquote, urlencode - from urlparse import parse_qsl, urljoin, urlparse, urlunparse, ParseResult + from urlparse import parse_qs, parse_qsl, urljoin, urlparse, urlunparse, ParseResult else: from urllib.parse import ( + parse_qs, parse_qsl, quote, quote_plus, @@ -17,7 +18,8 @@ else: ) -__export__ = (parse_qsl, +__export__ = (parse_qs, + parse_qsl, quote, quote_plus, unquote, diff --git a/searx/webapp.py b/searx/webapp.py @@ -403,11 +403,15 @@ def pre_request(): for k, v in request.args.items(): if k not in request.form: request.form[k] = v - try: - preferences.parse_dict(request.form) - except Exception as e: - logger.exception('invalid settings') - request.errors.append(gettext('Invalid settings')) + + if request.form.get('preferences'): + preferences.parse_encoded_data(request.form['preferences']) + else: + try: + preferences.parse_dict(request.form) + except Exception as e: + logger.exception('invalid settings') + request.errors.append(gettext('Invalid settings')) # request.user_plugins request.user_plugins = []