OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/logging.h" | 5 #include "base/logging.h" |
6 #include "base/registry.h" | 6 #include "base/registry.h" |
7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
8 | 8 |
9 #include "chrome_frame/test/test_server.h" | 9 #include "chrome_frame/test/test_server.h" |
10 | 10 |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 // 404's when the connection ends. | 208 // 404's when the connection ends. |
209 Connection* c = FindConnection(sock); | 209 Connection* c = FindConnection(sock); |
210 DCHECK(c); | 210 DCHECK(c); |
211 if (!FindResponse(c->request())) { | 211 if (!FindResponse(c->request())) { |
212 // extremely inefficient, but in one line and not that common... :) | 212 // extremely inefficient, but in one line and not that common... :) |
213 connections_.erase(std::find(connections_.begin(), connections_.end(), c)); | 213 connections_.erase(std::find(connections_.begin(), connections_.end(), c)); |
214 delete c; | 214 delete c; |
215 } | 215 } |
216 } | 216 } |
217 | 217 |
| 218 HTTPTestServer::HTTPTestServer(int port, const char* address) { |
| 219 net::EnsureWinsockInit(); |
| 220 server_ = ListenSocket::Listen(address, port, this); |
| 221 } |
| 222 |
| 223 HTTPTestServer::~HTTPTestServer() { |
| 224 } |
| 225 |
| 226 std::list<scoped_refptr<ConfigurableConnection>>::iterator |
| 227 HTTPTestServer::FindConnection(const ListenSocket* socket) { |
| 228 ConnectionList::iterator it; |
| 229 for (it = connection_list_.begin(); it != connection_list_.end(); ++it) { |
| 230 if ((*it)->socket_ == socket) { |
| 231 break; |
| 232 } |
| 233 } |
| 234 |
| 235 return it; |
| 236 } |
| 237 |
| 238 scoped_refptr<ConfigurableConnection> HTTPTestServer::ConnectionFromSocket( |
| 239 const ListenSocket* socket) { |
| 240 ConnectionList::iterator it = FindConnection(socket); |
| 241 if (it != connection_list_.end()) |
| 242 return *it; |
| 243 return NULL; |
| 244 } |
| 245 |
| 246 void HTTPTestServer::DidAccept(ListenSocket* server, ListenSocket* socket) { |
| 247 connection_list_.push_back(new ConfigurableConnection(socket)); |
| 248 } |
| 249 |
| 250 void HTTPTestServer::DidRead(ListenSocket* socket, const std::string& data) { |
| 251 scoped_refptr<ConfigurableConnection> connection = |
| 252 ConnectionFromSocket(socket); |
| 253 if (connection) { |
| 254 connection->r_.OnDataReceived(data); |
| 255 if (connection->r_.AllContentReceived()) { |
| 256 if (LowerCaseEqualsASCII(connection->r_.method(), "post")) |
| 257 this->Post(connection, connection->r_.path(), connection->r_); |
| 258 else |
| 259 this->Get(connection, connection->r_.path(), connection->r_); |
| 260 } |
| 261 } |
| 262 } |
| 263 |
| 264 void HTTPTestServer::DidClose(ListenSocket* socket) { |
| 265 ConnectionList::iterator it = FindConnection(socket); |
| 266 DCHECK(it != connection_list_.end()); |
| 267 connection_list_.erase(it); |
| 268 } |
| 269 |
| 270 void ConfigurableConnection::SendChunk() { |
| 271 int size = (int)data_.size(); |
| 272 const char* chunk_ptr = data_.c_str() + cur_pos_; |
| 273 int bytes_to_send = std::min(options_.chunk_size_, size - cur_pos_); |
| 274 |
| 275 socket_->Send(chunk_ptr, bytes_to_send); |
| 276 DLOG(INFO) << "Sent(" << cur_pos_ << "," << bytes_to_send |
| 277 << "): " << base::StringPiece(chunk_ptr, bytes_to_send); |
| 278 |
| 279 cur_pos_ += bytes_to_send; |
| 280 if (cur_pos_ < size) { |
| 281 MessageLoop::current()->PostDelayedTask(FROM_HERE, NewRunnableMethod(this, |
| 282 &ConfigurableConnection::SendChunk), options_.timeout_); |
| 283 } else { |
| 284 socket_ = 0; // close the connection. |
| 285 } |
| 286 } |
| 287 |
| 288 void ConfigurableConnection::Send(const std::string& headers, |
| 289 const std::string& content) { |
| 290 SendOptions options(SendOptions::IMMEDIATE, 0, 0); |
| 291 SendWithOptions(headers, content, options); |
| 292 } |
| 293 |
| 294 void ConfigurableConnection::SendWithOptions(const std::string& headers, |
| 295 const std::string& content, |
| 296 const SendOptions& options) { |
| 297 std::string content_length_header; |
| 298 if (!content.empty() && |
| 299 std::string::npos == headers.find("Context-Length:")) { |
| 300 content_length_header = StringPrintf("Content-Length: %u\r\n", |
| 301 content.size()); |
| 302 } |
| 303 |
| 304 // Save the options. |
| 305 options_ = options; |
| 306 |
| 307 if (options_.speed_ == SendOptions::IMMEDIATE) { |
| 308 socket_->Send(headers); |
| 309 socket_->Send(content_length_header, true); |
| 310 socket_->Send(content); |
| 311 socket_ = 0; // close the connection. |
| 312 return; |
| 313 } |
| 314 |
| 315 if (options_.speed_ == SendOptions::IMMEDIATE_HEADERS_DELAYED_CONTENT) { |
| 316 socket_->Send(headers); |
| 317 socket_->Send(content_length_header, true); |
| 318 DLOG(INFO) << "Headers sent: " << headers << content_length_header; |
| 319 data_.append(content); |
| 320 } |
| 321 |
| 322 if (options_.speed_ == SendOptions::DELAYED) { |
| 323 data_ = headers; |
| 324 data_.append(content_length_header); |
| 325 data_.append("\r\n"); |
| 326 } |
| 327 |
| 328 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 329 NewRunnableMethod(this, &ConfigurableConnection::SendChunk), |
| 330 options.timeout_); |
| 331 } |
218 } // namespace test_server | 332 } // namespace test_server |
OLD | NEW |