diff options
author | toyoshim@chromium.org <toyoshim@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-09 03:43:19 +0000 |
---|---|---|
committer | toyoshim@chromium.org <toyoshim@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-09 03:43:19 +0000 |
commit | 7f06df0de9f7d7ee95d8205be4cff7a7617813e7 (patch) | |
tree | beb4045b625bcaaa0c8d3054e63826e75a0ff7d0 | |
parent | d61d742a8f0725dcf73465c1dae850af073db071 (diff) | |
download | chromium_src-7f06df0de9f7d7ee95d8205be4cff7a7617813e7.zip chromium_src-7f06df0de9f7d7ee95d8205be4cff7a7617813e7.tar.gz chromium_src-7f06df0de9f7d7ee95d8205be4cff7a7617813e7.tar.bz2 |
Launch pywebsocket via net::TestServer
BUG=137639
Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=156742
Review URL: https://chromiumcodereview.appspot.com/10879029
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@160782 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | net/test/base_test_server.cc | 7 | ||||
-rw-r--r-- | net/test/base_test_server.h | 7 | ||||
-rw-r--r-- | net/test/local_test_server.cc | 4 | ||||
-rw-r--r-- | net/test/remote_test_server.cc | 3 | ||||
-rw-r--r-- | net/tools/testserver/run_testserver.cc | 71 | ||||
-rwxr-xr-x | net/tools/testserver/testserver.py | 43 |
6 files changed, 99 insertions, 36 deletions
diff --git a/net/test/base_test_server.cc b/net/test/base_test_server.cc index a4d1ff9..e672f19 100644 --- a/net/test/base_test_server.cc +++ b/net/test/base_test_server.cc @@ -29,8 +29,7 @@ namespace { std::string GetHostname(BaseTestServer::Type type, const BaseTestServer::SSLOptions& options) { - if ((type == BaseTestServer::TYPE_HTTPS || - type == BaseTestServer::TYPE_WSS) && + if (BaseTestServer::UsingSSL(type) && options.server_certificate == BaseTestServer::SSLOptions::CERT_MISMATCHED_NAME) { // Return a different hostname string that resolves to the same hostname. @@ -127,7 +126,7 @@ BaseTestServer::BaseTestServer(Type type, const SSLOptions& ssl_options) type_(type), started_(false), log_to_console_(false) { - DCHECK(type == TYPE_HTTPS || type == TYPE_WSS); + DCHECK(UsingSSL(type)); Init(GetHostname(type, ssl_options)); } @@ -309,7 +308,7 @@ bool BaseTestServer::LoadTestRootCert() const { bool BaseTestServer::SetupWhenServerStarted() { DCHECK(host_port_pair_.port()); - if ((type_ == TYPE_HTTPS || type_ == TYPE_WSS) && !LoadTestRootCert()) + if (UsingSSL(type_) && !LoadTestRootCert()) return false; started_ = true; diff --git a/net/test/base_test_server.h b/net/test/base_test_server.h index 173abf7..9f1290e 100644 --- a/net/test/base_test_server.h +++ b/net/test/base_test_server.h @@ -30,6 +30,8 @@ class BaseTestServer { public: typedef std::pair<std::string, std::string> StringPair; + // Following types represent protocol schemes. See also + // http://www.iana.org/assignments/uri-schemes.html enum Type { TYPE_BASIC_AUTH_PROXY, TYPE_FTP, @@ -180,6 +182,11 @@ class BaseTestServer { const std::vector<StringPair>& text_to_replace, std::string* replacement_path); + static bool UsingSSL(Type type) { + return type == BaseTestServer::TYPE_HTTPS || + type == BaseTestServer::TYPE_WSS; + } + protected: virtual ~BaseTestServer(); Type type() const { return type_; } diff --git a/net/test/local_test_server.cc b/net/test/local_test_server.cc index af94a50..17137a69 100644 --- a/net/test/local_test_server.cc +++ b/net/test/local_test_server.cc @@ -176,6 +176,8 @@ bool LocalTestServer::SetPythonPath() { AppendToPythonPath(third_party_dir.AppendASCII("tlslite")); AppendToPythonPath( third_party_dir.AppendASCII("pyftpdlib").AppendASCII("src")); + AppendToPythonPath( + third_party_dir.AppendASCII("pywebsocket").AppendASCII("src")); // Locate the Python code generated by the protocol buffers compiler. FilePath pyproto_dir; @@ -229,7 +231,7 @@ bool LocalTestServer::AddCommandLineArguments(CommandLine* command_line) const { break; case TYPE_WS: case TYPE_WSS: - // TODO(toyoshim): Handle correctly. See, http://crbug.com/137639 . + command_line->AppendArg("--websocket"); break; case TYPE_FTP: command_line->AppendArg("-f"); diff --git a/net/test/remote_test_server.cc b/net/test/remote_test_server.cc index 7532945..5020ca7 100644 --- a/net/test/remote_test_server.cc +++ b/net/test/remote_test_server.cc @@ -50,6 +50,9 @@ std::string GetServerTypeString(BaseTestServer::Type type) { case BaseTestServer::TYPE_HTTP: case BaseTestServer::TYPE_HTTPS: return "http"; + case BaseTestServer::TYPE_WS: + case BaseTestServer::TYPE_WSS: + return "ws"; case BaseTestServer::TYPE_SYNC: return "sync"; case BaseTestServer::TYPE_TCP_ECHO: diff --git a/net/tools/testserver/run_testserver.cc b/net/tools/testserver/run_testserver.cc index 9ad0f7d..58abe34 100644 --- a/net/tools/testserver/run_testserver.cc +++ b/net/tools/testserver/run_testserver.cc @@ -18,8 +18,9 @@ #include "net/test/test_server.h" static void PrintUsage() { - printf("run_testserver --doc-root=relpath [--http|--https|--ftp|--sync]\n" - " [--https-cert=ok|mismatched-name|expired]\n" + printf("run_testserver --doc-root=relpath\n" + " [--http|--https|--ws|--wss|--ftp|--sync]\n" + " [--ssl-cert=ok|mismatched-name|expired]\n" " [--port=<port>] [--xmpp-port=<xmpp_port>]\n"); printf("(NOTE: relpath should be relative to the 'src' directory.\n"); printf(" --port and --xmpp-port only work with the --sync flag.)\n"); @@ -101,21 +102,38 @@ int main(int argc, const char* argv[]) { return -1; } - net::TestServer::Type server_type(net::TestServer::TYPE_HTTP); - if (command_line->HasSwitch("https")) { + net::TestServer::Type server_type; + if (command_line->HasSwitch("http")) { + server_type = net::TestServer::TYPE_HTTP; + } else if (command_line->HasSwitch("https")) { server_type = net::TestServer::TYPE_HTTPS; + } else if (command_line->HasSwitch("ws")) { + server_type = net::TestServer::TYPE_WS; + } else if (command_line->HasSwitch("wss")) { + server_type = net::TestServer::TYPE_WSS; } else if (command_line->HasSwitch("ftp")) { server_type = net::TestServer::TYPE_FTP; } else if (command_line->HasSwitch("sync")) { server_type = net::TestServer::TYPE_SYNC; } else if (command_line->HasSwitch("sync-test")) { return RunSyncTest() ? 0 : -1; + } else { + // If no scheme switch is specified, select http or https scheme. + // TODO(toyoshim): Remove this estimation. + if (command_line->HasSwitch("ssl-cert")) + server_type = net::TestServer::TYPE_HTTPS; + else + server_type = net::TestServer::TYPE_HTTP; } net::TestServer::SSLOptions ssl_options; - if (command_line->HasSwitch("https-cert")) { - server_type = net::TestServer::TYPE_HTTPS; - std::string cert_option = command_line->GetSwitchValueASCII("https-cert"); + if (command_line->HasSwitch("ssl-cert")) { + if (!net::TestServer::UsingSSL(server_type)) { + printf("Error: --ssl-cert is specified on non-secure scheme\n"); + PrintUsage(); + return -1; + } + std::string cert_option = command_line->GetSwitchValueASCII("ssl-cert"); if (cert_option == "ok") { ssl_options.server_certificate = net::TestServer::SSLOptions::CERT_OK; } else if (cert_option == "mismatched-name") { @@ -125,7 +143,7 @@ int main(int argc, const char* argv[]) { ssl_options.server_certificate = net::TestServer::SSLOptions::CERT_EXPIRED; } else { - printf("Error: --https-cert has invalid value %s\n", cert_option.c_str()); + printf("Error: --ssl-cert has invalid value %s\n", cert_option.c_str()); PrintUsage(); return -1; } @@ -139,30 +157,21 @@ int main(int argc, const char* argv[]) { } scoped_ptr<net::TestServer> test_server; - switch (server_type) { - case net::TestServer::TYPE_HTTPS: { - test_server.reset(new net::TestServer(server_type, - ssl_options, - doc_root)); - break; - } - case net::TestServer::TYPE_SYNC: { - uint16 port = 0; - uint16 xmpp_port = 0; - if (!GetPortFromSwitch("port", &port) || - !GetPortFromSwitch("xmpp-port", &xmpp_port)) { - printf("Error: Could not extract --port and/or --xmpp-port.\n"); - return -1; - } - test_server.reset(new net::LocalSyncTestServer(port, xmpp_port)); - break; - } - default: { - test_server.reset(new net::TestServer(server_type, - net::TestServer::kLocalhost, - doc_root)); - break; + if (net::TestServer::UsingSSL(server_type)) { + test_server.reset(new net::TestServer(server_type, ssl_options, doc_root)); + } else if (server_type == net::TestServer::TYPE_SYNC) { + uint16 port = 0; + uint16 xmpp_port = 0; + if (!GetPortFromSwitch("port", &port) || + !GetPortFromSwitch("xmpp-port", &xmpp_port)) { + printf("Error: Could not extract --port and/or --xmpp-port.\n"); + return -1; } + test_server.reset(new net::LocalSyncTestServer(port, xmpp_port)); + } else { + test_server.reset(new net::TestServer(server_type, + net::TestServer::kLocalhost, + doc_root)); } if (!test_server->Start()) { diff --git a/net/tools/testserver/testserver.py b/net/tools/testserver/testserver.py index aeb0575..55bca1e 100755 --- a/net/tools/testserver/testserver.py +++ b/net/tools/testserver/testserver.py @@ -21,6 +21,7 @@ import errno import hashlib import httplib import json +import logging import minica import optparse import os @@ -42,6 +43,7 @@ import zlib warnings.filterwarnings("ignore", category=DeprecationWarning) import echo_message +from mod_pywebsocket.standalone import WebSocketServer import pyftpdlib.ftpserver import tlslite import tlslite.api @@ -55,6 +57,10 @@ SERVER_SYNC = 2 SERVER_TCP_ECHO = 3 SERVER_UDP_ECHO = 4 SERVER_BASIC_AUTH_PROXY = 5 +SERVER_WEBSOCKET = 6 + +# Default request queue size for WebSocketServer. +_DEFAULT_REQUEST_QUEUE_SIZE = 128 # Using debug() seems to cause hangs on XP: see http://crbug.com/64515 . debug_output = sys.stderr @@ -62,6 +68,29 @@ def debug(str): debug_output.write(str + "\n") debug_output.flush() +class WebSocketOptions: + """Holds options for WebSocketServer.""" + + def __init__(self, host, port, data_dir): + self.request_queue_size = _DEFAULT_REQUEST_QUEUE_SIZE + self.server_host = host + self.port = port + self.websock_handlers = data_dir + self.scan_dir = None + self.allow_handlers_outside_root_dir = False + self.websock_handlers_map_file = None + self.cgi_directories = [] + self.is_executable_method = None + self.allow_draft75 = False + self.strict = True + + # TODO(toyoshim): Support SSL and authenticates (http://crbug.com/137639) + self.use_tls = False + self.private_key = None + self.certificate = None + self.tls_client_ca = None + self.use_basic_auth = False + class RecordingSSLSessionCache(object): """RecordingSSLSessionCache acts as a TLS session cache and maintains a log of lookups and inserts in order to test session cache behaviours.""" @@ -2216,6 +2245,16 @@ def main(options, args): server.policy_keys = options.policy_keys server.policy_user = options.policy_user server.gdata_auth_token = options.auth_token + elif options.server_type == SERVER_WEBSOCKET: + # Launch pywebsocket via WebSocketServer. + logger = logging.getLogger() + logger.addHandler(logging.StreamHandler()) + # TODO(toyoshim): Remove following os.chdir. Currently this operation + # is required to work correctly. It should be fixed from pywebsocket side. + os.chdir(MakeDataDir()) + server = WebSocketServer(WebSocketOptions(host, port, MakeDataDir())) + print 'WebSocket server started on %s:%d...' % (host, server.server_port) + server_data['port'] = server.server_port elif options.server_type == SERVER_SYNC: xmpp_port = options.xmpp_port server = SyncHTTPServer((host, port), xmpp_port, SyncPageHandler) @@ -2320,6 +2359,10 @@ if __name__ == '__main__': dest='server_type', help='start up a proxy server which requires basic ' 'authentication.') + option_parser.add_option('', '--websocket', action='store_const', + const=SERVER_WEBSOCKET, default=SERVER_HTTP, + dest='server_type', + help='start up a WebSocket server.') option_parser.add_option('', '--log-to-console', action='store_const', const=True, default=False, dest='log_to_console', |