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

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

Issue 15740023: Revert "Revert 202112 "GTTF: Convert most tests in content to use Embedd..."" (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 6 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/embedded_test_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/files/file_path.h" 8 #include "base/files/file_path.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/path_service.h" 10 #include "base/path_service.h"
11 #include "base/run_loop.h" 11 #include "base/run_loop.h"
12 #include "base/stl_util.h" 12 #include "base/stl_util.h"
13 #include "base/string_util.h" 13 #include "base/string_util.h"
14 #include "base/stringprintf.h" 14 #include "base/stringprintf.h"
15 #include "base/threading/thread_restrictions.h" 15 #include "base/threading/thread_restrictions.h"
16 #include "net/base/ip_endpoint.h" 16 #include "net/base/ip_endpoint.h"
17 #include "net/base/net_errors.h" 17 #include "net/base/net_errors.h"
18 #include "net/test/embedded_test_server/http_connection.h" 18 #include "net/test/embedded_test_server/http_connection.h"
19 #include "net/test/embedded_test_server/http_request.h" 19 #include "net/test/embedded_test_server/http_request.h"
20 #include "net/test/embedded_test_server/http_response.h" 20 #include "net/test/embedded_test_server/http_response.h"
21 #include "net/tools/fetch/http_listen_socket.h" 21 #include "net/tools/fetch/http_listen_socket.h"
22 22
23 namespace net { 23 namespace net {
24 namespace test_server { 24 namespace test_server {
25 25
26 namespace { 26 namespace {
27 27
28 // Callback to handle requests with default predefined response for requests
29 // matching the address |url|.
30 scoped_ptr<HttpResponse> HandleDefaultRequest(const GURL& url,
31 const HttpResponse& response,
32 const HttpRequest& request) {
33 const GURL request_url = url.Resolve(request.relative_url);
34 if (url.path() != request_url.path())
35 return scoped_ptr<HttpResponse>(NULL);
36 return scoped_ptr<HttpResponse>(new HttpResponse(response));
37 }
38
39 // Handles |request| by serving a file from under |server_root|. 28 // Handles |request| by serving a file from under |server_root|.
40 scoped_ptr<HttpResponse> HandleFileRequest(const base::FilePath& server_root, 29 scoped_ptr<HttpResponse> HandleFileRequest(
41 const HttpRequest& request) { 30 const base::FilePath& server_root,
31 const HttpRequest& request) {
42 // This is a test-only server. Ignore I/O thread restrictions. 32 // This is a test-only server. Ignore I/O thread restrictions.
43 base::ThreadRestrictions::ScopedAllowIO allow_io; 33 base::ThreadRestrictions::ScopedAllowIO allow_io;
44 34
45 // Trim the first byte ('/'). 35 // Trim the first byte ('/').
46 std::string request_path(request.relative_url.substr(1)); 36 std::string request_path(request.relative_url.substr(1));
47 37
38 // Remove the query string if present.
39 size_t query_pos = request_path.find('?');
40 if (query_pos != std::string::npos)
41 request_path = request_path.substr(0, query_pos);
42
48 std::string file_contents; 43 std::string file_contents;
49 if (!file_util::ReadFileToString( 44 if (!file_util::ReadFileToString(
50 server_root.AppendASCII(request_path), &file_contents)) { 45 server_root.AppendASCII(request_path), &file_contents)) {
51 return scoped_ptr<HttpResponse>(NULL); 46 return scoped_ptr<HttpResponse>(NULL);
52 } 47 }
53 48
54 scoped_ptr<HttpResponse> http_response(new HttpResponse); 49 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse);
55 http_response->set_code(net::test_server::SUCCESS); 50 http_response->set_code(net::test_server::SUCCESS);
56 http_response->set_content(file_contents); 51 http_response->set_content(file_contents);
57 return http_response.Pass(); 52 return http_response.PassAs<HttpResponse>();
58 } 53 }
59 54
60 } // namespace 55 } // namespace
61 56
62 HttpListenSocket::HttpListenSocket(const SocketDescriptor socket_descriptor, 57 HttpListenSocket::HttpListenSocket(const SocketDescriptor socket_descriptor,
63 StreamListenSocket::Delegate* delegate) 58 StreamListenSocket::Delegate* delegate)
64 : TCPListenSocket(socket_descriptor, delegate) { 59 : TCPListenSocket(socket_descriptor, delegate) {
65 DCHECK(thread_checker_.CalledOnValidThread()); 60 DCHECK(thread_checker_.CalledOnValidThread());
66 } 61 }
67 62
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 listen_socket_ = NULL; // Release the listen socket. 145 listen_socket_ = NULL; // Release the listen socket.
151 STLDeleteContainerPairSecondPointers(connections_.begin(), 146 STLDeleteContainerPairSecondPointers(connections_.begin(),
152 connections_.end()); 147 connections_.end());
153 connections_.clear(); 148 connections_.clear();
154 } 149 }
155 150
156 void EmbeddedTestServer::HandleRequest(HttpConnection* connection, 151 void EmbeddedTestServer::HandleRequest(HttpConnection* connection,
157 scoped_ptr<HttpRequest> request) { 152 scoped_ptr<HttpRequest> request) {
158 DCHECK(io_thread_->BelongsToCurrentThread()); 153 DCHECK(io_thread_->BelongsToCurrentThread());
159 154
155 bool request_handled = false;
156
160 for (size_t i = 0; i < request_handlers_.size(); ++i) { 157 for (size_t i = 0; i < request_handlers_.size(); ++i) {
161 scoped_ptr<HttpResponse> response = 158 scoped_ptr<HttpResponse> response =
162 request_handlers_[i].Run(*request.get()); 159 request_handlers_[i].Run(*request.get());
163 if (response.get()) { 160 if (response.get()) {
164 connection->SendResponse(response.Pass()); 161 connection->SendResponse(response.Pass());
165 return; 162 request_handled = true;
163 break;
166 } 164 }
167 } 165 }
168 166
169 LOG(WARNING) << "Request not handled. Returning 404: " 167 if (!request_handled) {
170 << request->relative_url; 168 LOG(WARNING) << "Request not handled. Returning 404: "
171 scoped_ptr<HttpResponse> not_found_response(new HttpResponse()); 169 << request->relative_url;
172 not_found_response->set_code(NOT_FOUND); 170 scoped_ptr<BasicHttpResponse> not_found_response(new BasicHttpResponse);
173 connection->SendResponse(not_found_response.Pass()); 171 not_found_response->set_code(NOT_FOUND);
172 connection->SendResponse(
173 not_found_response.PassAs<HttpResponse>());
174 }
174 175
175 // Drop the connection, since we do not support multiple requests per 176 // Drop the connection, since we do not support multiple requests per
176 // connection. 177 // connection.
177 connections_.erase(connection->socket_.get()); 178 connections_.erase(connection->socket_.get());
178 delete connection; 179 delete connection;
179 } 180 }
180 181
181 GURL EmbeddedTestServer::GetURL(const std::string& relative_url) const { 182 GURL EmbeddedTestServer::GetURL(const std::string& relative_url) const {
182 DCHECK(StartsWithASCII(relative_url, "/", true /* case_sensitive */)) 183 DCHECK(StartsWithASCII(relative_url, "/", true /* case_sensitive */))
183 << relative_url; 184 << relative_url;
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 std::map<StreamListenSocket*, HttpConnection*>::iterator it = 238 std::map<StreamListenSocket*, HttpConnection*>::iterator it =
238 connections_.find(socket); 239 connections_.find(socket);
239 if (it == connections_.end()) { 240 if (it == connections_.end()) {
240 return NULL; 241 return NULL;
241 } 242 }
242 return it->second; 243 return it->second;
243 } 244 }
244 245
245 } // namespace test_server 246 } // namespace test_server
246 } // namespace net 247 } // 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