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 |
| 571 | import sys |
567 | 572 | headers = { |
568 | 573 | "Accept": "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5", |
569 | 574 | "Accept-Language": "en-us,en;q=0.5", |
… |
… |
|
571 | 576 | "Connection": "close", |
572 | 577 | "User-Agent": self.user_agent, |
573 | 578 | } |
| 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 | |
574 | 590 | try: |
575 | 591 | req = urllib2.Request(value, None, headers) |
576 | | u = urllib2.urlopen(req) |
| 592 | u = urlopen(req, timeout=URL_VERIFY_EXIST_SOCKET_TIMEOUT) |
577 | 593 | except ValueError: |
578 | 594 | 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']) |
580 | 598 | raise ValidationError(self.error_messages['invalid_link']) |
581 | 599 | return value |
582 | 600 | |