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..deeed7d021efcaad503d0f347866170c91623adc 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,27 @@ scoped_ptr<HttpResponse> HandleDefaultRequest(const GURL& url, |
return scoped_ptr<HttpResponse>(new HttpResponse(response)); |
} |
+// Handles |request| by serving a file from under |server_root|. |
+scoped_ptr<HttpResponse> HandleFileRequest(const base::FilePath& server_root, |
+ const HttpRequest& request) { |
+ // This is a test-only server. Ignore I/O thread restrictions. |
+ base::ThreadRestrictions::ScopedAllowIO allow_io; |
+ |
+ // Trim the first byte ('/'). |
+ std::string request_path(request.relative_url.substr(1)); |
+ |
+ std::string file_contents; |
+ if (!file_util::ReadFileToString( |
+ server_root.AppendASCII(request_path), &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 +87,10 @@ EmbeddedTestServer::EmbeddedTestServer( |
EmbeddedTestServer::~EmbeddedTestServer() { |
DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
+ if (Started() && !ShutdownAndWaitUntilComplete()) { |
+ LOG(ERROR) << "EmbeddedTestServer failed to shut down."; |
+ } |
} |
bool EmbeddedTestServer::InitializeAndWaitUntilReady() { |
@@ -159,6 +188,11 @@ GURL EmbeddedTestServer::GetURL(const std::string& relative_url) const { |
return base_url_.Resolve(relative_url); |
} |
+void EmbeddedTestServer::ServeFilesFromDirectory( |
+ const base::FilePath& directory) { |
+ RegisterRequestHandler(base::Bind(&HandleFileRequest, directory)); |
+} |
+ |
void EmbeddedTestServer::RegisterRequestHandler( |
const HandleRequestCallback& callback) { |
request_handlers_.push_back(callback); |