Merge branch 'feature/unicode' into feature/fo
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Conflicts: AUTHORS
authored by Kenneth Reitz · committed by Kenneth Reitz · parents 882ea76, 4922daf · Browse files · 3 files·+129 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
3 files+129 added−1 removed
- other:+129 added−1 removed
Provenance
- Commit
21cd47d- Tree
9827cb7- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
882ea76- Parent 2
4922daf
3 files changed+129 added−1 removed
modified:requests/utils.py+61 added
7 unmodified lines8 that are also useful for external consumption.9 10 """11 12+import cgi13 import cookielib14+import re15 16 17 def dict_from_cookiejar(cookiejar):18 """Returns a key/value dictionary from a CookieJar."""52 unmodified lines71 # add cookie to cookiejar72 cj.set_cookie(cookie)73 74 return cj75+76+77+def get_encodings_from_content(content):78+ """Returns encodings from given content string."""79+80+ charset_re = re.compile(r'<meta.*?charset=["\']*(.+?)["\'>]', flags=re.I)81+82+ return charset_re.findall(content)83+84+85+86+def get_encoding_from_headers(headers):87+ """Returns encodings from given HTTP Header Dict."""88+89+ content_type = headers.get('content-type')90+ content_type, params = cgi.parse_header(content_type)91+92+ if 'charset' in params:93+ return params['charset'].strip("'\"")94+95+96+def get_unicode_from_response(r):97+ """Returns the requested content back in unicode.98+99+ Tried:100+ 1. charset from content-type101+ 2. every encodings from <meta ... charset=XXX>102+ 3. fall back and replace all unicode characters103+ """104+105+ tried_encodings = []106+107+ # Try charset from content-type108+ encoding = get_encoding_from_headers(r.headers)109+110+ if encoding:111+ try:112+ print '!'113+ return unicode(r.content, encoding)114+ except UnicodeError:115+ tried_encodings.append(encoding)116+117+ # Try every encodings from <meta ... charset=XXX>118+ encodings = get_encodings_from_content(r.content)119+120+ for encoding in encodings:121+ if encoding in tried_encodings:122+ continue123+ try:124+125+ return unicode(r.content, encoding)126+ except (UnicodeError, TypeError):127+ tried_encodings.append(encoding)128+129+ # Fall back:130+ try:131+ return unicode(r.content, encoding, errors='replace')132+ except TypeError:133+ return r.contentmodified:AUTHORS+2 added-1 removed
37 unmodified lines38 - Armin Ronacher39 - Shrikant Sharat Kandula40 - Mikko Ohtamaa41 - Den Shabalin42-- Alejandro Giacometti42+- Alejandro Giacometti43+- Rick Makadded: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()