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 "net/http/http_network_layer.h" | 5 #include "net/http/http_network_layer.h" |
6 | 6 |
7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
8 #include "net/base/net_log.h" | 8 #include "net/base/net_log.h" |
9 #include "net/cert/mock_cert_verifier.h" | 9 #include "net/cert/mock_cert_verifier.h" |
10 #include "net/dns/mock_host_resolver.h" | 10 #include "net/dns/mock_host_resolver.h" |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 return HostPortPair::FromURL(GURL(SPDY_PROXY_AUTH_ORIGIN)).ToString(); | 53 return HostPortPair::FromURL(GURL(SPDY_PROXY_AUTH_ORIGIN)).ToString(); |
54 } | 54 } |
55 #endif | 55 #endif |
56 | 56 |
57 #if defined(SPDY_PROXY_AUTH_ORIGIN) && defined(DATA_REDUCTION_FALLBACK_HOST) | 57 #if defined(SPDY_PROXY_AUTH_ORIGIN) && defined(DATA_REDUCTION_FALLBACK_HOST) |
58 std::string GetChromeFallbackProxy() { | 58 std::string GetChromeFallbackProxy() { |
59 return HostPortPair::FromURL(GURL(DATA_REDUCTION_FALLBACK_HOST)).ToString(); | 59 return HostPortPair::FromURL(GURL(DATA_REDUCTION_FALLBACK_HOST)).ToString(); |
60 } | 60 } |
61 #endif | 61 #endif |
62 | 62 |
63 void ExecuteRequestExpectingContentAndHeader(const std::string& method, | 63 void ExecuteRequestExpectingContentAndHeader(const std::string& content, |
64 const std::string& content, | |
65 const std::string& header, | 64 const std::string& header, |
66 const std::string& value) { | 65 const std::string& value) { |
67 TestCompletionCallback callback; | 66 TestCompletionCallback callback; |
68 | 67 |
69 HttpRequestInfo request_info; | 68 HttpRequestInfo request_info; |
70 request_info.url = GURL("http://www.google.com/"); | 69 request_info.url = GURL("http://www.google.com/"); |
71 request_info.method = method; | 70 request_info.method = "GET"; |
72 request_info.load_flags = LOAD_NORMAL; | 71 request_info.load_flags = LOAD_NORMAL; |
73 | 72 |
74 scoped_ptr<HttpTransaction> trans; | 73 scoped_ptr<HttpTransaction> trans; |
75 int rv = factory_->CreateTransaction(DEFAULT_PRIORITY, &trans, NULL); | 74 int rv = factory_->CreateTransaction(DEFAULT_PRIORITY, &trans, NULL); |
76 EXPECT_EQ(OK, rv); | 75 EXPECT_EQ(OK, rv); |
77 | 76 |
78 rv = trans->Start(&request_info, callback.callback(), BoundNetLog()); | 77 rv = trans->Start(&request_info, callback.callback(), BoundNetLog()); |
79 if (rv == ERR_IO_PENDING) | 78 if (rv == ERR_IO_PENDING) |
80 rv = callback.WaitForResult(); | 79 rv = callback.WaitForResult(); |
81 ASSERT_EQ(OK, rv); | 80 ASSERT_EQ(OK, rv); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 }; | 115 }; |
117 TestProxyFallbackWithMockReads(bad_proxy, "", data_reads, | 116 TestProxyFallbackWithMockReads(bad_proxy, "", data_reads, |
118 arraysize(data_reads), 1u); | 117 arraysize(data_reads), 1u); |
119 } | 118 } |
120 | 119 |
121 void TestProxyFallbackWithMockReads(const std::string& bad_proxy, | 120 void TestProxyFallbackWithMockReads(const std::string& bad_proxy, |
122 const std::string& bad_proxy2, | 121 const std::string& bad_proxy2, |
123 MockRead data_reads[], | 122 MockRead data_reads[], |
124 int data_reads_size, | 123 int data_reads_size, |
125 unsigned int expected_retry_info_size) { | 124 unsigned int expected_retry_info_size) { |
126 TestProxyFallbackByMethodWithMockReads(bad_proxy, bad_proxy2, data_reads, | |
127 data_reads_size, "GET", "content", | |
128 true, expected_retry_info_size); | |
129 } | |
130 | |
131 void TestProxyFallbackByMethodWithMockReads( | |
132 const std::string& bad_proxy, | |
133 const std::string& bad_proxy2, | |
134 MockRead data_reads[], | |
135 int data_reads_size, | |
136 std::string method, | |
137 std::string content, | |
138 bool retry_expected, | |
139 unsigned int expected_retry_info_size) { | |
140 std::string trailer = | |
141 (method == "HEAD" || method == "PUT" || method == "POST") ? | |
142 "Content-Length: 0\r\n\r\n" : "\r\n"; | |
143 std::string request = | |
144 base::StringPrintf("%s http://www.google.com/ HTTP/1.1\r\n" | |
145 "Host: www.google.com\r\n" | |
146 "Proxy-Connection: keep-alive\r\n" | |
147 "%s", method.c_str(), trailer.c_str()); | |
148 | |
149 MockWrite data_writes[] = { | 125 MockWrite data_writes[] = { |
150 MockWrite(request.c_str()), | 126 MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 127 "Host: www.google.com\r\n" |
| 128 "Proxy-Connection: keep-alive\r\n\r\n"), |
151 }; | 129 }; |
152 | 130 |
153 StaticSocketDataProvider data1(data_reads, data_reads_size, | 131 StaticSocketDataProvider data1(data_reads, data_reads_size, |
154 data_writes, arraysize(data_writes)); | 132 data_writes, arraysize(data_writes)); |
155 mock_socket_factory_.AddSocketDataProvider(&data1); | 133 mock_socket_factory_.AddSocketDataProvider(&data1); |
156 | 134 |
157 // Second data provider returns the expected content. | 135 // Second data provider returns the expected content. |
158 MockRead data_reads2[3]; | 136 MockRead data_reads2[] = { |
159 size_t data_reads2_index = 0; | 137 MockRead("HTTP/1.0 200 OK\r\n" |
160 data_reads2[data_reads2_index++] = MockRead("HTTP/1.0 200 OK\r\n" | 138 "Server: not-proxy\r\n\r\n"), |
161 "Server: not-proxy\r\n\r\n"); | 139 MockRead("content"), |
162 if (!content.empty()) | 140 MockRead(SYNCHRONOUS, OK), |
163 data_reads2[data_reads2_index++] = MockRead(content.c_str()); | 141 }; |
164 data_reads2[data_reads2_index++] = MockRead(SYNCHRONOUS, OK); | |
165 | |
166 MockWrite data_writes2[] = { | 142 MockWrite data_writes2[] = { |
167 MockWrite(request.c_str()), | 143 MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 144 "Host: www.google.com\r\n" |
| 145 "Proxy-Connection: keep-alive\r\n\r\n"), |
168 }; | 146 }; |
169 StaticSocketDataProvider data2(data_reads2, data_reads2_index, | 147 StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
170 data_writes2, arraysize(data_writes2)); | 148 data_writes2, arraysize(data_writes2)); |
171 mock_socket_factory_.AddSocketDataProvider(&data2); | 149 mock_socket_factory_.AddSocketDataProvider(&data2); |
172 | 150 |
173 // Expect that we get "content" and not "Bypass message", and that there's | 151 // Expect that we get "content" and not "Bypass message", and that there's |
174 // a "not-proxy" "Server:" header in the final response. | 152 // a "not-proxy" "Server:" header in the final response. |
175 if (retry_expected) { | 153 ExecuteRequestExpectingContentAndHeader("content", "server", "not-proxy"); |
176 ExecuteRequestExpectingContentAndHeader(method, content, | |
177 "server", "not-proxy"); | |
178 } else { | |
179 ExecuteRequestExpectingContentAndHeader(method, "Bypass message", "", ""); | |
180 } | |
181 | 154 |
182 // We should also observe the bad proxy in the retry list. | 155 // We should also observe the bad proxy in the retry list. |
183 TestBadProxies(expected_retry_info_size, bad_proxy, bad_proxy2); | 156 TestBadProxies(expected_retry_info_size, bad_proxy, bad_proxy2); |
184 } | 157 } |
185 | 158 |
186 // Simulates a request through a proxy which returns a bypass, which is then | 159 // Simulates a request through a proxy which returns a bypass, which is then |
187 // retried through a direct connection to the origin site. | 160 // retried through a direct connection to the origin site. |
188 // Checks that the expected requests were issued, the expected content was | 161 // Checks that the expected requests were issued, the expected content was |
189 // received, and the proxy |bad_proxy| was marked as bad. | 162 // received, and the proxy |bad_proxy| was marked as bad. |
190 void TestProxyFallbackToDirect(const std::string& bad_proxy) { | 163 void TestProxyFallbackToDirect(const std::string& bad_proxy) { |
(...skipping 23 matching lines...) Expand all Loading... |
214 MockWrite("GET / HTTP/1.1\r\n" | 187 MockWrite("GET / HTTP/1.1\r\n" |
215 "Host: www.google.com\r\n" | 188 "Host: www.google.com\r\n" |
216 "Connection: keep-alive\r\n\r\n"), | 189 "Connection: keep-alive\r\n\r\n"), |
217 }; | 190 }; |
218 StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), | 191 StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
219 data_writes2, arraysize(data_writes2)); | 192 data_writes2, arraysize(data_writes2)); |
220 mock_socket_factory_.AddSocketDataProvider(&data2); | 193 mock_socket_factory_.AddSocketDataProvider(&data2); |
221 | 194 |
222 // Expect that we get "content" and not "Bypass message", and that there's | 195 // Expect that we get "content" and not "Bypass message", and that there's |
223 // a "not-proxy" "Server:" header in the final response. | 196 // a "not-proxy" "Server:" header in the final response. |
224 ExecuteRequestExpectingContentAndHeader("GET", "content", | 197 ExecuteRequestExpectingContentAndHeader("content", "server", "not-proxy"); |
225 "server", "not-proxy"); | |
226 | 198 |
227 // We should also observe the bad proxy in the retry list. | 199 // We should also observe the bad proxy in the retry list. |
228 TestBadProxies(1u, bad_proxy, ""); | 200 TestBadProxies(1u, bad_proxy, ""); |
229 } | 201 } |
230 | 202 |
231 // Simulates a request through a proxy which returns a bypass, under a | 203 // Simulates a request through a proxy which returns a bypass, under a |
232 // configuration where there is no valid bypass. |proxy_count| proxies | 204 // configuration where there is no valid bypass. |proxy_count| proxies |
233 // are expected to be configured. | 205 // are expected to be configured. |
234 // Checks that the expected requests were issued, the bypass message was the | 206 // Checks that the expected requests were issued, the bypass message was the |
235 // final received content, and all proxies were marked as bad. | 207 // final received content, and all proxies were marked as bad. |
(...skipping 14 matching lines...) Expand all Loading... |
250 StaticSocketDataProvider data1(data_reads, arraysize(data_reads), | 222 StaticSocketDataProvider data1(data_reads, arraysize(data_reads), |
251 data_writes, arraysize(data_writes)); | 223 data_writes, arraysize(data_writes)); |
252 StaticSocketDataProvider data2(data_reads, arraysize(data_reads), | 224 StaticSocketDataProvider data2(data_reads, arraysize(data_reads), |
253 data_writes, arraysize(data_writes)); | 225 data_writes, arraysize(data_writes)); |
254 | 226 |
255 mock_socket_factory_.AddSocketDataProvider(&data1); | 227 mock_socket_factory_.AddSocketDataProvider(&data1); |
256 if (proxy_count > 1) | 228 if (proxy_count > 1) |
257 mock_socket_factory_.AddSocketDataProvider(&data2); | 229 mock_socket_factory_.AddSocketDataProvider(&data2); |
258 | 230 |
259 // Expect that we get "Bypass message", and not "content".. | 231 // Expect that we get "Bypass message", and not "content".. |
260 ExecuteRequestExpectingContentAndHeader("GET", "Bypass message", "", ""); | 232 ExecuteRequestExpectingContentAndHeader("Bypass message", "", ""); |
261 | 233 |
262 // We should also observe the bad proxy or proxies in the retry list. | 234 // We should also observe the bad proxy or proxies in the retry list. |
263 TestBadProxies(proxy_count, bad_proxy, bad_proxy2); | 235 TestBadProxies(proxy_count, bad_proxy, bad_proxy2); |
264 } | 236 } |
265 | 237 |
266 MockClientSocketFactory mock_socket_factory_; | 238 MockClientSocketFactory mock_socket_factory_; |
267 MockHostResolver host_resolver_; | 239 MockHostResolver host_resolver_; |
268 scoped_ptr<CertVerifier> cert_verifier_; | 240 scoped_ptr<CertVerifier> cert_verifier_; |
269 scoped_ptr<TransportSecurityState> transport_security_state_; | 241 scoped_ptr<TransportSecurityState> transport_security_state_; |
270 scoped_ptr<ProxyService> proxy_service_; | 242 scoped_ptr<ProxyService> proxy_service_; |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
359 TestProxyFallback(bad_proxy); | 331 TestProxyFallback(bad_proxy); |
360 } | 332 } |
361 | 333 |
362 TEST_F(HttpNetworkLayerTest, ServerTwoProxyBypassFixed) { | 334 TEST_F(HttpNetworkLayerTest, ServerTwoProxyBypassFixed) { |
363 std::string bad_proxy = GetChromeProxy(); | 335 std::string bad_proxy = GetChromeProxy(); |
364 ConfigureTestDependencies( | 336 ConfigureTestDependencies( |
365 ProxyService::CreateFixed(bad_proxy +", good:8080")); | 337 ProxyService::CreateFixed(bad_proxy +", good:8080")); |
366 TestProxyFallback(bad_proxy); | 338 TestProxyFallback(bad_proxy); |
367 } | 339 } |
368 | 340 |
369 TEST_F(HttpNetworkLayerTest, BypassAndRetryIdempotentMethods) { | |
370 std::string bad_proxy = GetChromeProxy(); | |
371 const struct { | |
372 std::string method; | |
373 std::string content; | |
374 bool expected_to_retry; | |
375 } tests[] = { | |
376 { | |
377 "GET", | |
378 "content", | |
379 true, | |
380 }, | |
381 { | |
382 "OPTIONS", | |
383 "content", | |
384 true, | |
385 }, | |
386 { | |
387 "HEAD", | |
388 "", | |
389 true, | |
390 }, | |
391 { | |
392 "PUT", | |
393 "", | |
394 true, | |
395 }, | |
396 { | |
397 "DELETE", | |
398 "content", | |
399 true, | |
400 }, | |
401 { | |
402 "TRACE", | |
403 "content", | |
404 true, | |
405 }, | |
406 { | |
407 "POST", | |
408 "Bypass message", | |
409 false, | |
410 }, | |
411 }; | |
412 | |
413 for (size_t i = 0; i < arraysize(tests); ++i) { | |
414 ConfigureTestDependencies( | |
415 ProxyService::CreateFixed(bad_proxy +", good:8080")); | |
416 MockRead data_reads[] = { | |
417 MockRead("HTTP/1.1 200 OK\r\n" | |
418 "Chrome-Proxy: bypass=0\r\n\r\n"), | |
419 MockRead("Bypass message"), | |
420 MockRead(SYNCHRONOUS, OK), | |
421 }; | |
422 TestProxyFallbackByMethodWithMockReads(bad_proxy, "", data_reads, | |
423 arraysize(data_reads), | |
424 tests[i].method, | |
425 tests[i].content, | |
426 tests[i].expected_to_retry, 1u); | |
427 } | |
428 } | |
429 | |
430 TEST_F(HttpNetworkLayerTest, ServerOneProxyWithDirectBypassPac) { | 341 TEST_F(HttpNetworkLayerTest, ServerOneProxyWithDirectBypassPac) { |
431 std::string bad_proxy = GetChromeProxy(); | 342 std::string bad_proxy = GetChromeProxy(); |
432 ConfigureTestDependencies(ProxyService::CreateFixedFromPacResult( | 343 ConfigureTestDependencies(ProxyService::CreateFixedFromPacResult( |
433 "PROXY " + bad_proxy + "; DIRECT")); | 344 "PROXY " + bad_proxy + "; DIRECT")); |
434 TestProxyFallbackToDirect(bad_proxy); | 345 TestProxyFallbackToDirect(bad_proxy); |
435 } | 346 } |
436 | 347 |
437 TEST_F(HttpNetworkLayerTest, ServerOneProxyWithDirectBypassFixed) { | 348 TEST_F(HttpNetworkLayerTest, ServerOneProxyWithDirectBypassFixed) { |
438 std::string bad_proxy = GetChromeProxy(); | 349 std::string bad_proxy = GetChromeProxy(); |
439 ConfigureTestDependencies( | 350 ConfigureTestDependencies( |
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
724 ASSERT_EQ(ERR_CONNECTION_RESET, callback.GetResult(rv)); | 635 ASSERT_EQ(ERR_CONNECTION_RESET, callback.GetResult(rv)); |
725 | 636 |
726 // If the response info is null, that means that any consumer won't | 637 // If the response info is null, that means that any consumer won't |
727 // see the network accessed bit set. | 638 // see the network accessed bit set. |
728 EXPECT_EQ(NULL, trans->GetResponseInfo()); | 639 EXPECT_EQ(NULL, trans->GetResponseInfo()); |
729 } | 640 } |
730 | 641 |
731 } // namespace | 642 } // namespace |
732 | 643 |
733 } // namespace net | 644 } // namespace net |
OLD | NEW |