OLD | NEW |
1 # This file is part of the Adblock Plus web scripts, | 1 # This file is part of the Adblock Plus web scripts, |
2 # Copyright (C) 2006-2016 Eyeo GmbH | 2 # Copyright (C) 2006-2016 Eyeo GmbH |
3 # | 3 # |
4 # Adblock Plus is free software: you can redistribute it and/or modify | 4 # Adblock Plus is free software: you can redistribute it and/or modify |
5 # it under the terms of the GNU General Public License version 3 as | 5 # it under the terms of the GNU General Public License version 3 as |
6 # published by the Free Software Foundation. | 6 # published by the Free Software Foundation. |
7 # | 7 # |
8 # Adblock Plus is distributed in the hope that it will be useful, | 8 # Adblock Plus is distributed in the hope that it will be useful, |
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
(...skipping 25 matching lines...) Expand all Loading... |
36 | 36 |
37 def registerUrlHandler(url, func): | 37 def registerUrlHandler(url, func): |
38 if url in handlers: | 38 if url in handlers: |
39 raise Exception('A handler for url %s is already registered' % url) | 39 raise Exception('A handler for url %s is already registered' % url) |
40 handlers[url] = func | 40 handlers[url] = func |
41 | 41 |
42 # https://www.python.org/dev/peps/pep-0333/#url-reconstruction | 42 # https://www.python.org/dev/peps/pep-0333/#url-reconstruction |
43 | 43 |
44 | 44 |
45 def request_path(environ, include_query=True): | 45 def request_path(environ, include_query=True): |
46 path = urllib.quote(environ.get("SCRIPT_NAME", "") + | 46 path = urllib.quote(environ.get('SCRIPT_NAME', '') + |
47 environ.get("PATH_INFO", "")) | 47 environ.get('PATH_INFO', '')) |
48 query_string = environ.get("QUERY_STRING", "") | 48 query_string = environ.get('QUERY_STRING', '') |
49 if query_string and include_query: | 49 if query_string and include_query: |
50 path += "?" + urllib.quote(query_string) | 50 path += '?' + urllib.quote(query_string) |
51 return path | 51 return path |
52 | 52 |
53 | 53 |
54 def basic_auth(config_section="DEFAULT"): | 54 def basic_auth(config_section='DEFAULT'): |
55 def decorator(function): | 55 def decorator(function): |
56 def authenticating_wrapper(environ, start_response): | 56 def authenticating_wrapper(environ, start_response): |
57 return authenticate(function, environ, start_response, config_sectio
n) | 57 return authenticate(function, environ, start_response, config_sectio
n) |
58 return authenticating_wrapper | 58 return authenticating_wrapper |
59 return decorator | 59 return decorator |
60 | 60 |
61 | 61 |
62 def authenticate(f, environ, start_response, config_section): | 62 def authenticate(f, environ, start_response, config_section): |
63 if "HTTP_AUTHORIZATION" in environ: | 63 if 'HTTP_AUTHORIZATION' in environ: |
64 auth = environ["HTTP_AUTHORIZATION"].split() | 64 auth = environ['HTTP_AUTHORIZATION'].split() |
65 if len(auth) == 2: | 65 if len(auth) == 2: |
66 if auth[0].lower() == "basic": | 66 if auth[0].lower() == 'basic': |
67 username, password = base64.b64decode(auth[1]).split(":") | 67 username, password = base64.b64decode(auth[1]).split(':') |
68 config = get_config() | 68 config = get_config() |
69 expected_username = config.get(config_section, "basic_auth_usern
ame") | 69 expected_username = config.get(config_section, 'basic_auth_usern
ame') |
70 expected_password = config.get(config_section, "basic_auth_passw
ord") | 70 expected_password = config.get(config_section, 'basic_auth_passw
ord') |
71 if username == expected_username and password == expected_passwo
rd: | 71 if username == expected_username and password == expected_passwo
rd: |
72 return f(environ, start_response) | 72 return f(environ, start_response) |
73 | 73 |
74 realm = get_config().get("DEFAULT", "basic_auth_realm") | 74 realm = get_config().get('DEFAULT', 'basic_auth_realm') |
75 start_response("401 UNAUTHORIZED", | 75 start_response('401 UNAUTHORIZED', |
76 [("WWW-Authenticate", 'Basic realm="%s"' % realm)]) | 76 [('WWW-Authenticate', 'Basic realm="%s"' % realm)]) |
77 return "" | 77 return '' |
78 | 78 |
79 | 79 |
80 def send_simple_response(start_response, status_code, text=None): | 80 def send_simple_response(start_response, status_code, text=None): |
81 status_text = httplib.responses[status_code] | 81 status_text = httplib.responses[status_code] |
82 | 82 |
83 status = '%d %s' % (status_code, status_text) | 83 status = '%d %s' % (status_code, status_text) |
84 start_response(status, [('Content-Type', 'text/plain')]) | 84 start_response(status, [('Content-Type', 'text/plain')]) |
85 | 85 |
86 if text is not None: | 86 if text is not None: |
87 return [text] | 87 return [text] |
(...skipping 21 matching lines...) Expand all Loading... |
109 except UnicodeDecodeError: | 109 except UnicodeDecodeError: |
110 return send_simple_response(start_response, 400, 'Invalid form data
encoding') | 110 return send_simple_response(start_response, 400, 'Invalid form data
encoding') |
111 | 111 |
112 return func(environ, start_response, data) | 112 return func(environ, start_response, data) |
113 | 113 |
114 return wrapper | 114 return wrapper |
115 | 115 |
116 | 116 |
117 def multiplex(environ, start_response): | 117 def multiplex(environ, start_response): |
118 try: | 118 try: |
119 path = environ["PATH_INFO"] | 119 path = environ['PATH_INFO'] |
120 try: | 120 try: |
121 handler = handlers[path] | 121 handler = handlers[path] |
122 except KeyError: | 122 except KeyError: |
123 handler = handlers[re.sub(r"[^/]+$", "", path)] | 123 handler = handlers[re.sub(r'[^/]+$', '', path)] |
124 except KeyError: | 124 except KeyError: |
125 start_response("404 Not Found", [("Content-Type", "text/plain")]) | 125 start_response('404 Not Found', [('Content-Type', 'text/plain')]) |
126 return ["Not Found"] | 126 return ['Not Found'] |
127 | 127 |
128 return handler(environ, start_response) | 128 return handler(environ, start_response) |
129 | 129 |
130 for module in set(get_config().options("multiplexer")) - set(get_config().defaul
ts()): | 130 for module in set(get_config().options('multiplexer')) - set(get_config().defaul
ts()): |
131 module_path = get_config().get("multiplexer", module) | 131 module_path = get_config().get('multiplexer', module) |
132 if module_path: | 132 if module_path: |
133 imp.load_source(module, module_path) | 133 imp.load_source(module, module_path) |
134 else: | 134 else: |
135 importlib.import_module(module) | 135 importlib.import_module(module) |
OLD | NEW |