Commit 83595270 authored by Glenn Adams's avatar Glenn Adams
Browse files

Update wpt server driver, add pywebsocket.

parent 5207b1b0
......@@ -4,3 +4,6 @@
[submodule "tools/w3c/wptserve"]
path = tools/w3c/wptserve
url = https://github.com/w3c/wptserve.git
[submodule "tools/w3c/pywebsocket"]
path = tools/w3c/pywebsocket
url = https://github.com/w3c/pywebsocket.git
Subproject commit b2e1d11086fdf00b33a0d30c504f227e7d4fa86b
# -*- coding: utf-8 -*-
import sys
import os
import logging
import argparse
import json
import socket
import logging
import os
import signal
import socket
import sys
import threading
from multiprocessing import Process, Event
from collections import defaultdict
import time
import traceback
import urllib2
import uuid
import argparse
import time
from collections import defaultdict
from multiprocessing import Process, Event
def get_repo_root():
file_path = os.path.abspath(__file__)
......@@ -27,14 +28,16 @@ repo_root = get_repo_root()
sys.path.insert(1, os.path.join(repo_root, "tools", "w3c", "wptserve"))
from wptserve import server as wptserve, handlers
from wptserve.router import any_method
#sys.path.insert(1, os.path.join(repo_root, "tools", "w3c", "pywebsocket", "src"))
#from mod_pywebsocket import standalone as pywebsocket
sys.path.insert(1, os.path.join(repo_root, "tools", "w3c", "pywebsocket", "src"))
from mod_pywebsocket import standalone as pywebsocket
routes = [("GET", "/tools/w3c/runner/*", handlers.file_handler),
("GET", "/tools/w3c/resources/*", handlers.file_handler),
("GET", "/tools/common/*", handlers.file_handler),
("POST", "/tools/runner/update_manifest.py", handlers.python_script_handler),
(any_method, "/tools/*", handlers.ErrorHandler(404)),
(any_method, "/specs/*", handlers.ErrorHandler(404)),
(any_method, "/serve.py", handlers.ErrorHandler(404)),
(any_method, "*.py", handlers.python_script_handler),
("GET", "*.asis", handlers.as_is_handler),
("GET", "*", handlers.file_handler),
......@@ -81,16 +84,20 @@ class ServerProc(object):
self.daemon = None
self.stop = Event()
def start(self, init_func, config, paths, port, bind_hostname):
self.proc = Process(target=self.create_daemon, args=(init_func, config, paths, port, bind_hostname))
def start(self, init_func, host, port, paths, bind_hostname, external_config):
self.proc = Process(target=self.create_daemon,
args=(init_func, host, port, paths, bind_hostname, external_config))
self.proc.daemon = True
self.proc.start()
def create_daemon(self, init_func, config, paths, port, bind_hostname):
def create_daemon(self, init_func, host, port, paths, bind_hostname, external_config):
try:
self.daemon = init_func(config, paths, port, bind_hostname)
self.daemon = init_func(host, port, paths, bind_hostname, external_config)
except socket.error:
logger.error("Socket error on port %s" % port)
print >> sys.stderr, "Socket error on port %s" % port
raise
except:
print >> sys.stderr, traceback.format_exc()
raise
if self.daemon:
......@@ -109,22 +116,27 @@ class ServerProc(object):
self.proc.terminate()
self.proc.join()
def check_subdomains(config, paths, subdomains, bind_hostname):
def is_alive(self):
return self.proc.is_alive()
def check_subdomains(host, paths, bind_hostname):
port = get_port()
subdomains = get_subdomains(host)
wrapper = ServerProc()
wrapper.start(start_http_server, config, paths, port, bind_hostname)
wrapper.start(start_http_server, host, port, paths, bind_hostname, None)
connected = False
for i in range(10):
try:
urllib2.urlopen("http://%s:%d/" % (config["host"], port))
urllib2.urlopen("http://%s:%d/" % (host, port))
connected = True
break
except urllib2.URLError:
time.sleep(1)
if not connected:
logger.critical("Failed to connect to test server on http://%s:%s You may need to edit /etc/hosts or similar" % (config["host"], port))
logger.critical("Failed to connect to test server on http://%s:%s You may need to edit /etc/hosts or similar" % (host, port))
sys.exit(1)
for subdomain, (punycode, host) in subdomains.iteritems():
......@@ -137,50 +149,43 @@ def check_subdomains(config, paths, subdomains, bind_hostname):
wrapper.wait()
def get_subdomains(config):
def get_subdomains(host):
#This assumes that the tld is ascii-only or already in punycode
host = config["host"]
return {subdomain: (subdomain.encode("idna"), host)
for subdomain in subdomains}
def start_servers(config, paths, ports, bind_hostname):
def start_servers(host, ports, paths, bind_hostname, external_config):
servers = defaultdict(list)
host = config["host"]
for scheme, ports in ports.iteritems():
assert len(ports) == {"http":2}.get(scheme, 1)
for port in ports:
init_func = {"http":start_http_server,
"https":start_https_server,
# "ws":start_ws_server,
# "wss":start_wss_server
}[scheme]
"ws":start_ws_server,
"wss":start_wss_server}[scheme]
server_proc = ServerProc()
server_proc.start(init_func, config, paths, port, bind_hostname)
logger.info("Started server at %s://%s:%s" % (scheme, config["host"], port))
server_proc.start(init_func, host, port, paths, bind_hostname, external_config)
servers[scheme].append((port, server_proc))
return servers
def start_http_server(config, paths, port, bind_hostname):
return wptserve.WebTestHttpd(host=config["host"],
def start_http_server(host, port, paths, bind_hostname, external_config):
return wptserve.WebTestHttpd(host=host,
port=port,
doc_root=paths["doc_root"],
routes=routes,
rewrites=rewrites,
bind_hostname=bind_hostname,
config=config,
config=external_config,
use_ssl=False,
certificate=None)
def start_https_server(config, paths, port, bind_hostname):
def start_https_server(host, port, paths, bind_hostname):
return
'''
class WebSocketDaemon(object):
def __init__(self, host, port, doc_root, handlers_root, log_level, bind_hostname):
self.host = host
......@@ -201,7 +206,6 @@ class WebSocketDaemon(object):
self.server_thread = None
def start(self, block=False):
logger.info("Starting websockets server on %s:%s" % (self.host, self.port))
self.started = True
if block:
self.server.serve_forever()
......@@ -222,23 +226,21 @@ class WebSocketDaemon(object):
self.server.server_close()
self.server_thread.join()
self.server_thread = None
logger.info("Stopped websockets server on %s:%s" % (self.host, self.port))
except AttributeError:
pass
self.started = False
self.server = None
def start_ws_server(config, paths, port, bind_hostname):
return WebSocketDaemon(config["host"],
def start_ws_server(host, port, paths, bind_hostname, external_config):
return WebSocketDaemon(host,
str(port),
repo_root,
paths["ws_doc_root"],
"debug",
bind_hostname)
def start_wss_server(config, paths, port, bind_hostname):
def start_wss_server(host, port, path, bind_hostname, external_config):
return
''' and None
def get_ports(config):
rv = defaultdict(list)
......@@ -251,39 +253,40 @@ def get_ports(config):
rv[scheme].append(port)
return rv
def normalise_config(config, domains, ports):
ports_ = {}
for scheme, ports_used in ports.iteritems():
ports_[scheme] = ports_used
def normalise_config(config, ports):
host = config["external_host"] if config["external_host"] else config["host"]
domains = get_subdomains(host)
domains_ = domains.copy()
for key, value in domains.iteritems():
domains[key] = ".".join(value)
for key, value in domains_.iteritems():
domains_[key] = ".".join(value)
domains[""] = host
domains_[""] = config["host"]
ports_ = {}
for scheme, ports_used in ports.iteritems():
ports_[scheme] = ports_used
return {"host":config["host"],
"domains":domains_,
return {"host": host,
"domains": domains,
"ports": ports_}
def start(config):
host = config["host"]
domains = get_subdomains(host)
ports = get_ports(config)
domains = get_subdomains(config)
bind_hostname = config["bind_hostname"]
paths = {"doc_root": config["doc_root"],
# "ws_doc_root": config["ws_doc_root"]
}
"ws_doc_root": config["ws_doc_root"]}
if config["check_subdomains"]:
check_subdomains(config, paths, domains, bind_hostname)
check_subdomains(host, paths, bind_hostname)
config_ = normalise_config(config, domains, ports)
external_config = normalise_config(config, ports)
servers = start_servers(config_, paths, ports, bind_hostname)
servers = start_servers(host, ports, paths, bind_hostname, external_config)
return config_, servers
return external_config, servers
def iter_procs(servers):
......@@ -295,14 +298,12 @@ def value_set(config, key):
return key in config and config[key] is not None
def set_computed_defaults(config):
'''
if not value_set(config, "ws_doc_root"):
if value_set(config, "doc_root"):
root = config["doc_root"]
else:
root = repo_root
config["ws_doc_root"] = os.path.join(repo_root, "websockets", "handlers")
''' and None
if not value_set(config, "doc_root"):
config["doc_root"] = repo_root
......@@ -340,7 +341,8 @@ def load_config(default_path, override_path=None):
def main():
global logger
config = load_config(os.path.join(os.path.abspath(os.path.split(__file__)[0]), "server.config.default.json"), "server.config.json")
config = load_config(os.path.join(os.path.abspath(os.path.split(__file__)[0]), "server.config.default.json"),
"server.config.json")
logger = default_logger(config["log_level"])
......
{
"host": "web-platform.test",
"ports": {
"http": [ 8000, "auto" ],
"https": [ "auto" ]
},
"check_subdomains": true,
"log_level": "debug",
"bind_hostname": true
}
{"host": "web-platform.test",
"external_host": null,
"ports":{"http":[8000, "auto"],
"https":[],
"ws":["auto"]},
"check_subdomains": true,
"log_level":"debug",
"bind_hostname": true}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment