| 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 15 matching lines...) Expand all Loading... |
| 26 #include "base/threading/thread_local.h" | 26 #include "base/threading/thread_local.h" |
| 27 #include "chrome/test/chromedriver/logging.h" | 27 #include "chrome/test/chromedriver/logging.h" |
| 28 #include "chrome/test/chromedriver/net/port_server.h" | 28 #include "chrome/test/chromedriver/net/port_server.h" |
| 29 #include "chrome/test/chromedriver/server/http_handler.h" | 29 #include "chrome/test/chromedriver/server/http_handler.h" |
| 30 #include "chrome/test/chromedriver/version.h" | 30 #include "chrome/test/chromedriver/version.h" |
| 31 #include "net/base/ip_endpoint.h" | 31 #include "net/base/ip_endpoint.h" |
| 32 #include "net/base/net_errors.h" | 32 #include "net/base/net_errors.h" |
| 33 #include "net/server/http_server.h" | 33 #include "net/server/http_server.h" |
| 34 #include "net/server/http_server_request_info.h" | 34 #include "net/server/http_server_request_info.h" |
| 35 #include "net/server/http_server_response_info.h" | 35 #include "net/server/http_server_response_info.h" |
| 36 #include "net/socket/tcp_listen_socket.h" | 36 #include "net/socket/tcp_server_socket.h" |
| 37 | 37 |
| 38 namespace { | 38 namespace { |
| 39 | 39 |
| 40 const char* kLocalHostAddress = "127.0.0.1"; | 40 const char* kLocalHostAddress = "127.0.0.1"; |
| 41 | 41 |
| 42 typedef base::Callback< | 42 typedef base::Callback< |
| 43 void(const net::HttpServerRequestInfo&, const HttpResponseSenderFunc&)> | 43 void(const net::HttpServerRequestInfo&, const HttpResponseSenderFunc&)> |
| 44 HttpRequestHandlerFunc; | 44 HttpRequestHandlerFunc; |
| 45 | 45 |
| 46 class HttpServer : public net::HttpServer::Delegate { | 46 class HttpServer : public net::HttpServer::Delegate { |
| 47 public: | 47 public: |
| 48 explicit HttpServer(const HttpRequestHandlerFunc& handle_request_func) | 48 explicit HttpServer(const HttpRequestHandlerFunc& handle_request_func) |
| 49 : handle_request_func_(handle_request_func), | 49 : handle_request_func_(handle_request_func), |
| 50 weak_factory_(this) {} | 50 weak_factory_(this) {} |
| 51 | 51 |
| 52 virtual ~HttpServer() {} | 52 virtual ~HttpServer() {} |
| 53 | 53 |
| 54 bool Start(int port, bool allow_remote) { | 54 bool Start(int port, bool allow_remote) { |
| 55 std::string binding_ip = kLocalHostAddress; | 55 std::string binding_ip = kLocalHostAddress; |
| 56 if (allow_remote) | 56 if (allow_remote) |
| 57 binding_ip = "0.0.0.0"; | 57 binding_ip = "0.0.0.0"; |
| 58 server_ = new net::HttpServer( | 58 scoped_ptr<net::ServerSocket> server_socket( |
| 59 net::TCPListenSocketFactory(binding_ip, port), this); | 59 new net::TCPServerSocket(NULL, net::NetLog::Source())); |
| 60 server_socket->ListenWithAddressAndPort(binding_ip, port, 1); |
| 61 server_.reset(new net::HttpServer(server_socket.Pass(), this)); |
| 60 net::IPEndPoint address; | 62 net::IPEndPoint address; |
| 61 return server_->GetLocalAddress(&address) == net::OK; | 63 return server_->GetLocalAddress(&address) == net::OK; |
| 62 } | 64 } |
| 63 | 65 |
| 64 // Overridden from net::HttpServer::Delegate: | 66 // Overridden from net::HttpServer::Delegate: |
| 65 virtual void OnHttpRequest(int connection_id, | 67 virtual void OnHttpRequest(int connection_id, |
| 66 const net::HttpServerRequestInfo& info) OVERRIDE { | 68 const net::HttpServerRequestInfo& info) OVERRIDE { |
| 67 handle_request_func_.Run( | 69 handle_request_func_.Run( |
| 68 info, | 70 info, |
| 69 base::Bind(&HttpServer::OnResponse, | 71 base::Bind(&HttpServer::OnResponse, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 82 scoped_ptr<net::HttpServerResponseInfo> response) { | 84 scoped_ptr<net::HttpServerResponseInfo> response) { |
| 83 // Don't support keep-alive, since there's no way to detect if the | 85 // Don't support keep-alive, since there's no way to detect if the |
| 84 // client is HTTP/1.0. In such cases, the client may hang waiting for | 86 // client is HTTP/1.0. In such cases, the client may hang waiting for |
| 85 // the connection to close (e.g., python 2.7 urllib). | 87 // the connection to close (e.g., python 2.7 urllib). |
| 86 response->AddHeader("Connection", "close"); | 88 response->AddHeader("Connection", "close"); |
| 87 server_->SendResponse(connection_id, *response); | 89 server_->SendResponse(connection_id, *response); |
| 88 server_->Close(connection_id); | 90 server_->Close(connection_id); |
| 89 } | 91 } |
| 90 | 92 |
| 91 HttpRequestHandlerFunc handle_request_func_; | 93 HttpRequestHandlerFunc handle_request_func_; |
| 92 scoped_refptr<net::HttpServer> server_; | 94 scoped_ptr<net::HttpServer> server_; |
| 93 base::WeakPtrFactory<HttpServer> weak_factory_; // Should be last. | 95 base::WeakPtrFactory<HttpServer> weak_factory_; // Should be last. |
| 94 }; | 96 }; |
| 95 | 97 |
| 96 void SendResponseOnCmdThread( | 98 void SendResponseOnCmdThread( |
| 97 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | 99 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, |
| 98 const HttpResponseSenderFunc& send_response_on_io_func, | 100 const HttpResponseSenderFunc& send_response_on_io_func, |
| 99 scoped_ptr<net::HttpServerResponseInfo> response) { | 101 scoped_ptr<net::HttpServerResponseInfo> response) { |
| 100 io_task_runner->PostTask( | 102 io_task_runner->PostTask( |
| 101 FROM_HERE, base::Bind(send_response_on_io_func, base::Passed(&response))); | 103 FROM_HERE, base::Bind(send_response_on_io_func, base::Passed(&response))); |
| 102 } | 104 } |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 } | 302 } |
| 301 | 303 |
| 302 if (!InitLogging()) { | 304 if (!InitLogging()) { |
| 303 printf("Unable to initialize logging. Exiting...\n"); | 305 printf("Unable to initialize logging. Exiting...\n"); |
| 304 return 1; | 306 return 1; |
| 305 } | 307 } |
| 306 RunServer(port, allow_remote, whitelisted_ips, | 308 RunServer(port, allow_remote, whitelisted_ips, |
| 307 url_base, adb_port, port_server.Pass()); | 309 url_base, adb_port, port_server.Pass()); |
| 308 return 0; | 310 return 0; |
| 309 } | 311 } |
| OLD | NEW |