Merge https://github.com/rickmak/requests into feature/unicode
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Conflicts: requests/models.py
authored by Kenneth Reitz · committed by Kenneth Reitz · parents 8bfa909, d26f333 · Browse files · 2 files·+119 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+119 added−1 removed
- other:+119 added−1 removed
Provenance
- Commit
7eb272b- Tree
167ec1a- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
8bfa909- Parent 2
d26f333
2 files changed+119 added−1 removed
modified:requests/models.py+53 added-1 removed
8 unmodified lines9 import urllib10 import urllib211 import socket12 import zlib13+import cgi14+import re15 16 from urllib2 import HTTPError17 from urlparse import urlparse, urlunparse, urljoin18 from datetime import datetime414 unmodified lines433 try:434 self._content = zlib.decompress(self._content, 16+zlib.MAX_WBITS)435 except zlib.error:436 pass435- return self._content437+ return self.unicode_content(self._content)438+439+ 440 else:441 raise AttributeError442+ 443 444+ def get_content_type(self):445+ content_type = self.headers.get("content-type")446+ content_type, params = cgi.parse_header(content_type)447+ return content_type, params448+449+ def get_encoding_from_content_type(self):450+ content_type, params = self.get_content_type()451+ if "charset" in params:452+ return params["charset"].strip("'\"")453+454+ def get_encodings_from_content(self, content):455+ if self._charset_re is None:456+ self._charset_re = re.compile(457+ r'<meta.*?charset=["\']*(.+?)["\'>]', flags=re.I458+ )459+ return self._charset_re.findall(content)460+ 461+ def unicode_content(self, content):462+ """463+ Returns the requested content back in unicode.464+ Tried:465+ 1. charset from content-type466+ 2. every encodings from <meta ... charset=XXX>467+ 3. fall back and replace all unicode characters468+ """469+ tried_encodings = []470+ # Try charset from content-type471+ encoding = self.get_encoding_from_content_type()472+ if encoding:473+ try:474+ return unicode(content, encoding)475+ except UnicodeError:476+ tried_encodings.append(encoding)477+478+ # Try every encodings from <meta ... charset=XXX>479+ encodings = self.get_encodings_from_content(content)480+ for encoding in encodings:481+ if encoding in tried_encodings:482+ continue483+ try:484+ return unicode(content, encoding)485+ except UnicodeError:486+ tried_encodings.append(encoding)487+488+ # Fall back:489+ return unicode(content, encoding, errors="replace")490+491 def raise_for_status(self):492 """Raises stored :class:`HTTPError` or :class:`URLError`, if one occured."""493 if self.error:494 raise self.error181 unmodified linesadded:test_unicode.py+66 added
1+#!/usr/bin/env python2+# -*- coding: utf-8 -*-3+4+from __future__ import with_statement5+6+import unittest7+import cookielib8+9+try:10+ import omnijson as json11+except ImportError:12+ import json13+14+import requests15+16+17+18+HTTPBIN_URL = 'http://httpbin.org/'19+HTTPSBIN_URL = 'https://httpbin.ep.io/'20+21+# HTTPBIN_URL = 'http://staging.httpbin.org/'22+# HTTPSBIN_URL = 'https://httpbin-staging.ep.io/'23+24+25+def httpbin(*suffix):26+ """Returns url for HTTPBIN resource."""27+28+ return HTTPBIN_URL + '/'.join(suffix)29+30+31+def httpsbin(*suffix):32+ """Returns url for HTTPSBIN resource."""33+34+ return HTTPSBIN_URL + '/'.join(suffix)35+36+37+SERVICES = (httpbin, httpsbin)38+39+40+41+class RequestsTestSuite(unittest.TestCase):42+ """Requests test cases."""43+44+45+ def setUp(self):46+ pass47+48+49+ def tearDown(self):50+ """Teardown."""51+ pass52+53+54+ def test_HTTP_200_OK_GET_ON_ISO88591(self):55+ r = requests.get("http://www.qypedeals.de/Verzehrgutschein+für+Jellyfish")56+ self.assertEqual(r.status_code, 200)57+ self.assertIsInstance(r.content, unicode)58+ 59+ def test_HTTP_200_OK_GET_ON_BIG5(self):60+ r = requests.get("http://google.com.hk/")61+ self.assertEqual(r.status_code, 200)62+ self.assertIsInstance(r.content, unicode)63+64+65+if __name__ == '__main__':66+ unittest.main()