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 #include "net/server/http_server.h" | 5 #include "net/server/http_server.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
11 #include "base/sys_byteorder.h" | 11 #include "base/sys_byteorder.h" |
12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
13 #include "net/base/tcp_listen_socket.h" | 13 #include "net/base/tcp_listen_socket.h" |
14 #include "net/server/http_connection.h" | 14 #include "net/server/http_connection.h" |
15 #include "net/server/http_server_request_info.h" | 15 #include "net/server/http_server_request_info.h" |
16 #include "net/server/web_socket.h" | 16 #include "net/server/web_socket.h" |
17 | 17 |
18 namespace net { | 18 namespace net { |
19 | 19 |
20 HttpServer::HttpServer(const std::string& host, | 20 HttpServer::HttpServer(const std::string& host, |
21 int port, | 21 int port, |
22 HttpServer::Delegate* del) | 22 HttpServer::Delegate* del) |
23 : delegate_(del) { | 23 : delegate_(del) { |
24 server_ = TCPListenSocket::CreateAndListen(host, port, this); | 24 server_ = TCPListenSocket::CreateAndListen(host, port, this); |
25 } | 25 } |
26 | 26 |
27 HttpServer::~HttpServer() { | 27 void HttpServer::AcceptWebSocket( |
28 IdToConnectionMap copy = id_to_connection_; | 28 int connection_id, |
29 for (IdToConnectionMap::iterator it = copy.begin(); it != copy.end(); ++it) | 29 const HttpServerRequestInfo& request) { |
30 delete it->second; | 30 HttpConnection* connection = FindConnection(connection_id); |
| 31 if (connection == NULL) |
| 32 return; |
31 | 33 |
32 server_ = NULL; | 34 DCHECK(connection->web_socket_.get()); |
| 35 connection->web_socket_->Accept(request); |
| 36 } |
| 37 |
| 38 void HttpServer::SendOverWebSocket(int connection_id, |
| 39 const std::string& data) { |
| 40 HttpConnection* connection = FindConnection(connection_id); |
| 41 if (connection == NULL) |
| 42 return; |
| 43 DCHECK(connection->web_socket_.get()); |
| 44 connection->web_socket_->Send(data); |
33 } | 45 } |
34 | 46 |
35 void HttpServer::Send(int connection_id, const std::string& data) { | 47 void HttpServer::Send(int connection_id, const std::string& data) { |
36 HttpConnection* connection = FindConnection(connection_id); | 48 HttpConnection* connection = FindConnection(connection_id); |
37 if (connection == NULL) | 49 if (connection == NULL) |
38 return; | 50 return; |
39 connection->Send(data); | 51 connection->Send(data); |
40 } | 52 } |
41 | 53 |
42 void HttpServer::Send(int connection_id, const char* bytes, int len) { | 54 void HttpServer::Send(int connection_id, const char* bytes, int len) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 { | 86 { |
75 HttpConnection* connection = FindConnection(connection_id); | 87 HttpConnection* connection = FindConnection(connection_id); |
76 if (connection == NULL) | 88 if (connection == NULL) |
77 return; | 89 return; |
78 | 90 |
79 // Initiating close from server-side does not lead to the DidClose call. | 91 // Initiating close from server-side does not lead to the DidClose call. |
80 // Do it manually here. | 92 // Do it manually here. |
81 DidClose(connection->socket_); | 93 DidClose(connection->socket_); |
82 } | 94 } |
83 | 95 |
| 96 void HttpServer::DidAccept(ListenSocket* server, |
| 97 ListenSocket* socket) { |
| 98 HttpConnection* connection = new HttpConnection(this, socket); |
| 99 id_to_connection_[connection->id()] = connection; |
| 100 socket_to_connection_[socket] = connection; |
| 101 } |
| 102 |
| 103 void HttpServer::DidRead(ListenSocket* socket, |
| 104 const char* data, |
| 105 int len) { |
| 106 HttpConnection* connection = FindConnection(socket); |
| 107 DCHECK(connection != NULL); |
| 108 if (connection == NULL) |
| 109 return; |
| 110 |
| 111 connection->recv_data_.append(data, len); |
| 112 while (connection->recv_data_.length()) { |
| 113 if (connection->web_socket_.get()) { |
| 114 std::string message; |
| 115 WebSocket::ParseResult result = connection->web_socket_->Read(&message); |
| 116 if (result == WebSocket::FRAME_INCOMPLETE) |
| 117 break; |
| 118 |
| 119 if (result == WebSocket::FRAME_CLOSE || |
| 120 result == WebSocket::FRAME_ERROR) { |
| 121 Close(connection->id()); |
| 122 break; |
| 123 } |
| 124 delegate_->OnWebSocketMessage(connection->id(), message); |
| 125 continue; |
| 126 } |
| 127 |
| 128 HttpServerRequestInfo request; |
| 129 size_t pos = 0; |
| 130 if (!ParseHeaders(connection, &request, &pos)) |
| 131 break; |
| 132 |
| 133 std::string connection_header = request.GetHeaderValue("Connection"); |
| 134 if (connection_header == "Upgrade") { |
| 135 connection->web_socket_.reset(WebSocket::CreateWebSocket(connection, |
| 136 request, |
| 137 &pos)); |
| 138 |
| 139 if (!connection->web_socket_.get()) // Not enought data was received. |
| 140 break; |
| 141 delegate_->OnWebSocketRequest(connection->id(), request); |
| 142 connection->Shift(pos); |
| 143 continue; |
| 144 } |
| 145 // Request body is not supported. It is always empty. |
| 146 delegate_->OnHttpRequest(connection->id(), request); |
| 147 connection->Shift(pos); |
| 148 } |
| 149 } |
| 150 |
| 151 void HttpServer::DidClose(ListenSocket* socket) { |
| 152 HttpConnection* connection = FindConnection(socket); |
| 153 DCHECK(connection != NULL); |
| 154 id_to_connection_.erase(connection->id()); |
| 155 socket_to_connection_.erase(connection->socket_); |
| 156 delete connection; |
| 157 } |
| 158 |
| 159 HttpServer::~HttpServer() { |
| 160 IdToConnectionMap copy = id_to_connection_; |
| 161 for (IdToConnectionMap::iterator it = copy.begin(); it != copy.end(); ++it) |
| 162 delete it->second; |
| 163 |
| 164 server_ = NULL; |
| 165 } |
| 166 |
84 // | 167 // |
85 // HTTP Request Parser | 168 // HTTP Request Parser |
86 // This HTTP request parser uses a simple state machine to quickly parse | 169 // This HTTP request parser uses a simple state machine to quickly parse |
87 // through the headers. The parser is not 100% complete, as it is designed | 170 // through the headers. The parser is not 100% complete, as it is designed |
88 // for use in this simple test driver. | 171 // for use in this simple test driver. |
89 // | 172 // |
90 // Known issues: | 173 // Known issues: |
91 // - does not handle whitespace on first HTTP line correctly. Expects | 174 // - does not handle whitespace on first HTTP line correctly. Expects |
92 // a single space between the method/url and url/protocol. | 175 // a single space between the method/url and url/protocol. |
93 | 176 |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 return true; | 288 return true; |
206 case ST_ERR: | 289 case ST_ERR: |
207 return false; | 290 return false; |
208 } | 291 } |
209 } | 292 } |
210 } | 293 } |
211 // No more characters, but we haven't finished parsing yet. | 294 // No more characters, but we haven't finished parsing yet. |
212 return false; | 295 return false; |
213 } | 296 } |
214 | 297 |
215 void HttpServer::DidAccept(ListenSocket* server, | |
216 ListenSocket* socket) { | |
217 HttpConnection* connection = new HttpConnection(this, socket); | |
218 id_to_connection_[connection->id()] = connection; | |
219 socket_to_connection_[socket] = connection; | |
220 } | |
221 | |
222 void HttpServer::DidRead(ListenSocket* socket, | |
223 const char* data, | |
224 int len) { | |
225 HttpConnection* connection = FindConnection(socket); | |
226 DCHECK(connection != NULL); | |
227 if (connection == NULL) | |
228 return; | |
229 | |
230 connection->recv_data_.append(data, len); | |
231 while (connection->recv_data_.length()) { | |
232 if (connection->web_socket_.get()) { | |
233 std::string message; | |
234 WebSocket::ParseResult result = connection->web_socket_->Read(&message); | |
235 if (result == WebSocket::FRAME_INCOMPLETE) | |
236 break; | |
237 | |
238 if (result == WebSocket::FRAME_CLOSE || | |
239 result == WebSocket::FRAME_ERROR) { | |
240 Close(connection->id()); | |
241 break; | |
242 } | |
243 delegate_->OnWebSocketMessage(connection->id(), message); | |
244 continue; | |
245 } | |
246 | |
247 HttpServerRequestInfo request; | |
248 size_t pos = 0; | |
249 if (!ParseHeaders(connection, &request, &pos)) | |
250 break; | |
251 | |
252 std::string connection_header = request.GetHeaderValue("Connection"); | |
253 if (connection_header == "Upgrade") { | |
254 connection->web_socket_.reset(WebSocket::CreateWebSocket(connection, | |
255 request, | |
256 &pos)); | |
257 | |
258 if (!connection->web_socket_.get()) // Not enought data was received. | |
259 break; | |
260 delegate_->OnWebSocketRequest(connection->id(), request); | |
261 connection->Shift(pos); | |
262 continue; | |
263 } | |
264 // Request body is not supported. It is always empty. | |
265 delegate_->OnHttpRequest(connection->id(), request); | |
266 connection->Shift(pos); | |
267 } | |
268 } | |
269 | |
270 void HttpServer::DidClose(ListenSocket* socket) { | |
271 HttpConnection* connection = FindConnection(socket); | |
272 DCHECK(connection != NULL); | |
273 id_to_connection_.erase(connection->id()); | |
274 socket_to_connection_.erase(connection->socket_); | |
275 delete connection; | |
276 } | |
277 | |
278 HttpConnection* HttpServer::FindConnection(int connection_id) { | 298 HttpConnection* HttpServer::FindConnection(int connection_id) { |
279 IdToConnectionMap::iterator it = id_to_connection_.find(connection_id); | 299 IdToConnectionMap::iterator it = id_to_connection_.find(connection_id); |
280 if (it == id_to_connection_.end()) | 300 if (it == id_to_connection_.end()) |
281 return NULL; | 301 return NULL; |
282 return it->second; | 302 return it->second; |
283 } | 303 } |
284 | 304 |
285 HttpConnection* HttpServer::FindConnection(ListenSocket* socket) { | 305 HttpConnection* HttpServer::FindConnection(ListenSocket* socket) { |
286 SocketToConnectionMap::iterator it = socket_to_connection_.find(socket); | 306 SocketToConnectionMap::iterator it = socket_to_connection_.find(socket); |
287 if (it == socket_to_connection_.end()) | 307 if (it == socket_to_connection_.end()) |
288 return NULL; | 308 return NULL; |
289 return it->second; | 309 return it->second; |
290 } | 310 } |
291 | 311 |
292 void HttpServer::AcceptWebSocket( | |
293 int connection_id, | |
294 const HttpServerRequestInfo& request) { | |
295 HttpConnection* connection = FindConnection(connection_id); | |
296 if (connection == NULL) | |
297 return; | |
298 | |
299 DCHECK(connection->web_socket_.get()); | |
300 connection->web_socket_->Accept(request); | |
301 } | |
302 | |
303 void HttpServer::SendOverWebSocket(int connection_id, | |
304 const std::string& data) { | |
305 HttpConnection* connection = FindConnection(connection_id); | |
306 if (connection == NULL) | |
307 return; | |
308 DCHECK(connection->web_socket_.get()); | |
309 connection->web_socket_->Send(data); | |
310 } | |
311 | |
312 } // namespace net | 312 } // namespace net |
OLD | NEW |