Update SplitString calls to new form
[chromium-blink-merge.git] / net / server / http_server.h
blobb8be0ed6ab67889152636e33c52e8a883c087d50
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 ~Delegate() {}
35 virtual void OnConnect(int connection_id) = 0;
36 virtual void OnHttpRequest(int connection_id,
37 const HttpServerRequestInfo& info) = 0;
38 virtual void OnWebSocketRequest(int connection_id,
39 const HttpServerRequestInfo& info) = 0;
40 virtual void OnWebSocketMessage(int connection_id,
41 const std::string& data) = 0;
42 virtual void OnClose(int connection_id) = 0;
45 // Instantiates a http server with |server_socket| which already started
46 // listening, but not accepting. This constructor schedules accepting
47 // connections asynchronously in case when |delegate| is not ready to get
48 // callbacks yet.
49 HttpServer(scoped_ptr<ServerSocket> server_socket,
50 HttpServer::Delegate* delegate);
51 ~HttpServer();
53 void AcceptWebSocket(int connection_id,
54 const HttpServerRequestInfo& request);
55 void SendOverWebSocket(int connection_id, const std::string& data);
56 // Sends the provided data directly to the given connection. No validation is
57 // performed that data constitutes a valid HTTP response. A valid HTTP
58 // response may be split across multiple calls to SendRaw.
59 void SendRaw(int connection_id, const std::string& data);
60 // TODO(byungchul): Consider replacing function name with SendResponseInfo
61 void SendResponse(int connection_id, const HttpServerResponseInfo& response);
62 void Send(int connection_id,
63 HttpStatusCode status_code,
64 const std::string& data,
65 const std::string& mime_type);
66 void Send200(int connection_id,
67 const std::string& data,
68 const std::string& mime_type);
69 void Send404(int connection_id);
70 void Send500(int connection_id, const std::string& message);
72 void Close(int connection_id);
74 void SetReceiveBufferSize(int connection_id, int32 size);
75 void SetSendBufferSize(int connection_id, int32 size);
77 // Copies the local address to |address|. Returns a network error code.
78 int GetLocalAddress(IPEndPoint* address);
80 private:
81 friend class HttpServerTest;
83 typedef std::map<int, HttpConnection*> IdToConnectionMap;
85 void DoAcceptLoop();
86 void OnAcceptCompleted(int rv);
87 int HandleAcceptResult(int rv);
89 void DoReadLoop(HttpConnection* connection);
90 void OnReadCompleted(int connection_id, int rv);
91 int HandleReadResult(HttpConnection* connection, int rv);
93 void DoWriteLoop(HttpConnection* connection);
94 void OnWriteCompleted(int connection_id, int rv);
95 int HandleWriteResult(HttpConnection* connection, int rv);
97 // Expects the raw data to be stored in recv_data_. If parsing is successful,
98 // will remove the data parsed from recv_data_, leaving only the unused
99 // recv data.
100 bool ParseHeaders(const char* data,
101 size_t data_len,
102 HttpServerRequestInfo* info,
103 size_t* pos);
105 HttpConnection* FindConnection(int connection_id);
107 // Whether or not Close() has been called during delegate callback processing.
108 bool HasClosedConnection(HttpConnection* connection);
110 const scoped_ptr<ServerSocket> server_socket_;
111 scoped_ptr<StreamSocket> accepted_socket_;
112 HttpServer::Delegate* const delegate_;
114 int last_id_;
115 IdToConnectionMap id_to_connection_;
117 base::WeakPtrFactory<HttpServer> weak_ptr_factory_;
119 DISALLOW_COPY_AND_ASSIGN(HttpServer);
122 } // namespace net
124 #endif // NET_SERVER_HTTP_SERVER_H_