Skip to content
Myr

Improved content encoding detection.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

get_encodings_from_content() can now detect HTML in-document content encoding declarations in the following formats: - HTML5 - HTML4 - XHTML 1.x served with text/html MIME type - XHTML 1.x served as XML

authored by Jon Parise · committed by Jon Parise · parent d991fb0 · 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.

Trailers

  • Ref: http://www.w3.org/International/questions/qa-html-encoding-declarations

Diff stats

2 files+49 added−1 removed

  • other:+49 added−1 removed

2 files+49 added−1 removed

modified:requests/utils.py+5 added-1 removed
263 unmodified lines
264 :param content: bytestring to extract encodings from.265 """266267 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=["\']*(.+?)["\'>]')270269 return charset_re.findall(content)271 return (charset_re.findall(content) +272 pragma_re.findall(content) +273 xml_re.findall(content))274275276def get_encoding_from_headers(headers):277 """Returns encodings from given HTTP Header Dict.
275 unmodified lines
modified:test_requests.py+44 added
637 unmodified lines
638 r = requests.Request('GET', url).prepare()639 self.assertEqual(r.url, url)640641642class TestContentEncodingDetection(unittest.TestCase):643644 def test_none(self):645 encodings = requests.utils.get_encodings_from_content('')646 self.assertEqual(len(encodings), 0)647648 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')654655 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')661662 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')668669 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')675676 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'])684685686class TestCaseInsensitiveDict(unittest.TestCase):687688 def test_mapping_init(self):689 cid = CaseInsensitiveDict({'Foo': 'foo','BAr': 'bar'})
142 unmodified lines