| OLD | NEW |
| 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/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 return headers_ + "\r\n" + contents_; | 35 return headers_ + "\r\n" + contents_; |
| 36 } | 36 } |
| 37 | 37 |
| 38 private: | 38 private: |
| 39 std::string headers_; | 39 std::string headers_; |
| 40 std::string contents_; | 40 std::string contents_; |
| 41 | 41 |
| 42 DISALLOW_COPY_AND_ASSIGN(CustomHttpResponse); | 42 DISALLOW_COPY_AND_ASSIGN(CustomHttpResponse); |
| 43 }; | 43 }; |
| 44 | 44 |
| 45 // Handles |request| by serving a file from under |server_root|. | 45 // Handles |request| by serving a file from under |server_roots|. |
| 46 scoped_ptr<HttpResponse> HandleFileRequest( | 46 scoped_ptr<HttpResponse> HandleFileRequest( |
| 47 const base::FilePath& server_root, | 47 const std::vector<base::FilePath>& server_roots, |
| 48 const HttpRequest& request) { | 48 const HttpRequest& request) { |
| 49 // This is a test-only server. Ignore I/O thread restrictions. | 49 // This is a test-only server. Ignore I/O thread restrictions. |
| 50 base::ThreadRestrictions::ScopedAllowIO allow_io; | 50 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 51 | 51 |
| 52 std::string relative_url(request.relative_url); | 52 std::string relative_url(request.relative_url); |
| 53 // A proxy request will have an absolute path. Simulate the proxy by stripping | 53 // A proxy request will have an absolute path. Simulate the proxy by stripping |
| 54 // the scheme, host, and port. | 54 // the scheme, host, and port. |
| 55 GURL relative_gurl(relative_url); | 55 GURL relative_gurl(relative_url); |
| 56 if (relative_gurl.is_valid()) | 56 if (relative_gurl.is_valid()) |
| 57 relative_url = relative_gurl.PathForRequest(); | 57 relative_url = relative_gurl.PathForRequest(); |
| 58 | 58 |
| 59 // Trim the first byte ('/'). | 59 // Trim the first byte ('/'). |
| 60 std::string request_path = relative_url.substr(1); | 60 std::string request_path = relative_url.substr(1); |
| 61 | 61 |
| 62 // Remove the query string if present. | 62 // Remove the query string if present. |
| 63 size_t query_pos = request_path.find('?'); | 63 size_t query_pos = request_path.find('?'); |
| 64 if (query_pos != std::string::npos) | 64 if (query_pos != std::string::npos) |
| 65 request_path = request_path.substr(0, query_pos); | 65 request_path = request_path.substr(0, query_pos); |
| 66 | 66 |
| 67 base::FilePath file_path(server_root.AppendASCII(request_path)); | 67 for (const auto& server_root : server_roots) { |
| 68 std::string file_contents; | 68 base::FilePath file_path(server_root.AppendASCII(request_path)); |
| 69 if (!base::ReadFileToString(file_path, &file_contents)) | 69 std::string file_contents; |
| 70 return scoped_ptr<HttpResponse>(); | 70 if (!base::ReadFileToString(file_path, &file_contents)) |
| 71 continue; |
| 71 | 72 |
| 72 base::FilePath headers_path( | 73 base::FilePath headers_path( |
| 73 file_path.AddExtension(FILE_PATH_LITERAL("mock-http-headers"))); | 74 file_path.AddExtension(FILE_PATH_LITERAL("mock-http-headers"))); |
| 74 | 75 |
| 75 if (base::PathExists(headers_path)) { | 76 if (base::PathExists(headers_path)) { |
| 76 std::string headers_contents; | 77 std::string headers_contents; |
| 77 if (!base::ReadFileToString(headers_path, &headers_contents)) | 78 if (!base::ReadFileToString(headers_path, &headers_contents)) |
| 78 return scoped_ptr<HttpResponse>(); | 79 return scoped_ptr<HttpResponse>(); |
| 79 | 80 |
| 80 scoped_ptr<CustomHttpResponse> http_response( | 81 scoped_ptr<CustomHttpResponse> http_response( |
| 81 new CustomHttpResponse(headers_contents, file_contents)); | 82 new CustomHttpResponse(headers_contents, file_contents)); |
| 83 return http_response.Pass(); |
| 84 } |
| 85 |
| 86 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 87 http_response->set_code(HTTP_OK); |
| 88 http_response->set_content(file_contents); |
| 82 return http_response.Pass(); | 89 return http_response.Pass(); |
| 83 } | 90 } |
| 84 | 91 return scoped_ptr<HttpResponse>(); |
| 85 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); | |
| 86 http_response->set_code(HTTP_OK); | |
| 87 http_response->set_content(file_contents); | |
| 88 return http_response.Pass(); | |
| 89 } | 92 } |
| 90 | 93 |
| 91 } // namespace | 94 } // namespace |
| 92 | 95 |
| 93 HttpListenSocket::HttpListenSocket(const SocketDescriptor socket_descriptor, | 96 HttpListenSocket::HttpListenSocket(const SocketDescriptor socket_descriptor, |
| 94 StreamListenSocket::Delegate* delegate) | 97 StreamListenSocket::Delegate* delegate) |
| 95 : TCPListenSocket(socket_descriptor, delegate) { | 98 : TCPListenSocket(socket_descriptor, delegate) { |
| 96 DCHECK(thread_checker_.CalledOnValidThread()); | 99 DCHECK(thread_checker_.CalledOnValidThread()); |
| 97 } | 100 } |
| 98 | 101 |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 const std::string& hostname, | 272 const std::string& hostname, |
| 270 const std::string& relative_url) const { | 273 const std::string& relative_url) const { |
| 271 GURL local_url = GetURL(relative_url); | 274 GURL local_url = GetURL(relative_url); |
| 272 GURL::Replacements replace_host; | 275 GURL::Replacements replace_host; |
| 273 replace_host.SetHostStr(hostname); | 276 replace_host.SetHostStr(hostname); |
| 274 return local_url.ReplaceComponents(replace_host); | 277 return local_url.ReplaceComponents(replace_host); |
| 275 } | 278 } |
| 276 | 279 |
| 277 void EmbeddedTestServer::ServeFilesFromDirectory( | 280 void EmbeddedTestServer::ServeFilesFromDirectory( |
| 278 const base::FilePath& directory) { | 281 const base::FilePath& directory) { |
| 279 RegisterRequestHandler(base::Bind(&HandleFileRequest, directory)); | 282 std::vector<base::FilePath> directories; |
| 283 directories.push_back(directory); |
| 284 RegisterRequestHandler(base::Bind(&HandleFileRequest, directories)); |
| 285 } |
| 286 |
| 287 void EmbeddedTestServer::ServeFilesFromDirectories( |
| 288 const std::vector<base::FilePath>& directories) { |
| 289 RegisterRequestHandler(base::Bind(&HandleFileRequest, directories)); |
| 280 } | 290 } |
| 281 | 291 |
| 282 void EmbeddedTestServer::RegisterRequestHandler( | 292 void EmbeddedTestServer::RegisterRequestHandler( |
| 283 const HandleRequestCallback& callback) { | 293 const HandleRequestCallback& callback) { |
| 284 request_handlers_.push_back(callback); | 294 request_handlers_.push_back(callback); |
| 285 } | 295 } |
| 286 | 296 |
| 287 void EmbeddedTestServer::DidAccept( | 297 void EmbeddedTestServer::DidAccept( |
| 288 StreamListenSocket* server, | 298 StreamListenSocket* server, |
| 289 scoped_ptr<StreamListenSocket> connection) { | 299 scoped_ptr<StreamListenSocket> connection) { |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 FROM_HERE, closure, run_loop.QuitClosure())) { | 363 FROM_HERE, closure, run_loop.QuitClosure())) { |
| 354 return false; | 364 return false; |
| 355 } | 365 } |
| 356 run_loop.Run(); | 366 run_loop.Run(); |
| 357 | 367 |
| 358 return true; | 368 return true; |
| 359 } | 369 } |
| 360 | 370 |
| 361 } // namespace test_server | 371 } // namespace test_server |
| 362 } // namespace net | 372 } // namespace net |
| OLD | NEW |