From 5f758b2d39becf251a9097264d7414ad9c471d62 Mon Sep 17 00:00:00 2001
From: Adam Tauber
Date: Wed, 25 Oct 2017 23:56:37 +0200
Subject: [PATCH] [mod] compress saved preferences in url
---
searx/preferences.py | 15 +++++++++++++--
searx/templates/oscar/preferences.html | 2 +-
searx/url_utils.py | 6 ++++--
searx/webapp.py | 14 +++++++++-----
4 files changed, 27 insertions(+), 10 deletions(-)
diff --git a/searx/preferences.py b/searx/preferences.py
index 92fe3853c..d10f4ffc7 100644
--- 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
index 64706b06b..f4b2c63ea 100644
--- 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.") }}
{{ _('Search URL of the currently saved preferences') }} ({{ _('Note: specifying custom settings in the search URL can reduce privacy by leaking data to the clicked result sites.') }}):
-
+
diff --git a/searx/url_utils.py b/searx/url_utils.py
index e9919ab30..dcafc3ba8 100644
--- 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
index f90299b2a..dd93395ee 100644
--- 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 = []