From 01f4ce4c4993ecf0f5f3095e97ce92935337de61 Mon Sep 17 00:00:00 2001 From: Justin Bronn Date: Thu, 20 Oct 2011 20:08:33 +0000 Subject: [PATCH] Fixed #17059 -- Encode `GeoIP` query strings properly so `libGeoIP` returns results for unicode strings. git-svn-id: http://code.djangoproject.com/svn/django/trunk@17019 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/gis/geoip/base.py | 12 +++++++++--- django/contrib/gis/geoip/tests.py | 8 +++++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/django/contrib/gis/geoip/base.py b/django/contrib/gis/geoip/base.py index d12f58266b..e00e0a4d93 100644 --- a/django/contrib/gis/geoip/base.py +++ b/django/contrib/gis/geoip/base.py @@ -132,6 +132,9 @@ class GeoIP(object): if not isinstance(query, basestring): raise TypeError('GeoIP query must be a string, not type %s' % type(query).__name__) + # GeoIP only takes ASCII-encoded strings. + query = query.encode('ascii') + # Extra checks for the existence of country and city databases. if city_or_country and not (self._country or self._city): raise GeoIPException('Invalid GeoIP country and city data files.') @@ -140,13 +143,16 @@ class GeoIP(object): elif city and not self._city: raise GeoIPException('Invalid GeoIP city data file: %s' % self._city_file) + # Return the query string back to the caller. + return query + def city(self, query): """ Returns a dictionary of city information for the given IP address or Fully Qualified Domain Name (FQDN). Some information in the dictionary may be undefined (None). """ - self._check_query(query, city=True) + query = self._check_query(query, city=True) if ipv4_re.match(query): # If an IP address was passed in return GeoIP_record_by_addr(self._city, c_char_p(query)) @@ -156,7 +162,7 @@ class GeoIP(object): def country_code(self, query): "Returns the country code for the given IP Address or FQDN." - self._check_query(query, city_or_country=True) + query = self._check_query(query, city_or_country=True) if self._country: if ipv4_re.match(query): return GeoIP_country_code_by_addr(self._country, query) @@ -167,7 +173,7 @@ class GeoIP(object): def country_name(self, query): "Returns the country name for the given IP Address or FQDN." - self._check_query(query, city_or_country=True) + query = self._check_query(query, city_or_country=True) if self._country: if ipv4_re.match(query): return GeoIP_country_name_by_addr(self._country, query) diff --git a/django/contrib/gis/geoip/tests.py b/django/contrib/gis/geoip/tests.py index ba3cd8025f..6e1f1579a7 100644 --- a/django/contrib/gis/geoip/tests.py +++ b/django/contrib/gis/geoip/tests.py @@ -95,12 +95,18 @@ class GeoIPTest(unittest.TestCase): self.assertAlmostEqual(lon, tup[0], 4) self.assertAlmostEqual(lat, tup[1], 4) - def test05_unicode(self): + def test05_unicode_response(self): "Testing that GeoIP strings are properly encoded, see #16553." g = GeoIP() d = g.city('62.224.93.23') self.assertEqual(u'Sch\xf6mberg', d['city']) + def test06_unicode_query(self): + "Testing that GeoIP accepts unicode string queries, see #17059." + g = GeoIP() + d = g.country(u'whitehouse.gov') + self.assertEqual(u'US', d['country_code']) + def suite(): s = unittest.TestSuite()