OLD | NEW |
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 #include <ctime> | 9 #include <ctime> |
9 #include <sstream> | 10 #include <sstream> |
10 #include <utility> | 11 #include <utility> |
11 | 12 |
12 #include "base/base64.h" | 13 #include "base/base64.h" |
13 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
14 #include "base/format_macros.h" | 15 #include "base/format_macros.h" |
| 16 #include "base/memory/ptr_util.h" |
15 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
16 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
17 #include "base/threading/thread_restrictions.h" | 19 #include "base/threading/thread_restrictions.h" |
18 #include "net/base/escape.h" | 20 #include "net/base/escape.h" |
19 #include "net/base/url_util.h" | 21 #include "net/base/url_util.h" |
20 #include "net/http/http_byte_range.h" | 22 #include "net/http/http_byte_range.h" |
21 #include "net/http/http_util.h" | 23 #include "net/http/http_util.h" |
22 #include "net/test/embedded_test_server/http_request.h" | 24 #include "net/test/embedded_test_server/http_request.h" |
23 #include "url/gurl.h" | 25 #include "url/gurl.h" |
24 | 26 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 | 70 |
69 } // namespace | 71 } // namespace |
70 | 72 |
71 bool ShouldHandle(const HttpRequest& request, const std::string& path_prefix) { | 73 bool ShouldHandle(const HttpRequest& request, const std::string& path_prefix) { |
72 GURL url = request.GetURL(); | 74 GURL url = request.GetURL(); |
73 return url.path() == path_prefix || | 75 return url.path() == path_prefix || |
74 base::StartsWith(url.path(), path_prefix + "/", | 76 base::StartsWith(url.path(), path_prefix + "/", |
75 base::CompareCase::SENSITIVE); | 77 base::CompareCase::SENSITIVE); |
76 } | 78 } |
77 | 79 |
78 scoped_ptr<HttpResponse> HandlePrefixedRequest( | 80 std::unique_ptr<HttpResponse> HandlePrefixedRequest( |
79 const std::string& prefix, | 81 const std::string& prefix, |
80 const EmbeddedTestServer::HandleRequestCallback& handler, | 82 const EmbeddedTestServer::HandleRequestCallback& handler, |
81 const HttpRequest& request) { | 83 const HttpRequest& request) { |
82 if (ShouldHandle(request, prefix)) | 84 if (ShouldHandle(request, prefix)) |
83 return handler.Run(request); | 85 return handler.Run(request); |
84 return nullptr; | 86 return nullptr; |
85 } | 87 } |
86 | 88 |
87 RequestQuery ParseQuery(const GURL& url) { | 89 RequestQuery ParseQuery(const GURL& url) { |
88 RequestQuery queries; | 90 RequestQuery queries; |
(...skipping 22 matching lines...) Expand all Loading... |
111 new_file_path += "replace_text="; | 113 new_file_path += "replace_text="; |
112 new_file_path += base64_old; | 114 new_file_path += base64_old; |
113 new_file_path += ":"; | 115 new_file_path += ":"; |
114 new_file_path += base64_new; | 116 new_file_path += base64_new; |
115 } | 117 } |
116 | 118 |
117 *replacement_path = new_file_path; | 119 *replacement_path = new_file_path; |
118 } | 120 } |
119 | 121 |
120 // Handles |request| by serving a file from under |server_root|. | 122 // Handles |request| by serving a file from under |server_root|. |
121 scoped_ptr<HttpResponse> HandleFileRequest(const base::FilePath& server_root, | 123 std::unique_ptr<HttpResponse> HandleFileRequest( |
122 const HttpRequest& request) { | 124 const base::FilePath& server_root, |
| 125 const HttpRequest& request) { |
123 // This is a test-only server. Ignore I/O thread restrictions. | 126 // This is a test-only server. Ignore I/O thread restrictions. |
124 // 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. |
125 base::ThreadRestrictions::ScopedAllowIO allow_io; | 128 base::ThreadRestrictions::ScopedAllowIO allow_io; |
126 | 129 |
127 // 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 |
128 // the scheme, host, and port. | 131 // the scheme, host, and port. |
129 GURL request_url = request.GetURL(); | 132 GURL request_url = request.GetURL(); |
130 std::string relative_path(request_url.path()); | 133 std::string relative_path(request_url.path()); |
131 | 134 |
132 std::string post_prefix("/post/"); | 135 std::string post_prefix("/post/"); |
133 if (base::StartsWith(relative_path, post_prefix, | 136 if (base::StartsWith(relative_path, post_prefix, |
134 base::CompareCase::SENSITIVE)) { | 137 base::CompareCase::SENSITIVE)) { |
135 if (request.method != METHOD_POST) | 138 if (request.method != METHOD_POST) |
136 return nullptr; | 139 return nullptr; |
137 relative_path = relative_path.substr(post_prefix.size() - 1); | 140 relative_path = relative_path.substr(post_prefix.size() - 1); |
138 } | 141 } |
139 | 142 |
140 RequestQuery query = ParseQuery(request_url); | 143 RequestQuery query = ParseQuery(request_url); |
141 | 144 |
142 scoped_ptr<BasicHttpResponse> failed_response(new BasicHttpResponse); | 145 std::unique_ptr<BasicHttpResponse> failed_response(new BasicHttpResponse); |
143 failed_response->set_code(HTTP_NOT_FOUND); | 146 failed_response->set_code(HTTP_NOT_FOUND); |
144 | 147 |
145 if (query.find("expected_body") != query.end()) { | 148 if (query.find("expected_body") != query.end()) { |
146 if (request.content.find(query["expected_body"].front()) == | 149 if (request.content.find(query["expected_body"].front()) == |
147 std::string::npos) { | 150 std::string::npos) { |
148 return std::move(failed_response); | 151 return std::move(failed_response); |
149 } | 152 } |
150 } | 153 } |
151 | 154 |
152 if (query.find("expected_headers") != query.end()) { | 155 if (query.find("expected_headers") != query.end()) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 | 193 |
191 base::FilePath headers_path( | 194 base::FilePath headers_path( |
192 file_path.AddExtension(FILE_PATH_LITERAL("mock-http-headers"))); | 195 file_path.AddExtension(FILE_PATH_LITERAL("mock-http-headers"))); |
193 | 196 |
194 if (base::PathExists(headers_path)) { | 197 if (base::PathExists(headers_path)) { |
195 std::string headers_contents; | 198 std::string headers_contents; |
196 | 199 |
197 if (!base::ReadFileToString(headers_path, &headers_contents)) | 200 if (!base::ReadFileToString(headers_path, &headers_contents)) |
198 return nullptr; | 201 return nullptr; |
199 | 202 |
200 return make_scoped_ptr( | 203 return base::WrapUnique( |
201 new RawHttpResponse(headers_contents, file_contents)); | 204 new RawHttpResponse(headers_contents, file_contents)); |
202 } | 205 } |
203 | 206 |
204 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); | 207 std::unique_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
205 http_response->set_code(HTTP_OK); | 208 http_response->set_code(HTTP_OK); |
206 | 209 |
207 if (request.headers.find("Range") != request.headers.end()) { | 210 if (request.headers.find("Range") != request.headers.end()) { |
208 std::vector<HttpByteRange> ranges; | 211 std::vector<HttpByteRange> ranges; |
209 | 212 |
210 if (HttpUtil::ParseRangeHeader(request.headers.at("Range"), &ranges) && | 213 if (HttpUtil::ParseRangeHeader(request.headers.at("Range"), &ranges) && |
211 ranges.size() == 1) { | 214 ranges.size() == 1) { |
212 ranges[0].ComputeBounds(file_contents.size()); | 215 ranges[0].ComputeBounds(file_contents.size()); |
213 size_t start = ranges[0].first_byte_position(); | 216 size_t start = ranges[0].first_byte_position(); |
214 size_t end = ranges[0].last_byte_position(); | 217 size_t end = ranges[0].last_byte_position(); |
(...skipping 10 matching lines...) Expand all Loading... |
225 | 228 |
226 http_response->set_content_type(GetContentType(file_path)); | 229 http_response->set_content_type(GetContentType(file_path)); |
227 http_response->AddCustomHeader("Accept-Ranges", "bytes"); | 230 http_response->AddCustomHeader("Accept-Ranges", "bytes"); |
228 http_response->AddCustomHeader("ETag", "'" + file_path.MaybeAsASCII() + "'"); | 231 http_response->AddCustomHeader("ETag", "'" + file_path.MaybeAsASCII() + "'"); |
229 http_response->set_content(file_contents); | 232 http_response->set_content(file_contents); |
230 return std::move(http_response); | 233 return std::move(http_response); |
231 } | 234 } |
232 | 235 |
233 } // namespace test_server | 236 } // namespace test_server |
234 } // namespace net | 237 } // namespace net |
OLD | NEW |