| 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 #include <ctime> | 8 #include <ctime> |
| 9 #include <sstream> | 9 #include <sstream> |
| 10 #include <utility> |
| 10 | 11 |
| 11 #include "base/base64.h" | 12 #include "base/base64.h" |
| 12 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 13 #include "base/format_macros.h" | 14 #include "base/format_macros.h" |
| 14 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 15 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| 16 #include "base/threading/thread_restrictions.h" | 17 #include "base/threading/thread_restrictions.h" |
| 17 #include "net/base/escape.h" | 18 #include "net/base/escape.h" |
| 18 #include "net/base/url_util.h" | 19 #include "net/base/url_util.h" |
| 19 #include "net/http/http_byte_range.h" | 20 #include "net/http/http_byte_range.h" |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 } | 133 } |
| 133 | 134 |
| 134 RequestQuery query = ParseQuery(request_url); | 135 RequestQuery query = ParseQuery(request_url); |
| 135 | 136 |
| 136 scoped_ptr<BasicHttpResponse> failed_response(new BasicHttpResponse); | 137 scoped_ptr<BasicHttpResponse> failed_response(new BasicHttpResponse); |
| 137 failed_response->set_code(HTTP_NOT_FOUND); | 138 failed_response->set_code(HTTP_NOT_FOUND); |
| 138 | 139 |
| 139 if (query.find("expected_body") != query.end()) { | 140 if (query.find("expected_body") != query.end()) { |
| 140 if (request.content.find(query["expected_body"].front()) == | 141 if (request.content.find(query["expected_body"].front()) == |
| 141 std::string::npos) { | 142 std::string::npos) { |
| 142 return failed_response.Pass(); | 143 return std::move(failed_response); |
| 143 } | 144 } |
| 144 } | 145 } |
| 145 | 146 |
| 146 if (query.find("expected_headers") != query.end()) { | 147 if (query.find("expected_headers") != query.end()) { |
| 147 for (const auto& header : query["expected_headers"]) { | 148 for (const auto& header : query["expected_headers"]) { |
| 148 if (header.find(":") == std::string::npos) | 149 if (header.find(":") == std::string::npos) |
| 149 return failed_response.Pass(); | 150 return std::move(failed_response); |
| 150 std::string key = header.substr(0, header.find(":")); | 151 std::string key = header.substr(0, header.find(":")); |
| 151 std::string value = header.substr(header.find(":") + 1); | 152 std::string value = header.substr(header.find(":") + 1); |
| 152 if (request.headers.find(key) == request.headers.end() || | 153 if (request.headers.find(key) == request.headers.end() || |
| 153 request.headers.at(key) != value) { | 154 request.headers.at(key) != value) { |
| 154 return failed_response.Pass(); | 155 return std::move(failed_response); |
| 155 } | 156 } |
| 156 } | 157 } |
| 157 } | 158 } |
| 158 | 159 |
| 159 // Trim the first byte ('/'). | 160 // Trim the first byte ('/'). |
| 160 DCHECK(base::StartsWith(relative_path, "/", base::CompareCase::SENSITIVE)); | 161 DCHECK(base::StartsWith(relative_path, "/", base::CompareCase::SENSITIVE)); |
| 161 std::string request_path = relative_path.substr(1); | 162 std::string request_path = relative_path.substr(1); |
| 162 base::FilePath file_path(server_root.AppendASCII(request_path)); | 163 base::FilePath file_path(server_root.AppendASCII(request_path)); |
| 163 std::string file_contents; | 164 std::string file_contents; |
| 164 if (!base::ReadFileToString(file_path, &file_contents)) { | 165 if (!base::ReadFileToString(file_path, &file_contents)) { |
| 165 file_path = file_path.AppendASCII("index.html"); | 166 file_path = file_path.AppendASCII("index.html"); |
| 166 if (!base::ReadFileToString(file_path, &file_contents)) | 167 if (!base::ReadFileToString(file_path, &file_contents)) |
| 167 return nullptr; | 168 return nullptr; |
| 168 } | 169 } |
| 169 | 170 |
| 170 if (request.method == METHOD_HEAD) | 171 if (request.method == METHOD_HEAD) |
| 171 file_contents = ""; | 172 file_contents = ""; |
| 172 | 173 |
| 173 if (query.find("replace_text") != query.end()) { | 174 if (query.find("replace_text") != query.end()) { |
| 174 for (const auto& replacement : query["replace_text"]) { | 175 for (const auto& replacement : query["replace_text"]) { |
| 175 if (replacement.find(":") == std::string::npos) | 176 if (replacement.find(":") == std::string::npos) |
| 176 return failed_response.Pass(); | 177 return std::move(failed_response); |
| 177 std::string find; | 178 std::string find; |
| 178 std::string with; | 179 std::string with; |
| 179 base::Base64Decode(replacement.substr(0, replacement.find(":")), &find); | 180 base::Base64Decode(replacement.substr(0, replacement.find(":")), &find); |
| 180 base::Base64Decode(replacement.substr(replacement.find(":") + 1), &with); | 181 base::Base64Decode(replacement.substr(replacement.find(":") + 1), &with); |
| 181 base::ReplaceSubstringsAfterOffset(&file_contents, 0, find, with); | 182 base::ReplaceSubstringsAfterOffset(&file_contents, 0, find, with); |
| 182 } | 183 } |
| 183 } | 184 } |
| 184 | 185 |
| 185 base::FilePath headers_path( | 186 base::FilePath headers_path( |
| 186 file_path.AddExtension(FILE_PATH_LITERAL("mock-http-headers"))); | 187 file_path.AddExtension(FILE_PATH_LITERAL("mock-http-headers"))); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 214 file_contents.size())); | 215 file_contents.size())); |
| 215 | 216 |
| 216 file_contents = file_contents.substr(start, end - start + 1); | 217 file_contents = file_contents.substr(start, end - start + 1); |
| 217 } | 218 } |
| 218 } | 219 } |
| 219 | 220 |
| 220 http_response->set_content_type(GetContentType(file_path)); | 221 http_response->set_content_type(GetContentType(file_path)); |
| 221 http_response->AddCustomHeader("Accept-Ranges", "bytes"); | 222 http_response->AddCustomHeader("Accept-Ranges", "bytes"); |
| 222 http_response->AddCustomHeader("ETag", "'" + file_path.MaybeAsASCII() + "'"); | 223 http_response->AddCustomHeader("ETag", "'" + file_path.MaybeAsASCII() + "'"); |
| 223 http_response->set_content(file_contents); | 224 http_response->set_content(file_contents); |
| 224 return http_response.Pass(); | 225 return std::move(http_response); |
| 225 } | 226 } |
| 226 | 227 |
| 227 } // namespace test_server | 228 } // namespace test_server |
| 228 } // namespace net | 229 } // namespace net |
| OLD | NEW |