Chromium Code Reviews| Index: chrome/browser/chromeos/drive/test_servers/http_server.cc |
| diff --git a/chrome/browser/chromeos/drive/test_servers/http_server.cc b/chrome/browser/chromeos/drive/test_servers/http_server.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5d62fdd1b20cbf499fb77b8cc3a0e56a61cfa0ab |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/drive/test_servers/http_server.cc |
| @@ -0,0 +1,258 @@ |
| +// 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_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 HttpServer::CreateForTesting. |server| is a |
| +// pointer to the created server or NULL in case of failure. |
| +static void OnCreatedForTesting(HttpServer** result, |
| + HttpServer* 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<HttpServer> HttpServer::CreateForTesting() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + |
| + HttpServer* result = NULL; |
| + BrowserThread::PostTask( |
|
satorux1
2012/11/13 05:28:34
You want to call OnCreatedForTesting on UI thread,
mtomasz
2012/11/13 12:23:07
Hm. I am concerned that passing a result may be qu
satorux1
2012/11/14 01:42:23
What about doing something like this:
HttpServer*
mtomasz
2012/11/14 03:23:35
This is a good idea. Done sth like this.
|
| + BrowserThread::IO, |
| + FROM_HERE, |
| + base::Bind( |
| + &HttpServer::CreateOnIOThread, |
| + base::Bind(&OnCreatedForTesting, &result))); |
| + |
| + // Wait for the task completion. |
| + MessageLoop::current()->Run(); |
| + return scoped_ptr<HttpServer>(result); |
| +} |
| + |
| +// static |
| +void HttpServer::CreateOnIOThread(const CreateCallback& callback) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + int retries_left = kRetries + 1; |
| + int try_port = kPort; |
| + HttpServer* server = NULL; |
| + |
| + while (retries_left > 0) { |
| + SocketDescriptor socket_descriptor = net::TCPListenSocket::CreateAndBind( |
| + kIp, |
| + try_port); |
| + if (socket_descriptor != net::TCPListenSocket::kInvalidSocket) { |
| + server = new HttpServer(try_port, socket_descriptor); |
| + break; |
| + } |
| + retries_left--; |
|
satorux1
2012/11/13 05:28:34
nit: --retries_left;
post-increment/decrement is
mtomasz
2012/11/13 12:23:07
Done.
|
| + try_port++; |
| + } |
| + |
| + BrowserThread::PostTask(BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(callback, server)); |
| +} |
| + |
| +HttpServer::HttpServer(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_)); |
| +} |
| + |
| +HttpServer::~HttpServer() { |
| + STLDeleteContainerPairSecondPointers(connections_.begin(), |
| + connections_.end()); |
| +} |
| + |
| +void HttpServer::HandleRequest(HttpConnection* connection, |
| + scoped_ptr<HttpRequest> request) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + for (size_t i = 0; i < request_handlers_.size(); ++i) { |
| + scoped_ptr<HttpResponse> response = |
| + request_handlers_[i].Run(request.get()); |
| + if (response.get()) { |
| + connection->SendResponse(response.Pass()); |
| + return; |
| + } |
| + } |
| + |
| + 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 HttpServer::GetBaseURL() { |
| + return base_url_; |
| +} |
| + |
| +void HttpServer::RegisterRequestHandler( |
| + const HandleRequestCallback& callback) { |
| + request_handlers_.push_back(callback); |
| +} |
| + |
| +GURL HttpServer::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 HttpServer::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 HttpServer::RegisterSimpleTextResponse( |
| + const std::string& content, |
| + const std::string& content_type) { |
| + return RegisterTextResponse(GenerateUniqueIdentifier(), |
| + content, |
| + content_type, |
| + SUCCESS); |
| +} |
| + |
| +GURL HttpServer::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 HttpServer::RegisterSimpleFileResponse( |
| + const FilePath& file_path, |
| + const std::string& content_type) { |
| + return RegisterFileResponse( |
| + GenerateUniqueIdentifier() + "/" + file_path.BaseName().value(), |
| + file_path, |
| + content_type, |
| + SUCCESS); |
| +} |
| + |
| +std::string HttpServer::GenerateUniqueIdentifier() { |
| + std::stringstream result_builder; |
| + result_builder << ++last_unique_id_; |
| + return result_builder.str(); |
| +} |
| + |
| +void HttpServer::DidAccept(net::StreamListenSocket* server, |
| + net::StreamListenSocket* connection) { |
| + HttpConnection* http_connection = new HttpConnection( |
| + connection, |
| + base::Bind(&HttpServer::HandleRequest, weak_factory_.GetWeakPtr())); |
| + connections_[connection] = http_connection; |
| +} |
| + |
| +void HttpServer::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(std::string(data, length)); |
| +} |
| + |
| +void HttpServer::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* HttpServer::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 |