| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_auth_cache.h" | 5 #include "net/http/http_auth_cache.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 | 9 |
| 10 namespace { | 10 namespace { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 | 41 |
| 42 // Debug helper to check that |origin| arguments are properly formed. | 42 // Debug helper to check that |origin| arguments are properly formed. |
| 43 void CheckOriginIsValid(const GURL& origin) { | 43 void CheckOriginIsValid(const GURL& origin) { |
| 44 DCHECK(origin.is_valid()); | 44 DCHECK(origin.is_valid()); |
| 45 DCHECK(origin.SchemeIs("http") || origin.SchemeIs("https")); | 45 DCHECK(origin.SchemeIs("http") || origin.SchemeIs("https")); |
| 46 DCHECK(origin.GetOrigin() == origin); | 46 DCHECK(origin.GetOrigin() == origin); |
| 47 } | 47 } |
| 48 | 48 |
| 49 // Functor used by remove_if. | 49 // Functor used by remove_if. |
| 50 struct IsEnclosedBy { | 50 struct IsEnclosedBy { |
| 51 IsEnclosedBy(const std::string& path) : path(path) { } | 51 explicit IsEnclosedBy(const std::string& path) : path(path) { } |
| 52 bool operator() (const std::string& x) { | 52 bool operator() (const std::string& x) { |
| 53 return IsEnclosingPath(path, x); | 53 return IsEnclosingPath(path, x); |
| 54 } | 54 } |
| 55 const std::string& path; | 55 const std::string& path; |
| 56 }; | 56 }; |
| 57 | 57 |
| 58 } // namespace | 58 } // namespace |
| 59 | 59 |
| 60 namespace net { | 60 namespace net { |
| 61 | 61 |
| 62 // Performance: O(n), where n is the number of realm entries. | 62 // Performance: O(n), where n is the number of realm entries. |
| 63 HttpAuthCache::Entry* HttpAuthCache::Lookup(const GURL& origin, | 63 HttpAuthCache::Entry* HttpAuthCache::Lookup(const GURL& origin, |
| 64 const std::string& realm, | 64 const std::string& realm, |
| 65 const std::string& scheme) { | 65 const std::string& scheme) { |
| 66 CheckOriginIsValid(origin); | 66 CheckOriginIsValid(origin); |
| 67 | 67 |
| 68 // Linear scan through the realm entries. | 68 // Linear scan through the realm entries. |
| 69 for (EntryList::iterator it = entries_.begin(); it != entries_.end(); ++it) { | 69 for (EntryList::iterator it = entries_.begin(); it != entries_.end(); ++it) { |
| 70 if (it->origin() == origin && it->realm() == realm && | 70 if (it->origin() == origin && it->realm() == realm && |
| 71 it->scheme() == scheme) | 71 it->scheme() == scheme) |
| 72 return &(*it); | 72 return &(*it); |
| 73 } | 73 } |
| 74 return NULL; // No realm entry found. | 74 return NULL; // No realm entry found. |
| 75 } | 75 } |
| 76 | 76 |
| 77 // Performance: O(n*m), where n is the number of realm entries, m is the number | 77 // Performance: O(n*m), where n is the number of realm entries, m is the number |
| 78 // of path entries per realm. Both n amd m are expected to be small; m is | 78 // of path entries per realm. Both n amd m are expected to be small; m is |
| 79 // kept small because AddPath() only keeps the shallowest entry. | 79 // kept small because AddPath() only keeps the shallowest entry. |
| 80 HttpAuthCache::Entry* HttpAuthCache::LookupByPath(const GURL& origin, | 80 HttpAuthCache::Entry* HttpAuthCache::LookupByPath(const GURL& origin, |
| 81 const std::string& path) { | 81 const std::string& path) { |
| 82 CheckOriginIsValid(origin); | 82 CheckOriginIsValid(origin); |
| 83 CheckPathIsValid(path); | 83 CheckPathIsValid(path); |
| 84 | 84 |
| 85 // RFC 2617 section 2: | 85 // RFC 2617 section 2: |
| 86 // A client SHOULD assume that all paths at or deeper than the depth of | 86 // A client SHOULD assume that all paths at or deeper than the depth of |
| 87 // the last symbolic element in the path field of the Request-URI also are | 87 // the last symbolic element in the path field of the Request-URI also are |
| 88 // within the protection space ... | 88 // within the protection space ... |
| 89 std::string parent_dir = GetParentDirectory(path); | 89 std::string parent_dir = GetParentDirectory(path); |
| 90 | 90 |
| 91 // Linear scan through the realm entries. | 91 // Linear scan through the realm entries. |
| 92 for (EntryList::iterator it = entries_.begin(); it != entries_.end(); ++it) { | 92 for (EntryList::iterator it = entries_.begin(); it != entries_.end(); ++it) { |
| 93 if (it->origin() == origin && it->HasEnclosingPath(parent_dir)) | 93 if (it->origin() == origin && it->HasEnclosingPath(parent_dir)) |
| 94 return &(*it); | 94 return &(*it); |
| 95 } | 95 } |
| 96 return NULL; // No entry found. | 96 return NULL; // No entry found. |
| 97 } | 97 } |
| 98 | 98 |
| 99 HttpAuthCache::Entry* HttpAuthCache::Add(const GURL& origin, | 99 HttpAuthCache::Entry* HttpAuthCache::Add(const GURL& origin, |
| 100 const std::string& realm, | 100 const std::string& realm, |
| 101 const std::string& scheme, | 101 const std::string& scheme, |
| 102 const std::string& auth_challenge, | 102 const std::string& auth_challenge, |
| 103 const std::wstring& username, | 103 const string16& username, |
| 104 const std::wstring& password, | 104 const string16& password, |
| 105 const std::string& path) { | 105 const std::string& path) { |
| 106 CheckOriginIsValid(origin); | 106 CheckOriginIsValid(origin); |
| 107 CheckPathIsValid(path); | 107 CheckPathIsValid(path); |
| 108 | 108 |
| 109 // Check for existing entry (we will re-use it if present). | 109 // Check for existing entry (we will re-use it if present). |
| 110 HttpAuthCache::Entry* entry = Lookup(origin, realm, scheme); | 110 HttpAuthCache::Entry* entry = Lookup(origin, realm, scheme); |
| 111 if (!entry) { | 111 if (!entry) { |
| 112 // Failsafe to prevent unbounded memory growth of the cache. | 112 // Failsafe to prevent unbounded memory growth of the cache. |
| 113 if (entries_.size() >= kMaxNumRealmEntries) { | 113 if (entries_.size() >= kMaxNumRealmEntries) { |
| 114 LOG(WARNING) << "Num auth cache entries reached limit -- evicting"; | 114 LOG(WARNING) << "Num auth cache entries reached limit -- evicting"; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 ++it) { | 158 ++it) { |
| 159 if (IsEnclosingPath(*it, dir)) | 159 if (IsEnclosingPath(*it, dir)) |
| 160 return true; | 160 return true; |
| 161 } | 161 } |
| 162 return false; | 162 return false; |
| 163 } | 163 } |
| 164 | 164 |
| 165 bool HttpAuthCache::Remove(const GURL& origin, | 165 bool HttpAuthCache::Remove(const GURL& origin, |
| 166 const std::string& realm, | 166 const std::string& realm, |
| 167 const std::string& scheme, | 167 const std::string& scheme, |
| 168 const std::wstring& username, | 168 const string16& username, |
| 169 const std::wstring& password) { | 169 const string16& password) { |
| 170 for (EntryList::iterator it = entries_.begin(); it != entries_.end(); ++it) { | 170 for (EntryList::iterator it = entries_.begin(); it != entries_.end(); ++it) { |
| 171 if (it->origin() == origin && it->realm() == realm && | 171 if (it->origin() == origin && it->realm() == realm && |
| 172 it->scheme() == scheme) { | 172 it->scheme() == scheme) { |
| 173 if (username == it->username() && password == it->password()) { | 173 if (username == it->username() && password == it->password()) { |
| 174 entries_.erase(it); | 174 entries_.erase(it); |
| 175 return true; | 175 return true; |
| 176 } | 176 } |
| 177 return false; | 177 return false; |
| 178 } | 178 } |
| 179 } | 179 } |
| 180 return false; | 180 return false; |
| 181 } | 181 } |
| 182 | 182 |
| 183 } // namespace net | 183 } // namespace net |
| OLD | NEW |