[mod] compress saved preferences in url

This commit is contained in:
Adam Tauber 2017-10-25 23:56:37 +02:00
parent b34124fd8a
commit 5f758b2d39
4 changed files with 27 additions and 10 deletions

View File

@ -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():

View File

@ -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') }}" />

View File

@ -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,

View File

@ -403,6 +403,10 @@ def pre_request():
for k, v in request.args.items():
if k not in request.form:
request.form[k] = v
if request.form.get('preferences'):
preferences.parse_encoded_data(request.form['preferences'])
else:
try:
preferences.parse_dict(request.form)
except Exception as e: