| 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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 base::FilePath key_path = certs_dir.AppendASCII(cert_name); | 124 base::FilePath key_path = certs_dir.AppendASCII(cert_name); |
| 125 std::string key_string; | 125 std::string key_string; |
| 126 CHECK(base::ReadFileToString(key_path, &key_string)); | 126 CHECK(base::ReadFileToString(key_path, &key_string)); |
| 127 std::vector<std::string> headers; | 127 std::vector<std::string> headers; |
| 128 headers.push_back("PRIVATE KEY"); | 128 headers.push_back("PRIVATE KEY"); |
| 129 PEMTokenizer pem_tokenizer(key_string, headers); | 129 PEMTokenizer pem_tokenizer(key_string, headers); |
| 130 pem_tokenizer.GetNext(); | 130 pem_tokenizer.GetNext(); |
| 131 std::vector<uint8_t> key_vector; | 131 std::vector<uint8_t> key_vector; |
| 132 key_vector.assign(pem_tokenizer.data().begin(), pem_tokenizer.data().end()); | 132 key_vector.assign(pem_tokenizer.data().begin(), pem_tokenizer.data().end()); |
| 133 | 133 |
| 134 scoped_ptr<crypto::RSAPrivateKey> server_key( | 134 std::unique_ptr<crypto::RSAPrivateKey> server_key( |
| 135 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector)); | 135 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector)); |
| 136 context_ = | 136 context_ = |
| 137 CreateSSLServerContext(GetCertificate().get(), *server_key, ssl_config_); | 137 CreateSSLServerContext(GetCertificate().get(), *server_key, ssl_config_); |
| 138 } | 138 } |
| 139 | 139 |
| 140 void EmbeddedTestServer::StartAcceptingConnections() { | 140 void EmbeddedTestServer::StartAcceptingConnections() { |
| 141 DCHECK(!io_thread_.get()); | 141 DCHECK(!io_thread_.get()); |
| 142 base::Thread::Options thread_options; | 142 base::Thread::Options thread_options; |
| 143 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | 143 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 144 io_thread_.reset(new base::Thread("EmbeddedTestServer IO Thread")); | 144 io_thread_.reset(new base::Thread("EmbeddedTestServer IO Thread")); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 160 void EmbeddedTestServer::ShutdownOnIOThread() { | 160 void EmbeddedTestServer::ShutdownOnIOThread() { |
| 161 DCHECK(io_thread_->task_runner()->BelongsToCurrentThread()); | 161 DCHECK(io_thread_->task_runner()->BelongsToCurrentThread()); |
| 162 weak_factory_.InvalidateWeakPtrs(); | 162 weak_factory_.InvalidateWeakPtrs(); |
| 163 listen_socket_.reset(); | 163 listen_socket_.reset(); |
| 164 STLDeleteContainerPairSecondPointers(connections_.begin(), | 164 STLDeleteContainerPairSecondPointers(connections_.begin(), |
| 165 connections_.end()); | 165 connections_.end()); |
| 166 connections_.clear(); | 166 connections_.clear(); |
| 167 } | 167 } |
| 168 | 168 |
| 169 void EmbeddedTestServer::HandleRequest(HttpConnection* connection, | 169 void EmbeddedTestServer::HandleRequest(HttpConnection* connection, |
| 170 scoped_ptr<HttpRequest> request) { | 170 std::unique_ptr<HttpRequest> request) { |
| 171 DCHECK(io_thread_->task_runner()->BelongsToCurrentThread()); | 171 DCHECK(io_thread_->task_runner()->BelongsToCurrentThread()); |
| 172 | 172 |
| 173 scoped_ptr<HttpResponse> response; | 173 std::unique_ptr<HttpResponse> response; |
| 174 | 174 |
| 175 for (const auto& handler : request_handlers_) { | 175 for (const auto& handler : request_handlers_) { |
| 176 response = handler.Run(*request); | 176 response = handler.Run(*request); |
| 177 if (response) | 177 if (response) |
| 178 break; | 178 break; |
| 179 } | 179 } |
| 180 | 180 |
| 181 if (!response) { | 181 if (!response) { |
| 182 for (const auto& handler : default_request_handlers_) { | 182 for (const auto& handler : default_request_handlers_) { |
| 183 response = handler.Run(*request); | 183 response = handler.Run(*request); |
| 184 if (response) | 184 if (response) |
| 185 break; | 185 break; |
| 186 } | 186 } |
| 187 } | 187 } |
| 188 | 188 |
| 189 if (!response) { | 189 if (!response) { |
| 190 LOG(WARNING) << "Request not handled. Returning 404: " | 190 LOG(WARNING) << "Request not handled. Returning 404: " |
| 191 << request->relative_url; | 191 << request->relative_url; |
| 192 scoped_ptr<BasicHttpResponse> not_found_response(new BasicHttpResponse); | 192 std::unique_ptr<BasicHttpResponse> not_found_response( |
| 193 new BasicHttpResponse); |
| 193 not_found_response->set_code(HTTP_NOT_FOUND); | 194 not_found_response->set_code(HTTP_NOT_FOUND); |
| 194 response = std::move(not_found_response); | 195 response = std::move(not_found_response); |
| 195 } | 196 } |
| 196 | 197 |
| 197 response->SendResponse( | 198 response->SendResponse( |
| 198 base::Bind(&HttpConnection::SendResponseBytes, connection->GetWeakPtr()), | 199 base::Bind(&HttpConnection::SendResponseBytes, connection->GetWeakPtr()), |
| 199 base::Bind(&EmbeddedTestServer::DidClose, weak_factory_.GetWeakPtr(), | 200 base::Bind(&EmbeddedTestServer::DidClose, weak_factory_.GetWeakPtr(), |
| 200 connection)); | 201 connection)); |
| 201 } | 202 } |
| 202 | 203 |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 request_handlers_.push_back(callback); | 289 request_handlers_.push_back(callback); |
| 289 } | 290 } |
| 290 | 291 |
| 291 void EmbeddedTestServer::RegisterDefaultHandler( | 292 void EmbeddedTestServer::RegisterDefaultHandler( |
| 292 const HandleRequestCallback& callback) { | 293 const HandleRequestCallback& callback) { |
| 293 // TODO(svaldez): Add check to prevent RegisterHandler from being called | 294 // TODO(svaldez): Add check to prevent RegisterHandler from being called |
| 294 // after the server has started. https://crbug.com/546060 | 295 // after the server has started. https://crbug.com/546060 |
| 295 default_request_handlers_.push_back(callback); | 296 default_request_handlers_.push_back(callback); |
| 296 } | 297 } |
| 297 | 298 |
| 298 scoped_ptr<StreamSocket> EmbeddedTestServer::DoSSLUpgrade( | 299 std::unique_ptr<StreamSocket> EmbeddedTestServer::DoSSLUpgrade( |
| 299 scoped_ptr<StreamSocket> connection) { | 300 std::unique_ptr<StreamSocket> connection) { |
| 300 DCHECK(io_thread_->task_runner()->BelongsToCurrentThread()); | 301 DCHECK(io_thread_->task_runner()->BelongsToCurrentThread()); |
| 301 | 302 |
| 302 return context_->CreateSSLServerSocket(std::move(connection)); | 303 return context_->CreateSSLServerSocket(std::move(connection)); |
| 303 } | 304 } |
| 304 | 305 |
| 305 void EmbeddedTestServer::DoAcceptLoop() { | 306 void EmbeddedTestServer::DoAcceptLoop() { |
| 306 int rv = OK; | 307 int rv = OK; |
| 307 while (rv == OK) { | 308 while (rv == OK) { |
| 308 rv = listen_socket_->Accept( | 309 rv = listen_socket_->Accept( |
| 309 &accepted_socket_, base::Bind(&EmbeddedTestServer::OnAcceptCompleted, | 310 &accepted_socket_, base::Bind(&EmbeddedTestServer::OnAcceptCompleted, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 320 DoAcceptLoop(); | 321 DoAcceptLoop(); |
| 321 } | 322 } |
| 322 | 323 |
| 323 void EmbeddedTestServer::OnHandshakeDone(HttpConnection* connection, int rv) { | 324 void EmbeddedTestServer::OnHandshakeDone(HttpConnection* connection, int rv) { |
| 324 if (connection->socket_->IsConnected()) | 325 if (connection->socket_->IsConnected()) |
| 325 ReadData(connection); | 326 ReadData(connection); |
| 326 else | 327 else |
| 327 DidClose(connection); | 328 DidClose(connection); |
| 328 } | 329 } |
| 329 | 330 |
| 330 void EmbeddedTestServer::HandleAcceptResult(scoped_ptr<StreamSocket> socket) { | 331 void EmbeddedTestServer::HandleAcceptResult( |
| 332 std::unique_ptr<StreamSocket> socket) { |
| 331 DCHECK(io_thread_->task_runner()->BelongsToCurrentThread()); | 333 DCHECK(io_thread_->task_runner()->BelongsToCurrentThread()); |
| 332 if (connection_listener_) | 334 if (connection_listener_) |
| 333 connection_listener_->AcceptedSocket(*socket); | 335 connection_listener_->AcceptedSocket(*socket); |
| 334 | 336 |
| 335 if (is_using_ssl_) | 337 if (is_using_ssl_) |
| 336 socket = DoSSLUpgrade(std::move(socket)); | 338 socket = DoSSLUpgrade(std::move(socket)); |
| 337 | 339 |
| 338 HttpConnection* http_connection = new HttpConnection( | 340 HttpConnection* http_connection = new HttpConnection( |
| 339 std::move(socket), | 341 std::move(socket), |
| 340 base::Bind(&EmbeddedTestServer::HandleRequest, base::Unretained(this))); | 342 base::Bind(&EmbeddedTestServer::HandleRequest, base::Unretained(this))); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 412 const base::Closure& closure) { | 414 const base::Closure& closure) { |
| 413 // Note that PostTaskAndReply below requires | 415 // Note that PostTaskAndReply below requires |
| 414 // base::ThreadTaskRunnerHandle::Get() to return a task runner for posting | 416 // base::ThreadTaskRunnerHandle::Get() to return a task runner for posting |
| 415 // the reply task. However, in order to make EmbeddedTestServer universally | 417 // the reply task. However, in order to make EmbeddedTestServer universally |
| 416 // usable, it needs to cope with the situation where it's running on a thread | 418 // usable, it needs to cope with the situation where it's running on a thread |
| 417 // on which a message loop is not (yet) available or as has been destroyed | 419 // on which a message loop is not (yet) available or as has been destroyed |
| 418 // already. | 420 // already. |
| 419 // | 421 // |
| 420 // To handle this situation, create temporary message loop to support the | 422 // To handle this situation, create temporary message loop to support the |
| 421 // PostTaskAndReply operation if the current thread as no message loop. | 423 // PostTaskAndReply operation if the current thread as no message loop. |
| 422 scoped_ptr<base::MessageLoop> temporary_loop; | 424 std::unique_ptr<base::MessageLoop> temporary_loop; |
| 423 if (!base::MessageLoop::current()) | 425 if (!base::MessageLoop::current()) |
| 424 temporary_loop.reset(new base::MessageLoop()); | 426 temporary_loop.reset(new base::MessageLoop()); |
| 425 | 427 |
| 426 base::RunLoop run_loop; | 428 base::RunLoop run_loop; |
| 427 if (!io_thread_->task_runner()->PostTaskAndReply(FROM_HERE, closure, | 429 if (!io_thread_->task_runner()->PostTaskAndReply(FROM_HERE, closure, |
| 428 run_loop.QuitClosure())) { | 430 run_loop.QuitClosure())) { |
| 429 return false; | 431 return false; |
| 430 } | 432 } |
| 431 run_loop.Run(); | 433 run_loop.Run(); |
| 432 | 434 |
| 433 return true; | 435 return true; |
| 434 } | 436 } |
| 435 | 437 |
| 436 } // namespace test_server | 438 } // namespace test_server |
| 437 } // namespace net | 439 } // namespace net |
| OLD | NEW |