blob: 3362cfd9028b3d80427f2c1486815b726892ba32 [file] [log] [blame]
rsimha@chromium.org99a6f172013-01-20 01:10:24 +00001// Copyright 2013 The Chromium Authors. All rights reserved.
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <stdio.h>
6
7#include "base/at_exit.h"
8#include "base/command_line.h"
brettw@chromium.org9d094992013-02-24 05:40:52 +00009#include "base/files/file_path.h"
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000010#include "base/logging.h"
11#include "base/message_loop.h"
tim@chromium.orgc9ff5422011-06-18 11:53:42 +000012#include "base/test/test_timeouts.h"
darin@chromium.orgcc65c352011-04-15 19:07:49 +000013#include "base/utf_string_conversions.h"
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000014#include "net/test/test_server.h"
15
16static void PrintUsage() {
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +000017 printf("run_testserver --doc-root=relpath\n"
rsimha@chromium.org99a6f172013-01-20 01:10:24 +000018 " [--http|--https|--ws|--wss|--ftp]\n"
19 " [--ssl-cert=ok|mismatched-name|expired]\n");
rsimha@chromium.org6a5525c2012-05-24 20:40:21 +000020 printf("(NOTE: relpath should be relative to the 'src' directory.\n");
rsimha@chromium.org6a5525c2012-05-24 20:40:21 +000021}
22
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000023int main(int argc, const char* argv[]) {
24 base::AtExitManager at_exit_manager;
25 MessageLoopForIO message_loop;
26
27 // Process command line
28 CommandLine::Init(argc, argv);
29 CommandLine* command_line = CommandLine::ForCurrentProcess();
30
akalin@chromium.orgc3062de2011-01-11 01:03:36 +000031 if (!logging::InitLogging(
32 FILE_PATH_LITERAL("testserver.log"),
33 logging::LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG,
34 logging::LOCK_LOG_FILE,
35 logging::APPEND_TO_OLD_LOG_FILE,
36 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS)) {
akalin@chromium.org3dfcd452010-11-18 22:29:38 +000037 printf("Error: could not initialize logging. Exiting.\n");
38 return -1;
39 }
40
tim@chromium.org8d38ca12011-06-23 23:56:38 +000041 TestTimeouts::Initialize();
42
rsimha@chromium.org6a5525c2012-05-24 20:40:21 +000043 if (command_line->GetSwitches().empty() ||
rsimha@chromium.org99a6f172013-01-20 01:10:24 +000044 command_line->HasSwitch("help")) {
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000045 PrintUsage();
46 return -1;
47 }
48
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +000049 net::TestServer::Type server_type;
50 if (command_line->HasSwitch("http")) {
51 server_type = net::TestServer::TYPE_HTTP;
52 } else if (command_line->HasSwitch("https")) {
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000053 server_type = net::TestServer::TYPE_HTTPS;
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +000054 } else if (command_line->HasSwitch("ws")) {
55 server_type = net::TestServer::TYPE_WS;
56 } else if (command_line->HasSwitch("wss")) {
57 server_type = net::TestServer::TYPE_WSS;
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000058 } else if (command_line->HasSwitch("ftp")) {
59 server_type = net::TestServer::TYPE_FTP;
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +000060 } else {
61 // If no scheme switch is specified, select http or https scheme.
62 // TODO(toyoshim): Remove this estimation.
63 if (command_line->HasSwitch("ssl-cert"))
64 server_type = net::TestServer::TYPE_HTTPS;
65 else
66 server_type = net::TestServer::TYPE_HTTP;
akalin@chromium.org154bb132010-11-12 02:20:27 +000067 }
68
toyoshim@chromium.orgb1546ce2012-08-23 01:05:12 +000069 net::TestServer::SSLOptions ssl_options;
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +000070 if (command_line->HasSwitch("ssl-cert")) {
71 if (!net::TestServer::UsingSSL(server_type)) {
72 printf("Error: --ssl-cert is specified on non-secure scheme\n");
73 PrintUsage();
74 return -1;
75 }
76 std::string cert_option = command_line->GetSwitchValueASCII("ssl-cert");
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +000077 if (cert_option == "ok") {
toyoshim@chromium.orgb1546ce2012-08-23 01:05:12 +000078 ssl_options.server_certificate = net::TestServer::SSLOptions::CERT_OK;
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +000079 } else if (cert_option == "mismatched-name") {
toyoshim@chromium.orgb1546ce2012-08-23 01:05:12 +000080 ssl_options.server_certificate =
81 net::TestServer::SSLOptions::CERT_MISMATCHED_NAME;
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +000082 } else if (cert_option == "expired") {
toyoshim@chromium.orgb1546ce2012-08-23 01:05:12 +000083 ssl_options.server_certificate =
84 net::TestServer::SSLOptions::CERT_EXPIRED;
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +000085 } else {
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +000086 printf("Error: --ssl-cert has invalid value %s\n", cert_option.c_str());
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +000087 PrintUsage();
88 return -1;
89 }
90 }
91
brettw@chromium.orgb1ccf592013-02-08 20:40:15 +000092 base::FilePath doc_root = command_line->GetSwitchValuePath("doc-root");
rsimha@chromium.org99a6f172013-01-20 01:10:24 +000093 if (doc_root.empty()) {
akalin@chromium.org154bb132010-11-12 02:20:27 +000094 printf("Error: --doc-root must be specified\n");
95 PrintUsage();
96 return -1;
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000097 }
98
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +000099 scoped_ptr<net::TestServer> test_server;
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +0000100 if (net::TestServer::UsingSSL(server_type)) {
101 test_server.reset(new net::TestServer(server_type, ssl_options, doc_root));
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +0000102 } else {
103 test_server.reset(new net::TestServer(server_type,
104 net::TestServer::kLocalhost,
105 doc_root));
rsimha@chromium.org6a5525c2012-05-24 20:40:21 +0000106 }
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000107
108 if (!test_server->Start()) {
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +0000109 printf("Error: failed to start test server. Exiting.\n");
110 return -1;
111 }
112
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000113 if (!file_util::DirectoryExists(test_server->document_root())) {
darin@chromium.orgcc65c352011-04-15 19:07:49 +0000114 printf("Error: invalid doc root: \"%s\" does not exist!\n",
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000115 UTF16ToUTF8(test_server->document_root().LossyDisplayName()).c_str());
darin@chromium.orgcc65c352011-04-15 19:07:49 +0000116 return -1;
117 }
118
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +0000119 printf("testserver running at %s (type ctrl+c to exit)\n",
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000120 test_server->host_port_pair().ToString().c_str());
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +0000121
122 message_loop.Run();
123 return 0;
124}