| 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 #include "base/utf_string_conversions.h" |
| 8 | 9 |
| 9 #include "chrome_frame/test/test_server.h" | 10 #include "chrome_frame/test/test_server.h" |
| 10 | 11 |
| 11 #include "net/base/winsock_init.h" | 12 #include "net/base/winsock_init.h" |
| 12 #include "net/http/http_util.h" | 13 #include "net/http/http_util.h" |
| 13 | 14 |
| 14 namespace test_server { | 15 namespace test_server { |
| 15 const char kDefaultHeaderTemplate[] = | 16 const char kDefaultHeaderTemplate[] = |
| 16 "HTTP/1.1 %hs\r\n" | 17 "HTTP/1.1 %hs\r\n" |
| 17 "Connection: close\r\n" | 18 "Connection: close\r\n" |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 // 404's when the connection ends. | 209 // 404's when the connection ends. |
| 209 Connection* c = FindConnection(sock); | 210 Connection* c = FindConnection(sock); |
| 210 DCHECK(c); | 211 DCHECK(c); |
| 211 if (!FindResponse(c->request())) { | 212 if (!FindResponse(c->request())) { |
| 212 // extremely inefficient, but in one line and not that common... :) | 213 // extremely inefficient, but in one line and not that common... :) |
| 213 connections_.erase(std::find(connections_.begin(), connections_.end(), c)); | 214 connections_.erase(std::find(connections_.begin(), connections_.end(), c)); |
| 214 delete c; | 215 delete c; |
| 215 } | 216 } |
| 216 } | 217 } |
| 217 | 218 |
| 218 HTTPTestServer::HTTPTestServer(int port, const char* address) { | 219 HTTPTestServer::HTTPTestServer(int port, const std::wstring& address, |
| 220 FilePath root_dir) |
| 221 : port_(port), address_(address), root_dir_(root_dir) { |
| 219 net::EnsureWinsockInit(); | 222 net::EnsureWinsockInit(); |
| 220 server_ = ListenSocket::Listen(address, port, this); | 223 server_ = ListenSocket::Listen(WideToUTF8(address), port, this); |
| 221 } | 224 } |
| 222 | 225 |
| 223 HTTPTestServer::~HTTPTestServer() { | 226 HTTPTestServer::~HTTPTestServer() { |
| 224 } | 227 } |
| 225 | 228 |
| 226 std::list<scoped_refptr<ConfigurableConnection>>::iterator | 229 std::list<scoped_refptr<ConfigurableConnection>>::iterator |
| 227 HTTPTestServer::FindConnection(const ListenSocket* socket) { | 230 HTTPTestServer::FindConnection(const ListenSocket* socket) { |
| 228 ConnectionList::iterator it; | 231 ConnectionList::iterator it; |
| 229 for (it = connection_list_.begin(); it != connection_list_.end(); ++it) { | 232 for (it = connection_list_.begin(); it != connection_list_.end(); ++it) { |
| 230 if ((*it)->socket_ == socket) { | 233 if ((*it)->socket_ == socket) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 246 void HTTPTestServer::DidAccept(ListenSocket* server, ListenSocket* socket) { | 249 void HTTPTestServer::DidAccept(ListenSocket* server, ListenSocket* socket) { |
| 247 connection_list_.push_back(new ConfigurableConnection(socket)); | 250 connection_list_.push_back(new ConfigurableConnection(socket)); |
| 248 } | 251 } |
| 249 | 252 |
| 250 void HTTPTestServer::DidRead(ListenSocket* socket, const std::string& data) { | 253 void HTTPTestServer::DidRead(ListenSocket* socket, const std::string& data) { |
| 251 scoped_refptr<ConfigurableConnection> connection = | 254 scoped_refptr<ConfigurableConnection> connection = |
| 252 ConnectionFromSocket(socket); | 255 ConnectionFromSocket(socket); |
| 253 if (connection) { | 256 if (connection) { |
| 254 connection->r_.OnDataReceived(data); | 257 connection->r_.OnDataReceived(data); |
| 255 if (connection->r_.AllContentReceived()) { | 258 if (connection->r_.AllContentReceived()) { |
| 259 std::wstring path = UTF8ToWide(connection->r_.path()); |
| 256 if (LowerCaseEqualsASCII(connection->r_.method(), "post")) | 260 if (LowerCaseEqualsASCII(connection->r_.method(), "post")) |
| 257 this->Post(connection, connection->r_.path(), connection->r_); | 261 this->Post(connection, path, connection->r_); |
| 258 else | 262 else |
| 259 this->Get(connection, connection->r_.path(), connection->r_); | 263 this->Get(connection, path, connection->r_); |
| 260 } | 264 } |
| 261 } | 265 } |
| 262 } | 266 } |
| 263 | 267 |
| 264 void HTTPTestServer::DidClose(ListenSocket* socket) { | 268 void HTTPTestServer::DidClose(ListenSocket* socket) { |
| 265 ConnectionList::iterator it = FindConnection(socket); | 269 ConnectionList::iterator it = FindConnection(socket); |
| 266 DCHECK(it != connection_list_.end()); | 270 DCHECK(it != connection_list_.end()); |
| 267 connection_list_.erase(it); | 271 connection_list_.erase(it); |
| 268 } | 272 } |
| 269 | 273 |
| 274 std::wstring HTTPTestServer::Resolve(const std::wstring& path) { |
| 275 // Remove the first '/' if needed. |
| 276 std::wstring stripped_path = path; |
| 277 if (path.size() && path[0] == L'/') |
| 278 stripped_path = path.substr(1); |
| 279 |
| 280 if (port_ == 80) { |
| 281 if (stripped_path.empty()) { |
| 282 return StringPrintf(L"http://%ls", address_.c_str()); |
| 283 } else { |
| 284 return StringPrintf(L"http://%ls/%ls", address_.c_str(), |
| 285 stripped_path.c_str()); |
| 286 } |
| 287 } else { |
| 288 if (stripped_path.empty()) { |
| 289 return StringPrintf(L"http://%ls:%d", address_.c_str(), port_); |
| 290 } else { |
| 291 return StringPrintf(L"http://%ls:%d/%ls", address_.c_str(), port_, |
| 292 stripped_path.c_str()); |
| 293 } |
| 294 } |
| 295 } |
| 296 |
| 270 void ConfigurableConnection::SendChunk() { | 297 void ConfigurableConnection::SendChunk() { |
| 271 int size = (int)data_.size(); | 298 int size = (int)data_.size(); |
| 272 const char* chunk_ptr = data_.c_str() + cur_pos_; | 299 const char* chunk_ptr = data_.c_str() + cur_pos_; |
| 273 int bytes_to_send = std::min(options_.chunk_size_, size - cur_pos_); | 300 int bytes_to_send = std::min(options_.chunk_size_, size - cur_pos_); |
| 274 | 301 |
| 275 socket_->Send(chunk_ptr, bytes_to_send); | 302 socket_->Send(chunk_ptr, bytes_to_send); |
| 276 DLOG(INFO) << "Sent(" << cur_pos_ << "," << bytes_to_send | 303 DLOG(INFO) << "Sent(" << cur_pos_ << "," << bytes_to_send |
| 277 << "): " << base::StringPiece(chunk_ptr, bytes_to_send); | 304 << "): " << base::StringPiece(chunk_ptr, bytes_to_send); |
| 278 | 305 |
| 279 cur_pos_ += bytes_to_send; | 306 cur_pos_ += bytes_to_send; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 if (options_.speed_ == SendOptions::DELAYED) { | 349 if (options_.speed_ == SendOptions::DELAYED) { |
| 323 data_ = headers; | 350 data_ = headers; |
| 324 data_.append(content_length_header); | 351 data_.append(content_length_header); |
| 325 data_.append("\r\n"); | 352 data_.append("\r\n"); |
| 326 } | 353 } |
| 327 | 354 |
| 328 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 355 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 329 NewRunnableMethod(this, &ConfigurableConnection::SendChunk), | 356 NewRunnableMethod(this, &ConfigurableConnection::SendChunk), |
| 330 options.timeout_); | 357 options.timeout_); |
| 331 } | 358 } |
| 332 } // namespace test_server | 359 |
| 360 } // namespace test_server |
| OLD | NEW |