Index: chrome/browser/chromeos/drive/test_servers/http_test_server.cc |
diff --git a/chrome/browser/chromeos/drive/test_servers/http_test_server.cc b/chrome/browser/chromeos/drive/test_servers/http_test_server.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c361b70d60ca4932f1a02cb9cc8b451b07e29d2e |
--- /dev/null |
+++ b/chrome/browser/chromeos/drive/test_servers/http_test_server.cc |
@@ -0,0 +1,260 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/chromeos/drive/test_servers/http_test_server.h" |
+ |
+#include "base/bind.h" |
+#include "base/stl_util.h" |
+#include "base/file_util.h" |
+#include "base/stringprintf.h" |
+#include "chrome/browser/chromeos/drive/test_servers/http_request.h" |
+#include "chrome/browser/chromeos/drive/test_servers/http_response.h" |
+#include "chrome/browser/google_apis/gdata_test_util.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "net/tools/fetch/http_listen_socket.h" |
+ |
+#include <sstream> |
+ |
+namespace drive { |
+namespace test_servers { |
+ |
+using content::BrowserThread; |
+ |
+namespace { |
+ |
+const int kPort = 8040; |
+const char kIp[] = "127.0.0.1"; |
+const int kRetries = 10; |
+ |
+// Callback about success or failure in creating a server. |result| is a pointer |
+// to result passed back to HttpTestServer::CreateForTesting. |server| is a |
+// pointer to the created server or NULL in case of failure. |
+static void OnCreatedForTesting(HttpTestServer** result, |
+ HttpTestServer* server) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ *result = server; |
+ MessageLoop::current()->Quit(); |
+} |
+ |
+// Callback to handle requests with default predefined response for requests |
+// matching the address |url|. |
+scoped_ptr<HttpResponse> HandleDefaultRequest(const GURL& url, |
+ const HttpResponse& response, |
+ const HttpRequest* request) { |
+ if (url.path() != request->uri.path()) |
+ return scoped_ptr<HttpResponse>(NULL); |
+ return scoped_ptr<HttpResponse>(new HttpResponse(response)); |
+} |
+ |
+} // namespace |
+ |
+HttpListenSocket::HttpListenSocket(const SocketDescriptor socket_descriptor, |
+ net::StreamListenSocket::Delegate* delegate) |
+ : net::TCPListenSocket(socket_descriptor, delegate) { |
+} |
+ |
+void HttpListenSocket::Listen() { |
+ net::TCPListenSocket::Listen(); |
+} |
+ |
+HttpListenSocket::~HttpListenSocket() { |
+} |
+ |
+// static |
+scoped_ptr<HttpTestServer> HttpTestServer::CreateForTesting() { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ |
+ HttpTestServer* result = NULL; |
+ BrowserThread::PostTask( |
+ BrowserThread::IO, |
+ FROM_HERE, |
+ base::Bind( |
+ &HttpTestServer::CreateOnIOThread, |
+ base::Bind(&OnCreatedForTesting, &result))); |
+ |
+ // Wait for the task completion. |
+ MessageLoop::current()->Run(); |
+ return scoped_ptr<HttpTestServer>(result); |
+} |
+ |
+// static |
+void HttpTestServer::CreateOnIOThread(const CreateCallback& callback) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ |
+ int retries_left = kRetries + 1; |
+ int try_port = kPort; |
+ HttpTestServer* server = NULL; |
+ |
+ while (retries_left) { |
+ SocketDescriptor socket_descriptor = net::TCPListenSocket::CreateAndBind( |
+ kIp, |
+ try_port); |
+ if (socket_descriptor != net::TCPListenSocket::kInvalidSocket) { |
+ server = new HttpTestServer(try_port, socket_descriptor); |
+ break; |
+ } |
+ retries_left--; |
+ try_port++; |
+ } |
+ |
+ BrowserThread::PostTask(BrowserThread::UI, |
+ FROM_HERE, |
+ base::Bind(callback, server)); |
+} |
+ |
+HttpTestServer::HttpTestServer(int port, |
+ const SocketDescriptor& socket_descriptor) |
+ : listen_socket_(new HttpListenSocket( |
+ socket_descriptor, |
+ ALLOW_THIS_IN_INITIALIZER_LIST(this))), |
+ port_(port), |
+ last_unique_id_(0), |
+ weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
+ listen_socket_->Listen(); |
+ base_url_ = GURL(base::StringPrintf("http://%s:%d", kIp, port_)); |
+} |
+ |
+HttpTestServer::~HttpTestServer() { |
+ STLDeleteContainerPairSecondPointers(connections_.begin(), |
+ connections_.end()); |
+} |
+ |
+void HttpTestServer::HandleRequest(HttpConnection* connection, |
+ scoped_ptr<HttpRequest> request) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ |
+ std::vector<HandleRequestCallback>::iterator current_handler = |
+ request_handlers_.begin(); |
+ while (current_handler != request_handlers_.end()) { |
+ scoped_ptr<HttpResponse> response = current_handler->Run(request.get()); |
+ if (response.get()) { |
+ connection->SendResponse(response.Pass()); |
+ return; |
+ } |
+ current_handler++; |
+ } |
+ |
+ LOG(WARNING) << "Request not handled. Returning 404."; |
+ scoped_ptr<HttpResponse> not_found_response(new HttpResponse()); |
+ not_found_response->code = NOT_FOUND; |
+ connection->SendResponse(not_found_response.Pass()); |
+} |
+ |
+GURL HttpTestServer::GetBaseURL() { |
+ return base_url_; |
+} |
+ |
+void HttpTestServer::RegisterRequestHandler( |
+ const HandleRequestCallback& callback) { |
+ request_handlers_.push_back(callback); |
+} |
+ |
+GURL HttpTestServer::RegisterDefaultResponse( |
+ const std::string& relative_path, |
+ const HttpResponse& default_response) { |
+ GURL request_url = base_url_.Resolve(relative_path); |
+ const HandleRequestCallback callback = |
+ base::Bind(&HandleDefaultRequest, |
+ request_url, |
+ default_response); |
+ request_handlers_.push_back(callback); |
+ |
+ return request_url; |
+} |
+ |
+GURL HttpTestServer::RegisterTextResponse( |
+ const std::string& relative_path, |
+ const std::string& content, |
+ const std::string& content_type, |
+ const ResponseCode response_code) { |
+ HttpResponse default_response; |
+ default_response.content = content; |
+ default_response.content_type = content_type; |
+ default_response.code = response_code; |
+ |
+ return RegisterDefaultResponse(relative_path, default_response); |
+} |
+ |
+GURL HttpTestServer::RegisterTextResponse( |
+ const std::string& content, |
+ const std::string& content_type) { |
+ return RegisterTextResponse(GenerateUniqueIdentifier(), |
+ content, |
+ content_type, |
+ SUCCESS); |
+} |
+ |
+GURL HttpTestServer::RegisterFileResponse( |
+ const std::string& relative_path, |
+ const FilePath& file_path, |
+ const std::string& content_type, |
+ const ResponseCode response_code) { |
+ HttpResponse default_response; |
+ |
+ DCHECK(file_util::ReadFileToString(file_path, &default_response.content)) << |
+ "Failed to open the file: " << file_path.value(); |
+ |
+ default_response.content_type = content_type; |
+ default_response.code = response_code; |
+ |
+ return RegisterDefaultResponse(relative_path, default_response); |
+} |
+ |
+GURL HttpTestServer::RegisterFileResponse( |
+ const FilePath& file_path, |
+ const std::string& content_type) { |
+ return RegisterFileResponse( |
+ GenerateUniqueIdentifier() + "/" + file_path.BaseName().value(), |
+ file_path, |
+ content_type, |
+ SUCCESS); |
+} |
+ |
+std::string HttpTestServer::GenerateUniqueIdentifier() { |
+ std::stringstream result_builder; |
+ result_builder << ++last_unique_id_; |
+ return result_builder.str(); |
+} |
+ |
+void HttpTestServer::DidAccept(net::StreamListenSocket* server, |
+ net::StreamListenSocket* connection) { |
+ HttpConnection* http_connection = new HttpConnection( |
+ connection, |
+ base::Bind(&HttpTestServer::HandleRequest, weak_factory_.GetWeakPtr())); |
+ connections_[connection] = http_connection; |
+} |
+ |
+void HttpTestServer::DidRead(net::StreamListenSocket* connection, |
+ const char* data, |
+ int length) { |
+ HttpConnection* http_connection = FindConnection(connection); |
+ if (http_connection == NULL) { |
+ LOG(WARNING) << "Unknown connection."; |
+ return; |
+ } |
+ http_connection->ReceiveData(data, length); |
+} |
+ |
+void HttpTestServer::DidClose(net::StreamListenSocket* connection) { |
+ HttpConnection* http_connection = FindConnection(connection); |
+ if (http_connection == NULL) { |
+ LOG(WARNING) << "Unknown connection."; |
+ return; |
+ } |
+ delete http_connection; |
+ connections_.erase(connection); |
+} |
+ |
+HttpConnection* HttpTestServer::FindConnection( |
+ net::StreamListenSocket* socket) { |
+ std::map<net::StreamListenSocket*, HttpConnection*>::iterator it = |
+ connections_.find(socket); |
+ if (it == connections_.end()) { |
+ return NULL; |
+ } |
+ return it->second; |
+} |
+ |
+} // namespace test_servers |
+} // namespace drive |