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/url_request/url_fetcher_impl.h" | 5 #include "net/url_request/url_fetcher_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
12 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/message_loop/message_loop_proxy.h" | |
15 #include "base/path_service.h" | 14 #include "base/path_service.h" |
16 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
17 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
18 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
19 #include "base/synchronization/waitable_event.h" | 18 #include "base/synchronization/waitable_event.h" |
| 19 #include "base/thread_task_runner_handle.h" |
20 #include "base/threading/thread.h" | 20 #include "base/threading/thread.h" |
21 #include "build/build_config.h" | 21 #include "build/build_config.h" |
22 #include "crypto/nss_util.h" | 22 #include "crypto/nss_util.h" |
23 #include "net/base/elements_upload_data_stream.h" | 23 #include "net/base/elements_upload_data_stream.h" |
24 #include "net/base/network_change_notifier.h" | 24 #include "net/base/network_change_notifier.h" |
25 #include "net/base/upload_bytes_element_reader.h" | 25 #include "net/base/upload_bytes_element_reader.h" |
26 #include "net/base/upload_element_reader.h" | 26 #include "net/base/upload_element_reader.h" |
27 #include "net/base/upload_file_element_reader.h" | 27 #include "net/base/upload_file_element_reader.h" |
28 #include "net/dns/mock_host_resolver.h" | 28 #include "net/dns/mock_host_resolver.h" |
29 #include "net/http/http_response_headers.h" | 29 #include "net/http/http_response_headers.h" |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 static int GetNumFetcherCores() { | 299 static int GetNumFetcherCores() { |
300 return URLFetcherImpl::GetNumFetcherCores(); | 300 return URLFetcherImpl::GetNumFetcherCores(); |
301 } | 301 } |
302 | 302 |
303 // Creates a URLRequestContextGetter with a URLRequestContext that lives on | 303 // Creates a URLRequestContextGetter with a URLRequestContext that lives on |
304 // the current thread. | 304 // the current thread. |
305 scoped_refptr<FetcherTestURLRequestContextGetter> | 305 scoped_refptr<FetcherTestURLRequestContextGetter> |
306 CreateSameThreadContextGetter() { | 306 CreateSameThreadContextGetter() { |
307 return scoped_refptr<FetcherTestURLRequestContextGetter>( | 307 return scoped_refptr<FetcherTestURLRequestContextGetter>( |
308 new FetcherTestURLRequestContextGetter( | 308 new FetcherTestURLRequestContextGetter( |
309 base::MessageLoopProxy::current(), hanging_url().host())); | 309 base::ThreadTaskRunnerHandle::Get(), hanging_url().host())); |
310 } | 310 } |
311 | 311 |
312 // Creates a URLRequestContextGetter with a URLRequestContext that lives on | 312 // Creates a URLRequestContextGetter with a URLRequestContext that lives on |
313 // a separate network thread. | 313 // a separate network thread. |
314 scoped_refptr<FetcherTestURLRequestContextGetter> | 314 scoped_refptr<FetcherTestURLRequestContextGetter> |
315 CreateCrossThreadContextGetter() { | 315 CreateCrossThreadContextGetter() { |
316 if (!network_thread_) { | 316 if (!network_thread_) { |
317 network_thread_.reset(new base::Thread("network thread")); | 317 network_thread_.reset(new base::Thread("network thread")); |
318 base::Thread::Options network_thread_options; | 318 base::Thread::Options network_thread_options; |
319 network_thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | 319 network_thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
353 const base::FilePath& requested_out_path, | 353 const base::FilePath& requested_out_path, |
354 bool take_ownership) { | 354 bool take_ownership) { |
355 scoped_ptr<WaitingURLFetcherDelegate> delegate( | 355 scoped_ptr<WaitingURLFetcherDelegate> delegate( |
356 new WaitingURLFetcherDelegate()); | 356 new WaitingURLFetcherDelegate()); |
357 delegate->CreateFetcher( | 357 delegate->CreateFetcher( |
358 test_server_->GetURL(std::string(kTestServerFilePrefix) + | 358 test_server_->GetURL(std::string(kTestServerFilePrefix) + |
359 file_to_fetch), | 359 file_to_fetch), |
360 URLFetcher::GET, CreateSameThreadContextGetter()); | 360 URLFetcher::GET, CreateSameThreadContextGetter()); |
361 if (save_to_temporary_file) { | 361 if (save_to_temporary_file) { |
362 delegate->fetcher()->SaveResponseToTemporaryFile( | 362 delegate->fetcher()->SaveResponseToTemporaryFile( |
363 scoped_refptr<base::MessageLoopProxy>( | 363 base::ThreadTaskRunnerHandle::Get()); |
364 base::MessageLoopProxy::current())); | |
365 } else { | 364 } else { |
366 delegate->fetcher()->SaveResponseToFileAtPath( | 365 delegate->fetcher()->SaveResponseToFileAtPath( |
367 requested_out_path, scoped_refptr<base::MessageLoopProxy>( | 366 requested_out_path, base::ThreadTaskRunnerHandle::Get()); |
368 base::MessageLoopProxy::current())); | |
369 } | 367 } |
370 delegate->StartFetcherAndWait(); | 368 delegate->StartFetcherAndWait(); |
371 | 369 |
372 EXPECT_TRUE(delegate->fetcher()->GetStatus().is_success()); | 370 EXPECT_TRUE(delegate->fetcher()->GetStatus().is_success()); |
373 EXPECT_EQ(200, delegate->fetcher()->GetResponseCode()); | 371 EXPECT_EQ(200, delegate->fetcher()->GetResponseCode()); |
374 | 372 |
375 base::FilePath out_path; | 373 base::FilePath out_path; |
376 EXPECT_TRUE( | 374 EXPECT_TRUE( |
377 delegate->fetcher()->GetResponseAsFilePath(take_ownership, &out_path)); | 375 delegate->fetcher()->GetResponseAsFilePath(take_ownership, &out_path)); |
378 if (!save_to_temporary_file) { | 376 if (!save_to_temporary_file) { |
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
675 } | 673 } |
676 | 674 |
677 TEST_F(URLFetcherTest, PostEntireFile) { | 675 TEST_F(URLFetcherTest, PostEntireFile) { |
678 base::FilePath upload_path = GetUploadFileTestPath(); | 676 base::FilePath upload_path = GetUploadFileTestPath(); |
679 | 677 |
680 WaitingURLFetcherDelegate delegate; | 678 WaitingURLFetcherDelegate delegate; |
681 delegate.CreateFetcher(test_server_->GetURL("echo"), URLFetcher::POST, | 679 delegate.CreateFetcher(test_server_->GetURL("echo"), URLFetcher::POST, |
682 CreateSameThreadContextGetter()); | 680 CreateSameThreadContextGetter()); |
683 delegate.fetcher()->SetUploadFilePath("application/x-www-form-urlencoded", | 681 delegate.fetcher()->SetUploadFilePath("application/x-www-form-urlencoded", |
684 upload_path, 0, kuint64max, | 682 upload_path, 0, kuint64max, |
685 base::MessageLoopProxy::current()); | 683 base::ThreadTaskRunnerHandle::Get()); |
686 delegate.StartFetcherAndWait(); | 684 delegate.StartFetcherAndWait(); |
687 | 685 |
688 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); | 686 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); |
689 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); | 687 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); |
690 | 688 |
691 std::string expected; | 689 std::string expected; |
692 ASSERT_TRUE(base::ReadFileToString(upload_path, &expected)); | 690 ASSERT_TRUE(base::ReadFileToString(upload_path, &expected)); |
693 std::string data; | 691 std::string data; |
694 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); | 692 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); |
695 EXPECT_EQ(expected, data); | 693 EXPECT_EQ(expected, data); |
696 } | 694 } |
697 | 695 |
698 TEST_F(URLFetcherTest, PostFileRange) { | 696 TEST_F(URLFetcherTest, PostFileRange) { |
699 const size_t kRangeStart = 30; | 697 const size_t kRangeStart = 30; |
700 const size_t kRangeLength = 100; | 698 const size_t kRangeLength = 100; |
701 base::FilePath upload_path = GetUploadFileTestPath(); | 699 base::FilePath upload_path = GetUploadFileTestPath(); |
702 | 700 |
703 WaitingURLFetcherDelegate delegate; | 701 WaitingURLFetcherDelegate delegate; |
704 delegate.CreateFetcher(test_server_->GetURL("echo"), URLFetcher::POST, | 702 delegate.CreateFetcher(test_server_->GetURL("echo"), URLFetcher::POST, |
705 CreateSameThreadContextGetter()); | 703 CreateSameThreadContextGetter()); |
706 delegate.fetcher()->SetUploadFilePath("application/x-www-form-urlencoded", | 704 delegate.fetcher()->SetUploadFilePath("application/x-www-form-urlencoded", |
707 upload_path, kRangeStart, kRangeLength, | 705 upload_path, kRangeStart, kRangeLength, |
708 base::MessageLoopProxy::current()); | 706 base::ThreadTaskRunnerHandle::Get()); |
709 delegate.StartFetcherAndWait(); | 707 delegate.StartFetcherAndWait(); |
710 | 708 |
711 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); | 709 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); |
712 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); | 710 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); |
713 | 711 |
714 std::string expected; | 712 std::string expected; |
715 ASSERT_TRUE(base::ReadFileToString(upload_path, &expected)); | 713 ASSERT_TRUE(base::ReadFileToString(upload_path, &expected)); |
716 std::string data; | 714 std::string data; |
717 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); | 715 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); |
718 EXPECT_EQ(expected.substr(kRangeStart, kRangeLength), data); | 716 EXPECT_EQ(expected.substr(kRangeStart, kRangeLength), data); |
(...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1126 | 1124 |
1127 // Make sure that the URLFetcher releases its context getter pointer on | 1125 // Make sure that the URLFetcher releases its context getter pointer on |
1128 // cancellation, cross-thread case. | 1126 // cancellation, cross-thread case. |
1129 TEST_F(URLFetcherTest, CancelDifferentThreads) { | 1127 TEST_F(URLFetcherTest, CancelDifferentThreads) { |
1130 base::RunLoop run_loop_; | 1128 base::RunLoop run_loop_; |
1131 | 1129 |
1132 WaitingURLFetcherDelegate delegate; | 1130 WaitingURLFetcherDelegate delegate; |
1133 scoped_refptr<FetcherTestURLRequestContextGetter> context_getter( | 1131 scoped_refptr<FetcherTestURLRequestContextGetter> context_getter( |
1134 CreateCrossThreadContextGetter()); | 1132 CreateCrossThreadContextGetter()); |
1135 context_getter->set_on_destruction_callback(base::Bind( | 1133 context_getter->set_on_destruction_callback(base::Bind( |
1136 base::IgnoreResult(&base::MessageLoopProxy::PostTask), | 1134 base::IgnoreResult(&base::TaskRunner::PostTask), |
1137 base::MessageLoopProxy::current(), FROM_HERE, run_loop_.QuitClosure())); | 1135 base::ThreadTaskRunnerHandle::Get(), FROM_HERE, run_loop_.QuitClosure())); |
1138 delegate.CreateFetcher(hanging_url(), URLFetcher::GET, context_getter); | 1136 delegate.CreateFetcher(hanging_url(), URLFetcher::GET, context_getter); |
1139 | 1137 |
1140 // The getter won't be destroyed if the test holds on to a reference to it. | 1138 // The getter won't be destroyed if the test holds on to a reference to it. |
1141 context_getter = nullptr; | 1139 context_getter = nullptr; |
1142 | 1140 |
1143 delegate.fetcher()->Start(); | 1141 delegate.fetcher()->Start(); |
1144 delegate.CancelFetch(); | 1142 delegate.CancelFetch(); |
1145 run_loop_.Run(); | 1143 run_loop_.Run(); |
1146 | 1144 |
1147 EXPECT_FALSE(delegate.did_complete()); | 1145 EXPECT_FALSE(delegate.did_complete()); |
1148 } | 1146 } |
1149 | 1147 |
1150 TEST_F(URLFetcherTest, CancelWhileDelayedByThrottleDifferentThreads) { | 1148 TEST_F(URLFetcherTest, CancelWhileDelayedByThrottleDifferentThreads) { |
1151 GURL url = test_server_->GetURL(kDefaultResponsePath); | 1149 GURL url = test_server_->GetURL(kDefaultResponsePath); |
1152 base::RunLoop run_loop_; | 1150 base::RunLoop run_loop_; |
1153 | 1151 |
1154 WaitingURLFetcherDelegate delegate; | 1152 WaitingURLFetcherDelegate delegate; |
1155 scoped_refptr<FetcherTestURLRequestContextGetter> context_getter( | 1153 scoped_refptr<FetcherTestURLRequestContextGetter> context_getter( |
1156 CreateCrossThreadContextGetter()); | 1154 CreateCrossThreadContextGetter()); |
1157 context_getter->set_on_destruction_callback(base::Bind( | 1155 context_getter->set_on_destruction_callback(base::Bind( |
1158 base::IgnoreResult(&base::MessageLoopProxy::PostTask), | 1156 base::IgnoreResult(&base::TaskRunner::PostTask), |
1159 base::MessageLoopProxy::current(), FROM_HERE, run_loop_.QuitClosure())); | 1157 base::ThreadTaskRunnerHandle::Get(), FROM_HERE, run_loop_.QuitClosure())); |
1160 delegate.CreateFetcher(url, URLFetcher::GET, context_getter); | 1158 delegate.CreateFetcher(url, URLFetcher::GET, context_getter); |
1161 | 1159 |
1162 // Register an entry for test url using a sliding window of 400 seconds, and | 1160 // Register an entry for test url using a sliding window of 400 seconds, and |
1163 // max of 1 request. Also simulate a request having just started, so the | 1161 // max of 1 request. Also simulate a request having just started, so the |
1164 // next request will be affected by backoff of ~400 seconds. | 1162 // next request will be affected by backoff of ~400 seconds. |
1165 context_getter->AddThrottlerEntry( | 1163 context_getter->AddThrottlerEntry( |
1166 url, std::string() /* url_id */, 400000 /* sliding_window_period_ms */, | 1164 url, std::string() /* url_id */, 400000 /* sliding_window_period_ms */, |
1167 1 /* max_send_threshold */, 200000 /* initial_backoff_ms */, | 1165 1 /* max_send_threshold */, 200000 /* initial_backoff_ms */, |
1168 2.0 /* multiply_factor */, 0.0 /* jitter_factor */, | 1166 2.0 /* multiply_factor */, 0.0 /* jitter_factor */, |
1169 400000 /* maximum_backoff_ms */, | 1167 400000 /* maximum_backoff_ms */, |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1357 static const char kFileToFetch[] = "simple.html"; | 1355 static const char kFileToFetch[] = "simple.html"; |
1358 base::FilePath out_path = temp_dir.path().AppendASCII(kFileToFetch); | 1356 base::FilePath out_path = temp_dir.path().AppendASCII(kFileToFetch); |
1359 ASSERT_TRUE(base::CreateDirectory(out_path)); | 1357 ASSERT_TRUE(base::CreateDirectory(out_path)); |
1360 ASSERT_TRUE(base::PathExists(out_path)); | 1358 ASSERT_TRUE(base::PathExists(out_path)); |
1361 | 1359 |
1362 WaitingURLFetcherDelegate delegate; | 1360 WaitingURLFetcherDelegate delegate; |
1363 delegate.CreateFetcher( | 1361 delegate.CreateFetcher( |
1364 test_server_->GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), | 1362 test_server_->GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), |
1365 URLFetcher::GET, CreateSameThreadContextGetter()); | 1363 URLFetcher::GET, CreateSameThreadContextGetter()); |
1366 delegate.fetcher()->SaveResponseToFileAtPath( | 1364 delegate.fetcher()->SaveResponseToFileAtPath( |
1367 out_path, | 1365 out_path, base::ThreadTaskRunnerHandle::Get()); |
1368 scoped_refptr<base::MessageLoopProxy>(base::MessageLoopProxy::current())); | |
1369 delegate.StartFetcherAndWait(); | 1366 delegate.StartFetcherAndWait(); |
1370 | 1367 |
1371 EXPECT_FALSE(delegate.fetcher()->GetStatus().is_success()); | 1368 EXPECT_FALSE(delegate.fetcher()->GetStatus().is_success()); |
1372 EXPECT_EQ(ERR_ACCESS_DENIED, delegate.fetcher()->GetStatus().error()); | 1369 EXPECT_EQ(ERR_ACCESS_DENIED, delegate.fetcher()->GetStatus().error()); |
1373 } | 1370 } |
1374 | 1371 |
1375 // Get a small file and save it to a temp file. | 1372 // Get a small file and save it to a temp file. |
1376 TEST_F(URLFetcherTest, TempFileTestSmallGet) { | 1373 TEST_F(URLFetcherTest, TempFileTestSmallGet) { |
1377 SaveFileTest("simple.html", true, base::FilePath(), false); | 1374 SaveFileTest("simple.html", true, base::FilePath(), false); |
1378 } | 1375 } |
(...skipping 22 matching lines...) Expand all Loading... |
1401 EXPECT_EQ(-1, delegate.fetcher()->GetResponseCode()); | 1398 EXPECT_EQ(-1, delegate.fetcher()->GetResponseCode()); |
1402 EXPECT_TRUE(delegate.fetcher()->GetCookies().empty()); | 1399 EXPECT_TRUE(delegate.fetcher()->GetCookies().empty()); |
1403 std::string data; | 1400 std::string data; |
1404 EXPECT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); | 1401 EXPECT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); |
1405 EXPECT_TRUE(data.empty()); | 1402 EXPECT_TRUE(data.empty()); |
1406 } | 1403 } |
1407 | 1404 |
1408 } // namespace | 1405 } // namespace |
1409 | 1406 |
1410 } // namespace net | 1407 } // namespace net |
OLD | NEW |