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_FRAME_TEST_TEST_SERVER_H_ | 5 #ifndef CHROME_FRAME_TEST_TEST_SERVER_H_ |
6 #define CHROME_FRAME_TEST_TEST_SERVER_H_ | 6 #define CHROME_FRAME_TEST_TEST_SERVER_H_ |
7 | 7 |
8 // Implementation of an HTTP server for tests. | 8 // Implementation of an HTTP server for tests. |
9 // To instantiate the server, make sure you have a message loop on the | 9 // To instantiate the server, make sure you have a message loop on the |
10 // current thread and then create an instance of the SimpleWebServer class. | 10 // current thread and then create an instance of the SimpleWebServer class. |
11 // The server uses two basic concepts, a request and a response. | 11 // The server uses two basic concepts, a request and a response. |
12 // The Response interface represents an item (e.g. a document) available from | 12 // The Response interface represents an item (e.g. a document) available from |
13 // the server. A Request object represents a request from a client (e.g. a | 13 // the server. A Request object represents a request from a client (e.g. a |
14 // browser). There are several basic Response classes implemented in this file, | 14 // browser). There are several basic Response classes implemented in this file, |
15 // all derived from the Response interface. | 15 // all derived from the Response interface. |
16 // | 16 // |
17 // Here's a simple example that starts a web server that can serve up | 17 // Here's a simple example that starts a web server that can serve up |
18 // a single document (http://localhost:1337/foo). | 18 // a single document (http://<server.host()>:1337/foo). |
19 // All other requests will get a 404. | 19 // All other requests will get a 404. |
20 // | 20 // |
21 // MessageLoopForUI loop; | 21 // MessageLoopForUI loop; |
22 // test_server::SimpleWebServer server(1337); | 22 // test_server::SimpleWebServer server(1337); |
23 // test_server::SimpleResponse document("/foo", "Hello World!"); | 23 // test_server::SimpleResponse document("/foo", "Hello World!"); |
24 // test_server.AddResponse(&document); | 24 // test_server.AddResponse(&document); |
25 // loop.MessageLoop::Run(); | 25 // loop.MessageLoop::Run(); |
26 // | 26 // |
27 // To close the web server, just go to http://localhost:1337/quit. | 27 // To close the web server, just go to http://<server.host()>:1337/quit. |
28 // | 28 // |
29 // All Response classes count how many times they have been accessed. Just | 29 // All Response classes count how many times they have been accessed. Just |
30 // call Response::accessed(). | 30 // call Response::accessed(). |
31 // | 31 // |
32 // To implement a custom response object (e.g. to match against a request | 32 // To implement a custom response object (e.g. to match against a request |
33 // based on some data, serve up dynamic content or take some action on the | 33 // based on some data, serve up dynamic content or take some action on the |
34 // server), just inherit from one of the response classes or directly from the | 34 // server), just inherit from one of the response classes or directly from the |
35 // Response interface and add your response object to the server's list of | 35 // Response interface and add your response object to the server's list of |
36 // response objects. | 36 // response objects. |
37 | 37 |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 }; | 275 }; |
276 | 276 |
277 // typedef for a list of connections. Used by SimpleWebServer. | 277 // typedef for a list of connections. Used by SimpleWebServer. |
278 typedef std::list<Connection*> ConnectionList; | 278 typedef std::list<Connection*> ConnectionList; |
279 | 279 |
280 // Implementation of a simple http server. | 280 // Implementation of a simple http server. |
281 // Before creating an instance of the server, make sure the current thread | 281 // Before creating an instance of the server, make sure the current thread |
282 // has a message loop. | 282 // has a message loop. |
283 class SimpleWebServer : public net::StreamListenSocket::Delegate { | 283 class SimpleWebServer : public net::StreamListenSocket::Delegate { |
284 public: | 284 public: |
| 285 // Constructs a server listening at the given port on a local IPv4 address. |
| 286 // An address on a NIC is preferred over the loopback address. |
285 explicit SimpleWebServer(int port); | 287 explicit SimpleWebServer(int port); |
| 288 |
| 289 // Constructs a server listening at the given address:port. |
| 290 SimpleWebServer(const std::string& address, int port); |
286 virtual ~SimpleWebServer(); | 291 virtual ~SimpleWebServer(); |
287 | 292 |
288 void AddResponse(Response* response); | 293 void AddResponse(Response* response); |
289 | 294 |
290 // Ownership of response objects is by default assumed to be outside | 295 // Ownership of response objects is by default assumed to be outside |
291 // of the SimpleWebServer class. | 296 // of the SimpleWebServer class. |
292 // However, if the caller doesn't wish to maintain a list of response objects | 297 // However, if the caller doesn't wish to maintain a list of response objects |
293 // but rather let this class hold the only references to those objects, | 298 // but rather let this class hold the only references to those objects, |
294 // the caller can call this method to delete the objects as part of | 299 // the caller can call this method to delete the objects as part of |
295 // the cleanup process. | 300 // the cleanup process. |
296 void DeleteAllResponses(); | 301 void DeleteAllResponses(); |
297 | 302 |
298 // StreamListenSocket::Delegate overrides. | 303 // StreamListenSocket::Delegate overrides. |
299 virtual void DidAccept(net::StreamListenSocket* server, | 304 virtual void DidAccept(net::StreamListenSocket* server, |
300 net::StreamListenSocket* connection); | 305 net::StreamListenSocket* connection); |
301 virtual void DidRead(net::StreamListenSocket* connection, | 306 virtual void DidRead(net::StreamListenSocket* connection, |
302 const char* data, | 307 const char* data, |
303 int len); | 308 int len); |
304 virtual void DidClose(net::StreamListenSocket* sock); | 309 virtual void DidClose(net::StreamListenSocket* sock); |
305 | 310 |
| 311 // Returns the host on which the server is listening. This is suitable for |
| 312 // use in URLs for resources served by this instance. |
| 313 const std::string& host() const { |
| 314 return host_; |
| 315 } |
| 316 |
306 const ConnectionList& connections() const { | 317 const ConnectionList& connections() const { |
307 return connections_; | 318 return connections_; |
308 } | 319 } |
309 | 320 |
310 protected: | 321 protected: |
311 class QuitResponse : public SimpleResponse { | 322 class QuitResponse : public SimpleResponse { |
312 public: | 323 public: |
313 QuitResponse() | 324 QuitResponse() |
314 : SimpleResponse("/quit", "So long and thanks for all the fish.") { | 325 : SimpleResponse("/quit", "So long and thanks for all the fish.") { |
315 } | 326 } |
316 | 327 |
317 virtual void WriteContents(net::StreamListenSocket* socket) const { | 328 virtual void WriteContents(net::StreamListenSocket* socket) const { |
318 SimpleResponse::WriteContents(socket); | 329 SimpleResponse::WriteContents(socket); |
319 MessageLoop::current()->Quit(); | 330 MessageLoop::current()->Quit(); |
320 } | 331 } |
321 }; | 332 }; |
322 | 333 |
323 Response* FindResponse(const Request& request) const; | 334 Response* FindResponse(const Request& request) const; |
324 Connection* FindConnection(const net::StreamListenSocket* socket) const; | 335 Connection* FindConnection(const net::StreamListenSocket* socket) const; |
325 | 336 |
326 protected: | 337 std::string host_; |
327 scoped_refptr<net::StreamListenSocket> server_; | 338 scoped_refptr<net::StreamListenSocket> server_; |
328 ConnectionList connections_; | 339 ConnectionList connections_; |
329 std::list<Response*> responses_; | 340 std::list<Response*> responses_; |
330 QuitResponse quit_; | 341 QuitResponse quit_; |
331 | 342 |
332 private: | 343 private: |
| 344 void Construct(const std::string& address, int port); |
333 DISALLOW_COPY_AND_ASSIGN(SimpleWebServer); | 345 DISALLOW_COPY_AND_ASSIGN(SimpleWebServer); |
334 }; | 346 }; |
335 | 347 |
336 // Simple class holding incoming HTTP request. Can send the HTTP response | 348 // Simple class holding incoming HTTP request. Can send the HTTP response |
337 // at different rate - small chunks, on regular interval. | 349 // at different rate - small chunks, on regular interval. |
338 class ConfigurableConnection : public base::RefCounted<ConfigurableConnection> { | 350 class ConfigurableConnection : public base::RefCounted<ConfigurableConnection> { |
339 public: | 351 public: |
340 struct SendOptions { | 352 struct SendOptions { |
341 enum Speed { IMMEDIATE, DELAYED, IMMEDIATE_HEADERS_DELAYED_CONTENT }; | 353 enum Speed { IMMEDIATE, DELAYED, IMMEDIATE_HEADERS_DELAYED_CONTENT }; |
342 SendOptions() : speed_(IMMEDIATE), chunk_size_(0), timeout_(0) { } | 354 SendOptions() : speed_(IMMEDIATE), chunk_size_(0), timeout_(0) { } |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 | 437 |
426 scoped_refptr<net::StreamListenSocket> server_; | 438 scoped_refptr<net::StreamListenSocket> server_; |
427 ConnectionList connection_list_; | 439 ConnectionList connection_list_; |
428 | 440 |
429 DISALLOW_COPY_AND_ASSIGN(HTTPTestServer); | 441 DISALLOW_COPY_AND_ASSIGN(HTTPTestServer); |
430 }; | 442 }; |
431 | 443 |
432 } // namespace test_server | 444 } // namespace test_server |
433 | 445 |
434 #endif // CHROME_FRAME_TEST_TEST_SERVER_H_ | 446 #endif // CHROME_FRAME_TEST_TEST_SERVER_H_ |
OLD | NEW |