Fix SF bug 525520.

Don't automatically add a Host: header if the headers passed to
request() already has a Host key.
This commit is contained in:
Jeremy Hylton 2002-03-09 06:07:23 +00:00
parent dc5a508761
commit 3921ff675e
1 changed files with 32 additions and 18 deletions

View File

@ -410,7 +410,7 @@ class HTTPConnection:
self.close()
raise
def putrequest(self, method, url):
def putrequest(self, method, url, skip_host=0):
"""Send a request to the server.
`method' specifies an HTTP request method, e.g. 'GET'.
@ -461,24 +461,31 @@ class HTTPConnection:
if self._http_vsn == 11:
# Issue some standard headers for better HTTP/1.1 compliance
# this header is issued *only* for HTTP/1.1 connections. more
# specifically, this means it is only issued when the client uses
# the new HTTPConnection() class. backwards-compat clients will
# be using HTTP/1.0 and those clients may be issuing this header
# themselves. we should NOT issue it twice; some web servers (such
# as Apache) barf when they see two Host: headers
if not skip_host:
# this header is issued *only* for HTTP/1.1
# connections. more specifically, this means it is
# only issued when the client uses the new
# HTTPConnection() class. backwards-compat clients
# will be using HTTP/1.0 and those clients may be
# issuing this header themselves. we should NOT issue
# it twice; some web servers (such 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 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))
netloc = ''
if url.startswith('http'):
nil, netloc, nil, nil, nil = urlsplit(url)
if netloc:
self.putheader('Host', netloc)
elif self.port == HTTP_PORT:
self.putheader('Host', self.host)
else:
self.putheader('Host', "%s:%s" % (self.host, self.port))
# note: we are assuming that clients will not attempt to set these
# headers since *this* library must deal with the
@ -536,7 +543,14 @@ class HTTPConnection:
self._send_request(method, url, body, headers)
def _send_request(self, method, url, body, headers):
self.putrequest(method, url)
# If headers already contains a host header, then define the
# optional skip_host argument to putrequest(). The check is
# harder because field names are case insensitive.
if (headers.has_key('Host')
or [k for k in headers.iterkeys() if k.lower() == "host"]):
self.putrequest(method, url, skip_host=1)
else:
self.putrequest(method, url)
if body:
self.putheader('Content-Length', str(len(body)))