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 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 if (cert_ == CERT_MISMATCHED_NAME || cert_ == CERT_COMMON_NAME_IS_DOMAIN) { | 104 if (cert_ == CERT_MISMATCHED_NAME || cert_ == CERT_COMMON_NAME_IS_DOMAIN) { |
105 base_url_ = GURL( | 105 base_url_ = GURL( |
106 base::StringPrintf("https://localhost:%d", local_endpoint_.port())); | 106 base::StringPrintf("https://localhost:%d", local_endpoint_.port())); |
107 } | 107 } |
108 } else { | 108 } else { |
109 base_url_ = GURL("http://" + local_endpoint_.ToString()); | 109 base_url_ = GURL("http://" + local_endpoint_.ToString()); |
110 } | 110 } |
111 port_ = local_endpoint_.port(); | 111 port_ = local_endpoint_.port(); |
112 | 112 |
113 listen_socket_->DetachFromThread(); | 113 listen_socket_->DetachFromThread(); |
| 114 |
| 115 if (is_using_ssl_) |
| 116 InitializeSSLServerContext(); |
114 return true; | 117 return true; |
115 } | 118 } |
116 | 119 |
| 120 void EmbeddedTestServer::InitializeSSLServerContext() { |
| 121 base::FilePath certs_dir(GetTestCertsDirectory()); |
| 122 std::string cert_name = GetCertificateName(); |
| 123 |
| 124 base::FilePath key_path = certs_dir.AppendASCII(cert_name); |
| 125 std::string key_string; |
| 126 CHECK(base::ReadFileToString(key_path, &key_string)); |
| 127 std::vector<std::string> headers; |
| 128 headers.push_back("PRIVATE KEY"); |
| 129 PEMTokenizer pem_tokenizer(key_string, headers); |
| 130 pem_tokenizer.GetNext(); |
| 131 std::vector<uint8_t> key_vector; |
| 132 key_vector.assign(pem_tokenizer.data().begin(), pem_tokenizer.data().end()); |
| 133 |
| 134 scoped_ptr<crypto::RSAPrivateKey> server_key( |
| 135 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector)); |
| 136 context_ = |
| 137 CreateSSLServerContext(GetCertificate().get(), *server_key, ssl_config_); |
| 138 } |
| 139 |
117 void EmbeddedTestServer::StartAcceptingConnections() { | 140 void EmbeddedTestServer::StartAcceptingConnections() { |
118 DCHECK(!io_thread_.get()); | 141 DCHECK(!io_thread_.get()); |
119 base::Thread::Options thread_options; | 142 base::Thread::Options thread_options; |
120 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | 143 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
121 io_thread_.reset(new base::Thread("EmbeddedTestServer IO Thread")); | 144 io_thread_.reset(new base::Thread("EmbeddedTestServer IO Thread")); |
122 CHECK(io_thread_->StartWithOptions(thread_options)); | 145 CHECK(io_thread_->StartWithOptions(thread_options)); |
123 CHECK(io_thread_->WaitUntilThreadStarted()); | 146 CHECK(io_thread_->WaitUntilThreadStarted()); |
124 | 147 |
125 io_thread_->task_runner()->PostTask( | 148 io_thread_->task_runner()->PostTask( |
126 FROM_HERE, | 149 FROM_HERE, |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 const HandleRequestCallback& callback) { | 292 const HandleRequestCallback& callback) { |
270 // TODO(svaldez): Add check to prevent RegisterHandler from being called | 293 // TODO(svaldez): Add check to prevent RegisterHandler from being called |
271 // after the server has started. https://crbug.com/546060 | 294 // after the server has started. https://crbug.com/546060 |
272 default_request_handlers_.push_back(callback); | 295 default_request_handlers_.push_back(callback); |
273 } | 296 } |
274 | 297 |
275 scoped_ptr<StreamSocket> EmbeddedTestServer::DoSSLUpgrade( | 298 scoped_ptr<StreamSocket> EmbeddedTestServer::DoSSLUpgrade( |
276 scoped_ptr<StreamSocket> connection) { | 299 scoped_ptr<StreamSocket> connection) { |
277 DCHECK(io_thread_->task_runner()->BelongsToCurrentThread()); | 300 DCHECK(io_thread_->task_runner()->BelongsToCurrentThread()); |
278 | 301 |
279 base::FilePath certs_dir(GetTestCertsDirectory()); | 302 return context_->CreateSSLServerSocket(std::move(connection)); |
280 std::string cert_name = GetCertificateName(); | |
281 | |
282 base::FilePath key_path = certs_dir.AppendASCII(cert_name); | |
283 std::string key_string; | |
284 CHECK(base::ReadFileToString(key_path, &key_string)); | |
285 std::vector<std::string> headers; | |
286 headers.push_back("PRIVATE KEY"); | |
287 PEMTokenizer pem_tokenizer(key_string, headers); | |
288 pem_tokenizer.GetNext(); | |
289 std::vector<uint8_t> key_vector; | |
290 key_vector.assign(pem_tokenizer.data().begin(), pem_tokenizer.data().end()); | |
291 | |
292 scoped_ptr<crypto::RSAPrivateKey> server_key( | |
293 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector)); | |
294 | |
295 return CreateSSLServerSocket(std::move(connection), GetCertificate().get(), | |
296 *server_key, ssl_config_); | |
297 } | 303 } |
298 | 304 |
299 void EmbeddedTestServer::DoAcceptLoop() { | 305 void EmbeddedTestServer::DoAcceptLoop() { |
300 int rv = OK; | 306 int rv = OK; |
301 while (rv == OK) { | 307 while (rv == OK) { |
302 rv = listen_socket_->Accept( | 308 rv = listen_socket_->Accept( |
303 &accepted_socket_, base::Bind(&EmbeddedTestServer::OnAcceptCompleted, | 309 &accepted_socket_, base::Bind(&EmbeddedTestServer::OnAcceptCompleted, |
304 base::Unretained(this))); | 310 base::Unretained(this))); |
305 if (rv == ERR_IO_PENDING) | 311 if (rv == ERR_IO_PENDING) |
306 return; | 312 return; |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
422 run_loop.QuitClosure())) { | 428 run_loop.QuitClosure())) { |
423 return false; | 429 return false; |
424 } | 430 } |
425 run_loop.Run(); | 431 run_loop.Run(); |
426 | 432 |
427 return true; | 433 return true; |
428 } | 434 } |
429 | 435 |
430 } // namespace test_server | 436 } // namespace test_server |
431 } // namespace net | 437 } // namespace net |
OLD | NEW |