run_testserver.cc revision 4a5e2dc747d50c653511c68ccb2cfbfb740bd5a7
1// Copyright (c) 2010 The Chromium Authors. All rights reserved.
2// 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"
9#include "base/logging.h"
10#include "base/message_loop.h"
11#include "net/test/test_server.h"
12
13static void PrintUsage() {
14  printf("run_testserver --doc-root=relpath [--http|--https|--ftp|--sync]\n");
15  printf("(NOTE: relpath should be relative to the 'src' directory)\n");
16}
17
18int main(int argc, const char* argv[]) {
19  base::AtExitManager at_exit_manager;
20  MessageLoopForIO message_loop;
21
22  // Process command line
23  CommandLine::Init(argc, argv);
24  CommandLine* command_line = CommandLine::ForCurrentProcess();
25
26  if (command_line->GetSwitchCount() == 0 ||
27      command_line->HasSwitch("help")) {
28    PrintUsage();
29    return -1;
30  }
31
32  net::TestServer::Type server_type(net::TestServer::TYPE_HTTP);
33  if (command_line->HasSwitch("https")) {
34    server_type = net::TestServer::TYPE_HTTPS;
35  } else if (command_line->HasSwitch("ftp")) {
36    server_type = net::TestServer::TYPE_FTP;
37  } else if (command_line->HasSwitch("sync")) {
38    server_type = net::TestServer::TYPE_SYNC;
39  }
40
41  FilePath doc_root = command_line->GetSwitchValuePath("doc-root");
42  if ((server_type != net::TestServer::TYPE_SYNC) && doc_root.empty()) {
43    printf("Error: --doc-root must be specified\n");
44    PrintUsage();
45    return -1;
46  }
47
48  net::TestServer test_server(server_type, doc_root);
49  if (!test_server.Start()) {
50    printf("Error: failed to start test server. Exiting.\n");
51    return -1;
52  }
53
54  printf("testserver running at %s (type ctrl+c to exit)\n",
55         test_server.host_port_pair().ToString().c_str());
56
57  message_loop.Run();
58  return 0;
59}
60