| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_GOOGLE_APIS_TEST_SERVER_HTTP_SERVER_H_ | 5 #ifndef CHROME_BROWSER_GOOGLE_APIS_TEST_SERVER_HTTP_SERVER_H_ |
| 6 #define CHROME_BROWSER_GOOGLE_APIS_TEST_SERVER_HTTP_SERVER_H_ | 6 #define CHROME_BROWSER_GOOGLE_APIS_TEST_SERVER_HTTP_SERVER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/threading/thread_checker.h" |
| 16 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
| 17 #include "net/socket/tcp_listen_socket.h" | 18 #include "net/socket/tcp_listen_socket.h" |
| 18 | 19 |
| 19 namespace google_apis { | 20 namespace google_apis { |
| 20 namespace test_server { | 21 namespace test_server { |
| 21 | 22 |
| 22 class HttpConnection; | 23 class HttpConnection; |
| 23 class HttpResponse; | 24 class HttpResponse; |
| 24 struct HttpRequest; | 25 struct HttpRequest; |
| 25 | 26 |
| 26 // This class is required to be able to have composition instead of inheritance, | 27 // This class is required to be able to have composition instead of inheritance, |
| 27 class HttpListenSocket: public net::TCPListenSocket { | 28 class HttpListenSocket : public net::TCPListenSocket { |
| 28 public: | 29 public: |
| 29 HttpListenSocket(const SocketDescriptor socket_descriptor, | 30 HttpListenSocket(const SocketDescriptor socket_descriptor, |
| 30 net::StreamListenSocket::Delegate* delegate); | 31 net::StreamListenSocket::Delegate* delegate); |
| 31 virtual void Listen(); | 32 virtual void Listen(); |
| 32 | 33 |
| 33 private: | 34 private: |
| 34 virtual ~HttpListenSocket(); | 35 virtual ~HttpListenSocket(); |
| 36 |
| 37 base::ThreadChecker thread_checker_; |
| 35 }; | 38 }; |
| 36 | 39 |
| 37 // Class providing an HTTP server for testing purpose. This is a basic server | 40 // Class providing an HTTP server for testing purpose. This is a basic server |
| 38 // providing only an essential subset of HTTP/1.1 protocol. Especially, | 41 // providing only an essential subset of HTTP/1.1 protocol. Especially, |
| 39 // it assumes that the request syntax is correct. It *does not* support | 42 // it assumes that the request syntax is correct. It *does not* support |
| 40 // a Chunked Transfer Encoding. | 43 // a Chunked Transfer Encoding. |
| 41 // | 44 // |
| 42 // The common use case is below: | 45 // The common use case is below: |
| 43 // | 46 // |
| 47 // base::Thread io_thread_; |
| 44 // scoped_ptr<HttpServer> test_server_; | 48 // scoped_ptr<HttpServer> test_server_; |
| 45 // | 49 // |
| 46 // void SetUp() { | 50 // void SetUp() { |
| 47 // test_server_.reset(new HttpServer()); | 51 // base::Thread::Options thread_options; |
| 48 // DCHECK(test_server_.InitializeAndWaitUntilReady()); | 52 // thread_options.message_loop_type = MessageLoop::TYPE_IO; |
| 53 // ASSERT_TRUE(io_thread_.StartWithOptions(thread_options)); |
| 54 // |
| 55 // test_server_.reset(new HttpServer(io_thread_.message_loop_proxy())); |
| 56 // ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady()); |
| 49 // test_server_->RegisterRequestHandler( | 57 // test_server_->RegisterRequestHandler( |
| 50 // base::Bind(&FooTest::HandleRequest, base::Unretained(this))); | 58 // base::Bind(&FooTest::HandleRequest, base::Unretained(this))); |
| 51 // } | 59 // } |
| 52 // | 60 // |
| 53 // scoped_ptr<HttpResponse> HandleRequest(const HttpRequest& request) { | 61 // scoped_ptr<HttpResponse> HandleRequest(const HttpRequest& request) { |
| 54 // GURL absolute_url = test_server_->GetURL(request.relative_url); | 62 // GURL absolute_url = test_server_->GetURL(request.relative_url); |
| 55 // if (absolute_url.path() != "/test") | 63 // if (absolute_url.path() != "/test") |
| 56 // return scoped_ptr<HttpResponse>(); | 64 // return scoped_ptr<HttpResponse>(); |
| 57 // | 65 // |
| 58 // scoped_ptr<HttpResponse> http_response(new HttpResponse()); | 66 // scoped_ptr<HttpResponse> http_response(new HttpResponse()); |
| 59 // http_response->set_code(test_server::SUCCESS); | 67 // http_response->set_code(test_server::SUCCESS); |
| 60 // http_response->set_content("hello"); | 68 // http_response->set_content("hello"); |
| 61 // http_response->set_content_type("text/plain"); | 69 // http_response->set_content_type("text/plain"); |
| 62 // return http_response.Pass(); | 70 // return http_response.Pass(); |
| 63 // } | 71 // } |
| 64 // | 72 // |
| 65 class HttpServer : public net::StreamListenSocket::Delegate { | 73 class HttpServer : public net::StreamListenSocket::Delegate { |
| 66 public: | 74 public: |
| 67 typedef base::Callback<scoped_ptr<HttpResponse>(const HttpRequest& request)> | 75 typedef base::Callback<scoped_ptr<HttpResponse>(const HttpRequest& request)> |
| 68 HandleRequestCallback; | 76 HandleRequestCallback; |
| 69 | 77 |
| 70 // Creates a http test server. InitializeAndWaitUntilReady() must be called | 78 // Creates a http test server. |io_thread| is a task runner |
| 71 // to start the server. | 79 // with IO message loop, used as a backend thread. |
| 72 HttpServer(); | 80 // InitializeAndWaitUntilReady() must be called to start the server. |
| 81 explicit HttpServer( |
| 82 const scoped_refptr<base::SingleThreadTaskRunner>& io_thread); |
| 73 virtual ~HttpServer(); | 83 virtual ~HttpServer(); |
| 74 | 84 |
| 75 // Initializes and waits until the server is ready to accept requests. | 85 // Initializes and waits until the server is ready to accept requests. |
| 76 bool InitializeAndWaitUntilReady(); | 86 bool InitializeAndWaitUntilReady() WARN_UNUSED_RESULT; |
| 77 | 87 |
| 78 // Shuts down the http server and waits until the shutdown is complete. | 88 // Shuts down the http server and waits until the shutdown is complete. |
| 79 void ShutdownAndWaitUntilComplete(); | 89 bool ShutdownAndWaitUntilComplete() WARN_UNUSED_RESULT; |
| 80 | 90 |
| 81 // Checks if the server is started. | 91 // Checks if the server is started. |
| 82 bool Started() const { | 92 bool Started() const { |
| 83 return listen_socket_.get() != NULL; | 93 return listen_socket_.get() != NULL; |
| 84 } | 94 } |
| 85 | 95 |
| 86 // Returns the base URL to the server, which looks like | 96 // Returns the base URL to the server, which looks like |
| 87 // http://127.0.0.1:<port>/, where <port> is the actual port number used by | 97 // http://127.0.0.1:<port>/, where <port> is the actual port number used by |
| 88 // the server. | 98 // the server. |
| 89 const GURL& base_url() const { return base_url_; } | 99 const GURL& base_url() const { return base_url_; } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 117 // net::StreamListenSocket::Delegate overrides: | 127 // net::StreamListenSocket::Delegate overrides: |
| 118 virtual void DidAccept(net::StreamListenSocket* server, | 128 virtual void DidAccept(net::StreamListenSocket* server, |
| 119 net::StreamListenSocket* connection) OVERRIDE; | 129 net::StreamListenSocket* connection) OVERRIDE; |
| 120 virtual void DidRead(net::StreamListenSocket* connection, | 130 virtual void DidRead(net::StreamListenSocket* connection, |
| 121 const char* data, | 131 const char* data, |
| 122 int length) OVERRIDE; | 132 int length) OVERRIDE; |
| 123 virtual void DidClose(net::StreamListenSocket* connection) OVERRIDE; | 133 virtual void DidClose(net::StreamListenSocket* connection) OVERRIDE; |
| 124 | 134 |
| 125 HttpConnection* FindConnection(net::StreamListenSocket* socket); | 135 HttpConnection* FindConnection(net::StreamListenSocket* socket); |
| 126 | 136 |
| 137 scoped_refptr<base::SingleThreadTaskRunner> io_thread_; |
| 138 |
| 127 scoped_refptr<HttpListenSocket> listen_socket_; | 139 scoped_refptr<HttpListenSocket> listen_socket_; |
| 128 int port_; | 140 int port_; |
| 129 GURL base_url_; | 141 GURL base_url_; |
| 130 | 142 |
| 131 // Owns the HttpConnection objects. | 143 // Owns the HttpConnection objects. |
| 132 std::map<net::StreamListenSocket*, HttpConnection*> connections_; | 144 std::map<net::StreamListenSocket*, HttpConnection*> connections_; |
| 133 | 145 |
| 134 // Vector of registered request handlers. | 146 // Vector of registered request handlers. |
| 135 std::vector<HandleRequestCallback> request_handlers_; | 147 std::vector<HandleRequestCallback> request_handlers_; |
| 136 | 148 |
| 137 // Note: This should remain the last member so it'll be destroyed and | 149 // Note: This should remain the last member so it'll be destroyed and |
| 138 // invalidate its weak pointers before any other members are destroyed. | 150 // invalidate its weak pointers before any other members are destroyed. |
| 139 base::WeakPtrFactory<HttpServer> weak_factory_; | 151 base::WeakPtrFactory<HttpServer> weak_factory_; |
| 140 | 152 |
| 153 base::ThreadChecker thread_checker_; |
| 154 |
| 141 DISALLOW_COPY_AND_ASSIGN(HttpServer); | 155 DISALLOW_COPY_AND_ASSIGN(HttpServer); |
| 142 }; | 156 }; |
| 143 | 157 |
| 144 } // namespace test_servers | 158 } // namespace test_servers |
| 145 } // namespace google_apis | 159 } // namespace google_apis |
| 146 | 160 |
| 147 #endif // CHROME_BROWSER_GOOGLE_APIS_TEST_SERVER_HTTP_SERVER_H_ | 161 #endif // CHROME_BROWSER_GOOGLE_APIS_TEST_SERVER_HTTP_SERVER_H_ |
| OLD | NEW |