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