Pass cursor position to the autcomplete controller.
[chromium-blink-merge.git] / net / server / http_server.h
blob89a4b45f3254073f472c4cb1204080d1d9d8cb50
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 <map>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/macros.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "net/http/http_status_code.h"
17 namespace net {
19 class HttpConnection;
20 class HttpServerRequestInfo;
21 class HttpServerResponseInfo;
22 class IPEndPoint;
23 class ServerSocket;
24 class StreamSocket;
25 class WebSocket;
27 class HttpServer {
28 public:
29 // Delegate to handle http/websocket events. Beware that it is not safe to
30 // destroy the HttpServer in any of these callbacks.
31 class Delegate {
32 public:
33 virtual void OnConnect(int connection_id) = 0;
34 virtual void OnHttpRequest(int connection_id,
35 const HttpServerRequestInfo& info) = 0;
36 virtual void OnWebSocketRequest(int connection_id,
37 const HttpServerRequestInfo& info) = 0;
38 virtual void OnWebSocketMessage(int connection_id,
39 const std::string& data) = 0;
40 virtual void OnClose(int connection_id) = 0;
43 // Instantiates a http server with |server_socket| which already started
44 // listening, but not accepting. This constructor schedules accepting
45 // connections asynchronously in case when |delegate| is not ready to get
46 // callbacks yet.
47 HttpServer(scoped_ptr<ServerSocket> server_socket,
48 HttpServer::Delegate* delegate);
49 ~HttpServer();
51 void AcceptWebSocket(int connection_id,
52 const HttpServerRequestInfo& request);
53 void SendOverWebSocket(int connection_id, const std::string& data);
54 // Sends the provided data directly to the given connection. No validation is
55 // performed that data constitutes a valid HTTP response. A valid HTTP
56 // response may be split across multiple calls to SendRaw.
57 void SendRaw(int connection_id, const std::string& data);
58 // TODO(byungchul): Consider replacing function name with SendResponseInfo
59 void SendResponse(int connection_id, const HttpServerResponseInfo& response);
60 void Send(int connection_id,
61 HttpStatusCode status_code,
62 const std::string& data,
63 const std::string& mime_type);
64 void Send200(int connection_id,
65 const std::string& data,
66 const std::string& mime_type);
67 void Send404(int connection_id);
68 void Send500(int connection_id, const std::string& message);
70 void Close(int connection_id);
72 void SetReceiveBufferSize(int connection_id, int32 size);
73 void SetSendBufferSize(int connection_id, int32 size);
75 // Copies the local address to |address|. Returns a network error code.
76 int GetLocalAddress(IPEndPoint* address);
78 private:
79 friend class HttpServerTest;
81 typedef std::map<int, HttpConnection*> IdToConnectionMap;
83 void DoAcceptLoop();
84 void OnAcceptCompleted(int rv);
85 int HandleAcceptResult(int rv);
87 void DoReadLoop(HttpConnection* connection);
88 void OnReadCompleted(int connection_id, int rv);
89 int HandleReadResult(HttpConnection* connection, int rv);
91 void DoWriteLoop(HttpConnection* connection);
92 void OnWriteCompleted(int connection_id, int rv);
93 int HandleWriteResult(HttpConnection* connection, int rv);
95 // Expects the raw data to be stored in recv_data_. If parsing is successful,
96 // will remove the data parsed from recv_data_, leaving only the unused
97 // recv data.
98 bool ParseHeaders(const char* data,
99 size_t data_len,
100 HttpServerRequestInfo* info,
101 size_t* pos);
103 HttpConnection* FindConnection(int connection_id);
105 // Whether or not Close() has been called during delegate callback processing.
106 bool HasClosedConnection(HttpConnection* connection);
108 const scoped_ptr<ServerSocket> server_socket_;
109 scoped_ptr<StreamSocket> accepted_socket_;
110 HttpServer::Delegate* const delegate_;
112 int last_id_;
113 IdToConnectionMap id_to_connection_;
115 base::WeakPtrFactory<HttpServer> weak_ptr_factory_;
117 DISALLOW_COPY_AND_ASSIGN(HttpServer);
120 } // namespace net
122 #endif // NET_SERVER_HTTP_SERVER_H_