blob: f9a70ba59e5e3e18c64e6cdaf422cf1a96b7cd02 [file] [log] [blame]
darin@chromium.orgcc65c352011-04-15 19:07:49 +00001// Copyright (c) 2011 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"
akalin@chromium.org3dfcd452010-11-18 22:29:38 +00009#include "base/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() {
akalin@chromium.org154bb132010-11-12 02:20:27 +000017 printf("run_testserver --doc-root=relpath [--http|--https|--ftp|--sync]\n");
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000018 printf("(NOTE: relpath should be relative to the 'src' directory)\n");
19}
20
21int main(int argc, const char* argv[]) {
22 base::AtExitManager at_exit_manager;
23 MessageLoopForIO message_loop;
24
25 // Process command line
26 CommandLine::Init(argc, argv);
27 CommandLine* command_line = CommandLine::ForCurrentProcess();
28
akalin@chromium.orgc3062de2011-01-11 01:03:36 +000029 if (!logging::InitLogging(
30 FILE_PATH_LITERAL("testserver.log"),
31 logging::LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG,
32 logging::LOCK_LOG_FILE,
33 logging::APPEND_TO_OLD_LOG_FILE,
34 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS)) {
akalin@chromium.org3dfcd452010-11-18 22:29:38 +000035 printf("Error: could not initialize logging. Exiting.\n");
36 return -1;
37 }
38
tim@chromium.org8d38ca12011-06-23 23:56:38 +000039 TestTimeouts::Initialize();
40
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000041 if (command_line->GetSwitchCount() == 0 ||
42 command_line->HasSwitch("help")) {
43 PrintUsage();
44 return -1;
45 }
46
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000047 net::TestServer::Type server_type(net::TestServer::TYPE_HTTP);
48 if (command_line->HasSwitch("https")) {
49 server_type = net::TestServer::TYPE_HTTPS;
50 } else if (command_line->HasSwitch("ftp")) {
51 server_type = net::TestServer::TYPE_FTP;
akalin@chromium.org154bb132010-11-12 02:20:27 +000052 } else if (command_line->HasSwitch("sync")) {
53 server_type = net::TestServer::TYPE_SYNC;
54 }
55
56 FilePath doc_root = command_line->GetSwitchValuePath("doc-root");
57 if ((server_type != net::TestServer::TYPE_SYNC) && doc_root.empty()) {
58 printf("Error: --doc-root must be specified\n");
59 PrintUsage();
60 return -1;
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000061 }
62
63 net::TestServer test_server(server_type, doc_root);
64 if (!test_server.Start()) {
65 printf("Error: failed to start test server. Exiting.\n");
66 return -1;
67 }
68
darin@chromium.orgcc65c352011-04-15 19:07:49 +000069 if (!file_util::DirectoryExists(test_server.document_root())) {
70 printf("Error: invalid doc root: \"%s\" does not exist!\n",
71 UTF16ToUTF8(test_server.document_root().LossyDisplayName()).c_str());
72 return -1;
73 }
74
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000075 printf("testserver running at %s (type ctrl+c to exit)\n",
76 test_server.host_port_pair().ToString().c_str());
77
78 message_loop.Run();
79 return 0;
80}