Merge pull request #1575 from jparise/content-encoding
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Improved content encoding detection.
authored by Kenneth Reitz · committed by Kenneth Reitz · parents d991fb0, b9b5be7 · Browse files · 2 files·+49 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+49 added−1 removed
- other:+49 added−1 removed
Provenance
- Commit
6c73d86- Tree
f52f6e6- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
d991fb0- Parent 2
b9b5be7
2 files changed+49 added−1 removed
modified:requests/utils.py+5 added-1 removed
263 unmodified lines264 :param content: bytestring to extract encodings from.265 """266 267 charset_re = re.compile(r'<meta.*?charset=["\']*(.+?)["\'>]', flags=re.I)268+ pragma_re = re.compile(r'<meta.*?content=["\']*;?charset=(.+?)["\'>]', flags=re.I)269+ xml_re = re.compile(r'^<\?xml.*?encoding=["\']*(.+?)["\'>]')270 269- return charset_re.findall(content)271+ return (charset_re.findall(content) +272+ pragma_re.findall(content) +273+ xml_re.findall(content))274 275 276 def get_encoding_from_headers(headers):277 """Returns encodings from given HTTP Header Dict.275 unmodified linesmodified:test_requests.py+44 added
637 unmodified lines638 r = requests.Request('GET', url).prepare()639 self.assertEqual(r.url, url)640 641 642+class TestContentEncodingDetection(unittest.TestCase):643+644+ def test_none(self):645+ encodings = requests.utils.get_encodings_from_content('')646+ self.assertEqual(len(encodings), 0)647+648+ def test_html_charset(self):649+ """HTML5 meta charset attribute"""650+ content = '<meta charset="UTF-8">'651+ encodings = requests.utils.get_encodings_from_content(content)652+ self.assertEqual(len(encodings), 1)653+ self.assertEqual(encodings[0], 'UTF-8')654+655+ def test_html4_pragma(self):656+ """HTML4 pragma directive"""657+ content = '<meta http-equiv="Content-type" content="text/html;charset=UTF-8">'658+ encodings = requests.utils.get_encodings_from_content(content)659+ self.assertEqual(len(encodings), 1)660+ self.assertEqual(encodings[0], 'UTF-8')661+662+ def test_xhtml_pragma(self):663+ """XHTML 1.x served with text/html MIME type"""664+ content = '<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />'665+ encodings = requests.utils.get_encodings_from_content(content)666+ self.assertEqual(len(encodings), 1)667+ self.assertEqual(encodings[0], 'UTF-8')668+669+ def test_xml(self):670+ """XHTML 1.x served as XML"""671+ content = '<?xml version="1.0" encoding="UTF-8"?>'672+ encodings = requests.utils.get_encodings_from_content(content)673+ self.assertEqual(len(encodings), 1)674+ self.assertEqual(encodings[0], 'UTF-8')675+676+ def test_precedence(self):677+ content = '''678+ <?xml version="1.0" encoding="XML"?>679+ <meta charset="HTML5">680+ <meta http-equiv="Content-type" content="text/html;charset=HTML4" />681+ '''.strip()682+ encodings = requests.utils.get_encodings_from_content(content)683+ self.assertEqual(encodings, ['HTML5', 'HTML4', 'XML'])684+685+686 class TestCaseInsensitiveDict(unittest.TestCase):687 688 def test_mapping_init(self):689 cid = CaseInsensitiveDict({'Foo': 'foo','BAr': 'bar'})142 unmodified lines