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

View file

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