From bff727c04300f597e91c64dc259a6d5390a2d7b8 Mon Sep 17 00:00:00 2001 From: coletdjnz Date: Mon, 1 Apr 2024 14:02:15 +1300 Subject: [PATCH] Fix unclosed socket errors --- test/test_http_proxy.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/test/test_http_proxy.py b/test/test_http_proxy.py index 97ceef9f9..82529e1db 100644 --- a/test/test_http_proxy.py +++ b/test/test_http_proxy.py @@ -59,7 +59,9 @@ class HTTPProxyHandler(BaseHTTPRequestHandler, HTTPProxyAuthMixin): super().__init__(*args, **kwargs) def do_GET(self): - self.do_proxy_auth(self.username, self.password) + if not self.do_proxy_auth(self.username, self.password): + self.server.close_request(self.request) + return if self.path.endswith('/proxy_info'): payload = json.dumps(self.proxy_info or { 'client_address': self.client_address, @@ -75,6 +77,11 @@ class HTTPProxyHandler(BaseHTTPRequestHandler, HTTPProxyAuthMixin): self.send_header('Content-Length', str(len(payload))) self.end_headers() self.wfile.write(payload.encode()) + else: + self.send_response(404) + self.end_headers() + + self.server.close_request(self.request) if urllib3: @@ -135,7 +142,9 @@ class HTTPConnectProxyHandler(BaseHTTPRequestHandler, HTTPProxyAuthMixin): super().__init__(*args, **kwargs) def do_CONNECT(self): - self.do_proxy_auth(self.username, self.password) + if not self.do_proxy_auth(self.username, self.password): + self.server.close_request(self.request) + return self.send_response(200) self.end_headers() proxy_info = { @@ -148,6 +157,7 @@ class HTTPConnectProxyHandler(BaseHTTPRequestHandler, HTTPProxyAuthMixin): 'proxy': ':'.join(str(y) for y in self.connection.getsockname()), } self.request_handler(self.request, self.client_address, self.server, proxy_info=proxy_info) + self.server.close_request(self.request) class HTTPSConnectProxyHandler(HTTPConnectProxyHandler): @@ -249,6 +259,7 @@ class TestHTTPProxy: with pytest.raises(HTTPError) as exc_info: ctx.proxy_info_request(rh) assert exc_info.value.response.status == 407 + exc_info.value.response.close() def test_http_source_address(self, handler, ctx): with ctx.http_server(HTTPProxyHandler) as server_address: