commit 3268f33d6d6af9af63e81b3c04aa8192e7e42d50
Author: Claude Paroz <claude@2xlibre.net>
Date: Wed Dec 29 15:15:57 2010 +0100
Use custom urllib2 Request to use HEAD instead of GET request to validate url
diff --git a/django/core/validators.py b/django/core/validators.py
index b1b82db..d0af452 100644
a
|
b
|
class URLValidator(RegexValidator):
|
73 | 73 | |
74 | 74 | if self.verify_exists: |
75 | 75 | import urllib2 |
| 76 | class HeadRequest(urllib2.Request): |
| 77 | def get_method(self): |
| 78 | return "HEAD" |
76 | 79 | headers = { |
77 | 80 | "Accept": "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5", |
78 | 81 | "Accept-Language": "en-us,en;q=0.5", |
… |
… |
class URLValidator(RegexValidator):
|
80 | 83 | "Connection": "close", |
81 | 84 | "User-Agent": self.user_agent, |
82 | 85 | } |
| 86 | broken_error = ValidationError(_(u'This URL appears to be a broken link.'), code='invalid_link') |
83 | 87 | try: |
84 | | req = urllib2.Request(url, None, headers) |
| 88 | req = HeadRequest(url, None, headers) |
85 | 89 | u = urllib2.urlopen(req) |
86 | 90 | except ValueError: |
87 | 91 | raise ValidationError(_(u'Enter a valid URL.'), code='invalid') |
| 92 | except urllib2.HTTPError, e: |
| 93 | if e.code in (405, 501): |
| 94 | # Try a GET request (HEAD refused) |
| 95 | # See also: http://www.w3.org/Protocols/rfc2616/rfc2616.html |
| 96 | try: |
| 97 | req = urllib2.Request(url, None, headers) |
| 98 | u = urllib2.urlopen(req) |
| 99 | except: |
| 100 | raise broken_error |
| 101 | else: |
| 102 | raise broken_error |
88 | 103 | except: # urllib2.URLError, httplib.InvalidURL, etc. |
89 | | raise ValidationError(_(u'This URL appears to be a broken link.'), code='invalid_link') |
| 104 | raise broken_error |
90 | 105 | |
91 | 106 | |
92 | 107 | def validate_integer(value): |
diff --git a/tests/regressiontests/forms/tests/fields.py b/tests/regressiontests/forms/tests/fields.py
index 93ca5c1..9bf5bb0 100644
a
|
b
|
class FieldsTests(TestCase):
|
523 | 523 | self.assertEqual(u'http://www.google.com/', f.clean('http://www.google.com')) # This will fail if there's no Internet connection |
524 | 524 | self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example') |
525 | 525 | self.assertRaises(ValidationError, f.clean, 'http://www.broken.djangoproject.com') # bad domain |
| 526 | self.assertRaises(ValidationError, f.clean, 'http://qa-dev.w3.org/link-testsuite/http.php?code=405') # Method not allowed |
526 | 527 | try: |
527 | 528 | f.clean('http://www.broken.djangoproject.com') # bad domain |
528 | 529 | except ValidationError, e: |