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> |
| 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
9 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
10 #include "base/location.h" | 12 #include "base/location.h" |
11 #include "base/logging.h" | 13 #include "base/logging.h" |
12 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
13 #include "base/path_service.h" | 15 #include "base/path_service.h" |
14 #include "base/process/process_metrics.h" | 16 #include "base/process/process_metrics.h" |
15 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
16 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 if (response) | 161 if (response) |
160 break; | 162 break; |
161 } | 163 } |
162 } | 164 } |
163 | 165 |
164 if (!response) { | 166 if (!response) { |
165 LOG(WARNING) << "Request not handled. Returning 404: " | 167 LOG(WARNING) << "Request not handled. Returning 404: " |
166 << request->relative_url; | 168 << request->relative_url; |
167 scoped_ptr<BasicHttpResponse> not_found_response(new BasicHttpResponse); | 169 scoped_ptr<BasicHttpResponse> not_found_response(new BasicHttpResponse); |
168 not_found_response->set_code(HTTP_NOT_FOUND); | 170 not_found_response->set_code(HTTP_NOT_FOUND); |
169 response = not_found_response.Pass(); | 171 response = std::move(not_found_response); |
170 } | 172 } |
171 | 173 |
172 response->SendResponse( | 174 response->SendResponse( |
173 base::Bind(&HttpConnection::SendResponseBytes, connection->GetWeakPtr()), | 175 base::Bind(&HttpConnection::SendResponseBytes, connection->GetWeakPtr()), |
174 base::Bind(&EmbeddedTestServer::DidClose, weak_factory_.GetWeakPtr(), | 176 base::Bind(&EmbeddedTestServer::DidClose, weak_factory_.GetWeakPtr(), |
175 connection)); | 177 connection)); |
176 } | 178 } |
177 | 179 |
178 GURL EmbeddedTestServer::GetURL(const std::string& relative_url) const { | 180 GURL EmbeddedTestServer::GetURL(const std::string& relative_url) const { |
179 DCHECK(Started()) << "You must start the server first."; | 181 DCHECK(Started()) << "You must start the server first."; |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 std::vector<std::string> headers; | 285 std::vector<std::string> headers; |
284 headers.push_back("PRIVATE KEY"); | 286 headers.push_back("PRIVATE KEY"); |
285 PEMTokenizer pem_tokenizer(key_string, headers); | 287 PEMTokenizer pem_tokenizer(key_string, headers); |
286 pem_tokenizer.GetNext(); | 288 pem_tokenizer.GetNext(); |
287 std::vector<uint8_t> key_vector; | 289 std::vector<uint8_t> key_vector; |
288 key_vector.assign(pem_tokenizer.data().begin(), pem_tokenizer.data().end()); | 290 key_vector.assign(pem_tokenizer.data().begin(), pem_tokenizer.data().end()); |
289 | 291 |
290 scoped_ptr<crypto::RSAPrivateKey> server_key( | 292 scoped_ptr<crypto::RSAPrivateKey> server_key( |
291 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector)); | 293 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector)); |
292 | 294 |
293 return CreateSSLServerSocket(connection.Pass(), GetCertificate().get(), | 295 return CreateSSLServerSocket(std::move(connection), GetCertificate().get(), |
294 server_key.get(), ssl_config_); | 296 server_key.get(), ssl_config_); |
295 } | 297 } |
296 | 298 |
297 void EmbeddedTestServer::DoAcceptLoop() { | 299 void EmbeddedTestServer::DoAcceptLoop() { |
298 int rv = OK; | 300 int rv = OK; |
299 while (rv == OK) { | 301 while (rv == OK) { |
300 rv = listen_socket_->Accept( | 302 rv = listen_socket_->Accept( |
301 &accepted_socket_, base::Bind(&EmbeddedTestServer::OnAcceptCompleted, | 303 &accepted_socket_, base::Bind(&EmbeddedTestServer::OnAcceptCompleted, |
302 base::Unretained(this))); | 304 base::Unretained(this))); |
303 if (rv == ERR_IO_PENDING) | 305 if (rv == ERR_IO_PENDING) |
304 return; | 306 return; |
305 HandleAcceptResult(accepted_socket_.Pass()); | 307 HandleAcceptResult(std::move(accepted_socket_)); |
306 } | 308 } |
307 } | 309 } |
308 | 310 |
309 void EmbeddedTestServer::OnAcceptCompleted(int rv) { | 311 void EmbeddedTestServer::OnAcceptCompleted(int rv) { |
310 DCHECK_NE(ERR_IO_PENDING, rv); | 312 DCHECK_NE(ERR_IO_PENDING, rv); |
311 HandleAcceptResult(accepted_socket_.Pass()); | 313 HandleAcceptResult(std::move(accepted_socket_)); |
312 DoAcceptLoop(); | 314 DoAcceptLoop(); |
313 } | 315 } |
314 | 316 |
315 void EmbeddedTestServer::OnHandshakeDone(HttpConnection* connection, int rv) { | 317 void EmbeddedTestServer::OnHandshakeDone(HttpConnection* connection, int rv) { |
316 if (connection->socket_->IsConnected()) | 318 if (connection->socket_->IsConnected()) |
317 ReadData(connection); | 319 ReadData(connection); |
318 else | 320 else |
319 DidClose(connection); | 321 DidClose(connection); |
320 } | 322 } |
321 | 323 |
322 void EmbeddedTestServer::HandleAcceptResult(scoped_ptr<StreamSocket> socket) { | 324 void EmbeddedTestServer::HandleAcceptResult(scoped_ptr<StreamSocket> socket) { |
323 DCHECK(io_thread_->task_runner()->BelongsToCurrentThread()); | 325 DCHECK(io_thread_->task_runner()->BelongsToCurrentThread()); |
324 if (connection_listener_) | 326 if (connection_listener_) |
325 connection_listener_->AcceptedSocket(*socket); | 327 connection_listener_->AcceptedSocket(*socket); |
326 | 328 |
327 if (is_using_ssl_) | 329 if (is_using_ssl_) |
328 socket = DoSSLUpgrade(socket.Pass()); | 330 socket = DoSSLUpgrade(std::move(socket)); |
329 | 331 |
330 HttpConnection* http_connection = new HttpConnection( | 332 HttpConnection* http_connection = new HttpConnection( |
331 socket.Pass(), | 333 std::move(socket), |
332 base::Bind(&EmbeddedTestServer::HandleRequest, base::Unretained(this))); | 334 base::Bind(&EmbeddedTestServer::HandleRequest, base::Unretained(this))); |
333 connections_[http_connection->socket_.get()] = http_connection; | 335 connections_[http_connection->socket_.get()] = http_connection; |
334 | 336 |
335 if (is_using_ssl_) { | 337 if (is_using_ssl_) { |
336 SSLServerSocket* ssl_socket = | 338 SSLServerSocket* ssl_socket = |
337 static_cast<SSLServerSocket*>(http_connection->socket_.get()); | 339 static_cast<SSLServerSocket*>(http_connection->socket_.get()); |
338 int rv = ssl_socket->Handshake( | 340 int rv = ssl_socket->Handshake( |
339 base::Bind(&EmbeddedTestServer::OnHandshakeDone, base::Unretained(this), | 341 base::Bind(&EmbeddedTestServer::OnHandshakeDone, base::Unretained(this), |
340 http_connection)); | 342 http_connection)); |
341 if (rv != ERR_IO_PENDING) | 343 if (rv != ERR_IO_PENDING) |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 run_loop.QuitClosure())) { | 422 run_loop.QuitClosure())) { |
421 return false; | 423 return false; |
422 } | 424 } |
423 run_loop.Run(); | 425 run_loop.Run(); |
424 | 426 |
425 return true; | 427 return true; |
426 } | 428 } |
427 | 429 |
428 } // namespace test_server | 430 } // namespace test_server |
429 } // namespace net | 431 } // namespace net |
OLD | NEW |