Merge pull request #6 from channelcat/master

fix(request.py): problem in case of request without content-type head…
This commit is contained in:
Pahaz Blinov 2016-11-09 00:30:39 +05:00 committed by GitHub
commit aaee40aabd

View File

@ -8,6 +8,12 @@ from ujson import loads as json_loads
from .log import log from .log import log
DEFAULT_HTTP_CONTENT_TYPE = "application/octet-stream"
# HTTP/1.1: https://www.w3.org/Protocols/rfc2616/rfc2616-sec7.html#sec7.2.1
# > If the media type remains unknown, the recipient SHOULD treat it
# > as type "application/octet-stream"
class RequestParameters(dict): class RequestParameters(dict):
""" """
Hosts a dict with lists as values where get returns the first Hosts a dict with lists as values where get returns the first
@ -68,14 +74,13 @@ class Request:
@property @property
def form(self): def form(self):
if self.parsed_form is None: if self.parsed_form is None:
self.parsed_form = {} self.parsed_form = RequestParameters()
self.parsed_files = {} self.parsed_files = RequestParameters()
content_type, parameters = parse_header( content_type = self.headers.get(
self.headers.get('Content-Type', '')) 'Content-Type', DEFAULT_HTTP_CONTENT_TYPE)
content_type, parameters = parse_header(content_type)
try: try:
is_url_encoded = ( if content_type == 'application/x-www-form-urlencoded':
content_type == 'application/x-www-form-urlencoded')
if content_type is None or is_url_encoded:
self.parsed_form = RequestParameters( self.parsed_form = RequestParameters(
parse_qs(self.body.decode('utf-8'))) parse_qs(self.body.decode('utf-8')))
elif content_type == 'multipart/form-data': elif content_type == 'multipart/form-data':
@ -86,7 +91,6 @@ class Request:
except Exception as e: except Exception as e:
log.exception(e) log.exception(e)
pass pass
return self.parsed_form return self.parsed_form
@property @property