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

Delta Between Two Patch Sets: sitescripts/web.py

Issue 5177883412660224: Issue 2234 - Add a WSGI controller to collect email addresses for the Adblock Browser iOS launch (Closed)
Left Patch Set: Use codecs.open() Created April 5, 2015, 11:53 a.m.
Right Patch Set: URL-encode language before inserting into URL Created April 28, 2015, 10:50 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« sitescripts/submit_email/web/submit_email.py ('K') | « sitescripts/utils.py ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
22 from urlparse import parse_qsl 23 from urlparse import parse_qsl
23 24
24 from sitescripts.utils import get_config 25 from sitescripts.utils import get_config
25 26
26 handlers = {} 27 handlers = {}
27 authenticated_users = {} 28 authenticated_users = {}
28 29
29 def url_handler(url): 30 def url_handler(url):
30 def decorator(func): 31 def decorator(func):
31 registerUrlHandler(url, func) 32 registerUrlHandler(url, func)
(...skipping 21 matching lines...) Expand all
53 config = get_config() 54 config = get_config()
54 expected_username = config.get(config_section, "basic_auth_username") 55 expected_username = config.get(config_section, "basic_auth_username")
55 expected_password = config.get(config_section, "basic_auth_password") 56 expected_password = config.get(config_section, "basic_auth_password")
56 if username == expected_username and password == expected_password: 57 if username == expected_username and password == expected_password:
57 return f(environ, start_response) 58 return f(environ, start_response)
58 59
59 realm = get_config().get("DEFAULT", "basic_auth_realm") 60 realm = get_config().get("DEFAULT", "basic_auth_realm")
60 start_response("401 UNAUTHORIZED", 61 start_response("401 UNAUTHORIZED",
61 [("WWW-Authenticate", 'Basic realm="%s"' % realm)]) 62 [("WWW-Authenticate", 'Basic realm="%s"' % realm)])
62 return "" 63 return ""
63 64
kzar 2015/04/05 15:45:14 These additions are cool, perhaps we should even o
Sebastian Noack 2015/04/05 17:07:01 Yes, feel free to do so. ;) Note that there is al
64 def send_status_response(status, start_response): 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)
65 start_response(status, [('Content-Type', 'text/plain')]) 69 start_response(status, [('Content-Type', 'text/plain')])
66 return [status] 70
71 if text is not None:
72 return [text]
73 return [status_text]
67 74
68 def form_handler(func): 75 def form_handler(func):
69 def wrapper(environ, start_response): 76 def wrapper(environ, start_response):
70 if environ['REQUEST_METHOD'] != 'POST': 77 if environ['REQUEST_METHOD'] != 'POST':
71 return send_status_response('405 Method Not Allowed', start_response) 78 return send_simple_response(start_response, 405)
72 79
73 if not environ.get('CONTENT_TYPE', '').startswith('application/x-www-form-ur lencoded'): 80 if not environ.get('CONTENT_TYPE', '').startswith('application/x-www-form-ur lencoded'):
74 return send_status_response('415 Unsupported Media Type', start_response) 81 return send_simple_response(start_response, 415)
75 82
76 try: 83 try:
77 content_length = int(environ['CONTENT_LENGTH']) 84 content_length = int(environ['CONTENT_LENGTH'])
78 except (KeyError, ValueError): 85 except (KeyError, ValueError):
79 content_length = None 86 content_length = None
80 if content_length is None or content_length < 0: 87 if content_length is None or content_length < 0:
81 return send_status_response('411 Length Required', start_response) 88 return send_simple_response(start_response, 411)
82 89
83 raw_data = parse_qsl(environ['wsgi.input'].read(content_length)) 90 raw_data = parse_qsl(environ['wsgi.input'].read(content_length))
84 try: 91 try:
85 data = {k.decode('utf-8'): v.decode('utf-8') for k, v in raw_data} 92 data = {k.decode('utf-8'): v.decode('utf-8') for k, v in raw_data}
86 except UnicodeDecodeError: 93 except UnicodeDecodeError:
87 return send_status_response('400 Invalid character in form data', start_re sponse) 94 return send_simple_response(start_response, 400, 'Invalid form data encodi ng')
88 95
89 return func(environ, start_response, data) 96 return func(environ, start_response, data)
90 97
91 return wrapper 98 return wrapper
92 99
93 def multiplex(environ, start_response): 100 def multiplex(environ, start_response):
94 try: 101 try:
95 path = environ["PATH_INFO"] 102 path = environ["PATH_INFO"]
96 try: 103 try:
97 handler = handlers[path] 104 handler = handlers[path]
98 except KeyError: 105 except KeyError:
99 handler = handlers[re.sub(r"[^/]+$", "", path)] 106 handler = handlers[re.sub(r"[^/]+$", "", path)]
100 except KeyError: 107 except KeyError:
101 start_response("404 Not Found", [("Content-Type", "text/plain")]) 108 start_response("404 Not Found", [("Content-Type", "text/plain")])
102 return ["Not Found"] 109 return ["Not Found"]
103 110
104 return handler(environ, start_response) 111 return handler(environ, start_response)
105 112
106 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()):
107 module_path = get_config().get("multiplexer", module) 114 module_path = get_config().get("multiplexer", module)
108 if module_path: 115 if module_path:
109 imp.load_source(module, module_path) 116 imp.load_source(module, module_path)
110 else: 117 else:
111 importlib.import_module(module) 118 importlib.import_module(module)
LEFTRIGHT

Powered by Google App Engine
This is Rietveld