Fix Issue5111 - Wrap the Ipv6 host with [] in the Host header
authorSenthil Kumaran <orsenthil@gmail.com>
Sat, 13 Nov 2010 12:27:49 +0000 (12:27 +0000)
committerSenthil Kumaran <orsenthil@gmail.com>
Sat, 13 Nov 2010 12:27:49 +0000 (12:27 +0000)
Lib/http/client.py
Lib/test/test_httplib.py
Misc/NEWS

index 6c38c4aae35fa73917df0265f5c162089a6de204..0059b5138f260c0124c825364c90b0a80747d270 100644 (file)
@@ -874,6 +874,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 c3bafea4472e4440c7f7215a69ad256ab2a50f83..c5c0f7a3c77b5c91cc996d7a5d49b9b99b6f38e4 100644 (file)
@@ -106,6 +106,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 2887040d1e54d08c93c5ab8de398e13413ad1775..3d3a7283d363e7b3ae75fecbdf3320be9981477d 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -63,6 +63,8 @@ Core and Builtins
 Library
 -------
 
+- Issue #5111: IPv6 Host in the Header is wrapped inside [ ]. Patch by Chandru.
+
 - Fix Fraction.__hash__ so that Fraction.__hash__(-1) is -2.  (See
   also issue #10356.)