Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_GOOGLE_APIS_TEST_SERVER_HTTP_SERVER_H_ | |
| 6 #define CHROME_BROWSER_GOOGLE_APIS_TEST_SERVER_HTTP_SERVER_H_ | |
| 7 | |
| 8 #include <string> | |
| 9 #include <vector> | |
| 10 #include "base/basictypes.h" | |
| 11 #include "base/compiler_specific.h" | |
| 12 #include "base/file_path.h" | |
| 13 #include "base/memory/ref_counted.h" | |
| 14 #include "chrome/browser/google_apis/test_server/http_connection.h" | |
| 15 #include "chrome/browser/google_apis/test_server/http_response.h" | |
| 16 #include "chrome/browser/google_apis/gdata_test_util.h" | |
| 17 #include "net/base/tcp_listen_socket.h" | |
| 18 | |
| 19 namespace drive { | |
| 20 namespace test_server { | |
| 21 | |
| 22 // This class is required to be able to have composition instead of inheritance, | |
| 23 class HttpListenSocket: public net::TCPListenSocket { | |
| 24 public: | |
| 25 HttpListenSocket(const SocketDescriptor socket_descriptor, | |
| 26 net::StreamListenSocket::Delegate* delegate); | |
| 27 virtual void Listen(); | |
| 28 | |
| 29 private: | |
| 30 virtual ~HttpListenSocket(); | |
| 31 }; | |
| 32 | |
| 33 // Class providing a HTTP server for testing purpose. This is a basic server | |
| 34 // providing only an essential subset of HTTP/1.1 protocol. Especially, | |
| 35 // it assumes that the request syntax is correct. It *does not* support | |
| 36 // a Chunked Transfer Encoding. | |
| 37 // | |
| 38 // The common use case is below: | |
| 39 // | |
| 40 // scoped_ptr<HttpServer> test_server_; | |
| 41 // GURL hello_world_url_; | |
| 42 // GURL file_url_; | |
| 43 // (...) | |
| 44 // void SetUp() { | |
| 45 // test_server_.reset(new HttpServer()); | |
| 46 // hello_world_url = test_server->RegisterSimpleTextResponse( | |
| 47 // "<b>Hello world!</b>", | |
| 48 // "text/html"); | |
| 49 // metadata_url = test_server->RegisterFileResponse( | |
| 50 // "metadata/file.doc") | |
| 51 // "testing/data/metadata.xml", | |
| 52 // "text/xml", | |
| 53 // 200); | |
| 54 // } | |
| 55 class HttpServer : private net::StreamListenSocket::Delegate { | |
| 56 public: | |
| 57 typedef base::Callback<void(HttpServer* server)> CreateCallback; | |
| 58 typedef base::Callback<scoped_ptr<HttpResponse>(const HttpRequest* request)> | |
|
satorux1
2012/11/14 01:42:23
let's make it a const reference: const HttpRequest
mtomasz
2012/11/14 03:23:35
Done.
| |
| 59 HandleRequestCallback; | |
| 60 | |
| 61 ~HttpServer(); | |
| 62 | |
| 63 // Factory method. Created a server instance and returns a pointer or NULL in | |
| 64 // case of failure. Can be run from any thread. It is blocking. | |
| 65 static scoped_ptr<HttpServer> CreateForTesting(); | |
| 66 | |
| 67 // Provides URL to the server which is useful when general purpose provider | |
| 68 // is registered. | |
| 69 GURL GetBaseURL(); | |
| 70 | |
| 71 // The most general purpose method. Any request processing can be added using | |
| 72 // this method. Takes ownership of the object. The |callback| is called | |
| 73 // on UI thread. | |
| 74 void RegisterRequestHandler(const HandleRequestCallback& callback); | |
| 75 | |
| 76 // Used to provide the same predefined response for the requests matching | |
| 77 // the |relative_path|. Should be used if any custom data, such as additional | |
| 78 // headers should be send from the server. | |
| 79 GURL RegisterDefaultResponse( | |
| 80 const std::string& relative_path, | |
| 81 const HttpResponse& default_response); | |
| 82 | |
| 83 // Registers a simple text response. | |
| 84 GURL RegisterTextResponse( | |
| 85 const std::string& relative_path, | |
| 86 const std::string& content, | |
| 87 const std::string& content_type, | |
| 88 const ResponseCode response_code); | |
| 89 | |
| 90 // Registers a simple file response. The file is loaded into memory. | |
| 91 GURL RegisterFileResponse( | |
| 92 const std::string& relative_path, | |
| 93 const FilePath& file_path, | |
| 94 const std::string& content_type, | |
| 95 const ResponseCode response_code); | |
| 96 | |
| 97 private: | |
| 98 // Creates and starts server and attaches it to |socket_descriptor| socket. | |
| 99 // Passed |port| should be a valid port name, on which the socket is | |
| 100 // listening. Use factory methods to create an instance of the server. | |
| 101 HttpServer(int port, const SocketDescriptor& socket_descriptor); | |
| 102 | |
| 103 // Factory method. Created a server instance and returns a pointer to it | |
| 104 // through the |callback|. Returns NULL in case of failure. Must be called | |
| 105 // from IO thread. | |
| 106 static void CreateOnIOThread(const CreateCallback& callback); | |
| 107 | |
| 108 // Handles a request when it is parsed. It passes the request to registed | |
| 109 // request handlers and sends a http response. | |
| 110 void HandleRequest(HttpConnection* connection, | |
| 111 scoped_ptr<HttpRequest> request); | |
| 112 | |
| 113 // |server| is the original listening Socket, connection is the new | |
| 114 // Socket that was created. Ownership of |connection| is transferred | |
| 115 // to the delegate with this call. | |
| 116 void DidAccept(net::StreamListenSocket* server, | |
| 117 net::StreamListenSocket* connection) OVERRIDE; | |
| 118 void DidRead(net::StreamListenSocket* connection, | |
| 119 const char* data, | |
| 120 int length) OVERRIDE; | |
| 121 void DidClose(net::StreamListenSocket* connection) OVERRIDE; | |
| 122 | |
| 123 HttpConnection* FindConnection(net::StreamListenSocket* socket); | |
| 124 | |
| 125 scoped_refptr<HttpListenSocket> listen_socket_; | |
| 126 int port_; | |
| 127 GURL base_url_; | |
| 128 int last_unique_id_; | |
|
satorux1
2012/11/14 01:42:23
remove this.
mtomasz
2012/11/14 03:23:35
Done.
| |
| 129 | |
| 130 // Owns the HttpConnection objects. | |
| 131 std::map<net::StreamListenSocket*, HttpConnection*> connections_; | |
| 132 | |
| 133 // Vector of registered request handlers. | |
| 134 std::vector<HandleRequestCallback> request_handlers_; | |
| 135 | |
| 136 // Note: This should remain the last member so it'll be destroyed and | |
| 137 // invalidate its weak pointers before any other members are destroyed. | |
| 138 base::WeakPtrFactory<HttpServer> weak_factory_; | |
| 139 DISALLOW_COPY_AND_ASSIGN(HttpServer); | |
| 140 }; | |
| 141 | |
| 142 } // namespace test_servers | |
| 143 } // namespace drive | |
| 144 | |
| 145 #endif // CHROME_BROWSER_GOOGLE_APIS_TEST_SERVER_HTTP_SERVER_H_ | |
| OLD | NEW |