mirror of
https://github.com/Kozea/Radicale.git
synced 2025-06-29 16:55:32 +00:00
rework server listen code
This commit is contained in:
parent
61f3557e78
commit
02d157269e
1 changed files with 14 additions and 36 deletions
|
@ -3,6 +3,7 @@
|
||||||
# Copyright © 2008 Pascal Halter
|
# Copyright © 2008 Pascal Halter
|
||||||
# Copyright © 2008-2017 Guillaume Ayoub
|
# Copyright © 2008-2017 Guillaume Ayoub
|
||||||
# Copyright © 2017-2019 Unrud <unrud@outlook.com>
|
# Copyright © 2017-2019 Unrud <unrud@outlook.com>
|
||||||
|
# Copyright © 2024-2024 Peter Bieringer <pb@bieringer.de>
|
||||||
#
|
#
|
||||||
# This library is free software: you can redistribute it and/or modify
|
# This library is free software: you can redistribute it and/or modify
|
||||||
# it under the terms of the GNU General Public License as published by
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
@ -283,45 +284,22 @@ def serve(configuration: config.Configuration,
|
||||||
servers = {}
|
servers = {}
|
||||||
try:
|
try:
|
||||||
hosts: List[Tuple[str, int]] = configuration.get("server", "hosts")
|
hosts: List[Tuple[str, int]] = configuration.get("server", "hosts")
|
||||||
for address in hosts:
|
for AddressPort in hosts:
|
||||||
# Try to bind sockets for IPv4 and IPv6
|
# retrieve IPv4/IPv6 address of address
|
||||||
possible_families = (socket.AF_INET, socket.AF_INET6)
|
try:
|
||||||
bind_ok = False
|
getaddrinfo = socket.getaddrinfo(AddressPort[0], AddressPort[1], 0, socket.SOCK_STREAM, socket.IPPROTO_TCP);
|
||||||
for i, family in enumerate(possible_families):
|
except OSError as e:
|
||||||
is_last = i == len(possible_families) - 1
|
logger.warn("cannot retrieve IPv4 or IPv6 address of: %s" % (format_address(AddressPort)))
|
||||||
|
continue
|
||||||
|
logger.debug("getaddrinfo of '%s': %s" % (format_address(AddressPort), getaddrinfo))
|
||||||
|
for (AddressFamily, SocketKind, SocketProto, SocketFlags, SocketAddress) in getaddrinfo:
|
||||||
|
logger.debug("try to create server socket on '%s'" % (format_address(SocketAddress)))
|
||||||
try:
|
try:
|
||||||
server = server_class(configuration, family, address,
|
server = server_class(configuration, AddressFamily, (SocketAddress[0], SocketAddress[1]), RequestHandler)
|
||||||
RequestHandler)
|
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
# Ignore unsupported families (only one must work)
|
logger.warn("cannot create server socket on '%s': %s" % (format_address(SocketAddress), e))
|
||||||
if ((bind_ok or not is_last) and (
|
continue
|
||||||
isinstance(e, socket.gaierror) and (
|
|
||||||
# Hostname does not exist or doesn't have
|
|
||||||
# address for address family
|
|
||||||
# Linux: temporary failure in name resolution (-3)
|
|
||||||
e.errno == socket.EAI_AGAIN or
|
|
||||||
# macOS: IPv6 address for INET address family
|
|
||||||
e.errno == socket.EAI_NONAME or
|
|
||||||
# Address not for address family
|
|
||||||
e.errno == COMPAT_EAI_ADDRFAMILY or
|
|
||||||
e.errno == COMPAT_EAI_NODATA) or
|
|
||||||
# Workaround for PyPy
|
|
||||||
str(e) == "address family mismatched" or
|
|
||||||
# Address family not available (e.g. IPv6 disabled)
|
|
||||||
# macOS: IPv4 address for INET6 address family with
|
|
||||||
# IPV6_V6ONLY set
|
|
||||||
e.errno == errno.EADDRNOTAVAIL or
|
|
||||||
# Device or resource busy (16)
|
|
||||||
e.errno == errno.EBUSY or
|
|
||||||
# Address family not supported
|
|
||||||
e.errno == errno.EAFNOSUPPORT or
|
|
||||||
# Protocol not supported
|
|
||||||
e.errno == errno.EPROTONOSUPPORT)):
|
|
||||||
continue
|
|
||||||
raise RuntimeError("Failed to start server %r: %s" % (
|
|
||||||
format_address(address), e)) from e
|
|
||||||
servers[server.socket] = server
|
servers[server.socket] = server
|
||||||
bind_ok = True
|
|
||||||
server.set_app(application)
|
server.set_app(application)
|
||||||
logger.info("Listening on %r%s",
|
logger.info("Listening on %r%s",
|
||||||
format_address(server.server_address),
|
format_address(server.server_address),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue