Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Justin Bronn authored
12  django/contrib/gis/geoip/base.py
@@ -132,6 +132,9 @@ def _check_query(self, query, country=False, city=False, city_or_country=False):
132 132
         if not isinstance(query, basestring):
133 133
             raise TypeError('GeoIP query must be a string, not type %s' % type(query).__name__)
134 134
 
  135
+        # GeoIP only takes ASCII-encoded strings.
  136
+        query = query.encode('ascii')
  137
+
135 138
         # Extra checks for the existence of country and city databases.
136 139
         if city_or_country and not (self._country or self._city):
137 140
             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):
140 143
         elif city and not self._city:
141 144
             raise GeoIPException('Invalid GeoIP city data file: %s' % self._city_file)
142 145
 
  146
+        # Return the query string back to the caller.
  147
+        return query
  148
+
143 149
     def city(self, query):
144 150
         """
145 151
         Returns a dictionary of city information for the given IP address or
146 152
         Fully Qualified Domain Name (FQDN).  Some information in the dictionary
147 153
         may be undefined (None).
148 154
         """
149  
-        self._check_query(query, city=True)
  155
+        query = self._check_query(query, city=True)
150 156
         if ipv4_re.match(query):
151 157
             # If an IP address was passed in
152 158
             return GeoIP_record_by_addr(self._city, c_char_p(query))
@@ -156,7 +162,7 @@ def city(self, query):
156 162
 
157 163
     def country_code(self, query):
158 164
         "Returns the country code for the given IP Address or FQDN."
159  
-        self._check_query(query, city_or_country=True)
  165
+        query = self._check_query(query, city_or_country=True)
160 166
         if self._country:
161 167
             if ipv4_re.match(query):
162 168
                 return GeoIP_country_code_by_addr(self._country, query)
@@ -167,7 +173,7 @@ def country_code(self, query):
167 173
 
168 174
     def country_name(self, query):
169 175
         "Returns the country name for the given IP Address or FQDN."
170  
-        self._check_query(query, city_or_country=True)
  176
+        query = self._check_query(query, city_or_country=True)
171 177
         if self._country:
172 178
             if ipv4_re.match(query):
173 179
                 return GeoIP_country_name_by_addr(self._country, query)
8  django/contrib/gis/geoip/tests.py
@@ -95,12 +95,18 @@ def test04_city(self):
95 95
                 self.assertAlmostEqual(lon, tup[0], 4)
96 96
                 self.assertAlmostEqual(lat, tup[1], 4)
97 97
 
98  
-    def test05_unicode(self):
  98
+    def test05_unicode_response(self):
99 99
         "Testing that GeoIP strings are properly encoded, see #16553."
100 100
         g = GeoIP()
101 101
         d = g.city('62.224.93.23')
102 102
         self.assertEqual(u'Sch\xf6mberg', d['city'])
103 103
 
  104
+    def test06_unicode_query(self):
  105
+        "Testing that GeoIP accepts unicode string queries, see #17059."
  106
+        g = GeoIP()
  107
+        d = g.country(u'whitehouse.gov')
  108
+        self.assertEqual(u'US', d['country_code'])
  109
+
104 110
 
105 111
 def suite():
106 112
     s = unittest.TestSuite()

0 notes on commit 01f4ce4

Please sign in to comment.
Something went wrong with that request. Please try again.