Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #17059 -- Encode `GeoIP` query strings properly so `libGeoIP` r…

…eturns results for unicode strings.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17019 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 01f4ce4c4993ecf0f5f3095e97ce92935337de61 1 parent b3b9049
@jbronn jbronn authored
View
12 django/contrib/gis/geoip/base.py
@@ -132,6 +132,9 @@ def _check_query(self, query, country=False, city=False, city_or_country=False):
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 @@ def _check_query(self, query, country=False, city=False, city_or_country=False):
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 @@ def city(self, query):
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 @@ def country_code(self, query):
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)
View
8 django/contrib/gis/geoip/tests.py
@@ -95,12 +95,18 @@ def test04_city(self):
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()
Please sign in to comment.
Something went wrong with that request. Please try again.