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 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" | |
| 37 | 36 |
| 38 namespace { | 37 namespace { |
| 39 | 38 |
| 40 const char* kLocalHostAddress = "127.0.0.1"; | 39 const char* kLocalHostAddress = "127.0.0.1"; |
| 41 | 40 |
| 42 typedef base::Callback< | 41 typedef base::Callback< |
| 43 void(const net::HttpServerRequestInfo&, const HttpResponseSenderFunc&)> | 42 void(const net::HttpServerRequestInfo&, const HttpResponseSenderFunc&)> |
| 44 HttpRequestHandlerFunc; | 43 HttpRequestHandlerFunc; |
| 45 | 44 |
| 46 class HttpServer : public net::HttpServer::Delegate { | 45 class HttpServer : public net::HttpServer::Delegate { |
| 47 public: | 46 public: |
| 48 explicit HttpServer(const HttpRequestHandlerFunc& handle_request_func) | 47 explicit HttpServer(const HttpRequestHandlerFunc& handle_request_func) |
| 49 : handle_request_func_(handle_request_func), | 48 : handle_request_func_(handle_request_func), |
| 50 weak_factory_(this) {} | 49 weak_factory_(this) {} |
| 51 | 50 |
| 52 virtual ~HttpServer() {} | 51 virtual ~HttpServer() {} |
| 53 | 52 |
| 54 bool Start(int port, bool allow_remote) { | 53 bool Start(int port, bool allow_remote) { |
| 55 std::string binding_ip = kLocalHostAddress; | 54 std::string binding_ip = kLocalHostAddress; |
| 56 if (allow_remote) | 55 if (allow_remote) |
| 57 binding_ip = "0.0.0.0"; | 56 binding_ip = "0.0.0.0"; |
| 58 server_ = new net::HttpServer( | 57 server_.reset(net::HttpServer::Create(binding_ip, port, this)); |
| 59 net::TCPListenSocketFactory(binding_ip, port), this); | 58 if (!server_) { |
|
pfeldman
2014/05/22 06:01:05
style nit: we don't put {} around single line bloc
byungchul
2014/05/30 00:19:02
Done.
| |
| 59 return false; | |
| 60 } | |
| 60 net::IPEndPoint address; | 61 net::IPEndPoint address; |
| 61 return server_->GetLocalAddress(&address) == net::OK; | 62 return server_->GetLocalAddress(&address) == net::OK; |
| 62 } | 63 } |
| 63 | 64 |
| 64 // Overridden from net::HttpServer::Delegate: | 65 // Overridden from net::HttpServer::Delegate: |
| 65 virtual void OnHttpRequest(int connection_id, | 66 virtual void OnHttpRequest(int connection_id, |
| 66 const net::HttpServerRequestInfo& info) OVERRIDE { | 67 const net::HttpServerRequestInfo& info) OVERRIDE { |
| 67 handle_request_func_.Run( | 68 handle_request_func_.Run( |
| 68 info, | 69 info, |
| 69 base::Bind(&HttpServer::OnResponse, | 70 base::Bind(&HttpServer::OnResponse, |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 82 scoped_ptr<net::HttpServerResponseInfo> response) { | 83 scoped_ptr<net::HttpServerResponseInfo> response) { |
| 83 // Don't support keep-alive, since there's no way to detect if the | 84 // 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 | 85 // 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). | 86 // the connection to close (e.g., python 2.7 urllib). |
| 86 response->AddHeader("Connection", "close"); | 87 response->AddHeader("Connection", "close"); |
| 87 server_->SendResponse(connection_id, *response); | 88 server_->SendResponse(connection_id, *response); |
| 88 server_->Close(connection_id); | 89 server_->Close(connection_id); |
| 89 } | 90 } |
| 90 | 91 |
| 91 HttpRequestHandlerFunc handle_request_func_; | 92 HttpRequestHandlerFunc handle_request_func_; |
| 92 scoped_refptr<net::HttpServer> server_; | 93 scoped_ptr<net::HttpServer> server_; |
| 93 base::WeakPtrFactory<HttpServer> weak_factory_; // Should be last. | 94 base::WeakPtrFactory<HttpServer> weak_factory_; // Should be last. |
| 94 }; | 95 }; |
| 95 | 96 |
| 96 void SendResponseOnCmdThread( | 97 void SendResponseOnCmdThread( |
| 97 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | 98 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, |
| 98 const HttpResponseSenderFunc& send_response_on_io_func, | 99 const HttpResponseSenderFunc& send_response_on_io_func, |
| 99 scoped_ptr<net::HttpServerResponseInfo> response) { | 100 scoped_ptr<net::HttpServerResponseInfo> response) { |
| 100 io_task_runner->PostTask( | 101 io_task_runner->PostTask( |
| 101 FROM_HERE, base::Bind(send_response_on_io_func, base::Passed(&response))); | 102 FROM_HERE, base::Bind(send_response_on_io_func, base::Passed(&response))); |
| 102 } | 103 } |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 300 } | 301 } |
| 301 | 302 |
| 302 if (!InitLogging()) { | 303 if (!InitLogging()) { |
| 303 printf("Unable to initialize logging. Exiting...\n"); | 304 printf("Unable to initialize logging. Exiting...\n"); |
| 304 return 1; | 305 return 1; |
| 305 } | 306 } |
| 306 RunServer(port, allow_remote, whitelisted_ips, | 307 RunServer(port, allow_remote, whitelisted_ips, |
| 307 url_base, adb_port, port_server.Pass()); | 308 url_base, adb_port, port_server.Pass()); |
| 308 return 0; | 309 return 0; |
| 309 } | 310 } |
| OLD | NEW |