Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #2934: greatly improved the accuracy if the isExistingURL check…

…. Also introduced a new setting, URL_VALIDATOR_USER_AGENT, which is the User-Agent that the validator will use to check for URL existance. Thanks, Jeremy.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4035 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 4d59730fad1fe882ad957b7ad463398329d2c8ea 1 parent 212cb21
Jacob Kaplan-Moss authored November 07, 2006
4  django/conf/global_settings.py
@@ -228,6 +228,10 @@
228 228
 # Hint: you really don't!
229 229
 TRANSACTIONS_MANAGED = False
230 230
 
  231
+# The User-Agent string to use when checking for URL validity through the
  232
+# isExistingURL validator.
  233
+URL_VALIDATOR_USER_AGENT = "Django/0.96pre (http://www.djangoproject.com)"
  234
+
231 235
 ##############
232 236
 # MIDDLEWARE #
233 237
 ##############
23  django/core/validators.py
@@ -8,6 +8,7 @@
8 8
 form field is required.
9 9
 """
10 10
 
  11
+import urllib2
11 12
 from django.conf import settings
12 13
 from django.utils.translation import gettext, gettext_lazy, ngettext
13 14
 from django.utils.functional import Promise, lazy
@@ -223,18 +224,26 @@ def isWellFormedXmlFragment(field_data, all_data):
223 224
     isWellFormedXml('<root>%s</root>' % field_data, all_data)
224 225
 
225 226
 def isExistingURL(field_data, all_data):
226  
-    import urllib2
227 227
     try:
228  
-        u = urllib2.urlopen(field_data)
  228
+        headers = {
  229
+            "Accept" : "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
  230
+            "Accept-Language" : "en-us,en;q=0.5",
  231
+            "Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
  232
+            "Connection" : "close",
  233
+            "User-Agent": settings.URL_VALIDATOR_USER_AGENT
  234
+            }
  235
+        req = urllib2.Request(field_data,None, headers)
  236
+        u = urllib2.urlopen(req)
229 237
     except ValueError:
230  
-        raise ValidationError, gettext("Invalid URL: %s") % field_data
  238
+        raise ValidationError, _("Invalid URL: %s") % field_data
231 239
     except urllib2.HTTPError, e:
232 240
         # 401s are valid; they just mean authorization is required.
233  
-        if e.code not in ('401',):
234  
-            raise ValidationError, gettext("The URL %s is a broken link.") % field_data
  241
+        # 301 and 302 are redirects; they just mean look somewhere else.
  242
+        if str(e.code) not in ('401','301','302'):
  243
+            raise ValidationError, _("The URL %s is a broken link.") % field_data
235 244
     except: # urllib2.URLError, httplib.InvalidURL, etc.
236  
-        raise ValidationError, gettext("The URL %s is a broken link.") % field_data
237  
-
  245
+        raise ValidationError, _("The URL %s is a broken link.") % field_data
  246
+        
238 247
 def isValidUSState(field_data, all_data):
239 248
     "Checks that the given string is a valid two-letter U.S. state abbreviation"
240 249
     states = ['AA', 'AE', 'AK', 'AL', 'AP', 'AR', 'AS', 'AZ', 'CA', 'CO', 'CT', 'DC', 'DE', 'FL', 'FM', 'GA', 'GU', 'HI', 'IA', 'ID', 'IL', 'IN', 'KS', 'KY', 'LA', 'MA', 'MD', 'ME', 'MH', 'MI', 'MN', 'MO', 'MP', 'MS', 'MT', 'NC', 'ND', 'NE', 'NH', 'NJ', 'NM', 'NV', 'NY', 'OH', 'OK', 'OR', 'PA', 'PR', 'PW', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VA', 'VI', 'VT', 'WA', 'WI', 'WV', 'WY']
10  docs/settings.txt
@@ -814,6 +814,16 @@ manual configuration option (see below), Django will *not* touch the ``TZ``
814 814
 environment variable, and it'll be up to you to ensure your processes are
815 815
 running in the correct environment.
816 816
 
  817
+URL_VALIDATOR_USER_AGENT
  818
+------------------------
  819
+
  820
+Default: ``Django/<version> (http://www.djangoproject.com/)``
  821
+
  822
+The string to use as the ``User-Agent`` header when checking to see if URLs
  823
+exist (see the ``verify_exists`` option on URLField_).
  824
+
  825
+.. URLField: ../model_api/#urlfield
  826
+
817 827
 USE_ETAGS
818 828
 ---------
819 829
 

0 notes on commit 4d59730

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