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

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

Issue 15069003: Rename the embedded test server to EmbeddedTestServer in net::test_server namespace. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: nit Created 7 years, 7 months 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 | Annotate | Revision Log
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/http_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/run_loop.h" 8 #include "base/run_loop.h"
9 #include "base/stl_util.h" 9 #include "base/stl_util.h"
10 #include "base/string_util.h" 10 #include "base/string_util.h"
11 #include "base/stringprintf.h" 11 #include "base/stringprintf.h"
12 #include "net/test/embedded_test_server/http_connection.h" 12 #include "net/test/embedded_test_server/http_connection.h"
13 #include "net/test/embedded_test_server/http_request.h" 13 #include "net/test/embedded_test_server/http_request.h"
14 #include "net/test/embedded_test_server/http_response.h" 14 #include "net/test/embedded_test_server/http_response.h"
15 #include "net/tools/fetch/http_listen_socket.h" 15 #include "net/tools/fetch/http_listen_socket.h"
16 16
17 namespace google_apis { 17 namespace net {
18 namespace test_server { 18 namespace test_server {
19 19
20 namespace { 20 namespace {
21 21
22 const int kPort = 8040; 22 const int kPort = 8040;
23 const char kIp[] = "127.0.0.1"; 23 const char kIp[] = "127.0.0.1";
24 const int kRetries = 10; 24 const int kRetries = 10;
25 25
26 // Callback to handle requests with default predefined response for requests 26 // Callback to handle requests with default predefined response for requests
27 // matching the address |url|. 27 // matching the address |url|.
28 scoped_ptr<HttpResponse> HandleDefaultRequest(const GURL& url, 28 scoped_ptr<HttpResponse> HandleDefaultRequest(const GURL& url,
29 const HttpResponse& response, 29 const HttpResponse& response,
30 const HttpRequest& request) { 30 const HttpRequest& request) {
31 const GURL request_url = url.Resolve(request.relative_url); 31 const GURL request_url = url.Resolve(request.relative_url);
32 if (url.path() != request_url.path()) 32 if (url.path() != request_url.path())
33 return scoped_ptr<HttpResponse>(NULL); 33 return scoped_ptr<HttpResponse>(NULL);
34 return scoped_ptr<HttpResponse>(new HttpResponse(response)); 34 return scoped_ptr<HttpResponse>(new HttpResponse(response));
35 } 35 }
36 36
37 } // namespace 37 } // namespace
38 38
39 HttpListenSocket::HttpListenSocket(const SocketDescriptor socket_descriptor, 39 HttpListenSocket::HttpListenSocket(const SocketDescriptor socket_descriptor,
40 net::StreamListenSocket::Delegate* delegate) 40 StreamListenSocket::Delegate* delegate)
41 : net::TCPListenSocket(socket_descriptor, delegate) { 41 : TCPListenSocket(socket_descriptor, delegate) {
42 DCHECK(thread_checker_.CalledOnValidThread()); 42 DCHECK(thread_checker_.CalledOnValidThread());
43 } 43 }
44 44
45 void HttpListenSocket::Listen() { 45 void HttpListenSocket::Listen() {
46 DCHECK(thread_checker_.CalledOnValidThread()); 46 DCHECK(thread_checker_.CalledOnValidThread());
47 net::TCPListenSocket::Listen(); 47 TCPListenSocket::Listen();
48 } 48 }
49 49
50 HttpListenSocket::~HttpListenSocket() { 50 HttpListenSocket::~HttpListenSocket() {
51 DCHECK(thread_checker_.CalledOnValidThread()); 51 DCHECK(thread_checker_.CalledOnValidThread());
52 } 52 }
53 53
54 HttpServer::HttpServer( 54 EmbeddedTestServer::EmbeddedTestServer(
55 const scoped_refptr<base::SingleThreadTaskRunner>& io_thread) 55 const scoped_refptr<base::SingleThreadTaskRunner>& io_thread)
56 : io_thread_(io_thread), 56 : io_thread_(io_thread),
57 port_(-1), 57 port_(-1),
58 weak_factory_(this) { 58 weak_factory_(this) {
59 DCHECK(io_thread_); 59 DCHECK(io_thread_);
60 DCHECK(thread_checker_.CalledOnValidThread()); 60 DCHECK(thread_checker_.CalledOnValidThread());
61 } 61 }
62 62
63 HttpServer::~HttpServer() { 63 EmbeddedTestServer::~EmbeddedTestServer() {
64 DCHECK(thread_checker_.CalledOnValidThread()); 64 DCHECK(thread_checker_.CalledOnValidThread());
65 } 65 }
66 66
67 bool HttpServer::InitializeAndWaitUntilReady() { 67 bool EmbeddedTestServer::InitializeAndWaitUntilReady() {
68 DCHECK(thread_checker_.CalledOnValidThread()); 68 DCHECK(thread_checker_.CalledOnValidThread());
69 69
70 base::RunLoop run_loop; 70 base::RunLoop run_loop;
71 if (!io_thread_->PostTaskAndReply( 71 if (!io_thread_->PostTaskAndReply(
72 FROM_HERE, 72 FROM_HERE,
73 base::Bind(&HttpServer::InitializeOnIOThread, base::Unretained(this)), 73 base::Bind(&EmbeddedTestServer::InitializeOnIOThread,
74 base::Unretained(this)),
74 run_loop.QuitClosure())) { 75 run_loop.QuitClosure())) {
75 return false; 76 return false;
76 } 77 }
77 run_loop.Run(); 78 run_loop.Run();
78 79
79 return Started(); 80 return Started();
80 } 81 }
81 82
82 bool HttpServer::ShutdownAndWaitUntilComplete() { 83 bool EmbeddedTestServer::ShutdownAndWaitUntilComplete() {
83 DCHECK(thread_checker_.CalledOnValidThread()); 84 DCHECK(thread_checker_.CalledOnValidThread());
84 85
85 base::RunLoop run_loop; 86 base::RunLoop run_loop;
86 if (!io_thread_->PostTaskAndReply( 87 if (!io_thread_->PostTaskAndReply(
87 FROM_HERE, 88 FROM_HERE,
88 base::Bind(&HttpServer::ShutdownOnIOThread, base::Unretained(this)), 89 base::Bind(&EmbeddedTestServer::ShutdownOnIOThread,
90 base::Unretained(this)),
89 run_loop.QuitClosure())) { 91 run_loop.QuitClosure())) {
90 return false; 92 return false;
91 } 93 }
92 run_loop.Run(); 94 run_loop.Run();
93 95
94 return true; 96 return true;
95 } 97 }
96 98
97 void HttpServer::InitializeOnIOThread() { 99 void EmbeddedTestServer::InitializeOnIOThread() {
98 DCHECK(io_thread_->BelongsToCurrentThread()); 100 DCHECK(io_thread_->BelongsToCurrentThread());
99 DCHECK(!Started()); 101 DCHECK(!Started());
100 102
101 int retries_left = kRetries + 1; 103 int retries_left = kRetries + 1;
102 int try_port = kPort; 104 int try_port = kPort;
103 105
104 while (retries_left > 0) { 106 while (retries_left > 0) {
105 SocketDescriptor socket_descriptor = net::TCPListenSocket::CreateAndBind( 107 SocketDescriptor socket_descriptor = TCPListenSocket::CreateAndBind(
106 kIp, 108 kIp,
107 try_port); 109 try_port);
108 if (socket_descriptor != net::TCPListenSocket::kInvalidSocket) { 110 if (socket_descriptor != TCPListenSocket::kInvalidSocket) {
109 listen_socket_ = new HttpListenSocket(socket_descriptor, this); 111 listen_socket_ = new HttpListenSocket(socket_descriptor, this);
110 listen_socket_->Listen(); 112 listen_socket_->Listen();
111 base_url_ = GURL(base::StringPrintf("http://%s:%d", kIp, try_port)); 113 base_url_ = GURL(base::StringPrintf("http://%s:%d", kIp, try_port));
112 port_ = try_port; 114 port_ = try_port;
113 break; 115 break;
114 } 116 }
115 retries_left--; 117 retries_left--;
116 try_port++; 118 try_port++;
117 } 119 }
118 } 120 }
119 121
120 void HttpServer::ShutdownOnIOThread() { 122 void EmbeddedTestServer::ShutdownOnIOThread() {
121 DCHECK(io_thread_->BelongsToCurrentThread()); 123 DCHECK(io_thread_->BelongsToCurrentThread());
122 124
123 listen_socket_ = NULL; // Release the listen socket. 125 listen_socket_ = NULL; // Release the listen socket.
124 STLDeleteContainerPairSecondPointers(connections_.begin(), 126 STLDeleteContainerPairSecondPointers(connections_.begin(),
125 connections_.end()); 127 connections_.end());
126 connections_.clear(); 128 connections_.clear();
127 } 129 }
128 130
129 void HttpServer::HandleRequest(HttpConnection* connection, 131 void EmbeddedTestServer::HandleRequest(HttpConnection* connection,
130 scoped_ptr<HttpRequest> request) { 132 scoped_ptr<HttpRequest> request) {
131 DCHECK(io_thread_->BelongsToCurrentThread()); 133 DCHECK(io_thread_->BelongsToCurrentThread());
132 134
133 for (size_t i = 0; i < request_handlers_.size(); ++i) { 135 for (size_t i = 0; i < request_handlers_.size(); ++i) {
134 scoped_ptr<HttpResponse> response = 136 scoped_ptr<HttpResponse> response =
135 request_handlers_[i].Run(*request.get()); 137 request_handlers_[i].Run(*request.get());
136 if (response.get()) { 138 if (response.get()) {
137 connection->SendResponse(response.Pass()); 139 connection->SendResponse(response.Pass());
138 return; 140 return;
139 } 141 }
140 } 142 }
141 143
142 LOG(WARNING) << "Request not handled. Returning 404: " 144 LOG(WARNING) << "Request not handled. Returning 404: "
143 << request->relative_url; 145 << request->relative_url;
144 scoped_ptr<HttpResponse> not_found_response(new HttpResponse()); 146 scoped_ptr<HttpResponse> not_found_response(new HttpResponse());
145 not_found_response->set_code(NOT_FOUND); 147 not_found_response->set_code(NOT_FOUND);
146 connection->SendResponse(not_found_response.Pass()); 148 connection->SendResponse(not_found_response.Pass());
147 149
148 // Drop the connection, since we do not support multiple requests per 150 // Drop the connection, since we do not support multiple requests per
149 // connection. 151 // connection.
150 connections_.erase(connection->socket_.get()); 152 connections_.erase(connection->socket_.get());
151 delete connection; 153 delete connection;
152 } 154 }
153 155
154 GURL HttpServer::GetURL(const std::string& relative_url) const { 156 GURL EmbeddedTestServer::GetURL(const std::string& relative_url) const {
155 DCHECK(StartsWithASCII(relative_url, "/", true /* case_sensitive */)) 157 DCHECK(StartsWithASCII(relative_url, "/", true /* case_sensitive */))
156 << relative_url; 158 << relative_url;
157 return base_url_.Resolve(relative_url); 159 return base_url_.Resolve(relative_url);
158 } 160 }
159 161
160 void HttpServer::RegisterRequestHandler( 162 void EmbeddedTestServer::RegisterRequestHandler(
161 const HandleRequestCallback& callback) { 163 const HandleRequestCallback& callback) {
162 request_handlers_.push_back(callback); 164 request_handlers_.push_back(callback);
163 } 165 }
164 166
165 void HttpServer::DidAccept(net::StreamListenSocket* server, 167 void EmbeddedTestServer::DidAccept(StreamListenSocket* server,
166 net::StreamListenSocket* connection) { 168 StreamListenSocket* connection) {
167 DCHECK(io_thread_->BelongsToCurrentThread()); 169 DCHECK(io_thread_->BelongsToCurrentThread());
168 170
169 HttpConnection* http_connection = new HttpConnection( 171 HttpConnection* http_connection = new HttpConnection(
170 connection, 172 connection,
171 base::Bind(&HttpServer::HandleRequest, weak_factory_.GetWeakPtr())); 173 base::Bind(&EmbeddedTestServer::HandleRequest,
174 weak_factory_.GetWeakPtr()));
172 connections_[connection] = http_connection; 175 connections_[connection] = http_connection;
173 } 176 }
174 177
175 void HttpServer::DidRead(net::StreamListenSocket* connection, 178 void EmbeddedTestServer::DidRead(StreamListenSocket* connection,
176 const char* data, 179 const char* data,
177 int length) { 180 int length) {
178 DCHECK(io_thread_->BelongsToCurrentThread()); 181 DCHECK(io_thread_->BelongsToCurrentThread());
179 182
180 HttpConnection* http_connection = FindConnection(connection); 183 HttpConnection* http_connection = FindConnection(connection);
181 if (http_connection == NULL) { 184 if (http_connection == NULL) {
182 LOG(WARNING) << "Unknown connection."; 185 LOG(WARNING) << "Unknown connection.";
183 return; 186 return;
184 } 187 }
185 http_connection->ReceiveData(std::string(data, length)); 188 http_connection->ReceiveData(std::string(data, length));
186 } 189 }
187 190
188 void HttpServer::DidClose(net::StreamListenSocket* connection) { 191 void EmbeddedTestServer::DidClose(StreamListenSocket* connection) {
189 DCHECK(io_thread_->BelongsToCurrentThread()); 192 DCHECK(io_thread_->BelongsToCurrentThread());
190 193
191 HttpConnection* http_connection = FindConnection(connection); 194 HttpConnection* http_connection = FindConnection(connection);
192 if (http_connection == NULL) { 195 if (http_connection == NULL) {
193 LOG(WARNING) << "Unknown connection."; 196 LOG(WARNING) << "Unknown connection.";
194 return; 197 return;
195 } 198 }
196 delete http_connection; 199 delete http_connection;
197 connections_.erase(connection); 200 connections_.erase(connection);
198 } 201 }
199 202
200 HttpConnection* HttpServer::FindConnection( 203 HttpConnection* EmbeddedTestServer::FindConnection(
201 net::StreamListenSocket* socket) { 204 StreamListenSocket* socket) {
202 DCHECK(io_thread_->BelongsToCurrentThread()); 205 DCHECK(io_thread_->BelongsToCurrentThread());
203 206
204 std::map<net::StreamListenSocket*, HttpConnection*>::iterator it = 207 std::map<StreamListenSocket*, HttpConnection*>::iterator it =
205 connections_.find(socket); 208 connections_.find(socket);
206 if (it == connections_.end()) { 209 if (it == connections_.end()) {
207 return NULL; 210 return NULL;
208 } 211 }
209 return it->second; 212 return it->second;
210 } 213 }
211 214
212 } // namespace test_server 215 } // namespace test_server
213 } // namespace google_apis 216 } // namespace net
OLDNEW
« no previous file with comments | « net/test/embedded_test_server/embedded_test_server.h ('k') | net/test/embedded_test_server/embedded_test_server_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698