| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome_frame/test/test_server.h" | 5 #include "chrome_frame/test/test_server.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 #include <objbase.h> | 8 #include <objbase.h> |
| 9 #include <urlmon.h> | 9 #include <urlmon.h> |
| 10 | 10 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 const char kDefaultContentType[] = "text/html; charset=UTF-8"; | 31 const char kDefaultContentType[] = "text/html; charset=UTF-8"; |
| 32 | 32 |
| 33 void Request::ParseHeaders(const std::string& headers) { | 33 void Request::ParseHeaders(const std::string& headers) { |
| 34 DCHECK(method_.length() == 0); | 34 DCHECK(method_.length() == 0); |
| 35 | 35 |
| 36 size_t pos = headers.find("\r\n"); | 36 size_t pos = headers.find("\r\n"); |
| 37 DCHECK(pos != std::string::npos); | 37 DCHECK(pos != std::string::npos); |
| 38 if (pos != std::string::npos) { | 38 if (pos != std::string::npos) { |
| 39 headers_ = headers.substr(pos + 2); | 39 headers_ = headers.substr(pos + 2); |
| 40 | 40 |
| 41 base::StringTokenizer tokenizer(headers.begin(), headers.begin() + pos, " ")
; | 41 base::StringTokenizer tokenizer( |
| 42 headers.begin(), headers.begin() + pos, " "); |
| 42 std::string* parse[] = { &method_, &path_, &version_ }; | 43 std::string* parse[] = { &method_, &path_, &version_ }; |
| 43 int field = 0; | 44 int field = 0; |
| 44 while (tokenizer.GetNext() && field < arraysize(parse)) { | 45 while (tokenizer.GetNext() && field < arraysize(parse)) { |
| 45 parse[field++]->assign(tokenizer.token_begin(), | 46 parse[field++]->assign(tokenizer.token_begin(), |
| 46 tokenizer.token_end()); | 47 tokenizer.token_end()); |
| 47 } | 48 } |
| 48 } | 49 } |
| 49 | 50 |
| 50 // Check for content-length in case we're being sent some data. | 51 // Check for content-length in case we're being sent some data. |
| 51 net::HttpUtil::HeadersIterator it(headers_.begin(), headers_.end(), | 52 net::HttpUtil::HeadersIterator it(headers_.begin(), headers_.end(), |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 } | 147 } |
| 147 | 148 |
| 148 SimpleWebServer::~SimpleWebServer() { | 149 SimpleWebServer::~SimpleWebServer() { |
| 149 ConnectionList::const_iterator it; | 150 ConnectionList::const_iterator it; |
| 150 for (it = connections_.begin(); it != connections_.end(); ++it) | 151 for (it = connections_.begin(); it != connections_.end(); ++it) |
| 151 delete (*it); | 152 delete (*it); |
| 152 connections_.clear(); | 153 connections_.clear(); |
| 153 } | 154 } |
| 154 | 155 |
| 155 void SimpleWebServer::Construct(const std::string& address, int port) { | 156 void SimpleWebServer::Construct(const std::string& address, int port) { |
| 156 CHECK(MessageLoop::current()) << "SimpleWebServer requires a message loop"; | 157 CHECK(base::MessageLoop::current()) |
| 158 << "SimpleWebServer requires a message loop"; |
| 157 net::EnsureWinsockInit(); | 159 net::EnsureWinsockInit(); |
| 158 AddResponse(&quit_); | 160 AddResponse(&quit_); |
| 159 host_ = address; | 161 host_ = address; |
| 160 server_ = net::TCPListenSocket::CreateAndListen(address, port, this); | 162 server_ = net::TCPListenSocket::CreateAndListen(address, port, this); |
| 161 LOG_IF(DFATAL, !server_.get()) | 163 LOG_IF(DFATAL, !server_.get()) |
| 162 << "Failed to create listener socket at " << address << ":" << port; | 164 << "Failed to create listener socket at " << address << ":" << port; |
| 163 } | 165 } |
| 164 | 166 |
| 165 void SimpleWebServer::AddResponse(Response* response) { | 167 void SimpleWebServer::AddResponse(Response* response) { |
| 166 responses_.push_back(response); | 168 responses_.push_back(response); |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 349 int size = (int)data_.size(); | 351 int size = (int)data_.size(); |
| 350 const char* chunk_ptr = data_.c_str() + cur_pos_; | 352 const char* chunk_ptr = data_.c_str() + cur_pos_; |
| 351 int bytes_to_send = std::min(options_.chunk_size_, size - cur_pos_); | 353 int bytes_to_send = std::min(options_.chunk_size_, size - cur_pos_); |
| 352 | 354 |
| 353 socket_->Send(chunk_ptr, bytes_to_send); | 355 socket_->Send(chunk_ptr, bytes_to_send); |
| 354 VLOG(1) << "Sent(" << cur_pos_ << "," << bytes_to_send << "): " | 356 VLOG(1) << "Sent(" << cur_pos_ << "," << bytes_to_send << "): " |
| 355 << base::StringPiece(chunk_ptr, bytes_to_send); | 357 << base::StringPiece(chunk_ptr, bytes_to_send); |
| 356 | 358 |
| 357 cur_pos_ += bytes_to_send; | 359 cur_pos_ += bytes_to_send; |
| 358 if (cur_pos_ < size) { | 360 if (cur_pos_ < size) { |
| 359 MessageLoop::current()->PostDelayedTask( | 361 base::MessageLoop::current()->PostDelayedTask( |
| 360 FROM_HERE, base::Bind(&ConfigurableConnection::SendChunk, this), | 362 FROM_HERE, base::Bind(&ConfigurableConnection::SendChunk, this), |
| 361 base::TimeDelta::FromMilliseconds(options_.timeout_)); | 363 base::TimeDelta::FromMilliseconds(options_.timeout_)); |
| 362 } else { | 364 } else { |
| 363 socket_ = 0; // close the connection. | 365 socket_ = 0; // close the connection. |
| 364 } | 366 } |
| 365 } | 367 } |
| 366 | 368 |
| 367 void ConfigurableConnection::Close() { | 369 void ConfigurableConnection::Close() { |
| 368 socket_ = NULL; | 370 socket_ = NULL; |
| 369 } | 371 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 386 | 388 |
| 387 // Save the options. | 389 // Save the options. |
| 388 options_ = options; | 390 options_ = options; |
| 389 | 391 |
| 390 if (options_.speed_ == SendOptions::IMMEDIATE) { | 392 if (options_.speed_ == SendOptions::IMMEDIATE) { |
| 391 socket_->Send(headers); | 393 socket_->Send(headers); |
| 392 socket_->Send(content_length_header, true); | 394 socket_->Send(content_length_header, true); |
| 393 socket_->Send(content); | 395 socket_->Send(content); |
| 394 // Post a task to close the socket since StreamListenSocket doesn't like | 396 // Post a task to close the socket since StreamListenSocket doesn't like |
| 395 // instances to go away from within its callbacks. | 397 // instances to go away from within its callbacks. |
| 396 MessageLoop::current()->PostTask( | 398 base::MessageLoop::current()->PostTask( |
| 397 FROM_HERE, base::Bind(&ConfigurableConnection::Close, this)); | 399 FROM_HERE, base::Bind(&ConfigurableConnection::Close, this)); |
| 398 | 400 |
| 399 return; | 401 return; |
| 400 } | 402 } |
| 401 | 403 |
| 402 if (options_.speed_ == SendOptions::IMMEDIATE_HEADERS_DELAYED_CONTENT) { | 404 if (options_.speed_ == SendOptions::IMMEDIATE_HEADERS_DELAYED_CONTENT) { |
| 403 socket_->Send(headers); | 405 socket_->Send(headers); |
| 404 socket_->Send(content_length_header, true); | 406 socket_->Send(content_length_header, true); |
| 405 VLOG(1) << "Headers sent: " << headers << content_length_header; | 407 VLOG(1) << "Headers sent: " << headers << content_length_header; |
| 406 data_.append(content); | 408 data_.append(content); |
| 407 } | 409 } |
| 408 | 410 |
| 409 if (options_.speed_ == SendOptions::DELAYED) { | 411 if (options_.speed_ == SendOptions::DELAYED) { |
| 410 data_ = headers; | 412 data_ = headers; |
| 411 data_.append(content_length_header); | 413 data_.append(content_length_header); |
| 412 data_.append("\r\n"); | 414 data_.append("\r\n"); |
| 413 } | 415 } |
| 414 | 416 |
| 415 MessageLoop::current()->PostDelayedTask( | 417 base::MessageLoop::current()->PostDelayedTask( |
| 416 FROM_HERE, base::Bind(&ConfigurableConnection::SendChunk, this), | 418 FROM_HERE, base::Bind(&ConfigurableConnection::SendChunk, this), |
| 417 base::TimeDelta::FromMilliseconds(options.timeout_)); | 419 base::TimeDelta::FromMilliseconds(options.timeout_)); |
| 418 } | 420 } |
| 419 | 421 |
| 420 } // namespace test_server | 422 } // namespace test_server |
| OLD | NEW |