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_
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"
20 class HttpServerRequestInfo
;
21 class HttpServerResponseInfo
;
29 // Delegate to handle http/websocket events. Beware that it is not safe to
30 // destroy the HttpServer in any of these callbacks.
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
);
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
);
75 friend class HttpServerTest
;
77 typedef std::map
<int, HttpConnection
*> IdToConnectionMap
;
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
94 bool ParseHeaders(const char* data
,
96 HttpServerRequestInfo
* info
,
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_
;
109 IdToConnectionMap id_to_connection_
;
111 base::WeakPtrFactory
<HttpServer
> weak_ptr_factory_
;
113 DISALLOW_COPY_AND_ASSIGN(HttpServer
);
118 #endif // NET_SERVER_HTTP_SERVER_H_