Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / net / server / http_server.h
blob2ae698b98c5329424e31b2b2bfab616fad85c3ac
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 OnHttpRequest(int connection_id,
34 const HttpServerRequestInfo& info) = 0;
35 virtual void OnWebSocketRequest(int connection_id,
36 const HttpServerRequestInfo& info) = 0;
37 virtual void OnWebSocketMessage(int connection_id,
38 const std::string& data) = 0;
39 virtual void OnClose(int connection_id) = 0;
42 HttpServer(scoped_ptr<ServerSocket> server_socket,
43 HttpServer::Delegate* delegate);
44 ~HttpServer();
46 void AcceptWebSocket(int connection_id,
47 const HttpServerRequestInfo& request);
48 void SendOverWebSocket(int connection_id, const std::string& data);
49 // Sends the provided data directly to the given connection. No validation is
50 // performed that data constitutes a valid HTTP response. A valid HTTP
51 // response may be split across multiple calls to SendRaw.
52 void SendRaw(int connection_id, const std::string& data);
53 // TODO(byungchul): Consider replacing function name with SendResponseInfo
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 void SetReceiveBufferSize(int connection_id, int32 size);
68 void SetSendBufferSize(int connection_id, int32 size);
70 // Copies the local address to |address|. Returns a network error code.
71 int GetLocalAddress(IPEndPoint* address);
73 private:
74 friend class HttpServerTest;
76 typedef std::map<int, HttpConnection*> IdToConnectionMap;
78 void DoAcceptLoop();
79 void OnAcceptCompleted(int rv);
80 int HandleAcceptResult(int rv);
82 void DoReadLoop(HttpConnection* connection);
83 void OnReadCompleted(int connection_id, int rv);
84 int HandleReadResult(HttpConnection* connection, int rv);
86 void DoWriteLoop(HttpConnection* connection);
87 void OnWriteCompleted(int connection_id, int rv);
88 int HandleWriteResult(HttpConnection* connection, int rv);
90 // Expects the raw data to be stored in recv_data_. If parsing is successful,
91 // will remove the data parsed from recv_data_, leaving only the unused
92 // recv data.
93 bool ParseHeaders(const char* data,
94 size_t data_len,
95 HttpServerRequestInfo* info,
96 size_t* pos);
98 HttpConnection* FindConnection(int connection_id);
100 // Whether or not Close() has been called during delegate callback processing.
101 bool HasClosedConnection(HttpConnection* connection);
103 const scoped_ptr<ServerSocket> server_socket_;
104 scoped_ptr<StreamSocket> accepted_socket_;
105 HttpServer::Delegate* const delegate_;
107 int last_id_;
108 IdToConnectionMap id_to_connection_;
110 base::WeakPtrFactory<HttpServer> weak_ptr_factory_;
112 DISALLOW_COPY_AND_ASSIGN(HttpServer);
115 } // namespace net
117 #endif // NET_SERVER_HTTP_SERVER_H_