| 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/string_util.h" | 8 #include "base/string_util.h" |
| 9 | 9 |
| 10 namespace { | 10 namespace { |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 best_match = &(*it); | 105 best_match = &(*it); |
| 106 } | 106 } |
| 107 } | 107 } |
| 108 return best_match; | 108 return best_match; |
| 109 } | 109 } |
| 110 | 110 |
| 111 HttpAuthCache::Entry* HttpAuthCache::Add(const GURL& origin, | 111 HttpAuthCache::Entry* HttpAuthCache::Add(const GURL& origin, |
| 112 const std::string& realm, | 112 const std::string& realm, |
| 113 HttpAuth::Scheme scheme, | 113 HttpAuth::Scheme scheme, |
| 114 const std::string& auth_challenge, | 114 const std::string& auth_challenge, |
| 115 const string16& username, | 115 const AuthCredentials& credentials, |
| 116 const string16& password, | |
| 117 const std::string& path) { | 116 const std::string& path) { |
| 118 CheckOriginIsValid(origin); | 117 CheckOriginIsValid(origin); |
| 119 CheckPathIsValid(path); | 118 CheckPathIsValid(path); |
| 120 | 119 |
| 121 // Check for existing entry (we will re-use it if present). | 120 // Check for existing entry (we will re-use it if present). |
| 122 HttpAuthCache::Entry* entry = Lookup(origin, realm, scheme); | 121 HttpAuthCache::Entry* entry = Lookup(origin, realm, scheme); |
| 123 if (!entry) { | 122 if (!entry) { |
| 124 // Failsafe to prevent unbounded memory growth of the cache. | 123 // Failsafe to prevent unbounded memory growth of the cache. |
| 125 if (entries_.size() >= kMaxNumRealmEntries) { | 124 if (entries_.size() >= kMaxNumRealmEntries) { |
| 126 LOG(WARNING) << "Num auth cache entries reached limit -- evicting"; | 125 LOG(WARNING) << "Num auth cache entries reached limit -- evicting"; |
| 127 entries_.pop_back(); | 126 entries_.pop_back(); |
| 128 } | 127 } |
| 129 | 128 |
| 130 entries_.push_front(Entry()); | 129 entries_.push_front(Entry()); |
| 131 entry = &entries_.front(); | 130 entry = &entries_.front(); |
| 132 entry->origin_ = origin; | 131 entry->origin_ = origin; |
| 133 entry->realm_ = realm; | 132 entry->realm_ = realm; |
| 134 entry->scheme_ = scheme; | 133 entry->scheme_ = scheme; |
| 135 } | 134 } |
| 136 DCHECK_EQ(origin, entry->origin_); | 135 DCHECK_EQ(origin, entry->origin_); |
| 137 DCHECK_EQ(realm, entry->realm_); | 136 DCHECK_EQ(realm, entry->realm_); |
| 138 DCHECK_EQ(scheme, entry->scheme_); | 137 DCHECK_EQ(scheme, entry->scheme_); |
| 139 | 138 |
| 140 entry->auth_challenge_ = auth_challenge; | 139 entry->auth_challenge_ = auth_challenge; |
| 141 entry->username_ = username; | 140 entry->credentials_ = credentials; |
| 142 entry->password_ = password; | |
| 143 entry->nonce_count_ = 1; | 141 entry->nonce_count_ = 1; |
| 144 entry->AddPath(path); | 142 entry->AddPath(path); |
| 145 | 143 |
| 146 return entry; | 144 return entry; |
| 147 } | 145 } |
| 148 | 146 |
| 149 HttpAuthCache::Entry::~Entry() { | 147 HttpAuthCache::Entry::~Entry() { |
| 150 } | 148 } |
| 151 | 149 |
| 152 void HttpAuthCache::Entry::UpdateStaleChallenge( | 150 void HttpAuthCache::Entry::UpdateStaleChallenge( |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 *path_len = it->length(); | 190 *path_len = it->length(); |
| 193 return true; | 191 return true; |
| 194 } | 192 } |
| 195 } | 193 } |
| 196 return false; | 194 return false; |
| 197 } | 195 } |
| 198 | 196 |
| 199 bool HttpAuthCache::Remove(const GURL& origin, | 197 bool HttpAuthCache::Remove(const GURL& origin, |
| 200 const std::string& realm, | 198 const std::string& realm, |
| 201 HttpAuth::Scheme scheme, | 199 HttpAuth::Scheme scheme, |
| 202 const string16& username, | 200 const AuthCredentials& credentials) { |
| 203 const string16& password) { | |
| 204 for (EntryList::iterator it = entries_.begin(); it != entries_.end(); ++it) { | 201 for (EntryList::iterator it = entries_.begin(); it != entries_.end(); ++it) { |
| 205 if (it->origin() == origin && it->realm() == realm && | 202 if (it->origin() == origin && it->realm() == realm && |
| 206 it->scheme() == scheme) { | 203 it->scheme() == scheme) { |
| 207 if (username == it->username() && password == it->password()) { | 204 if (credentials.Equals(it->credentials())) { |
| 208 entries_.erase(it); | 205 entries_.erase(it); |
| 209 return true; | 206 return true; |
| 210 } | 207 } |
| 211 return false; | 208 return false; |
| 212 } | 209 } |
| 213 } | 210 } |
| 214 return false; | 211 return false; |
| 215 } | 212 } |
| 216 | 213 |
| 217 bool HttpAuthCache::UpdateStaleChallenge(const GURL& origin, | 214 bool HttpAuthCache::UpdateStaleChallenge(const GURL& origin, |
| 218 const std::string& realm, | 215 const std::string& realm, |
| 219 HttpAuth::Scheme scheme, | 216 HttpAuth::Scheme scheme, |
| 220 const std::string& auth_challenge) { | 217 const std::string& auth_challenge) { |
| 221 HttpAuthCache::Entry* entry = Lookup(origin, realm, scheme); | 218 HttpAuthCache::Entry* entry = Lookup(origin, realm, scheme); |
| 222 if (!entry) | 219 if (!entry) |
| 223 return false; | 220 return false; |
| 224 entry->UpdateStaleChallenge(auth_challenge); | 221 entry->UpdateStaleChallenge(auth_challenge); |
| 225 return true; | 222 return true; |
| 226 } | 223 } |
| 227 | 224 |
| 228 void HttpAuthCache::UpdateAllFrom(const HttpAuthCache& other) { | 225 void HttpAuthCache::UpdateAllFrom(const HttpAuthCache& other) { |
| 229 for (EntryList::const_iterator it = other.entries_.begin(); | 226 for (EntryList::const_iterator it = other.entries_.begin(); |
| 230 it != other.entries_.end(); ++it) { | 227 it != other.entries_.end(); ++it) { |
| 231 // Add an Entry with one of the original entry's paths. | 228 // Add an Entry with one of the original entry's paths. |
| 232 DCHECK(it->paths_.size() > 0); | 229 DCHECK(it->paths_.size() > 0); |
| 233 Entry* entry = Add(it->origin(), it->realm(), it->scheme(), | 230 Entry* entry = Add(it->origin(), it->realm(), it->scheme(), |
| 234 it->auth_challenge(), it->username(), it->password(), | 231 it->auth_challenge(), it->credentials(), |
| 235 it->paths_.back()); | 232 it->paths_.back()); |
| 236 // Copy all other paths. | 233 // Copy all other paths. |
| 237 for (Entry::PathList::const_reverse_iterator it2 = ++it->paths_.rbegin(); | 234 for (Entry::PathList::const_reverse_iterator it2 = ++it->paths_.rbegin(); |
| 238 it2 != it->paths_.rend(); ++it2) | 235 it2 != it->paths_.rend(); ++it2) |
| 239 entry->AddPath(*it2); | 236 entry->AddPath(*it2); |
| 240 // Copy nonce count (for digest authentication). | 237 // Copy nonce count (for digest authentication). |
| 241 entry->nonce_count_ = it->nonce_count_; | 238 entry->nonce_count_ = it->nonce_count_; |
| 242 } | 239 } |
| 243 } | 240 } |
| 244 | 241 |
| 245 } // namespace net | 242 } // namespace net |
| OLD | NEW |