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/stl_util.h" | |
9 #include "base/string_util.h" | 10 #include "base/string_util.h" |
10 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
11 #include "base/sys_byteorder.h" | 12 #include "base/sys_byteorder.h" |
12 #include "build/build_config.h" | 13 #include "build/build_config.h" |
13 #include "net/base/tcp_listen_socket.h" | 14 #include "net/base/tcp_listen_socket.h" |
14 #include "net/server/http_connection.h" | 15 #include "net/server/http_connection.h" |
15 #include "net/server/http_server_request_info.h" | 16 #include "net/server/http_server_request_info.h" |
16 #include "net/server/web_socket.h" | 17 #include "net/server/web_socket.h" |
17 | 18 |
18 namespace net { | 19 namespace net { |
19 | 20 |
20 HttpServer::HttpServer(const std::string& host, | 21 HttpServer::HttpServer(HttpServer::Delegate* delegate, |
21 int port, | 22 StreamListenSocketFactory* factory) |
22 HttpServer::Delegate* del) | 23 : delegate_(delegate), |
23 : delegate_(del) { | 24 ALLOW_THIS_IN_INITIALIZER_LIST(server_(factory->CreateAndListen(this))) { |
mmenke
2012/05/14 20:45:19
nit: Fix indent.
Philippe
2012/05/15 14:39:09
Done.
| |
24 server_ = TCPListenSocket::CreateAndListen(host, port, this); | 25 DCHECK(server_); |
25 } | 26 } |
26 | 27 |
27 void HttpServer::AcceptWebSocket( | 28 void HttpServer::AcceptWebSocket( |
28 int connection_id, | 29 int connection_id, |
29 const HttpServerRequestInfo& request) { | 30 const HttpServerRequestInfo& request) { |
30 HttpConnection* connection = FindConnection(connection_id); | 31 HttpConnection* connection = FindConnection(connection_id); |
31 if (connection == NULL) | 32 if (connection == NULL) |
32 return; | 33 return; |
33 | 34 |
34 DCHECK(connection->web_socket_.get()); | 35 DCHECK(connection->web_socket_.get()); |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
150 | 151 |
151 void HttpServer::DidClose(StreamListenSocket* socket) { | 152 void HttpServer::DidClose(StreamListenSocket* socket) { |
152 HttpConnection* connection = FindConnection(socket); | 153 HttpConnection* connection = FindConnection(socket); |
153 DCHECK(connection != NULL); | 154 DCHECK(connection != NULL); |
154 id_to_connection_.erase(connection->id()); | 155 id_to_connection_.erase(connection->id()); |
155 socket_to_connection_.erase(connection->socket_); | 156 socket_to_connection_.erase(connection->socket_); |
156 delete connection; | 157 delete connection; |
157 } | 158 } |
158 | 159 |
159 HttpServer::~HttpServer() { | 160 HttpServer::~HttpServer() { |
160 IdToConnectionMap copy = id_to_connection_; | 161 STLDeleteContainerPairSecondPointers( |
161 for (IdToConnectionMap::iterator it = copy.begin(); it != copy.end(); ++it) | 162 id_to_connection_.begin(), id_to_connection_.end()); |
162 delete it->second; | |
163 | |
164 server_ = NULL; | 163 server_ = NULL; |
165 } | 164 } |
166 | 165 |
167 // | 166 // |
168 // HTTP Request Parser | 167 // HTTP Request Parser |
169 // This HTTP request parser uses a simple state machine to quickly parse | 168 // This HTTP request parser uses a simple state machine to quickly parse |
170 // through the headers. The parser is not 100% complete, as it is designed | 169 // through the headers. The parser is not 100% complete, as it is designed |
171 // for use in this simple test driver. | 170 // for use in this simple test driver. |
172 // | 171 // |
173 // Known issues: | 172 // Known issues: |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
303 } | 302 } |
304 | 303 |
305 HttpConnection* HttpServer::FindConnection(StreamListenSocket* socket) { | 304 HttpConnection* HttpServer::FindConnection(StreamListenSocket* socket) { |
306 SocketToConnectionMap::iterator it = socket_to_connection_.find(socket); | 305 SocketToConnectionMap::iterator it = socket_to_connection_.find(socket); |
307 if (it == socket_to_connection_.end()) | 306 if (it == socket_to_connection_.end()) |
308 return NULL; | 307 return NULL; |
309 return it->second; | 308 return it->second; |
310 } | 309 } |
311 | 310 |
312 } // namespace net | 311 } // namespace net |
OLD | NEW |