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: Renamed files Created April 23, 2015, 2:48 p.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.example ('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,8 @@
import imp
import importlib
import re
+from urlparse import parse_qsl
+
from sitescripts.utils import get_config
handlers = {}
@@ -59,6 +61,35 @@
[("WWW-Authenticate", 'Basic realm="%s"' % realm)])
return ""
+def send_status_response(status, start_response):
+ start_response(status, [('Content-Type', 'text/plain')])
+ return [status]
+
+def form_handler(func):
+ def wrapper(environ, start_response):
+ if environ['REQUEST_METHOD'] != 'POST':
+ return send_status_response('405 Method Not Allowed', start_response)
+
+ if not environ.get('CONTENT_TYPE', '').startswith('application/x-www-form-urlencoded'):
+ return send_status_response('415 Unsupported Media Type', start_response)
+
+ try:
+ content_length = int(environ['CONTENT_LENGTH'])
+ except (KeyError, ValueError):
+ content_length = None
+ if content_length is None or content_length < 0:
+ return send_status_response('411 Length Required', start_response)
+
+ 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_status_response('400 Invalid character in form data', start_response)
+
+ return func(environ, start_response, data)
+
+ return wrapper
+
def multiplex(environ, start_response):
try:
path = environ["PATH_INFO"]
« .sitescripts.example ('K') | « sitescripts/utils.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld