Explorar el Código

[mod] compress saved preferences in url

Adam Tauber hace 7 años
padre
commit
5f758b2d39
Se han modificado 4 ficheros con 27 adiciones y 10 borrados
  1. 13
    2
      searx/preferences.py
  2. 1
    1
      searx/templates/oscar/preferences.html
  3. 4
    2
      searx/url_utils.py
  4. 9
    5
      searx/webapp.py

+ 13
- 2
searx/preferences.py Ver fichero

@@ -1,6 +1,13 @@
1
+from base64 import urlsafe_b64encode, urlsafe_b64decode
2
+from zlib import compress, decompress
3
+from sys import version
4
+
1 5
 from searx import settings, autocomplete
2 6
 from searx.languages import language_codes as languages
3
-from searx.url_utils import urlencode
7
+from searx.url_utils import parse_qs, urlencode
8
+
9
+if version[0] == '3':
10
+    unicode = str
4 11
 
5 12
 
6 13
 COOKIE_MAX_AGE = 60 * 60 * 24 * 365 * 5  # 5 years
@@ -279,7 +286,11 @@ class Preferences(object):
279 286
         settings_kv['disabled_plugins'] = ','.join(self.plugins.disabled)
280 287
         settings_kv['enabled_plugins'] = ','.join(self.plugins.enabled)
281 288
 
282
-        return urlencode(settings_kv)
289
+        return urlsafe_b64encode(compress(urlencode(settings_kv).encode('utf-8'))).decode('utf-8')
290
+
291
+    def parse_encoded_data(self, input_data):
292
+        decoded_data = decompress(urlsafe_b64decode(input_data.encode('utf-8')))
293
+        self.parse_dict({x: y[0] for x,y in parse_qs(unicode(decoded_data)).items()})
283 294
 
284 295
     def parse_dict(self, input_data):
285 296
         for user_setting_name, user_setting in input_data.items():

+ 1
- 1
searx/templates/oscar/preferences.html Ver fichero

@@ -287,7 +287,7 @@
287 287
         {{ _("These cookies serve your sole convenience, we don't use these cookies to track you.") }}
288 288
         </p>
289 289
         <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/>
290
-          <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 %}">
290
+          <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 %}">
291 291
         </p>
292 292
 
293 293
         <input type="submit" class="btn btn-primary" value="{{ _('save') }}" />

+ 4
- 2
searx/url_utils.py Ver fichero

@@ -2,9 +2,10 @@ from sys import version_info
2 2
 
3 3
 if version_info[0] == 2:
4 4
     from urllib import quote, quote_plus, unquote, urlencode
5
-    from urlparse import parse_qsl, urljoin, urlparse, urlunparse, ParseResult
5
+    from urlparse import parse_qs, parse_qsl, urljoin, urlparse, urlunparse, ParseResult
6 6
 else:
7 7
     from urllib.parse import (
8
+        parse_qs,
8 9
         parse_qsl,
9 10
         quote,
10 11
         quote_plus,
@@ -17,7 +18,8 @@ else:
17 18
     )
18 19
 
19 20
 
20
-__export__ = (parse_qsl,
21
+__export__ = (parse_qs,
22
+              parse_qsl,
21 23
               quote,
22 24
               quote_plus,
23 25
               unquote,

+ 9
- 5
searx/webapp.py Ver fichero

@@ -403,11 +403,15 @@ def pre_request():
403 403
     for k, v in request.args.items():
404 404
         if k not in request.form:
405 405
             request.form[k] = v
406
-    try:
407
-        preferences.parse_dict(request.form)
408
-    except Exception as e:
409
-        logger.exception('invalid settings')
410
-        request.errors.append(gettext('Invalid settings'))
406
+
407
+    if request.form.get('preferences'):
408
+        preferences.parse_encoded_data(request.form['preferences'])
409
+    else:
410
+        try:
411
+            preferences.parse_dict(request.form)
412
+        except Exception as e:
413
+            logger.exception('invalid settings')
414
+            request.errors.append(gettext('Invalid settings'))
411 415
 
412 416
     # request.user_plugins
413 417
     request.user_plugins = []