| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/metrics/histogram_macros.h" | 8 #include "base/metrics/histogram_macros.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 | 10 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 | 73 |
| 74 HttpAuthCache::HttpAuthCache() { | 74 HttpAuthCache::HttpAuthCache() { |
| 75 } | 75 } |
| 76 | 76 |
| 77 HttpAuthCache::~HttpAuthCache() { | 77 HttpAuthCache::~HttpAuthCache() { |
| 78 } | 78 } |
| 79 | 79 |
| 80 // Performance: O(n), where n is the number of realm entries. | 80 // Performance: O(n), where n is the number of realm entries. |
| 81 HttpAuthCache::Entry* HttpAuthCache::Lookup(const GURL& origin, | 81 HttpAuthCache::Entry* HttpAuthCache::Lookup(const GURL& origin, |
| 82 const std::string& realm, | 82 const std::string& realm, |
| 83 HttpAuth::Scheme scheme) { | 83 const std::string& scheme) { |
| 84 CheckOriginIsValid(origin); | 84 CheckOriginIsValid(origin); |
| 85 DCHECK(HttpAuth::IsValidNormalizedScheme(scheme)); |
| 85 | 86 |
| 86 int entries_examined = 0; | 87 int entries_examined = 0; |
| 87 // Linear scan through the realm entries. | 88 // Linear scan through the realm entries. |
| 88 for (EntryList::iterator it = entries_.begin(); it != entries_.end(); ++it) { | 89 for (EntryList::iterator it = entries_.begin(); it != entries_.end(); ++it) { |
| 89 ++entries_examined; | 90 ++entries_examined; |
| 90 if (it->origin() == origin && it->realm() == realm && | 91 if (it->origin() == origin && it->realm() == realm && |
| 91 it->scheme() == scheme) { | 92 it->scheme() == scheme) { |
| 92 it->last_use_time_ = base::TimeTicks::Now(); | 93 it->last_use_time_ = base::TimeTicks::Now(); |
| 93 RecordLookupPosition(entries_examined); | 94 RecordLookupPosition(entries_examined); |
| 94 return &(*it); | 95 return &(*it); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 } | 129 } |
| 129 } | 130 } |
| 130 if (best_match) | 131 if (best_match) |
| 131 best_match->last_use_time_ = base::TimeTicks::Now(); | 132 best_match->last_use_time_ = base::TimeTicks::Now(); |
| 132 RecordLookupByPathPosition(best_match_position); | 133 RecordLookupByPathPosition(best_match_position); |
| 133 return best_match; | 134 return best_match; |
| 134 } | 135 } |
| 135 | 136 |
| 136 HttpAuthCache::Entry* HttpAuthCache::Add(const GURL& origin, | 137 HttpAuthCache::Entry* HttpAuthCache::Add(const GURL& origin, |
| 137 const std::string& realm, | 138 const std::string& realm, |
| 138 HttpAuth::Scheme scheme, | 139 const std::string& scheme, |
| 139 const std::string& auth_challenge, | 140 const std::string& auth_challenge, |
| 140 const AuthCredentials& credentials, | 141 const AuthCredentials& credentials, |
| 141 const std::string& path) { | 142 const std::string& path) { |
| 142 CheckOriginIsValid(origin); | 143 CheckOriginIsValid(origin); |
| 143 CheckPathIsValid(path); | 144 CheckPathIsValid(path); |
| 145 DCHECK(HttpAuth::IsValidNormalizedScheme(scheme)); |
| 144 | 146 |
| 145 base::TimeTicks now = base::TimeTicks::Now(); | 147 base::TimeTicks now = base::TimeTicks::Now(); |
| 146 | 148 |
| 147 // Check for existing entry (we will re-use it if present). | 149 // Check for existing entry (we will re-use it if present). |
| 148 HttpAuthCache::Entry* entry = Lookup(origin, realm, scheme); | 150 HttpAuthCache::Entry* entry = Lookup(origin, realm, scheme); |
| 149 if (!entry) { | 151 if (!entry) { |
| 150 bool evicted = false; | 152 bool evicted = false; |
| 151 // Failsafe to prevent unbounded memory growth of the cache. | 153 // Failsafe to prevent unbounded memory growth of the cache. |
| 152 if (entries_.size() >= kMaxNumRealmEntries) { | 154 if (entries_.size() >= kMaxNumRealmEntries) { |
| 153 LOG(WARNING) << "Num auth cache entries reached limit -- evicting"; | 155 LOG(WARNING) << "Num auth cache entries reached limit -- evicting"; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 182 | 184 |
| 183 HttpAuthCache::Entry::~Entry() { | 185 HttpAuthCache::Entry::~Entry() { |
| 184 } | 186 } |
| 185 | 187 |
| 186 void HttpAuthCache::Entry::UpdateStaleChallenge( | 188 void HttpAuthCache::Entry::UpdateStaleChallenge( |
| 187 const std::string& auth_challenge) { | 189 const std::string& auth_challenge) { |
| 188 auth_challenge_ = auth_challenge; | 190 auth_challenge_ = auth_challenge; |
| 189 nonce_count_ = 1; | 191 nonce_count_ = 1; |
| 190 } | 192 } |
| 191 | 193 |
| 192 HttpAuthCache::Entry::Entry() | 194 HttpAuthCache::Entry::Entry() : nonce_count_(0) {} |
| 193 : scheme_(HttpAuth::AUTH_SCHEME_MAX), | |
| 194 nonce_count_(0) { | |
| 195 } | |
| 196 | 195 |
| 197 void HttpAuthCache::Entry::AddPath(const std::string& path) { | 196 void HttpAuthCache::Entry::AddPath(const std::string& path) { |
| 198 std::string parent_dir = GetParentDirectory(path); | 197 std::string parent_dir = GetParentDirectory(path); |
| 199 if (!HasEnclosingPath(parent_dir, NULL)) { | 198 if (!HasEnclosingPath(parent_dir, NULL)) { |
| 200 // Remove any entries that have been subsumed by the new entry. | 199 // Remove any entries that have been subsumed by the new entry. |
| 201 paths_.remove_if(IsEnclosedBy(parent_dir)); | 200 paths_.remove_if(IsEnclosedBy(parent_dir)); |
| 202 | 201 |
| 203 bool evicted = false; | 202 bool evicted = false; |
| 204 // Failsafe to prevent unbounded memory growth of the cache. | 203 // Failsafe to prevent unbounded memory growth of the cache. |
| 205 if (paths_.size() >= kMaxNumPathsPerRealmEntry) { | 204 if (paths_.size() >= kMaxNumPathsPerRealmEntry) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 228 if (path_len) | 227 if (path_len) |
| 229 *path_len = it->length(); | 228 *path_len = it->length(); |
| 230 return true; | 229 return true; |
| 231 } | 230 } |
| 232 } | 231 } |
| 233 return false; | 232 return false; |
| 234 } | 233 } |
| 235 | 234 |
| 236 bool HttpAuthCache::Remove(const GURL& origin, | 235 bool HttpAuthCache::Remove(const GURL& origin, |
| 237 const std::string& realm, | 236 const std::string& realm, |
| 238 HttpAuth::Scheme scheme, | 237 const std::string& scheme, |
| 239 const AuthCredentials& credentials) { | 238 const AuthCredentials& credentials) { |
| 239 DCHECK(HttpAuth::IsValidNormalizedScheme(scheme)); |
| 240 for (EntryList::iterator it = entries_.begin(); it != entries_.end(); ++it) { | 240 for (EntryList::iterator it = entries_.begin(); it != entries_.end(); ++it) { |
| 241 if (it->origin() == origin && it->realm() == realm && | 241 if (it->origin() == origin && it->realm() == realm && |
| 242 it->scheme() == scheme) { | 242 it->scheme() == scheme) { |
| 243 if (credentials.Equals(it->credentials())) { | 243 if (credentials.Equals(it->credentials())) { |
| 244 entries_.erase(it); | 244 entries_.erase(it); |
| 245 return true; | 245 return true; |
| 246 } | 246 } |
| 247 return false; | 247 return false; |
| 248 } | 248 } |
| 249 } | 249 } |
| 250 return false; | 250 return false; |
| 251 } | 251 } |
| 252 | 252 |
| 253 void HttpAuthCache::Clear() { | 253 void HttpAuthCache::Clear() { |
| 254 entries_.clear(); | 254 entries_.clear(); |
| 255 } | 255 } |
| 256 | 256 |
| 257 bool HttpAuthCache::UpdateStaleChallenge(const GURL& origin, | 257 bool HttpAuthCache::UpdateStaleChallenge(const GURL& origin, |
| 258 const std::string& realm, | 258 const std::string& realm, |
| 259 HttpAuth::Scheme scheme, | 259 const std::string& scheme, |
| 260 const std::string& auth_challenge) { | 260 const std::string& auth_challenge) { |
| 261 DCHECK(HttpAuth::IsValidNormalizedScheme(scheme)); |
| 261 HttpAuthCache::Entry* entry = Lookup(origin, realm, scheme); | 262 HttpAuthCache::Entry* entry = Lookup(origin, realm, scheme); |
| 262 if (!entry) | 263 if (!entry) |
| 263 return false; | 264 return false; |
| 264 entry->UpdateStaleChallenge(auth_challenge); | 265 entry->UpdateStaleChallenge(auth_challenge); |
| 265 entry->last_use_time_ = base::TimeTicks::Now(); | 266 entry->last_use_time_ = base::TimeTicks::Now(); |
| 266 return true; | 267 return true; |
| 267 } | 268 } |
| 268 | 269 |
| 269 void HttpAuthCache::UpdateAllFrom(const HttpAuthCache& other) { | 270 void HttpAuthCache::UpdateAllFrom(const HttpAuthCache& other) { |
| 270 for (EntryList::const_iterator it = other.entries_.begin(); | 271 for (EntryList::const_iterator it = other.entries_.begin(); |
| 271 it != other.entries_.end(); ++it) { | 272 it != other.entries_.end(); ++it) { |
| 272 // Add an Entry with one of the original entry's paths. | 273 // Add an Entry with one of the original entry's paths. |
| 273 DCHECK(it->paths_.size() > 0); | 274 DCHECK(it->paths_.size() > 0); |
| 274 Entry* entry = Add(it->origin(), it->realm(), it->scheme(), | 275 Entry* entry = Add(it->origin(), it->realm(), it->scheme(), |
| 275 it->auth_challenge(), it->credentials(), | 276 it->auth_challenge(), it->credentials(), |
| 276 it->paths_.back()); | 277 it->paths_.back()); |
| 277 // Copy all other paths. | 278 // Copy all other paths. |
| 278 for (Entry::PathList::const_reverse_iterator it2 = ++it->paths_.rbegin(); | 279 for (Entry::PathList::const_reverse_iterator it2 = ++it->paths_.rbegin(); |
| 279 it2 != it->paths_.rend(); ++it2) | 280 it2 != it->paths_.rend(); ++it2) |
| 280 entry->AddPath(*it2); | 281 entry->AddPath(*it2); |
| 281 // Copy nonce count (for digest authentication). | 282 // Copy nonce count (for digest authentication). |
| 282 entry->nonce_count_ = it->nonce_count_; | 283 entry->nonce_count_ = it->nonce_count_; |
| 283 } | 284 } |
| 284 } | 285 } |
| 285 | 286 |
| 286 } // namespace net | 287 } // namespace net |
| OLD | NEW |