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_EMBEDDED_TEST_SERVER_H_ | 5 #ifndef NET_TEST_EMBEDDED_TEST_SERVER_EMBEDDED_TEST_SERVER_H_ |
6 #define NET_TEST_EMBEDDED_TEST_SERVER_EMBEDDED_TEST_SERVER_H_ | 6 #define NET_TEST_EMBEDDED_TEST_SERVER_EMBEDDED_TEST_SERVER_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
| 11 #include <memory> |
11 #include <string> | 12 #include <string> |
12 #include <vector> | 13 #include <vector> |
13 | 14 |
14 #include "base/callback.h" | 15 #include "base/callback.h" |
15 #include "base/compiler_specific.h" | 16 #include "base/compiler_specific.h" |
16 #include "base/files/file_path.h" | 17 #include "base/files/file_path.h" |
17 #include "base/macros.h" | 18 #include "base/macros.h" |
18 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
19 #include "base/memory/scoped_ptr.h" | |
20 #include "base/memory/weak_ptr.h" | 20 #include "base/memory/weak_ptr.h" |
21 #include "base/threading/thread.h" | 21 #include "base/threading/thread.h" |
22 #include "base/threading/thread_checker.h" | 22 #include "base/threading/thread_checker.h" |
23 #include "crypto/rsa_private_key.h" | 23 #include "crypto/rsa_private_key.h" |
24 #include "net/base/address_list.h" | 24 #include "net/base/address_list.h" |
25 #include "net/base/host_port_pair.h" | 25 #include "net/base/host_port_pair.h" |
26 #include "net/base/ip_endpoint.h" | 26 #include "net/base/ip_endpoint.h" |
27 #include "net/cert/x509_certificate.h" | 27 #include "net/cert/x509_certificate.h" |
28 #include "net/socket/ssl_server_socket.h" | 28 #include "net/socket/ssl_server_socket.h" |
29 #include "net/socket/stream_socket.h" | 29 #include "net/socket/stream_socket.h" |
(...skipping 20 matching lines...) Expand all Loading... |
50 // | 50 // |
51 // The common use case for unit tests is below: | 51 // The common use case for unit tests is below: |
52 // | 52 // |
53 // void SetUp() { | 53 // void SetUp() { |
54 // test_server_.reset(new EmbeddedTestServer()); | 54 // test_server_.reset(new EmbeddedTestServer()); |
55 // ASSERT_TRUE(test_server_.Start()); | 55 // ASSERT_TRUE(test_server_.Start()); |
56 // test_server_->RegisterRequestHandler( | 56 // test_server_->RegisterRequestHandler( |
57 // base::Bind(&FooTest::HandleRequest, base::Unretained(this))); | 57 // base::Bind(&FooTest::HandleRequest, base::Unretained(this))); |
58 // } | 58 // } |
59 // | 59 // |
60 // scoped_ptr<HttpResponse> HandleRequest(const HttpRequest& request) { | 60 // std::unique_ptr<HttpResponse> HandleRequest(const HttpRequest& request) { |
61 // GURL absolute_url = test_server_->GetURL(request.relative_url); | 61 // GURL absolute_url = test_server_->GetURL(request.relative_url); |
62 // if (absolute_url.path() != "/test") | 62 // if (absolute_url.path() != "/test") |
63 // return scoped_ptr<HttpResponse>(); | 63 // return std::unique_ptr<HttpResponse>(); |
64 // | 64 // |
65 // scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse()); | 65 // std::unique_ptr<BasicHttpResponse> http_response(new BasicHttpResponse()); |
66 // http_response->set_code(test_server::SUCCESS); | 66 // http_response->set_code(test_server::SUCCESS); |
67 // http_response->set_content("hello"); | 67 // http_response->set_content("hello"); |
68 // http_response->set_content_type("text/plain"); | 68 // http_response->set_content_type("text/plain"); |
69 // return http_response; | 69 // return http_response; |
70 // } | 70 // } |
71 // | 71 // |
72 // For a test that spawns another process such as browser_tests, it is | 72 // For a test that spawns another process such as browser_tests, it is |
73 // suggested to call Start in SetUpOnMainThread after the process is spawned. | 73 // suggested to call Start in SetUpOnMainThread after the process is spawned. |
74 // If you have to do it before the process spawns, you need to first setup the | 74 // If you have to do it before the process spawns, you need to first setup the |
75 // listen socket so that there is no no other threads running while spawning | 75 // listen socket so that there is no no other threads running while spawning |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 // intermediate cross-signed by an unknown root, while the client (via | 107 // intermediate cross-signed by an unknown root, while the client (via |
108 // TestRootStore) is expected to have a self-signed version of the | 108 // TestRootStore) is expected to have a self-signed version of the |
109 // intermediate. | 109 // intermediate. |
110 CERT_CHAIN_WRONG_ROOT, | 110 CERT_CHAIN_WRONG_ROOT, |
111 | 111 |
112 // Causes the testserver to use a hostname that is a domain | 112 // Causes the testserver to use a hostname that is a domain |
113 // instead of an IP. | 113 // instead of an IP. |
114 CERT_COMMON_NAME_IS_DOMAIN, | 114 CERT_COMMON_NAME_IS_DOMAIN, |
115 }; | 115 }; |
116 | 116 |
117 typedef base::Callback<scoped_ptr<HttpResponse>( | 117 typedef base::Callback<std::unique_ptr<HttpResponse>( |
118 const HttpRequest& request)> HandleRequestCallback; | 118 const HttpRequest& request)> |
| 119 HandleRequestCallback; |
119 | 120 |
120 // Creates a http test server. Start() must be called to start the server. | 121 // Creates a http test server. Start() must be called to start the server. |
121 // |type| indicates the protocol type of the server (HTTP/HTTPS). | 122 // |type| indicates the protocol type of the server (HTTP/HTTPS). |
122 EmbeddedTestServer(); | 123 EmbeddedTestServer(); |
123 explicit EmbeddedTestServer(Type type); | 124 explicit EmbeddedTestServer(Type type); |
124 ~EmbeddedTestServer(); | 125 ~EmbeddedTestServer(); |
125 | 126 |
126 // Sets a connection listener, that would be notified when various connection | 127 // Sets a connection listener, that would be notified when various connection |
127 // events happen. May only be called before the server is started. Caller | 128 // events happen. May only be called before the server is started. Caller |
128 // maintains ownership of the listener. | 129 // maintains ownership of the listener. |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 | 209 |
209 // Adds default handlers, including those added by AddDefaultHandlers, to be | 210 // Adds default handlers, including those added by AddDefaultHandlers, to be |
210 // tried after all other user-specified handlers have been tried. | 211 // tried after all other user-specified handlers have been tried. |
211 void RegisterDefaultHandler(const HandleRequestCallback& callback); | 212 void RegisterDefaultHandler(const HandleRequestCallback& callback); |
212 | 213 |
213 private: | 214 private: |
214 // Shuts down the server. | 215 // Shuts down the server. |
215 void ShutdownOnIOThread(); | 216 void ShutdownOnIOThread(); |
216 | 217 |
217 // Upgrade the TCP connection to one over SSL. | 218 // Upgrade the TCP connection to one over SSL. |
218 scoped_ptr<StreamSocket> DoSSLUpgrade(scoped_ptr<StreamSocket> connection); | 219 std::unique_ptr<StreamSocket> DoSSLUpgrade( |
| 220 std::unique_ptr<StreamSocket> connection); |
219 // Handles async callback when the SSL handshake has been completed. | 221 // Handles async callback when the SSL handshake has been completed. |
220 void OnHandshakeDone(HttpConnection* connection, int rv); | 222 void OnHandshakeDone(HttpConnection* connection, int rv); |
221 | 223 |
222 // Begins accepting new client connections. | 224 // Begins accepting new client connections. |
223 void DoAcceptLoop(); | 225 void DoAcceptLoop(); |
224 // Handles async callback when there is a new client socket. |rv| is the | 226 // Handles async callback when there is a new client socket. |rv| is the |
225 // return value of the socket Accept. | 227 // return value of the socket Accept. |
226 void OnAcceptCompleted(int rv); | 228 void OnAcceptCompleted(int rv); |
227 // Adds the new |socket| to the list of clients and begins the reading | 229 // Adds the new |socket| to the list of clients and begins the reading |
228 // data. | 230 // data. |
229 void HandleAcceptResult(scoped_ptr<StreamSocket> socket); | 231 void HandleAcceptResult(std::unique_ptr<StreamSocket> socket); |
230 | 232 |
231 // Attempts to read data from the |connection|'s socket. | 233 // Attempts to read data from the |connection|'s socket. |
232 void ReadData(HttpConnection* connection); | 234 void ReadData(HttpConnection* connection); |
233 // Handles async callback when new data has been read from the |connection|. | 235 // Handles async callback when new data has been read from the |connection|. |
234 void OnReadCompleted(HttpConnection* connection, int rv); | 236 void OnReadCompleted(HttpConnection* connection, int rv); |
235 // Parses the data read from the |connection| and returns true if the entire | 237 // Parses the data read from the |connection| and returns true if the entire |
236 // request has been received. | 238 // request has been received. |
237 bool HandleReadResult(HttpConnection* connection, int rv); | 239 bool HandleReadResult(HttpConnection* connection, int rv); |
238 | 240 |
239 // Closes and removes the connection upon error or completion. | 241 // Closes and removes the connection upon error or completion. |
240 void DidClose(HttpConnection* connection); | 242 void DidClose(HttpConnection* connection); |
241 | 243 |
242 // Handles a request when it is parsed. It passes the request to registered | 244 // Handles a request when it is parsed. It passes the request to registered |
243 // request handlers and sends a http response. | 245 // request handlers and sends a http response. |
244 void HandleRequest(HttpConnection* connection, | 246 void HandleRequest(HttpConnection* connection, |
245 scoped_ptr<HttpRequest> request); | 247 std::unique_ptr<HttpRequest> request); |
246 | 248 |
247 // Initializes the SSLServerContext so that SSLServerSocket connections may | 249 // Initializes the SSLServerContext so that SSLServerSocket connections may |
248 // share the same cache | 250 // share the same cache |
249 void InitializeSSLServerContext(); | 251 void InitializeSSLServerContext(); |
250 | 252 |
251 HttpConnection* FindConnection(StreamSocket* socket); | 253 HttpConnection* FindConnection(StreamSocket* socket); |
252 | 254 |
253 // Posts a task to the |io_thread_| and waits for a reply. | 255 // Posts a task to the |io_thread_| and waits for a reply. |
254 bool PostTaskToIOThreadAndWait( | 256 bool PostTaskToIOThreadAndWait( |
255 const base::Closure& closure) WARN_UNUSED_RESULT; | 257 const base::Closure& closure) WARN_UNUSED_RESULT; |
256 | 258 |
257 const bool is_using_ssl_; | 259 const bool is_using_ssl_; |
258 | 260 |
259 scoped_ptr<base::Thread> io_thread_; | 261 std::unique_ptr<base::Thread> io_thread_; |
260 | 262 |
261 scoped_ptr<TCPServerSocket> listen_socket_; | 263 std::unique_ptr<TCPServerSocket> listen_socket_; |
262 scoped_ptr<StreamSocket> accepted_socket_; | 264 std::unique_ptr<StreamSocket> accepted_socket_; |
263 | 265 |
264 EmbeddedTestServerConnectionListener* connection_listener_; | 266 EmbeddedTestServerConnectionListener* connection_listener_; |
265 uint16_t port_; | 267 uint16_t port_; |
266 GURL base_url_; | 268 GURL base_url_; |
267 IPEndPoint local_endpoint_; | 269 IPEndPoint local_endpoint_; |
268 | 270 |
269 // Owns the HttpConnection objects. | 271 // Owns the HttpConnection objects. |
270 std::map<StreamSocket*, HttpConnection*> connections_; | 272 std::map<StreamSocket*, HttpConnection*> connections_; |
271 | 273 |
272 // Vector of registered and default request handlers. | 274 // Vector of registered and default request handlers. |
273 std::vector<HandleRequestCallback> request_handlers_; | 275 std::vector<HandleRequestCallback> request_handlers_; |
274 std::vector<HandleRequestCallback> default_request_handlers_; | 276 std::vector<HandleRequestCallback> default_request_handlers_; |
275 | 277 |
276 base::ThreadChecker thread_checker_; | 278 base::ThreadChecker thread_checker_; |
277 | 279 |
278 net::SSLServerConfig ssl_config_; | 280 net::SSLServerConfig ssl_config_; |
279 ServerCertificate cert_; | 281 ServerCertificate cert_; |
280 scoped_ptr<SSLServerContext> context_; | 282 std::unique_ptr<SSLServerContext> context_; |
281 | 283 |
282 base::WeakPtrFactory<EmbeddedTestServer> weak_factory_; | 284 base::WeakPtrFactory<EmbeddedTestServer> weak_factory_; |
283 | 285 |
284 DISALLOW_COPY_AND_ASSIGN(EmbeddedTestServer); | 286 DISALLOW_COPY_AND_ASSIGN(EmbeddedTestServer); |
285 }; | 287 }; |
286 | 288 |
287 } // namespace test_server | 289 } // namespace test_server |
288 | 290 |
289 // TODO(svaldez): Refactor EmbeddedTestServer to be in the net namespace. | 291 // TODO(svaldez): Refactor EmbeddedTestServer to be in the net namespace. |
290 using test_server::EmbeddedTestServer; | 292 using test_server::EmbeddedTestServer; |
291 | 293 |
292 } // namespace net | 294 } // namespace net |
293 | 295 |
294 #endif // NET_TEST_EMBEDDED_TEST_SERVER_EMBEDDED_TEST_SERVER_H_ | 296 #endif // NET_TEST_EMBEDDED_TEST_SERVER_EMBEDDED_TEST_SERVER_H_ |
OLD | NEW |