From bff727c04300f597e91c64dc259a6d5390a2d7b8 Mon Sep 17 00:00:00 2001 From: coletdjnz Date: Mon, 1 Apr 2024 14:02:15 +1300 Subject: [PATCH 1/3] 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 @@ def __init__(self, *args, proxy_info=None, username=None, password=None, request 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 @@ def do_GET(self): 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 @@ def __init__(self, *args, username=None, password=None, request_handler=None, ** 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 @@ def do_CONNECT(self): '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 @@ def test_http_bad_auth(self, handler, ctx): 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: From 41add1d7af2e9a1db5640481f637eb7035b04fa1 Mon Sep 17 00:00:00 2001 From: coletdjnz Date: Mon, 1 Apr 2024 14:37:21 +1300 Subject: [PATCH 2/3] be gone unclosed socket --- test/test_http_proxy.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/test_http_proxy.py b/test/test_http_proxy.py index 82529e1db..f7175653b 100644 --- a/test/test_http_proxy.py +++ b/test/test_http_proxy.py @@ -156,8 +156,9 @@ def do_CONNECT(self): 'path': self.path, 'proxy': ':'.join(str(y) for y in self.connection.getsockname()), } + request = self.request self.request_handler(self.request, self.client_address, self.server, proxy_info=proxy_info) - self.server.close_request(self.request) + self.server.close_request(request) class HTTPSConnectProxyHandler(HTTPConnectProxyHandler): From 6c3140a8c133e08082dbb4d10a5dd1e82fdd8246 Mon Sep 17 00:00:00 2001 From: coletdjnz Date: Thu, 4 Apr 2024 19:34:28 +1300 Subject: [PATCH 3/3] try this --- test/test_http_proxy.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/test_http_proxy.py b/test/test_http_proxy.py index f7175653b..3f6035d90 100644 --- a/test/test_http_proxy.py +++ b/test/test_http_proxy.py @@ -156,9 +156,8 @@ def do_CONNECT(self): 'path': self.path, 'proxy': ':'.join(str(y) for y in self.connection.getsockname()), } - request = self.request self.request_handler(self.request, self.client_address, self.server, proxy_info=proxy_info) - self.server.close_request(request) + self.server.close_request(self.request) class HTTPSConnectProxyHandler(HTTPConnectProxyHandler): @@ -167,8 +166,13 @@ def __init__(self, request, *args, **kwargs): sslctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) sslctx.load_cert_chain(certfn, None) request = sslctx.wrap_socket(request, server_side=True) + self._original_request = request super().__init__(request, *args, **kwargs) + def do_CONNECT(self): + super().do_CONNECT() + self.server.close_request(self._original_request) + @contextlib.contextmanager def proxy_server(proxy_server_class, request_handler, bind_ip=None, **proxy_server_kwargs):