Ticket #14955: urllib2_head_request_3.diff

File urllib2_head_request_3.diff, 3.3 KB (added by Claude Paroz, 14 years ago)

Put HeadRequest class at root level

  • django/core/validators.py

    commit a2f2e6e47bd4f66af1c91ff327dab61d7a20bdc4
    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..c8af901 100644
    a b  
    11import re
     2import urllib2
    23import urlparse
    34
    45from django.core.exceptions import ValidationError
    class RegexValidator(object):  
    3839        if not self.regex.search(smart_unicode(value)):
    3940            raise ValidationError(self.message, code=self.code)
    4041
     42class HeadRequest(urllib2.Request):
     43    def get_method(self):
     44        return "HEAD"
     45
    4146class URLValidator(RegexValidator):
    4247    regex = re.compile(
    4348        r'^https?://' # http:// or https://
    class URLValidator(RegexValidator):  
    7277            url = value
    7378
    7479        if self.verify_exists:
    75             import urllib2
    7680            headers = {
    7781                "Accept": "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
    7882                "Accept-Language": "en-us,en;q=0.5",
    class URLValidator(RegexValidator):  
    8084                "Connection": "close",
    8185                "User-Agent": self.user_agent,
    8286            }
     87            broken_error = ValidationError(_(u'This URL appears to be a broken link.'), code='invalid_link')
    8388            try:
    84                 req = urllib2.Request(url, None, headers)
     89                req = HeadRequest(url, None, headers)
    8590                u = urllib2.urlopen(req)
    8691            except ValueError:
    8792                raise ValidationError(_(u'Enter a valid URL.'), code='invalid')
     93            except urllib2.HTTPError, e:
     94                if e.code in (405, 501):
     95                    # Try a GET request (HEAD refused)
     96                    # See also: http://www.w3.org/Protocols/rfc2616/rfc2616.html
     97                    try:
     98                        req = urllib2.Request(url, None, headers)
     99                        u = urllib2.urlopen(req)
     100                    except:
     101                        raise broken_error
     102                else:
     103                    raise broken_error
    88104            except: # urllib2.URLError, httplib.InvalidURL, etc.
    89                 raise ValidationError(_(u'This URL appears to be a broken link.'), code='invalid_link')
     105                raise broken_error
    90106
    91107
    92108def validate_integer(value):
  • tests/regressiontests/forms/tests/fields.py

    diff --git a/tests/regressiontests/forms/tests/fields.py b/tests/regressiontests/forms/tests/fields.py
    index 576a9c3..65c1172 100644
    a b class FieldsTests(TestCase):  
    561561        self.assertEqual(u'http://www.google.com/', f.clean('http://www.google.com')) # This will fail if there's no Internet connection
    562562        self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example')
    563563        self.assertRaises(ValidationError, f.clean, 'http://www.broken.djangoproject.com') # bad domain
     564        self.assertRaises(ValidationError, f.clean, 'http://qa-dev.w3.org/link-testsuite/http.php?code=405') # Method not allowed
    564565        try:
    565566            f.clean('http://www.broken.djangoproject.com') # bad domain
    566567        except ValidationError, e:
Back to Top