Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(316)

Side by Side Diff: net/test/embedded_test_server/embedded_test_server.cc

Issue 2562893002: Adds ScopedPortExceptions to EmbeddedTestServer. (Closed)
Patch Set: Another approach Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
11 #include "base/files/file_util.h" 11 #include "base/files/file_util.h"
12 #include "base/location.h" 12 #include "base/location.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
15 #include "base/message_loop/message_loop.h" 15 #include "base/message_loop/message_loop.h"
16 #include "base/path_service.h" 16 #include "base/path_service.h"
17 #include "base/process/process_metrics.h" 17 #include "base/process/process_metrics.h"
18 #include "base/run_loop.h" 18 #include "base/run_loop.h"
19 #include "base/strings/string_util.h" 19 #include "base/strings/string_util.h"
20 #include "base/strings/stringprintf.h" 20 #include "base/strings/stringprintf.h"
21 #include "base/threading/thread_restrictions.h" 21 #include "base/threading/thread_restrictions.h"
22 #include "base/threading/thread_task_runner_handle.h" 22 #include "base/threading/thread_task_runner_handle.h"
23 #include "crypto/rsa_private_key.h" 23 #include "crypto/rsa_private_key.h"
24 #include "net/base/ip_endpoint.h" 24 #include "net/base/ip_endpoint.h"
25 #include "net/base/net_errors.h" 25 #include "net/base/net_errors.h"
26 #include "net/base/port_util.h"
26 #include "net/cert/pem_tokenizer.h" 27 #include "net/cert/pem_tokenizer.h"
27 #include "net/cert/test_root_certs.h" 28 #include "net/cert/test_root_certs.h"
28 #include "net/log/net_log_source.h" 29 #include "net/log/net_log_source.h"
29 #include "net/socket/ssl_server_socket.h" 30 #include "net/socket/ssl_server_socket.h"
30 #include "net/socket/stream_socket.h" 31 #include "net/socket/stream_socket.h"
31 #include "net/socket/tcp_server_socket.h" 32 #include "net/socket/tcp_server_socket.h"
32 #include "net/ssl/ssl_server_config.h" 33 #include "net/ssl/ssl_server_config.h"
33 #include "net/test/cert_test_util.h" 34 #include "net/test/cert_test_util.h"
34 #include "net/test/embedded_test_server/default_handlers.h" 35 #include "net/test/embedded_test_server/default_handlers.h"
35 #include "net/test/embedded_test_server/embedded_test_server_connection_listener .h" 36 #include "net/test/embedded_test_server/embedded_test_server_connection_listener .h"
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 bool success = InitializeAndListen(); 86 bool success = InitializeAndListen();
86 if (!success) 87 if (!success)
87 return false; 88 return false;
88 StartAcceptingConnections(); 89 StartAcceptingConnections();
89 return true; 90 return true;
90 } 91 }
91 92
92 bool EmbeddedTestServer::InitializeAndListen() { 93 bool EmbeddedTestServer::InitializeAndListen() {
93 DCHECK(!Started()); 94 DCHECK(!Started());
94 95
95 listen_socket_.reset(new TCPServerSocket(nullptr, NetLogSource())); 96 const int max_tries = 5;
97 int num_tries = 0;
98 bool is_valid_port = false;
96 99
97 int result = listen_socket_->ListenWithAddressAndPort("127.0.0.1", 0, 10); 100 do {
98 if (result) { 101 if (++num_tries > max_tries) {
99 LOG(ERROR) << "Listen failed: " << ErrorToString(result); 102 LOG(ERROR) << "Failed to listen on a valid port after " << max_tries
100 listen_socket_.reset(); 103 << " attempts.";
101 return false; 104 listen_socket_.reset();
102 } 105 return false;
106 }
103 107
104 result = listen_socket_->GetLocalAddress(&local_endpoint_); 108 listen_socket_.reset(new TCPServerSocket(nullptr, NetLogSource()));
105 if (result != OK) { 109
106 LOG(ERROR) << "GetLocalAddress failed: " << ErrorToString(result); 110 int result = listen_socket_->ListenWithAddressAndPort("127.0.0.1", 0, 10);
107 listen_socket_.reset(); 111 if (result) {
108 return false; 112 LOG(ERROR) << "Listen failed: " << ErrorToString(result);
109 } 113 listen_socket_.reset();
114 return false;
115 }
116
117 result = listen_socket_->GetLocalAddress(&local_endpoint_);
118 if (result != OK) {
119 LOG(ERROR) << "GetLocalAddress failed: " << ErrorToString(result);
120 listen_socket_.reset();
121 return false;
122 }
123
124 port_ = local_endpoint_.port();
125 is_valid_port |= net::IsPortAllowedForScheme(
126 port_, is_using_ssl_ ? url::kHttpsScheme : url::kHttpScheme);
127 } while (!is_valid_port);
110 128
111 if (is_using_ssl_) { 129 if (is_using_ssl_) {
112 base_url_ = GURL("https://" + local_endpoint_.ToString()); 130 base_url_ = GURL("https://" + local_endpoint_.ToString());
113 if (cert_ == CERT_MISMATCHED_NAME || cert_ == CERT_COMMON_NAME_IS_DOMAIN) { 131 if (cert_ == CERT_MISMATCHED_NAME || cert_ == CERT_COMMON_NAME_IS_DOMAIN) {
114 base_url_ = GURL( 132 base_url_ = GURL(
115 base::StringPrintf("https://localhost:%d", local_endpoint_.port())); 133 base::StringPrintf("https://localhost:%d", local_endpoint_.port()));
116 } 134 }
117 } else { 135 } else {
118 base_url_ = GURL("http://" + local_endpoint_.ToString()); 136 base_url_ = GURL("http://" + local_endpoint_.ToString());
119 } 137 }
120 port_ = local_endpoint_.port();
121 138
122 listen_socket_->DetachFromThread(); 139 listen_socket_->DetachFromThread();
123 140
124 if (is_using_ssl_) 141 if (is_using_ssl_)
125 InitializeSSLServerContext(); 142 InitializeSSLServerContext();
126 return true; 143 return true;
127 } 144 }
128 145
129 void EmbeddedTestServer::InitializeSSLServerContext() { 146 void EmbeddedTestServer::InitializeSSLServerContext() {
130 base::ThreadRestrictions::ScopedAllowIO allow_io_for_ssl_initialization; 147 base::ThreadRestrictions::ScopedAllowIO allow_io_for_ssl_initialization;
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after
461 run_loop.QuitClosure())) { 478 run_loop.QuitClosure())) {
462 return false; 479 return false;
463 } 480 }
464 run_loop.Run(); 481 run_loop.Run();
465 482
466 return true; 483 return true;
467 } 484 }
468 485
469 } // namespace test_server 486 } // namespace test_server
470 } // namespace net 487 } // namespace net
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698