Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #14955 -- Made the URLValidator use a HEAD request when verifyi…

…ng a URL. Thanks, Claude Paroz.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15500 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit e258d9a10b92ef581556d9f265d7798d28b3330d 1 parent 492b8a0
@jezdez jezdez authored
View
23 django/core/validators.py
@@ -1,4 +1,5 @@
import re
+import urllib2
import urlparse
from django.core.exceptions import ValidationError
@@ -38,6 +39,10 @@ def __call__(self, value):
if not self.regex.search(smart_unicode(value)):
raise ValidationError(self.message, code=self.code)
+class HeadRequest(urllib2.Request):
+ def get_method(self):
+ return "HEAD"
+
class URLValidator(RegexValidator):
regex = re.compile(
r'^https?://' # http:// or https://
@@ -72,7 +77,6 @@ def __call__(self, value):
url = value
if self.verify_exists:
- import urllib2
headers = {
"Accept": "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
"Accept-Language": "en-us,en;q=0.5",
@@ -80,13 +84,26 @@ def __call__(self, value):
"Connection": "close",
"User-Agent": self.user_agent,
}
+ broken_error = ValidationError(
+ _(u'This URL appears to be a broken link.'), code='invalid_link')
try:
- req = urllib2.Request(url, None, headers)
+ req = HeadRequest(url, None, headers)
u = urllib2.urlopen(req)
except ValueError:
raise ValidationError(_(u'Enter a valid URL.'), code='invalid')
+ except urllib2.HTTPError, e:
+ if e.code in (405, 501):
+ # Try a GET request (HEAD refused)
+ # See also: http://www.w3.org/Protocols/rfc2616/rfc2616.html
+ try:
+ req = urllib2.Request(url, None, headers)
+ u = urllib2.urlopen(req)
+ except:
+ raise broken_error
+ else:
+ raise broken_error
except: # urllib2.URLError, httplib.InvalidURL, etc.
- raise ValidationError(_(u'This URL appears to be a broken link.'), code='invalid_link')
+ raise broken_error
def validate_integer(value):
View
1  tests/regressiontests/forms/tests/fields.py
@@ -561,6 +561,7 @@ def test_urlfield_3(self):
self.assertEqual(u'http://www.google.com/', f.clean('http://www.google.com')) # This will fail if there's no Internet connection
self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example')
self.assertRaises(ValidationError, f.clean, 'http://www.broken.djangoproject.com') # bad domain
+ self.assertRaises(ValidationError, f.clean, 'http://qa-dev.w3.org/link-testsuite/http.php?code=405') # Method not allowed
try:
f.clean('http://www.broken.djangoproject.com') # bad domain
except ValidationError, e:
Please sign in to comment.
Something went wrong with that request. Please try again.