diff options
author | Jeremy Hylton <jeremy@alum.mit.edu> | 2002-03-09 06:07:23 (GMT) |
---|---|---|
committer | Jeremy Hylton <jeremy@alum.mit.edu> | 2002-03-09 06:07:23 (GMT) |
commit | 3921ff675ec544f3738bcaf606cca745b9a508ea (patch) | |
tree | 5c508bf5162474da922053c2719f92dfb9b62db0 | |
parent | dc5a508761d7260bc863a2f3068723c298336382 (diff) | |
download | cpython-3921ff675ec544f3738bcaf606cca745b9a508ea.zip cpython-3921ff675ec544f3738bcaf606cca745b9a508ea.tar.gz cpython-3921ff675ec544f3738bcaf606cca745b9a508ea.tar.bz2 |
Fix SF bug 525520.
Don't automatically add a Host: header if the headers passed to
request() already has a Host key.
-rw-r--r-- | Lib/httplib.py | 54 |
1 files changed, 34 insertions, 20 deletions
diff --git a/Lib/httplib.py b/Lib/httplib.py index f299873..d7254ff 100644 --- a/Lib/httplib.py +++ b/Lib/httplib.py @@ -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 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)) + 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. + + 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))) |