Index: net/test/embedded_test_server/embedded_test_server.cc |
diff --git a/net/test/embedded_test_server/embedded_test_server.cc b/net/test/embedded_test_server/embedded_test_server.cc |
index c42ff639f241cfe176416f9954fef2976030ff12..d21127cb7e92ed186441bed2d0a6d883aabca4d5 100644 |
--- a/net/test/embedded_test_server/embedded_test_server.cc |
+++ b/net/test/embedded_test_server/embedded_test_server.cc |
@@ -5,10 +5,14 @@ |
#include "net/test/embedded_test_server/embedded_test_server.h" |
#include "base/bind.h" |
+#include "base/files/file_path.h" |
+#include "base/file_util.h" |
+#include "base/path_service.h" |
#include "base/run_loop.h" |
#include "base/stl_util.h" |
#include "base/string_util.h" |
#include "base/stringprintf.h" |
+#include "base/threading/thread_restrictions.h" |
#include "net/test/embedded_test_server/http_connection.h" |
#include "net/test/embedded_test_server/http_request.h" |
#include "net/test/embedded_test_server/http_response.h" |
@@ -34,6 +38,24 @@ scoped_ptr<HttpResponse> HandleDefaultRequest(const GURL& url, |
return scoped_ptr<HttpResponse>(new HttpResponse(response)); |
} |
+scoped_ptr<HttpResponse> HandleFileRequest(const base::FilePath& file_path, |
satorux1
2013/05/16 04:57:59
function comment is missing.
satorux1
2013/05/16 04:57:59
file_path -> directory_path ?
|
+ const HttpRequest& request) { |
+ // This is a test-only server. Ignore I/O thread restrictions. |
+ base::ThreadRestrictions::ScopedAllowIO allow_io; |
+ |
+ std::string file_contents; |
satorux1
2013/05/16 04:57:59
Maybe:
// Trim the first byte ('/').
|
+ if (!file_util::ReadFileToString( |
+ file_path.AppendASCII(request.relative_url.substr(1)), |
+ &file_contents)) { |
+ return scoped_ptr<HttpResponse>(NULL); |
+ } |
+ |
+ scoped_ptr<HttpResponse> http_response(new HttpResponse); |
+ http_response->set_code(net::test_server::SUCCESS); |
+ http_response->set_content(file_contents); |
+ return http_response.Pass(); |
+} |
+ |
} // namespace |
HttpListenSocket::HttpListenSocket(const SocketDescriptor socket_descriptor, |
@@ -62,6 +84,10 @@ EmbeddedTestServer::EmbeddedTestServer( |
EmbeddedTestServer::~EmbeddedTestServer() { |
DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
+ if (Started() && !ShutdownAndWaitUntilComplete()) { |
+ LOG(ERROR) << "EmbeddedTestServer failed to shut down."; |
+ } |
} |
bool EmbeddedTestServer::InitializeAndWaitUntilReady() { |
@@ -159,6 +185,16 @@ GURL EmbeddedTestServer::GetURL(const std::string& relative_url) const { |
return base_url_.Resolve(relative_url); |
} |
+bool EmbeddedTestServer::ServeFilesFromDirectory( |
+ int root_key, const base::FilePath& directory) { |
+ base::FilePath root_path; |
+ if (!PathService::Get(root_key, &root_path)) |
+ return false; |
+ RegisterRequestHandler(base::Bind(&HandleFileRequest, |
+ root_path.Append(directory))); |
+ return true; |
+} |
+ |
void EmbeddedTestServer::RegisterRequestHandler( |
const HandleRequestCallback& callback) { |
request_handlers_.push_back(callback); |