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_server_socket.h" | 36 #include "net/socket/tcp_listen_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 scoped_ptr<net::ServerSocket> server_socket( | 58 server_ = new net::HttpServer( |
59 new net::TCPServerSocket(NULL, net::NetLog::Source())); | 59 net::TCPListenSocketFactory(binding_ip, port), this); |
60 server_socket->ListenWithAddressAndPort(binding_ip, port, 1); | |
61 server_.reset(new net::HttpServer(server_socket.Pass(), this)); | |
62 net::IPEndPoint address; | 60 net::IPEndPoint address; |
63 return server_->GetLocalAddress(&address) == net::OK; | 61 return server_->GetLocalAddress(&address) == net::OK; |
64 } | 62 } |
65 | 63 |
66 // Overridden from net::HttpServer::Delegate: | 64 // Overridden from net::HttpServer::Delegate: |
67 virtual void OnHttpRequest(int connection_id, | 65 virtual void OnHttpRequest(int connection_id, |
68 const net::HttpServerRequestInfo& info) OVERRIDE { | 66 const net::HttpServerRequestInfo& info) OVERRIDE { |
69 handle_request_func_.Run( | 67 handle_request_func_.Run( |
70 info, | 68 info, |
71 base::Bind(&HttpServer::OnResponse, | 69 base::Bind(&HttpServer::OnResponse, |
(...skipping 12 matching lines...) Expand all Loading... |
84 scoped_ptr<net::HttpServerResponseInfo> response) { | 82 scoped_ptr<net::HttpServerResponseInfo> response) { |
85 // Don't support keep-alive, since there's no way to detect if the | 83 // Don't support keep-alive, since there's no way to detect if the |
86 // client is HTTP/1.0. In such cases, the client may hang waiting for | 84 // client is HTTP/1.0. In such cases, the client may hang waiting for |
87 // the connection to close (e.g., python 2.7 urllib). | 85 // the connection to close (e.g., python 2.7 urllib). |
88 response->AddHeader("Connection", "close"); | 86 response->AddHeader("Connection", "close"); |
89 server_->SendResponse(connection_id, *response); | 87 server_->SendResponse(connection_id, *response); |
90 server_->Close(connection_id); | 88 server_->Close(connection_id); |
91 } | 89 } |
92 | 90 |
93 HttpRequestHandlerFunc handle_request_func_; | 91 HttpRequestHandlerFunc handle_request_func_; |
94 scoped_ptr<net::HttpServer> server_; | 92 scoped_refptr<net::HttpServer> server_; |
95 base::WeakPtrFactory<HttpServer> weak_factory_; // Should be last. | 93 base::WeakPtrFactory<HttpServer> weak_factory_; // Should be last. |
96 }; | 94 }; |
97 | 95 |
98 void SendResponseOnCmdThread( | 96 void SendResponseOnCmdThread( |
99 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | 97 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, |
100 const HttpResponseSenderFunc& send_response_on_io_func, | 98 const HttpResponseSenderFunc& send_response_on_io_func, |
101 scoped_ptr<net::HttpServerResponseInfo> response) { | 99 scoped_ptr<net::HttpServerResponseInfo> response) { |
102 io_task_runner->PostTask( | 100 io_task_runner->PostTask( |
103 FROM_HERE, base::Bind(send_response_on_io_func, base::Passed(&response))); | 101 FROM_HERE, base::Bind(send_response_on_io_func, base::Passed(&response))); |
104 } | 102 } |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 } | 300 } |
303 | 301 |
304 if (!InitLogging()) { | 302 if (!InitLogging()) { |
305 printf("Unable to initialize logging. Exiting...\n"); | 303 printf("Unable to initialize logging. Exiting...\n"); |
306 return 1; | 304 return 1; |
307 } | 305 } |
308 RunServer(port, allow_remote, whitelisted_ips, | 306 RunServer(port, allow_remote, whitelisted_ips, |
309 url_base, adb_port, port_server.Pass()); | 307 url_base, adb_port, port_server.Pass()); |
310 return 0; | 308 return 0; |
311 } | 309 } |
OLD | NEW |