Ticket #9857: 9857-2-alternative-using-2.6-timeout.patch

File 9857-2-alternative-using-2.6-timeout.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
     571            import sys
    567572            headers = {
    568573                "Accept": "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
    569574                "Accept-Language": "en-us,en;q=0.5",
     
    571576                "Connection": "close",
    572577                "User-Agent": self.user_agent,
    573578            }
     579            if sys.version_info[:2]<(2,6):
     580                def urlopen(req, timeout=None):
     581                    socket_default_timeout = socket.getdefaulttimeout()
     582                    socket.setdefaulttimeout(timeout)
     583                    try:
     584                        return urllib2.urlopen(req)
     585                    finally:
     586                        socket.setdefaulttimeout(socket_default_timeout)
     587            else:
     588                urlopen = urllib2.urlopen
     589
    574590            try:
    575591                req = urllib2.Request(value, None, headers)
    576                 u = urllib2.urlopen(req)
     592                u = urlopen(req, timeout=URL_VERIFY_EXIST_SOCKET_TIMEOUT)
    577593            except ValueError:
    578594                raise ValidationError(self.error_messages['invalid'])
    579             except: # urllib2.URLError, httplib.InvalidURL, etc.
     595            except Exception, e: # urllib2.URLError, httplib.InvalidURL, etc.
     596                if e.args and isinstance(e.args[0], socket.timeout):
     597                    raise ValidationError(self.error_messages['invalid_timeout'])
    580598                raise ValidationError(self.error_messages['invalid_link'])
    581599        return value
    582600
Back to Top