summaryrefslogtreecommitdiffstats
path: root/net/url_request
diff options
context:
space:
mode:
Diffstat (limited to 'net/url_request')
-rw-r--r--net/url_request/url_request_ftp_job.cc9
-rw-r--r--net/url_request/url_request_new_ftp_job.cc24
-rw-r--r--net/url_request/url_request_unittest.cc96
3 files changed, 124 insertions, 5 deletions
diff --git a/net/url_request/url_request_ftp_job.cc b/net/url_request/url_request_ftp_job.cc
index eca845a..bc7f085 100644
--- a/net/url_request/url_request_ftp_job.cc
+++ b/net/url_request/url_request_ftp_job.cc
@@ -138,7 +138,8 @@ void URLRequestFtpJob::SendRequest() {
username = WideToUTF8(server_auth_->username);
password = WideToUTF8(server_auth_->password);
request_->context()->ftp_auth_cache()->Add(request_->url().GetOrigin(),
- server_auth_.get());
+ server_auth_->username,
+ server_auth_->password);
} else {
if (request_->url().has_username()) {
username = request_->url().username();
@@ -183,13 +184,15 @@ void URLRequestFtpJob::OnIOComplete(const AsyncResult& result) {
GURL origin = request_->url().GetOrigin();
if (server_auth_ != NULL &&
server_auth_->state == net::AUTH_STATE_HAVE_AUTH) {
- request_->context()->ftp_auth_cache()->Remove(origin);
+ request_->context()->ftp_auth_cache()->Remove(origin,
+ server_auth_->username,
+ server_auth_->password);
} else {
server_auth_ = new net::AuthData();
}
server_auth_->state = net::AUTH_STATE_NEED_AUTH;
- scoped_refptr<net::AuthData> cached_auth =
+ net::FtpAuthCache::Entry* cached_auth =
request_->context()->ftp_auth_cache()->Lookup(origin);
if (cached_auth) {
diff --git a/net/url_request/url_request_new_ftp_job.cc b/net/url_request/url_request_new_ftp_job.cc
index 7755503..83d1a9c 100644
--- a/net/url_request/url_request_new_ftp_job.cc
+++ b/net/url_request/url_request_new_ftp_job.cc
@@ -136,6 +136,9 @@ void URLRequestNewFtpJob::SetAuth(const std::wstring& username,
server_auth_->username = username;
server_auth_->password = password;
+ request_->context()->ftp_auth_cache()->Add(request_->url().GetOrigin(),
+ username, password);
+
RestartTransactionWithAuth();
}
@@ -361,9 +364,26 @@ void URLRequestNewFtpJob::OnStartCompleted(int result) {
if (result == net::OK) {
NotifyHeadersComplete();
} else if (transaction_->GetResponseInfo()->needs_auth) {
- server_auth_ = new net::AuthData();
+ GURL origin = request_->url().GetOrigin();
+ if (server_auth_ && server_auth_->state == net::AUTH_STATE_HAVE_AUTH) {
+ request_->context()->ftp_auth_cache()->Remove(origin,
+ server_auth_->username,
+ server_auth_->password);
+ } else if (!server_auth_) {
+ server_auth_ = new net::AuthData();
+ }
server_auth_->state = net::AUTH_STATE_NEED_AUTH;
- NotifyHeadersComplete();
+
+ net::FtpAuthCache::Entry* cached_auth =
+ request_->context()->ftp_auth_cache()->Lookup(origin);
+
+ if (cached_auth) {
+ // Retry using cached auth data.
+ SetAuth(cached_auth->username, cached_auth->password);
+ } else {
+ // Prompt for a username/password.
+ NotifyHeadersComplete();
+ }
} else {
NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result));
}
diff --git a/net/url_request/url_request_unittest.cc b/net/url_request/url_request_unittest.cc
index d82e085..0926c30 100644
--- a/net/url_request/url_request_unittest.cc
+++ b/net/url_request/url_request_unittest.cc
@@ -2123,3 +2123,99 @@ TEST_F(URLRequestTestFTP, FTPCheckWrongUserRestart) {
EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
}
}
+
+TEST_F(URLRequestTestFTP, FTPCacheURLCredentials) {
+ ASSERT_TRUE(NULL != server_.get());
+ FilePath app_path;
+ PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
+ app_path = app_path.AppendASCII("LICENSE");
+
+ scoped_ptr<TestDelegate> d(new TestDelegate);
+ {
+ // Pass correct login identity in the URL.
+ TestURLRequest r(server_->TestServerPage("/LICENSE",
+ "chrome", "chrome"),
+ d.get());
+ r.Start();
+ EXPECT_TRUE(r.is_pending());
+
+ MessageLoop::current()->Run();
+
+ int64 file_size = 0;
+ file_util::GetFileSize(app_path, &file_size);
+
+ EXPECT_FALSE(r.is_pending());
+ EXPECT_EQ(1, d->response_started_count());
+ EXPECT_FALSE(d->received_data_before_response());
+ EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
+ }
+
+ d.reset(new TestDelegate);
+ {
+ // This request should use cached identity from previous request.
+ TestURLRequest r(server_->TestServerPage("/LICENSE"), d.get());
+ r.Start();
+ EXPECT_TRUE(r.is_pending());
+
+ MessageLoop::current()->Run();
+
+ int64 file_size = 0;
+ file_util::GetFileSize(app_path, &file_size);
+
+ EXPECT_FALSE(r.is_pending());
+ EXPECT_EQ(1, d->response_started_count());
+ EXPECT_FALSE(d->received_data_before_response());
+ EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
+ }
+}
+
+TEST_F(URLRequestTestFTP, FTPCacheLoginBoxCredentials) {
+ ASSERT_TRUE(NULL != server_.get());
+ FilePath app_path;
+ PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
+ app_path = app_path.AppendASCII("LICENSE");
+
+ scoped_ptr<TestDelegate> d(new TestDelegate);
+ // Set correct login credentials. The delegate will be asked for them when
+ // the initial login with wrong credentials will fail.
+ d->set_username(L"chrome");
+ d->set_password(L"chrome");
+ {
+ TestURLRequest r(server_->TestServerPage("/LICENSE",
+ "chrome", "wrong_password"),
+ d.get());
+ r.Start();
+ EXPECT_TRUE(r.is_pending());
+
+ MessageLoop::current()->Run();
+
+ int64 file_size = 0;
+ file_util::GetFileSize(app_path, &file_size);
+
+ EXPECT_FALSE(r.is_pending());
+ EXPECT_EQ(1, d->response_started_count());
+ EXPECT_FALSE(d->received_data_before_response());
+ EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
+ }
+
+ // Use a new delegate without explicit credentials. The cached ones should be
+ // used.
+ d.reset(new TestDelegate);
+ {
+ // Don't pass wrong credentials in the URL, they would override valid cached
+ // ones.
+ TestURLRequest r(server_->TestServerPage("/LICENSE"), d.get());
+ r.Start();
+ EXPECT_TRUE(r.is_pending());
+
+ MessageLoop::current()->Run();
+
+ int64 file_size = 0;
+ file_util::GetFileSize(app_path, &file_size);
+
+ EXPECT_FALSE(r.is_pending());
+ EXPECT_EQ(1, d->response_started_count());
+ EXPECT_FALSE(d->received_data_before_response());
+ EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
+ }
+}