|
|
|
@ -43,7 +43,14 @@ from . import logger
|
|
|
|
|
log = logger.create()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WebServer:
|
|
|
|
|
|
|
|
|
|
def _readable_listen_address(address, port):
|
|
|
|
|
if ':' in address:
|
|
|
|
|
address = "[" + address + "]"
|
|
|
|
|
return '%s:%s' % (address, port)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WebServer(object):
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
signal.signal(signal.SIGINT, self._killServer)
|
|
|
|
@ -55,14 +62,12 @@ class WebServer:
|
|
|
|
|
self.app = None
|
|
|
|
|
self.listen_address = None
|
|
|
|
|
self.listen_port = None
|
|
|
|
|
self.IPV6 = False
|
|
|
|
|
self.unix_socket_file = None
|
|
|
|
|
self.ssl_args = None
|
|
|
|
|
|
|
|
|
|
def init_app(self, application, config):
|
|
|
|
|
self.app = application
|
|
|
|
|
self.listen_address = config.get_config_ipaddress()
|
|
|
|
|
self.IPV6 = config.get_ipaddress_type()
|
|
|
|
|
self.listen_port = config.config_port
|
|
|
|
|
|
|
|
|
|
if config.config_access_log:
|
|
|
|
@ -77,8 +82,7 @@ class WebServer:
|
|
|
|
|
keyfile_path = config.get_config_keyfile()
|
|
|
|
|
if certfile_path and keyfile_path:
|
|
|
|
|
if os.path.isfile(certfile_path) and os.path.isfile(keyfile_path):
|
|
|
|
|
self.ssl_args = {"certfile": certfile_path,
|
|
|
|
|
"keyfile": keyfile_path}
|
|
|
|
|
self.ssl_args = dict(certfile=certfile_path, keyfile=keyfile_path)
|
|
|
|
|
else:
|
|
|
|
|
log.warning('The specified paths for the ssl certificate file and/or key file seem to be broken. Ignoring ssl.')
|
|
|
|
|
log.warning('Cert path: %s', certfile_path)
|
|
|
|
@ -106,32 +110,33 @@ class WebServer:
|
|
|
|
|
if os.name != 'nt':
|
|
|
|
|
unix_socket_file = os.environ.get("CALIBRE_UNIX_SOCKET")
|
|
|
|
|
if unix_socket_file:
|
|
|
|
|
output = "socket:" + unix_socket_file + ":" + str(self.listen_port)
|
|
|
|
|
return self._make_gevent_unix_socket(unix_socket_file), output
|
|
|
|
|
return self._make_gevent_unix_socket(unix_socket_file), "unix:" + unix_socket_file
|
|
|
|
|
|
|
|
|
|
if self.listen_address:
|
|
|
|
|
return (self.listen_address, self.listen_port), self._get_readable_listen_address()
|
|
|
|
|
return (self.listen_address, self.listen_port), None
|
|
|
|
|
|
|
|
|
|
if os.name == 'nt':
|
|
|
|
|
self.listen_address = '0.0.0.0'
|
|
|
|
|
return (self.listen_address, self.listen_port), self._get_readable_listen_address()
|
|
|
|
|
return (self.listen_address, self.listen_port), None
|
|
|
|
|
|
|
|
|
|
address = ('', self.listen_port)
|
|
|
|
|
try:
|
|
|
|
|
address = ('::', self.listen_port)
|
|
|
|
|
sock = WSGIServer.get_listener(address, family=socket.AF_INET6)
|
|
|
|
|
output = self._get_readable_listen_address(True)
|
|
|
|
|
except socket.error as ex:
|
|
|
|
|
log.error('%s', ex)
|
|
|
|
|
log.warning('Unable to listen on "", trying on IPv4 only...')
|
|
|
|
|
output = self._get_readable_listen_address(False)
|
|
|
|
|
address = ('', self.listen_port)
|
|
|
|
|
sock = WSGIServer.get_listener(address, family=socket.AF_INET)
|
|
|
|
|
return sock, output
|
|
|
|
|
|
|
|
|
|
return sock, _readable_listen_address(*address)
|
|
|
|
|
|
|
|
|
|
def _start_gevent(self):
|
|
|
|
|
ssl_args = self.ssl_args or {}
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
sock, output = self._make_gevent_socket()
|
|
|
|
|
if output is None:
|
|
|
|
|
output = _readable_listen_address(self.listen_address, self.listen_port)
|
|
|
|
|
log.info('Starting Gevent server on %s', output)
|
|
|
|
|
self.wsgiserver = WSGIServer(sock, self.app, log=self.access_logger, spawn=Pool(), **ssl_args)
|
|
|
|
|
self.wsgiserver.serve_forever()
|
|
|
|
@ -141,7 +146,7 @@ class WebServer:
|
|
|
|
|
self.unix_socket_file = None
|
|
|
|
|
|
|
|
|
|
def _start_tornado(self):
|
|
|
|
|
log.info('Starting Tornado server on %s', self._get_readable_listen_address())
|
|
|
|
|
log.info('Starting Tornado server on %s', _readable_listen_address(self.listen_address, self.listen_port))
|
|
|
|
|
|
|
|
|
|
# Max Buffersize set to 200MB )
|
|
|
|
|
http_server = HTTPServer(WSGIContainer(self.app),
|
|
|
|
@ -153,18 +158,6 @@ class WebServer:
|
|
|
|
|
# wait for stop signal
|
|
|
|
|
self.wsgiserver.close(True)
|
|
|
|
|
|
|
|
|
|
def _get_readable_listen_address(self, ipV6=False):
|
|
|
|
|
if self.listen_address == "":
|
|
|
|
|
listen_string = '""'
|
|
|
|
|
else:
|
|
|
|
|
ipV6 = self.IPV6
|
|
|
|
|
listen_string = self.listen_address
|
|
|
|
|
if ipV6:
|
|
|
|
|
adress = "[" + listen_string + "]"
|
|
|
|
|
else:
|
|
|
|
|
adress = listen_string
|
|
|
|
|
return adress + ":" + str(self.listen_port)
|
|
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
|
try:
|
|
|
|
|
if _GEVENT:
|
|
|
|
@ -191,7 +184,7 @@ class WebServer:
|
|
|
|
|
os.execv(sys.executable, arguments)
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def _killServer(self, signum, frame):
|
|
|
|
|
def _killServer(self, ignored_signum, ignored_frame):
|
|
|
|
|
self.stop()
|
|
|
|
|
|
|
|
|
|
def stop(self, restart=False):
|
|
|
|
|