Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixes #13804 -- URLField validation failure for a url containing '://…

…' on the path and no scheme. Backport of r14657

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@14658 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit d04ffd72471a7b400ca8e3263effb87713c7dba1 1 parent 4a85193
Chris Beaven authored November 21, 2010
17  django/forms/fields.py
@@ -546,14 +546,23 @@ def __init__(self, max_length=None, min_length=None, verify_exists=False,
546 546
 
547 547
     def to_python(self, value):
548 548
         if value:
549  
-            if '://' not in value:
550  
-                # If no URL scheme given, assume http://
551  
-                value = u'http://%s' % value
552 549
             url_fields = list(urlparse.urlsplit(value))
  550
+            if not url_fields[0]:
  551
+                # If no URL scheme given, assume http://
  552
+                url_fields[0] = 'http'
  553
+            if not url_fields[1]:
  554
+                # Assume that if no domain is provided, that the path segment
  555
+                # contains the domain. 
  556
+                url_fields[1] = url_fields[2]
  557
+                url_fields[2] = ''
  558
+                # Rebuild the url_fields list, since the domain segment may now
  559
+                # contain the path too.
  560
+                value = urlparse.urlunsplit(url_fields)
  561
+                url_fields = list(urlparse.urlsplit(value))
553 562
             if not url_fields[2]:
554 563
                 # the path portion may need to be added before query params
555 564
                 url_fields[2] = '/'
556  
-                value = urlparse.urlunsplit(url_fields)
  565
+            value = urlparse.urlunsplit(url_fields)
557 566
         return super(URLField, self).to_python(value)
558 567
 
559 568
 class BooleanField(Field):
1  tests/regressiontests/forms/tests/fields.py
@@ -492,6 +492,7 @@ def test_urlfield_1(self):
492 492
         self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://inv-.-alid.com')
493 493
         self.assertEqual(u'http://valid-----hyphens.com/', f.clean('http://valid-----hyphens.com'))
494 494
         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'))
  495
+        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'))
495 496
 
496 497
     def test_url_regex_ticket11198(self):
497 498
         f = URLField()

0 notes on commit d04ffd7

Please sign in to comment.
Something went wrong with that request. Please try again.