diff --git a/django/core/handlers/wsgi.py b/django/core/handlers/wsgi.py
index 45cb226..2b99b38 100644
a
|
b
|
|
1 | 1 | from __future__ import unicode_literals |
2 | 2 | |
| 3 | import codecs |
3 | 4 | import logging |
4 | 5 | import sys |
5 | 6 | from io import BytesIO |
… |
… |
class WSGIRequest(http.HttpRequest):
|
144 | 145 | self.META['PATH_INFO'] = path_info |
145 | 146 | self.META['SCRIPT_NAME'] = script_name |
146 | 147 | self.method = environ['REQUEST_METHOD'].upper() |
| 148 | _, content_params = self._parse_content_type(self.META.get('CONTENT_TYPE', '')) |
| 149 | if 'charset' in content_params: |
| 150 | try: |
| 151 | codecs.lookup(content_params['charset']) |
| 152 | except LookupError: |
| 153 | pass |
| 154 | else: |
| 155 | self.encoding = content_params['charset'] |
147 | 156 | self._post_parse_error = False |
148 | 157 | try: |
149 | 158 | content_length = int(self.environ.get('CONTENT_LENGTH')) |
… |
… |
class WSGIRequest(http.HttpRequest):
|
155 | 164 | def _is_secure(self): |
156 | 165 | return 'wsgi.url_scheme' in self.environ and self.environ['wsgi.url_scheme'] == 'https' |
157 | 166 | |
| 167 | def _parse_content_type(self, ctype): |
| 168 | """ |
| 169 | Example input: "text/plain; charset=iso-8859-1" |
| 170 | Return: ('text/plain', {'charset': 'iso-8859-1'}) |
| 171 | """ |
| 172 | content_type, _, params = ctype.partition(';') |
| 173 | content_params = {} |
| 174 | for parameter in params.split(';'): |
| 175 | k, _, v = parameter.strip().partition('=') |
| 176 | content_params[k] = v |
| 177 | return content_type, content_params |
| 178 | |
158 | 179 | def _get_request(self): |
159 | 180 | if not hasattr(self, '_request'): |
160 | 181 | self._request = datastructures.MergeDict(self.POST, self.GET) |
diff --git a/tests/regressiontests/requests/tests.py b/tests/regressiontests/requests/tests.py
index 6522620..eaf25ea 100644
a
|
b
|
|
| 1 | # -*- encoding: utf-8 -*- |
1 | 2 | from __future__ import unicode_literals |
2 | 3 | |
3 | 4 | import time |
… |
… |
class RequestsTests(unittest.TestCase):
|
352 | 353 | self.assertRaises(Exception, lambda: request.body) |
353 | 354 | self.assertEqual(request.POST, {}) |
354 | 355 | |
| 356 | def test_alternate_charset_POST(self): |
| 357 | """ |
| 358 | Test a POST with non-utf-8 payload encoding. |
| 359 | """ |
| 360 | from django.utils.http import urllib_parse |
| 361 | payload = FakePayload(urllib_parse.urlencode({'key': 'España'.encode('latin-1')})) |
| 362 | request = WSGIRequest({ |
| 363 | 'REQUEST_METHOD': 'POST', |
| 364 | 'CONTENT_LENGTH': len(payload), |
| 365 | 'CONTENT_TYPE': 'application/x-www-form-urlencoded; charset=iso-8859-1', |
| 366 | 'wsgi.input': payload, |
| 367 | }) |
| 368 | self.assertEqual(request.POST, {'key': ['España']}) |
| 369 | |
355 | 370 | def test_body_after_POST_multipart(self): |
356 | 371 | """ |
357 | 372 | Reading body after parsing multipart is not allowed |