summaryrefslogtreecommitdiffstats
path: root/Lib/http/client.py
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2021-03-08 07:59:37 (GMT)
committerGitHub <noreply@github.com>2021-03-08 07:59:37 (GMT)
commitc6e7cf1ee09c88d35e6703c33a61eca7b9db54f3 (patch)
tree941937755bf1c1eba5c3c80d894ffb81b678d76f /Lib/http/client.py
parent2c0a0b04a42dc4965fcfaef936f497e44f06dea5 (diff)
downloadcpython-c6e7cf1ee09c88d35e6703c33a61eca7b9db54f3.zip
cpython-c6e7cf1ee09c88d35e6703c33a61eca7b9db54f3.tar.gz
cpython-c6e7cf1ee09c88d35e6703c33a61eca7b9db54f3.tar.bz2
bpo-43332: Buffer proxy connection setup packets before sending. (GH-24780)
We now buffer the CONNECT request + tunnel HTTP headers into a single send call. This prevents the OS from generating multiple network packets for connection setup when not necessary, improving efficiency. (cherry picked from commit c25910a135c2245accadb324b40dd6453015e056) Co-authored-by: Gregory P. Smith <greg@krypto.org>
Diffstat (limited to 'Lib/http/client.py')
-rw-r--r--Lib/http/client.py21
1 files changed, 11 insertions, 10 deletions
diff --git a/Lib/http/client.py b/Lib/http/client.py
index 16afc87..a96fd5e 100644
--- a/Lib/http/client.py
+++ b/Lib/http/client.py
@@ -886,23 +886,24 @@ class HTTPConnection:
self.debuglevel = level
def _tunnel(self):
- connect_str = "CONNECT %s:%d HTTP/1.0\r\n" % (self._tunnel_host,
- self._tunnel_port)
- connect_bytes = connect_str.encode("ascii")
- self.send(connect_bytes)
+ connect = b"CONNECT %s:%d HTTP/1.0\r\n" % (
+ self._tunnel_host.encode("ascii"), self._tunnel_port)
+ headers = [connect]
for header, value in self._tunnel_headers.items():
- header_str = "%s: %s\r\n" % (header, value)
- header_bytes = header_str.encode("latin-1")
- self.send(header_bytes)
- self.send(b'\r\n')
+ headers.append(f"{header}: {value}\r\n".encode("latin-1"))
+ headers.append(b"\r\n")
+ # Making a single send() call instead of one per line encourages
+ # the host OS to use a more optimal packet size instead of
+ # potentially emitting a series of small packets.
+ self.send(b"".join(headers))
+ del headers
response = self.response_class(self.sock, method=self._method)
(version, code, message) = response._read_status()
if code != http.HTTPStatus.OK:
self.close()
- raise OSError("Tunnel connection failed: %d %s" % (code,
- message.strip()))
+ raise OSError(f"Tunnel connection failed: {code} {message.strip()}")
while True:
line = response.fp.readline(_MAXLINE + 1)
if len(line) > _MAXLINE: