Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: sitescripts/web.py

Issue 5177883412660224: Issue 2234 - Add a WSGI controller to collect email addresses for the Adblock Browser iOS launch (Closed)
Patch Set: Send error response when verification request is invalid Created April 24, 2015, 12:04 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sitescripts/utils.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 from urlparse import parse_qsl
23
22 from sitescripts.utils import get_config 24 from sitescripts.utils import get_config
23 25
24 handlers = {} 26 handlers = {}
25 authenticated_users = {} 27 authenticated_users = {}
26 28
27 def url_handler(url): 29 def url_handler(url):
28 def decorator(func): 30 def decorator(func):
29 registerUrlHandler(url, func) 31 registerUrlHandler(url, func)
30 return func 32 return func
31 return decorator 33 return decorator
(...skipping 20 matching lines...) Expand all
52 expected_username = config.get(config_section, "basic_auth_username") 54 expected_username = config.get(config_section, "basic_auth_username")
53 expected_password = config.get(config_section, "basic_auth_password") 55 expected_password = config.get(config_section, "basic_auth_password")
54 if username == expected_username and password == expected_password: 56 if username == expected_username and password == expected_password:
55 return f(environ, start_response) 57 return f(environ, start_response)
56 58
57 realm = get_config().get("DEFAULT", "basic_auth_realm") 59 realm = get_config().get("DEFAULT", "basic_auth_realm")
58 start_response("401 UNAUTHORIZED", 60 start_response("401 UNAUTHORIZED",
59 [("WWW-Authenticate", 'Basic realm="%s"' % realm)]) 61 [("WWW-Authenticate", 'Basic realm="%s"' % realm)])
60 return "" 62 return ""
61 63
64 def send_status_response(status, start_response):
65 start_response(status, [('Content-Type', 'text/plain')])
66 return [status]
67
68 def form_handler(func):
69 def wrapper(environ, start_response):
70 if environ['REQUEST_METHOD'] != 'POST':
71 return send_status_response('405 Method Not Allowed', start_response)
72
73 if not environ.get('CONTENT_TYPE', '').startswith('application/x-www-form-ur lencoded'):
74 return send_status_response('415 Unsupported Media Type', start_response)
75
76 try:
77 content_length = int(environ['CONTENT_LENGTH'])
78 except (KeyError, ValueError):
79 content_length = None
80 if content_length is None or content_length < 0:
81 return send_status_response('411 Length Required', start_response)
82
83 raw_data = parse_qsl(environ['wsgi.input'].read(content_length))
84 try:
85 data = {k.decode('utf-8'): v.decode('utf-8') for k, v in raw_data}
86 except UnicodeDecodeError:
87 return send_status_response('400 Invalid character in form data', start_re sponse)
88
89 return func(environ, start_response, data)
90
91 return wrapper
92
62 def multiplex(environ, start_response): 93 def multiplex(environ, start_response):
63 try: 94 try:
64 path = environ["PATH_INFO"] 95 path = environ["PATH_INFO"]
65 try: 96 try:
66 handler = handlers[path] 97 handler = handlers[path]
67 except KeyError: 98 except KeyError:
68 handler = handlers[re.sub(r"[^/]+$", "", path)] 99 handler = handlers[re.sub(r"[^/]+$", "", path)]
69 except KeyError: 100 except KeyError:
70 start_response("404 Not Found", [("Content-Type", "text/plain")]) 101 start_response("404 Not Found", [("Content-Type", "text/plain")])
71 return ["Not Found"] 102 return ["Not Found"]
72 103
73 return handler(environ, start_response) 104 return handler(environ, start_response)
74 105
75 for module in set(get_config().options("multiplexer")) - set(get_config().defaul ts()): 106 for module in set(get_config().options("multiplexer")) - set(get_config().defaul ts()):
76 module_path = get_config().get("multiplexer", module) 107 module_path = get_config().get("multiplexer", module)
77 if module_path: 108 if module_path:
78 imp.load_source(module, module_path) 109 imp.load_source(module, module_path)
79 else: 110 else:
80 importlib.import_module(module) 111 importlib.import_module(module)
OLDNEW
« no previous file with comments | « sitescripts/utils.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld