Skip to content

Commit b8a7880

Browse files
committed
Fixes #13804 -- URLField validation failure for a url containing '://' on the path and no scheme
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14657 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent f6ead36 commit b8a7880

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

django/forms/fields.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -581,14 +581,23 @@ def __init__(self, max_length=None, min_length=None, verify_exists=False,
581581

582582
def to_python(self, value):
583583
if value:
584-
if '://' not in value:
585-
# If no URL scheme given, assume http://
586-
value = u'http://%s' % value
587584
url_fields = list(urlparse.urlsplit(value))
585+
if not url_fields[0]:
586+
# If no URL scheme given, assume http://
587+
url_fields[0] = 'http'
588+
if not url_fields[1]:
589+
# Assume that if no domain is provided, that the path segment
590+
# contains the domain.
591+
url_fields[1] = url_fields[2]
592+
url_fields[2] = ''
593+
# Rebuild the url_fields list, since the domain segment may now
594+
# contain the path too.
595+
value = urlparse.urlunsplit(url_fields)
596+
url_fields = list(urlparse.urlsplit(value))
588597
if not url_fields[2]:
589598
# the path portion may need to be added before query params
590599
url_fields[2] = '/'
591-
value = urlparse.urlunsplit(url_fields)
600+
value = urlparse.urlunsplit(url_fields)
592601
return super(URLField, self).to_python(value)
593602

594603
class BooleanField(Field):

tests/regressiontests/forms/tests/fields.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ def test_urlfield_1(self):
495495
self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://inv-.-alid.com')
496496
self.assertEqual(u'http://valid-----hyphens.com/', f.clean('http://valid-----hyphens.com'))
497497
self.assertEqual(u'http://some.idn.xyz\xe4\xf6\xfc\xdfabc.domain.com:123/blah', f.clean('http://some.idn.xyzäöüßabc.domain.com:123/blah'))
498+
self.assertEqual(u'http://www.example.com/s/http://code.djangoproject.com/ticket/13804', f.clean('www.example.com/s/http://code.djangoproject.com/ticket/13804'))
498499

499500
def test_url_regex_ticket11198(self):
500501
f = URLField()

0 commit comments

Comments
 (0)