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

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

Issue 2481923002: [WIP] make GURL::path() return a StringPiece (Closed)
Patch Set: thanks asan Created 4 years, 1 month 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/request_handler_util.h" 5 #include "net/test/embedded_test_server/request_handler_util.h"
6 6
7 #include <stdlib.h> 7 #include <stdlib.h>
8 8
9 #include <ctime> 9 #include <ctime>
10 #include <sstream> 10 #include <sstream>
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 std::unique_ptr<HttpResponse> HandleFileRequest( 123 std::unique_ptr<HttpResponse> HandleFileRequest(
124 const base::FilePath& server_root, 124 const base::FilePath& server_root,
125 const HttpRequest& request) { 125 const HttpRequest& request) {
126 // This is a test-only server. Ignore I/O thread restrictions. 126 // This is a test-only server. Ignore I/O thread restrictions.
127 // TODO(svaldez): Figure out why thread is I/O restricted in the first place. 127 // TODO(svaldez): Figure out why thread is I/O restricted in the first place.
128 base::ThreadRestrictions::ScopedAllowIO allow_io; 128 base::ThreadRestrictions::ScopedAllowIO allow_io;
129 129
130 // A proxy request will have an absolute path. Simulate the proxy by stripping 130 // A proxy request will have an absolute path. Simulate the proxy by stripping
131 // the scheme, host, and port. 131 // the scheme, host, and port.
132 GURL request_url = request.GetURL(); 132 GURL request_url = request.GetURL();
133 std::string relative_path(request_url.path()); 133 base::StringPiece relative_path(request_url.path());
134 134
135 std::string post_prefix("/post/"); 135 std::string post_prefix("/post/");
136 if (base::StartsWith(relative_path, post_prefix, 136 if (base::StartsWith(relative_path, post_prefix,
137 base::CompareCase::SENSITIVE)) { 137 base::CompareCase::SENSITIVE)) {
138 if (request.method != METHOD_POST) 138 if (request.method != METHOD_POST)
139 return nullptr; 139 return nullptr;
140 relative_path = relative_path.substr(post_prefix.size() - 1); 140 relative_path = relative_path.substr(post_prefix.size() - 1);
141 } 141 }
142 142
143 RequestQuery query = ParseQuery(request_url); 143 RequestQuery query = ParseQuery(request_url);
(...skipping 16 matching lines...) Expand all
160 std::string value = header.substr(header.find(":") + 1); 160 std::string value = header.substr(header.find(":") + 1);
161 if (request.headers.find(key) == request.headers.end() || 161 if (request.headers.find(key) == request.headers.end() ||
162 request.headers.at(key) != value) { 162 request.headers.at(key) != value) {
163 return std::move(failed_response); 163 return std::move(failed_response);
164 } 164 }
165 } 165 }
166 } 166 }
167 167
168 // Trim the first byte ('/'). 168 // Trim the first byte ('/').
169 DCHECK(base::StartsWith(relative_path, "/", base::CompareCase::SENSITIVE)); 169 DCHECK(base::StartsWith(relative_path, "/", base::CompareCase::SENSITIVE));
170 std::string request_path = relative_path.substr(1); 170 base::StringPiece request_path = relative_path.substr(1);
171 base::FilePath file_path(server_root.AppendASCII(request_path)); 171 base::FilePath file_path(server_root.AppendASCII(request_path));
172 std::string file_contents; 172 std::string file_contents;
173 if (!base::ReadFileToString(file_path, &file_contents)) { 173 if (!base::ReadFileToString(file_path, &file_contents)) {
174 file_path = file_path.AppendASCII("index.html"); 174 file_path = file_path.AppendASCII("index.html");
175 if (!base::ReadFileToString(file_path, &file_contents)) 175 if (!base::ReadFileToString(file_path, &file_contents))
176 return nullptr; 176 return nullptr;
177 } 177 }
178 178
179 if (request.method == METHOD_HEAD) 179 if (request.method == METHOD_HEAD)
180 file_contents = ""; 180 file_contents = "";
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 227
228 http_response->set_content_type(GetContentType(file_path)); 228 http_response->set_content_type(GetContentType(file_path));
229 http_response->AddCustomHeader("Accept-Ranges", "bytes"); 229 http_response->AddCustomHeader("Accept-Ranges", "bytes");
230 http_response->AddCustomHeader("ETag", "'" + file_path.MaybeAsASCII() + "'"); 230 http_response->AddCustomHeader("ETag", "'" + file_path.MaybeAsASCII() + "'");
231 http_response->set_content(file_contents); 231 http_response->set_content(file_contents);
232 return std::move(http_response); 232 return std::move(http_response);
233 } 233 }
234 234
235 } // namespace test_server 235 } // namespace test_server
236 } // namespace net 236 } // namespace net
OLDNEW
« no previous file with comments | « net/test/embedded_test_server/http_request.cc ('k') | net/test/url_request/url_request_mock_http_job.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698