OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "net/ssl/ssl_client_session_cache_openssl.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/time/clock.h" | |
11 #include "base/time/default_clock.h" | |
12 | |
13 namespace net { | |
14 | |
15 SSLClientSessionCacheOpenSSL::SSLClientSessionCacheOpenSSL(const Config& config) | |
16 : clock_(new base::DefaultClock), | |
17 config_(config), | |
18 cache_(config.max_entries), | |
19 lookups_since_flush_(0) { | |
20 } | |
21 | |
22 SSLClientSessionCacheOpenSSL::~SSLClientSessionCacheOpenSSL() { | |
23 // TODO(davidben): The session cache is currently a singleton, so it is | |
24 // destroyed on a different thread than the one it's created on. When | |
25 // https://crbug.com/458365 is fixed, this will no longer be an issue. | |
26 thread_checker_.DetachFromThread(); | |
27 | |
28 Flush(); | |
29 } | |
30 | |
31 size_t SSLClientSessionCacheOpenSSL::size() const { | |
32 return cache_.size(); | |
33 } | |
34 | |
35 SSL_SESSION* SSLClientSessionCacheOpenSSL::Lookup( | |
36 const std::string& cache_key) { | |
37 DCHECK(thread_checker_.CalledOnValidThread()); | |
38 | |
39 // Expire stale sessions. | |
40 lookups_since_flush_++; | |
41 if (lookups_since_flush_ >= config_.expiration_check_count) { | |
42 lookups_since_flush_ = 0; | |
43 FlushExpiredSessions(); | |
44 } | |
45 | |
46 CacheEntryMap::iterator iter = cache_.Get(cache_key); | |
47 if (iter == cache_.end()) | |
48 return nullptr; | |
49 if (iter->second->IsExpired(clock_->Now())) { | |
50 cache_.Erase(iter); | |
51 return nullptr; | |
52 } | |
53 return iter->second->session.get(); | |
54 } | |
55 | |
56 void SSLClientSessionCacheOpenSSL::Insert(const std::string& cache_key, | |
57 SSL_SESSION* session) { | |
58 DCHECK(thread_checker_.CalledOnValidThread()); | |
59 | |
60 // Make a new entry. | |
61 CacheEntry* entry = new CacheEntry; | |
62 entry->session.reset(SSL_SESSION_up_ref(session)); | |
63 entry->expiration = clock_->Now() + config_.timeout; | |
64 | |
65 // Takes ownership. | |
66 cache_.Put(cache_key, entry); | |
67 } | |
68 | |
69 void SSLClientSessionCacheOpenSSL::Flush() { | |
70 DCHECK(thread_checker_.CalledOnValidThread()); | |
71 | |
72 cache_.Clear(); | |
73 } | |
74 | |
75 void SSLClientSessionCacheOpenSSL::SetClockForTesting( | |
76 scoped_ptr<base::Clock> clock) { | |
77 DCHECK(thread_checker_.CalledOnValidThread()); | |
78 | |
79 clock_ = clock.Pass(); | |
80 } | |
81 | |
82 SSLClientSessionCacheOpenSSL::CacheEntry::CacheEntry() { | |
83 } | |
84 | |
85 SSLClientSessionCacheOpenSSL::CacheEntry::~CacheEntry() { | |
86 } | |
87 | |
88 bool SSLClientSessionCacheOpenSSL::CacheEntry::IsExpired(base::Time now) { | |
89 return now > expiration; | |
Ryan Sleevi
2015/04/02 06:53:15
I still can't help but feel like this is going to
davidben
2015/04/02 07:21:45
We could, but I'm not sure I see the difference. T
Ryan Sleevi
2015/04/02 07:46:55
I meant re-using a session beyond the timelimit co
davidben
2015/04/02 19:05:10
Ah, okay. Done.
| |
90 } | |
91 | |
92 void SSLClientSessionCacheOpenSSL::FlushExpiredSessions() { | |
93 base::Time now = clock_->Now(); | |
94 CacheEntryMap::iterator iter = cache_.begin(); | |
95 while (iter != cache_.end()) { | |
96 if (iter->second->IsExpired(now)) { | |
97 iter = cache_.Erase(iter); | |
98 } else { | |
99 ++iter; | |
100 } | |
101 } | |
102 } | |
103 | |
104 } // namespace net | |
OLD | NEW |