Add --output-dir flag for page_runner.py to specify a custom directory for
[chromium-blink-merge.git] / net / server / http_server.h
blob47214ab158ec639436894790579d19ced2bb84c7
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 HttpServer(scoped_ptr<ServerSocket> server_socket,
44 HttpServer::Delegate* delegate);
45 ~HttpServer();
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 // TODO(byungchul): Consider replacing function name with SendResponseInfo
55 void SendResponse(int connection_id, const HttpServerResponseInfo& response);
56 void Send(int connection_id,
57 HttpStatusCode status_code,
58 const std::string& data,
59 const std::string& mime_type);
60 void Send200(int connection_id,
61 const std::string& data,
62 const std::string& mime_type);
63 void Send404(int connection_id);
64 void Send500(int connection_id, const std::string& message);
66 void Close(int connection_id);
68 void SetReceiveBufferSize(int connection_id, int32 size);
69 void SetSendBufferSize(int connection_id, int32 size);
71 // Copies the local address to |address|. Returns a network error code.
72 int GetLocalAddress(IPEndPoint* address);
74 private:
75 friend class HttpServerTest;
77 typedef std::map<int, HttpConnection*> IdToConnectionMap;
79 void DoAcceptLoop();
80 void OnAcceptCompleted(int rv);
81 int HandleAcceptResult(int rv);
83 void DoReadLoop(HttpConnection* connection);
84 void OnReadCompleted(int connection_id, int rv);
85 int HandleReadResult(HttpConnection* connection, int rv);
87 void DoWriteLoop(HttpConnection* connection);
88 void OnWriteCompleted(int connection_id, int rv);
89 int HandleWriteResult(HttpConnection* connection, int rv);
91 // Expects the raw data to be stored in recv_data_. If parsing is successful,
92 // will remove the data parsed from recv_data_, leaving only the unused
93 // recv data.
94 bool ParseHeaders(const char* data,
95 size_t data_len,
96 HttpServerRequestInfo* info,
97 size_t* pos);
99 HttpConnection* FindConnection(int connection_id);
101 // Whether or not Close() has been called during delegate callback processing.
102 bool HasClosedConnection(HttpConnection* connection);
104 const scoped_ptr<ServerSocket> server_socket_;
105 scoped_ptr<StreamSocket> accepted_socket_;
106 HttpServer::Delegate* const delegate_;
108 int last_id_;
109 IdToConnectionMap id_to_connection_;
111 base::WeakPtrFactory<HttpServer> weak_ptr_factory_;
113 DISALLOW_COPY_AND_ASSIGN(HttpServer);
116 } // namespace net
118 #endif // NET_SERVER_HTTP_SERVER_H_