OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_SERVER_HTTP_SERVER_H_ | 5 #ifndef NET_SERVER_HTTP_SERVER_H_ |
6 #define NET_SERVER_HTTP_SERVER_H_ | 6 #define NET_SERVER_HTTP_SERVER_H_ |
7 | 7 |
| 8 #include <list> |
8 #include <map> | 9 #include <map> |
9 #include <string> | |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/macros.h" | |
13 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
14 #include "base/memory/weak_ptr.h" | |
15 #include "net/http/http_status_code.h" | 13 #include "net/http/http_status_code.h" |
| 14 #include "net/socket/stream_listen_socket.h" |
16 | 15 |
17 namespace net { | 16 namespace net { |
18 | 17 |
19 class HttpConnection; | 18 class HttpConnection; |
20 class HttpServerRequestInfo; | 19 class HttpServerRequestInfo; |
21 class HttpServerResponseInfo; | 20 class HttpServerResponseInfo; |
22 class IPEndPoint; | 21 class IPEndPoint; |
23 class ServerSocket; | |
24 class StreamSocket; | |
25 class WebSocket; | 22 class WebSocket; |
26 | 23 |
27 class HttpServer { | 24 class HttpServer : public StreamListenSocket::Delegate, |
| 25 public base::RefCountedThreadSafe<HttpServer> { |
28 public: | 26 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 { | 27 class Delegate { |
32 public: | 28 public: |
33 virtual void OnHttpRequest(int connection_id, | 29 virtual void OnHttpRequest(int connection_id, |
34 const HttpServerRequestInfo& info) = 0; | 30 const HttpServerRequestInfo& info) = 0; |
| 31 |
35 virtual void OnWebSocketRequest(int connection_id, | 32 virtual void OnWebSocketRequest(int connection_id, |
36 const HttpServerRequestInfo& info) = 0; | 33 const HttpServerRequestInfo& info) = 0; |
| 34 |
37 virtual void OnWebSocketMessage(int connection_id, | 35 virtual void OnWebSocketMessage(int connection_id, |
38 const std::string& data) = 0; | 36 const std::string& data) = 0; |
| 37 |
39 virtual void OnClose(int connection_id) = 0; | 38 virtual void OnClose(int connection_id) = 0; |
| 39 |
| 40 protected: |
| 41 virtual ~Delegate() {} |
40 }; | 42 }; |
41 | 43 |
42 HttpServer(scoped_ptr<ServerSocket> server_socket, | 44 HttpServer(const StreamListenSocketFactory& socket_factory, |
43 HttpServer::Delegate* delegate); | 45 HttpServer::Delegate* delegate); |
44 ~HttpServer(); | |
45 | 46 |
46 void AcceptWebSocket(int connection_id, | 47 void AcceptWebSocket(int connection_id, |
47 const HttpServerRequestInfo& request); | 48 const HttpServerRequestInfo& request); |
48 void SendOverWebSocket(int connection_id, const std::string& data); | 49 void SendOverWebSocket(int connection_id, const std::string& data); |
49 // Sends the provided data directly to the given connection. No validation is | 50 // 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 // performed that data constitutes a valid HTTP response. A valid HTTP |
51 // response may be split across multiple calls to SendRaw. | 52 // response may be split across multiple calls to SendRaw. |
52 void SendRaw(int connection_id, const std::string& data); | 53 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); | 54 void SendResponse(int connection_id, const HttpServerResponseInfo& response); |
55 void Send(int connection_id, | 55 void Send(int connection_id, |
56 HttpStatusCode status_code, | 56 HttpStatusCode status_code, |
57 const std::string& data, | 57 const std::string& data, |
58 const std::string& mime_type); | 58 const std::string& mime_type); |
59 void Send200(int connection_id, | 59 void Send200(int connection_id, |
60 const std::string& data, | 60 const std::string& data, |
61 const std::string& mime_type); | 61 const std::string& mime_type); |
62 void Send404(int connection_id); | 62 void Send404(int connection_id); |
63 void Send500(int connection_id, const std::string& message); | 63 void Send500(int connection_id, const std::string& message); |
64 | 64 |
65 void Close(int connection_id); | 65 void Close(int connection_id); |
66 | 66 |
67 void SetReceiveBufferSize(int connection_id, int32 size); | |
68 void SetSendBufferSize(int connection_id, int32 size); | |
69 | |
70 // Copies the local address to |address|. Returns a network error code. | 67 // Copies the local address to |address|. Returns a network error code. |
71 int GetLocalAddress(IPEndPoint* address); | 68 int GetLocalAddress(IPEndPoint* address); |
72 | 69 |
| 70 // ListenSocketDelegate |
| 71 virtual void DidAccept(StreamListenSocket* server, |
| 72 scoped_ptr<StreamListenSocket> socket) OVERRIDE; |
| 73 virtual void DidRead(StreamListenSocket* socket, |
| 74 const char* data, |
| 75 int len) OVERRIDE; |
| 76 virtual void DidClose(StreamListenSocket* socket) OVERRIDE; |
| 77 |
| 78 protected: |
| 79 virtual ~HttpServer(); |
| 80 |
73 private: | 81 private: |
74 friend class HttpServerTest; | 82 friend class base::RefCountedThreadSafe<HttpServer>; |
75 | 83 friend class HttpConnection; |
76 typedef std::map<int, HttpConnection*> IdToConnectionMap; | |
77 | |
78 void DoAcceptLoop(); | |
79 void OnAcceptCompleted(int rv); | |
80 int HandleAcceptResult(int rv); | |
81 | |
82 void DoReadLoop(HttpConnection* connection); | |
83 void OnReadCompleted(int connection_id, int rv); | |
84 int HandleReadResult(HttpConnection* connection, int rv); | |
85 | |
86 void DoWriteLoop(HttpConnection* connection); | |
87 void OnWriteCompleted(int connection_id, int rv); | |
88 int HandleWriteResult(HttpConnection* connection, int rv); | |
89 | 84 |
90 // Expects the raw data to be stored in recv_data_. If parsing is successful, | 85 // 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 | 86 // will remove the data parsed from recv_data_, leaving only the unused |
92 // recv data. | 87 // recv data. |
93 bool ParseHeaders(const char* data, | 88 bool ParseHeaders(HttpConnection* connection, |
94 size_t data_len, | |
95 HttpServerRequestInfo* info, | 89 HttpServerRequestInfo* info, |
96 size_t* pos); | 90 size_t* pos); |
97 | 91 |
98 HttpConnection* FindConnection(int connection_id); | 92 HttpConnection* FindConnection(int connection_id); |
| 93 HttpConnection* FindConnection(StreamListenSocket* socket); |
99 | 94 |
100 // Whether or not Close() has been called during delegate callback processing. | 95 HttpServer::Delegate* delegate_; |
101 bool HasClosedConnection(HttpConnection* connection); | 96 scoped_ptr<StreamListenSocket> server_; |
102 | 97 typedef std::map<int, HttpConnection*> IdToConnectionMap; |
103 const scoped_ptr<ServerSocket> server_socket_; | |
104 scoped_ptr<StreamSocket> accepted_socket_; | |
105 HttpServer::Delegate* const delegate_; | |
106 | |
107 int last_id_; | |
108 IdToConnectionMap id_to_connection_; | 98 IdToConnectionMap id_to_connection_; |
109 | 99 typedef std::map<StreamListenSocket*, HttpConnection*> SocketToConnectionMap; |
110 base::WeakPtrFactory<HttpServer> weak_ptr_factory_; | 100 SocketToConnectionMap socket_to_connection_; |
111 | 101 |
112 DISALLOW_COPY_AND_ASSIGN(HttpServer); | 102 DISALLOW_COPY_AND_ASSIGN(HttpServer); |
113 }; | 103 }; |
114 | 104 |
115 } // namespace net | 105 } // namespace net |
116 | 106 |
117 #endif // NET_SERVER_HTTP_SERVER_H_ | 107 #endif // NET_SERVER_HTTP_SERVER_H_ |
OLD | NEW |