Chromium Code Reviews| 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..ff81696323de7d5fe4f7932b7b6df608518a6fac |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/drive/test_servers/http_test_server.cc |
| @@ -0,0 +1,248 @@ |
| +// 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 |
| + |
| +// 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(); |
|
satorux1
2012/11/12 06:34:15
Can you do this instead?
content::RunAllPendingIn
mtomasz
2012/11/14 03:23:35
Done.
|
| + 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) { |
|
satorux1
2012/11/12 06:34:15
retries_left > 0
mtomasz
2012/11/12 12:17:44
Done.
|
| + 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)) { |
|
satorux1
2012/11/12 06:34:15
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::I
mtomasz
2012/11/13 12:23:07
Done.
|
| + listen_socket_->Listen(); |
| + base_url_ = GURL(base::StringPrintf("http://%s:%d", kIp, port_)); |
| +} |
| + |
| +HttpTestServer::~HttpTestServer() { |
|
satorux1
2012/11/12 06:34:15
on what thread this has to be deleted? I guess it'
mtomasz
2012/11/14 03:23:35
UI. Done.
|
| + 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()) { |
|
satorux1
2012/11/12 06:34:15
Why not a for loop?
for (size_t i = 0; i < reques
mtomasz
2012/11/12 12:17:44
Done. Do we in general prefer simple loops over it
|
| + 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( |
|
satorux1
2012/11/12 06:34:15
Function overloading like this is not allowed per
mtomasz
2012/11/12 12:17:44
Done.
|
| + 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( |
|
satorux1
2012/11/12 06:34:15
ditto.
mtomasz
2012/11/12 12:17:44
Done.
|
| + 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 |