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

Unified 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: URL-encode language before inserting into URL Created April 28, 2015, 10:50 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« sitescripts/submit_email/web/submit_email.py ('K') | « sitescripts/utils.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sitescripts/web.py
===================================================================
--- a/sitescripts/web.py
+++ b/sitescripts/web.py
@@ -19,6 +19,9 @@
import imp
import importlib
import re
+import httplib
+from urlparse import parse_qsl
+
from sitescripts.utils import get_config
handlers = {}
@@ -59,6 +62,41 @@
[("WWW-Authenticate", 'Basic realm="%s"' % realm)])
return ""
+def send_simple_response(start_response, status_code, text=None):
+ status_text = httplib.responses[status_code]
+
+ status = '%d %s' % (status_code, status_text)
+ start_response(status, [('Content-Type', 'text/plain')])
+
+ if text is not None:
+ return [text]
+ return [status_text]
+
+def form_handler(func):
+ def wrapper(environ, start_response):
+ if environ['REQUEST_METHOD'] != 'POST':
+ return send_simple_response(start_response, 405)
+
+ if not environ.get('CONTENT_TYPE', '').startswith('application/x-www-form-urlencoded'):
+ return send_simple_response(start_response, 415)
+
+ try:
+ content_length = int(environ['CONTENT_LENGTH'])
+ except (KeyError, ValueError):
+ content_length = None
+ if content_length is None or content_length < 0:
+ return send_simple_response(start_response, 411)
+
+ raw_data = parse_qsl(environ['wsgi.input'].read(content_length))
+ try:
+ data = {k.decode('utf-8'): v.decode('utf-8') for k, v in raw_data}
+ except UnicodeDecodeError:
+ return send_simple_response(start_response, 400, 'Invalid form data encoding')
+
+ return func(environ, start_response, data)
+
+ return wrapper
+
def multiplex(environ, start_response):
try:
path = environ["PATH_INFO"]
« sitescripts/submit_email/web/submit_email.py ('K') | « sitescripts/utils.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld