| 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/google_apis/test_server/http_server.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/run_loop.h" | |
| 9 #include "base/stl_util.h" | |
| 10 #include "base/string_util.h" | |
| 11 #include "base/stringprintf.h" | |
| 12 #include "chrome/browser/google_apis/test_server/http_connection.h" | |
| 13 #include "chrome/browser/google_apis/test_server/http_request.h" | |
| 14 #include "chrome/browser/google_apis/test_server/http_response.h" | |
| 15 #include "net/tools/fetch/http_listen_socket.h" | |
| 16 | |
| 17 namespace google_apis { | |
| 18 namespace test_server { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 const int kPort = 8040; | |
| 23 const char kIp[] = "127.0.0.1"; | |
| 24 const int kRetries = 10; | |
| 25 | |
| 26 // Callback to handle requests with default predefined response for requests | |
| 27 // matching the address |url|. | |
| 28 scoped_ptr<HttpResponse> HandleDefaultRequest(const GURL& url, | |
| 29 const HttpResponse& response, | |
| 30 const HttpRequest& request) { | |
| 31 const GURL request_url = url.Resolve(request.relative_url); | |
| 32 if (url.path() != request_url.path()) | |
| 33 return scoped_ptr<HttpResponse>(NULL); | |
| 34 return scoped_ptr<HttpResponse>(new HttpResponse(response)); | |
| 35 } | |
| 36 | |
| 37 } // namespace | |
| 38 | |
| 39 HttpListenSocket::HttpListenSocket(const SocketDescriptor socket_descriptor, | |
| 40 net::StreamListenSocket::Delegate* delegate) | |
| 41 : net::TCPListenSocket(socket_descriptor, delegate) { | |
| 42 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 43 } | |
| 44 | |
| 45 void HttpListenSocket::Listen() { | |
| 46 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 47 net::TCPListenSocket::Listen(); | |
| 48 } | |
| 49 | |
| 50 HttpListenSocket::~HttpListenSocket() { | |
| 51 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 52 } | |
| 53 | |
| 54 HttpServer::HttpServer( | |
| 55 const scoped_refptr<base::SingleThreadTaskRunner>& io_thread) | |
| 56 : io_thread_(io_thread), | |
| 57 port_(-1), | |
| 58 weak_factory_(this) { | |
| 59 DCHECK(io_thread_); | |
| 60 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 61 } | |
| 62 | |
| 63 HttpServer::~HttpServer() { | |
| 64 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 65 } | |
| 66 | |
| 67 bool HttpServer::InitializeAndWaitUntilReady() { | |
| 68 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 69 | |
| 70 base::RunLoop run_loop; | |
| 71 if (!io_thread_->PostTaskAndReply( | |
| 72 FROM_HERE, | |
| 73 base::Bind(&HttpServer::InitializeOnIOThread, base::Unretained(this)), | |
| 74 run_loop.QuitClosure())) { | |
| 75 return false; | |
| 76 } | |
| 77 run_loop.Run(); | |
| 78 | |
| 79 return Started(); | |
| 80 } | |
| 81 | |
| 82 bool HttpServer::ShutdownAndWaitUntilComplete() { | |
| 83 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 84 | |
| 85 base::RunLoop run_loop; | |
| 86 if (!io_thread_->PostTaskAndReply( | |
| 87 FROM_HERE, | |
| 88 base::Bind(&HttpServer::ShutdownOnIOThread, base::Unretained(this)), | |
| 89 run_loop.QuitClosure())) { | |
| 90 return false; | |
| 91 } | |
| 92 run_loop.Run(); | |
| 93 | |
| 94 return true; | |
| 95 } | |
| 96 | |
| 97 void HttpServer::InitializeOnIOThread() { | |
| 98 DCHECK(io_thread_->BelongsToCurrentThread()); | |
| 99 DCHECK(!Started()); | |
| 100 | |
| 101 int retries_left = kRetries + 1; | |
| 102 int try_port = kPort; | |
| 103 | |
| 104 while (retries_left > 0) { | |
| 105 SocketDescriptor socket_descriptor = net::TCPListenSocket::CreateAndBind( | |
| 106 kIp, | |
| 107 try_port); | |
| 108 if (socket_descriptor != net::TCPListenSocket::kInvalidSocket) { | |
| 109 listen_socket_ = new HttpListenSocket(socket_descriptor, this); | |
| 110 listen_socket_->Listen(); | |
| 111 base_url_ = GURL(base::StringPrintf("http://%s:%d", kIp, try_port)); | |
| 112 port_ = try_port; | |
| 113 break; | |
| 114 } | |
| 115 retries_left--; | |
| 116 try_port++; | |
| 117 } | |
| 118 } | |
| 119 | |
| 120 void HttpServer::ShutdownOnIOThread() { | |
| 121 DCHECK(io_thread_->BelongsToCurrentThread()); | |
| 122 | |
| 123 listen_socket_ = NULL; // Release the listen socket. | |
| 124 STLDeleteContainerPairSecondPointers(connections_.begin(), | |
| 125 connections_.end()); | |
| 126 connections_.clear(); | |
| 127 } | |
| 128 | |
| 129 void HttpServer::HandleRequest(HttpConnection* connection, | |
| 130 scoped_ptr<HttpRequest> request) { | |
| 131 DCHECK(io_thread_->BelongsToCurrentThread()); | |
| 132 | |
| 133 for (size_t i = 0; i < request_handlers_.size(); ++i) { | |
| 134 scoped_ptr<HttpResponse> response = | |
| 135 request_handlers_[i].Run(*request.get()); | |
| 136 if (response.get()) { | |
| 137 connection->SendResponse(response.Pass()); | |
| 138 return; | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 LOG(WARNING) << "Request not handled. Returning 404: " | |
| 143 << request->relative_url; | |
| 144 scoped_ptr<HttpResponse> not_found_response(new HttpResponse()); | |
| 145 not_found_response->set_code(NOT_FOUND); | |
| 146 connection->SendResponse(not_found_response.Pass()); | |
| 147 | |
| 148 // Drop the connection, since we do not support multiple requests per | |
| 149 // connection. | |
| 150 connections_.erase(connection->socket_.get()); | |
| 151 delete connection; | |
| 152 } | |
| 153 | |
| 154 GURL HttpServer::GetURL(const std::string& relative_url) const { | |
| 155 DCHECK(StartsWithASCII(relative_url, "/", true /* case_sensitive */)) | |
| 156 << relative_url; | |
| 157 return base_url_.Resolve(relative_url); | |
| 158 } | |
| 159 | |
| 160 void HttpServer::RegisterRequestHandler( | |
| 161 const HandleRequestCallback& callback) { | |
| 162 request_handlers_.push_back(callback); | |
| 163 } | |
| 164 | |
| 165 void HttpServer::DidAccept(net::StreamListenSocket* server, | |
| 166 net::StreamListenSocket* connection) { | |
| 167 DCHECK(io_thread_->BelongsToCurrentThread()); | |
| 168 | |
| 169 HttpConnection* http_connection = new HttpConnection( | |
| 170 connection, | |
| 171 base::Bind(&HttpServer::HandleRequest, weak_factory_.GetWeakPtr())); | |
| 172 connections_[connection] = http_connection; | |
| 173 } | |
| 174 | |
| 175 void HttpServer::DidRead(net::StreamListenSocket* connection, | |
| 176 const char* data, | |
| 177 int length) { | |
| 178 DCHECK(io_thread_->BelongsToCurrentThread()); | |
| 179 | |
| 180 HttpConnection* http_connection = FindConnection(connection); | |
| 181 if (http_connection == NULL) { | |
| 182 LOG(WARNING) << "Unknown connection."; | |
| 183 return; | |
| 184 } | |
| 185 http_connection->ReceiveData(std::string(data, length)); | |
| 186 } | |
| 187 | |
| 188 void HttpServer::DidClose(net::StreamListenSocket* connection) { | |
| 189 DCHECK(io_thread_->BelongsToCurrentThread()); | |
| 190 | |
| 191 HttpConnection* http_connection = FindConnection(connection); | |
| 192 if (http_connection == NULL) { | |
| 193 LOG(WARNING) << "Unknown connection."; | |
| 194 return; | |
| 195 } | |
| 196 delete http_connection; | |
| 197 connections_.erase(connection); | |
| 198 } | |
| 199 | |
| 200 HttpConnection* HttpServer::FindConnection( | |
| 201 net::StreamListenSocket* socket) { | |
| 202 DCHECK(io_thread_->BelongsToCurrentThread()); | |
| 203 | |
| 204 std::map<net::StreamListenSocket*, HttpConnection*>::iterator it = | |
| 205 connections_.find(socket); | |
| 206 if (it == connections_.end()) { | |
| 207 return NULL; | |
| 208 } | |
| 209 return it->second; | |
| 210 } | |
| 211 | |
| 212 } // namespace test_server | |
| 213 } // namespace google_apis | |
| OLD | NEW |