| 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 #include <locale> | 8 #include <locale> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> |
| 10 #include <vector> | 11 #include <vector> |
| 11 | 12 |
| 12 #include "base/at_exit.h" | 13 #include "base/at_exit.h" |
| 13 #include "base/bind.h" | 14 #include "base/bind.h" |
| 14 #include "base/callback.h" | 15 #include "base/callback.h" |
| 15 #include "base/command_line.h" | 16 #include "base/command_line.h" |
| 16 #include "base/files/file_path.h" | 17 #include "base/files/file_path.h" |
| 17 #include "base/lazy_instance.h" | 18 #include "base/lazy_instance.h" |
| 18 #include "base/logging.h" | 19 #include "base/logging.h" |
| 19 #include "base/macros.h" | 20 #include "base/macros.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 | 58 |
| 58 ~HttpServer() override {} | 59 ~HttpServer() override {} |
| 59 | 60 |
| 60 bool Start(uint16_t port, bool allow_remote) { | 61 bool Start(uint16_t port, bool allow_remote) { |
| 61 std::string binding_ip = kLocalHostAddress; | 62 std::string binding_ip = kLocalHostAddress; |
| 62 if (allow_remote) | 63 if (allow_remote) |
| 63 binding_ip = "0.0.0.0"; | 64 binding_ip = "0.0.0.0"; |
| 64 scoped_ptr<net::ServerSocket> server_socket( | 65 scoped_ptr<net::ServerSocket> server_socket( |
| 65 new net::TCPServerSocket(NULL, net::NetLog::Source())); | 66 new net::TCPServerSocket(NULL, net::NetLog::Source())); |
| 66 server_socket->ListenWithAddressAndPort(binding_ip, port, 1); | 67 server_socket->ListenWithAddressAndPort(binding_ip, port, 1); |
| 67 server_.reset(new net::HttpServer(server_socket.Pass(), this)); | 68 server_.reset(new net::HttpServer(std::move(server_socket), this)); |
| 68 net::IPEndPoint address; | 69 net::IPEndPoint address; |
| 69 return server_->GetLocalAddress(&address) == net::OK; | 70 return server_->GetLocalAddress(&address) == net::OK; |
| 70 } | 71 } |
| 71 | 72 |
| 72 // Overridden from net::HttpServer::Delegate: | 73 // Overridden from net::HttpServer::Delegate: |
| 73 void OnConnect(int connection_id) override { | 74 void OnConnect(int connection_id) override { |
| 74 server_->SetSendBufferSize(connection_id, kBufferSize); | 75 server_->SetSendBufferSize(connection_id, kBufferSize); |
| 75 server_->SetReceiveBufferSize(connection_id, kBufferSize); | 76 server_->SetReceiveBufferSize(connection_id, kBufferSize); |
| 76 } | 77 } |
| 77 void OnHttpRequest(int connection_id, | 78 void OnHttpRequest(int connection_id, |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 const HttpResponseSenderFunc& send_response_func) { | 121 const HttpResponseSenderFunc& send_response_func) { |
| 121 if (!whitelisted_ips.empty()) { | 122 if (!whitelisted_ips.empty()) { |
| 122 std::string peer_address = request.peer.ToStringWithoutPort(); | 123 std::string peer_address = request.peer.ToStringWithoutPort(); |
| 123 if (peer_address != kLocalHostAddress && | 124 if (peer_address != kLocalHostAddress && |
| 124 std::find(whitelisted_ips.begin(), whitelisted_ips.end(), | 125 std::find(whitelisted_ips.begin(), whitelisted_ips.end(), |
| 125 peer_address) == whitelisted_ips.end()) { | 126 peer_address) == whitelisted_ips.end()) { |
| 126 LOG(WARNING) << "unauthorized access from " << request.peer.ToString(); | 127 LOG(WARNING) << "unauthorized access from " << request.peer.ToString(); |
| 127 scoped_ptr<net::HttpServerResponseInfo> response( | 128 scoped_ptr<net::HttpServerResponseInfo> response( |
| 128 new net::HttpServerResponseInfo(net::HTTP_UNAUTHORIZED)); | 129 new net::HttpServerResponseInfo(net::HTTP_UNAUTHORIZED)); |
| 129 response->SetBody("Unauthorized access", "text/plain"); | 130 response->SetBody("Unauthorized access", "text/plain"); |
| 130 send_response_func.Run(response.Pass()); | 131 send_response_func.Run(std::move(response)); |
| 131 return; | 132 return; |
| 132 } | 133 } |
| 133 } | 134 } |
| 134 | 135 |
| 135 handler->Handle(request, send_response_func); | 136 handler->Handle(request, send_response_func); |
| 136 } | 137 } |
| 137 | 138 |
| 138 void HandleRequestOnIOThread( | 139 void HandleRequestOnIOThread( |
| 139 const scoped_refptr<base::SingleThreadTaskRunner>& cmd_task_runner, | 140 const scoped_refptr<base::SingleThreadTaskRunner>& cmd_task_runner, |
| 140 const HttpRequestHandlerFunc& handle_request_on_cmd_func, | 141 const HttpRequestHandlerFunc& handle_request_on_cmd_func, |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 const std::string& url_base, | 175 const std::string& url_base, |
| 175 int adb_port, | 176 int adb_port, |
| 176 scoped_ptr<PortServer> port_server) { | 177 scoped_ptr<PortServer> port_server) { |
| 177 base::Thread io_thread("ChromeDriver IO"); | 178 base::Thread io_thread("ChromeDriver IO"); |
| 178 CHECK(io_thread.StartWithOptions( | 179 CHECK(io_thread.StartWithOptions( |
| 179 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | 180 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
| 180 | 181 |
| 181 base::MessageLoop cmd_loop; | 182 base::MessageLoop cmd_loop; |
| 182 base::RunLoop cmd_run_loop; | 183 base::RunLoop cmd_run_loop; |
| 183 HttpHandler handler(cmd_run_loop.QuitClosure(), io_thread.task_runner(), | 184 HttpHandler handler(cmd_run_loop.QuitClosure(), io_thread.task_runner(), |
| 184 url_base, adb_port, port_server.Pass()); | 185 url_base, adb_port, std::move(port_server)); |
| 185 HttpRequestHandlerFunc handle_request_func = | 186 HttpRequestHandlerFunc handle_request_func = |
| 186 base::Bind(&HandleRequestOnCmdThread, &handler, whitelisted_ips); | 187 base::Bind(&HandleRequestOnCmdThread, &handler, whitelisted_ips); |
| 187 | 188 |
| 188 io_thread.message_loop()->PostTask( | 189 io_thread.message_loop()->PostTask( |
| 189 FROM_HERE, | 190 FROM_HERE, |
| 190 base::Bind(&StartServerOnIOThread, port, allow_remote, | 191 base::Bind(&StartServerOnIOThread, port, allow_remote, |
| 191 base::Bind(&HandleRequestOnIOThread, cmd_loop.task_runner(), | 192 base::Bind(&HandleRequestOnIOThread, cmd_loop.task_runner(), |
| 192 handle_request_func))); | 193 handle_request_func))); |
| 193 // Run the command loop. This loop is quit after the response for a shutdown | 194 // Run the command loop. This loop is quit after the response for a shutdown |
| 194 // request is posted to the IO loop. After the command loop quits, a task | 195 // request is posted to the IO loop. After the command loop quits, a task |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 } else { | 305 } else { |
| 305 printf("All remote connections are allowed. Use a whitelist instead!\n"); | 306 printf("All remote connections are allowed. Use a whitelist instead!\n"); |
| 306 } | 307 } |
| 307 fflush(stdout); | 308 fflush(stdout); |
| 308 } | 309 } |
| 309 | 310 |
| 310 if (!InitLogging()) { | 311 if (!InitLogging()) { |
| 311 printf("Unable to initialize logging. Exiting...\n"); | 312 printf("Unable to initialize logging. Exiting...\n"); |
| 312 return 1; | 313 return 1; |
| 313 } | 314 } |
| 314 RunServer(port, allow_remote, whitelisted_ips, | 315 RunServer(port, allow_remote, whitelisted_ips, url_base, adb_port, |
| 315 url_base, adb_port, port_server.Pass()); | 316 std::move(port_server)); |
| 316 return 0; | 317 return 0; |
| 317 } | 318 } |
| OLD | NEW |