Ticket #11826: rfc2616-ticket11826.patch

File rfc2616-ticket11826.patch, 1.8 KB (added by wam, 15 years ago)

Patch to url_re to allow URLs with no abs_name component, plus testcase

  • django/forms/fields.py

     
    537537    r'localhost|' #localhost...
    538538    r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
    539539    r'(?::\d+)?' # optional port
    540     r'(?:/?|/\S+)$', re.IGNORECASE)
     540    r'(?:/?|[/?]\S+)$', re.IGNORECASE)
    541541
    542542class URLField(RegexField):
    543543    default_error_messages = {
     
    553553        self.user_agent = validator_user_agent
    554554
    555555    def clean(self, value):
    556         # If no URL scheme given, assume http://
    557         if value and '://' not in value:
    558             value = u'http://%s' % value
    559         # If no URL path given, assume /
    560         if value and not urlparse.urlsplit(value)[2]:
    561             value += '/'
     556        if value:
     557            if '://' not in value:
     558                # If no URL scheme given, assume http://
     559                value = u'http://%s' % value
     560            url_fields = list(urlparse.urlsplit(value))
     561            if not url_fields[2]:
     562                # the path portion may need to be added before query params
     563                url_fields[2] = '/'
     564                value = urlparse.urlunsplit(url_fields)
    562565        value = super(URLField, self).clean(value)
    563566        if value == u'':
    564567            return value
  • tests/regressiontests/forms/fields.py

     
    972972Traceback (most recent call last):
    973973...
    974974ValidationError: [u'Enter a valid URL.']
     975>>> f.clean('http://example.com?some_param=some_value')  # RFC 2616 3.2.3
     976u'http://example.com/?some_param=some_value'
    975977>>> f.clean('http://.com')
    976978Traceback (most recent call last):
    977979...
Back to Top