2016-11-04 21:04:37 +00:00
|
|
|
#!/usr/bin/env python3
|
2014-01-13 16:31:57 +00:00
|
|
|
#
|
|
|
|
# Copyright (c) 2013,Thibault Saunier <thibault.saunier@collabora.com>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2.1 of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this program; if not, write to the
|
|
|
|
# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
# Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
import os
|
|
|
|
import time
|
2016-11-04 21:04:37 +00:00
|
|
|
from . import loggable
|
2014-01-13 16:31:57 +00:00
|
|
|
import subprocess
|
2014-05-19 16:06:46 +00:00
|
|
|
import sys
|
2019-03-16 15:21:34 +00:00
|
|
|
import urllib.request
|
|
|
|
import urllib.error
|
|
|
|
import urllib.parse
|
2014-01-13 16:31:57 +00:00
|
|
|
|
2019-03-17 00:37:37 +00:00
|
|
|
from .utils import printc, Colors
|
|
|
|
|
2014-01-13 16:31:57 +00:00
|
|
|
logcat = "httpserver"
|
|
|
|
|
|
|
|
|
|
|
|
class HTTPServer(loggable.Loggable):
|
2014-10-24 12:23:52 +00:00
|
|
|
|
2014-01-13 16:31:57 +00:00
|
|
|
""" Class to run a SimpleHttpServer in a process."""
|
2014-10-24 12:23:52 +00:00
|
|
|
|
2014-01-13 16:31:57 +00:00
|
|
|
def __init__(self, options):
|
|
|
|
loggable.Loggable.__init__(self)
|
|
|
|
self.options = options
|
|
|
|
self._process = None
|
|
|
|
self._logsfile = None
|
|
|
|
|
|
|
|
def _check_is_up(self, timeout=60):
|
|
|
|
""" Check if the server is up, running a simple test based on wget. """
|
|
|
|
start = time.time()
|
|
|
|
while True:
|
|
|
|
try:
|
2016-11-04 21:04:37 +00:00
|
|
|
response = urllib.request.urlopen('http://127.0.0.1:%s' % (
|
2016-03-09 09:05:49 +00:00
|
|
|
self.options.http_server_port))
|
2014-01-13 16:31:57 +00:00
|
|
|
return True
|
2016-11-04 21:04:37 +00:00
|
|
|
except urllib.error.URLError as e:
|
2014-01-13 16:31:57 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
if time.time() - start > timeout:
|
|
|
|
return False
|
|
|
|
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
""" Start the server in a subprocess """
|
2015-01-15 14:32:12 +00:00
|
|
|
self._logsfile = open(os.path.join(self.options.logsdir,
|
|
|
|
"httpserver.logs"), 'w+')
|
2014-01-13 16:31:57 +00:00
|
|
|
if self.options.http_server_dir is not None:
|
|
|
|
if self._check_is_up(timeout=2):
|
|
|
|
return True
|
|
|
|
|
2019-03-17 00:37:37 +00:00
|
|
|
printc("-> Starting HTTP server... ", end='')
|
2014-01-13 16:31:57 +00:00
|
|
|
try:
|
2016-03-08 06:54:32 +00:00
|
|
|
self.debug("Launching http server")
|
2014-08-08 10:33:54 +00:00
|
|
|
cmd = "%s %s %d %s" % (sys.executable, os.path.join(os.path.dirname(__file__),
|
2014-10-24 12:23:52 +00:00
|
|
|
"RangeHTTPServer.py"),
|
|
|
|
self.options.http_server_port,
|
|
|
|
self.options.http_bandwith,
|
|
|
|
)
|
2014-01-13 16:31:57 +00:00
|
|
|
curdir = os.path.abspath(os.curdir)
|
|
|
|
os.chdir(self.options.http_server_dir)
|
2014-10-24 12:23:52 +00:00
|
|
|
# cmd = "twistd -no web --path=%s -p %d" % (
|
|
|
|
# self.options.http_server_dir, self.options.http_server_port)
|
|
|
|
self.debug(
|
2016-03-08 06:54:32 +00:00
|
|
|
"Launching server: %s (logs in %s)", cmd, self._logsfile)
|
2014-01-13 16:31:57 +00:00
|
|
|
self._process = subprocess.Popen(cmd.split(" "),
|
|
|
|
stderr=self._logsfile,
|
|
|
|
stdout=self._logsfile)
|
|
|
|
os.chdir(curdir)
|
2016-03-08 06:54:32 +00:00
|
|
|
self.debug("Launched http server")
|
2014-01-13 16:31:57 +00:00
|
|
|
# Dirty way to avoid eating to much CPU...
|
|
|
|
# good enough for us anyway.
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
if self._check_is_up():
|
2019-03-17 00:37:37 +00:00
|
|
|
printc("OK", Colors.OKGREEN)
|
2014-01-13 16:31:57 +00:00
|
|
|
return True
|
|
|
|
else:
|
2019-03-17 00:37:37 +00:00
|
|
|
printc("FAILURE", Colors.FAIL)
|
2014-01-13 16:31:57 +00:00
|
|
|
self._process.terminate()
|
|
|
|
self._process = None
|
|
|
|
except OSError as ex:
|
2019-03-17 00:37:37 +00:00
|
|
|
printc("FAILURE", Colors.FAIL)
|
2014-01-13 16:31:57 +00:00
|
|
|
self.warning(logcat, "Could not launch server %s" % ex)
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
""" Stop the server subprocess if running. """
|
|
|
|
if self._process:
|
|
|
|
self._process.terminate()
|
|
|
|
self._process = None
|
2015-04-29 12:12:01 +00:00
|
|
|
self.debug("Server stopped")
|