Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stdio.h> | 5 #include <stdio.h> |
| 6 #include <locale> | 6 #include <locale> |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/at_exit.h" | 10 #include "base/at_exit.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 45 HttpRequestHandlerFunc; | 45 HttpRequestHandlerFunc; |
| 46 | 46 |
| 47 class HttpServer : public net::HttpServer::Delegate { | 47 class HttpServer : public net::HttpServer::Delegate { |
| 48 public: | 48 public: |
| 49 explicit HttpServer(const HttpRequestHandlerFunc& handle_request_func) | 49 explicit HttpServer(const HttpRequestHandlerFunc& handle_request_func) |
| 50 : handle_request_func_(handle_request_func), | 50 : handle_request_func_(handle_request_func), |
| 51 weak_factory_(this) {} | 51 weak_factory_(this) {} |
| 52 | 52 |
| 53 virtual ~HttpServer() {} | 53 virtual ~HttpServer() {} |
| 54 | 54 |
| 55 bool Start(int port, bool allow_remote) { | 55 bool Start(uint16 port, bool allow_remote) { |
| 56 std::string binding_ip = kLocalHostAddress; | 56 std::string binding_ip = kLocalHostAddress; |
| 57 if (allow_remote) | 57 if (allow_remote) |
| 58 binding_ip = "0.0.0.0"; | 58 binding_ip = "0.0.0.0"; |
| 59 scoped_ptr<net::ServerSocket> server_socket( | 59 scoped_ptr<net::ServerSocket> server_socket( |
| 60 new net::TCPServerSocket(NULL, net::NetLog::Source())); | 60 new net::TCPServerSocket(NULL, net::NetLog::Source())); |
| 61 server_socket->ListenWithAddressAndPort(binding_ip, port, 1); | 61 server_socket->ListenWithAddressAndPort(binding_ip, port, 1); |
| 62 server_.reset(new net::HttpServer(server_socket.Pass(), this)); | 62 server_.reset(new net::HttpServer(server_socket.Pass(), this)); |
| 63 net::IPEndPoint address; | 63 net::IPEndPoint address; |
| 64 return server_->GetLocalAddress(&address) == net::OK; | 64 return server_->GetLocalAddress(&address) == net::OK; |
| 65 } | 65 } |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 147 base::LazyInstance<base::ThreadLocalPointer<HttpServer> > | 147 base::LazyInstance<base::ThreadLocalPointer<HttpServer> > |
| 148 lazy_tls_server = LAZY_INSTANCE_INITIALIZER; | 148 lazy_tls_server = LAZY_INSTANCE_INITIALIZER; |
| 149 | 149 |
| 150 void StopServerOnIOThread() { | 150 void StopServerOnIOThread() { |
| 151 // Note, |server| may be NULL. | 151 // Note, |server| may be NULL. |
| 152 HttpServer* server = lazy_tls_server.Pointer()->Get(); | 152 HttpServer* server = lazy_tls_server.Pointer()->Get(); |
| 153 lazy_tls_server.Pointer()->Set(NULL); | 153 lazy_tls_server.Pointer()->Set(NULL); |
| 154 delete server; | 154 delete server; |
| 155 } | 155 } |
| 156 | 156 |
| 157 void StartServerOnIOThread(int port, | 157 void StartServerOnIOThread(uint16 port, |
| 158 bool allow_remote, | 158 bool allow_remote, |
| 159 const HttpRequestHandlerFunc& handle_request_func) { | 159 const HttpRequestHandlerFunc& handle_request_func) { |
| 160 scoped_ptr<HttpServer> temp_server(new HttpServer(handle_request_func)); | 160 scoped_ptr<HttpServer> temp_server(new HttpServer(handle_request_func)); |
| 161 if (!temp_server->Start(port, allow_remote)) { | 161 if (!temp_server->Start(port, allow_remote)) { |
| 162 printf("Port not available. Exiting...\n"); | 162 printf("Port not available. Exiting...\n"); |
| 163 exit(1); | 163 exit(1); |
| 164 } | 164 } |
| 165 lazy_tls_server.Pointer()->Set(temp_server.release()); | 165 lazy_tls_server.Pointer()->Set(temp_server.release()); |
| 166 } | 166 } |
| 167 | 167 |
| 168 void RunServer(int port, | 168 void RunServer(uint16 port, |
| 169 bool allow_remote, | 169 bool allow_remote, |
| 170 const std::vector<std::string>& whitelisted_ips, | 170 const std::vector<std::string>& whitelisted_ips, |
| 171 const std::string& url_base, | 171 const std::string& url_base, |
| 172 int adb_port, | 172 int adb_port, |
| 173 scoped_ptr<PortServer> port_server) { | 173 scoped_ptr<PortServer> port_server) { |
| 174 base::Thread io_thread("ChromeDriver IO"); | 174 base::Thread io_thread("ChromeDriver IO"); |
| 175 CHECK(io_thread.StartWithOptions( | 175 CHECK(io_thread.StartWithOptions( |
| 176 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | 176 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
| 177 | 177 |
| 178 base::MessageLoop cmd_loop; | 178 base::MessageLoop cmd_loop; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 212 CommandLine* cmd_line = CommandLine::ForCurrentProcess(); | 212 CommandLine* cmd_line = CommandLine::ForCurrentProcess(); |
| 213 | 213 |
| 214 #if defined(OS_LINUX) | 214 #if defined(OS_LINUX) |
| 215 // Select the locale from the environment by passing an empty string instead | 215 // Select the locale from the environment by passing an empty string instead |
| 216 // of the default "C" locale. This is particularly needed for the keycode | 216 // of the default "C" locale. This is particularly needed for the keycode |
| 217 // conversion code to work. | 217 // conversion code to work. |
| 218 setlocale(LC_ALL, ""); | 218 setlocale(LC_ALL, ""); |
| 219 #endif | 219 #endif |
| 220 | 220 |
| 221 // Parse command line flags. | 221 // Parse command line flags. |
| 222 int port = 9515; | 222 uint16 port = 9515; |
| 223 int adb_port = 5037; | 223 int adb_port = 5037; |
| 224 bool allow_remote = false; | 224 bool allow_remote = false; |
| 225 std::vector<std::string> whitelisted_ips; | 225 std::vector<std::string> whitelisted_ips; |
| 226 std::string url_base; | 226 std::string url_base; |
| 227 scoped_ptr<PortServer> port_server; | 227 scoped_ptr<PortServer> port_server; |
| 228 if (cmd_line->HasSwitch("h") || cmd_line->HasSwitch("help")) { | 228 if (cmd_line->HasSwitch("h") || cmd_line->HasSwitch("help")) { |
| 229 std::string options; | 229 std::string options; |
| 230 const char* const kOptionAndDescriptions[] = { | 230 const char* const kOptionAndDescriptions[] = { |
| 231 "port=PORT", "port to listen on", | 231 "port=PORT", "port to listen on", |
| 232 "adb-port=PORT", "adb server port", | 232 "adb-port=PORT", "adb server port", |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 246 kOptionAndDescriptions[i], kOptionAndDescriptions[i + 1]); | 246 kOptionAndDescriptions[i], kOptionAndDescriptions[i + 1]); |
| 247 } | 247 } |
| 248 printf("Usage: %s [OPTIONS]\n\nOptions\n%s", argv[0], options.c_str()); | 248 printf("Usage: %s [OPTIONS]\n\nOptions\n%s", argv[0], options.c_str()); |
| 249 return 0; | 249 return 0; |
| 250 } | 250 } |
| 251 if (cmd_line->HasSwitch("v") || cmd_line->HasSwitch("version")) { | 251 if (cmd_line->HasSwitch("v") || cmd_line->HasSwitch("version")) { |
| 252 printf("ChromeDriver %s\n", kChromeDriverVersion); | 252 printf("ChromeDriver %s\n", kChromeDriverVersion); |
| 253 return 0; | 253 return 0; |
| 254 } | 254 } |
| 255 if (cmd_line->HasSwitch("port")) { | 255 if (cmd_line->HasSwitch("port")) { |
| 256 if (!base::StringToInt(cmd_line->GetSwitchValueASCII("port"), &port)) { | 256 int cmd_line_port; |
| 257 if (!base::StringToInt(cmd_line->GetSwitchValueASCII("port"), | |
| 258 &cmd_line_port) || | |
| 259 cmd_line_port < 0 || cmd_line_port > 65535) { | |
|
Peter Kasting
2014/11/12 23:54:00
Again, the old code probably should have had these
| |
| 257 printf("Invalid port. Exiting...\n"); | 260 printf("Invalid port. Exiting...\n"); |
| 258 return 1; | 261 return 1; |
| 259 } | 262 } |
| 263 port = static_cast<uint16>(cmd_line_port); | |
| 260 } | 264 } |
| 261 if (cmd_line->HasSwitch("adb-port")) { | 265 if (cmd_line->HasSwitch("adb-port")) { |
| 262 if (!base::StringToInt(cmd_line->GetSwitchValueASCII("adb-port"), | 266 if (!base::StringToInt(cmd_line->GetSwitchValueASCII("adb-port"), |
| 263 &adb_port)) { | 267 &adb_port)) { |
| 264 printf("Invalid adb-port. Exiting...\n"); | 268 printf("Invalid adb-port. Exiting...\n"); |
| 265 return 1; | 269 return 1; |
| 266 } | 270 } |
| 267 } | 271 } |
| 268 if (cmd_line->HasSwitch("port-server")) { | 272 if (cmd_line->HasSwitch("port-server")) { |
| 269 #if defined(OS_LINUX) | 273 #if defined(OS_LINUX) |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 286 if (url_base.empty() || url_base[0] != '/') | 290 if (url_base.empty() || url_base[0] != '/') |
| 287 url_base = "/" + url_base; | 291 url_base = "/" + url_base; |
| 288 if (url_base[url_base.length() - 1] != '/') | 292 if (url_base[url_base.length() - 1] != '/') |
| 289 url_base = url_base + "/"; | 293 url_base = url_base + "/"; |
| 290 if (cmd_line->HasSwitch("whitelisted-ips")) { | 294 if (cmd_line->HasSwitch("whitelisted-ips")) { |
| 291 allow_remote = true; | 295 allow_remote = true; |
| 292 std::string whitelist = cmd_line->GetSwitchValueASCII("whitelisted-ips"); | 296 std::string whitelist = cmd_line->GetSwitchValueASCII("whitelisted-ips"); |
| 293 base::SplitString(whitelist, ',', &whitelisted_ips); | 297 base::SplitString(whitelist, ',', &whitelisted_ips); |
| 294 } | 298 } |
| 295 if (!cmd_line->HasSwitch("silent")) { | 299 if (!cmd_line->HasSwitch("silent")) { |
| 296 printf("Starting ChromeDriver %s on port %d\n", kChromeDriverVersion, port); | 300 printf("Starting ChromeDriver %s on port %u\n", kChromeDriverVersion, port); |
| 297 if (!allow_remote) { | 301 if (!allow_remote) { |
| 298 printf("Only local connections are allowed.\n"); | 302 printf("Only local connections are allowed.\n"); |
| 299 } else if (!whitelisted_ips.empty()) { | 303 } else if (!whitelisted_ips.empty()) { |
| 300 printf("Remote connections are allowed by a whitelist (%s).\n", | 304 printf("Remote connections are allowed by a whitelist (%s).\n", |
| 301 cmd_line->GetSwitchValueASCII("whitelisted-ips").c_str()); | 305 cmd_line->GetSwitchValueASCII("whitelisted-ips").c_str()); |
| 302 } else { | 306 } else { |
| 303 printf("All remote connections are allowed. Use a whitelist instead!\n"); | 307 printf("All remote connections are allowed. Use a whitelist instead!\n"); |
| 304 } | 308 } |
| 305 fflush(stdout); | 309 fflush(stdout); |
| 306 } | 310 } |
| 307 | 311 |
| 308 if (!InitLogging()) { | 312 if (!InitLogging()) { |
| 309 printf("Unable to initialize logging. Exiting...\n"); | 313 printf("Unable to initialize logging. Exiting...\n"); |
| 310 return 1; | 314 return 1; |
| 311 } | 315 } |
| 312 RunServer(port, allow_remote, whitelisted_ips, | 316 RunServer(port, allow_remote, whitelisted_ips, |
| 313 url_base, adb_port, port_server.Pass()); | 317 url_base, adb_port, port_server.Pass()); |
| 314 return 0; | 318 return 0; |
| 315 } | 319 } |
| OLD | NEW |