| OLD | NEW |
| 1 # coding: utf-8 | 1 # coding: utf-8 |
| 2 | 2 |
| 3 # This file is part of the Adblock Plus web scripts, | 3 # This file is part of the Adblock Plus web scripts, |
| 4 # Copyright (C) 2006-2015 Eyeo GmbH | 4 # Copyright (C) 2006-2015 Eyeo GmbH |
| 5 # | 5 # |
| 6 # Adblock Plus is free software: you can redistribute it and/or modify | 6 # Adblock Plus is free software: you can redistribute it and/or modify |
| 7 # it under the terms of the GNU General Public License version 3 as | 7 # it under the terms of the GNU General Public License version 3 as |
| 8 # published by the Free Software Foundation. | 8 # published by the Free Software Foundation. |
| 9 # | 9 # |
| 10 # Adblock Plus is distributed in the hope that it will be useful, | 10 # Adblock Plus is distributed in the hope that it will be useful, |
| 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 # GNU General Public License for more details. | 13 # GNU General Public License for more details. |
| 14 # | 14 # |
| 15 # You should have received a copy of the GNU General Public License | 15 # You should have received a copy of the GNU General Public License |
| 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 17 | 17 |
| 18 import base64 | 18 import base64 |
| 19 import imp | 19 import imp |
| 20 import importlib | 20 import importlib |
| 21 import re | 21 import re |
| 22 import httplib |
| 23 from urlparse import parse_qsl |
| 24 |
| 22 from sitescripts.utils import get_config | 25 from sitescripts.utils import get_config |
| 23 | 26 |
| 24 handlers = {} | 27 handlers = {} |
| 25 authenticated_users = {} | 28 authenticated_users = {} |
| 26 | 29 |
| 27 def url_handler(url): | 30 def url_handler(url): |
| 28 def decorator(func): | 31 def decorator(func): |
| 29 registerUrlHandler(url, func) | 32 registerUrlHandler(url, func) |
| 30 return func | 33 return func |
| 31 return decorator | 34 return decorator |
| (...skipping 20 matching lines...) Expand all Loading... |
| 52 expected_username = config.get(config_section, "basic_auth_username") | 55 expected_username = config.get(config_section, "basic_auth_username") |
| 53 expected_password = config.get(config_section, "basic_auth_password") | 56 expected_password = config.get(config_section, "basic_auth_password") |
| 54 if username == expected_username and password == expected_password: | 57 if username == expected_username and password == expected_password: |
| 55 return f(environ, start_response) | 58 return f(environ, start_response) |
| 56 | 59 |
| 57 realm = get_config().get("DEFAULT", "basic_auth_realm") | 60 realm = get_config().get("DEFAULT", "basic_auth_realm") |
| 58 start_response("401 UNAUTHORIZED", | 61 start_response("401 UNAUTHORIZED", |
| 59 [("WWW-Authenticate", 'Basic realm="%s"' % realm)]) | 62 [("WWW-Authenticate", 'Basic realm="%s"' % realm)]) |
| 60 return "" | 63 return "" |
| 61 | 64 |
| 65 def send_simple_response(start_response, status_code, text=None): |
| 66 status_text = httplib.responses[status_code] |
| 67 |
| 68 status = '%d %s' % (status_code, status_text) |
| 69 start_response(status, [('Content-Type', 'text/plain')]) |
| 70 |
| 71 if text is not None: |
| 72 return [text] |
| 73 return [status_text] |
| 74 |
| 75 def form_handler(func): |
| 76 def wrapper(environ, start_response): |
| 77 if environ['REQUEST_METHOD'] != 'POST': |
| 78 return send_simple_response(start_response, 405) |
| 79 |
| 80 if not environ.get('CONTENT_TYPE', '').startswith('application/x-www-form-ur
lencoded'): |
| 81 return send_simple_response(start_response, 415) |
| 82 |
| 83 try: |
| 84 content_length = int(environ['CONTENT_LENGTH']) |
| 85 except (KeyError, ValueError): |
| 86 content_length = None |
| 87 if content_length is None or content_length < 0: |
| 88 return send_simple_response(start_response, 411) |
| 89 |
| 90 raw_data = parse_qsl(environ['wsgi.input'].read(content_length)) |
| 91 try: |
| 92 data = {k.decode('utf-8'): v.decode('utf-8') for k, v in raw_data} |
| 93 except UnicodeDecodeError: |
| 94 return send_simple_response(start_response, 400, 'Invalid form data encodi
ng') |
| 95 |
| 96 return func(environ, start_response, data) |
| 97 |
| 98 return wrapper |
| 99 |
| 62 def multiplex(environ, start_response): | 100 def multiplex(environ, start_response): |
| 63 try: | 101 try: |
| 64 path = environ["PATH_INFO"] | 102 path = environ["PATH_INFO"] |
| 65 try: | 103 try: |
| 66 handler = handlers[path] | 104 handler = handlers[path] |
| 67 except KeyError: | 105 except KeyError: |
| 68 handler = handlers[re.sub(r"[^/]+$", "", path)] | 106 handler = handlers[re.sub(r"[^/]+$", "", path)] |
| 69 except KeyError: | 107 except KeyError: |
| 70 start_response("404 Not Found", [("Content-Type", "text/plain")]) | 108 start_response("404 Not Found", [("Content-Type", "text/plain")]) |
| 71 return ["Not Found"] | 109 return ["Not Found"] |
| 72 | 110 |
| 73 return handler(environ, start_response) | 111 return handler(environ, start_response) |
| 74 | 112 |
| 75 for module in set(get_config().options("multiplexer")) - set(get_config().defaul
ts()): | 113 for module in set(get_config().options("multiplexer")) - set(get_config().defaul
ts()): |
| 76 module_path = get_config().get("multiplexer", module) | 114 module_path = get_config().get("multiplexer", module) |
| 77 if module_path: | 115 if module_path: |
| 78 imp.load_source(module, module_path) | 116 imp.load_source(module, module_path) |
| 79 else: | 117 else: |
| 80 importlib.import_module(module) | 118 importlib.import_module(module) |
| OLD | NEW |