Changeset 1936:a32f3fe4fe6d
- Timestamp:
- 02/24/10 14:01:29 (5 months ago)
- Author:
- Pablo Hoffman <pablo@…>
- Branch:
- default
- Message:
-
Fixed encoding issue (reported in #135) when the encoding declared in the HTTP header is unknown. This is the patch proposed by Rolando, with an update to the Request/Response documentation.
- Files:
-
Legend:
- Unmodified
- Added
- Removed
-
|
r1816
|
r1936
|
|
| 467 | 467 | .. attribute:: TextResponse.encoding |
| 468 | 468 | |
| 469 | | A string with the encoding of this response. The encoding is resolved in the |
| 470 | | following order: |
| | 469 | A string with the encoding of this response. The encoding is resolved by |
| | 470 | trying the following mechanisms, in order: |
| 471 | 471 | |
| 472 | 472 | 1. the encoding passed in the constructor `encoding` argument |
| 473 | 473 | |
| 474 | | 2. the encoding declared in the Content-Type HTTP header |
| | 474 | 2. the encoding declared in the Content-Type HTTP header. If this |
| | 475 | encoding is not valid (ie. unknown), it is ignored and the next |
| | 476 | resolution mechanism is tried. |
| 475 | 477 | |
| 476 | 478 | 3. the encoding declared in the response body. The TextResponse class |
-
|
r1809
|
r1936
|
|
| 6 | 6 | """ |
| 7 | 7 | |
| | 8 | import codecs |
| 8 | 9 | import re |
| 9 | 10 | |
| … |
… |
|
| 65 | 66 | encoding = self._ENCODING_RE.search(content_type) |
| 66 | 67 | if encoding: |
| 67 | | return encoding.group(1) |
| | 68 | enc = encoding.group(1) |
| | 69 | try: |
| | 70 | codecs.lookup(enc) # check if the encoding is valid |
| | 71 | return enc |
| | 72 | except LookupError: |
| | 73 | pass |
| 68 | 74 | |
| 69 | 75 | @memoizemethod_noargs |
-
|
r1809
|
r1936
|
|
| 176 | 176 | r3 = self.response_class("http://www.example.com", headers={"Content-type": ["text/html; charset=iso-8859-1"]}, body="\xa3") |
| 177 | 177 | r4 = self.response_class("http://www.example.com", body="\xa2\xa3") |
| | 178 | r5 = self.response_class("http://www.example.com", |
| | 179 | headers={"Content-type": ["text/html; charset=None"]}, body="\xc2\xa3") |
| 178 | 180 | |
| 179 | 181 | self.assertEqual(r1.headers_encoding(), "utf-8") |
| … |
… |
|
| 183 | 185 | self.assertEqual(r3.encoding, 'iso-8859-1') |
| 184 | 186 | self.assertEqual(r4.headers_encoding(), None) |
| | 187 | self.assertEqual(r5.headers_encoding(), None) |
| | 188 | self.assertEqual(r5.encoding, "utf-8") |
| 185 | 189 | assert r4.body_encoding() is not None and r4.body_encoding() != 'ascii' |
| 186 | 190 | self._assert_response_values(r1, 'utf-8', u"\xa3") |