12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "net/test/spawned_test_server/local_test_server.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/json/json_reader.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/path_service.h"
11a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch#include "base/process/kill.h"
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string_number_conversions.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/values.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/host_port_pair.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_errors.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/test/python_utils.h"
177dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "url/gurl.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net {
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool AppendArgumentFromJSONValue(const std::string& key,
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 const base::Value& value_node,
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                 base::CommandLine* command_line) {
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string argument_name = "--" + key;
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (value_node.GetType()) {
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case base::Value::TYPE_NULL:
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      command_line->AppendArg(argument_name);
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case base::Value::TYPE_INTEGER: {
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      int value;
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      bool result = value_node.GetAsInteger(&value);
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      DCHECK(result);
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      command_line->AppendArg(argument_name + "=" + base::IntToString(value));
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    case base::Value::TYPE_STRING: {
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      std::string value;
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      bool result = value_node.GetAsString(&value);
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (!result || value.empty())
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return false;
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      command_line->AppendArg(argument_name + "=" + value);
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case base::Value::TYPE_BOOLEAN:
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case base::Value::TYPE_DOUBLE:
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case base::Value::TYPE_LIST:
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case base::Value::TYPE_DICTIONARY:
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case base::Value::TYPE_BINARY:
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default:
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NOTREACHED() << "improper json type";
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return false;
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)LocalTestServer::LocalTestServer(Type type,
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 const std::string& host,
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                 const base::FilePath& document_root)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : BaseTestServer(type, host) {
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!Init(document_root))
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NOTREACHED();
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)LocalTestServer::LocalTestServer(Type type,
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 const SSLOptions& ssl_options,
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                 const base::FilePath& document_root)
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : BaseTestServer(type, ssl_options) {
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!Init(document_root))
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NOTREACHED();
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)LocalTestServer::~LocalTestServer() {
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Stop();
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool LocalTestServer::GetTestServerPath(base::FilePath* testserver_path) const {
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath testserver_dir;
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!PathService::Get(base::DIR_SOURCE_ROOT, &testserver_dir)) {
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(ERROR) << "Failed to get DIR_SOURCE_ROOT";
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  testserver_dir = testserver_dir.Append(FILE_PATH_LITERAL("net"))
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                 .Append(FILE_PATH_LITERAL("tools"))
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                 .Append(FILE_PATH_LITERAL("testserver"));
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  *testserver_path = testserver_dir.Append(FILE_PATH_LITERAL("testserver.py"));
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool LocalTestServer::Start() {
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return StartInBackground() && BlockUntilStarted();
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool LocalTestServer::StartInBackground() {
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get path to Python server script.
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath testserver_path;
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!GetTestServerPath(&testserver_path))
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!SetPythonPath())
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!LaunchPython(testserver_path))
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return true;
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool LocalTestServer::BlockUntilStarted() {
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!WaitToStart()) {
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Stop();
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return SetupWhenServerStarted();
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool LocalTestServer::Stop() {
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CleanUpWhenStoppingServer();
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!process_handle_)
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return true;
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // First check if the process has already terminated.
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool ret = base::WaitForSingleProcess(process_handle_, base::TimeDelta());
129a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (!ret) {
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ret = base::KillProcess(process_handle_, 1, true);
131a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (ret) {
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::CloseProcessHandle(process_handle_);
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    process_handle_ = base::kNullProcessHandle;
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    VLOG(1) << "Kill failed?";
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return ret;
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool LocalTestServer::Init(const base::FilePath& document_root) {
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (document_root.IsAbsolute())
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // At this point, the port that the test server will listen on is unknown.
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The test server will listen on an ephemeral port, and write the port
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // number out over a pipe that this TestServer object will read from. Once
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // that is complete, the host port pair will contain the actual port.
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(!GetPort());
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  process_handle_ = base::kNullProcessHandle;
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath src_dir;
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!PathService::Get(base::DIR_SOURCE_ROOT, &src_dir))
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetResourcePath(src_dir.Append(document_root),
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  src_dir.AppendASCII("net")
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         .AppendASCII("data")
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         .AppendASCII("ssl")
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         .AppendASCII("certificates"));
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool LocalTestServer::SetPythonPath() const {
1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath third_party_dir;
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!PathService::Get(base::DIR_SOURCE_ROOT, &third_party_dir)) {
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(ERROR) << "Failed to get DIR_SOURCE_ROOT";
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  third_party_dir = third_party_dir.AppendASCII("third_party");
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // For simplejson. (simplejson, unlike all the other Python modules
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // we include, doesn't have an extra 'simplejson' directory, so we
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // need to include its parent directory, i.e. third_party_dir).
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AppendToPythonPath(third_party_dir);
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AppendToPythonPath(third_party_dir.AppendASCII("tlslite"));
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AppendToPythonPath(
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      third_party_dir.AppendASCII("pyftpdlib").AppendASCII("src"));
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AppendToPythonPath(
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      third_party_dir.AppendASCII("pywebsocket").AppendASCII("src"));
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Locate the Python code generated by the protocol buffers compiler.
1852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath pyproto_dir;
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!GetPyProtoPath(&pyproto_dir)) {
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(WARNING) << "Cannot find pyproto dir for generated code. "
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 << "Testserver features that rely on it will not work";
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return true;
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AppendToPythonPath(pyproto_dir);
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
196a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool LocalTestServer::AddCommandLineArguments(
197a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    base::CommandLine* command_line) const {
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::DictionaryValue arguments_dict;
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!GenerateArguments(&arguments_dict))
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Serialize the argument dictionary into CommandLine.
2037d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  for (base::DictionaryValue::Iterator it(arguments_dict); !it.IsAtEnd();
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       it.Advance()) {
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const base::Value& value = it.value();
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& key = it.key();
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Add arguments from a list.
2095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (value.IsType(base::Value::TYPE_LIST)) {
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const base::ListValue* list = NULL;
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (!value.GetAsList(&list) || !list || list->empty())
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return false;
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      for (base::ListValue::const_iterator list_it = list->begin();
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           list_it != list->end(); ++list_it) {
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (!AppendArgumentFromJSONValue(key, *(*list_it), command_line))
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          return false;
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else if (!AppendArgumentFromJSONValue(key, value, command_line)) {
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return false;
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Append the appropriate server type argument.
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (type()) {
2252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case TYPE_HTTP:  // The default type is HTTP, no argument required.
2262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      break;
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case TYPE_HTTPS:
2282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      command_line->AppendArg("--https");
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case TYPE_WS:
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case TYPE_WSS:
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      command_line->AppendArg("--websocket");
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case TYPE_FTP:
2352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      command_line->AppendArg("--ftp");
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case TYPE_TCP_ECHO:
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      command_line->AppendArg("--tcp-echo");
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case TYPE_UDP_ECHO:
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      command_line->AppendArg("--udp-echo");
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case TYPE_BASIC_AUTH_PROXY:
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      command_line->AppendArg("--basic-auth-proxy");
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default:
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NOTREACHED();
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return false;
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace net
255