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 "" |
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
|
+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"] |