1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

Fixed #26014 -- Added WSGIRequest content_type and content_params attributes.

Parsed the CONTENT_TYPE header once and recorded it on the request.
This commit is contained in:
Curtis Maloney
2015-08-07 15:51:39 +10:00
committed by Tim Graham
parent dca8b916ff
commit 6f1318734f
5 changed files with 42 additions and 8 deletions

View File

@@ -98,14 +98,14 @@ class WSGIRequest(http.HttpRequest):
self.META['PATH_INFO'] = path_info
self.META['SCRIPT_NAME'] = script_name
self.method = environ['REQUEST_METHOD'].upper()
_, content_params = cgi.parse_header(environ.get('CONTENT_TYPE', ''))
if 'charset' in content_params:
self.content_type, self.content_params = cgi.parse_header(environ.get('CONTENT_TYPE', ''))
if 'charset' in self.content_params:
try:
codecs.lookup(content_params['charset'])
codecs.lookup(self.content_params['charset'])
except LookupError:
pass
else:
self.encoding = content_params['charset']
self.encoding = self.content_params['charset']
self._post_parse_error = False
try:
content_length = int(environ.get('CONTENT_LENGTH'))

View File

@@ -61,6 +61,8 @@ class HttpRequest(object):
self.method = None
self.resolver_match = None
self._post_parse_error = False
self.content_type = None
self.content_params = None
def __repr__(self):
if self.method is None or not self.get_full_path():
@@ -278,7 +280,7 @@ class HttpRequest(object):
self._mark_post_parse_error()
return
if self.META.get('CONTENT_TYPE', '').startswith('multipart/form-data'):
if self.content_type == 'multipart/form-data':
if hasattr(self, '_body'):
# Use already read data
data = BytesIO(self._body)
@@ -296,7 +298,7 @@ class HttpRequest(object):
# empty POST
self._mark_post_parse_error()
raise
elif self.META.get('CONTENT_TYPE', '').startswith('application/x-www-form-urlencoded'):
elif self.content_type == 'application/x-www-form-urlencoded':
self._post, self._files = QueryDict(self.body, encoding=self._encoding), MultiValueDict()
else:
self._post, self._files = QueryDict('', encoding=self._encoding), MultiValueDict()