[rh:curl_cffi] Support curl_cffi 0.16.x (#17439)

Closes #17341
Authored by: bashonly, coletdjnz

Co-authored-by: coletdjnz <coletdjnz@protonmail.com>
This commit is contained in:
bashonly
2026-08-15 23:50:51 +00:00
committed by GitHub
co-authored by coletdjnz
parent d2dcbfc574
commit 4dc054d51c
5 changed files with 42 additions and 9 deletions
+22 -2
View File
@@ -83,6 +83,12 @@ class HTTPProxyHandler(BaseHTTPRequestHandler, HTTPProxyAuthMixin):
self.server.close_request(self.request)
def finish(self):
try:
super().finish()
finally:
self.server.close_request(self.request)
if urllib3:
import urllib3.util.ssltransport
@@ -132,7 +138,11 @@ class HTTPSProxyHandler(HTTPProxyHandler):
request = SSLTransport(request, ssl_context=sslctx, server_side=True)
else:
request = sslctx.wrap_socket(request, server_side=True)
super().__init__(request, *args, **kwargs)
try:
super().__init__(request, *args, **kwargs)
except Exception:
request.close()
raise
class HTTPConnectProxyHandler(BaseHTTPRequestHandler, HTTPProxyAuthMixin):
@@ -163,6 +173,12 @@ class HTTPConnectProxyHandler(BaseHTTPRequestHandler, HTTPProxyAuthMixin):
self.request_handler(self.request, self.client_address, self.server, proxy_info=proxy_info)
self.server.close_request(self.request)
def finish(self):
try:
super().finish()
finally:
self.server.close_request(self.request)
class HTTPSConnectProxyHandler(HTTPConnectProxyHandler):
def __init__(self, request, *args, **kwargs):
@@ -171,7 +187,11 @@ class HTTPSConnectProxyHandler(HTTPConnectProxyHandler):
sslctx.load_cert_chain(certfn, None)
request = sslctx.wrap_socket(request, server_side=True)
self._original_request = request
super().__init__(request, *args, **kwargs)
try:
super().__init__(request, *args, **kwargs)
except Exception:
request.close()
raise
def do_CONNECT(self):
super().do_CONNECT()
+12 -2
View File
@@ -388,13 +388,23 @@ class TestHTTPRequestHandler(TestRequestHandlerBase):
assert res.status == 200
res.close()
def test_percent_encode(self, handler):
def test_percent_encode_unicode(self, handler):
# RFC 3986 §6.2.2.1 defines that percent-encoding SHOULD be normalized to uppercase.
with handler() as rh:
# Unicode characters should be encoded with uppercase percent-encoding
res = validate_and_send(rh, Request(f'http://127.0.0.1:{self.http_port}/中文.html'))
assert res.status == 200
res.close()
# don't normalize existing percent encodings
@pytest.mark.skip_handler('CurlCFFI', 'not supported by curl-cffi (non-standard)')
def test_percent_encode_keep_existing(self, handler):
# NOTE: RFC 3986 §6.2.2.1 defines that percent-encoding SHOULD be normalized to uppercase.
# For compatibility with legacy sites (e.g., redirects using lowercase encodings and only accept that),
# our default handlers (urllib/requests) preserve existing percent-encoding instead of normalizing it.
#
# CurlCFFI is excluded because it forces uppercase encodings and is hard to change. This is acceptable
# since CurlCFFI is used only for impersonation. https://github.com/curl/curl/pull/21592
with handler() as rh:
res = validate_and_send(rh, Request(f'http://127.0.0.1:{self.http_port}/%c7%9f'))
assert res.status == 200
res.close()