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/stl_util.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
12 #include "base/sys_byteorder.h" | 12 #include "base/sys_byteorder.h" |
13 #include "build/build_config.h" | 13 #include "build/build_config.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 #include "net/socket/tcp_listen_socket.h" | 17 #include "net/socket/tcp_listen_socket.h" |
18 | 18 |
19 namespace net { | 19 namespace net { |
20 | 20 |
21 HttpServer::HttpServer(const StreamListenSocketFactory& factory, | 21 HttpServer::HttpServer(const StreamListenSocketFactory& factory, |
22 HttpServer::Delegate* delegate) | 22 HttpServer::Delegate* delegate) |
23 : delegate_(delegate), | 23 : delegate_(delegate), |
24 server_(factory.CreateAndListen(this)) { | 24 server_(factory.CreateAndListen(this)) { |
25 DCHECK(server_); | 25 DCHECK(server_.get()); |
26 } | 26 } |
27 | 27 |
28 void HttpServer::AcceptWebSocket( | 28 void HttpServer::AcceptWebSocket( |
29 int connection_id, | 29 int connection_id, |
30 const HttpServerRequestInfo& request) { | 30 const HttpServerRequestInfo& request) { |
31 HttpConnection* connection = FindConnection(connection_id); | 31 HttpConnection* connection = FindConnection(connection_id); |
32 if (connection == NULL) | 32 if (connection == NULL) |
33 return; | 33 return; |
34 | 34 |
35 DCHECK(connection->web_socket_.get()); | 35 DCHECK(connection->web_socket_.get()); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 Send(connection_id, HTTP_INTERNAL_SERVER_ERROR, message, "text/html"); | 69 Send(connection_id, HTTP_INTERNAL_SERVER_ERROR, message, "text/html"); |
70 } | 70 } |
71 | 71 |
72 void HttpServer::Close(int connection_id) { | 72 void HttpServer::Close(int connection_id) { |
73 HttpConnection* connection = FindConnection(connection_id); | 73 HttpConnection* connection = FindConnection(connection_id); |
74 if (connection == NULL) | 74 if (connection == NULL) |
75 return; | 75 return; |
76 | 76 |
77 // Initiating close from server-side does not lead to the DidClose call. | 77 // Initiating close from server-side does not lead to the DidClose call. |
78 // Do it manually here. | 78 // Do it manually here. |
79 DidClose(connection->socket_); | 79 DidClose(connection->socket_.get()); |
80 } | 80 } |
81 | 81 |
82 int HttpServer::GetLocalAddress(IPEndPoint* address) { | 82 int HttpServer::GetLocalAddress(IPEndPoint* address) { |
83 return server_->GetLocalAddress(address); | 83 return server_->GetLocalAddress(address); |
84 } | 84 } |
85 | 85 |
86 void HttpServer::DidAccept(StreamListenSocket* server, | 86 void HttpServer::DidAccept(StreamListenSocket* server, |
87 StreamListenSocket* socket) { | 87 StreamListenSocket* socket) { |
88 HttpConnection* connection = new HttpConnection(this, socket); | 88 HttpConnection* connection = new HttpConnection(this, socket); |
89 id_to_connection_[connection->id()] = connection; | 89 id_to_connection_[connection->id()] = connection; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 // Request body is not supported. It is always empty. | 135 // Request body is not supported. It is always empty. |
136 delegate_->OnHttpRequest(connection->id(), request); | 136 delegate_->OnHttpRequest(connection->id(), request); |
137 connection->Shift(pos); | 137 connection->Shift(pos); |
138 } | 138 } |
139 } | 139 } |
140 | 140 |
141 void HttpServer::DidClose(StreamListenSocket* socket) { | 141 void HttpServer::DidClose(StreamListenSocket* socket) { |
142 HttpConnection* connection = FindConnection(socket); | 142 HttpConnection* connection = FindConnection(socket); |
143 DCHECK(connection != NULL); | 143 DCHECK(connection != NULL); |
144 id_to_connection_.erase(connection->id()); | 144 id_to_connection_.erase(connection->id()); |
145 socket_to_connection_.erase(connection->socket_); | 145 socket_to_connection_.erase(connection->socket_.get()); |
146 delete connection; | 146 delete connection; |
147 } | 147 } |
148 | 148 |
149 HttpServer::~HttpServer() { | 149 HttpServer::~HttpServer() { |
150 STLDeleteContainerPairSecondPointers( | 150 STLDeleteContainerPairSecondPointers( |
151 id_to_connection_.begin(), id_to_connection_.end()); | 151 id_to_connection_.begin(), id_to_connection_.end()); |
152 server_ = NULL; | 152 server_ = NULL; |
153 } | 153 } |
154 | 154 |
155 // | 155 // |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 } | 291 } |
292 | 292 |
293 HttpConnection* HttpServer::FindConnection(StreamListenSocket* socket) { | 293 HttpConnection* HttpServer::FindConnection(StreamListenSocket* socket) { |
294 SocketToConnectionMap::iterator it = socket_to_connection_.find(socket); | 294 SocketToConnectionMap::iterator it = socket_to_connection_.find(socket); |
295 if (it == socket_to_connection_.end()) | 295 if (it == socket_to_connection_.end()) |
296 return NULL; | 296 return NULL; |
297 return it->second; | 297 return it->second; |
298 } | 298 } |
299 | 299 |
300 } // namespace net | 300 } // namespace net |
OLD | NEW |