Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/gdata/test_servers/http_test_server.h" | |
| 6 | |
| 7 #include "base/stl_util.h" | |
| 8 #include "base/file_util.h" | |
| 9 #include "chrome/browser/chromeos/gdata/test_servers/http_request.h" | |
| 10 #include "chrome/browser/chromeos/gdata/test_servers/http_response.h" | |
| 11 #include "content/public/browser/browser_thread.h" | |
| 12 #include "net/tools/fetch/http_listen_socket.h" | |
| 13 | |
| 14 #include <sstream> | |
| 15 | |
| 16 namespace gdata { | |
| 17 namespace test_servers { | |
| 18 | |
| 19 using content::BrowserThread; | |
| 20 | |
| 21 namespace { | |
| 22 const int kPort = 8040; | |
|
satorux1
2012/10/12 08:29:18
The port may already be in use by another test ins
mtomasz
2012/10/12 11:09:46
It is already implemented. The server tries succee
| |
| 23 const std::string kIp = "127.0.0.1"; | |
| 24 const int kRetries = 10; | |
| 25 } // namespace | |
| 26 | |
| 27 DefaultResponseProvider::DefaultResponseProvider( | |
| 28 const GURL& request_url, | |
| 29 const HttpResponse& default_response) : | |
| 30 request_url_(request_url), | |
| 31 default_response_(default_response) { | |
| 32 } | |
| 33 | |
| 34 DefaultResponseProvider::~DefaultResponseProvider() { | |
| 35 } | |
| 36 | |
| 37 bool DefaultResponseProvider::CanHandleRequest(HttpRequest* request) { | |
| 38 // TODO(mtomasz): We ignore a query string here. Is it fine? | |
| 39 // TODO(mtomasz): We do not verify a host here, is it ok? | |
| 40 // TODO(mtomasz): How about proxy? | |
| 41 return request->url.path() == request_url_.path(); | |
| 42 } | |
| 43 | |
| 44 scoped_ptr<HttpResponse> DefaultResponseProvider::ProcessRequest( | |
| 45 HttpRequest* request) { | |
| 46 return scoped_ptr<HttpResponse>( | |
| 47 new HttpResponse(default_response_)).Pass(); | |
| 48 } | |
| 49 | |
| 50 HttpTestServer::HttpTestServer(bool start_on_create) : port_(-1), | |
| 51 last_unique_id_(0) { | |
| 52 if (start_on_create) { | |
| 53 Start(); | |
| 54 } | |
| 55 } | |
| 56 | |
| 57 HttpTestServer::~HttpTestServer() { | |
| 58 STLDeleteContainerPointers(response_providers_.begin(), | |
| 59 response_providers_.end()); | |
| 60 } | |
| 61 | |
| 62 bool HttpTestServer::Start() { | |
| 63 if (server_.get() != NULL) { | |
| 64 LOG(ERROR) << "Server already started."; | |
| 65 return false; | |
| 66 } | |
| 67 | |
| 68 int port = kPort; | |
| 69 int tries_left = kRetries + 1; | |
| 70 | |
| 71 // Try to create a listening socket. | |
| 72 while (tries_left) { | |
| 73 server_ = HttpServer::CreateServer(kIp, port, this); | |
| 74 if (server_.get() != NULL) { | |
| 75 port_ = port; | |
| 76 std::stringstream base_url_builder; | |
| 77 base_url_builder << "http://" << kIp << ":" << port; | |
| 78 base_url_ = GURL(base_url_builder.str()); | |
| 79 return true; | |
| 80 } | |
| 81 port++; | |
| 82 tries_left--; | |
| 83 } | |
| 84 | |
| 85 LOG(ERROR) << "Unabled to start the server."; | |
| 86 return false; | |
| 87 } | |
| 88 | |
| 89 bool HttpTestServer::HandleRequest(HttpConnection& connection, | |
| 90 scoped_ptr<HttpRequest> request) { | |
| 91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 92 | |
| 93 std::vector<ResponseProviderInterface*>::iterator current_provider = | |
| 94 response_providers_.begin(); | |
| 95 while (current_provider != response_providers_.end()) { | |
| 96 if ((*current_provider)->CanHandleRequest(request.get())) { | |
| 97 scoped_ptr<HttpResponse> response = | |
| 98 (*current_provider)->ProcessRequest(request.get()); | |
| 99 connection.SendResponse(response.Pass()); | |
| 100 return true; | |
| 101 } | |
| 102 current_provider++; | |
| 103 } | |
| 104 | |
| 105 // TODO(mtomasz): Important. Return 404 here instead of just closing | |
| 106 // the socket. | |
| 107 LOG(ERROR) << "Request not handled. Closing the connection."; | |
| 108 return false; | |
| 109 } | |
| 110 | |
| 111 GURL HttpTestServer::GetBaseURL() { | |
| 112 return base_url_; | |
| 113 } | |
| 114 | |
| 115 GURL HttpTestServer::GetErrorURL(RESPONSE_CODE code) { | |
| 116 // TODO(mtomasz): Implement this. | |
| 117 return base_url_; | |
| 118 } | |
| 119 | |
| 120 void HttpTestServer::RegisterResponseProvider( | |
| 121 ResponseProviderInterface& provider) { | |
| 122 response_providers_.push_back(&provider); | |
| 123 } | |
| 124 | |
| 125 GURL HttpTestServer::RegisterDefaultResponse( | |
| 126 const std::string& relative_path, | |
| 127 const HttpResponse& default_response) { | |
| 128 GURL request_url = base_url_.Resolve(relative_path); | |
| 129 response_providers_.push_back(new DefaultResponseProvider(request_url, | |
| 130 default_response)); | |
| 131 return request_url; | |
| 132 } | |
| 133 | |
| 134 GURL HttpTestServer::RegisterTextResponse( | |
| 135 const std::string& relative_path, | |
| 136 const std::string& content, | |
| 137 const std::string& content_type, | |
| 138 const RESPONSE_CODE response_code) { | |
| 139 HttpResponse default_response; | |
| 140 // TODO(mtomasz): Handle content. | |
| 141 default_response.content_type = content_type; | |
| 142 default_response.code = response_code; | |
| 143 | |
| 144 return RegisterDefaultResponse(relative_path, | |
| 145 default_response); | |
| 146 } | |
| 147 | |
| 148 GURL HttpTestServer::RegisterTextResponse( | |
| 149 const std::string& content, | |
| 150 const std::string& content_type) { | |
| 151 return RegisterTextResponse(GenerateUniqueIdentifier(), | |
| 152 content, | |
| 153 content_type, | |
| 154 SUCCESS); | |
| 155 } | |
| 156 | |
| 157 GURL HttpTestServer::RegisterFileResponse( | |
| 158 const std::string& relative_path, | |
| 159 const FilePath& file_path, | |
| 160 const std::string& content_type, | |
| 161 const RESPONSE_CODE response_code) { | |
| 162 HttpResponse default_response; | |
| 163 | |
| 164 // TODO(mtomasz): Handle errors. | |
| 165 file_util::ReadFileToString(file_path, &default_response.content); | |
| 166 | |
| 167 default_response.content_type = content_type; | |
| 168 default_response.code = response_code; | |
| 169 | |
| 170 return RegisterDefaultResponse(relative_path, | |
| 171 default_response); | |
| 172 } | |
| 173 | |
| 174 GURL HttpTestServer::RegisterFileResponse( | |
| 175 const FilePath& file_path, | |
| 176 const std::string& content_type) { | |
| 177 return RegisterFileResponse( | |
| 178 GenerateUniqueIdentifier() + "/" + file_path.BaseName().value(), | |
| 179 file_path, | |
| 180 content_type, | |
| 181 SUCCESS); | |
| 182 } | |
| 183 | |
| 184 std::string HttpTestServer::GenerateUniqueIdentifier() { | |
| 185 std::stringstream result_builder; | |
| 186 result_builder << ++last_unique_id_; | |
| 187 return result_builder.str(); | |
| 188 } | |
| 189 | |
| 190 } // namespace test_servers | |
| 191 } // namespace gdata | |
| OLD | NEW |