Extract protobuf database into a new 'leveldb_proto' component
[chromium-blink-merge.git] / net / server / http_server.h
blob4309d122f1ead040eb28f2c2557a489b9fbff1b6
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 NET_SERVER_HTTP_SERVER_H_
6 #define NET_SERVER_HTTP_SERVER_H_
8 #include <list>
9 #include <map>
11 #include "base/basictypes.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "net/http/http_status_code.h"
14 #include "net/socket/stream_listen_socket.h"
16 namespace net {
18 class HttpConnection;
19 class HttpServerRequestInfo;
20 class HttpServerResponseInfo;
21 class IPEndPoint;
22 class WebSocket;
24 class HttpServer : public StreamListenSocket::Delegate,
25 public base::RefCountedThreadSafe<HttpServer> {
26 public:
27 class Delegate {
28 public:
29 virtual void OnHttpRequest(int connection_id,
30 const HttpServerRequestInfo& info) = 0;
32 virtual void OnWebSocketRequest(int connection_id,
33 const HttpServerRequestInfo& info) = 0;
35 virtual void OnWebSocketMessage(int connection_id,
36 const std::string& data) = 0;
38 virtual void OnClose(int connection_id) = 0;
40 protected:
41 virtual ~Delegate() {}
44 HttpServer(const StreamListenSocketFactory& socket_factory,
45 HttpServer::Delegate* delegate);
47 void AcceptWebSocket(int connection_id,
48 const HttpServerRequestInfo& request);
49 void SendOverWebSocket(int connection_id, const std::string& data);
50 // Sends the provided data directly to the given connection. No validation is
51 // performed that data constitutes a valid HTTP response. A valid HTTP
52 // response may be split across multiple calls to SendRaw.
53 void SendRaw(int connection_id, const std::string& data);
54 void SendResponse(int connection_id, const HttpServerResponseInfo& response);
55 void Send(int connection_id,
56 HttpStatusCode status_code,
57 const std::string& data,
58 const std::string& mime_type);
59 void Send200(int connection_id,
60 const std::string& data,
61 const std::string& mime_type);
62 void Send404(int connection_id);
63 void Send500(int connection_id, const std::string& message);
65 void Close(int connection_id);
67 // Copies the local address to |address|. Returns a network error code.
68 int GetLocalAddress(IPEndPoint* address);
70 // ListenSocketDelegate
71 virtual void DidAccept(StreamListenSocket* server,
72 scoped_ptr<StreamListenSocket> socket) OVERRIDE;
73 virtual void DidRead(StreamListenSocket* socket,
74 const char* data,
75 int len) OVERRIDE;
76 virtual void DidClose(StreamListenSocket* socket) OVERRIDE;
78 protected:
79 virtual ~HttpServer();
81 private:
82 friend class base::RefCountedThreadSafe<HttpServer>;
83 friend class HttpConnection;
85 // Expects the raw data to be stored in recv_data_. If parsing is successful,
86 // will remove the data parsed from recv_data_, leaving only the unused
87 // recv data.
88 bool ParseHeaders(HttpConnection* connection,
89 HttpServerRequestInfo* info,
90 size_t* pos);
92 HttpConnection* FindConnection(int connection_id);
93 HttpConnection* FindConnection(StreamListenSocket* socket);
95 HttpServer::Delegate* delegate_;
96 scoped_ptr<StreamListenSocket> server_;
97 typedef std::map<int, HttpConnection*> IdToConnectionMap;
98 IdToConnectionMap id_to_connection_;
99 typedef std::map<StreamListenSocket*, HttpConnection*> SocketToConnectionMap;
100 SocketToConnectionMap socket_to_connection_;
102 DISALLOW_COPY_AND_ASSIGN(HttpServer);
105 } // namespace net
107 #endif // NET_SERVER_HTTP_SERVER_H_