1ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved. 2c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Use of this source code is governed by a BSD-style license that can be 3c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// found in the LICENSE file. 4c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 5c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include <stdio.h> 6c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 7c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "base/at_exit.h" 8c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "base/command_line.h" 9201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch#include "base/file_path.h" 10c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "base/logging.h" 11c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "base/message_loop.h" 12ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/utf_string_conversions.h" 133345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#include "net/test/test_server.h" 14c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 15c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochstatic void PrintUsage() { 164a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch printf("run_testserver --doc-root=relpath [--http|--https|--ftp|--sync]\n"); 17c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch printf("(NOTE: relpath should be relative to the 'src' directory)\n"); 18c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 19c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 20c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochint main(int argc, const char* argv[]) { 21c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch base::AtExitManager at_exit_manager; 22c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch MessageLoopForIO message_loop; 23c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 24c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Process command line 25c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch CommandLine::Init(argc, argv); 26c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch CommandLine* command_line = CommandLine::ForCurrentProcess(); 27c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 2872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen if (!logging::InitLogging( 2972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen FILE_PATH_LITERAL("testserver.log"), 3072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen logging::LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG, 3172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen logging::LOCK_LOG_FILE, 3272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen logging::APPEND_TO_OLD_LOG_FILE, 3372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS)) { 34201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch printf("Error: could not initialize logging. Exiting.\n"); 35201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch return -1; 36201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch } 37201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch 38c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch if (command_line->GetSwitchCount() == 0 || 39c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch command_line->HasSwitch("help")) { 40c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch PrintUsage(); 41c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch return -1; 42c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch } 43c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 443345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick net::TestServer::Type server_type(net::TestServer::TYPE_HTTP); 453345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick if (command_line->HasSwitch("https")) { 463345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick server_type = net::TestServer::TYPE_HTTPS; 473345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick } else if (command_line->HasSwitch("ftp")) { 483345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick server_type = net::TestServer::TYPE_FTP; 494a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch } else if (command_line->HasSwitch("sync")) { 504a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch server_type = net::TestServer::TYPE_SYNC; 514a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch } 524a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch 534a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch FilePath doc_root = command_line->GetSwitchValuePath("doc-root"); 544a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch if ((server_type != net::TestServer::TYPE_SYNC) && doc_root.empty()) { 554a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch printf("Error: --doc-root must be specified\n"); 564a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch PrintUsage(); 574a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch return -1; 583345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick } 593345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick 603345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick net::TestServer test_server(server_type, doc_root); 613345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick if (!test_server.Start()) { 623345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick printf("Error: failed to start test server. Exiting.\n"); 633345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick return -1; 64c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch } 65c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 66ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen if (!file_util::DirectoryExists(test_server.document_root())) { 67ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen printf("Error: invalid doc root: \"%s\" does not exist!\n", 68ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen UTF16ToUTF8(test_server.document_root().LossyDisplayName()).c_str()); 69ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen return -1; 70ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen } 71ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen 723345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick printf("testserver running at %s (type ctrl+c to exit)\n", 733345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick test_server.host_port_pair().ToString().c_str()); 74c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 75c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch message_loop.Run(); 76c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch return 0; 77c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 78