Fix build break
[chromium-blink-merge.git] / chrome / browser / google_apis / test_server / http_server.h
blob9f288eeb8c82bc16998bb6d6503a612a6fb99c56
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.
5 #ifndef CHROME_BROWSER_GOOGLE_APIS_TEST_SERVER_HTTP_SERVER_H_
6 #define CHROME_BROWSER_GOOGLE_APIS_TEST_SERVER_HTTP_SERVER_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/weak_ptr.h"
16 #include "googleurl/src/gurl.h"
17 #include "net/socket/tcp_listen_socket.h"
19 namespace google_apis {
20 namespace test_server {
22 class HttpConnection;
23 class HttpResponse;
24 struct HttpRequest;
26 // This class is required to be able to have composition instead of inheritance,
27 class HttpListenSocket: public net::TCPListenSocket {
28 public:
29 HttpListenSocket(const SocketDescriptor socket_descriptor,
30 net::StreamListenSocket::Delegate* delegate);
31 virtual void Listen();
33 private:
34 virtual ~HttpListenSocket();
37 // 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,
39 // it assumes that the request syntax is correct. It *does not* support
40 // a Chunked Transfer Encoding.
42 // The common use case is below:
44 // scoped_ptr<HttpServer> test_server_;
46 // void SetUp() {
47 // test_server_.reset(new HttpServer());
48 // DCHECK(test_server_.InitializeAndWaitUntilReady());
49 // test_server_->RegisterRequestHandler(
50 // base::Bind(&FooTest::HandleRequest, base::Unretained(this)));
51 // }
53 // scoped_ptr<HttpResponse> HandleRequest(const HttpRequest& request) {
54 // GURL absolute_url = test_server_->GetURL(request.relative_url);
55 // if (absolute_url.path() != "/test")
56 // return scoped_ptr<HttpResponse>();
58 // scoped_ptr<HttpResponse> http_response(new HttpResponse());
59 // http_response->set_code(test_server::SUCCESS);
60 // http_response->set_content("hello");
61 // http_response->set_content_type("text/plain");
62 // return http_response.Pass();
63 // }
65 class HttpServer : public net::StreamListenSocket::Delegate {
66 public:
67 typedef base::Callback<scoped_ptr<HttpResponse>(const HttpRequest& request)>
68 HandleRequestCallback;
70 // Creates a http test server. InitializeAndWaitUntilReady() must be called
71 // to start the server.
72 HttpServer();
73 virtual ~HttpServer();
75 // Initializes and waits until the server is ready to accept requests.
76 bool InitializeAndWaitUntilReady();
78 // Shuts down the http server and waits until the shutdown is complete.
79 void ShutdownAndWaitUntilComplete();
81 // Checks if the server is started.
82 bool Started() const {
83 return listen_socket_.get() != NULL;
86 // 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
88 // the server.
89 const GURL& base_url() const { return base_url_; }
91 // Returns a URL to the server based on the given relative URL, which
92 // should start with '/'. For example: GetURL("/path?query=foo") =>
93 // http://127.0.0.1:<port>/path?query=foo.
94 GURL GetURL(const std::string& relative_url) const;
96 // Returns the port number used by the server.
97 int port() const { return port_; }
99 // The most general purpose method. Any request processing can be added using
100 // this method. Takes ownership of the object. The |callback| is called
101 // on UI thread.
102 void RegisterRequestHandler(const HandleRequestCallback& callback);
104 private:
105 // Initializes and starts the server. If initialization succeeds, Starts()
106 // will return true.
107 void InitializeOnIOThread();
109 // Shuts down the server.
110 void ShutdownOnIOThread();
112 // Handles a request when it is parsed. It passes the request to registed
113 // request handlers and sends a http response.
114 void HandleRequest(HttpConnection* connection,
115 scoped_ptr<HttpRequest> request);
117 // net::StreamListenSocket::Delegate overrides:
118 virtual void DidAccept(net::StreamListenSocket* server,
119 net::StreamListenSocket* connection) OVERRIDE;
120 virtual void DidRead(net::StreamListenSocket* connection,
121 const char* data,
122 int length) OVERRIDE;
123 virtual void DidClose(net::StreamListenSocket* connection) OVERRIDE;
125 HttpConnection* FindConnection(net::StreamListenSocket* socket);
127 scoped_refptr<HttpListenSocket> listen_socket_;
128 int port_;
129 GURL base_url_;
131 // Owns the HttpConnection objects.
132 std::map<net::StreamListenSocket*, HttpConnection*> connections_;
134 // Vector of registered request handlers.
135 std::vector<HandleRequestCallback> request_handlers_;
137 // Note: This should remain the last member so it'll be destroyed and
138 // invalidate its weak pointers before any other members are destroyed.
139 base::WeakPtrFactory<HttpServer> weak_factory_;
141 DISALLOW_COPY_AND_ASSIGN(HttpServer);
144 } // namespace test_servers
145 } // namespace google_apis
147 #endif // CHROME_BROWSER_GOOGLE_APIS_TEST_SERVER_HTTP_SERVER_H_