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 <windows.h> | 5 #include <windows.h> |
6 #include <objbase.h> | 6 #include <objbase.h> |
7 #include <urlmon.h> | 7 #include <urlmon.h> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
12 #include "base/string_piece.h" | 12 #include "base/string_piece.h" |
13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
14 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
| 16 #include "chrome_frame/test/chrome_frame_test_utils.h" |
16 #include "chrome_frame/test/test_server.h" | 17 #include "chrome_frame/test/test_server.h" |
17 #include "net/base/tcp_listen_socket.h" | 18 #include "net/base/tcp_listen_socket.h" |
18 #include "net/base/winsock_init.h" | 19 #include "net/base/winsock_init.h" |
19 #include "net/http/http_util.h" | 20 #include "net/http/http_util.h" |
20 | 21 |
21 namespace test_server { | 22 namespace test_server { |
22 const char kDefaultHeaderTemplate[] = | 23 const char kDefaultHeaderTemplate[] = |
23 "HTTP/1.1 %hs\r\n" | 24 "HTTP/1.1 %hs\r\n" |
24 "Connection: close\r\n" | 25 "Connection: close\r\n" |
25 "Content-Type: %hs\r\n" | 26 "Content-Type: %hs\r\n" |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 *headers = base::StringPrintf("HTTP/1.1 302 Found\r\n" | 130 *headers = base::StringPrintf("HTTP/1.1 302 Found\r\n" |
130 "Connection: close\r\n" | 131 "Connection: close\r\n" |
131 "Content-Length: 0\r\n" | 132 "Content-Length: 0\r\n" |
132 "Content-Type: text/html\r\n" | 133 "Content-Type: text/html\r\n" |
133 "Location: %hs\r\n\r\n", | 134 "Location: %hs\r\n\r\n", |
134 redirect_url_.c_str()); | 135 redirect_url_.c_str()); |
135 return true; | 136 return true; |
136 } | 137 } |
137 | 138 |
138 SimpleWebServer::SimpleWebServer(int port) { | 139 SimpleWebServer::SimpleWebServer(int port) { |
139 CHECK(MessageLoop::current()) << "SimpleWebServer requires a message loop"; | 140 Construct(chrome_frame_test::GetLocalIPv4Address(), port); |
140 net::EnsureWinsockInit(); | 141 } |
141 AddResponse(&quit_); | 142 |
142 server_ = net::TCPListenSocket::CreateAndListen("127.0.0.1", port, this); | 143 SimpleWebServer::SimpleWebServer(const std::string& address, int port) { |
143 DCHECK(server_.get() != NULL); | 144 Construct(address, port); |
144 } | 145 } |
145 | 146 |
146 SimpleWebServer::~SimpleWebServer() { | 147 SimpleWebServer::~SimpleWebServer() { |
147 ConnectionList::const_iterator it; | 148 ConnectionList::const_iterator it; |
148 for (it = connections_.begin(); it != connections_.end(); ++it) | 149 for (it = connections_.begin(); it != connections_.end(); ++it) |
149 delete (*it); | 150 delete (*it); |
150 connections_.clear(); | 151 connections_.clear(); |
151 } | 152 } |
152 | 153 |
| 154 void SimpleWebServer::Construct(const std::string& address, int port) { |
| 155 CHECK(MessageLoop::current()) << "SimpleWebServer requires a message loop"; |
| 156 net::EnsureWinsockInit(); |
| 157 AddResponse(&quit_); |
| 158 host_ = address; |
| 159 server_ = net::TCPListenSocket::CreateAndListen(address, port, this); |
| 160 LOG_IF(DFATAL, !server_.get()) |
| 161 << "Failed to create listener socket at " << address << ":" << port; |
| 162 } |
| 163 |
153 void SimpleWebServer::AddResponse(Response* response) { | 164 void SimpleWebServer::AddResponse(Response* response) { |
154 responses_.push_back(response); | 165 responses_.push_back(response); |
155 } | 166 } |
156 | 167 |
157 void SimpleWebServer::DeleteAllResponses() { | 168 void SimpleWebServer::DeleteAllResponses() { |
158 std::list<Response*>::const_iterator it; | 169 std::list<Response*>::const_iterator it; |
159 for (it = responses_.begin(); it != responses_.end(); ++it) { | 170 for (it = responses_.begin(); it != responses_.end(); ++it) { |
160 if ((*it) != &quit_) | 171 if ((*it) != &quit_) |
161 delete (*it); | 172 delete (*it); |
162 } | 173 } |
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 data_.append(content_length_header); | 410 data_.append(content_length_header); |
400 data_.append("\r\n"); | 411 data_.append("\r\n"); |
401 } | 412 } |
402 | 413 |
403 MessageLoop::current()->PostDelayedTask( | 414 MessageLoop::current()->PostDelayedTask( |
404 FROM_HERE, base::Bind(&ConfigurableConnection::SendChunk, this), | 415 FROM_HERE, base::Bind(&ConfigurableConnection::SendChunk, this), |
405 base::TimeDelta::FromMilliseconds(options.timeout_)); | 416 base::TimeDelta::FromMilliseconds(options.timeout_)); |
406 } | 417 } |
407 | 418 |
408 } // namespace test_server | 419 } // namespace test_server |
OLD | NEW |