Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion RangeHTTPServer/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@
from . import RangeRequestHandler

import argparse
import contextlib
import socket


class DualStackServer(SimpleHTTPServer.ThreadingHTTPServer):
def server_bind(self):
# suppress exception when protocol is IPv4
with contextlib.suppress(Exception):
Comment on lines +29 to +30
Copy link

Copilot AI Apr 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

contextlib.suppress(Exception) is overly broad here and can hide real bugs (e.g., programming errors or unexpected socket state). Narrow the suppression to the specific exception types expected from this setsockopt call (typically OSError / AttributeError, depending on platform) so genuine failures still surface.

Suggested change
# suppress exception when protocol is IPv4
with contextlib.suppress(Exception):
# suppress expected platform/socket capability errors when protocol is IPv4
with contextlib.suppress(OSError, AttributeError):

Copilot uses AI. Check for mistakes.
self.socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
return super().server_bind()
Comment on lines +27 to +32
Copy link

Copilot AI Apr 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DualStackServer only tweaks IPV6_V6ONLY but doesn’t set address_family = socket.AF_INET6. As a result, if a user passes an IPv6 bind address like --bind ::, the underlying HTTPServer/ThreadingHTTPServer will still create an AF_INET socket and the bind will fail. To match the CPython dual-stack fix, set the server’s address_family appropriately (and ideally only use the dual-stack server when IPv6 is available via socket.has_dualstack_ipv6()).

Copilot uses AI. Check for mistakes.


parser = argparse.ArgumentParser()
parser.add_argument('port', action='store',
default=8000, type=int,
Expand All @@ -28,4 +40,4 @@
help='bind to this address (default: all interfaces)')

args = parser.parse_args()
SimpleHTTPServer.test(HandlerClass=RangeRequestHandler, port=args.port, bind=args.bind)
SimpleHTTPServer.test(HandlerClass=RangeRequestHandler, ServerClass=DualStackServer, port=args.port, bind=args.bind)
Copy link

Copilot AI Apr 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SimpleHTTPServer.test(..., ServerClass=DualStackServer, ...) always forces the custom server class even when dual-stack IPv6 isn’t supported on the host. Consider selecting the ServerClass conditionally (e.g., use the dual-stack variant only when socket.has_dualstack_ipv6() is true and the bind address is unspecified/IPv6), and otherwise fall back to the default server class to avoid startup failures on IPv4-only environments.

Copilot uses AI. Check for mistakes.
Loading