| 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/test/embedded_test_server/embedded_test_server.h" | 5 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 | 149 |
| 150 void EmbeddedTestServer::InitializeOnIOThread() { | 150 void EmbeddedTestServer::InitializeOnIOThread() { |
| 151 DCHECK(io_thread_->BelongsToCurrentThread()); | 151 DCHECK(io_thread_->BelongsToCurrentThread()); |
| 152 DCHECK(!Started()); | 152 DCHECK(!Started()); |
| 153 | 153 |
| 154 SocketDescriptor socket_descriptor = | 154 SocketDescriptor socket_descriptor = |
| 155 TCPListenSocket::CreateAndBindAnyPort("127.0.0.1", &port_); | 155 TCPListenSocket::CreateAndBindAnyPort("127.0.0.1", &port_); |
| 156 if (socket_descriptor == kInvalidSocket) | 156 if (socket_descriptor == kInvalidSocket) |
| 157 return; | 157 return; |
| 158 | 158 |
| 159 listen_socket_ = new HttpListenSocket(socket_descriptor, this); | 159 listen_socket_.reset(new HttpListenSocket(socket_descriptor, this)); |
| 160 listen_socket_->Listen(); | 160 listen_socket_->Listen(); |
| 161 | 161 |
| 162 IPEndPoint address; | 162 IPEndPoint address; |
| 163 int result = listen_socket_->GetLocalAddress(&address); | 163 int result = listen_socket_->GetLocalAddress(&address); |
| 164 if (result == OK) { | 164 if (result == OK) { |
| 165 base_url_ = GURL(std::string("http://") + address.ToString()); | 165 base_url_ = GURL(std::string("http://") + address.ToString()); |
| 166 } else { | 166 } else { |
| 167 LOG(ERROR) << "GetLocalAddress failed: " << ErrorToString(result); | 167 LOG(ERROR) << "GetLocalAddress failed: " << ErrorToString(result); |
| 168 } | 168 } |
| 169 } | 169 } |
| 170 | 170 |
| 171 void EmbeddedTestServer::ShutdownOnIOThread() { | 171 void EmbeddedTestServer::ShutdownOnIOThread() { |
| 172 DCHECK(io_thread_->BelongsToCurrentThread()); | 172 DCHECK(io_thread_->BelongsToCurrentThread()); |
| 173 | 173 |
| 174 listen_socket_ = NULL; // Release the listen socket. | 174 listen_socket_.reset(); |
| 175 STLDeleteContainerPairSecondPointers(connections_.begin(), | 175 STLDeleteContainerPairSecondPointers(connections_.begin(), |
| 176 connections_.end()); | 176 connections_.end()); |
| 177 connections_.clear(); | 177 connections_.clear(); |
| 178 } | 178 } |
| 179 | 179 |
| 180 void EmbeddedTestServer::HandleRequest(HttpConnection* connection, | 180 void EmbeddedTestServer::HandleRequest(HttpConnection* connection, |
| 181 scoped_ptr<HttpRequest> request) { | 181 scoped_ptr<HttpRequest> request) { |
| 182 DCHECK(io_thread_->BelongsToCurrentThread()); | 182 DCHECK(io_thread_->BelongsToCurrentThread()); |
| 183 | 183 |
| 184 bool request_handled = false; | 184 bool request_handled = false; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 void EmbeddedTestServer::ServeFilesFromDirectory( | 217 void EmbeddedTestServer::ServeFilesFromDirectory( |
| 218 const base::FilePath& directory) { | 218 const base::FilePath& directory) { |
| 219 RegisterRequestHandler(base::Bind(&HandleFileRequest, directory)); | 219 RegisterRequestHandler(base::Bind(&HandleFileRequest, directory)); |
| 220 } | 220 } |
| 221 | 221 |
| 222 void EmbeddedTestServer::RegisterRequestHandler( | 222 void EmbeddedTestServer::RegisterRequestHandler( |
| 223 const HandleRequestCallback& callback) { | 223 const HandleRequestCallback& callback) { |
| 224 request_handlers_.push_back(callback); | 224 request_handlers_.push_back(callback); |
| 225 } | 225 } |
| 226 | 226 |
| 227 void EmbeddedTestServer::DidAccept(StreamListenSocket* server, | 227 void EmbeddedTestServer::DidAccept( |
| 228 StreamListenSocket* connection) { | 228 StreamListenSocket* server, |
| 229 scoped_ptr<StreamListenSocket> connection) { |
| 229 DCHECK(io_thread_->BelongsToCurrentThread()); | 230 DCHECK(io_thread_->BelongsToCurrentThread()); |
| 230 | 231 |
| 231 HttpConnection* http_connection = new HttpConnection( | 232 HttpConnection* http_connection = new HttpConnection( |
| 232 connection, | 233 connection.Pass(), |
| 233 base::Bind(&EmbeddedTestServer::HandleRequest, | 234 base::Bind(&EmbeddedTestServer::HandleRequest, |
| 234 weak_factory_.GetWeakPtr())); | 235 weak_factory_.GetWeakPtr())); |
| 235 connections_[connection] = http_connection; | 236 // TODO(szym): Make HttpConnection the StreamListenSocket delegate. |
| 237 connections_[http_connection->socket_.get()] = http_connection; |
| 236 } | 238 } |
| 237 | 239 |
| 238 void EmbeddedTestServer::DidRead(StreamListenSocket* connection, | 240 void EmbeddedTestServer::DidRead(StreamListenSocket* connection, |
| 239 const char* data, | 241 const char* data, |
| 240 int length) { | 242 int length) { |
| 241 DCHECK(io_thread_->BelongsToCurrentThread()); | 243 DCHECK(io_thread_->BelongsToCurrentThread()); |
| 242 | 244 |
| 243 HttpConnection* http_connection = FindConnection(connection); | 245 HttpConnection* http_connection = FindConnection(connection); |
| 244 if (http_connection == NULL) { | 246 if (http_connection == NULL) { |
| 245 LOG(WARNING) << "Unknown connection."; | 247 LOG(WARNING) << "Unknown connection."; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 267 std::map<StreamListenSocket*, HttpConnection*>::iterator it = | 269 std::map<StreamListenSocket*, HttpConnection*>::iterator it = |
| 268 connections_.find(socket); | 270 connections_.find(socket); |
| 269 if (it == connections_.end()) { | 271 if (it == connections_.end()) { |
| 270 return NULL; | 272 return NULL; |
| 271 } | 273 } |
| 272 return it->second; | 274 return it->second; |
| 273 } | 275 } |
| 274 | 276 |
| 275 } // namespace test_server | 277 } // namespace test_server |
| 276 } // namespace net | 278 } // namespace net |
| OLD | NEW |