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