From ee7abce58b13959d1abca7492d84718d9682243a Mon Sep 17 00:00:00 2001 From: ggqlq Date: Sun, 18 May 2025 12:50:40 +0800 Subject: [PATCH 1/7] fix ipv6 and directory flag --- Lib/http/server.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/Lib/http/server.py b/Lib/http/server.py index f42e9a375e479a..82b3131c33e38b 100644 --- a/Lib/http/server.py +++ b/Lib/http/server.py @@ -976,9 +976,9 @@ def test(HandlerClass=BaseHTTPRequestHandler, ServerClass.address_family, addr = _get_best_family(bind, port) HandlerClass.protocol_version = protocol - if tls_cert: - server = ThreadingHTTPSServer(addr, HandlerClass, certfile=tls_cert, - keyfile=tls_key, password=tls_password) + if issubclass(ServerClass, HTTPSServer): + server = ServerClass(addr, HandlerClass, certfile=tls_cert, + keyfile=tls_key, password=tls_password) else: server = ServerClass(addr, HandlerClass) @@ -1051,9 +1051,22 @@ def finish_request(self, request, client_address): self.RequestHandlerClass(request, client_address, self, directory=args.directory) + class HTTPSDualStackServer(ThreadingHTTPSServer): + def server_bind(self): + with contextlib.suppress(Exception): + self.socket.setsockopt( + socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0) + return super().server_bind() + + def finish_request(self, request, client_address): + self.RequestHandlerClass(request, client_address, self, + directory=args.directory) + + ServerClass = HTTPSDualStackServer if args.tls_cert else DualStackServer + test( HandlerClass=SimpleHTTPRequestHandler, - ServerClass=DualStackServer, + ServerClass=ServerClass, port=args.port, bind=args.bind, protocol=args.protocol, From cf99f4d7090d197412a94855637ee90cc8ab1932 Mon Sep 17 00:00:00 2001 From: ggqlq Date: Sun, 18 May 2025 14:16:39 +0800 Subject: [PATCH 2/7] check if ServerClass is a class --- Lib/http/server.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/http/server.py b/Lib/http/server.py index 82b3131c33e38b..743b8f0885bd9d 100644 --- a/Lib/http/server.py +++ b/Lib/http/server.py @@ -976,7 +976,8 @@ def test(HandlerClass=BaseHTTPRequestHandler, ServerClass.address_family, addr = _get_best_family(bind, port) HandlerClass.protocol_version = protocol - if issubclass(ServerClass, HTTPSServer): + # Make sure ServerClass is a class object, not a function instance + if isinstance(ServerClass, type) and issubclass(ServerClass, HTTPSServer): server = ServerClass(addr, HandlerClass, certfile=tls_cert, keyfile=tls_key, password=tls_password) else: From 26539a81ca10c8055238bd58f6d0b4a0c58dca36 Mon Sep 17 00:00:00 2001 From: ggqlq Date: Sun, 18 May 2025 20:49:55 +0800 Subject: [PATCH 3/7] use a mixin class --- Lib/http/server.py | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/Lib/http/server.py b/Lib/http/server.py index 743b8f0885bd9d..6e9e18dd7168dc 100644 --- a/Lib/http/server.py +++ b/Lib/http/server.py @@ -1039,7 +1039,7 @@ def test(HandlerClass=BaseHTTPRequestHandler, parser.error(f"Failed to read TLS password file: {e}") # ensure dual-stack is not disabled; ref #38907 - class DualStackServer(ThreadingHTTPServer): + class DualStackServerMixin: def server_bind(self): # suppress exception when protocol is IPv4 @@ -1052,18 +1052,10 @@ def finish_request(self, request, client_address): self.RequestHandlerClass(request, client_address, self, directory=args.directory) - class HTTPSDualStackServer(ThreadingHTTPSServer): - def server_bind(self): - with contextlib.suppress(Exception): - self.socket.setsockopt( - socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0) - return super().server_bind() - - def finish_request(self, request, client_address): - self.RequestHandlerClass(request, client_address, self, - directory=args.directory) + class HTTPDualStackServer(DualStackServerMixin, ThreadingHTTPServer): pass + class HTTPSDualStackServer(DualStackServerMixin, ThreadingHTTPSServer): pass - ServerClass = HTTPSDualStackServer if args.tls_cert else DualStackServer + ServerClass = HTTPSDualStackServer if args.tls_cert else HTTPDualStackServer test( HandlerClass=SimpleHTTPRequestHandler, From cf74a01c5228c9077a08ea968c5b2b30ceafe8b1 Mon Sep 17 00:00:00 2001 From: ggqlq Date: Sun, 18 May 2025 20:55:35 +0800 Subject: [PATCH 4/7] fix the check logic of ServerClass in the 'test' function --- Lib/http/server.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Lib/http/server.py b/Lib/http/server.py index 6e9e18dd7168dc..698089863937ff 100644 --- a/Lib/http/server.py +++ b/Lib/http/server.py @@ -976,8 +976,7 @@ def test(HandlerClass=BaseHTTPRequestHandler, ServerClass.address_family, addr = _get_best_family(bind, port) HandlerClass.protocol_version = protocol - # Make sure ServerClass is a class object, not a function instance - if isinstance(ServerClass, type) and issubclass(ServerClass, HTTPSServer): + if tls_cert: server = ServerClass(addr, HandlerClass, certfile=tls_cert, keyfile=tls_key, password=tls_password) else: From 0a96419aed4f23ea77db2abbbb3870913d3517de Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 18 May 2025 13:23:30 +0000 Subject: [PATCH 5/7] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2025-05-18-13-23-29.gh-issue-134168.hgx3Xg.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2025-05-18-13-23-29.gh-issue-134168.hgx3Xg.rst diff --git a/Misc/NEWS.d/next/Library/2025-05-18-13-23-29.gh-issue-134168.hgx3Xg.rst b/Misc/NEWS.d/next/Library/2025-05-18-13-23-29.gh-issue-134168.hgx3Xg.rst new file mode 100644 index 00000000000000..582a60c6dbd6cd --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-05-18-13-23-29.gh-issue-134168.hgx3Xg.rst @@ -0,0 +1 @@ +http.server: Fix IPv6 address binding and --directory handling when using HTTPS. From 7cf05e9e792405abfeb30e3db09814d3274d3d41 Mon Sep 17 00:00:00 2001 From: ggqlq <124190229+ggqlq@users.noreply.github.com> Date: Sun, 18 May 2025 21:44:37 +0800 Subject: [PATCH 6/7] Update Lib/http/server.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Lib/http/server.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Lib/http/server.py b/Lib/http/server.py index 698089863937ff..a7eb7e107736ec 100644 --- a/Lib/http/server.py +++ b/Lib/http/server.py @@ -1051,8 +1051,10 @@ def finish_request(self, request, client_address): self.RequestHandlerClass(request, client_address, self, directory=args.directory) - class HTTPDualStackServer(DualStackServerMixin, ThreadingHTTPServer): pass - class HTTPSDualStackServer(DualStackServerMixin, ThreadingHTTPSServer): pass + class HTTPDualStackServer(DualStackServerMixin, ThreadingHTTPServer): + pass + class HTTPSDualStackServer(DualStackServerMixin, ThreadingHTTPSServer): + pass ServerClass = HTTPSDualStackServer if args.tls_cert else HTTPDualStackServer From ebdd8572176df7a4221b87374067e9da89c5d5a1 Mon Sep 17 00:00:00 2001 From: ggqlq <124190229+ggqlq@users.noreply.github.com> Date: Sun, 18 May 2025 21:44:45 +0800 Subject: [PATCH 7/7] Update Misc/NEWS.d/next/Library/2025-05-18-13-23-29.gh-issue-134168.hgx3Xg.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- .../Library/2025-05-18-13-23-29.gh-issue-134168.hgx3Xg.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-05-18-13-23-29.gh-issue-134168.hgx3Xg.rst b/Misc/NEWS.d/next/Library/2025-05-18-13-23-29.gh-issue-134168.hgx3Xg.rst index 582a60c6dbd6cd..5a0e20005db11e 100644 --- a/Misc/NEWS.d/next/Library/2025-05-18-13-23-29.gh-issue-134168.hgx3Xg.rst +++ b/Misc/NEWS.d/next/Library/2025-05-18-13-23-29.gh-issue-134168.hgx3Xg.rst @@ -1 +1,2 @@ -http.server: Fix IPv6 address binding and --directory handling when using HTTPS. +:mod:`http.server`: Fix IPv6 address binding and +:option:`--directory ` handling when using HTTPS. pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy