[mod] https rewrite pluginification

This commit is contained in:
Adam Tauber 2015-04-13 00:30:12 +02:00
parent 146928a749
commit d2a636f75d
41 changed files with 29 additions and 26 deletions

View File

@ -36,11 +36,6 @@ if 'SEARX_SETTINGS_PATH' in environ:
else: else:
settings_path = join(searx_dir, 'settings.yml') settings_path = join(searx_dir, 'settings.yml')
if 'SEARX_HTTPS_REWRITE_PATH' in environ:
https_rewrite_path = environ['SEARX_HTTPS_REWRITE_PATH']
else:
https_rewrite_path = join(searx_dir, 'https_rules')
# load settings # load settings
with open(settings_path) as settings_yaml: with open(settings_path) as settings_yaml:
settings = load(settings_yaml) settings = load(settings_yaml)
@ -52,10 +47,4 @@ else:
logger = logging.getLogger('searx') logger = logging.getLogger('searx')
# load https rules only if https rewrite is enabled
if settings.get('server', {}).get('https_rewrite'):
# loade https rules
from searx.https_rewrite import load_https_rules
load_https_rules(https_rewrite_path)
logger.info('Initialisation done') logger.info('Initialisation done')

View File

@ -14,13 +14,15 @@ along with searx. If not, see < http://www.gnu.org/licenses/ >.
(C) 2015 by Adam Tauber, <asciimoo@gmail.com> (C) 2015 by Adam Tauber, <asciimoo@gmail.com>
''' '''
from searx.plugins import (self_ip,
search_on_category_select)
from searx import logger
from sys import exit from sys import exit
from searx import logger
logger = logger.getChild('plugins') logger = logger.getChild('plugins')
from searx.plugins import (https_rewrite,
self_ip,
search_on_category_select)
required_attrs = (('name', str), required_attrs = (('name', str),
('description', str), ('description', str),
('default_on', bool)) ('default_on', bool))
@ -68,5 +70,6 @@ class PluginStore():
plugins = PluginStore() plugins = PluginStore()
plugins.register(https_rewrite)
plugins.register(self_ip) plugins.register(self_ip)
plugins.register(search_on_category_select) plugins.register(search_on_category_select)

View File

@ -18,11 +18,22 @@ along with searx. If not, see < http://www.gnu.org/licenses/ >.
import re import re
from urlparse import urlparse from urlparse import urlparse
from lxml import etree from lxml import etree
from os import listdir from os import listdir, environ
from os.path import isfile, isdir, join from os.path import isfile, isdir, join
from searx import logger from searx.plugins import logger
from flask.ext.babel import gettext
from searx import searx_dir
name = "HTTPS rewrite"
description = gettext('Rewrite HTTP links to HTTPS if possible')
default_on = True
if 'SEARX_HTTPS_REWRITE_PATH' in environ:
rules_path = environ['SEARX_rules_path']
else:
rules_path = join(searx_dir, 'plugins/https_rules')
logger = logger.getChild("https_rewrite") logger = logger.getChild("https_rewrite")
# https://gitweb.torproject.org/\ # https://gitweb.torproject.org/\
@ -33,7 +44,7 @@ https_rules = []
# load single ruleset from a xml file # load single ruleset from a xml file
def load_single_https_ruleset(filepath): def load_single_https_ruleset(rules_path):
ruleset = () ruleset = ()
# init parser # init parser
@ -41,7 +52,7 @@ def load_single_https_ruleset(filepath):
# load and parse xml-file # load and parse xml-file
try: try:
tree = etree.parse(filepath, parser) tree = etree.parse(rules_path, parser)
except: except:
# TODO, error message # TODO, error message
return () return ()
@ -207,3 +218,10 @@ def https_url_rewrite(result):
# target has matched, do not search over the other rules # target has matched, do not search over the other rules
break break
return result return result
def on_result(request, ctx):
result = ctx['result']
if result['parsed_url'].scheme == 'http':
https_url_rewrite(result)
return True

View File

@ -6,7 +6,6 @@ server:
base_url : False # Set custom base_url. Possible values: False or "https://your.custom.host/location/" base_url : False # Set custom base_url. Possible values: False or "https://your.custom.host/location/"
themes_path : "" # Custom ui themes path - leave it blank if you didn't change themes_path : "" # Custom ui themes path - leave it blank if you didn't change
default_theme : oscar # ui theme default_theme : oscar # ui theme
https_rewrite : True # Force rewrite result urls. See searx/https_rewrite.py
useragent_suffix : "" # suffix of searx_useragent, could contain informations like an email address to the administrator useragent_suffix : "" # suffix of searx_useragent, could contain informations like an email address to the administrator
image_proxy : False # Proxying image results through searx image_proxy : False # Proxying image results through searx
default_locale : "" # Default interface locale - leave blank to detect from browser information or use codes from the 'locales' config section default_locale : "" # Default interface locale - leave blank to detect from browser information or use codes from the 'locales' config section

View File

@ -59,7 +59,6 @@ from searx.utils import (
) )
from searx.version import VERSION_STRING from searx.version import VERSION_STRING
from searx.languages import language_codes from searx.languages import language_codes
from searx.https_rewrite import https_url_rewrite
from searx.search import Search from searx.search import Search
from searx.query import Query from searx.query import Query
from searx.autocomplete import searx_bang, backends as autocomplete_backends from searx.autocomplete import searx_bang, backends as autocomplete_backends
@ -359,15 +358,10 @@ def index():
for result in search.results: for result in search.results:
plugins.call('on_result', request, locals())
if not search.paging and engines[result['engine']].paging: if not search.paging and engines[result['engine']].paging:
search.paging = True search.paging = True
# check if HTTPS rewrite is required
if settings['server']['https_rewrite']\
and result['parsed_url'].scheme == 'http':
result = https_url_rewrite(result)
if search.request_data.get('format', 'html') == 'html': if search.request_data.get('format', 'html') == 'html':
if 'content' in result: if 'content' in result:
result['content'] = highlight_content(result['content'], result['content'] = highlight_content(result['content'],