[Python-checkins] CVS: python/dist/src/Lib httplib.py,1.42.10.1,1.42.10.2

Michael Hudson mwh@users.sourceforge.net
Mon, 11 Mar 2002 02:06:38 -0800


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv2294

Modified Files:
      Tag: release22-maint
	httplib.py 
Log Message:
backport jhylton's checkin of
    revision 1.45 of httplib.py

SF bug report #405939: wrong Host header with proxy

In August, Greg said this looked good, so I'm going ahead with it.

The fix is different from the one in the bug report.  Instead of using
a regular expression to extract the host from the url, I use
urlparse.urlsplit.

Martin commented that the patch doesn't address URLs that have basic
authentication username and password in the header.  I don't see any
code anywhere in httplib that supports this feature, so I'm not going
to address it for this fix.

Bug fix candidate.


Index: httplib.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/httplib.py,v
retrieving revision 1.42.10.1
retrieving revision 1.42.10.2
diff -C2 -d -r1.42.10.1 -r1.42.10.2
*** httplib.py	16 Feb 2002 23:08:24 -0000	1.42.10.1
--- httplib.py	11 Mar 2002 10:06:36 -0000	1.42.10.2
***************
*** 70,73 ****
--- 70,74 ----
  import mimetools
  import socket
+ from urlparse import urlsplit
  
  try:
***************
*** 468,474 ****
              # as Apache) barf when they see two Host: headers
  
!             # if we need a non-standard port,include it in the header
!             if self.port == HTTP_PORT:
!                 self.putheader('Host', self.host)
              else:
                  self.putheader('Host', "%s:%s" % (self.host, self.port))
--- 469,481 ----
              # as Apache) barf when they see two Host: headers
  
!             # If we need a non-standard port,include it in the header.
!             # If the request is going through a proxy, but the host of
!             # the actual URL, not the host of the proxy.
! 
!             if url.startswith('http:'):
!                 nil, netloc, nil, nil, nil = urlsplit(url)
!                 self.putheader('Host', netloc)
!             elif self.port == HTTP_PORT:
!                 self.putheader('Host', netloc)
              else:
                  self.putheader('Host', "%s:%s" % (self.host, self.port))
***************
*** 857,860 ****
--- 864,878 ----
      print
      print h.getfile().read()
+ 
+     # minimal test that code to extract host from url works
+     class HTTP11(HTTP):
+         _http_vsn = 11
+         _http_vsn_str = 'HTTP/1.1'
+ 
+     h = HTTP11('www.python.org')
+     h.putrequest('GET', 'http://www.python.org/~jeremy/')
+     h.endheaders()
+     h.getreply()
+     h.close()
  
      if hasattr(socket, 'ssl'):