summaryrefslogtreecommitdiffstats
path: root/net/ftp
diff options
context:
space:
mode:
authorericroman@google.com <ericroman@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-01-15 06:43:48 +0000
committerericroman@google.com <ericroman@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-01-15 06:43:48 +0000
commit515838ce76cb8bec7f51f6143cac74f113e247ad (patch)
tree71b70d594974310b35d1c8f7843aeda5717c044b /net/ftp
parent92352a66515fd9a01f538529356ad45870109f28 (diff)
downloadchromium_src-515838ce76cb8bec7f51f6143cac74f113e247ad.zip
chromium_src-515838ce76cb8bec7f51f6143cac74f113e247ad.tar.gz
chromium_src-515838ce76cb8bec7f51f6143cac74f113e247ad.tar.bz2
post-winhttp cleanup: refactor net/base/auth_cache into net/ftp/ftp_auth_cache.
Also moves AuthCache::HttpKey() --> GetSignonRealmKey(). Review URL: http://codereview.chromium.org/18218 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@8085 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/ftp')
-rw-r--r--net/ftp/ftp_auth_cache.cc35
-rw-r--r--net/ftp/ftp_auth_cache.h58
-rw-r--r--net/ftp/ftp_auth_cache_unittest.cc70
-rw-r--r--net/ftp/ftp_network_layer.cc2
-rw-r--r--net/ftp/ftp_network_layer.h2
-rw-r--r--net/ftp/ftp_network_session.h6
-rw-r--r--net/ftp/ftp_transaction_factory.h4
7 files changed, 170 insertions, 7 deletions
diff --git a/net/ftp/ftp_auth_cache.cc b/net/ftp/ftp_auth_cache.cc
new file mode 100644
index 0000000..8f1d18a
--- /dev/null
+++ b/net/ftp/ftp_auth_cache.cc
@@ -0,0 +1,35 @@
+// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/ftp/ftp_auth_cache.h"
+
+#include "base/string_util.h"
+#include "googleurl/src/gurl.h"
+
+namespace net {
+
+AuthData* FtpAuthCache::Lookup(const GURL& origin) {
+ AuthCacheMap::iterator iter = cache_.find(MakeKey(origin));
+ return (iter == cache_.end()) ? NULL : iter->second;
+}
+
+void FtpAuthCache::Add(const GURL& origin, AuthData* value) {
+ cache_[MakeKey(origin)] = value;
+
+ // TODO(eroman): enforce a maximum number of entries.
+}
+
+void FtpAuthCache::Remove(const GURL& origin) {
+ cache_.erase(MakeKey(origin));
+}
+
+// static
+FtpAuthCache::AuthCacheKey FtpAuthCache::MakeKey(const GURL& origin) {
+ DCHECK(origin.SchemeIs("ftp"));
+ DCHECK(origin.GetOrigin() == origin);
+ return origin.spec();
+}
+
+} // namespace net
+
diff --git a/net/ftp/ftp_auth_cache.h b/net/ftp/ftp_auth_cache.h
new file mode 100644
index 0000000..752842c
--- /dev/null
+++ b/net/ftp/ftp_auth_cache.h
@@ -0,0 +1,58 @@
+// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef NET_FTP_FTP_AUTH_CACHE_H_
+#define NET_FTP_FTP_AUTH_CACHE_H_
+
+#include <string>
+#include <map>
+
+#include "net/base/auth.h"
+
+class GURL;
+
+namespace net {
+
+// The FtpAuthCache class is a simple cache structure to store authentication
+// information for ftp. Provides lookup, insertion, and deletion of entries.
+// The parameter for doing lookups, insertions, and deletions is a GURL of the
+// server's address (not a full URL with path, since FTP auth isn't per path).
+// For example:
+// GURL("ftp://myserver") -- OK (implied port of 21)
+// GURL("ftp://myserver:21") -- OK
+// GURL("ftp://myserver/PATH") -- WRONG, paths not allowed
+class FtpAuthCache {
+ public:
+ FtpAuthCache() {}
+ ~FtpAuthCache() {}
+
+ // Check if we have authentication data for ftp server at |origin|.
+ // Returns the address of corresponding AuthData object (if found) or NULL
+ // (if not found).
+ AuthData* Lookup(const GURL& origin);
+
+ // Add an entry for |origin| to the cache. If there is already an
+ // entry for |origin|, it will be overwritten. Both parameters are IN only.
+ void Add(const GURL& origin, AuthData* value);
+
+ // Remove the entry for |origin| from the cache, if one exists.
+ void Remove(const GURL& origin);
+
+ private:
+ typedef std::string AuthCacheKey;
+ typedef scoped_refptr<AuthData> AuthCacheValue;
+ typedef std::map<AuthCacheKey,AuthCacheValue> AuthCacheMap;
+
+ // Get the key in hash table |cache_| where entries for ftp server |origin|
+ // should be saved.
+ static AuthCacheKey MakeKey(const GURL& origin);
+
+ // internal representation of cache, an STL map.
+ AuthCacheMap cache_;
+};
+
+} // namespace net
+
+#endif // NET_FTP_FTP_AUTH_CACHE_H_
+
diff --git a/net/ftp/ftp_auth_cache_unittest.cc b/net/ftp/ftp_auth_cache_unittest.cc
new file mode 100644
index 0000000..1164540
--- /dev/null
+++ b/net/ftp/ftp_auth_cache_unittest.cc
@@ -0,0 +1,70 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "googleurl/src/gurl.h"
+#include "net/ftp/ftp_auth_cache.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+using net::AuthData;
+using net::FtpAuthCache;
+
+TEST(FtpAuthCacheTest, LookupAddRemove) {
+ FtpAuthCache cache;
+
+ GURL origin1("ftp://foo1");
+ scoped_refptr<AuthData> data1(new AuthData());
+
+ GURL origin2("ftp://foo2");
+ scoped_refptr<AuthData> data2(new AuthData());
+
+ GURL origin3("ftp://foo3");
+ scoped_refptr<AuthData> data3(new AuthData());
+
+ // Lookup non-existent entry.
+ EXPECT_EQ(NULL, cache.Lookup(origin1));
+
+ // Add entry for origin1.
+ cache.Add(origin1, data1.get());
+ EXPECT_EQ(data1.get(), cache.Lookup(origin1));
+
+ // Add an entry for origin2.
+ cache.Add(origin2, data2.get());
+ EXPECT_EQ(data1.get(), cache.Lookup(origin1));
+ EXPECT_EQ(data2.get(), cache.Lookup(origin2));
+
+ // Overwrite the entry for origin1.
+ cache.Add(origin1, data3.get());
+ EXPECT_EQ(data3.get(), cache.Lookup(origin1));
+ EXPECT_EQ(data2.get(), cache.Lookup(origin2));
+
+ // Remove entry of origin1.
+ cache.Remove(origin1);
+ EXPECT_EQ(NULL, cache.Lookup(origin1));
+ EXPECT_EQ(data2.get(), cache.Lookup(origin2));
+
+ // Remove non-existent entry
+ cache.Remove(origin1);
+ EXPECT_EQ(NULL, cache.Lookup(origin1));
+ EXPECT_EQ(data2.get(), cache.Lookup(origin2));
+}
+
+// Check that if the origin differs only by port number, it is considered
+// a separate origin.
+TEST(FtpAuthCacheTest, LookupWithPort) {
+ FtpAuthCache cache;
+
+ GURL origin1("ftp://foo:80");
+ scoped_refptr<AuthData> data1(new AuthData());
+
+ GURL origin2("ftp://foo:21");
+ scoped_refptr<AuthData> data2(new AuthData());
+
+ cache.Add(origin1, data1.get());
+ cache.Add(origin2, data2.get());
+
+ EXPECT_EQ(data1.get(), cache.Lookup(origin1));
+ EXPECT_EQ(data2.get(), cache.Lookup(origin2));
+}
+
+
diff --git a/net/ftp/ftp_network_layer.cc b/net/ftp/ftp_network_layer.cc
index ea21b4f..06884ce 100644
--- a/net/ftp/ftp_network_layer.cc
+++ b/net/ftp/ftp_network_layer.cc
@@ -26,7 +26,7 @@ FtpTransaction* FtpNetworkLayer::CreateTransaction() {
session_, ClientSocketFactory::GetDefaultFactory());
}
-AuthCache* FtpNetworkLayer::GetAuthCache() {
+FtpAuthCache* FtpNetworkLayer::GetAuthCache() {
return session_->auth_cache();
}
diff --git a/net/ftp/ftp_network_layer.h b/net/ftp/ftp_network_layer.h
index 0f071b9..fa5c430 100644
--- a/net/ftp/ftp_network_layer.h
+++ b/net/ftp/ftp_network_layer.h
@@ -19,7 +19,7 @@ class FtpNetworkLayer : public FtpTransactionFactory {
// FtpTransactionFactory methods:
virtual FtpTransaction* CreateTransaction();
- virtual AuthCache* GetAuthCache();
+ virtual FtpAuthCache* GetAuthCache();
virtual void Suspend(bool suspend);
private:
diff --git a/net/ftp/ftp_network_session.h b/net/ftp/ftp_network_session.h
index 83a1cc7..13ab216 100644
--- a/net/ftp/ftp_network_session.h
+++ b/net/ftp/ftp_network_session.h
@@ -6,7 +6,7 @@
#define NET_FTP_FTP_NETWORK_SESSION_H_
#include "base/ref_counted.h"
-#include "net/base/auth_cache.h"
+#include "net/ftp/ftp_auth_cache.h"
namespace net {
@@ -15,10 +15,10 @@ class FtpNetworkSession : public base::RefCounted<FtpNetworkSession> {
public:
FtpNetworkSession() {}
- AuthCache* auth_cache() { return &auth_cache_; }
+ FtpAuthCache* auth_cache() { return &auth_cache_; }
private:
- AuthCache auth_cache_;
+ FtpAuthCache auth_cache_;
};
} // namespace net
diff --git a/net/ftp/ftp_transaction_factory.h b/net/ftp/ftp_transaction_factory.h
index 67af645..f4cc53a 100644
--- a/net/ftp/ftp_transaction_factory.h
+++ b/net/ftp/ftp_transaction_factory.h
@@ -7,7 +7,7 @@
namespace net {
-class AuthCache;
+class FtpAuthCache;
class FtpTransaction;
// An interface to a class that can create FtpTransaction objects.
@@ -19,7 +19,7 @@ class FtpTransactionFactory {
virtual FtpTransaction* CreateTransaction() = 0;
// Returns the associated FTP auth cache if any (may be NULL).
- virtual AuthCache* GetAuthCache() = 0;
+ virtual FtpAuthCache* GetAuthCache() = 0;
// Suspends the creation of new transactions. If |suspend| is false, creation
// of new transactions is resumed.