]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Merged revisions 86450 via svnmerge from
authorSenthil Kumaran <orsenthil@gmail.com>
Sun, 14 Nov 2010 03:14:52 +0000 (03:14 +0000)
committerSenthil Kumaran <orsenthil@gmail.com>
Sun, 14 Nov 2010 03:14:52 +0000 (03:14 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r86450 | senthil.kumaran | 2010-11-13 20:27:49 +0800 (Sat, 13 Nov 2010) | 3 lines

  Fix Issue5111 -  Wrap the Ipv6 host with [] in the Host header
........

Lib/http/client.py
Lib/test/test_httplib.py
Misc/NEWS

index f273b03c6f61b41cb2162791d0818b6973a65ccb..bd092c215efd320b88bf1c95257264282e46a70a 100644 (file)
@@ -873,6 +873,13 @@ class HTTPConnection:
                         host_enc = self.host.encode("ascii")
                     except UnicodeEncodeError:
                         host_enc = self.host.encode("idna")
+
+                    # As per RFC 273, IPv6 address should be wrapped with []
+                    # when used as Host header
+
+                    if self.host.find(':') >= 0:
+                        host_enc = b'[' + host_enc + b']'
+
                     if self.port == self.default_port:
                         self.putheader('Host', host_enc)
                     else:
index 6b703dd43c3a8f824c61369c7245142c361de5bb..ee283168b68d1482df0faecf737e6544da14d596 100644 (file)
@@ -96,6 +96,25 @@ class HeaderTests(TestCase):
         conn.putheader('Content-length', 42)
         self.assertTrue(b'Content-length: 42' in conn._buffer)
 
+    def test_ipv6host_header(self):
+        # Default host header on IPv6 transaction should wrapped by [] if
+        # its actual IPv6 address
+        expected = b'GET /foo HTTP/1.1\r\nHost: [2001::]:81\r\n' \
+                   b'Accept-Encoding: identity\r\n\r\n'
+        conn = client.HTTPConnection('[2001::]:81')
+        sock = FakeSocket('')
+        conn.sock = sock
+        conn.request('GET', '/foo')
+        self.assertTrue(sock.data.startswith(expected))
+
+        expected = b'GET /foo HTTP/1.1\r\nHost: [2001:102A::]\r\n' \
+                   b'Accept-Encoding: identity\r\n\r\n'
+        conn = client.HTTPConnection('[2001:102A::]')
+        sock = FakeSocket('')
+        conn.sock = sock
+        conn.request('GET', '/foo')
+        self.assertTrue(sock.data.startswith(expected))
+
 
 class BasicTest(TestCase):
     def test_status_lines(self):
index 7fb9717494c643f8ac1168c6e4ba666bbc6d8729..fcf9ec7089e5607dea9977d17accdab5d553894d 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -155,6 +155,8 @@ C-API
 Library
 -------
 
+- Issue #5111: IPv6 Host in the Header is wrapped inside [ ]. Patch by Chandru.
+
 - Issue #4471: Properly shutdown socket in IMAP.shutdown().  Patch by
   Lorenzo M. Catucci.