Ticket #9857: 9857-2.patch

File 9857-2.patch, 3.0 KB (added by trbs, 16 years ago)
  • django/conf/global_settings.py

    diff -r 98f99d6291d4 django/conf/global_settings.py
    a b  
    288288# isExistingURL validator.
    289289from django import get_version
    290290URL_VALIDATOR_USER_AGENT = "Django/%s (http://www.djangoproject.com)" % get_version()
     291URL_VERIFY_EXIST_SOCKET_TIMEOUT = 10
    291292
    292293# The tablespaces to use for each model when not specified otherwise.
    293294DEFAULT_TABLESPACE = ''
  • django/forms/fields.py

    diff -r 98f99d6291d4 django/forms/fields.py
    a b  
    436436try:
    437437    from django.conf import settings
    438438    URL_VALIDATOR_USER_AGENT = settings.URL_VALIDATOR_USER_AGENT
     439    URL_VERIFY_EXIST_SOCKET_TIMEOUT = settings.URL_VERIFY_EXIST_SOCKET_TIMEOUT
    439440except ImportError:
    440441    # It's OK if Django settings aren't configured.
    441442    URL_VALIDATOR_USER_AGENT = 'Django (http://www.djangoproject.com/)'
     443    URL_VERIFY_EXIST_SOCKET_TIMEOUT = 10
    442444
    443445
    444446class FileField(Field):
     
    543545    default_error_messages = {
    544546        'invalid': _(u'Enter a valid URL.'),
    545547        'invalid_link': _(u'This URL appears to be a broken link.'),
     548        'invalid_timeout': _(u'This URL took to long to respond, appears to be broken.'),
    546549    }
    547550
    548551    def __init__(self, max_length=None, min_length=None, verify_exists=False,
     
    563566        if value == u'':
    564567            return value
    565568        if self.verify_exists:
     569            import socket
    566570            import urllib2
    567571            headers = {
    568572                "Accept": "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
     
    571575                "Connection": "close",
    572576                "User-Agent": self.user_agent,
    573577            }
     578            socket_default_timeout = socket.getdefaulttimeout()
     579            socket.setdefaulttimeout(URL_VERIFY_EXIST_SOCKET_TIMEOUT)
    574580            try:
    575                 req = urllib2.Request(value, None, headers)
    576                 u = urllib2.urlopen(req)
    577             except ValueError:
    578                 raise ValidationError(self.error_messages['invalid'])
    579             except: # urllib2.URLError, httplib.InvalidURL, etc.
    580                 raise ValidationError(self.error_messages['invalid_link'])
     581                try:
     582                    req = urllib2.Request(value, None, headers)
     583                    u = urllib2.urlopen(req)
     584                except ValueError:
     585                    raise ValidationError(self.error_messages['invalid'])
     586                except Exception, e: # urllib2.URLError, httplib.InvalidURL, etc.
     587                    if e.args and isinstance(e.args[0], socket.timeout):
     588                        raise ValidationError(self.error_messages['invalid_timeout'])
     589                    raise ValidationError(self.error_messages['invalid_link'])
     590            finally:
     591                socket.setdefaulttimeout(socket_default_timeout)
    581592        return value
    582593
    583594class BooleanField(Field):
Back to Top