diff -r 98f99d6291d4 django/conf/global_settings.py
a
|
b
|
|
288 | 288 | # isExistingURL validator. |
289 | 289 | from django import get_version |
290 | 290 | URL_VALIDATOR_USER_AGENT = "Django/%s (http://www.djangoproject.com)" % get_version() |
| 291 | URL_VERIFY_EXIST_SOCKET_TIMEOUT = 10 |
291 | 292 | |
292 | 293 | # The tablespaces to use for each model when not specified otherwise. |
293 | 294 | DEFAULT_TABLESPACE = '' |
diff -r 98f99d6291d4 django/forms/fields.py
a
|
b
|
|
436 | 436 | try: |
437 | 437 | from django.conf import settings |
438 | 438 | URL_VALIDATOR_USER_AGENT = settings.URL_VALIDATOR_USER_AGENT |
| 439 | URL_VERIFY_EXIST_SOCKET_TIMEOUT = settings.URL_VERIFY_EXIST_SOCKET_TIMEOUT |
439 | 440 | except ImportError: |
440 | 441 | # It's OK if Django settings aren't configured. |
441 | 442 | URL_VALIDATOR_USER_AGENT = 'Django (http://www.djangoproject.com/)' |
| 443 | URL_VERIFY_EXIST_SOCKET_TIMEOUT = 10 |
442 | 444 | |
443 | 445 | |
444 | 446 | class FileField(Field): |
… |
… |
|
543 | 545 | default_error_messages = { |
544 | 546 | 'invalid': _(u'Enter a valid URL.'), |
545 | 547 | '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.'), |
546 | 549 | } |
547 | 550 | |
548 | 551 | def __init__(self, max_length=None, min_length=None, verify_exists=False, |
… |
… |
|
563 | 566 | if value == u'': |
564 | 567 | return value |
565 | 568 | if self.verify_exists: |
| 569 | import socket |
566 | 570 | import urllib2 |
567 | 571 | headers = { |
568 | 572 | "Accept": "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5", |
… |
… |
|
571 | 575 | "Connection": "close", |
572 | 576 | "User-Agent": self.user_agent, |
573 | 577 | } |
| 578 | socket_default_timeout = socket.getdefaulttimeout() |
| 579 | socket.setdefaulttimeout(URL_VERIFY_EXIST_SOCKET_TIMEOUT) |
574 | 580 | 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) |
581 | 592 | return value |
582 | 593 | |
583 | 594 | class BooleanField(Field): |