Skip to content
Myr

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

3 files+129 added−1 removed

modified:requests/utils.py+61 added
7 unmodified lines
8that are also useful for external consumption.910"""1112import cgi13import cookielib14import re151617def dict_from_cookiejar(cookiejar):18 """Returns a key/value dictionary from a CookieJar."""
52 unmodified lines
71 # add cookie to cookiejar72 cj.set_cookie(cookie)7374 return cj757677def get_encodings_from_content(content):78 """Returns encodings from given content string."""7980 charset_re = re.compile(r'<meta.*?charset=["\']*(.+?)["\'>]', flags=re.I)8182 return charset_re.findall(content)83848586def get_encoding_from_headers(headers):87 """Returns encodings from given HTTP Header Dict."""8889 content_type = headers.get('content-type')90 content_type, params = cgi.parse_header(content_type)9192 if 'charset' in params:93 return params['charset'].strip("'\"")949596def get_unicode_from_response(r):97 """Returns the requested content back in unicode.9899 Tried:100 1. charset from content-type101 2. every encodings from <meta ... charset=XXX>102 3. fall back and replace all unicode characters103 """104105 tried_encodings = []106107 # Try charset from content-type108 encoding = get_encoding_from_headers(r.headers)109110 if encoding:111 try:112 print '!'113 return unicode(r.content, encoding)114 except UnicodeError:115 tried_encodings.append(encoding)116117 # Try every encodings from <meta ... charset=XXX>118 encodings = get_encodings_from_content(r.content)119120 for encoding in encodings:121 if encoding in tried_encodings:122 continue123 try:124125 return unicode(r.content, encoding)126 except (UnicodeError, TypeError):127 tried_encodings.append(encoding)128129 # Fall back:130 try:131 return unicode(r.content, encoding, errors='replace')132 except TypeError:133 return r.content
modified:AUTHORS+2 added-1 removed
37 unmodified lines
38- Armin Ronacher39- Shrikant Sharat Kandula40- Mikko Ohtamaa41- Den Shabalin42- Alejandro Giacometti42- Alejandro Giacometti43- Rick Mak
added:test_unicode.py+66 added
1#!/usr/bin/env python2# -*- coding: utf-8 -*-34from __future__ import with_statement56import unittest7import cookielib89try:10 import omnijson as json11except ImportError:12 import json1314import requests15161718HTTPBIN_URL = 'http://httpbin.org/'19HTTPSBIN_URL = 'https://httpbin.ep.io/'2021# HTTPBIN_URL = 'http://staging.httpbin.org/'22# HTTPSBIN_URL = 'https://httpbin-staging.ep.io/'232425def httpbin(*suffix):26 """Returns url for HTTPBIN resource."""2728 return HTTPBIN_URL + '/'.join(suffix)293031def httpsbin(*suffix):32 """Returns url for HTTPSBIN resource."""3334 return HTTPSBIN_URL + '/'.join(suffix)353637SERVICES = (httpbin, httpsbin)38394041class RequestsTestSuite(unittest.TestCase):42 """Requests test cases."""434445 def setUp(self):46 pass474849 def tearDown(self):50 """Teardown."""51 pass525354 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)636465if __name__ == '__main__':66 unittest.main()